blob: 93d9693dbd9c60c6d2cd55a14764b20baee148d9 [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
17#define LOG_TAG "graphics_composer_aidl_hal_readback_tests@3"
18
19#include <aidl/Gtest.h>
20#include <aidl/Vintf.h>
Ady Abraham46219f52021-12-20 09:44:31 -080021#include <aidl/android/hardware/graphics/composer3/IComposer.h>
ramindanibab8ba92021-11-18 01:24:11 +000022#include <gtest/gtest.h>
Alec Mouri51067012022-01-06 17:28:39 -080023#include <ui/DisplayId.h>
24#include <ui/DisplayIdentification.h>
ramindanibab8ba92021-11-18 01:24:11 +000025#include <ui/GraphicBuffer.h>
ramindanibab8ba92021-11-18 01:24:11 +000026#include <ui/PixelFormat.h>
27#include <ui/Rect.h>
ramindani458e53e2022-02-23 17:30:16 +000028#include "GraphicsComposerCallback.h"
29#include "ReadbackVts.h"
30#include "RenderEngineVts.h"
31#include "VtsComposerClient.h"
Alec Mouri51067012022-01-06 17:28:39 -080032
33// tinyxml2 does implicit conversions >:(
34#pragma clang diagnostic push
35#pragma clang diagnostic ignored "-Wconversion"
36#include <tinyxml2.h>
37#pragma clang diagnostic pop
ramindanibab8ba92021-11-18 01:24:11 +000038
39namespace aidl::android::hardware::graphics::composer3::vts {
40namespace {
41
42using ::android::Rect;
43using common::Dataspace;
44using common::PixelFormat;
45
46class GraphicsCompositionTestBase : public ::testing::Test {
47 protected:
48 void SetUpBase(const std::string& name) {
ramindanidcecfd42022-02-03 23:52:19 +000049 mComposerClient = std::make_shared<VtsComposerClient>(name);
50 ASSERT_TRUE(mComposerClient->createClient().isOk());
ramindanibab8ba92021-11-18 01:24:11 +000051
ramindanidcecfd42022-02-03 23:52:19 +000052 const auto& [status, displays] = mComposerClient->getDisplays();
53 ASSERT_TRUE(status.isOk());
54 mDisplays = displays;
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -040055 mWriter.reset(new ComposerClientWriter(getPrimaryDisplayId()));
ramindanibab8ba92021-11-18 01:24:11 +000056
57 setTestColorModes();
58
59 // explicitly disable vsync
ramindanidcecfd42022-02-03 23:52:19 +000060 for (const auto& display : mDisplays) {
61 EXPECT_TRUE(mComposerClient->setVsync(display.getDisplayId(), /*enable*/ false).isOk());
62 }
63 mComposerClient->setVsyncAllowed(/*isAllowed*/ false);
ramindanibab8ba92021-11-18 01:24:11 +000064
ramindanidcecfd42022-02-03 23:52:19 +000065 EXPECT_TRUE(mComposerClient->setPowerMode(getPrimaryDisplayId(), PowerMode::ON).isOk());
ramindanibab8ba92021-11-18 01:24:11 +000066
67 ASSERT_NO_FATAL_FAILURE(
68 mTestRenderEngine = std::unique_ptr<TestRenderEngine>(new TestRenderEngine(
69 ::android::renderengine::RenderEngineCreationArgs::Builder()
70 .setPixelFormat(static_cast<int>(common::PixelFormat::RGBA_8888))
71 .setImageCacheSize(TestRenderEngine::sMaxFrameBufferAcquireBuffers)
72 .setUseColorManagerment(true)
73 .setEnableProtectedContext(false)
74 .setPrecacheToneMapperShaderOnly(false)
75 .setContextPriority(::android::renderengine::RenderEngine::
76 ContextPriority::HIGH)
77 .build())));
78
79 ::android::renderengine::DisplaySettings clientCompositionDisplay;
ramindanidcecfd42022-02-03 23:52:19 +000080 clientCompositionDisplay.physicalDisplay = Rect(getDisplayWidth(), getDisplayHeight());
ramindanibab8ba92021-11-18 01:24:11 +000081 clientCompositionDisplay.clip = clientCompositionDisplay.physicalDisplay;
82
Brian Lindahld103cd62022-12-09 07:26:28 -070083 mTestRenderEngine->initGraphicBuffer(static_cast<uint32_t>(getDisplayWidth()),
84 static_cast<uint32_t>(getDisplayHeight()),
85 /*layerCount*/ 1U,
86 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_SW_READ_OFTEN |
87 GRALLOC_USAGE_SW_WRITE_OFTEN);
ramindanibab8ba92021-11-18 01:24:11 +000088 mTestRenderEngine->setDisplaySettings(clientCompositionDisplay);
89 }
90
91 void TearDown() override {
ramindanidcecfd42022-02-03 23:52:19 +000092 ASSERT_TRUE(mComposerClient->setPowerMode(getPrimaryDisplayId(), PowerMode::OFF).isOk());
93 ASSERT_TRUE(mComposerClient->tearDown());
94 mComposerClient.reset();
Ady Abraham3192f3d2021-12-03 16:08:56 -080095 const auto errors = mReader.takeErrors();
96 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanidcecfd42022-02-03 23:52:19 +000097 ASSERT_TRUE(mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty());
ramindanibab8ba92021-11-18 01:24:11 +000098 }
99
ramindanidcecfd42022-02-03 23:52:19 +0000100 const VtsDisplay& getPrimaryDisplay() const { return mDisplays[0]; }
101
102 int64_t getPrimaryDisplayId() const { return getPrimaryDisplay().getDisplayId(); }
103
104 int64_t getInvalidDisplayId() const { return mComposerClient->getInvalidDisplayId(); }
105
106 int32_t getDisplayWidth() const { return getPrimaryDisplay().getDisplayWidth(); }
107
108 int32_t getDisplayHeight() const { return getPrimaryDisplay().getDisplayHeight(); }
109
ramindanid5751092022-04-22 22:30:20 +0000110 void assertServiceSpecificError(const ScopedAStatus& status, int32_t serviceSpecificError) {
111 ASSERT_EQ(status.getExceptionCode(), EX_SERVICE_SPECIFIC);
112 ASSERT_EQ(status.getServiceSpecificError(), serviceSpecificError);
113 }
114
Brian Lindahld103cd62022-12-09 07:26:28 -0700115 sp<GraphicBuffer> allocateBuffer(uint32_t width, uint32_t height, uint64_t usage) {
116 sp<GraphicBuffer> graphicBuffer =
117 sp<GraphicBuffer>::make(width, height, ::android::PIXEL_FORMAT_RGBA_8888,
118 /*layerCount*/ 1u, static_cast<uint32_t>(usage),
119 "VtsHalGraphicsComposer3_ReadbackTest");
ramindani0a2bee42022-02-10 01:27:42 +0000120
121 if (graphicBuffer && ::android::OK == graphicBuffer->initCheck()) {
Brian Lindahld103cd62022-12-09 07:26:28 -0700122 return graphicBuffer;
ramindani0a2bee42022-02-10 01:27:42 +0000123 }
Brian Lindahld103cd62022-12-09 07:26:28 -0700124 return nullptr;
125 }
126
127 sp<GraphicBuffer> allocateBuffer(uint64_t usage) {
128 return allocateBuffer(static_cast<uint32_t>(getDisplayWidth()),
129 static_cast<uint32_t>(getDisplayHeight()), usage);
ramindanibab8ba92021-11-18 01:24:11 +0000130 }
131
Alec Mouri51067012022-01-06 17:28:39 -0800132 uint64_t getStableDisplayId(int64_t display) {
ramindanidcecfd42022-02-03 23:52:19 +0000133 const auto& [status, identification] =
134 mComposerClient->getDisplayIdentificationData(display);
135 EXPECT_TRUE(status.isOk());
Alec Mouri51067012022-01-06 17:28:39 -0800136
137 if (const auto info = ::android::parseDisplayIdentificationData(
138 static_cast<uint8_t>(identification.port), identification.data)) {
139 return info->id.value;
140 }
141
142 return ::android::PhysicalDisplayId::fromPort(static_cast<uint8_t>(identification.port))
143 .value;
144 }
145
146 // Gets the per-display XML config
147 std::unique_ptr<tinyxml2::XMLDocument> getDisplayConfigXml(int64_t display) {
148 std::stringstream pathBuilder;
149 pathBuilder << "/vendor/etc/displayconfig/display_id_" << getStableDisplayId(display)
150 << ".xml";
151 const std::string path = pathBuilder.str();
152 auto document = std::make_unique<tinyxml2::XMLDocument>();
153 const tinyxml2::XMLError error = document->LoadFile(path.c_str());
154 if (error == tinyxml2::XML_SUCCESS) {
155 return document;
156 } else {
157 return nullptr;
158 }
159 }
160
161 // Gets the max display brightness for this display.
162 // If the display config xml does not exist, then assume that the display is not well-configured
163 // enough to provide a display brightness, so return nullopt.
164 std::optional<float> getMaxDisplayBrightnessNits(int64_t display) {
165 const auto document = getDisplayConfigXml(display);
166 if (!document) {
167 // Assume the device doesn't support display brightness
168 return std::nullopt;
169 }
170
171 const auto root = document->RootElement();
172 if (!root) {
173 // If there's somehow no root element, then this isn't a valid config
174 return std::nullopt;
175 }
176
177 const auto screenBrightnessMap = root->FirstChildElement("screenBrightnessMap");
178 if (!screenBrightnessMap) {
179 // A valid display config must have a screen brightness map
180 return std::nullopt;
181 }
182
183 auto point = screenBrightnessMap->FirstChildElement("point");
184 float maxNits = -1.f;
185 while (point != nullptr) {
186 const auto nits = point->FirstChildElement("nits");
187 if (nits) {
188 maxNits = std::max(maxNits, nits->FloatText(-1.f));
189 }
190 point = point->NextSiblingElement("point");
191 }
192
193 if (maxNits < 0.f) {
194 // If we got here, then there were no point elements containing a nit value, so this
195 // config isn't valid
196 return std::nullopt;
197 }
198
199 return maxNits;
200 }
201
ramindanibab8ba92021-11-18 01:24:11 +0000202 void writeLayers(const std::vector<std::shared_ptr<TestLayer>>& layers) {
ramindanidcecfd42022-02-03 23:52:19 +0000203 for (const auto& layer : layers) {
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400204 layer->write(*mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000205 }
206 execute();
207 }
208
209 void execute() {
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400210 const auto& commands = mWriter->getPendingCommands();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800211 if (commands.empty()) {
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400212 mWriter->reset();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800213 return;
ramindanibab8ba92021-11-18 01:24:11 +0000214 }
215
ramindanidcecfd42022-02-03 23:52:19 +0000216 auto [status, results] = mComposerClient->executeCommands(commands);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800217 ASSERT_TRUE(status.isOk()) << "executeCommands failed " << status.getDescription();
ramindanibab8ba92021-11-18 01:24:11 +0000218
Ady Abraham46219f52021-12-20 09:44:31 -0800219 mReader.parse(std::move(results));
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400220 mWriter->reset();
ramindanibab8ba92021-11-18 01:24:11 +0000221 }
222
ramindani44c952f2022-02-28 23:29:29 +0000223 bool getHasReadbackBuffer() {
ramindanidcecfd42022-02-03 23:52:19 +0000224 auto [status, readBackBufferAttributes] =
225 mComposerClient->getReadbackBufferAttributes(getPrimaryDisplayId());
226 if (status.isOk()) {
227 mPixelFormat = readBackBufferAttributes.format;
228 mDataspace = readBackBufferAttributes.dataspace;
ramindani44c952f2022-02-28 23:29:29 +0000229 return ReadbackHelper::readbackSupported(mPixelFormat, mDataspace);
ramindanidcecfd42022-02-03 23:52:19 +0000230 }
ramindanid5751092022-04-22 22:30:20 +0000231 EXPECT_NO_FATAL_FAILURE(
232 assertServiceSpecificError(status, IComposerClient::EX_UNSUPPORTED));
ramindani44c952f2022-02-28 23:29:29 +0000233 return false;
ramindanib27f33b2021-12-03 19:36:10 +0000234 }
235
ramindanidcecfd42022-02-03 23:52:19 +0000236 std::shared_ptr<VtsComposerClient> mComposerClient;
237 std::vector<VtsDisplay> mDisplays;
238 // use the slot count usually set by SF
ramindanibab8ba92021-11-18 01:24:11 +0000239 std::vector<ColorMode> mTestColorModes;
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400240 std::unique_ptr<ComposerClientWriter> mWriter;
Ady Abraham91c9d1a2021-12-15 18:14:45 -0800241 ComposerClientReader mReader;
ramindanibab8ba92021-11-18 01:24:11 +0000242 std::unique_ptr<TestRenderEngine> mTestRenderEngine;
ramindanibab8ba92021-11-18 01:24:11 +0000243 common::PixelFormat mPixelFormat;
244 common::Dataspace mDataspace;
245
246 static constexpr uint32_t kClientTargetSlotCount = 64;
247
248 private:
ramindanibab8ba92021-11-18 01:24:11 +0000249 void setTestColorModes() {
250 mTestColorModes.clear();
ramindanidcecfd42022-02-03 23:52:19 +0000251 const auto& [status, modes] = mComposerClient->getColorModes(getPrimaryDisplayId());
252 ASSERT_TRUE(status.isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000253
254 for (ColorMode mode : modes) {
255 if (std::find(ReadbackHelper::colorModes.begin(), ReadbackHelper::colorModes.end(),
256 mode) != ReadbackHelper::colorModes.end()) {
257 mTestColorModes.push_back(mode);
258 }
259 }
260 }
261};
262
263class GraphicsCompositionTest : public GraphicsCompositionTestBase,
264 public testing::WithParamInterface<std::string> {
265 public:
266 void SetUp() override { SetUpBase(GetParam()); }
267};
268
269TEST_P(GraphicsCompositionTest, SingleSolidColorLayer) {
270 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000271 EXPECT_TRUE(mComposerClient
272 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
273 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000274
ramindani44c952f2022-02-28 23:29:29 +0000275 bool isSupported;
276 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000277 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000278 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
279 return;
280 }
281
ramindanidcecfd42022-02-03 23:52:19 +0000282 auto layer = std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
283 common::Rect coloredSquare({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000284 layer->setColor(BLUE);
285 layer->setDisplayFrame(coloredSquare);
286 layer->setZOrder(10);
287
288 std::vector<std::shared_ptr<TestLayer>> layers = {layer};
289
290 // expected color for each pixel
ramindanidcecfd42022-02-03 23:52:19 +0000291 std::vector<Color> expectedColors(
292 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
293 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), coloredSquare, BLUE);
ramindanibab8ba92021-11-18 01:24:11 +0000294
ramindanidcecfd42022-02-03 23:52:19 +0000295 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700296 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000297 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
298
299 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800300 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400301 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000302 execute();
303 // if hwc cannot handle and asks for composition change,
304 // just succeed the test
ramindanidcecfd42022-02-03 23:52:19 +0000305 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +0000306 GTEST_SUCCEED();
307 return;
308 }
Ady Abraham3192f3d2021-12-03 16:08:56 -0800309 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400310 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000311 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800312 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000313
314 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
315 mTestRenderEngine->setRenderLayers(layers);
316 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
317 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
318 }
319}
320
321TEST_P(GraphicsCompositionTest, SetLayerBuffer) {
322 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000323 EXPECT_TRUE(mComposerClient
324 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
325 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000326
ramindani44c952f2022-02-28 23:29:29 +0000327 bool isSupported;
328 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000329 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000330 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
331 return;
332 }
333
ramindanidcecfd42022-02-03 23:52:19 +0000334 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700335 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000336 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000337 std::vector<Color> expectedColors(
338 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
339 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
340 {0, 0, getDisplayWidth(), getDisplayHeight() / 4}, RED);
341 ReadbackHelper::fillColorsArea(
342 expectedColors, getDisplayWidth(),
343 {0, getDisplayHeight() / 4, getDisplayWidth(), getDisplayHeight() / 2}, GREEN);
344 ReadbackHelper::fillColorsArea(
345 expectedColors, getDisplayWidth(),
346 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, BLUE);
ramindanibab8ba92021-11-18 01:24:11 +0000347
348 auto layer = std::make_shared<TestBufferLayer>(
ramindani0a2bee42022-02-10 01:27:42 +0000349 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
350 getDisplayHeight(), common::PixelFormat::RGBA_8888);
ramindanidcecfd42022-02-03 23:52:19 +0000351 layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000352 layer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400353 layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000354 ASSERT_NO_FATAL_FAILURE(layer->setBuffer(expectedColors));
355
356 std::vector<std::shared_ptr<TestLayer>> layers = {layer};
357
358 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800359 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400360 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000361 execute();
362
ramindanidcecfd42022-02-03 23:52:19 +0000363 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +0000364 GTEST_SUCCEED();
365 return;
366 }
Ady Abraham3192f3d2021-12-03 16:08:56 -0800367 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000368
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400369 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000370 execute();
371
Ady Abraham3192f3d2021-12-03 16:08:56 -0800372 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000373
374 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
375 mTestRenderEngine->setRenderLayers(layers);
376 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
377 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
378 }
379}
380
Brian Lindahld103cd62022-12-09 07:26:28 -0700381TEST_P(GraphicsCompositionTest, SetLayerBufferWithSlotsToClear) {
Brian Lindahl95205702023-01-17 14:53:56 -0700382 const auto& [status, readbackBufferAttributes] =
Brian Lindahld103cd62022-12-09 07:26:28 -0700383 mComposerClient->getReadbackBufferAttributes(getPrimaryDisplayId());
Brian Lindahl95205702023-01-17 14:53:56 -0700384 if (!status.isOk()) {
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700385 GTEST_SUCCEED() << "Readback not supported";
386 return;
387 }
388
389 sp<GraphicBuffer> readbackBuffer;
390 ASSERT_NO_FATAL_FAILURE(ReadbackHelper::createReadbackBuffer(
391 readbackBufferAttributes, getPrimaryDisplay(), &readbackBuffer));
392 if (readbackBuffer == nullptr) {
393 GTEST_SUCCEED() << "Unsupported readback buffer attributes";
394 return;
395 }
396 // no fence needed for the readback buffer
397 ScopedFileDescriptor noFence(-1);
398
399 sp<GraphicBuffer> clearSlotBuffer = allocateBuffer(1u, 1u, GRALLOC_USAGE_HW_COMPOSER);
400 ASSERT_NE(nullptr, clearSlotBuffer);
401
402 // red buffer
403 uint64_t usage = GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN;
404 sp<GraphicBuffer> redBuffer = allocateBuffer(usage);
405 ASSERT_NE(nullptr, redBuffer);
406 int redFence;
407 ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBufferAndGetFence(redBuffer, RED, &redFence));
408
409 // blue buffer
410 sp<GraphicBuffer> blueBuffer = allocateBuffer(usage);
411 ASSERT_NE(nullptr, blueBuffer);
412 int blueFence;
413 ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBufferAndGetFence(blueBuffer, BLUE, &blueFence));
414
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700415 // layer defaults
416 common::Rect rectFullDisplay = {0, 0, getDisplayWidth(), getDisplayHeight()};
417 int64_t display = getPrimaryDisplayId();
Brian Lindahl95205702023-01-17 14:53:56 -0700418 auto [layerStatus, layer] = mComposerClient->createLayer(getPrimaryDisplayId(), 3);
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700419 ASSERT_TRUE(layerStatus.isOk());
420 mWriter->setLayerDisplayFrame(display, layer, rectFullDisplay);
421 mWriter->setLayerCompositionType(display, layer, Composition::DEVICE);
422
423 // set the layer to the blue buffer
424 // should be blue
425 {
426 auto status = mComposerClient->setReadbackBuffer(display, readbackBuffer->handle, noFence);
427 ASSERT_TRUE(status.isOk());
428 mWriter->setLayerBuffer(display, layer, /*slot*/ 0, blueBuffer->handle, blueFence);
Brian Lindahld103cd62022-12-09 07:26:28 -0700429 mWriter->validateDisplay(display, ComposerClientWriter::kNoTimestamp);
430 execute();
431 ASSERT_TRUE(mReader.takeChangedCompositionTypes(display).empty());
432 ASSERT_TRUE(mReader.takeErrors().empty());
433 mWriter->presentDisplay(display);
434 execute();
435 auto [fenceStatus, fence] = mComposerClient->getReadbackBufferFence(display);
436 ASSERT_TRUE(fenceStatus.isOk());
437 ReadbackHelper::compareColorToBuffer(BLUE, readbackBuffer, fence);
438 }
439
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700440 // change the layer to the red buffer
441 // should be red
Brian Lindahld103cd62022-12-09 07:26:28 -0700442 {
443 auto status = mComposerClient->setReadbackBuffer(display, readbackBuffer->handle, noFence);
444 ASSERT_TRUE(status.isOk());
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700445 mWriter->setLayerBuffer(display, layer, /*slot*/ 1, redBuffer->handle, redFence);
Brian Lindahld103cd62022-12-09 07:26:28 -0700446 mWriter->validateDisplay(display, ComposerClientWriter::kNoTimestamp);
447 execute();
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700448 ASSERT_TRUE(mReader.takeChangedCompositionTypes(display).empty());
Brian Lindahld103cd62022-12-09 07:26:28 -0700449 ASSERT_TRUE(mReader.takeErrors().empty());
450 mWriter->presentDisplay(display);
451 execute();
452 auto [fenceStatus, fence] = mComposerClient->getReadbackBufferFence(display);
453 ASSERT_TRUE(fenceStatus.isOk());
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700454 ReadbackHelper::compareColorToBuffer(RED, readbackBuffer, fence);
455 }
456
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700457 // clear the slot for the blue buffer
Brian Lindahl95205702023-01-17 14:53:56 -0700458 // should still be red
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700459 {
460 auto status = mComposerClient->setReadbackBuffer(display, readbackBuffer->handle, noFence);
461 ASSERT_TRUE(status.isOk());
462 mWriter->setLayerBufferWithNewCommand(display, layer, /*slot*/ 0, clearSlotBuffer->handle,
463 /*fence*/ -1);
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700464 mWriter->validateDisplay(display, ComposerClientWriter::kNoTimestamp);
465 execute();
466 ASSERT_TRUE(mReader.takeChangedCompositionTypes(display).empty());
467 ASSERT_TRUE(mReader.takeErrors().empty());
468 mWriter->presentDisplay(display);
469 execute();
470 auto [fenceStatus, fence] = mComposerClient->getReadbackBufferFence(display);
471 ASSERT_TRUE(fenceStatus.isOk());
Brian Lindahl95205702023-01-17 14:53:56 -0700472 ReadbackHelper::compareColorToBuffer(RED, readbackBuffer, fence);
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700473 }
474
Brian Lindahl95205702023-01-17 14:53:56 -0700475 // clear the slot for the red buffer, and set the buffer with the same slot to the blue buffer
476 // should be blue
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700477 {
478 auto status = mComposerClient->setReadbackBuffer(display, readbackBuffer->handle, noFence);
479 ASSERT_TRUE(status.isOk());
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700480 mWriter->setLayerBufferWithNewCommand(display, layer, /*slot*/ 1, clearSlotBuffer->handle,
481 /*fence*/ -1);
Brian Lindahl95205702023-01-17 14:53:56 -0700482 mWriter->setLayerBuffer(display, layer, /*slot*/ 1, blueBuffer->handle, blueFence);
Brian Lindahl78ff2d62022-12-18 11:21:41 -0700483 mWriter->validateDisplay(display, ComposerClientWriter::kNoTimestamp);
484 execute();
485 ASSERT_TRUE(mReader.takeChangedCompositionTypes(display).empty());
486 ASSERT_TRUE(mReader.takeErrors().empty());
487 mWriter->presentDisplay(display);
488 execute();
489 auto [fenceStatus, fence] = mComposerClient->getReadbackBufferFence(display);
490 ASSERT_TRUE(fenceStatus.isOk());
491 ReadbackHelper::compareColorToBuffer(BLUE, readbackBuffer, fence);
Brian Lindahld103cd62022-12-09 07:26:28 -0700492 }
Brian Lindahl95205702023-01-17 14:53:56 -0700493
494 // clear the slot for the now-blue buffer
495 // should be black (no buffer)
496 // TODO(b/262037933) Ensure we never clear the active buffer's slot with the placeholder buffer
497 // by setting the layer to the color black
498 {
499 auto status = mComposerClient->setReadbackBuffer(display, readbackBuffer->handle, noFence);
500 ASSERT_TRUE(status.isOk());
501 mWriter->setLayerBufferWithNewCommand(display, layer, /*slot*/ 1, clearSlotBuffer->handle,
502 /*fence*/ -1);
503 mWriter->validateDisplay(display, ComposerClientWriter::kNoTimestamp);
504 execute();
505 if (!mReader.takeChangedCompositionTypes(display).empty()) {
506 GTEST_SUCCEED();
507 return;
508 }
509 ASSERT_TRUE(mReader.takeErrors().empty());
510 mWriter->presentDisplay(display);
511 execute();
512 auto [fenceStatus, fence] = mComposerClient->getReadbackBufferFence(display);
513 ASSERT_TRUE(fenceStatus.isOk());
514 ReadbackHelper::compareColorToBuffer(BLACK, readbackBuffer, fence);
515 }
Brian Lindahld103cd62022-12-09 07:26:28 -0700516}
517
ramindanibab8ba92021-11-18 01:24:11 +0000518TEST_P(GraphicsCompositionTest, SetLayerBufferNoEffect) {
519 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000520 EXPECT_TRUE(mComposerClient
521 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
522 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000523
ramindani44c952f2022-02-28 23:29:29 +0000524 bool isSupported;
525 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000526 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000527 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
528 return;
529 }
530
ramindanidcecfd42022-02-03 23:52:19 +0000531 auto layer = std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
532 common::Rect coloredSquare({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000533 layer->setColor(BLUE);
534 layer->setDisplayFrame(coloredSquare);
535 layer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400536 layer->write(*mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000537
538 // This following buffer call should have no effect
Brian Lindahld103cd62022-12-09 07:26:28 -0700539 uint64_t usage = GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN;
540 sp<GraphicBuffer> graphicBuffer = allocateBuffer(usage);
541 ASSERT_NE(nullptr, graphicBuffer);
ramindani0a2bee42022-02-10 01:27:42 +0000542 const auto& buffer = graphicBuffer->handle;
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400543 mWriter->setLayerBuffer(getPrimaryDisplayId(), layer->getLayer(), /*slot*/ 0, buffer,
544 /*acquireFence*/ -1);
ramindanibab8ba92021-11-18 01:24:11 +0000545
546 // expected color for each pixel
ramindanidcecfd42022-02-03 23:52:19 +0000547 std::vector<Color> expectedColors(
548 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
549 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), coloredSquare, BLUE);
ramindanibab8ba92021-11-18 01:24:11 +0000550
ramindanidcecfd42022-02-03 23:52:19 +0000551 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700552 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000553 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
554
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400555 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000556 execute();
557
ramindanidcecfd42022-02-03 23:52:19 +0000558 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +0000559 GTEST_SUCCEED();
560 return;
561 }
Ady Abraham3192f3d2021-12-03 16:08:56 -0800562 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400563 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000564 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800565 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000566
567 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
568 }
569}
570
ramindanib27f33b2021-12-03 19:36:10 +0000571TEST_P(GraphicsCompositionTest, SetReadbackBuffer) {
ramindani44c952f2022-02-28 23:29:29 +0000572 bool isSupported;
573 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000574 if (!isSupported) {
ramindanib27f33b2021-12-03 19:36:10 +0000575 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
576 return;
577 }
578
ramindanidcecfd42022-02-03 23:52:19 +0000579 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700580 getDisplayHeight(), mPixelFormat);
ramindanib27f33b2021-12-03 19:36:10 +0000581
582 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
583}
584
ramindanidcecfd42022-02-03 23:52:19 +0000585TEST_P(GraphicsCompositionTest, SetReadbackBuffer_BadDisplay) {
ramindani44c952f2022-02-28 23:29:29 +0000586 bool isSupported;
587 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000588 if (!isSupported) {
ramindanib27f33b2021-12-03 19:36:10 +0000589 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
590 return;
591 }
592
Brian Lindahld103cd62022-12-09 07:26:28 -0700593 uint64_t usage = GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN;
594 sp<GraphicBuffer> graphicBuffer = allocateBuffer(usage);
595 ASSERT_NE(nullptr, graphicBuffer);
ramindani0a2bee42022-02-10 01:27:42 +0000596 const auto& bufferHandle = graphicBuffer->handle;
ramindanib27f33b2021-12-03 19:36:10 +0000597 ::ndk::ScopedFileDescriptor fence = ::ndk::ScopedFileDescriptor(-1);
598
ramindanidcecfd42022-02-03 23:52:19 +0000599 const auto status =
600 mComposerClient->setReadbackBuffer(getInvalidDisplayId(), bufferHandle, fence);
ramindanib27f33b2021-12-03 19:36:10 +0000601
ramindanidcecfd42022-02-03 23:52:19 +0000602 EXPECT_FALSE(status.isOk());
ramindanid5751092022-04-22 22:30:20 +0000603 EXPECT_NO_FATAL_FAILURE(assertServiceSpecificError(status, IComposerClient::EX_BAD_DISPLAY));
ramindanib27f33b2021-12-03 19:36:10 +0000604}
605
ramindanidcecfd42022-02-03 23:52:19 +0000606TEST_P(GraphicsCompositionTest, SetReadbackBuffer_BadParameter) {
ramindani44c952f2022-02-28 23:29:29 +0000607 bool isSupported;
608 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000609 if (!isSupported) {
ramindanib27f33b2021-12-03 19:36:10 +0000610 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
611 return;
612 }
613
ramindanidcecfd42022-02-03 23:52:19 +0000614 const native_handle_t bufferHandle{};
ramindanib27f33b2021-12-03 19:36:10 +0000615 ndk::ScopedFileDescriptor releaseFence = ndk::ScopedFileDescriptor(-1);
ramindanidcecfd42022-02-03 23:52:19 +0000616 const auto status =
617 mComposerClient->setReadbackBuffer(getPrimaryDisplayId(), &bufferHandle, releaseFence);
ramindanib27f33b2021-12-03 19:36:10 +0000618
ramindanidcecfd42022-02-03 23:52:19 +0000619 EXPECT_FALSE(status.isOk());
ramindanid5751092022-04-22 22:30:20 +0000620 EXPECT_NO_FATAL_FAILURE(assertServiceSpecificError(status, IComposerClient::EX_BAD_PARAMETER));
ramindanib27f33b2021-12-03 19:36:10 +0000621}
622
623TEST_P(GraphicsCompositionTest, GetReadbackBufferFenceInactive) {
ramindani44c952f2022-02-28 23:29:29 +0000624 bool isSupported;
625 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000626 if (!isSupported) {
ramindanib27f33b2021-12-03 19:36:10 +0000627 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
628 return;
629 }
630
ramindanidcecfd42022-02-03 23:52:19 +0000631 const auto& [status, releaseFence] =
632 mComposerClient->getReadbackBufferFence(getPrimaryDisplayId());
ramindanib27f33b2021-12-03 19:36:10 +0000633
ramindanidcecfd42022-02-03 23:52:19 +0000634 EXPECT_FALSE(status.isOk());
ramindanid5751092022-04-22 22:30:20 +0000635 EXPECT_NO_FATAL_FAILURE(assertServiceSpecificError(status, IComposerClient::EX_UNSUPPORTED));
Alec Mouri62ae37b2022-01-20 17:16:38 -0800636 EXPECT_EQ(-1, releaseFence.get());
ramindanib27f33b2021-12-03 19:36:10 +0000637}
638
ramindanibab8ba92021-11-18 01:24:11 +0000639TEST_P(GraphicsCompositionTest, ClientComposition) {
ramindanidcecfd42022-02-03 23:52:19 +0000640 EXPECT_TRUE(
641 mComposerClient->setClientTargetSlotCount(getPrimaryDisplayId(), kClientTargetSlotCount)
642 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000643
644 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000645 EXPECT_TRUE(mComposerClient
646 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
ramindanibab8ba92021-11-18 01:24:11 +0000647 .isOk());
648
ramindani44c952f2022-02-28 23:29:29 +0000649 bool isSupported;
650 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000651 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000652 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
653 return;
654 }
655
ramindanidcecfd42022-02-03 23:52:19 +0000656 std::vector<Color> expectedColors(
657 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
658 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
659 {0, 0, getDisplayWidth(), getDisplayHeight() / 4}, RED);
660 ReadbackHelper::fillColorsArea(
661 expectedColors, getDisplayWidth(),
662 {0, getDisplayHeight() / 4, getDisplayWidth(), getDisplayHeight() / 2}, GREEN);
663 ReadbackHelper::fillColorsArea(
664 expectedColors, getDisplayWidth(),
665 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, BLUE);
ramindanibab8ba92021-11-18 01:24:11 +0000666
ramindani0a2bee42022-02-10 01:27:42 +0000667 auto layer = std::make_shared<TestBufferLayer>(mComposerClient, *mTestRenderEngine,
668 getPrimaryDisplayId(), getDisplayWidth(),
669 getDisplayHeight(), PixelFormat::RGBA_FP16);
ramindanidcecfd42022-02-03 23:52:19 +0000670 layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000671 layer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400672 layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000673
674 std::vector<std::shared_ptr<TestLayer>> layers = {layer};
675
ramindanidcecfd42022-02-03 23:52:19 +0000676 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700677 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000678 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
679 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800680 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400681 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000682 execute();
683
ramindanidcecfd42022-02-03 23:52:19 +0000684 auto changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
Ady Abraham46219f52021-12-20 09:44:31 -0800685 if (!changedCompositionTypes.empty()) {
Ady Abraham3192f3d2021-12-03 16:08:56 -0800686 ASSERT_EQ(1, changedCompositionTypes.size());
Ady Abraham46219f52021-12-20 09:44:31 -0800687 ASSERT_EQ(Composition::CLIENT, changedCompositionTypes[0].composition);
ramindanibab8ba92021-11-18 01:24:11 +0000688
689 PixelFormat clientFormat = PixelFormat::RGBA_8888;
Brian Lindahld103cd62022-12-09 07:26:28 -0700690 auto clientUsage = GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_OFTEN |
691 GRALLOC_USAGE_SW_WRITE_OFTEN;
ramindanibab8ba92021-11-18 01:24:11 +0000692 Dataspace clientDataspace = ReadbackHelper::getDataspaceForColorMode(mode);
ramindanidcecfd42022-02-03 23:52:19 +0000693 common::Rect damage{0, 0, getDisplayWidth(), getDisplayHeight()};
ramindanibab8ba92021-11-18 01:24:11 +0000694
695 // create client target buffer
Brian Lindahld103cd62022-12-09 07:26:28 -0700696 sp<GraphicBuffer> graphicBuffer = allocateBuffer(clientUsage);
697 ASSERT_NE(nullptr, graphicBuffer);
ramindani0a2bee42022-02-10 01:27:42 +0000698 const auto& buffer = graphicBuffer->handle;
ramindanibab8ba92021-11-18 01:24:11 +0000699 void* clientBufData;
ramindani0a2bee42022-02-10 01:27:42 +0000700 const auto stride = static_cast<uint32_t>(graphicBuffer->stride);
701 graphicBuffer->lock(clientUsage, layer->getAccessRegion(), &clientBufData);
ramindanibab8ba92021-11-18 01:24:11 +0000702
703 ASSERT_NO_FATAL_FAILURE(
ramindani0a2bee42022-02-10 01:27:42 +0000704 ReadbackHelper::fillBuffer(layer->getWidth(), layer->getHeight(), stride,
ramindanibab8ba92021-11-18 01:24:11 +0000705 clientBufData, clientFormat, expectedColors));
ramindanib1144212022-02-10 01:51:24 +0000706 int32_t clientFence;
707 const auto unlockStatus = graphicBuffer->unlockAsync(&clientFence);
708 ASSERT_EQ(::android::OK, unlockStatus);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400709 mWriter->setClientTarget(getPrimaryDisplayId(), /*slot*/ 0, buffer, clientFence,
710 clientDataspace, std::vector<common::Rect>(1, damage));
711 layer->setToClientComposition(*mWriter);
712 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000713 execute();
ramindanidcecfd42022-02-03 23:52:19 +0000714 changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
Ady Abraham46219f52021-12-20 09:44:31 -0800715 ASSERT_TRUE(changedCompositionTypes.empty());
ramindanibab8ba92021-11-18 01:24:11 +0000716 }
Ady Abraham3192f3d2021-12-03 16:08:56 -0800717 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000718
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400719 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000720 execute();
721
Ady Abraham3192f3d2021-12-03 16:08:56 -0800722 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000723
724 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
725 }
726}
727
728TEST_P(GraphicsCompositionTest, DeviceAndClientComposition) {
ramindanidcecfd42022-02-03 23:52:19 +0000729 ASSERT_TRUE(
730 mComposerClient->setClientTargetSlotCount(getPrimaryDisplayId(), kClientTargetSlotCount)
731 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000732
733 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000734 EXPECT_TRUE(mComposerClient
735 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
736 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000737
ramindani44c952f2022-02-28 23:29:29 +0000738 bool isSupported;
739 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000740 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000741 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
742 return;
743 }
744
ramindanidcecfd42022-02-03 23:52:19 +0000745 std::vector<Color> expectedColors(
746 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
747 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
748 {0, 0, getDisplayWidth(), getDisplayHeight() / 2}, GREEN);
749 ReadbackHelper::fillColorsArea(
750 expectedColors, getDisplayWidth(),
751 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, RED);
ramindanibab8ba92021-11-18 01:24:11 +0000752
ramindanidcecfd42022-02-03 23:52:19 +0000753 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700754 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000755 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
756
757 auto deviceLayer = std::make_shared<TestBufferLayer>(
ramindani0a2bee42022-02-10 01:27:42 +0000758 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
759 getDisplayHeight() / 2, PixelFormat::RGBA_8888);
ramindanibab8ba92021-11-18 01:24:11 +0000760 std::vector<Color> deviceColors(deviceLayer->getWidth() * deviceLayer->getHeight());
761 ReadbackHelper::fillColorsArea(deviceColors, static_cast<int32_t>(deviceLayer->getWidth()),
762 {0, 0, static_cast<int32_t>(deviceLayer->getWidth()),
763 static_cast<int32_t>(deviceLayer->getHeight())},
764 GREEN);
765 deviceLayer->setDisplayFrame({0, 0, static_cast<int32_t>(deviceLayer->getWidth()),
766 static_cast<int32_t>(deviceLayer->getHeight())});
767 deviceLayer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400768 deviceLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000769 ASSERT_NO_FATAL_FAILURE(deviceLayer->setBuffer(deviceColors));
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400770 deviceLayer->write(*mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000771
772 PixelFormat clientFormat = PixelFormat::RGBA_8888;
Brian Lindahld103cd62022-12-09 07:26:28 -0700773 auto clientUsage =
774 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
ramindanibab8ba92021-11-18 01:24:11 +0000775 Dataspace clientDataspace = ReadbackHelper::getDataspaceForColorMode(mode);
ramindanidcecfd42022-02-03 23:52:19 +0000776 int32_t clientWidth = getDisplayWidth();
777 int32_t clientHeight = getDisplayHeight() / 2;
ramindanibab8ba92021-11-18 01:24:11 +0000778
779 auto clientLayer = std::make_shared<TestBufferLayer>(
ramindani0a2bee42022-02-10 01:27:42 +0000780 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), clientWidth,
781 clientHeight, PixelFormat::RGBA_FP16, Composition::DEVICE);
ramindanidcecfd42022-02-03 23:52:19 +0000782 common::Rect clientFrame = {0, getDisplayHeight() / 2, getDisplayWidth(),
783 getDisplayHeight()};
ramindanibab8ba92021-11-18 01:24:11 +0000784 clientLayer->setDisplayFrame(clientFrame);
785 clientLayer->setZOrder(0);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400786 clientLayer->write(*mWriter);
787 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000788 execute();
789
ramindanidcecfd42022-02-03 23:52:19 +0000790 auto changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
Ady Abraham3192f3d2021-12-03 16:08:56 -0800791 if (changedCompositionTypes.size() != 1) {
ramindanibab8ba92021-11-18 01:24:11 +0000792 continue;
793 }
794 // create client target buffer
Ady Abraham46219f52021-12-20 09:44:31 -0800795 ASSERT_EQ(Composition::CLIENT, changedCompositionTypes[0].composition);
Brian Lindahld103cd62022-12-09 07:26:28 -0700796 sp<GraphicBuffer> graphicBuffer = allocateBuffer(clientUsage);
797 ASSERT_NE(nullptr, graphicBuffer);
ramindani0a2bee42022-02-10 01:27:42 +0000798 const auto& buffer = graphicBuffer->handle;
ramindanibab8ba92021-11-18 01:24:11 +0000799
800 void* clientBufData;
ramindani0a2bee42022-02-10 01:27:42 +0000801 graphicBuffer->lock(clientUsage, {0, 0, getDisplayWidth(), getDisplayHeight()},
802 &clientBufData);
ramindanibab8ba92021-11-18 01:24:11 +0000803
ramindanidcecfd42022-02-03 23:52:19 +0000804 std::vector<Color> clientColors(
805 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
806 ReadbackHelper::fillColorsArea(clientColors, getDisplayWidth(), clientFrame, RED);
ramindanibab8ba92021-11-18 01:24:11 +0000807 ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBuffer(
ramindanidcecfd42022-02-03 23:52:19 +0000808 static_cast<uint32_t>(getDisplayWidth()), static_cast<uint32_t>(getDisplayHeight()),
ramindani0a2bee42022-02-10 01:27:42 +0000809 graphicBuffer->getStride(), clientBufData, clientFormat, clientColors));
ramindanib1144212022-02-10 01:51:24 +0000810 int32_t clientFence;
811 const auto unlockStatus = graphicBuffer->unlockAsync(&clientFence);
812 ASSERT_EQ(::android::OK, unlockStatus);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400813 mWriter->setClientTarget(getPrimaryDisplayId(), /*slot*/ 0, buffer, clientFence,
814 clientDataspace, std::vector<common::Rect>(1, clientFrame));
815 clientLayer->setToClientComposition(*mWriter);
816 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000817 execute();
ramindanidcecfd42022-02-03 23:52:19 +0000818 changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
Ady Abraham46219f52021-12-20 09:44:31 -0800819 ASSERT_TRUE(changedCompositionTypes.empty());
Ady Abraham3192f3d2021-12-03 16:08:56 -0800820 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000821
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400822 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000823 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800824 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000825 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
826 }
827}
828
829TEST_P(GraphicsCompositionTest, SetLayerDamage) {
830 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000831 EXPECT_TRUE(mComposerClient
832 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
833 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000834
ramindani44c952f2022-02-28 23:29:29 +0000835 bool isSupported;
836 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000837 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000838 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
839 return;
840 }
841
ramindanidcecfd42022-02-03 23:52:19 +0000842 common::Rect redRect = {0, 0, getDisplayWidth() / 4, getDisplayHeight() / 4};
ramindanibab8ba92021-11-18 01:24:11 +0000843
ramindanidcecfd42022-02-03 23:52:19 +0000844 std::vector<Color> expectedColors(
845 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
846 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
ramindanibab8ba92021-11-18 01:24:11 +0000847
ramindani0a2bee42022-02-10 01:27:42 +0000848 auto layer = std::make_shared<TestBufferLayer>(mComposerClient, *mTestRenderEngine,
849 getPrimaryDisplayId(), getDisplayWidth(),
850 getDisplayHeight(), PixelFormat::RGBA_8888);
ramindanidcecfd42022-02-03 23:52:19 +0000851 layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000852 layer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400853 layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +0000854 ASSERT_NO_FATAL_FAILURE(layer->setBuffer(expectedColors));
855
856 std::vector<std::shared_ptr<TestLayer>> layers = {layer};
857
ramindanidcecfd42022-02-03 23:52:19 +0000858 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700859 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000860 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
861
862 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800863 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400864 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000865 execute();
ramindanidcecfd42022-02-03 23:52:19 +0000866 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +0000867 GTEST_SUCCEED();
868 return;
869 }
Ady Abraham3192f3d2021-12-03 16:08:56 -0800870 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400871 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000872 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800873 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000874
875 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
876
877 // update surface damage and recheck
ramindanidcecfd42022-02-03 23:52:19 +0000878 redRect = {getDisplayWidth() / 4, getDisplayHeight() / 4, getDisplayWidth() / 2,
879 getDisplayHeight() / 2};
880 ReadbackHelper::clearColors(expectedColors, getDisplayWidth(), getDisplayHeight(),
881 getDisplayWidth());
882 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
ramindanibab8ba92021-11-18 01:24:11 +0000883
884 ASSERT_NO_FATAL_FAILURE(layer->fillBuffer(expectedColors));
885 layer->setSurfaceDamage(
ramindanidcecfd42022-02-03 23:52:19 +0000886 std::vector<common::Rect>(1, {0, 0, getDisplayWidth() / 2, getDisplayWidth() / 2}));
ramindanibab8ba92021-11-18 01:24:11 +0000887
888 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
889
890 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800891 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400892 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000893 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800894 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanidcecfd42022-02-03 23:52:19 +0000895 ASSERT_TRUE(mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400896 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000897 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800898 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000899
900 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
901 }
902}
903
904TEST_P(GraphicsCompositionTest, SetLayerPlaneAlpha) {
905 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000906 EXPECT_TRUE(mComposerClient
907 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
908 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000909
ramindani44c952f2022-02-28 23:29:29 +0000910 bool isSupported;
911 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000912 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000913 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
914 return;
915 }
916
ramindanidcecfd42022-02-03 23:52:19 +0000917 auto layer = std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000918 layer->setColor(RED);
ramindanidcecfd42022-02-03 23:52:19 +0000919 layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000920 layer->setZOrder(10);
921 layer->setAlpha(0);
922 layer->setBlendMode(BlendMode::PREMULTIPLIED);
923
924 std::vector<std::shared_ptr<TestLayer>> layers = {layer};
925
ramindanidcecfd42022-02-03 23:52:19 +0000926 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700927 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000928
929 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
930
931 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800932 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400933 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000934 execute();
ramindanidcecfd42022-02-03 23:52:19 +0000935 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +0000936 GTEST_SUCCEED();
937 return;
938 }
Ady Abraham3192f3d2021-12-03 16:08:56 -0800939 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000940
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400941 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +0000942 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -0800943 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +0000944
ramindanidcecfd42022-02-03 23:52:19 +0000945 std::vector<Color> expectedColors(
946 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
ramindanibab8ba92021-11-18 01:24:11 +0000947
948 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
949 mTestRenderEngine->setRenderLayers(layers);
950 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
951 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
952 }
953}
954
955TEST_P(GraphicsCompositionTest, SetLayerSourceCrop) {
956 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +0000957 EXPECT_TRUE(mComposerClient
958 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
959 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +0000960
ramindani44c952f2022-02-28 23:29:29 +0000961 bool isSupported;
962 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +0000963 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +0000964 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
965 return;
966 }
967
ramindanidcecfd42022-02-03 23:52:19 +0000968 std::vector<Color> expectedColors(
969 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
970 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
971 {0, 0, getDisplayWidth(), getDisplayHeight() / 4}, RED);
972 ReadbackHelper::fillColorsArea(
973 expectedColors, getDisplayWidth(),
974 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, BLUE);
ramindanibab8ba92021-11-18 01:24:11 +0000975
ramindani0a2bee42022-02-10 01:27:42 +0000976 auto layer = std::make_shared<TestBufferLayer>(mComposerClient, *mTestRenderEngine,
977 getPrimaryDisplayId(), getDisplayWidth(),
978 getDisplayHeight(), PixelFormat::RGBA_8888);
ramindanidcecfd42022-02-03 23:52:19 +0000979 layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +0000980 layer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400981 layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanidcecfd42022-02-03 23:52:19 +0000982 layer->setSourceCrop({0, static_cast<float>(getDisplayHeight() / 2),
983 static_cast<float>(getDisplayWidth()),
984 static_cast<float>(getDisplayHeight())});
ramindanibab8ba92021-11-18 01:24:11 +0000985 ASSERT_NO_FATAL_FAILURE(layer->setBuffer(expectedColors));
986
987 std::vector<std::shared_ptr<TestLayer>> layers = {layer};
988
989 // update expected colors to match crop
ramindanidcecfd42022-02-03 23:52:19 +0000990 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
991 {0, 0, getDisplayWidth(), getDisplayHeight()}, BLUE);
992 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -0700993 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +0000994 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
995 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -0800996 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -0400997 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +0000998 execute();
ramindanidcecfd42022-02-03 23:52:19 +0000999 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001000 GTEST_SUCCEED();
1001 return;
1002 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001003 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001004 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001005 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001006 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001007 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1008 mTestRenderEngine->setRenderLayers(layers);
1009 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1010 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1011 }
1012}
1013
1014TEST_P(GraphicsCompositionTest, SetLayerZOrder) {
1015 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001016 EXPECT_TRUE(mComposerClient
1017 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1018 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +00001019
ramindani44c952f2022-02-28 23:29:29 +00001020 bool isSupported;
1021 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001022 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001023 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1024 return;
1025 }
1026
ramindanidcecfd42022-02-03 23:52:19 +00001027 common::Rect redRect = {0, 0, getDisplayWidth(), getDisplayHeight() / 2};
1028 common::Rect blueRect = {0, getDisplayHeight() / 4, getDisplayWidth(), getDisplayHeight()};
1029 auto redLayer = std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001030 redLayer->setColor(RED);
1031 redLayer->setDisplayFrame(redRect);
1032
ramindanidcecfd42022-02-03 23:52:19 +00001033 auto blueLayer = std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001034 blueLayer->setColor(BLUE);
1035 blueLayer->setDisplayFrame(blueRect);
1036 blueLayer->setZOrder(5);
1037
1038 std::vector<std::shared_ptr<TestLayer>> layers = {redLayer, blueLayer};
ramindanidcecfd42022-02-03 23:52:19 +00001039 std::vector<Color> expectedColors(
1040 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
ramindanibab8ba92021-11-18 01:24:11 +00001041
1042 // red in front of blue
1043 redLayer->setZOrder(10);
1044
1045 // fill blue first so that red will overwrite on overlap
ramindanidcecfd42022-02-03 23:52:19 +00001046 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), blueRect, BLUE);
1047 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
ramindanibab8ba92021-11-18 01:24:11 +00001048
ramindanidcecfd42022-02-03 23:52:19 +00001049 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001050 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001051 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1052
1053 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001054 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001055 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001056 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001057 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001058 GTEST_SUCCEED();
1059 return;
1060 }
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001061 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001062 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001063 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001064
1065 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1066
1067 redLayer->setZOrder(1);
ramindanidcecfd42022-02-03 23:52:19 +00001068 ReadbackHelper::clearColors(expectedColors, getDisplayWidth(), getDisplayHeight(),
1069 getDisplayWidth());
1070 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
1071 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), blueRect, BLUE);
ramindanibab8ba92021-11-18 01:24:11 +00001072
1073 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1074
1075 writeLayers(layers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001076 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001077 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001078 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001079 ASSERT_TRUE(mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty());
Ady Abraham3192f3d2021-12-03 16:08:56 -08001080 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001081 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001082 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001083 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001084
1085 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1086 mTestRenderEngine->setRenderLayers(layers);
1087 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1088 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1089 }
1090}
1091
Alec Mourib1f16722022-02-07 13:03:44 -08001092TEST_P(GraphicsCompositionTest, SetLayerBrightnessDims) {
ramindanidcecfd42022-02-03 23:52:19 +00001093 const auto& [status, capabilities] =
1094 mComposerClient->getDisplayCapabilities(getPrimaryDisplayId());
1095 ASSERT_TRUE(status.isOk());
Alec Mouri51067012022-01-06 17:28:39 -08001096
1097 const bool brightnessSupport = std::find(capabilities.begin(), capabilities.end(),
1098 DisplayCapability::BRIGHTNESS) != capabilities.end();
1099
1100 if (!brightnessSupport) {
1101 GTEST_SUCCEED() << "Cannot verify dimming behavior without brightness support";
1102 return;
1103 }
1104
1105 const std::optional<float> maxBrightnessNitsOptional =
ramindanidcecfd42022-02-03 23:52:19 +00001106 getMaxDisplayBrightnessNits(getPrimaryDisplayId());
Alec Mouri51067012022-01-06 17:28:39 -08001107
1108 ASSERT_TRUE(maxBrightnessNitsOptional.has_value());
1109
1110 const float maxBrightnessNits = *maxBrightnessNitsOptional;
1111
1112 // Preconditions to successfully run are knowing the max brightness and successfully applying
1113 // the max brightness
1114 ASSERT_GT(maxBrightnessNits, 0.f);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001115 mWriter->setDisplayBrightness(getPrimaryDisplayId(), /*brightness*/ 1.f, maxBrightnessNits);
Alec Mouri51067012022-01-06 17:28:39 -08001116 execute();
1117 ASSERT_TRUE(mReader.takeErrors().empty());
1118
1119 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001120 EXPECT_TRUE(mComposerClient
1121 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1122 .isOk());
Alec Mouri51067012022-01-06 17:28:39 -08001123
ramindani44c952f2022-02-28 23:29:29 +00001124 bool isSupported;
1125 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001126 if (!isSupported) {
Alec Mouri51067012022-01-06 17:28:39 -08001127 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace for "
1128 "color mode: "
1129 << toString(mode);
1130 continue;
1131 }
ramindanidcecfd42022-02-03 23:52:19 +00001132 const common::Rect redRect = {0, 0, getDisplayWidth(), getDisplayHeight() / 2};
1133 const common::Rect dimmerRedRect = {0, getDisplayHeight() / 2, getDisplayWidth(),
1134 getDisplayHeight()};
1135 const auto redLayer =
1136 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
Alec Mouri51067012022-01-06 17:28:39 -08001137 redLayer->setColor(RED);
1138 redLayer->setDisplayFrame(redRect);
1139 redLayer->setWhitePointNits(maxBrightnessNits);
Alec Mourib1f16722022-02-07 13:03:44 -08001140 redLayer->setBrightness(1.f);
Alec Mouri51067012022-01-06 17:28:39 -08001141
1142 const auto dimmerRedLayer =
ramindanidcecfd42022-02-03 23:52:19 +00001143 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
Alec Mouri51067012022-01-06 17:28:39 -08001144 dimmerRedLayer->setColor(RED);
1145 dimmerRedLayer->setDisplayFrame(dimmerRedRect);
1146 // Intentionally use a small dimming ratio as some implementations may be more likely to
1147 // kick into GPU composition to apply dithering when the dimming ratio is high.
1148 static constexpr float kDimmingRatio = 0.9f;
1149 dimmerRedLayer->setWhitePointNits(maxBrightnessNits * kDimmingRatio);
Alec Mourib1f16722022-02-07 13:03:44 -08001150 dimmerRedLayer->setBrightness(kDimmingRatio);
Alec Mouri51067012022-01-06 17:28:39 -08001151
1152 const std::vector<std::shared_ptr<TestLayer>> layers = {redLayer, dimmerRedLayer};
ramindanidcecfd42022-02-03 23:52:19 +00001153 std::vector<Color> expectedColors(
1154 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
Alec Mouri51067012022-01-06 17:28:39 -08001155
ramindanidcecfd42022-02-03 23:52:19 +00001156 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
1157 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), dimmerRedRect, DIM_RED);
Alec Mouri51067012022-01-06 17:28:39 -08001158
ramindanidcecfd42022-02-03 23:52:19 +00001159 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001160 getDisplayHeight(), mPixelFormat);
Alec Mouri51067012022-01-06 17:28:39 -08001161 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1162
1163 writeLayers(layers);
1164 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001165 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
Alec Mouri51067012022-01-06 17:28:39 -08001166 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001167 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
Alec Mouri51067012022-01-06 17:28:39 -08001168 GTEST_SUCCEED()
1169 << "Readback verification not supported for GPU composition for color mode: "
1170 << toString(mode);
1171 continue;
1172 }
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001173 mWriter->presentDisplay(getPrimaryDisplayId());
Alec Mouri51067012022-01-06 17:28:39 -08001174 execute();
1175 ASSERT_TRUE(mReader.takeErrors().empty());
1176
1177 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1178 mTestRenderEngine->setRenderLayers(layers);
1179 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1180 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1181 }
1182}
1183
ramindanibab8ba92021-11-18 01:24:11 +00001184class GraphicsBlendModeCompositionTest
1185 : public GraphicsCompositionTestBase,
1186 public testing::WithParamInterface<std::tuple<std::string, std::string>> {
1187 public:
1188 void SetUp() override {
1189 SetUpBase(std::get<0>(GetParam()));
ramindanib636af22022-02-10 02:55:56 +00001190 // TODO(b/219590743) we should remove the below SRGB color mode
1191 // once we have the BlendMode test fix for all the versions of the ColorMode
ramindania4e76362022-03-02 01:48:06 +00001192 mTestColorModes.erase(
1193 std::remove_if(mTestColorModes.begin(), mTestColorModes.end(),
1194 [](ColorMode mode) { return mode != ColorMode::SRGB; }),
1195 mTestColorModes.end());
ramindanibab8ba92021-11-18 01:24:11 +00001196 mBackgroundColor = BLACK;
1197 mTopLayerColor = RED;
1198 }
1199
1200 void setBackgroundColor(Color color) { mBackgroundColor = color; }
1201
1202 void setTopLayerColor(Color color) { mTopLayerColor = color; }
1203
1204 void setUpLayers(BlendMode blendMode) {
1205 mLayers.clear();
ramindanidcecfd42022-02-03 23:52:19 +00001206 std::vector<Color> topLayerPixelColors(
1207 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1208 ReadbackHelper::fillColorsArea(topLayerPixelColors, getDisplayWidth(),
1209 {0, 0, getDisplayWidth(), getDisplayHeight()},
1210 mTopLayerColor);
ramindanibab8ba92021-11-18 01:24:11 +00001211
ramindanidcecfd42022-02-03 23:52:19 +00001212 auto backgroundLayer =
1213 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
1214 backgroundLayer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +00001215 backgroundLayer->setZOrder(0);
1216 backgroundLayer->setColor(mBackgroundColor);
1217
ramindani0a2bee42022-02-10 01:27:42 +00001218 auto layer = std::make_shared<TestBufferLayer>(mComposerClient, *mTestRenderEngine,
1219 getPrimaryDisplayId(), getDisplayWidth(),
1220 getDisplayHeight(), PixelFormat::RGBA_8888);
ramindanidcecfd42022-02-03 23:52:19 +00001221 layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +00001222 layer->setZOrder(10);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001223 layer->setDataspace(Dataspace::UNKNOWN, *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +00001224 ASSERT_NO_FATAL_FAILURE(layer->setBuffer(topLayerPixelColors));
1225
1226 layer->setBlendMode(blendMode);
1227 layer->setAlpha(std::stof(std::get<1>(GetParam())));
1228
1229 mLayers.push_back(backgroundLayer);
1230 mLayers.push_back(layer);
1231 }
1232
1233 void setExpectedColors(std::vector<Color>& expectedColors) {
1234 ASSERT_EQ(2, mLayers.size());
ramindanidcecfd42022-02-03 23:52:19 +00001235 ReadbackHelper::clearColors(expectedColors, getDisplayWidth(), getDisplayHeight(),
1236 getDisplayWidth());
ramindanibab8ba92021-11-18 01:24:11 +00001237
1238 auto layer = mLayers[1];
1239 BlendMode blendMode = layer->getBlendMode();
Ady Abraham1bee7ab2022-01-06 17:22:08 -08001240 float alpha = mTopLayerColor.a * layer->getAlpha();
ramindanibab8ba92021-11-18 01:24:11 +00001241 if (blendMode == BlendMode::NONE) {
1242 for (auto& expectedColor : expectedColors) {
Ady Abraham1bee7ab2022-01-06 17:22:08 -08001243 expectedColor.r = mTopLayerColor.r * layer->getAlpha();
1244 expectedColor.g = mTopLayerColor.g * layer->getAlpha();
1245 expectedColor.b = mTopLayerColor.b * layer->getAlpha();
1246 expectedColor.a = alpha;
ramindanibab8ba92021-11-18 01:24:11 +00001247 }
1248 } else if (blendMode == BlendMode::PREMULTIPLIED) {
1249 for (auto& expectedColor : expectedColors) {
Ady Abraham1bee7ab2022-01-06 17:22:08 -08001250 expectedColor.r =
1251 mTopLayerColor.r * layer->getAlpha() + mBackgroundColor.r * (1.0f - alpha);
1252 expectedColor.g =
1253 mTopLayerColor.g * layer->getAlpha() + mBackgroundColor.g * (1.0f - alpha);
1254 expectedColor.b =
1255 mTopLayerColor.b * layer->getAlpha() + mBackgroundColor.b * (1.0f - alpha);
1256 expectedColor.a = alpha + mBackgroundColor.a * (1.0f - alpha);
ramindanibab8ba92021-11-18 01:24:11 +00001257 }
1258 } else if (blendMode == BlendMode::COVERAGE) {
1259 for (auto& expectedColor : expectedColors) {
Ady Abraham1bee7ab2022-01-06 17:22:08 -08001260 expectedColor.r = mTopLayerColor.r * alpha + mBackgroundColor.r * (1.0f - alpha);
1261 expectedColor.g = mTopLayerColor.g * alpha + mBackgroundColor.g * (1.0f - alpha);
1262 expectedColor.b = mTopLayerColor.b * alpha + mBackgroundColor.b * (1.0f - alpha);
1263 expectedColor.a = mTopLayerColor.a * alpha + mBackgroundColor.a * (1.0f - alpha);
ramindanibab8ba92021-11-18 01:24:11 +00001264 }
1265 }
1266 }
1267
1268 protected:
1269 std::vector<std::shared_ptr<TestLayer>> mLayers;
1270 Color mBackgroundColor;
1271 Color mTopLayerColor;
1272};
ramindanic7585d92022-04-15 18:30:41 +00001273
1274TEST_P(GraphicsBlendModeCompositionTest, None) {
ramindanibab8ba92021-11-18 01:24:11 +00001275 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001276 EXPECT_TRUE(mComposerClient
1277 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1278 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +00001279
ramindani44c952f2022-02-28 23:29:29 +00001280 bool isSupported;
1281 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001282 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001283 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1284 return;
1285 }
1286
ramindanidcecfd42022-02-03 23:52:19 +00001287 std::vector<Color> expectedColors(
1288 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
ramindanibab8ba92021-11-18 01:24:11 +00001289
1290 setBackgroundColor(BLACK);
1291 setTopLayerColor(TRANSLUCENT_RED);
1292 setUpLayers(BlendMode::NONE);
1293 setExpectedColors(expectedColors);
1294
ramindanidcecfd42022-02-03 23:52:19 +00001295 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001296 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001297 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1298 writeLayers(mLayers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001299 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001300 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001301 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001302 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001303 GTEST_SUCCEED();
1304 return;
1305 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001306 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001307 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001308 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001309 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001310
1311 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1312 mTestRenderEngine->setRenderLayers(mLayers);
1313 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1314 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1315 }
1316}
1317
ramindani07e6f842022-02-15 15:28:33 +00001318TEST_P(GraphicsBlendModeCompositionTest, Coverage) {
ramindanibab8ba92021-11-18 01:24:11 +00001319 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001320 EXPECT_TRUE(mComposerClient
1321 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1322 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +00001323
ramindani44c952f2022-02-28 23:29:29 +00001324 bool isSupported;
1325 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001326 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001327 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1328 return;
1329 }
1330
ramindanidcecfd42022-02-03 23:52:19 +00001331 std::vector<Color> expectedColors(
1332 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
ramindanibab8ba92021-11-18 01:24:11 +00001333
1334 setBackgroundColor(BLACK);
1335 setTopLayerColor(TRANSLUCENT_RED);
1336
1337 setUpLayers(BlendMode::COVERAGE);
1338 setExpectedColors(expectedColors);
1339
ramindanidcecfd42022-02-03 23:52:19 +00001340 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001341 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001342 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1343 writeLayers(mLayers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001344 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001345 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001346 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001347 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001348 GTEST_SUCCEED();
1349 return;
1350 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001351 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001352 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001353 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001354 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001355 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1356 }
1357}
1358
1359TEST_P(GraphicsBlendModeCompositionTest, Premultiplied) {
1360 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001361 EXPECT_TRUE(mComposerClient
1362 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1363 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +00001364
ramindani44c952f2022-02-28 23:29:29 +00001365 bool isSupported;
1366 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001367 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001368 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1369 return;
1370 }
ramindanibab8ba92021-11-18 01:24:11 +00001371
ramindanidcecfd42022-02-03 23:52:19 +00001372 std::vector<Color> expectedColors(
1373 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
ramindanibab8ba92021-11-18 01:24:11 +00001374
1375 setBackgroundColor(BLACK);
1376 setTopLayerColor(TRANSLUCENT_RED);
1377 setUpLayers(BlendMode::PREMULTIPLIED);
1378 setExpectedColors(expectedColors);
1379
ramindanidcecfd42022-02-03 23:52:19 +00001380 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001381 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001382 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1383 writeLayers(mLayers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001384 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001385 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001386 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001387 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001388 GTEST_SUCCEED();
1389 return;
1390 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001391 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001392 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001393 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001394 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001395 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1396 mTestRenderEngine->setRenderLayers(mLayers);
1397 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1398 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1399 }
1400}
1401
1402class GraphicsTransformCompositionTest : public GraphicsCompositionTest {
1403 protected:
1404 void SetUp() override {
1405 GraphicsCompositionTest::SetUp();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001406
ramindanidcecfd42022-02-03 23:52:19 +00001407 auto backgroundLayer =
1408 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId());
Ady Abraham1bee7ab2022-01-06 17:22:08 -08001409 backgroundLayer->setColor({0.0f, 0.0f, 0.0f, 0.0f});
ramindanidcecfd42022-02-03 23:52:19 +00001410 backgroundLayer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
ramindanibab8ba92021-11-18 01:24:11 +00001411 backgroundLayer->setZOrder(0);
1412
ramindanidcecfd42022-02-03 23:52:19 +00001413 mSideLength =
1414 getDisplayWidth() < getDisplayHeight() ? getDisplayWidth() : getDisplayHeight();
ramindanibab8ba92021-11-18 01:24:11 +00001415 common::Rect redRect = {0, 0, mSideLength / 2, mSideLength / 2};
1416 common::Rect blueRect = {mSideLength / 2, mSideLength / 2, mSideLength, mSideLength};
1417
ramindani0a2bee42022-02-10 01:27:42 +00001418 mLayer = std::make_shared<TestBufferLayer>(mComposerClient, *mTestRenderEngine,
1419 getPrimaryDisplayId(), mSideLength, mSideLength,
1420 PixelFormat::RGBA_8888);
ramindanibab8ba92021-11-18 01:24:11 +00001421 mLayer->setDisplayFrame({0, 0, mSideLength, mSideLength});
1422 mLayer->setZOrder(10);
1423
1424 std::vector<Color> baseColors(static_cast<size_t>(mSideLength * mSideLength));
1425 ReadbackHelper::fillColorsArea(baseColors, mSideLength, redRect, RED);
1426 ReadbackHelper::fillColorsArea(baseColors, mSideLength, blueRect, BLUE);
1427 ASSERT_NO_FATAL_FAILURE(mLayer->setBuffer(baseColors));
1428 mLayers = {backgroundLayer, mLayer};
1429 }
1430
1431 protected:
1432 std::shared_ptr<TestBufferLayer> mLayer;
1433 std::vector<std::shared_ptr<TestLayer>> mLayers;
1434 int mSideLength;
1435};
1436
1437TEST_P(GraphicsTransformCompositionTest, FLIP_H) {
1438 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001439 auto status = mComposerClient->setColorMode(getPrimaryDisplayId(), mode,
1440 RenderIntent::COLORIMETRIC);
ramindanid5751092022-04-22 22:30:20 +00001441 if (!status.isOk() && status.getExceptionCode() == EX_SERVICE_SPECIFIC &&
ramindanidcecfd42022-02-03 23:52:19 +00001442 (status.getServiceSpecificError() == IComposerClient::EX_UNSUPPORTED ||
1443 status.getServiceSpecificError() == IComposerClient::EX_BAD_PARAMETER)) {
ramindanibab8ba92021-11-18 01:24:11 +00001444 SUCCEED() << "ColorMode not supported, skip test";
1445 return;
1446 }
1447
ramindani44c952f2022-02-28 23:29:29 +00001448 bool isSupported;
1449 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001450 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001451 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1452 return;
1453 }
ramindanidcecfd42022-02-03 23:52:19 +00001454 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001455 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001456 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1457 mLayer->setTransform(Transform::FLIP_H);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001458 mLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +00001459
ramindanidcecfd42022-02-03 23:52:19 +00001460 std::vector<Color> expectedColors(
1461 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1462 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
ramindanibab8ba92021-11-18 01:24:11 +00001463 {mSideLength / 2, 0, mSideLength, mSideLength / 2}, RED);
ramindanidcecfd42022-02-03 23:52:19 +00001464 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
ramindanibab8ba92021-11-18 01:24:11 +00001465 {0, mSideLength / 2, mSideLength / 2, mSideLength}, BLUE);
1466
1467 writeLayers(mLayers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001468 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001469 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001470 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001471 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001472 GTEST_SUCCEED();
1473 return;
1474 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001475 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001476 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001477 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001478 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001479
1480 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1481 mTestRenderEngine->setRenderLayers(mLayers);
1482 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1483 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1484 }
1485}
1486
1487TEST_P(GraphicsTransformCompositionTest, FLIP_V) {
1488 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001489 EXPECT_TRUE(mComposerClient
1490 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1491 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +00001492
ramindani44c952f2022-02-28 23:29:29 +00001493 bool isSupported;
1494 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001495 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001496 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1497 return;
1498 }
ramindanidcecfd42022-02-03 23:52:19 +00001499 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001500 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001501 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1502
1503 mLayer->setTransform(Transform::FLIP_V);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001504 mLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +00001505
ramindanidcecfd42022-02-03 23:52:19 +00001506 std::vector<Color> expectedColors(
1507 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1508 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
ramindanibab8ba92021-11-18 01:24:11 +00001509 {0, mSideLength / 2, mSideLength / 2, mSideLength}, RED);
ramindanidcecfd42022-02-03 23:52:19 +00001510 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
ramindanibab8ba92021-11-18 01:24:11 +00001511 {mSideLength / 2, 0, mSideLength, mSideLength / 2}, BLUE);
1512
1513 writeLayers(mLayers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001514 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001515 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001516 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001517 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001518 GTEST_SUCCEED();
1519 return;
1520 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001521 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001522 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001523 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001524 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001525 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1526 mTestRenderEngine->setRenderLayers(mLayers);
1527 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1528 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1529 }
1530}
1531
1532TEST_P(GraphicsTransformCompositionTest, ROT_180) {
1533 for (ColorMode mode : mTestColorModes) {
ramindanidcecfd42022-02-03 23:52:19 +00001534 EXPECT_TRUE(mComposerClient
1535 ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1536 .isOk());
ramindanibab8ba92021-11-18 01:24:11 +00001537
ramindani44c952f2022-02-28 23:29:29 +00001538 bool isSupported;
1539 ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
ramindanidcecfd42022-02-03 23:52:19 +00001540 if (!isSupported) {
ramindanibab8ba92021-11-18 01:24:11 +00001541 GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1542 return;
1543 }
ramindanidcecfd42022-02-03 23:52:19 +00001544 ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
Brian Lindahld103cd62022-12-09 07:26:28 -07001545 getDisplayHeight(), mPixelFormat);
ramindanibab8ba92021-11-18 01:24:11 +00001546 ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1547
1548 mLayer->setTransform(Transform::ROT_180);
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001549 mLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode), *mWriter);
ramindanibab8ba92021-11-18 01:24:11 +00001550
ramindanidcecfd42022-02-03 23:52:19 +00001551 std::vector<Color> expectedColors(
1552 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1553 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
ramindanibab8ba92021-11-18 01:24:11 +00001554 {mSideLength / 2, mSideLength / 2, mSideLength, mSideLength},
1555 RED);
ramindanidcecfd42022-02-03 23:52:19 +00001556 ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
ramindanibab8ba92021-11-18 01:24:11 +00001557 {0, 0, mSideLength / 2, mSideLength / 2}, BLUE);
1558
1559 writeLayers(mLayers);
Ady Abraham3192f3d2021-12-03 16:08:56 -08001560 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001561 mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp);
ramindanibab8ba92021-11-18 01:24:11 +00001562 execute();
ramindanidcecfd42022-02-03 23:52:19 +00001563 if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
ramindanibab8ba92021-11-18 01:24:11 +00001564 GTEST_SUCCEED();
1565 return;
1566 }
Ady Abraham3192f3d2021-12-03 16:08:56 -08001567 ASSERT_TRUE(mReader.takeErrors().empty());
Leon Scroggins IIIc3d695d2022-09-16 15:34:59 -04001568 mWriter->presentDisplay(getPrimaryDisplayId());
ramindanibab8ba92021-11-18 01:24:11 +00001569 execute();
Ady Abraham3192f3d2021-12-03 16:08:56 -08001570 ASSERT_TRUE(mReader.takeErrors().empty());
ramindanibab8ba92021-11-18 01:24:11 +00001571 ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1572 mTestRenderEngine->setRenderLayers(mLayers);
1573 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1574 ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1575 }
1576}
1577
1578GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsCompositionTest);
1579INSTANTIATE_TEST_SUITE_P(
1580 PerInstance, GraphicsCompositionTest,
1581 testing::ValuesIn(::android::getAidlHalInstanceNames(IComposer::descriptor)),
1582 ::android::PrintInstanceNameToString);
1583
1584GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsBlendModeCompositionTest);
1585INSTANTIATE_TEST_SUITE_P(BlendMode, GraphicsBlendModeCompositionTest,
1586 testing::Combine(testing::ValuesIn(::android::getAidlHalInstanceNames(
1587 IComposer::descriptor)),
1588 testing::Values("0.2", "1.0")));
1589
1590GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsTransformCompositionTest);
1591INSTANTIATE_TEST_SUITE_P(
1592 PerInstance, GraphicsTransformCompositionTest,
1593 testing::ValuesIn(::android::getAidlHalInstanceNames(IComposer::descriptor)),
1594 ::android::PrintInstanceNameToString);
1595
1596} // namespace
Ady Abraham3192f3d2021-12-03 16:08:56 -08001597} // namespace aidl::android::hardware::graphics::composer3::vts