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