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> |
ramindani | 458e53e | 2022-02-23 17:30:16 +0000 | [diff] [blame] | 19 | #include "RenderEngineVts.h" |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 20 | #include "renderengine/ExternalTexture.h" |
Vishnu Nair | 5932971 | 2022-01-13 07:59:37 -0800 | [diff] [blame] | 21 | #include "renderengine/impl/ExternalTexture.h" |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 22 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 23 | using ::android::status_t; |
| 24 | |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 25 | namespace aidl::android::hardware::graphics::composer3::vts { |
| 26 | |
| 27 | const std::vector<ColorMode> ReadbackHelper::colorModes = {ColorMode::SRGB, ColorMode::DISPLAY_P3}; |
| 28 | const std::vector<Dataspace> ReadbackHelper::dataspaces = {common::Dataspace::SRGB, |
| 29 | common::Dataspace::DISPLAY_P3}; |
| 30 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 31 | void TestLayer::write(ComposerClientWriter& writer) { |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 32 | ::android::status_t status = ::android::OK; |
| 33 | ASSERT_EQ(::android::OK, status); |
| 34 | |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 35 | writer.setLayerDisplayFrame(mDisplay, mLayer, mDisplayFrame); |
| 36 | writer.setLayerSourceCrop(mDisplay, mLayer, mSourceCrop); |
| 37 | writer.setLayerZOrder(mDisplay, mLayer, mZOrder); |
| 38 | writer.setLayerSurfaceDamage(mDisplay, mLayer, mSurfaceDamage); |
| 39 | writer.setLayerTransform(mDisplay, mLayer, mTransform); |
| 40 | writer.setLayerPlaneAlpha(mDisplay, mLayer, mAlpha); |
| 41 | writer.setLayerBlendMode(mDisplay, mLayer, mBlendMode); |
Alec Mouri | b1f1672 | 2022-02-07 13:03:44 -0800 | [diff] [blame] | 42 | writer.setLayerBrightness(mDisplay, mLayer, mBrightness); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 45 | bool ReadbackHelper::readbackSupported(const common::PixelFormat& pixelFormat, |
| 46 | const common::Dataspace& dataspace) { |
| 47 | // TODO: add support for RGBA_1010102 |
| 48 | if (pixelFormat != common::PixelFormat::RGB_888 && |
| 49 | pixelFormat != common::PixelFormat::RGBA_8888) { |
| 50 | return false; |
| 51 | } |
| 52 | if (std::find(dataspaces.begin(), dataspaces.end(), dataspace) == dataspaces.end()) { |
| 53 | return false; |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | void ReadbackHelper::createReadbackBuffer(ReadbackBufferAttributes readbackBufferAttributes, |
| 59 | const VtsDisplay& display, |
| 60 | sp<GraphicBuffer>* graphicBuffer) { |
| 61 | ASSERT_NE(nullptr, graphicBuffer); |
| 62 | if (!readbackSupported(readbackBufferAttributes.format, readbackBufferAttributes.dataspace)) { |
| 63 | *graphicBuffer = nullptr; |
| 64 | } |
| 65 | uint64_t usage = |
| 66 | static_cast<uint64_t>(static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) | |
| 67 | static_cast<uint64_t>(common::BufferUsage::GPU_TEXTURE)); |
| 68 | |
| 69 | uint32_t layerCount = 1; |
| 70 | *graphicBuffer = sp<GraphicBuffer>::make( |
| 71 | static_cast<uint32_t>(display.getDisplayWidth()), |
| 72 | static_cast<uint32_t>(display.getDisplayHeight()), |
| 73 | static_cast<::android::PixelFormat>(readbackBufferAttributes.format), layerCount, usage, |
| 74 | "ReadbackBuffer"); |
| 75 | ASSERT_NE(nullptr, *graphicBuffer); |
| 76 | ASSERT_EQ(::android::OK, (*graphicBuffer)->initCheck()); |
| 77 | } |
| 78 | |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 79 | std::string ReadbackHelper::getColorModeString(ColorMode mode) { |
| 80 | switch (mode) { |
| 81 | case ColorMode::SRGB: |
| 82 | return {"SRGB"}; |
| 83 | case ColorMode::DISPLAY_P3: |
| 84 | return {"DISPLAY_P3"}; |
| 85 | default: |
| 86 | return {"Unsupported color mode for readback"}; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | std::string ReadbackHelper::getDataspaceString(common::Dataspace dataspace) { |
| 91 | switch (dataspace) { |
| 92 | case common::Dataspace::SRGB: |
| 93 | return {"SRGB"}; |
| 94 | case common::Dataspace::DISPLAY_P3: |
| 95 | return {"DISPLAY_P3"}; |
| 96 | case common::Dataspace::UNKNOWN: |
| 97 | return {"UNKNOWN"}; |
| 98 | default: |
| 99 | return {"Unsupported dataspace for readback"}; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | Dataspace ReadbackHelper::getDataspaceForColorMode(ColorMode mode) { |
| 104 | switch (mode) { |
| 105 | case ColorMode::DISPLAY_P3: |
| 106 | return Dataspace::DISPLAY_P3; |
| 107 | case ColorMode::SRGB: |
| 108 | default: |
| 109 | return common::Dataspace::UNKNOWN; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | LayerSettings TestLayer::toRenderEngineLayerSettings() { |
| 114 | LayerSettings layerSettings; |
| 115 | |
| 116 | layerSettings.alpha = ::android::half(mAlpha); |
| 117 | layerSettings.disableBlending = mBlendMode == BlendMode::NONE; |
ramindani | c7585d9 | 2022-04-15 18:30:41 +0000 | [diff] [blame] | 118 | layerSettings.source.buffer.isOpaque = mBlendMode == BlendMode::NONE; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 119 | layerSettings.geometry.boundaries = ::android::FloatRect( |
| 120 | static_cast<float>(mDisplayFrame.left), static_cast<float>(mDisplayFrame.top), |
| 121 | static_cast<float>(mDisplayFrame.right), static_cast<float>(mDisplayFrame.bottom)); |
| 122 | |
| 123 | const ::android::mat4 translation = ::android::mat4::translate(::android::vec4( |
| 124 | (static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_H) |
| 125 | ? static_cast<float>(-mDisplayFrame.right) |
| 126 | : 0.0f), |
| 127 | (static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_V) |
| 128 | ? static_cast<float>(-mDisplayFrame.bottom) |
| 129 | : 0.0f), |
| 130 | 0.0f, 1.0f)); |
| 131 | |
| 132 | const ::android::mat4 scale = ::android::mat4::scale(::android::vec4( |
| 133 | static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_H) ? -1.0f |
| 134 | : 1.0f, |
| 135 | static_cast<uint64_t>(mTransform) & static_cast<uint64_t>(Transform::FLIP_V) ? -1.0f |
| 136 | : 1.0f, |
| 137 | 1.0f, 1.0f)); |
| 138 | |
| 139 | layerSettings.geometry.positionTransform = scale * translation; |
Alec Mouri | 5106701 | 2022-01-06 17:28:39 -0800 | [diff] [blame] | 140 | layerSettings.whitePointNits = mWhitePointNits; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 141 | |
| 142 | return layerSettings; |
| 143 | } |
| 144 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 145 | int32_t ReadbackHelper::GetBytesPerPixel(PixelFormat pixelFormat) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 146 | switch (pixelFormat) { |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 147 | case PixelFormat::RGBA_8888: |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 148 | return 4; |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 149 | case PixelFormat::RGB_888: |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 150 | return 3; |
| 151 | default: |
| 152 | return -1; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void ReadbackHelper::fillBuffer(uint32_t width, uint32_t height, uint32_t stride, void* bufferData, |
| 157 | common::PixelFormat pixelFormat, |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 158 | const std::vector<Color>& desiredColors) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 159 | ASSERT_TRUE(pixelFormat == common::PixelFormat::RGB_888 || |
| 160 | pixelFormat == common::PixelFormat::RGBA_8888); |
| 161 | int32_t bytesPerPixel = GetBytesPerPixel(pixelFormat); |
| 162 | ASSERT_NE(-1, bytesPerPixel); |
| 163 | for (int row = 0; row < height; row++) { |
| 164 | for (int col = 0; col < width; col++) { |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 165 | int pixel = row * static_cast<int32_t>(width) + col; |
| 166 | Color desiredColor = desiredColors[static_cast<size_t>(pixel)]; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 167 | |
| 168 | int offset = (row * static_cast<int32_t>(stride) + col) * bytesPerPixel; |
| 169 | uint8_t* pixelColor = (uint8_t*)bufferData + offset; |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 170 | pixelColor[0] = static_cast<uint8_t>(std::round(255.0f * desiredColor.r)); |
| 171 | pixelColor[1] = static_cast<uint8_t>(std::round(255.0f * desiredColor.g)); |
| 172 | pixelColor[2] = static_cast<uint8_t>(std::round(255.0f * desiredColor.b)); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 173 | |
| 174 | if (bytesPerPixel == 4) { |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 175 | pixelColor[3] = static_cast<uint8_t>(std::round(255.0f * desiredColor.a)); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 181 | void ReadbackHelper::clearColors(std::vector<Color>& colors, 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 | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 186 | colors[static_cast<size_t>(pixel)] = BLACK; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 191 | void ReadbackHelper::fillColorsArea(std::vector<Color>& colors, int32_t stride, Rect area, |
| 192 | Color desiredColor) { |
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 | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 196 | colors[static_cast<size_t>(pixel)] = desiredColor; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 201 | void ReadbackHelper::fillBufferAndGetFence(const sp<GraphicBuffer>& graphicBuffer, |
| 202 | Color desiredColor, int* fillFence) { |
| 203 | ASSERT_NE(nullptr, fillFence); |
| 204 | std::vector<Color> desiredColors( |
| 205 | static_cast<size_t>(graphicBuffer->getWidth() * graphicBuffer->getHeight())); |
| 206 | ::android::Rect bounds = graphicBuffer->getBounds(); |
| 207 | fillColorsArea(desiredColors, static_cast<int32_t>(graphicBuffer->getWidth()), |
| 208 | {bounds.left, bounds.top, bounds.right, bounds.bottom}, desiredColor); |
| 209 | ASSERT_NO_FATAL_FAILURE(fillBufferAndGetFence(graphicBuffer, desiredColors, fillFence)); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 212 | void ReadbackHelper::fillBufferAndGetFence(const sp<GraphicBuffer>& graphicBuffer, |
| 213 | const std::vector<Color>& desiredColors, |
| 214 | int* fillFence) { |
| 215 | ASSERT_TRUE(graphicBuffer->getPixelFormat() == ::android::PIXEL_FORMAT_RGB_888 || |
| 216 | graphicBuffer->getPixelFormat() == ::android::PIXEL_FORMAT_RGBA_8888); |
| 217 | void* bufData; |
| 218 | int32_t bytesPerPixel = -1; |
| 219 | int32_t bytesPerStride = -1; |
| 220 | status_t status = |
| 221 | graphicBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 222 | &bufData, &bytesPerPixel, &bytesPerStride); |
| 223 | ASSERT_EQ(::android::OK, status); |
| 224 | |
| 225 | const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0) |
| 226 | ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel) |
| 227 | : graphicBuffer->getStride(); |
| 228 | ReadbackHelper::fillBuffer( |
| 229 | graphicBuffer->getWidth(), graphicBuffer->getHeight(), stride, bufData, |
| 230 | static_cast<common::PixelFormat>(graphicBuffer->getPixelFormat()), desiredColors); |
| 231 | status = graphicBuffer->unlockAsync(fillFence); |
| 232 | ASSERT_EQ(::android::OK, status); |
| 233 | } |
| 234 | |
| 235 | void ReadbackHelper::compareColorToBuffer(Color expectedColor, |
| 236 | const sp<GraphicBuffer>& graphicBuffer, |
| 237 | const ndk::ScopedFileDescriptor& fence) { |
| 238 | std::vector<Color> expectedColors( |
| 239 | static_cast<size_t>(graphicBuffer->getWidth() * graphicBuffer->getHeight())); |
| 240 | ::android::Rect bounds = graphicBuffer->getBounds(); |
| 241 | fillColorsArea(expectedColors, static_cast<int32_t>(graphicBuffer->getWidth()), |
| 242 | {bounds.left, bounds.top, bounds.right, bounds.bottom}, expectedColor); |
| 243 | compareColorsToBuffer(expectedColors, graphicBuffer, fence); |
| 244 | } |
| 245 | |
| 246 | void ReadbackHelper::compareColorsToBuffer(const std::vector<Color>& expectedColors, |
| 247 | const sp<GraphicBuffer>& graphicBuffer, |
| 248 | const ndk::ScopedFileDescriptor& fence) { |
| 249 | ASSERT_TRUE(graphicBuffer->getPixelFormat() == ::android::PIXEL_FORMAT_RGB_888 || |
| 250 | graphicBuffer->getPixelFormat() == ::android::PIXEL_FORMAT_RGBA_8888); |
| 251 | |
| 252 | int bytesPerPixel = -1; |
| 253 | int bytesPerStride = -1; |
| 254 | void* bufData = nullptr; |
| 255 | status_t status = graphicBuffer->lockAsync(GRALLOC_USAGE_SW_READ_OFTEN, &bufData, |
| 256 | dup(fence.get()), &bytesPerPixel, &bytesPerStride); |
| 257 | ASSERT_EQ(::android::OK, status); |
| 258 | |
| 259 | const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0) |
| 260 | ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel) |
| 261 | : graphicBuffer->getStride(); |
| 262 | |
| 263 | if (bytesPerPixel == -1) { |
| 264 | bytesPerPixel = ReadbackHelper::GetBytesPerPixel( |
| 265 | static_cast<PixelFormat>(graphicBuffer->getPixelFormat())); |
| 266 | } |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 267 | ASSERT_NE(-1, bytesPerPixel); |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 268 | for (int row = 0; row < graphicBuffer->getHeight(); row++) { |
| 269 | for (int col = 0; col < graphicBuffer->getWidth(); col++) { |
| 270 | int pixel = row * static_cast<int32_t>(graphicBuffer->getWidth()) + col; |
Alec Mouri | 3e037cf | 2022-01-24 08:58:28 -0800 | [diff] [blame] | 271 | int offset = (row * static_cast<int32_t>(stride) + col) * bytesPerPixel; |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 272 | uint8_t* pixelColor = (uint8_t*)bufData + offset; |
Ady Abraham | 1bee7ab | 2022-01-06 17:22:08 -0800 | [diff] [blame] | 273 | const Color expectedColor = expectedColors[static_cast<size_t>(pixel)]; |
Ady Abraham | 1bee7ab | 2022-01-06 17:22:08 -0800 | [diff] [blame] | 274 | ASSERT_EQ(std::round(255.0f * expectedColor.r), pixelColor[0]); |
| 275 | ASSERT_EQ(std::round(255.0f * expectedColor.g), pixelColor[1]); |
| 276 | ASSERT_EQ(std::round(255.0f * expectedColor.b), pixelColor[2]); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 279 | |
| 280 | status = graphicBuffer->unlock(); |
| 281 | ASSERT_EQ(::android::OK, status); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 282 | } |
| 283 | |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 284 | ReadbackBuffer::ReadbackBuffer(int64_t display, const std::shared_ptr<VtsComposerClient>& client, |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 285 | int32_t width, int32_t height, common::PixelFormat pixelFormat) |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 286 | : mComposerClient(client) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 287 | mDisplay = display; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 288 | mWidth = static_cast<uint32_t>(width); |
| 289 | mHeight = static_cast<uint32_t>(height); |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 290 | mPixelFormat = pixelFormat; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 291 | mLayerCount = 1; |
| 292 | mUsage = static_cast<uint64_t>(static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) | |
| 293 | static_cast<uint64_t>(common::BufferUsage::GPU_TEXTURE)); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void ReadbackBuffer::setReadbackBuffer() { |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 297 | mGraphicBuffer = sp<GraphicBuffer>::make(mWidth, mHeight, |
| 298 | static_cast<::android::PixelFormat>(mPixelFormat), |
| 299 | mLayerCount, mUsage, "ReadbackBuffer"); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 300 | ASSERT_NE(nullptr, mGraphicBuffer); |
| 301 | ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck()); |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 302 | ::ndk::ScopedFileDescriptor noFence = ::ndk::ScopedFileDescriptor(-1); |
| 303 | const auto& status = |
| 304 | mComposerClient->setReadbackBuffer(mDisplay, mGraphicBuffer->handle, noFence); |
| 305 | ASSERT_TRUE(status.isOk()); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 306 | } |
| 307 | |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 308 | void ReadbackBuffer::checkReadbackBuffer(const std::vector<Color>& expectedColors) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 309 | // lock buffer for reading |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 310 | const auto& [fenceStatus, bufferFence] = mComposerClient->getReadbackBufferFence(mDisplay); |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 311 | ASSERT_TRUE(fenceStatus.isOk()); |
| 312 | ReadbackHelper::compareColorsToBuffer(expectedColors, mGraphicBuffer, bufferFence); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 315 | void TestColorLayer::write(ComposerClientWriter& writer) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 316 | TestLayer::write(writer); |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 317 | writer.setLayerCompositionType(mDisplay, mLayer, Composition::SOLID_COLOR); |
| 318 | writer.setLayerColor(mDisplay, mLayer, mColor); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | LayerSettings TestColorLayer::toRenderEngineLayerSettings() { |
| 322 | LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings(); |
| 323 | |
Ady Abraham | 1bee7ab | 2022-01-06 17:22:08 -0800 | [diff] [blame] | 324 | layerSettings.source.solidColor = ::android::half3(mColor.r, mColor.g, mColor.b); |
| 325 | layerSettings.alpha = mAlpha * mColor.a; |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 326 | return layerSettings; |
| 327 | } |
| 328 | |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 329 | TestBufferLayer::TestBufferLayer(const std::shared_ptr<VtsComposerClient>& client, |
Ady Abraham | 4aa4ead | 2021-12-03 16:07:19 -0800 | [diff] [blame] | 330 | TestRenderEngine& renderEngine, int64_t display, uint32_t width, |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 331 | uint32_t height, common::PixelFormat format, |
| 332 | Composition composition) |
| 333 | : TestLayer{client, display}, mRenderEngine(renderEngine) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 334 | mComposition = composition; |
| 335 | mWidth = width; |
| 336 | mHeight = height; |
| 337 | mLayerCount = 1; |
| 338 | mPixelFormat = format; |
| 339 | mUsage = (static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) | |
| 340 | static_cast<uint64_t>(common::BufferUsage::CPU_WRITE_OFTEN) | |
| 341 | static_cast<uint64_t>(common::BufferUsage::COMPOSER_OVERLAY) | |
| 342 | static_cast<uint64_t>(common::BufferUsage::GPU_TEXTURE)); |
| 343 | |
| 344 | mAccessRegion.top = 0; |
| 345 | mAccessRegion.left = 0; |
| 346 | mAccessRegion.right = static_cast<int32_t>(width); |
| 347 | mAccessRegion.bottom = static_cast<int32_t>(height); |
| 348 | |
| 349 | setSourceCrop({0, 0, (float)width, (float)height}); |
| 350 | } |
| 351 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 352 | void TestBufferLayer::write(ComposerClientWriter& writer) { |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 353 | TestLayer::write(writer); |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 354 | writer.setLayerCompositionType(mDisplay, mLayer, mComposition); |
| 355 | writer.setLayerVisibleRegion(mDisplay, mLayer, std::vector<Rect>(1, mDisplayFrame)); |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 356 | if (mGraphicBuffer) { |
| 357 | writer.setLayerBuffer(mDisplay, mLayer, /*slot*/ 0, mGraphicBuffer->handle, mFillFence); |
| 358 | } |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | LayerSettings TestBufferLayer::toRenderEngineLayerSettings() { |
| 362 | LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings(); |
Vishnu Nair | 5932971 | 2022-01-13 07:59:37 -0800 | [diff] [blame] | 363 | layerSettings.source.buffer.buffer = |
| 364 | std::make_shared<::android::renderengine::impl::ExternalTexture>( |
Alec Mouri | 3e037cf | 2022-01-24 08:58:28 -0800 | [diff] [blame] | 365 | mGraphicBuffer, mRenderEngine.getInternalRenderEngine(), |
Vishnu Nair | 5932971 | 2022-01-13 07:59:37 -0800 | [diff] [blame] | 366 | ::android::renderengine::impl::ExternalTexture::Usage::READABLE); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 367 | |
| 368 | layerSettings.source.buffer.usePremultipliedAlpha = mBlendMode == BlendMode::PREMULTIPLIED; |
| 369 | |
| 370 | const float scaleX = (mSourceCrop.right - mSourceCrop.left) / (static_cast<float>(mWidth)); |
| 371 | const float scaleY = (mSourceCrop.bottom - mSourceCrop.top) / (static_cast<float>(mHeight)); |
| 372 | const float translateX = mSourceCrop.left / (static_cast<float>(mWidth)); |
| 373 | const float translateY = mSourceCrop.top / (static_cast<float>(mHeight)); |
| 374 | |
| 375 | layerSettings.source.buffer.textureTransform = |
ramindani | dcecfd4 | 2022-02-03 23:52:19 +0000 | [diff] [blame] | 376 | ::android::mat4::translate(::android::vec4(translateX, translateY, 0.0f, 1.0f)) * |
| 377 | ::android::mat4::scale(::android::vec4(scaleX, scaleY, 1.0f, 1.0f)); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 378 | |
| 379 | return layerSettings; |
| 380 | } |
| 381 | |
| 382 | void TestBufferLayer::fillBuffer(std::vector<Color>& expectedColors) { |
| 383 | void* bufData; |
Alec Mouri | 3e037cf | 2022-01-24 08:58:28 -0800 | [diff] [blame] | 384 | int32_t bytesPerPixel = -1; |
| 385 | int32_t bytesPerStride = -1; |
| 386 | auto status = mGraphicBuffer->lock(mUsage, &bufData, &bytesPerPixel, &bytesPerStride); |
| 387 | const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0) |
| 388 | ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel) |
| 389 | : mGraphicBuffer->getStride(); |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 390 | ASSERT_EQ(::android::OK, status); |
| 391 | ReadbackHelper::fillBuffer(mWidth, mHeight, stride, bufData, mPixelFormat, expectedColors); |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 392 | |
| 393 | const auto unlockStatus = mGraphicBuffer->unlockAsync(&mFillFence); |
| 394 | ASSERT_EQ(::android::OK, unlockStatus); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void TestBufferLayer::setBuffer(std::vector<Color> colors) { |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 398 | mGraphicBuffer = allocateBuffer(); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 399 | ASSERT_NE(nullptr, mGraphicBuffer); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 400 | ASSERT_EQ(::android::OK, mGraphicBuffer->initCheck()); |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 401 | fillBuffer(colors); |
ramindani | 0a2bee4 | 2022-02-10 01:27:42 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Brian Lindahl | d103cd6 | 2022-12-09 07:26:28 -0700 | [diff] [blame^] | 404 | sp<GraphicBuffer> TestBufferLayer::allocateBuffer() { |
| 405 | return sp<GraphicBuffer>::make(mWidth, mHeight, |
| 406 | static_cast<::android::PixelFormat>(mPixelFormat), mLayerCount, |
| 407 | mUsage, "TestBufferLayer"); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 410 | void TestBufferLayer::setDataspace(common::Dataspace dataspace, ComposerClientWriter& writer) { |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 411 | writer.setLayerDataspace(mDisplay, mLayer, dataspace); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Ady Abraham | 91c9d1a | 2021-12-15 18:14:45 -0800 | [diff] [blame] | 414 | void TestBufferLayer::setToClientComposition(ComposerClientWriter& writer) { |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 415 | writer.setLayerCompositionType(mDisplay, mLayer, Composition::CLIENT); |
ramindani | bab8ba9 | 2021-11-18 01:24:11 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Ady Abraham | 3192f3d | 2021-12-03 16:08:56 -0800 | [diff] [blame] | 418 | } // namespace aidl::android::hardware::graphics::composer3::vts |