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