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 #include <memory>
29 
30 namespace android {
31 namespace hardware {
32 namespace graphics {
33 namespace composer {
34 namespace V2_2 {
35 namespace vts {
36 
37 using android::hardware::hidl_handle;
38 using common::V1_1::BufferUsage;
39 using common::V1_1::Dataspace;
40 using common::V1_1::PixelFormat;
41 using IMapper2_1 = mapper::V2_1::IMapper;
42 using Gralloc2_1 = mapper::V2_1::vts::Gralloc;
43 using renderengine::LayerSettings;
44 using V2_1::Display;
45 using V2_1::Layer;
46 using V2_1::vts::AccessRegion;
47 using V2_1::vts::TestCommandReader;
48 
49 static const IComposerClient::Color BLACK = {0, 0, 0, 0xff};
50 static const IComposerClient::Color RED = {0xff, 0, 0, 0xff};
51 static const IComposerClient::Color TRANSLUCENT_RED = {0xff, 0, 0, 0x33};
52 static const IComposerClient::Color GREEN = {0, 0xff, 0, 0xff};
53 static const IComposerClient::Color BLUE = {0, 0, 0xff, 0xff};
54 
55 class TestRenderEngine;
56 
57 class TestLayer {
58   public:
TestLayer(const std::shared_ptr<ComposerClient> & client,Display display)59     TestLayer(const std::shared_ptr<ComposerClient>& client, Display display)
60         : mLayer(client->createLayer(display, kBufferSlotCount)), mComposerClient(client) {}
61 
62     // ComposerClient will take care of destroying layers, no need to explicitly
63     // call destroyLayers here
~TestLayer()64     virtual ~TestLayer(){};
65 
66     virtual void write(const std::shared_ptr<CommandWriterBase>& writer);
67     virtual LayerSettings toRenderEngineLayerSettings();
68 
setDisplayFrame(IComposerClient::Rect frame)69     void setDisplayFrame(IComposerClient::Rect frame) { mDisplayFrame = frame; }
setSourceCrop(IComposerClient::FRect crop)70     void setSourceCrop(IComposerClient::FRect crop) { mSourceCrop = crop; }
setZOrder(uint32_t z)71     void setZOrder(uint32_t z) { mZOrder = z; }
72 
setSurfaceDamage(std::vector<IComposerClient::Rect> surfaceDamage)73     void setSurfaceDamage(std::vector<IComposerClient::Rect> surfaceDamage) {
74         mSurfaceDamage = surfaceDamage;
75     }
76 
setTransform(Transform transform)77     void setTransform(Transform transform) { mTransform = transform; }
setAlpha(float alpha)78     void setAlpha(float alpha) { mAlpha = alpha; }
setBlendMode(IComposerClient::BlendMode blendMode)79     void setBlendMode(IComposerClient::BlendMode blendMode) { mBlendMode = blendMode; }
80 
81     static constexpr uint32_t kBufferSlotCount = 64;
82 
83     IComposerClient::Rect mDisplayFrame = {0, 0, 0, 0};
84     uint32_t mZOrder = 0;
85     std::vector<IComposerClient::Rect> mSurfaceDamage;
86     Transform mTransform = static_cast<Transform>(0);
87     IComposerClient::FRect mSourceCrop = {0, 0, 0, 0};
88     float mAlpha = 1.0;
89     IComposerClient::BlendMode mBlendMode = IComposerClient::BlendMode::NONE;
90 
91   protected:
92     Layer mLayer;
93 
94   private:
95     std::shared_ptr<ComposerClient> const mComposerClient;
96 };
97 
98 class TestColorLayer : public TestLayer {
99   public:
TestColorLayer(const std::shared_ptr<ComposerClient> & client,Display display)100     TestColorLayer(const std::shared_ptr<ComposerClient>& client, Display display)
101         : TestLayer{client, display} {}
102 
103     void write(const std::shared_ptr<CommandWriterBase>& writer) override;
104 
105     LayerSettings toRenderEngineLayerSettings() override;
106 
setColor(IComposerClient::Color color)107     void setColor(IComposerClient::Color color) { mColor = color; }
108 
109   private:
110     IComposerClient::Color mColor = {0xff, 0xff, 0xff, 0xff};
111 };
112 
113 class TestBufferLayer : public TestLayer {
114   public:
115     TestBufferLayer(
116             const std::shared_ptr<ComposerClient>& client, const std::shared_ptr<Gralloc>& gralloc,
117             TestRenderEngine& renderEngine, Display display, int32_t width, int32_t height,
118             PixelFormat format,
119             IComposerClient::Composition composition = IComposerClient::Composition::DEVICE);
120 
121     void write(const std::shared_ptr<CommandWriterBase>& writer) override;
122 
123     LayerSettings toRenderEngineLayerSettings() override;
124 
125     void fillBuffer(std::vector<IComposerClient::Color> expectedColors);
126 
127     void setBuffer(std::vector<IComposerClient::Color> colors);
128 
129     void setDataspace(Dataspace dataspace, const std::shared_ptr<CommandWriterBase>& writer);
130 
131     void setToClientComposition(const std::shared_ptr<CommandWriterBase>& writer);
132 
133     uint32_t mWidth;
134     uint32_t mHeight;
135     uint32_t mLayerCount;
136     PixelFormat mFormat;
137     uint64_t mUsage;
138     AccessRegion mAccessRegion;
139     uint32_t mStride;
140 
141   protected:
142     IComposerClient::Composition mComposition;
143     std::shared_ptr<Gralloc> mGralloc;
144     TestRenderEngine& mRenderEngine;
145     int32_t mFillFence;
146     std::unique_ptr<Gralloc::NativeHandleWrapper> mBufferHandle;
147 };
148 
149 class ReadbackHelper {
150   public:
151     static std::string getColorModeString(ColorMode mode);
152 
153     static std::string getDataspaceString(Dataspace dataspace);
154 
155     static Dataspace getDataspaceForColorMode(ColorMode mode);
156 
157     static int32_t GetBytesPerPixel(PixelFormat pixelFormat);
158 
159     static void fillBuffer(int32_t width, int32_t height, uint32_t stride, void* bufferData,
160                            PixelFormat pixelFormat,
161                            std::vector<IComposerClient::Color> desiredPixelColors);
162 
163     static void clearColors(std::vector<IComposerClient::Color>& expectedColors, int32_t width,
164                             int32_t height, int32_t displayWidth);
165 
166     static void fillColorsArea(std::vector<IComposerClient::Color>& expectedColors, int32_t stride,
167                                IComposerClient::Rect area, IComposerClient::Color color);
168 
169     static bool readbackSupported(const PixelFormat& pixelFormat, const Dataspace& dataspace,
170                                   const Error error);
171 
172     static const std::vector<ColorMode> colorModes;
173     static const std::vector<Dataspace> dataspaces;
174 
175     static void compareColorBuffers(std::vector<IComposerClient::Color>& expectedColors,
176                                     void* bufferData, const uint32_t stride, const uint32_t width,
177                                     const uint32_t height, const PixelFormat pixelFormat);
178 };
179 
180 class ReadbackBuffer {
181   public:
182     ReadbackBuffer(Display display, const std::shared_ptr<ComposerClient>& client,
183                    const std::shared_ptr<Gralloc>& gralloc, uint32_t width, uint32_t height,
184                    PixelFormat pixelFormat, Dataspace dataspace);
185 
186     void setReadbackBuffer();
187 
188     void checkReadbackBuffer(std::vector<IComposerClient::Color> expectedColors);
189 
190   protected:
191     uint32_t mWidth;
192     uint32_t mHeight;
193     uint32_t mLayerCount;
194     PixelFormat mFormat;
195     uint64_t mUsage;
196     AccessRegion mAccessRegion;
197     uint32_t mStride;
198     std::unique_ptr<Gralloc::NativeHandleWrapper> mBufferHandle = nullptr;
199     PixelFormat mPixelFormat;
200     Dataspace mDataspace;
201     Display mDisplay;
202     std::shared_ptr<Gralloc> mGralloc;
203     std::shared_ptr<ComposerClient> mComposerClient;
204 };
205 
206 }  // namespace vts
207 }  // namespace V2_2
208 }  // namespace composer
209 }  // namespace graphics
210 }  // namespace hardware
211 }  // namespace android
212