xref: /aosp_15_r20/hardware/interfaces/graphics/composer/aidl/vts/ReadbackVts.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 #include "ReadbackVts.h"
18 #include <aidl/android/hardware/graphics/common/BufferUsage.h>
19 #include <cmath>
20 #include "RenderEngineVts.h"
21 #include "renderengine/ExternalTexture.h"
22 #include "renderengine/impl/ExternalTexture.h"
23 
24 namespace aidl::android::hardware::graphics::composer3::vts {
25 
26 const std::vector<ColorMode> ReadbackHelper::colorModes = {ColorMode::SRGB, ColorMode::DISPLAY_P3};
27 const std::vector<Dataspace> ReadbackHelper::dataspaces = {common::Dataspace::SRGB,
28                                                            common::Dataspace::DISPLAY_P3};
29 
write(ComposerClientWriter & writer)30 void TestLayer::write(ComposerClientWriter& writer) {
31     writer.setLayerDisplayFrame(mDisplay, mLayer, mDisplayFrame);
32     writer.setLayerSourceCrop(mDisplay, mLayer, mSourceCrop);
33     writer.setLayerZOrder(mDisplay, mLayer, mZOrder);
34     writer.setLayerSurfaceDamage(mDisplay, mLayer, mSurfaceDamage);
35     writer.setLayerTransform(mDisplay, mLayer, mTransform);
36     writer.setLayerPlaneAlpha(mDisplay, mLayer, mAlpha);
37     writer.setLayerBlendMode(mDisplay, mLayer, mBlendMode);
38     writer.setLayerBrightness(mDisplay, mLayer, mBrightness);
39     writer.setLayerDataspace(mDisplay, mLayer, mDataspace);
40 }
41 
getColorModeString(ColorMode mode)42 std::string ReadbackHelper::getColorModeString(ColorMode mode) {
43     switch (mode) {
44         case ColorMode::SRGB:
45             return {"SRGB"};
46         case ColorMode::DISPLAY_P3:
47             return {"DISPLAY_P3"};
48         default:
49             return {"Unsupported color mode for readback"};
50     }
51 }
52 
getDataspaceString(common::Dataspace dataspace)53 std::string ReadbackHelper::getDataspaceString(common::Dataspace dataspace) {
54     switch (dataspace) {
55         case common::Dataspace::SRGB:
56             return {"SRGB"};
57         case common::Dataspace::DISPLAY_P3:
58             return {"DISPLAY_P3"};
59         case common::Dataspace::UNKNOWN:
60             return {"UNKNOWN"};
61         default:
62             return {"Unsupported dataspace for readback"};
63     }
64 }
65 
getDataspaceForColorMode(ColorMode mode)66 Dataspace ReadbackHelper::getDataspaceForColorMode(ColorMode mode) {
67     switch (mode) {
68         case ColorMode::DISPLAY_P3:
69             return Dataspace::DISPLAY_P3;
70         case ColorMode::SRGB:
71             return Dataspace::SRGB;
72         default:
73             return Dataspace::UNKNOWN;
74     }
75 }
76 
toRenderEngineLayerSettings()77 LayerSettings TestLayer::toRenderEngineLayerSettings() {
78     LayerSettings layerSettings;
79 
80     layerSettings.alpha = ::android::half(mAlpha);
81     layerSettings.disableBlending = mBlendMode == BlendMode::NONE;
82     layerSettings.source.buffer.isOpaque = mBlendMode == BlendMode::NONE;
83     layerSettings.geometry.boundaries = ::android::FloatRect(
84             static_cast<float>(mDisplayFrame.left), static_cast<float>(mDisplayFrame.top),
85             static_cast<float>(mDisplayFrame.right), static_cast<float>(mDisplayFrame.bottom));
86 
87     const ::android::mat4 translation = ::android::mat4::translate(::android::vec4(
88             (static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_H)
89                      ? static_cast<float>(-mDisplayFrame.right)
90                      : 0.0f),
91             (static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_V)
92                      ? static_cast<float>(-mDisplayFrame.bottom)
93                      : 0.0f),
94             0.0f, 1.0f));
95 
96     const ::android::mat4 scale = ::android::mat4::scale(::android::vec4(
97             static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_H) ? -1.0f
98                                                                                          : 1.0f,
99             static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_V) ? -1.0f
100                                                                                          : 1.0f,
101             1.0f, 1.0f));
102 
103     layerSettings.geometry.positionTransform = scale * translation;
104     layerSettings.whitePointNits = mWhitePointNits;
105     layerSettings.sourceDataspace = static_cast<::android::ui::Dataspace>(mDataspace);
106 
107     return layerSettings;
108 }
109 
GetBitsPerChannel(common::PixelFormat pixelFormat)110 int32_t ReadbackHelper::GetBitsPerChannel(common::PixelFormat pixelFormat) {
111     switch (pixelFormat) {
112         case common::PixelFormat::RGBA_1010102:
113             return 10;
114         case common::PixelFormat::RGBA_8888:
115         case common::PixelFormat::RGB_888:
116             return 8;
117         default:
118             return -1;
119     }
120 }
121 
GetAlphaBits(common::PixelFormat pixelFormat)122 int32_t ReadbackHelper::GetAlphaBits(common::PixelFormat pixelFormat) {
123     switch (pixelFormat) {
124         case common::PixelFormat::RGBA_8888:
125             return 8;
126         case common::PixelFormat::RGBA_1010102:
127             return 2;
128         case common::PixelFormat::RGB_888:
129             return 0;
130         default:
131             return -1;
132     }
133 }
134 
fillBuffer(uint32_t width,uint32_t height,uint32_t stride,int32_t bytesPerPixel,void * bufferData,common::PixelFormat pixelFormat,std::vector<Color> desiredPixelColors)135 void ReadbackHelper::fillBuffer(uint32_t width, uint32_t height, uint32_t stride,
136                                 int32_t bytesPerPixel, void* bufferData,
137                                 common::PixelFormat pixelFormat,
138                                 std::vector<Color> desiredPixelColors) {
139     ASSERT_TRUE(pixelFormat == common::PixelFormat::RGB_888 ||
140                 pixelFormat == common::PixelFormat::RGBA_8888 ||
141                 pixelFormat == common::PixelFormat::RGBA_1010102);
142     int32_t bitsPerChannel = GetBitsPerChannel(pixelFormat);
143     int32_t alphaBits = GetAlphaBits(pixelFormat);
144     ASSERT_NE(-1, alphaBits);
145     ASSERT_NE(-1, bitsPerChannel);
146     ASSERT_NE(-1, bytesPerPixel);
147 
148     uint32_t maxValue = (1 << bitsPerChannel) - 1;
149     uint32_t maxAlphaValue = (1 << alphaBits) - 1;
150     for (uint32_t row = 0; row < height; row++) {
151         for (uint32_t col = 0; col < width; col++) {
152             auto pixel = row * width + col;
153             Color srcColor = desiredPixelColors[static_cast<size_t>(pixel)];
154 
155             uint32_t offset = (row * stride + col) * static_cast<uint32_t>(bytesPerPixel);
156 
157             uint32_t* pixelStart = (uint32_t*)((uint8_t*)bufferData + offset);
158 
159             uint32_t red = static_cast<uint32_t>(std::round(maxValue * srcColor.r));
160             uint32_t green = static_cast<uint32_t>(std::round(maxValue * srcColor.g));
161             uint32_t blue = static_cast<uint32_t>(std::round(maxValue * srcColor.b));
162 
163             // Boo we're not word aligned so special case this.
164             if (pixelFormat == common::PixelFormat::RGB_888) {
165                 uint8_t* pixelColor = (uint8_t*)pixelStart;
166                 pixelColor[0] = static_cast<uint8_t>(red);
167                 pixelColor[1] = static_cast<uint8_t>(green);
168                 pixelColor[2] = static_cast<uint8_t>(blue);
169             } else {
170                 uint32_t alpha = static_cast<uint32_t>(std::round(maxAlphaValue * srcColor.a));
171                 uint32_t color = (alpha << (32 - alphaBits)) |
172                                  (blue << (32 - alphaBits - bitsPerChannel)) |
173                                  (green << (32 - alphaBits - bitsPerChannel * 2)) |
174                                  (red << (32 - alphaBits - bitsPerChannel * 3));
175                 *pixelStart = color;
176             }
177         }
178     }
179 }
180 
clearColors(std::vector<Color> & expectedColors,int32_t width,int32_t height,int32_t displayWidth)181 void ReadbackHelper::clearColors(std::vector<Color>& expectedColors, int32_t width, int32_t height,
182                                  int32_t displayWidth) {
183     for (int row = 0; row < height; row++) {
184         for (int col = 0; col < width; col++) {
185             int pixel = row * displayWidth + col;
186             expectedColors[static_cast<size_t>(pixel)] = BLACK;
187         }
188     }
189 }
190 
fillColorsArea(std::vector<Color> & expectedColors,int32_t stride,Rect area,Color color)191 void ReadbackHelper::fillColorsArea(std::vector<Color>& expectedColors, int32_t stride, Rect area,
192                                     Color color) {
193     for (int row = area.top; row < area.bottom; row++) {
194         for (int col = area.left; col < area.right; col++) {
195             int pixel = row * stride + col;
196             expectedColors[static_cast<size_t>(pixel)] = color;
197         }
198     }
199 }
200 
readbackSupported(const common::PixelFormat & pixelFormat,const common::Dataspace & dataspace)201 bool ReadbackHelper::readbackSupported(const common::PixelFormat& pixelFormat,
202                                        const common::Dataspace& dataspace) {
203     if (pixelFormat != common::PixelFormat::RGB_888 &&
204         pixelFormat != common::PixelFormat::RGBA_8888 &&
205         pixelFormat != common::PixelFormat::RGBA_1010102) {
206         return false;
207     }
208     if (std::find(dataspaces.begin(), dataspaces.end(), dataspace) == dataspaces.end()) {
209         return false;
210     }
211     return true;
212 }
213 
compareColorBuffers(const std::vector<Color> & expectedColors,void * bufferData,const uint32_t stride,int32_t bytesPerPixel,const uint32_t width,const uint32_t height,common::PixelFormat pixelFormat)214 void ReadbackHelper::compareColorBuffers(const std::vector<Color>& expectedColors, void* bufferData,
215                                          const uint32_t stride, int32_t bytesPerPixel,
216                                          const uint32_t width, const uint32_t height,
217                                          common::PixelFormat pixelFormat) {
218     int32_t bitsPerChannel = GetBitsPerChannel(pixelFormat);
219     int32_t alphaBits = GetAlphaBits(pixelFormat);
220     ASSERT_GT(bytesPerPixel, 0);
221     ASSERT_NE(-1, alphaBits);
222     ASSERT_NE(-1, bitsPerChannel);
223     uint32_t maxValue = (1 << bitsPerChannel) - 1;
224     uint32_t maxAlphaValue = (1 << alphaBits) - 1;
225     for (uint32_t row = 0; row < height; row++) {
226         for (uint32_t col = 0; col < width; col++) {
227             auto pixel = row * width + col;
228             const Color expectedColor = expectedColors[static_cast<size_t>(pixel)];
229 
230             uint32_t offset = (row * stride + col) * static_cast<uint32_t>(bytesPerPixel);
231             uint32_t* pixelStart = (uint32_t*)((uint8_t*)bufferData + offset);
232 
233             uint32_t expectedRed = static_cast<uint32_t>(std::round(maxValue * expectedColor.r));
234             uint32_t expectedGreen = static_cast<uint32_t>(std::round(maxValue * expectedColor.g));
235             uint32_t expectedBlue = static_cast<uint32_t>(std::round(maxValue * expectedColor.b));
236 
237             // Boo we're not word aligned so special case this.
238             if (pixelFormat == common::PixelFormat::RGB_888) {
239                 uint8_t* pixelColor = (uint8_t*)pixelStart;
240                 ASSERT_EQ(pixelColor[0], static_cast<uint8_t>(expectedRed))
241                         << "Red channel mismatch at (" << row << ", " << col << ")";
242                 ASSERT_EQ(pixelColor[1], static_cast<uint8_t>(expectedGreen))
243                         << "Green channel mismatch at (" << row << ", " << col << ")";
244                 ASSERT_EQ(pixelColor[2], static_cast<uint8_t>(expectedBlue))
245                         << "Blue channel mismatch at (" << row << ", " << col << ")";
246             } else {
247                 uint32_t expectedAlpha =
248                         static_cast<uint32_t>(std::round(maxAlphaValue * expectedColor.a));
249 
250                 uint32_t actualRed =
251                         (*pixelStart >> (32 - alphaBits - bitsPerChannel * 3)) & maxValue;
252                 uint32_t actualGreen =
253                         (*pixelStart >> (32 - alphaBits - bitsPerChannel * 2)) & maxValue;
254                 uint32_t actualBlue = (*pixelStart >> (32 - alphaBits - bitsPerChannel)) & maxValue;
255                 uint32_t actualAlpha = (*pixelStart >> (32 - alphaBits)) & maxAlphaValue;
256 
257                 ASSERT_EQ(expectedRed, actualRed)
258                         << "Red channel mismatch at (" << row << ", " << col << ")";
259                 ASSERT_EQ(expectedGreen, actualGreen)
260                         << "Green channel mismatch at (" << row << ", " << col << ")";
261                 ASSERT_EQ(expectedBlue, actualBlue)
262                         << "Blue channel mismatch at (" << row << ", " << col << ")";
263             }
264         }
265     }
266 }
267 
compareColorBuffers(void * expectedBuffer,void * actualBuffer,const uint32_t stride,int32_t bytesPerPixel,const uint32_t width,const uint32_t height,common::PixelFormat pixelFormat)268 void ReadbackHelper::compareColorBuffers(void* expectedBuffer, void* actualBuffer,
269                                          const uint32_t stride, int32_t bytesPerPixel,
270                                          const uint32_t width, const uint32_t height,
271                                          common::PixelFormat pixelFormat) {
272     int32_t bitsPerChannel = GetBitsPerChannel(pixelFormat);
273     int32_t alphaBits = GetAlphaBits(pixelFormat);
274     ASSERT_GT(bytesPerPixel, 0);
275     ASSERT_NE(-1, alphaBits);
276     ASSERT_NE(-1, bitsPerChannel);
277     uint32_t maxValue = (1 << bitsPerChannel) - 1;
278     uint32_t maxAlphaValue = (1 << alphaBits) - 1;
279     for (uint32_t row = 0; row < height; row++) {
280         for (uint32_t col = 0; col < width; col++) {
281             uint32_t offset = (row * stride + col) * static_cast<uint32_t>(bytesPerPixel);
282             uint32_t* expectedStart = (uint32_t*)((uint8_t*)expectedBuffer + offset);
283             uint32_t* actualStart = (uint32_t*)((uint8_t*)actualBuffer + offset);
284 
285             // Boo we're not word aligned so special case this.
286             if (pixelFormat == common::PixelFormat::RGB_888) {
287                 uint8_t* expectedPixel = (uint8_t*)expectedStart;
288                 uint8_t* actualPixel = (uint8_t*)actualStart;
289                 ASSERT_EQ(actualPixel[0], expectedPixel[0])
290                         << "Red channel mismatch at (" << row << ", " << col << ")";
291                 ASSERT_EQ(actualPixel[1], expectedPixel[1])
292                         << "Green channel mismatch at (" << row << ", " << col << ")";
293                 ASSERT_EQ(actualPixel[2], expectedPixel[2])
294                         << "Blue channel mismatch at (" << row << ", " << col << ")";
295             } else {
296                 uint32_t expectedRed =
297                         (*expectedStart >> (32 - alphaBits - bitsPerChannel * 3)) & maxValue;
298                 uint32_t expectedGreen =
299                         (*expectedStart >> (32 - alphaBits - bitsPerChannel * 2)) & maxValue;
300                 uint32_t expectedBlue =
301                         (*expectedStart >> (32 - alphaBits - bitsPerChannel)) & maxValue;
302                 uint32_t expectedAlpha = (*expectedStart >> (32 - alphaBits)) & maxAlphaValue;
303 
304                 uint32_t actualRed =
305                         (*actualStart >> (32 - alphaBits - bitsPerChannel * 3)) & maxValue;
306                 uint32_t actualGreen =
307                         (*actualStart >> (32 - alphaBits - bitsPerChannel * 2)) & maxValue;
308                 uint32_t actualBlue =
309                         (*actualStart >> (32 - alphaBits - bitsPerChannel)) & maxValue;
310                 uint32_t actualAlpha = (*actualStart >> (32 - alphaBits)) & maxAlphaValue;
311 
312                 ASSERT_EQ(expectedRed, actualRed)
313                         << "Red channel mismatch at (" << row << ", " << col << ")";
314                 ASSERT_EQ(expectedGreen, actualGreen)
315                         << "Green channel mismatch at (" << row << ", " << col << ")";
316                 ASSERT_EQ(expectedBlue, actualBlue)
317                         << "Blue channel mismatch at (" << row << ", " << col << ")";
318             }
319         }
320     }
321 }
322 
ReadbackBuffer(int64_t display,const std::shared_ptr<VtsComposerClient> & client,int32_t width,int32_t height,common::PixelFormat pixelFormat,common::Dataspace dataspace)323 ReadbackBuffer::ReadbackBuffer(int64_t display, const std::shared_ptr<VtsComposerClient>& client,
324                                int32_t width, int32_t height, common::PixelFormat pixelFormat,
325                                common::Dataspace dataspace)
326     : mComposerClient(client) {
327     mDisplay = display;
328 
329     mPixelFormat = pixelFormat;
330     mDataspace = dataspace;
331 
332     mWidth = static_cast<uint32_t>(width);
333     mHeight = static_cast<uint32_t>(height);
334     mLayerCount = 1;
335     mUsage = static_cast<uint64_t>(static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) |
336                                    static_cast<uint64_t>(common::BufferUsage::GPU_TEXTURE));
337 
338     mAccessRegion.top = 0;
339     mAccessRegion.left = 0;
340     mAccessRegion.right = static_cast<int32_t>(width);
341     mAccessRegion.bottom = static_cast<int32_t>(height);
342 }
343 
allocateBuffer()344 ::android::sp<::android::GraphicBuffer> ReadbackBuffer::allocateBuffer() {
345     return ::android::sp<::android::GraphicBuffer>::make(
346             mWidth, mHeight, static_cast<::android::PixelFormat>(mPixelFormat), mLayerCount, mUsage,
347             "ReadbackBuffer");
348 }
349 
setReadbackBuffer()350 void ReadbackBuffer::setReadbackBuffer() {
351     mGraphicBuffer = allocateBuffer();
352     ASSERT_NE(nullptr, mGraphicBuffer);
353     ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck());
354     const auto& bufferHandle = mGraphicBuffer->handle;
355     ::ndk::ScopedFileDescriptor fence = ::ndk::ScopedFileDescriptor(-1);
356     EXPECT_TRUE(mComposerClient->setReadbackBuffer(mDisplay, bufferHandle, fence).isOk());
357 }
358 
checkReadbackBuffer(const std::vector<Color> & expectedColors)359 void ReadbackBuffer::checkReadbackBuffer(const std::vector<Color>& expectedColors) {
360     ASSERT_NE(nullptr, mGraphicBuffer);
361     // lock buffer for reading
362     const auto& [fenceStatus, bufferFence] = mComposerClient->getReadbackBufferFence(mDisplay);
363     EXPECT_TRUE(fenceStatus.isOk());
364 
365     int bytesPerPixel = -1;
366     int bytesPerStride = -1;
367     void* bufData = nullptr;
368 
369     auto status = mGraphicBuffer->lockAsync(mUsage, mAccessRegion, &bufData, dup(bufferFence.get()),
370                                             &bytesPerPixel, &bytesPerStride);
371     EXPECT_EQ(::android::OK, status);
372     ASSERT_TRUE(mPixelFormat == PixelFormat::RGB_888 || mPixelFormat == PixelFormat::RGBA_8888 ||
373                 mPixelFormat == PixelFormat::RGBA_1010102);
374     const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0)
375                                     ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel)
376                                     : mGraphicBuffer->getStride();
377     ReadbackHelper::compareColorBuffers(expectedColors, bufData, stride, bytesPerPixel, mWidth,
378                                         mHeight, mPixelFormat);
379     status = mGraphicBuffer->unlock();
380     EXPECT_EQ(::android::OK, status);
381 }
382 
getBuffer()383 ::android::sp<::android::GraphicBuffer> ReadbackBuffer::getBuffer() {
384     const auto& [fenceStatus, bufferFence] = mComposerClient->getReadbackBufferFence(mDisplay);
385     EXPECT_TRUE(fenceStatus.isOk());
386     if (bufferFence.get() != -1) {
387         sync_wait(bufferFence.get(), -1);
388     }
389     return mGraphicBuffer;
390 }
391 
write(ComposerClientWriter & writer)392 void TestColorLayer::write(ComposerClientWriter& writer) {
393     TestLayer::write(writer);
394     writer.setLayerCompositionType(mDisplay, mLayer, Composition::SOLID_COLOR);
395     writer.setLayerColor(mDisplay, mLayer, mColor);
396 }
397 
toRenderEngineLayerSettings()398 LayerSettings TestColorLayer::toRenderEngineLayerSettings() {
399     LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings();
400 
401     layerSettings.source.solidColor = ::android::half3(mColor.r, mColor.g, mColor.b);
402     layerSettings.alpha = mAlpha * mColor.a;
403     return layerSettings;
404 }
405 
TestBufferLayer(const std::shared_ptr<VtsComposerClient> & client,TestRenderEngine & renderEngine,int64_t display,uint32_t width,uint32_t height,common::PixelFormat format,ComposerClientWriter & writer,Composition composition)406 TestBufferLayer::TestBufferLayer(const std::shared_ptr<VtsComposerClient>& client,
407                                  TestRenderEngine& renderEngine, int64_t display, uint32_t width,
408                                  uint32_t height, common::PixelFormat format,
409                                  ComposerClientWriter& writer, Composition composition)
410     : TestLayer{client, display, writer}, mRenderEngine(renderEngine) {
411     mComposition = composition;
412     mWidth = width;
413     mHeight = height;
414     mLayerCount = 1;
415     mPixelFormat = format;
416     mUsage = (static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) |
417               static_cast<uint64_t>(common::BufferUsage::CPU_WRITE_OFTEN) |
418               static_cast<uint64_t>(common::BufferUsage::COMPOSER_OVERLAY) |
419               static_cast<uint64_t>(common::BufferUsage::GPU_TEXTURE));
420 
421     mAccessRegion.top = 0;
422     mAccessRegion.left = 0;
423     mAccessRegion.right = static_cast<int32_t>(width);
424     mAccessRegion.bottom = static_cast<int32_t>(height);
425 
426     setSourceCrop({0, 0, (float)width, (float)height});
427 }
428 
write(ComposerClientWriter & writer)429 void TestBufferLayer::write(ComposerClientWriter& writer) {
430     TestLayer::write(writer);
431     writer.setLayerCompositionType(mDisplay, mLayer, mComposition);
432     writer.setLayerVisibleRegion(mDisplay, mLayer, std::vector<Rect>(1, mDisplayFrame));
433     if (mGraphicBuffer) {
434         writer.setLayerBuffer(mDisplay, mLayer, /*slot*/ 0, mGraphicBuffer->handle, mFillFence);
435     }
436 }
437 
toRenderEngineLayerSettings()438 LayerSettings TestBufferLayer::toRenderEngineLayerSettings() {
439     LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings();
440     layerSettings.source.buffer.buffer =
441             std::make_shared<::android::renderengine::impl::ExternalTexture>(
442                     mGraphicBuffer, mRenderEngine.getInternalRenderEngine(),
443                     ::android::renderengine::impl::ExternalTexture::Usage::READABLE);
444 
445     layerSettings.source.buffer.usePremultipliedAlpha = mBlendMode == BlendMode::PREMULTIPLIED;
446 
447     const float scaleX = (mSourceCrop.right - mSourceCrop.left) / (static_cast<float>(mWidth));
448     const float scaleY = (mSourceCrop.bottom - mSourceCrop.top) / (static_cast<float>(mHeight));
449     const float translateX = mSourceCrop.left / (static_cast<float>(mWidth));
450     const float translateY = mSourceCrop.top / (static_cast<float>(mHeight));
451 
452     layerSettings.source.buffer.textureTransform =
453             ::android::mat4::translate(::android::vec4(translateX, translateY, 0.0f, 1.0f)) *
454             ::android::mat4::scale(::android::vec4(scaleX, scaleY, 1.0f, 1.0f));
455 
456     return layerSettings;
457 }
458 
fillBuffer(std::vector<Color> & expectedColors)459 void TestBufferLayer::fillBuffer(std::vector<Color>& expectedColors) {
460     void* bufData;
461     int32_t bytesPerPixel = -1;
462     int32_t bytesPerStride = -1;
463     auto status = mGraphicBuffer->lock(mUsage, &bufData, &bytesPerPixel, &bytesPerStride);
464     const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0)
465                                     ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel)
466                                     : mGraphicBuffer->getStride();
467     EXPECT_EQ(::android::OK, status);
468     ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBuffer(mWidth, mHeight, stride, bytesPerPixel,
469                                                        bufData, mPixelFormat, expectedColors));
470 
471     const auto unlockStatus = mGraphicBuffer->unlockAsync(&mFillFence);
472     ASSERT_EQ(::android::OK, unlockStatus);
473 }
474 
setBuffer(std::vector<Color> colors)475 void TestBufferLayer::setBuffer(std::vector<Color> colors) {
476     mGraphicBuffer = allocateBuffer();
477     ASSERT_NE(nullptr, mGraphicBuffer);
478     ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck());
479     ASSERT_NO_FATAL_FAILURE(fillBuffer(colors));
480 }
481 
allocateBuffer()482 ::android::sp<::android::GraphicBuffer> TestBufferLayer::allocateBuffer() {
483     return ::android::sp<::android::GraphicBuffer>::make(
484             mWidth, mHeight, static_cast<::android::PixelFormat>(mPixelFormat), mLayerCount, mUsage,
485             "TestBufferLayer");
486 }
487 
setToClientComposition(ComposerClientWriter & writer)488 void TestBufferLayer::setToClientComposition(ComposerClientWriter& writer) {
489     writer.setLayerCompositionType(mDisplay, mLayer, Composition::CLIENT);
490 }
491 
492 }  // namespace aidl::android::hardware::graphics::composer3::vts
493