ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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 | |
ramindani | 458e53e | 2022-02-23 17:30:16 +0000 | [diff] [blame] | 17 | #include "ReadbackVts.h" |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 18 | #include <aidl/android/hardware/graphics/common/BufferUsage.h> |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 19 | #include <cmath> |
ramindani | 458e53e | 2022-02-23 17:30:16 +0000 | [diff] [blame] | 20 | #include "RenderEngineVts.h" |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 21 | #include "renderengine/ExternalTexture.h" |
Vishnu Nair | 5932971 | 2022-01-13 07:59:37 -0800 | [diff] [blame] | 22 | #include "renderengine/impl/ExternalTexture.h" |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 23 | |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 30 | void TestLayer::write(ComposerClientWriter& writer) { |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 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); |
Alec Mouri | b1f1672 | 2022-02-07 13:03:44 -0800 | [diff] [blame] | 38 | writer.setLayerBrightness(mDisplay, mLayer, mBrightness); |
Alec Mouri | f6c039a | 2023-10-06 23:02:17 +0000 | [diff] [blame] | 39 | writer.setLayerDataspace(mDisplay, mLayer, mDataspace); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 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 | |
| 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 | |
| 66 | Dataspace ReadbackHelper::getDataspaceForColorMode(ColorMode mode) { |
| 67 | switch (mode) { |
| 68 | case ColorMode::DISPLAY_P3: |
| 69 | return Dataspace::DISPLAY_P3; |
| 70 | case ColorMode::SRGB: |
Sally Qi | 0ca2927 | 2024-02-23 11:07:08 -0800 | [diff] [blame] | 71 | return Dataspace::SRGB; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 72 | default: |
Sally Qi | 0ca2927 | 2024-02-23 11:07:08 -0800 | [diff] [blame] | 73 | return Dataspace::UNKNOWN; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | LayerSettings TestLayer::toRenderEngineLayerSettings() { |
| 78 | LayerSettings layerSettings; |
| 79 | |
| 80 | layerSettings.alpha = ::android::half(mAlpha); |
| 81 | layerSettings.disableBlending = mBlendMode == BlendMode::NONE; |
ramindani | c7585d9 | 2022-04-15 18:30:41 +0000 | [diff] [blame] | 82 | layerSettings.source.buffer.isOpaque = mBlendMode == BlendMode::NONE; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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; |
Alec Mouri | 5106701 | 2022-01-06 17:28:39 -0800 | [diff] [blame] | 104 | layerSettings.whitePointNits = mWhitePointNits; |
Alec Mouri | f6c039a | 2023-10-06 23:02:17 +0000 | [diff] [blame] | 105 | layerSettings.sourceDataspace = static_cast<::android::ui::Dataspace>(mDataspace); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 106 | |
| 107 | return layerSettings; |
| 108 | } |
| 109 | |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 110 | int32_t ReadbackHelper::GetBitsPerChannel(common::PixelFormat pixelFormat) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 111 | switch (pixelFormat) { |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 112 | case common::PixelFormat::RGBA_1010102: |
| 113 | return 10; |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 114 | case common::PixelFormat::RGBA_8888: |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 115 | case common::PixelFormat::RGB_888: |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 116 | return 8; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 117 | default: |
| 118 | return -1; |
| 119 | } |
| 120 | } |
| 121 | |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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 | |
| 135 | void ReadbackHelper::fillBuffer(uint32_t width, uint32_t height, uint32_t stride, |
| 136 | int32_t bytesPerPixel, void* bufferData, |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 137 | common::PixelFormat pixelFormat, |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 138 | std::vector<Color> desiredPixelColors) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 139 | ASSERT_TRUE(pixelFormat == common::PixelFormat::RGB_888 || |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 146 | ASSERT_NE(-1, bytesPerPixel); |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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; |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 153 | Color srcColor = desiredPixelColors[static_cast<size_t>(pixel)]; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 154 | |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 155 | uint32_t offset = (row * stride + col) * static_cast<uint32_t>(bytesPerPixel); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 156 | |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 181 | void ReadbackHelper::clearColors(std::vector<Color>& expectedColors, int32_t width, int32_t height, |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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; |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 186 | expectedColors[static_cast<size_t>(pixel)] = BLACK; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 191 | void ReadbackHelper::fillColorsArea(std::vector<Color>& expectedColors, int32_t stride, Rect area, |
| 192 | Color color) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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; |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 196 | expectedColors[static_cast<size_t>(pixel)] = color; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 201 | bool ReadbackHelper::readbackSupported(const common::PixelFormat& pixelFormat, |
| 202 | const common::Dataspace& dataspace) { |
| 203 | if (pixelFormat != common::PixelFormat::RGB_888 && |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 204 | pixelFormat != common::PixelFormat::RGBA_8888 && |
| 205 | pixelFormat != common::PixelFormat::RGBA_1010102) { |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 206 | return false; |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame] | 207 | } |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 208 | if (std::find(dataspaces.begin(), dataspaces.end(), dataspace) == dataspaces.end()) { |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | void ReadbackHelper::compareColorBuffers(const std::vector<Color>& expectedColors, void* bufferData, |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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; |
Ady Abraham | 1bee7ab | 2022-01-06 17:22:08 -0800 | [diff] [blame] | 228 | const Color expectedColor = expectedColors[static_cast<size_t>(pixel)]; |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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 | } |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
Alec Mouri | f6c039a | 2023-10-06 23:02:17 +0000 | [diff] [blame] | 268 | void ReadbackHelper::compareColorBuffers(void* expectedBuffer, void* actualBuffer, |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 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 | } |
Alec Mouri | f6c039a | 2023-10-06 23:02:17 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 323 | ReadbackBuffer::ReadbackBuffer(int64_t display, const std::shared_ptr<VtsComposerClient>& client, |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 324 | int32_t width, int32_t height, common::PixelFormat pixelFormat, |
| 325 | common::Dataspace dataspace) |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 326 | : mComposerClient(client) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 327 | mDisplay = display; |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 328 | |
| 329 | mPixelFormat = pixelFormat; |
| 330 | mDataspace = dataspace; |
| 331 | |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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)); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 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 | |
| 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"); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void ReadbackBuffer::setReadbackBuffer() { |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 351 | mGraphicBuffer = allocateBuffer(); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 352 | ASSERT_NE(nullptr, mGraphicBuffer); |
| 353 | ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck()); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 354 | const auto& bufferHandle = mGraphicBuffer->handle; |
| 355 | ::ndk::ScopedFileDescriptor fence = ::ndk::ScopedFileDescriptor(-1); |
| 356 | EXPECT_TRUE(mComposerClient->setReadbackBuffer(mDisplay, bufferHandle, fence).isOk()); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 357 | } |
| 358 | |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 359 | void ReadbackBuffer::checkReadbackBuffer(const std::vector<Color>& expectedColors) { |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 360 | ASSERT_NE(nullptr, mGraphicBuffer); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 361 | // lock buffer for reading |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 362 | const auto& [fenceStatus, bufferFence] = mComposerClient->getReadbackBufferFence(mDisplay); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 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); |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 372 | ASSERT_TRUE(mPixelFormat == PixelFormat::RGB_888 || mPixelFormat == PixelFormat::RGBA_8888 || |
| 373 | mPixelFormat == PixelFormat::RGBA_1010102); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 374 | const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0) |
| 375 | ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel) |
| 376 | : mGraphicBuffer->getStride(); |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 377 | ReadbackHelper::compareColorBuffers(expectedColors, bufData, stride, bytesPerPixel, mWidth, |
| 378 | mHeight, mPixelFormat); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 379 | status = mGraphicBuffer->unlock(); |
| 380 | EXPECT_EQ(::android::OK, status); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Alec Mouri | f6c039a | 2023-10-06 23:02:17 +0000 | [diff] [blame] | 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 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 392 | void TestColorLayer::write(ComposerClientWriter& writer) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 393 | TestLayer::write(writer); |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 394 | writer.setLayerCompositionType(mDisplay, mLayer, Composition::SOLID_COLOR); |
| 395 | writer.setLayerColor(mDisplay, mLayer, mColor); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | LayerSettings TestColorLayer::toRenderEngineLayerSettings() { |
| 399 | LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings(); |
| 400 | |
Ady Abraham | 1bee7ab | 2022-01-06 17:22:08 -0800 | [diff] [blame] | 401 | layerSettings.source.solidColor = ::android::half3(mColor.r, mColor.g, mColor.b); |
| 402 | layerSettings.alpha = mAlpha * mColor.a; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 403 | return layerSettings; |
| 404 | } |
| 405 | |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 406 | TestBufferLayer::TestBufferLayer(const std::shared_ptr<VtsComposerClient>& client, |
Ady Abraham | 4aa4ead | 2021-12-03 16:07:19 -0800 | [diff] [blame] | 407 | TestRenderEngine& renderEngine, int64_t display, uint32_t width, |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 408 | uint32_t height, common::PixelFormat format, |
Ady Abraham | a00d246 | 2023-12-26 14:21:20 -0800 | [diff] [blame] | 409 | ComposerClientWriter& writer, Composition composition) |
| 410 | : TestLayer{client, display, writer}, mRenderEngine(renderEngine) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 429 | void TestBufferLayer::write(ComposerClientWriter& writer) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 430 | TestLayer::write(writer); |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 431 | writer.setLayerCompositionType(mDisplay, mLayer, mComposition); |
| 432 | writer.setLayerVisibleRegion(mDisplay, mLayer, std::vector<Rect>(1, mDisplayFrame)); |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 433 | if (mGraphicBuffer) { |
| 434 | writer.setLayerBuffer(mDisplay, mLayer, /*slot*/ 0, mGraphicBuffer->handle, mFillFence); |
| 435 | } |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | LayerSettings TestBufferLayer::toRenderEngineLayerSettings() { |
| 439 | LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings(); |
Vishnu Nair | 5932971 | 2022-01-13 07:59:37 -0800 | [diff] [blame] | 440 | layerSettings.source.buffer.buffer = |
| 441 | std::make_shared<::android::renderengine::impl::ExternalTexture>( |
Alec Mouri | 3e037cf | 2022-01-24 08:58:28 -0800 | [diff] [blame] | 442 | mGraphicBuffer, mRenderEngine.getInternalRenderEngine(), |
Vishnu Nair | 5932971 | 2022-01-13 07:59:37 -0800 | [diff] [blame] | 443 | ::android::renderengine::impl::ExternalTexture::Usage::READABLE); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 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 = |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 453 | ::android::mat4::translate(::android::vec4(translateX, translateY, 0.0f, 1.0f)) * |
| 454 | ::android::mat4::scale(::android::vec4(scaleX, scaleY, 1.0f, 1.0f)); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 455 | |
| 456 | return layerSettings; |
| 457 | } |
| 458 | |
| 459 | void TestBufferLayer::fillBuffer(std::vector<Color>& expectedColors) { |
| 460 | void* bufData; |
Alec Mouri | 3e037cf | 2022-01-24 08:58:28 -0800 | [diff] [blame] | 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(); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 467 | EXPECT_EQ(::android::OK, status); |
Alec Mouri | 6a00022 | 2024-08-16 23:08:09 +0000 | [diff] [blame] | 468 | ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBuffer(mWidth, mHeight, stride, bytesPerPixel, |
| 469 | bufData, mPixelFormat, expectedColors)); |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 470 | |
| 471 | const auto unlockStatus = mGraphicBuffer->unlockAsync(&mFillFence); |
| 472 | ASSERT_EQ(::android::OK, unlockStatus); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void TestBufferLayer::setBuffer(std::vector<Color> colors) { |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 476 | mGraphicBuffer = allocateBuffer(); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 477 | ASSERT_NE(nullptr, mGraphicBuffer); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 478 | ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck()); |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 479 | ASSERT_NO_FATAL_FAILURE(fillBuffer(colors)); |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Brian Lindahl | e887a25 | 2023-01-17 14:54:19 -0700 | [diff] [blame] | 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"); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 488 | void TestBufferLayer::setToClientComposition(ComposerClientWriter& writer) { |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 489 | writer.setLayerCompositionType(mDisplay, mLayer, Composition::CLIENT); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 492 | } // namespace aidl::android::hardware::graphics::composer3::vts |