1 /**
2  * Copyright (c) 2021, 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 <aidl/android/hardware/graphics/composer3/IComposerClient.h>
20 #include <android-base/unique_fd.h>
21 #include <android/hardware/graphics/composer3/ComposerClientReader.h>
22 #include <android/hardware/graphics/composer3/ComposerClientWriter.h>
23 #include <renderengine/RenderEngine.h>
24 #include <ui/GraphicBuffer.h>
25 #include <memory>
26 #include "GraphicsComposerCallback.h"
27 #include "VtsComposerClient.h"
28 
29 namespace aidl::android::hardware::graphics::composer3::vts {
30 
31 using ::android::renderengine::LayerSettings;
32 using common::Dataspace;
33 using common::PixelFormat;
34 
35 static const Color BLACK = {0.0f, 0.0f, 0.0f, 1.0f};
36 static const Color RED = {1.0f, 0.0f, 0.0f, 1.0f};
37 // DIM_RED is 90% dimmed from RED in linear space
38 // hard-code as value 243 in 8-bit space here, as calculating it requires
39 // oetf(eotf(value) * .9), which is a complex non-linear transformation
40 static const Color DIM_RED = {243.f / 255.f, 0.0f, 0.0f, 1.0f};
41 static const Color TRANSLUCENT_RED = {1.0f, 0.0f, 0.0f, 0.3f};
42 static const Color GREEN = {0.0f, 1.0f, 0.0f, 1.0f};
43 static const Color BLUE = {0.0f, 0.0f, 1.0f, 1.0f};
44 static const Color WHITE = {1.0f, 1.0f, 1.0f, 1.0f};
45 static const Color LIGHT_RED = {0.5f, 0.0f, 0.0f, 1.0f};
46 static const Color LIGHT_GREEN = {0.0f, 0.5f, 0.0f, 1.0f};
47 static const Color LIGHT_BLUE = {0.0f, 0.0f, 0.5f, 1.0f};
48 
49 class TestRenderEngine;
50 
51 class TestLayer {
52   public:
TestLayer(const std::shared_ptr<VtsComposerClient> & client,int64_t display,ComposerClientWriter & writer)53     TestLayer(const std::shared_ptr<VtsComposerClient>& client, int64_t display,
54               ComposerClientWriter& writer)
55         : mDisplay(display) {
56         const auto& [status, layer] = client->createLayer(display, kBufferSlotCount, &writer);
57         EXPECT_TRUE(status.isOk());
58         mLayer = layer;
59     }
60 
61     // ComposerClient will take care of destroying layers, no need to explicitly
62     // call destroyLayers here
~TestLayer()63     virtual ~TestLayer(){};
64 
65     virtual void write(ComposerClientWriter& writer);
66     virtual LayerSettings toRenderEngineLayerSettings();
67 
setDisplayFrame(Rect frame)68     void setDisplayFrame(Rect frame) { mDisplayFrame = frame; }
setSourceCrop(FRect crop)69     void setSourceCrop(FRect crop) { mSourceCrop = crop; }
setZOrder(uint32_t z)70     void setZOrder(uint32_t z) { mZOrder = z; }
setWhitePointNits(float whitePointNits)71     void setWhitePointNits(float whitePointNits) { mWhitePointNits = whitePointNits; }
setBrightness(float brightness)72     void setBrightness(float brightness) { mBrightness = brightness; }
73 
setSurfaceDamage(std::vector<Rect> surfaceDamage)74     void setSurfaceDamage(std::vector<Rect> surfaceDamage) {
75         mSurfaceDamage = std::move(surfaceDamage);
76     }
77 
setDataspace(Dataspace dataspace)78     void setDataspace(Dataspace dataspace) { mDataspace = dataspace; }
79 
setTransform(Transform transform)80     void setTransform(Transform transform) { mTransform = transform; }
setAlpha(float alpha)81     void setAlpha(float alpha) { mAlpha = alpha; }
setBlendMode(BlendMode blendMode)82     void setBlendMode(BlendMode blendMode) { mBlendMode = blendMode; }
83 
getBlendMode()84     BlendMode getBlendMode() const { return mBlendMode; }
85 
getZOrder()86     uint32_t getZOrder() const { return mZOrder; }
87 
getAlpha()88     float getAlpha() const { return mAlpha; }
89 
getLayer()90     int64_t getLayer() const { return mLayer; }
91 
getBrightness()92     float getBrightness() const { return mBrightness; }
93 
94   protected:
95     int64_t mDisplay;
96     int64_t mLayer;
97     Rect mDisplayFrame = {0, 0, 0, 0};
98     float mBrightness = 1.f;
99     float mWhitePointNits = -1.f;
100     std::vector<Rect> mSurfaceDamage;
101     Transform mTransform = static_cast<Transform>(0);
102     FRect mSourceCrop = {0, 0, 0, 0};
103     static constexpr uint32_t kBufferSlotCount = 64;
104     float mAlpha = 1.0;
105     BlendMode mBlendMode = BlendMode::NONE;
106     uint32_t mZOrder = 0;
107     Dataspace mDataspace = Dataspace::UNKNOWN;
108 };
109 
110 class TestColorLayer : public TestLayer {
111   public:
TestColorLayer(const std::shared_ptr<VtsComposerClient> & client,int64_t display,ComposerClientWriter & writer)112     TestColorLayer(const std::shared_ptr<VtsComposerClient>& client, int64_t display,
113                    ComposerClientWriter& writer)
114         : TestLayer{client, display, writer} {}
115 
116     void write(ComposerClientWriter& writer) override;
117 
118     LayerSettings toRenderEngineLayerSettings() override;
119 
setColor(Color color)120     void setColor(Color color) { mColor = color; }
121 
122   private:
123     Color mColor = WHITE;
124 };
125 
126 class TestBufferLayer : public TestLayer {
127   public:
128     TestBufferLayer(const std::shared_ptr<VtsComposerClient>& client,
129                     TestRenderEngine& renderEngine, int64_t display, uint32_t width,
130                     uint32_t height, common::PixelFormat format, ComposerClientWriter& writer,
131                     Composition composition = Composition::DEVICE);
132 
133     void write(ComposerClientWriter& writer) override;
134 
135     LayerSettings toRenderEngineLayerSettings() override;
136 
137     void fillBuffer(std::vector<Color>& expectedColors);
138 
139     void setBuffer(std::vector<Color> colors);
140 
141     void setToClientComposition(ComposerClientWriter& writer);
142 
getWidth()143     uint32_t getWidth() const { return mWidth; }
144 
getHeight()145     uint32_t getHeight() const { return mHeight; }
146 
getAccessRegion()147     ::android::Rect getAccessRegion() const { return mAccessRegion; }
148 
getLayerCount()149     uint32_t getLayerCount() const { return mLayerCount; }
150 
151   protected:
152     Composition mComposition;
153     ::android::sp<::android::GraphicBuffer> mGraphicBuffer;
154     TestRenderEngine& mRenderEngine;
155     int32_t mFillFence;
156     uint32_t mWidth;
157     uint32_t mHeight;
158     uint32_t mLayerCount;
159     PixelFormat mPixelFormat;
160     uint32_t mUsage;
161     ::android::Rect mAccessRegion;
162 
163   private:
164     ::android::sp<::android::GraphicBuffer> allocateBuffer();
165 };
166 
167 class ReadbackHelper {
168   public:
169     static std::string getColorModeString(ColorMode mode);
170 
171     static std::string getDataspaceString(Dataspace dataspace);
172 
173     static Dataspace getDataspaceForColorMode(ColorMode mode);
174 
175     static int32_t GetBytesPerPixel(PixelFormat pixelFormat);
176 
177     static void fillBuffer(uint32_t width, uint32_t height, uint32_t stride, void* bufferData,
178                            PixelFormat pixelFormat, std::vector<Color> desiredPixelColors);
179 
180     static void clearColors(std::vector<Color>& expectedColors, int32_t width, int32_t height,
181                             int32_t displayWidth);
182 
183     static void fillColorsArea(std::vector<Color>& expectedColors, int32_t stride, Rect area,
184                                Color color);
185 
186     static bool readbackSupported(const PixelFormat& pixelFormat, const Dataspace& dataspace);
187 
188     static const std::vector<ColorMode> colorModes;
189     static const std::vector<Dataspace> dataspaces;
190 
191     static void compareColorBuffers(const std::vector<Color>& expectedColors, void* bufferData,
192                                     const uint32_t stride, const uint32_t width,
193                                     const uint32_t height, PixelFormat pixelFormat);
194     static void compareColorBuffers(void* expectedBuffer, void* actualBuffer, const uint32_t stride,
195                                     const uint32_t width, const uint32_t height,
196                                     PixelFormat pixelFormat);
197 };
198 
199 class ReadbackBuffer {
200   public:
201     ReadbackBuffer(int64_t display, const std::shared_ptr<VtsComposerClient>& client, int32_t width,
202                    int32_t height, common::PixelFormat pixelFormat, common::Dataspace dataspace);
203 
204     void setReadbackBuffer();
205 
206     void checkReadbackBuffer(const std::vector<Color>& expectedColors);
207 
208     ::android::sp<::android::GraphicBuffer> getBuffer();
209 
210   protected:
211     uint32_t mWidth;
212     uint32_t mHeight;
213     uint32_t mLayerCount;
214     uint32_t mUsage;
215     PixelFormat mPixelFormat;
216     Dataspace mDataspace;
217     int64_t mDisplay;
218     ::android::sp<::android::GraphicBuffer> mGraphicBuffer;
219     std::shared_ptr<VtsComposerClient> mComposerClient;
220     ::android::Rect mAccessRegion;
221     native_handle_t mBufferHandle;
222 
223   private:
224     ::android::sp<::android::GraphicBuffer> allocateBuffer();
225 };
226 
227 }  // namespace aidl::android::hardware::graphics::composer3::vts
228