blob: 9d5928d896fb9317afa4b8c2fa637d6208ef21c1 [file] [log] [blame]
ramindanibab8ba92021-11-18 01:24:11 +00001/**
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
ramindani458e53e2022-02-23 17:30:16 +000017#include "ReadbackVts.h"
ramindanibab8ba92021-11-18 01:24:11 +000018#include <aidl/android/hardware/graphics/common/BufferUsage.h>
Alec Mouri6a000222024-08-16 23:08:09 +000019#include <cmath>
ramindani458e53e2022-02-23 17:30:16 +000020#include "RenderEngineVts.h"
ramindanibab8ba92021-11-18 01:24:11 +000021#include "renderengine/ExternalTexture.h"
Vishnu Nair59329712022-01-13 07:59:37 -080022#include "renderengine/impl/ExternalTexture.h"
ramindanibab8ba92021-11-18 01:24:11 +000023
ramindanibab8ba92021-11-18 01:24:11 +000024namespace aidl::android::hardware::graphics::composer3::vts {
25
26const std::vector<ColorMode> ReadbackHelper::colorModes = {ColorMode::SRGB, ColorMode::DISPLAY_P3};
27const std::vector<Dataspace> ReadbackHelper::dataspaces = {common::Dataspace::SRGB,
28 common::Dataspace::DISPLAY_P3};
29
Ady Abraham91c9d1a2021-12-15 18:14:45 -080030void TestLayer::write(ComposerClientWriter& writer) {
Ady Abraham3192f3d2021-12-03 16:08:56 -080031 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);
Alec Mourib1f16722022-02-07 13:03:44 -080038 writer.setLayerBrightness(mDisplay, mLayer, mBrightness);
Alec Mourif6c039a2023-10-06 23:02:17 +000039 writer.setLayerDataspace(mDisplay, mLayer, mDataspace);
ramindanibab8ba92021-11-18 01:24:11 +000040}
41
42std::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
53std::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
66Dataspace ReadbackHelper::getDataspaceForColorMode(ColorMode mode) {
67 switch (mode) {
68 case ColorMode::DISPLAY_P3:
69 return Dataspace::DISPLAY_P3;
70 case ColorMode::SRGB:
Sally Qi0ca29272024-02-23 11:07:08 -080071 return Dataspace::SRGB;
ramindanibab8ba92021-11-18 01:24:11 +000072 default:
Sally Qi0ca29272024-02-23 11:07:08 -080073 return Dataspace::UNKNOWN;
ramindanibab8ba92021-11-18 01:24:11 +000074 }
75}
76
77LayerSettings TestLayer::toRenderEngineLayerSettings() {
78 LayerSettings layerSettings;
79
80 layerSettings.alpha = ::android::half(mAlpha);
81 layerSettings.disableBlending = mBlendMode == BlendMode::NONE;
ramindanic7585d92022-04-15 18:30:41 +000082 layerSettings.source.buffer.isOpaque = mBlendMode == BlendMode::NONE;
ramindanibab8ba92021-11-18 01:24:11 +000083 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;
Alec Mouri51067012022-01-06 17:28:39 -0800104 layerSettings.whitePointNits = mWhitePointNits;
Alec Mourif6c039a2023-10-06 23:02:17 +0000105 layerSettings.sourceDataspace = static_cast<::android::ui::Dataspace>(mDataspace);
ramindanibab8ba92021-11-18 01:24:11 +0000106
107 return layerSettings;
108}
109
Alec Mouri6a000222024-08-16 23:08:09 +0000110int32_t ReadbackHelper::GetBitsPerChannel(common::PixelFormat pixelFormat) {
ramindanibab8ba92021-11-18 01:24:11 +0000111 switch (pixelFormat) {
Alec Mouri6a000222024-08-16 23:08:09 +0000112 case common::PixelFormat::RGBA_1010102:
113 return 10;
Brian Lindahle887a252023-01-17 14:54:19 -0700114 case common::PixelFormat::RGBA_8888:
Brian Lindahle887a252023-01-17 14:54:19 -0700115 case common::PixelFormat::RGB_888:
Alec Mouri6a000222024-08-16 23:08:09 +0000116 return 8;
ramindanibab8ba92021-11-18 01:24:11 +0000117 default:
118 return -1;
119 }
120}
121
Alec Mouri6a000222024-08-16 23:08:09 +0000122int32_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
135void ReadbackHelper::fillBuffer(uint32_t width, uint32_t height, uint32_t stride,
136 int32_t bytesPerPixel, void* bufferData,
ramindanibab8ba92021-11-18 01:24:11 +0000137 common::PixelFormat pixelFormat,
Brian Lindahle887a252023-01-17 14:54:19 -0700138 std::vector<Color> desiredPixelColors) {
ramindanibab8ba92021-11-18 01:24:11 +0000139 ASSERT_TRUE(pixelFormat == common::PixelFormat::RGB_888 ||
Alec Mouri6a000222024-08-16 23:08:09 +0000140 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);
ramindanibab8ba92021-11-18 01:24:11 +0000146 ASSERT_NE(-1, bytesPerPixel);
Alec Mouri6a000222024-08-16 23:08:09 +0000147
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;
Brian Lindahle887a252023-01-17 14:54:19 -0700153 Color srcColor = desiredPixelColors[static_cast<size_t>(pixel)];
ramindanibab8ba92021-11-18 01:24:11 +0000154
Alec Mouri6a000222024-08-16 23:08:09 +0000155 uint32_t offset = (row * stride + col) * static_cast<uint32_t>(bytesPerPixel);
ramindanibab8ba92021-11-18 01:24:11 +0000156
Alec Mouri6a000222024-08-16 23:08:09 +0000157 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;
ramindanibab8ba92021-11-18 01:24:11 +0000176 }
177 }
178 }
179}
180
Brian Lindahle887a252023-01-17 14:54:19 -0700181void ReadbackHelper::clearColors(std::vector<Color>& expectedColors, int32_t width, int32_t height,
ramindanibab8ba92021-11-18 01:24:11 +0000182 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;
Brian Lindahle887a252023-01-17 14:54:19 -0700186 expectedColors[static_cast<size_t>(pixel)] = BLACK;
ramindanibab8ba92021-11-18 01:24:11 +0000187 }
188 }
189}
190
Brian Lindahle887a252023-01-17 14:54:19 -0700191void ReadbackHelper::fillColorsArea(std::vector<Color>& expectedColors, int32_t stride, Rect area,
192 Color color) {
ramindanibab8ba92021-11-18 01:24:11 +0000193 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;
Brian Lindahle887a252023-01-17 14:54:19 -0700196 expectedColors[static_cast<size_t>(pixel)] = color;
ramindanibab8ba92021-11-18 01:24:11 +0000197 }
198 }
199}
200
Brian Lindahle887a252023-01-17 14:54:19 -0700201bool ReadbackHelper::readbackSupported(const common::PixelFormat& pixelFormat,
202 const common::Dataspace& dataspace) {
203 if (pixelFormat != common::PixelFormat::RGB_888 &&
Alec Mouri6a000222024-08-16 23:08:09 +0000204 pixelFormat != common::PixelFormat::RGBA_8888 &&
205 pixelFormat != common::PixelFormat::RGBA_1010102) {
Brian Lindahle887a252023-01-17 14:54:19 -0700206 return false;
Brian Lindahld103cd62022-12-09 07:26:28 -0700207 }
Brian Lindahle887a252023-01-17 14:54:19 -0700208 if (std::find(dataspaces.begin(), dataspaces.end(), dataspace) == dataspaces.end()) {
209 return false;
210 }
211 return true;
212}
213
214void ReadbackHelper::compareColorBuffers(const std::vector<Color>& expectedColors, void* bufferData,
Alec Mouri6a000222024-08-16 23:08:09 +0000215 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;
Ady Abraham1bee7ab2022-01-06 17:22:08 -0800228 const Color expectedColor = expectedColors[static_cast<size_t>(pixel)];
Alec Mouri6a000222024-08-16 23:08:09 +0000229
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 }
ramindanibab8ba92021-11-18 01:24:11 +0000264 }
265 }
266}
267
Alec Mourif6c039a2023-10-06 23:02:17 +0000268void ReadbackHelper::compareColorBuffers(void* expectedBuffer, void* actualBuffer,
Alec Mouri6a000222024-08-16 23:08:09 +0000269 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 }
Alec Mourif6c039a2023-10-06 23:02:17 +0000319 }
320 }
321}
322
ramindanidcecfd42022-02-03 23:52:19 +0000323ReadbackBuffer::ReadbackBuffer(int64_t display, const std::shared_ptr<VtsComposerClient>& client,
Brian Lindahle887a252023-01-17 14:54:19 -0700324 int32_t width, int32_t height, common::PixelFormat pixelFormat,
325 common::Dataspace dataspace)
ramindanidcecfd42022-02-03 23:52:19 +0000326 : mComposerClient(client) {
ramindanibab8ba92021-11-18 01:24:11 +0000327 mDisplay = display;
Brian Lindahle887a252023-01-17 14:54:19 -0700328
329 mPixelFormat = pixelFormat;
330 mDataspace = dataspace;
331
ramindanibab8ba92021-11-18 01:24:11 +0000332 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));
Brian Lindahle887a252023-01-17 14:54:19 -0700337
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
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");
ramindanibab8ba92021-11-18 01:24:11 +0000348}
349
350void ReadbackBuffer::setReadbackBuffer() {
Brian Lindahle887a252023-01-17 14:54:19 -0700351 mGraphicBuffer = allocateBuffer();
ramindanibab8ba92021-11-18 01:24:11 +0000352 ASSERT_NE(nullptr, mGraphicBuffer);
353 ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck());
Brian Lindahle887a252023-01-17 14:54:19 -0700354 const auto& bufferHandle = mGraphicBuffer->handle;
355 ::ndk::ScopedFileDescriptor fence = ::ndk::ScopedFileDescriptor(-1);
356 EXPECT_TRUE(mComposerClient->setReadbackBuffer(mDisplay, bufferHandle, fence).isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000357}
358
ramindanidcecfd42022-02-03 23:52:19 +0000359void ReadbackBuffer::checkReadbackBuffer(const std::vector<Color>& expectedColors) {
Brian Lindahle887a252023-01-17 14:54:19 -0700360 ASSERT_NE(nullptr, mGraphicBuffer);
ramindanibab8ba92021-11-18 01:24:11 +0000361 // lock buffer for reading
ramindanidcecfd42022-02-03 23:52:19 +0000362 const auto& [fenceStatus, bufferFence] = mComposerClient->getReadbackBufferFence(mDisplay);
Brian Lindahle887a252023-01-17 14:54:19 -0700363 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);
Alec Mouri6a000222024-08-16 23:08:09 +0000372 ASSERT_TRUE(mPixelFormat == PixelFormat::RGB_888 || mPixelFormat == PixelFormat::RGBA_8888 ||
373 mPixelFormat == PixelFormat::RGBA_1010102);
Brian Lindahle887a252023-01-17 14:54:19 -0700374 const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0)
375 ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel)
376 : mGraphicBuffer->getStride();
Alec Mouri6a000222024-08-16 23:08:09 +0000377 ReadbackHelper::compareColorBuffers(expectedColors, bufData, stride, bytesPerPixel, mWidth,
378 mHeight, mPixelFormat);
Brian Lindahle887a252023-01-17 14:54:19 -0700379 status = mGraphicBuffer->unlock();
380 EXPECT_EQ(::android::OK, status);
ramindanibab8ba92021-11-18 01:24:11 +0000381}
382
Alec Mourif6c039a2023-10-06 23:02:17 +0000383::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
Ady Abraham91c9d1a2021-12-15 18:14:45 -0800392void TestColorLayer::write(ComposerClientWriter& writer) {
ramindanibab8ba92021-11-18 01:24:11 +0000393 TestLayer::write(writer);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800394 writer.setLayerCompositionType(mDisplay, mLayer, Composition::SOLID_COLOR);
395 writer.setLayerColor(mDisplay, mLayer, mColor);
ramindanibab8ba92021-11-18 01:24:11 +0000396}
397
398LayerSettings TestColorLayer::toRenderEngineLayerSettings() {
399 LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings();
400
Ady Abraham1bee7ab2022-01-06 17:22:08 -0800401 layerSettings.source.solidColor = ::android::half3(mColor.r, mColor.g, mColor.b);
402 layerSettings.alpha = mAlpha * mColor.a;
ramindanibab8ba92021-11-18 01:24:11 +0000403 return layerSettings;
404}
405
ramindanidcecfd42022-02-03 23:52:19 +0000406TestBufferLayer::TestBufferLayer(const std::shared_ptr<VtsComposerClient>& client,
Ady Abraham4aa4ead2021-12-03 16:07:19 -0800407 TestRenderEngine& renderEngine, int64_t display, uint32_t width,
ramindanibab8ba92021-11-18 01:24:11 +0000408 uint32_t height, common::PixelFormat format,
Ady Abrahama00d2462023-12-26 14:21:20 -0800409 ComposerClientWriter& writer, Composition composition)
410 : TestLayer{client, display, writer}, mRenderEngine(renderEngine) {
ramindanibab8ba92021-11-18 01:24:11 +0000411 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
Ady Abraham91c9d1a2021-12-15 18:14:45 -0800429void TestBufferLayer::write(ComposerClientWriter& writer) {
ramindanibab8ba92021-11-18 01:24:11 +0000430 TestLayer::write(writer);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800431 writer.setLayerCompositionType(mDisplay, mLayer, mComposition);
432 writer.setLayerVisibleRegion(mDisplay, mLayer, std::vector<Rect>(1, mDisplayFrame));
ramindani0a2bee42022-02-10 01:27:42 +0000433 if (mGraphicBuffer) {
434 writer.setLayerBuffer(mDisplay, mLayer, /*slot*/ 0, mGraphicBuffer->handle, mFillFence);
435 }
ramindanibab8ba92021-11-18 01:24:11 +0000436}
437
438LayerSettings TestBufferLayer::toRenderEngineLayerSettings() {
439 LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings();
Vishnu Nair59329712022-01-13 07:59:37 -0800440 layerSettings.source.buffer.buffer =
441 std::make_shared<::android::renderengine::impl::ExternalTexture>(
Alec Mouri3e037cf2022-01-24 08:58:28 -0800442 mGraphicBuffer, mRenderEngine.getInternalRenderEngine(),
Vishnu Nair59329712022-01-13 07:59:37 -0800443 ::android::renderengine::impl::ExternalTexture::Usage::READABLE);
ramindanibab8ba92021-11-18 01:24:11 +0000444
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 =
ramindanidcecfd42022-02-03 23:52:19 +0000453 ::android::mat4::translate(::android::vec4(translateX, translateY, 0.0f, 1.0f)) *
454 ::android::mat4::scale(::android::vec4(scaleX, scaleY, 1.0f, 1.0f));
ramindanibab8ba92021-11-18 01:24:11 +0000455
456 return layerSettings;
457}
458
459void TestBufferLayer::fillBuffer(std::vector<Color>& expectedColors) {
460 void* bufData;
Alec Mouri3e037cf2022-01-24 08:58:28 -0800461 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();
Brian Lindahle887a252023-01-17 14:54:19 -0700467 EXPECT_EQ(::android::OK, status);
Alec Mouri6a000222024-08-16 23:08:09 +0000468 ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBuffer(mWidth, mHeight, stride, bytesPerPixel,
469 bufData, mPixelFormat, expectedColors));
ramindani0a2bee42022-02-10 01:27:42 +0000470
471 const auto unlockStatus = mGraphicBuffer->unlockAsync(&mFillFence);
472 ASSERT_EQ(::android::OK, unlockStatus);
ramindanibab8ba92021-11-18 01:24:11 +0000473}
474
475void TestBufferLayer::setBuffer(std::vector<Color> colors) {
ramindani0a2bee42022-02-10 01:27:42 +0000476 mGraphicBuffer = allocateBuffer();
ramindanibab8ba92021-11-18 01:24:11 +0000477 ASSERT_NE(nullptr, mGraphicBuffer);
ramindanibab8ba92021-11-18 01:24:11 +0000478 ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck());
Brian Lindahle887a252023-01-17 14:54:19 -0700479 ASSERT_NO_FATAL_FAILURE(fillBuffer(colors));
ramindani0a2bee42022-02-10 01:27:42 +0000480}
481
Brian Lindahle887a252023-01-17 14:54:19 -0700482::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");
ramindanibab8ba92021-11-18 01:24:11 +0000486}
487
Ady Abraham91c9d1a2021-12-15 18:14:45 -0800488void TestBufferLayer::setToClientComposition(ComposerClientWriter& writer) {
Ady Abraham3192f3d2021-12-03 16:08:56 -0800489 writer.setLayerCompositionType(mDisplay, mLayer, Composition::CLIENT);
ramindanibab8ba92021-11-18 01:24:11 +0000490}
491
Ady Abraham3192f3d2021-12-03 16:08:56 -0800492} // namespace aidl::android::hardware::graphics::composer3::vts