1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android-base/unique_fd.h>
20 #include <android/hardware/graphics/composer/2.2/IComposerClient.h>
21 #include <composer-command-buffer/2.2/ComposerCommandBuffer.h>
22 #include <composer-vts/2.1/GraphicsComposerCallback.h>
23 #include <composer-vts/2.1/TestCommandReader.h>
24 #include <composer-vts/2.2/ComposerVts.h>
25 #include <mapper-vts/2.1/MapperVts.h>
26 #include <renderengine/RenderEngine.h>
27 
28 namespace android {
29 namespace hardware {
30 namespace graphics {
31 namespace composer {
32 namespace V2_2 {
33 namespace vts {
34 
35 using android::hardware::hidl_handle;
36 using common::V1_1::BufferUsage;
37 using common::V1_1::Dataspace;
38 using common::V1_1::PixelFormat;
39 using IMapper2_1 = mapper::V2_1::IMapper;
40 using Gralloc2_1 = mapper::V2_1::vts::Gralloc;
41 using renderengine::LayerSettings;
42 using V2_1::Display;
43 using V2_1::Layer;
44 using V2_1::vts::AccessRegion;
45 using V2_1::vts::TestCommandReader;
46 
47 static const IComposerClient::Color BLACK = {0, 0, 0, 0xff};
48 static const IComposerClient::Color RED = {0xff, 0, 0, 0xff};
49 static const IComposerClient::Color TRANSLUCENT_RED = {0xff, 0, 0, 0x33};
50 static const IComposerClient::Color GREEN = {0, 0xff, 0, 0xff};
51 static const IComposerClient::Color BLUE = {0, 0, 0xff, 0xff};
52 
53 class TestLayer {
54   public:
TestLayer(const std::shared_ptr<ComposerClient> & client,Display display)55     TestLayer(const std::shared_ptr<ComposerClient>& client, Display display)
56         : mLayer(client->createLayer(display, kBufferSlotCount)), mComposerClient(client) {}
57 
58     // ComposerClient will take care of destroying layers, no need to explicitly
59     // call destroyLayers here
~TestLayer()60     virtual ~TestLayer(){};
61 
62     virtual void write(const std::shared_ptr<CommandWriterBase>& writer);
63     virtual LayerSettings toRenderEngineLayerSettings();
64 
setDisplayFrame(IComposerClient::Rect frame)65     void setDisplayFrame(IComposerClient::Rect frame) { mDisplayFrame = frame; }
setSourceCrop(IComposerClient::FRect crop)66     void setSourceCrop(IComposerClient::FRect crop) { mSourceCrop = crop; }
setZOrder(uint32_t z)67     void setZOrder(uint32_t z) { mZOrder = z; }
68 
setSurfaceDamage(std::vector<IComposerClient::Rect> surfaceDamage)69     void setSurfaceDamage(std::vector<IComposerClient::Rect> surfaceDamage) {
70         mSurfaceDamage = surfaceDamage;
71     }
72 
setTransform(Transform transform)73     void setTransform(Transform transform) { mTransform = transform; }
setAlpha(float alpha)74     void setAlpha(float alpha) { mAlpha = alpha; }
setBlendMode(IComposerClient::BlendMode blendMode)75     void setBlendMode(IComposerClient::BlendMode blendMode) { mBlendMode = blendMode; }
76 
77     static constexpr uint32_t kBufferSlotCount = 64;
78 
79     IComposerClient::Rect mDisplayFrame = {0, 0, 0, 0};
80     uint32_t mZOrder = 0;
81     std::vector<IComposerClient::Rect> mSurfaceDamage;
82     Transform mTransform = static_cast<Transform>(0);
83     IComposerClient::FRect mSourceCrop = {0, 0, 0, 0};
84     float mAlpha = 1.0;
85     IComposerClient::BlendMode mBlendMode = IComposerClient::BlendMode::NONE;
86 
87   protected:
88     Layer mLayer;
89 
90   private:
91     std::shared_ptr<ComposerClient> const mComposerClient;
92 };
93 
94 class TestColorLayer : public TestLayer {
95   public:
TestColorLayer(const std::shared_ptr<ComposerClient> & client,Display display)96     TestColorLayer(const std::shared_ptr<ComposerClient>& client, Display display)
97         : TestLayer{client, display} {}
98 
99     void write(const std::shared_ptr<CommandWriterBase>& writer) override;
100 
101     LayerSettings toRenderEngineLayerSettings() override;
102 
setColor(IComposerClient::Color color)103     void setColor(IComposerClient::Color color) { mColor = color; }
104 
105   private:
106     IComposerClient::Color mColor = {0xff, 0xff, 0xff, 0xff};
107 };
108 
109 class TestBufferLayer : public TestLayer {
110   public:
111     TestBufferLayer(
112             const std::shared_ptr<ComposerClient>& client, const std::shared_ptr<Gralloc>& gralloc,
113             Display display, int32_t width, int32_t height, PixelFormat format,
114             IComposerClient::Composition composition = IComposerClient::Composition::DEVICE);
115 
116     ~TestBufferLayer();
117 
118     void write(const std::shared_ptr<CommandWriterBase>& writer) override;
119 
120     LayerSettings toRenderEngineLayerSettings() override;
121 
122     void fillBuffer(std::vector<IComposerClient::Color> expectedColors);
123 
124     void setBuffer(std::vector<IComposerClient::Color> colors);
125 
126     void setDataspace(Dataspace dataspace, const std::shared_ptr<CommandWriterBase>& writer);
127 
128     void setToClientComposition(const std::shared_ptr<CommandWriterBase>& writer);
129 
130     uint32_t mWidth;
131     uint32_t mHeight;
132     uint32_t mLayerCount;
133     PixelFormat mFormat;
134     uint64_t mUsage;
135     AccessRegion mAccessRegion;
136     uint32_t mStride;
137 
138   protected:
139     IComposerClient::Composition mComposition;
140     std::shared_ptr<Gralloc> mGralloc;
141     int32_t mFillFence;
142     const native_handle_t* mBufferHandle = nullptr;
143 };
144 
145 class ReadbackHelper {
146   public:
147     static std::string getColorModeString(ColorMode mode);
148 
149     static std::string getDataspaceString(Dataspace dataspace);
150 
151     static Dataspace getDataspaceForColorMode(ColorMode mode);
152 
153     static int32_t GetBytesPerPixel(PixelFormat pixelFormat);
154 
155     static void fillBuffer(int32_t width, int32_t height, uint32_t stride, void* bufferData,
156                            PixelFormat pixelFormat,
157                            std::vector<IComposerClient::Color> desiredPixelColors);
158 
159     static void clearColors(std::vector<IComposerClient::Color>& expectedColors, int32_t width,
160                             int32_t height, int32_t displayWidth);
161 
162     static void fillColorsArea(std::vector<IComposerClient::Color>& expectedColors, int32_t stride,
163                                IComposerClient::Rect area, IComposerClient::Color color);
164 
165     static bool readbackSupported(const PixelFormat& pixelFormat, const Dataspace& dataspace,
166                                   const Error error);
167 
168     static const std::vector<ColorMode> colorModes;
169     static const std::vector<Dataspace> dataspaces;
170 
171     static void compareColorBuffers(std::vector<IComposerClient::Color>& expectedColors,
172                                     void* bufferData, const uint32_t stride, const uint32_t width,
173                                     const uint32_t height, const PixelFormat pixelFormat);
174 };
175 
176 class ReadbackBuffer {
177   public:
178     ReadbackBuffer(Display display, const std::shared_ptr<ComposerClient>& client,
179                    const std::shared_ptr<Gralloc>& gralloc, uint32_t width, uint32_t height,
180                    PixelFormat pixelFormat, Dataspace dataspace);
181     ~ReadbackBuffer();
182 
183     void setReadbackBuffer();
184 
185     void checkReadbackBuffer(std::vector<IComposerClient::Color> expectedColors);
186 
187   protected:
188     uint32_t mWidth;
189     uint32_t mHeight;
190     uint32_t mLayerCount;
191     PixelFormat mFormat;
192     uint64_t mUsage;
193     AccessRegion mAccessRegion;
194     uint32_t mStride;
195     const native_handle_t* mBufferHandle = nullptr;
196     PixelFormat mPixelFormat;
197     Dataspace mDataspace;
198     Display mDisplay;
199     std::shared_ptr<Gralloc> mGralloc;
200     std::shared_ptr<ComposerClient> mComposerClient;
201 };
202 
203 }  // namespace vts
204 }  // namespace V2_2
205 }  // namespace composer
206 }  // namespace graphics
207 }  // namespace hardware
208 }  // namespace android
209