1 /**
2  * Copyright (c) 2021, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "graphics_composer_aidl_hal_readback_tests@3"
18 
19 #include <aidl/Gtest.h>
20 #include <aidl/Vintf.h>
21 #include <aidl/android/hardware/graphics/common/BufferUsage.h>
22 #include <aidl/android/hardware/graphics/composer3/IComposer.h>
23 #include <gtest/gtest.h>
24 #include <ui/DisplayId.h>
25 #include <ui/DisplayIdentification.h>
26 #include <ui/GraphicBuffer.h>
27 #include <ui/PixelFormat.h>
28 #include <ui/Rect.h>
29 #include "GraphicsComposerCallback.h"
30 #include "ReadbackVts.h"
31 #include "RenderEngineVts.h"
32 #include "VtsComposerClient.h"
33 
34 namespace aidl::android::hardware::graphics::composer3::vts {
35 namespace {
36 
37 using ::android::Rect;
38 using common::Dataspace;
39 using common::PixelFormat;
40 
41 class GraphicsCompositionTestBase : public ::testing::Test {
42   protected:
SetUpBase(const std::string & name)43     void SetUpBase(const std::string& name) {
44         mComposerClient = std::make_shared<VtsComposerClient>(name);
45         ASSERT_TRUE(mComposerClient->createClient().isOk());
46 
47         const auto& [status, displays] = mComposerClient->getDisplays();
48         ASSERT_TRUE(status.isOk());
49         mDisplays = displays;
50         mWriter.reset(new ComposerClientWriter(getPrimaryDisplayId()));
51 
52         setTestColorModes();
53 
54         // explicitly disable vsync
55         for (const auto& display : mDisplays) {
56             EXPECT_TRUE(mComposerClient->setVsync(display.getDisplayId(), /*enable*/ false).isOk());
57         }
58         mComposerClient->setVsyncAllowed(/*isAllowed*/ false);
59 
60         EXPECT_TRUE(mComposerClient->setPowerMode(getPrimaryDisplayId(), PowerMode::ON).isOk());
61 
62         const auto format = getHasReadbackBuffer() ? mPixelFormat : common::PixelFormat::RGBA_8888;
63 
64         ASSERT_NO_FATAL_FAILURE(
65                 mTestRenderEngine = std::unique_ptr<TestRenderEngine>(new TestRenderEngine(
66                         ::android::renderengine::RenderEngineCreationArgs::Builder()
67                                 .setPixelFormat(static_cast<int>(format))
68                                 .setImageCacheSize(TestRenderEngine::sMaxFrameBufferAcquireBuffers)
69                                 .setEnableProtectedContext(false)
70                                 .setPrecacheToneMapperShaderOnly(false)
71                                 .setContextPriority(::android::renderengine::RenderEngine::
72                                                             ContextPriority::HIGH)
73                                 .build())));
74 
75         mClientCompositionDisplaySettings.physicalDisplay =
76                 Rect(getDisplayWidth(), getDisplayHeight());
77         mClientCompositionDisplaySettings.clip = mClientCompositionDisplaySettings.physicalDisplay;
78 
79         mTestRenderEngine->initGraphicBuffer(
80                 static_cast<uint32_t>(getDisplayWidth()), static_cast<uint32_t>(getDisplayHeight()),
81                 /*layerCount*/ 1U,
82                 static_cast<uint64_t>(
83                         static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) |
84                         static_cast<uint64_t>(common::BufferUsage::CPU_WRITE_OFTEN) |
85                         static_cast<uint64_t>(common::BufferUsage::GPU_RENDER_TARGET)));
86         mTestRenderEngine->setDisplaySettings(mClientCompositionDisplaySettings);
87     }
88 
TearDown()89     void TearDown() override {
90         ASSERT_FALSE(mDisplays.empty());
91         ASSERT_TRUE(mComposerClient->setPowerMode(getPrimaryDisplayId(), PowerMode::OFF).isOk());
92         ASSERT_TRUE(mComposerClient->tearDown(mWriter.get()));
93         mComposerClient.reset();
94         const auto errors = mReader.takeErrors();
95         ASSERT_TRUE(mReader.takeErrors().empty());
96         ASSERT_TRUE(mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty());
97     }
98 
getPrimaryDisplay() const99     const VtsDisplay& getPrimaryDisplay() const { return mDisplays[0]; }
100 
getPrimaryDisplayId() const101     int64_t getPrimaryDisplayId() const { return getPrimaryDisplay().getDisplayId(); }
102 
getInvalidDisplayId() const103     int64_t getInvalidDisplayId() const { return mComposerClient->getInvalidDisplayId(); }
104 
getDisplayWidth() const105     int32_t getDisplayWidth() const { return getPrimaryDisplay().getDisplayWidth(); }
106 
getDisplayHeight() const107     int32_t getDisplayHeight() const { return getPrimaryDisplay().getDisplayHeight(); }
108 
assertServiceSpecificError(const ScopedAStatus & status,int32_t serviceSpecificError)109     void assertServiceSpecificError(const ScopedAStatus& status, int32_t serviceSpecificError) {
110         ASSERT_EQ(status.getExceptionCode(), EX_SERVICE_SPECIFIC);
111         ASSERT_EQ(status.getServiceSpecificError(), serviceSpecificError);
112     }
113 
allocateBuffer(uint32_t usage)114     std::pair<bool, ::android::sp<::android::GraphicBuffer>> allocateBuffer(uint32_t usage) {
115         const auto width = static_cast<uint32_t>(getDisplayWidth());
116         const auto height = static_cast<uint32_t>(getDisplayHeight());
117 
118         const auto& graphicBuffer = ::android::sp<::android::GraphicBuffer>::make(
119                 width, height, ::android::PIXEL_FORMAT_RGBA_8888,
120                 /*layerCount*/ 1u, usage, "VtsHalGraphicsComposer3_ReadbackTest");
121 
122         if (graphicBuffer && ::android::OK == graphicBuffer->initCheck()) {
123             return {true, graphicBuffer};
124         }
125         return {false, graphicBuffer};
126     }
127 
writeLayers(const std::vector<std::shared_ptr<TestLayer>> & layers)128     void writeLayers(const std::vector<std::shared_ptr<TestLayer>>& layers) {
129         for (const auto& layer : layers) {
130             layer->write(*mWriter);
131         }
132         execute();
133     }
134 
execute()135     void execute() {
136         auto commands = mWriter->takePendingCommands();
137         if (commands.empty()) {
138             return;
139         }
140 
141         auto [status, results] = mComposerClient->executeCommands(commands);
142         ASSERT_TRUE(status.isOk()) << "executeCommands failed " << status.getDescription();
143 
144         mReader.parse(std::move(results));
145     }
146 
getHasReadbackBuffer()147     bool getHasReadbackBuffer() {
148         auto [status, readBackBufferAttributes] =
149                 mComposerClient->getReadbackBufferAttributes(getPrimaryDisplayId());
150         if (status.isOk()) {
151             mPixelFormat = readBackBufferAttributes.format;
152             mDataspace = readBackBufferAttributes.dataspace;
153             return ReadbackHelper::readbackSupported(mPixelFormat, mDataspace);
154         }
155         EXPECT_NO_FATAL_FAILURE(
156                 assertServiceSpecificError(status, IComposerClient::EX_UNSUPPORTED));
157         return false;
158     }
159 
160     std::shared_ptr<VtsComposerClient> mComposerClient;
161     std::vector<VtsDisplay> mDisplays;
162     // use the slot count usually set by SF
163     std::vector<ColorMode> mTestColorModes;
164     std::unique_ptr<ComposerClientWriter> mWriter;
165     ComposerClientReader mReader;
166     std::unique_ptr<TestRenderEngine> mTestRenderEngine;
167     common::PixelFormat mPixelFormat;
168     common::Dataspace mDataspace;
169     ::android::renderengine::DisplaySettings mClientCompositionDisplaySettings;
170 
171     static constexpr uint32_t kClientTargetSlotCount = 64;
172 
173   private:
setTestColorModes()174     void setTestColorModes() {
175         mTestColorModes.clear();
176         const auto& [status, modes] = mComposerClient->getColorModes(getPrimaryDisplayId());
177         ASSERT_TRUE(status.isOk());
178 
179         for (ColorMode mode : modes) {
180             if (std::find(ReadbackHelper::colorModes.begin(), ReadbackHelper::colorModes.end(),
181                           mode) != ReadbackHelper::colorModes.end()) {
182                 mTestColorModes.push_back(mode);
183             }
184         }
185     }
186 };
187 
188 class GraphicsCompositionTest : public GraphicsCompositionTestBase,
189                                 public testing::WithParamInterface<std::string> {
190   public:
SetUp()191     void SetUp() override { SetUpBase(GetParam()); }
192 };
193 
TEST_P(GraphicsCompositionTest,SingleSolidColorLayer)194 TEST_P(GraphicsCompositionTest, SingleSolidColorLayer) {
195     for (ColorMode mode : mTestColorModes) {
196         EXPECT_TRUE(mComposerClient
197                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
198                             .isOk());
199 
200         bool isSupported;
201         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
202         if (!isSupported) {
203             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
204             return;
205         }
206 
207         auto layer =
208                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
209         common::Rect coloredSquare({0, 0, getDisplayWidth(), getDisplayHeight()});
210         layer->setColor(BLUE);
211         layer->setDisplayFrame(coloredSquare);
212         layer->setZOrder(10);
213 
214         std::vector<std::shared_ptr<TestLayer>> layers = {layer};
215 
216         // expected color for each pixel
217         std::vector<Color> expectedColors(
218                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
219         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), coloredSquare, BLUE);
220 
221         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
222                                       getDisplayHeight(), mPixelFormat, mDataspace);
223         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
224 
225         writeLayers(layers);
226         ASSERT_TRUE(mReader.takeErrors().empty());
227         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
228                                  VtsComposerClient::kNoFrameIntervalNs);
229         execute();
230         // if hwc cannot handle and asks for composition change,
231         // just succeed the test
232         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
233             GTEST_SUCCEED();
234             return;
235         }
236         ASSERT_TRUE(mReader.takeErrors().empty());
237         mWriter->presentDisplay(getPrimaryDisplayId());
238         execute();
239         ASSERT_TRUE(mReader.takeErrors().empty());
240 
241         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
242         mTestRenderEngine->setRenderLayers(layers);
243         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
244         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
245     }
246 }
247 
TEST_P(GraphicsCompositionTest,SetLayerBuffer)248 TEST_P(GraphicsCompositionTest, SetLayerBuffer) {
249     for (ColorMode mode : mTestColorModes) {
250         EXPECT_TRUE(mComposerClient
251                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
252                             .isOk());
253 
254         bool isSupported;
255         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
256         if (!isSupported) {
257             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
258             return;
259         }
260 
261         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
262                                       getDisplayHeight(), mPixelFormat, mDataspace);
263         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
264         std::vector<Color> expectedColors(
265                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
266         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
267                                        {0, 0, getDisplayWidth(), getDisplayHeight() / 4}, RED);
268         ReadbackHelper::fillColorsArea(
269                 expectedColors, getDisplayWidth(),
270                 {0, getDisplayHeight() / 4, getDisplayWidth(), getDisplayHeight() / 2}, GREEN);
271         ReadbackHelper::fillColorsArea(
272                 expectedColors, getDisplayWidth(),
273                 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, BLUE);
274 
275         auto layer = std::make_shared<TestBufferLayer>(
276                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
277                 getDisplayHeight(), common::PixelFormat::RGBA_8888, *mWriter);
278         layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
279         layer->setZOrder(10);
280         layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
281         ASSERT_NO_FATAL_FAILURE(layer->setBuffer(expectedColors));
282 
283         std::vector<std::shared_ptr<TestLayer>> layers = {layer};
284 
285         writeLayers(layers);
286         ASSERT_TRUE(mReader.takeErrors().empty());
287         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
288                                  VtsComposerClient::kNoFrameIntervalNs);
289         execute();
290 
291         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
292             GTEST_SUCCEED();
293             return;
294         }
295         ASSERT_TRUE(mReader.takeErrors().empty());
296 
297         mWriter->presentDisplay(getPrimaryDisplayId());
298         execute();
299 
300         ASSERT_TRUE(mReader.takeErrors().empty());
301 
302         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
303         mTestRenderEngine->setRenderLayers(layers);
304         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
305         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
306     }
307 }
308 
TEST_P(GraphicsCompositionTest,SetLayerBufferNoEffect)309 TEST_P(GraphicsCompositionTest, SetLayerBufferNoEffect) {
310     for (ColorMode mode : mTestColorModes) {
311         EXPECT_TRUE(mComposerClient
312                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
313                             .isOk());
314 
315         bool isSupported;
316         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
317         if (!isSupported) {
318             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
319             return;
320         }
321 
322         auto layer =
323                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
324         common::Rect coloredSquare({0, 0, getDisplayWidth(), getDisplayHeight()});
325         layer->setColor(BLUE);
326         layer->setDisplayFrame(coloredSquare);
327         layer->setZOrder(10);
328         layer->write(*mWriter);
329 
330         // This following buffer call should have no effect
331         const auto usage = static_cast<uint32_t>(common::BufferUsage::CPU_WRITE_OFTEN) |
332                            static_cast<uint32_t>(common::BufferUsage::CPU_READ_OFTEN);
333         const auto& [graphicBufferStatus, graphicBuffer] = allocateBuffer(usage);
334         ASSERT_TRUE(graphicBufferStatus);
335         const auto& buffer = graphicBuffer->handle;
336         mWriter->setLayerBuffer(getPrimaryDisplayId(), layer->getLayer(), /*slot*/ 0, buffer,
337                                 /*acquireFence*/ -1);
338 
339         // expected color for each pixel
340         std::vector<Color> expectedColors(
341                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
342         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), coloredSquare, BLUE);
343 
344         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
345                                       getDisplayHeight(), mPixelFormat, mDataspace);
346         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
347 
348         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
349                                  VtsComposerClient::kNoFrameIntervalNs);
350         execute();
351 
352         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
353             GTEST_SUCCEED();
354             return;
355         }
356         ASSERT_TRUE(mReader.takeErrors().empty());
357         mWriter->presentDisplay(getPrimaryDisplayId());
358         execute();
359         ASSERT_TRUE(mReader.takeErrors().empty());
360 
361         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
362     }
363 }
364 
TEST_P(GraphicsCompositionTest,SetReadbackBuffer)365 TEST_P(GraphicsCompositionTest, SetReadbackBuffer) {
366     bool isSupported;
367     ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
368     if (!isSupported) {
369         GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
370         return;
371     }
372 
373     ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
374                                   getDisplayHeight(), mPixelFormat, mDataspace);
375 
376     ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
377 }
378 
TEST_P(GraphicsCompositionTest,SetReadbackBuffer_BadDisplay)379 TEST_P(GraphicsCompositionTest, SetReadbackBuffer_BadDisplay) {
380     bool isSupported;
381     ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
382     if (!isSupported) {
383         GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
384         return;
385     }
386 
387     const auto usage = static_cast<uint32_t>(common::BufferUsage::CPU_WRITE_OFTEN) |
388                        static_cast<uint32_t>(common::BufferUsage::CPU_READ_OFTEN);
389     const auto& [graphicBufferStatus, graphicBuffer] = allocateBuffer(usage);
390     ASSERT_TRUE(graphicBufferStatus);
391     const auto& bufferHandle = graphicBuffer->handle;
392     ::ndk::ScopedFileDescriptor fence = ::ndk::ScopedFileDescriptor(-1);
393 
394     const auto status =
395             mComposerClient->setReadbackBuffer(getInvalidDisplayId(), bufferHandle, fence);
396 
397     EXPECT_FALSE(status.isOk());
398     EXPECT_NO_FATAL_FAILURE(assertServiceSpecificError(status, IComposerClient::EX_BAD_DISPLAY));
399 }
400 
TEST_P(GraphicsCompositionTest,SetReadbackBuffer_BadParameter)401 TEST_P(GraphicsCompositionTest, SetReadbackBuffer_BadParameter) {
402     bool isSupported;
403     ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
404     if (!isSupported) {
405         GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
406         return;
407     }
408 
409     const native_handle_t bufferHandle{};
410     ndk::ScopedFileDescriptor releaseFence = ndk::ScopedFileDescriptor(-1);
411     const auto status =
412             mComposerClient->setReadbackBuffer(getPrimaryDisplayId(), &bufferHandle, releaseFence);
413 
414     EXPECT_FALSE(status.isOk());
415     EXPECT_NO_FATAL_FAILURE(assertServiceSpecificError(status, IComposerClient::EX_BAD_PARAMETER));
416 }
417 
TEST_P(GraphicsCompositionTest,GetReadbackBufferFenceInactive)418 TEST_P(GraphicsCompositionTest, GetReadbackBufferFenceInactive) {
419     bool isSupported;
420     ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
421     if (!isSupported) {
422         GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
423         return;
424     }
425 
426     const auto& [status, releaseFence] =
427             mComposerClient->getReadbackBufferFence(getPrimaryDisplayId());
428 
429     EXPECT_FALSE(status.isOk());
430     EXPECT_NO_FATAL_FAILURE(assertServiceSpecificError(status, IComposerClient::EX_UNSUPPORTED));
431     EXPECT_EQ(-1, releaseFence.get());
432 }
433 
TEST_P(GraphicsCompositionTest,ClientComposition)434 TEST_P(GraphicsCompositionTest, ClientComposition) {
435     EXPECT_TRUE(
436             mComposerClient->setClientTargetSlotCount(getPrimaryDisplayId(), kClientTargetSlotCount)
437                     .isOk());
438 
439     for (ColorMode mode : mTestColorModes) {
440         EXPECT_TRUE(mComposerClient
441                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
442                             .isOk());
443 
444         bool isSupported;
445         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
446         if (!isSupported) {
447             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
448             return;
449         }
450 
451         std::vector<Color> expectedColors(
452                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
453         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
454                                        {0, 0, getDisplayWidth(), getDisplayHeight() / 4}, RED);
455         ReadbackHelper::fillColorsArea(
456                 expectedColors, getDisplayWidth(),
457                 {0, getDisplayHeight() / 4, getDisplayWidth(), getDisplayHeight() / 2}, GREEN);
458         ReadbackHelper::fillColorsArea(
459                 expectedColors, getDisplayWidth(),
460                 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, BLUE);
461 
462         auto layer = std::make_shared<TestBufferLayer>(
463                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
464                 getDisplayHeight(), PixelFormat::RGBA_FP16, *mWriter);
465         layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
466         layer->setZOrder(10);
467         layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
468 
469         std::vector<std::shared_ptr<TestLayer>> layers = {layer};
470 
471         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
472                                       getDisplayHeight(), mPixelFormat, mDataspace);
473         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
474         writeLayers(layers);
475         ASSERT_TRUE(mReader.takeErrors().empty());
476         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
477                                  VtsComposerClient::kNoFrameIntervalNs);
478         execute();
479 
480         auto changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
481         if (!changedCompositionTypes.empty()) {
482             ASSERT_EQ(1, changedCompositionTypes.size());
483             ASSERT_EQ(Composition::CLIENT, changedCompositionTypes[0].composition);
484 
485             PixelFormat clientFormat = PixelFormat::RGBA_8888;
486             auto clientUsage = static_cast<uint32_t>(
487                     static_cast<uint32_t>(common::BufferUsage::CPU_READ_OFTEN) |
488                     static_cast<uint32_t>(common::BufferUsage::CPU_WRITE_OFTEN) |
489                     static_cast<uint32_t>(common::BufferUsage::COMPOSER_CLIENT_TARGET));
490             Dataspace clientDataspace = ReadbackHelper::getDataspaceForColorMode(mode);
491             common::Rect damage{0, 0, getDisplayWidth(), getDisplayHeight()};
492 
493             // create client target buffer
494             const auto& [graphicBufferStatus, graphicBuffer] = allocateBuffer(clientUsage);
495             ASSERT_TRUE(graphicBufferStatus);
496             const auto& buffer = graphicBuffer->handle;
497             void* clientBufData;
498             const auto stride = static_cast<uint32_t>(graphicBuffer->stride);
499             graphicBuffer->lock(clientUsage, layer->getAccessRegion(), &clientBufData);
500 
501             ASSERT_NO_FATAL_FAILURE(
502                     ReadbackHelper::fillBuffer(layer->getWidth(), layer->getHeight(), stride,
503                                                clientBufData, clientFormat, expectedColors));
504             int32_t clientFence;
505             const auto unlockStatus = graphicBuffer->unlockAsync(&clientFence);
506             ASSERT_EQ(::android::OK, unlockStatus);
507             mWriter->setClientTarget(getPrimaryDisplayId(), /*slot*/ 0, buffer, clientFence,
508                                      clientDataspace, std::vector<common::Rect>(1, damage), 1.f);
509             layer->setToClientComposition(*mWriter);
510             mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
511                                      VtsComposerClient::kNoFrameIntervalNs);
512             execute();
513             changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
514             ASSERT_TRUE(changedCompositionTypes.empty());
515         }
516         ASSERT_TRUE(mReader.takeErrors().empty());
517 
518         mWriter->presentDisplay(getPrimaryDisplayId());
519         execute();
520 
521         ASSERT_TRUE(mReader.takeErrors().empty());
522 
523         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
524     }
525 }
526 
TEST_P(GraphicsCompositionTest,MixedColorSpaces)527 TEST_P(GraphicsCompositionTest, MixedColorSpaces) {
528     ASSERT_TRUE(
529             mComposerClient->setClientTargetSlotCount(getPrimaryDisplayId(), kClientTargetSlotCount)
530                     .isOk());
531     const auto& [status, properties] = mComposerClient->getOverlaySupport();
532     if (!status.isOk() && status.getExceptionCode() == EX_SERVICE_SPECIFIC &&
533         status.getServiceSpecificError() == IComposerClient::EX_UNSUPPORTED) {
534         GTEST_SUCCEED() << "getOverlaySupport is not supported";
535         return;
536     }
537 
538     if (properties.supportMixedColorSpaces == false) {
539         GTEST_SUCCEED() << "supportMixedColorSpaces is not supported";
540         return;
541     }
542 
543     for (ColorMode mode : mTestColorModes) {
544         EXPECT_TRUE(mComposerClient
545                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
546                             .isOk());
547 
548         bool isSupported;
549         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
550         if (!isSupported) {
551             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
552             return;
553         }
554 
555         // sRGB layer
556         auto srgbLayer = std::make_shared<TestBufferLayer>(
557                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
558                 getDisplayHeight() / 2, PixelFormat::RGBA_8888, *mWriter);
559         std::vector<Color> sRgbDeviceColors(srgbLayer->getWidth() * srgbLayer->getHeight());
560         ReadbackHelper::fillColorsArea(sRgbDeviceColors, getDisplayWidth(),
561                                        {0, 0, static_cast<int32_t>(srgbLayer->getWidth()),
562                                         static_cast<int32_t>(srgbLayer->getHeight())},
563                                        GREEN);
564         srgbLayer->setDisplayFrame({0, 0, static_cast<int32_t>(srgbLayer->getWidth()),
565                                     static_cast<int32_t>(srgbLayer->getHeight())});
566         srgbLayer->setZOrder(10);
567         srgbLayer->setDataspace(Dataspace::SRGB);
568         ASSERT_NO_FATAL_FAILURE(srgbLayer->setBuffer(sRgbDeviceColors));
569 
570         // display P3 layer
571         auto displayP3Layer = std::make_shared<TestBufferLayer>(
572                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
573                 getDisplayHeight() / 2, PixelFormat::RGBA_8888, *mWriter);
574         std::vector<Color> displayP3DeviceColors(
575                 static_cast<size_t>(displayP3Layer->getWidth() * displayP3Layer->getHeight()));
576         ReadbackHelper::fillColorsArea(displayP3DeviceColors, getDisplayWidth(),
577                                        {0, 0, static_cast<int32_t>(displayP3Layer->getWidth()),
578                                         static_cast<int32_t>(displayP3Layer->getHeight())},
579                                        RED);
580         displayP3Layer->setDisplayFrame(
581                 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()});
582         displayP3Layer->setZOrder(10);
583         displayP3Layer->setDataspace(Dataspace::DISPLAY_P3);
584         ASSERT_NO_FATAL_FAILURE(displayP3Layer->setBuffer(displayP3DeviceColors));
585 
586         writeLayers({srgbLayer, displayP3Layer});
587 
588         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
589                                  VtsComposerClient::kNoFrameIntervalNs);
590         execute();
591 
592         auto changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
593         ASSERT_TRUE(changedCompositionTypes.empty());
594 
595         mWriter->presentDisplay(getPrimaryDisplayId());
596         execute();
597 
598         changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
599         ASSERT_TRUE(changedCompositionTypes.empty());
600         ASSERT_TRUE(mReader.takeErrors().empty());
601     }
602 }
603 
TEST_P(GraphicsCompositionTest,DeviceAndClientComposition)604 TEST_P(GraphicsCompositionTest, DeviceAndClientComposition) {
605     ASSERT_TRUE(
606             mComposerClient->setClientTargetSlotCount(getPrimaryDisplayId(), kClientTargetSlotCount)
607                     .isOk());
608 
609     for (ColorMode mode : mTestColorModes) {
610         EXPECT_TRUE(mComposerClient
611                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
612                             .isOk());
613 
614         bool isSupported;
615         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
616         if (!isSupported) {
617             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
618             return;
619         }
620 
621         std::vector<Color> expectedColors(
622                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
623         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
624                                        {0, 0, getDisplayWidth(), getDisplayHeight() / 2}, GREEN);
625         ReadbackHelper::fillColorsArea(
626                 expectedColors, getDisplayWidth(),
627                 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, RED);
628 
629         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
630                                       getDisplayHeight(), mPixelFormat, mDataspace);
631         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
632 
633         auto deviceLayer = std::make_shared<TestBufferLayer>(
634                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
635                 getDisplayHeight() / 2, PixelFormat::RGBA_8888, *mWriter);
636         std::vector<Color> deviceColors(deviceLayer->getWidth() * deviceLayer->getHeight());
637         ReadbackHelper::fillColorsArea(deviceColors, static_cast<int32_t>(deviceLayer->getWidth()),
638                                        {0, 0, static_cast<int32_t>(deviceLayer->getWidth()),
639                                         static_cast<int32_t>(deviceLayer->getHeight())},
640                                        GREEN);
641         deviceLayer->setDisplayFrame({0, 0, static_cast<int32_t>(deviceLayer->getWidth()),
642                                       static_cast<int32_t>(deviceLayer->getHeight())});
643         deviceLayer->setZOrder(10);
644         deviceLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
645         ASSERT_NO_FATAL_FAILURE(deviceLayer->setBuffer(deviceColors));
646         deviceLayer->write(*mWriter);
647 
648         PixelFormat clientFormat = PixelFormat::RGBA_8888;
649         auto clientUsage = static_cast<uint32_t>(
650                 static_cast<uint64_t>(common::BufferUsage::CPU_READ_OFTEN) |
651                 static_cast<uint32_t>(common::BufferUsage::CPU_WRITE_OFTEN) |
652                 static_cast<uint32_t>(common::BufferUsage::COMPOSER_CLIENT_TARGET));
653         Dataspace clientDataspace = ReadbackHelper::getDataspaceForColorMode(mode);
654         int32_t clientWidth = getDisplayWidth();
655         int32_t clientHeight = getDisplayHeight() / 2;
656 
657         auto clientLayer = std::make_shared<TestBufferLayer>(
658                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), clientWidth,
659                 clientHeight, PixelFormat::RGBA_FP16, *mWriter, Composition::DEVICE);
660         common::Rect clientFrame = {0, getDisplayHeight() / 2, getDisplayWidth(),
661                                     getDisplayHeight()};
662         clientLayer->setDisplayFrame(clientFrame);
663         clientLayer->setZOrder(0);
664         clientLayer->write(*mWriter);
665         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
666                                  VtsComposerClient::kNoFrameIntervalNs);
667         execute();
668 
669         auto changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
670         if (changedCompositionTypes.size() != 1) {
671             continue;
672         }
673         // create client target buffer
674         ASSERT_EQ(Composition::CLIENT, changedCompositionTypes[0].composition);
675         const auto& [graphicBufferStatus, graphicBuffer] = allocateBuffer(clientUsage);
676         ASSERT_TRUE(graphicBufferStatus);
677         const auto& buffer = graphicBuffer->handle;
678 
679         void* clientBufData;
680         graphicBuffer->lock(clientUsage, {0, 0, getDisplayWidth(), getDisplayHeight()},
681                             &clientBufData);
682 
683         std::vector<Color> clientColors(
684                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
685         ReadbackHelper::fillColorsArea(clientColors, getDisplayWidth(), clientFrame, RED);
686         ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBuffer(
687                 static_cast<uint32_t>(getDisplayWidth()), static_cast<uint32_t>(getDisplayHeight()),
688                 graphicBuffer->getStride(), clientBufData, clientFormat, clientColors));
689         int32_t clientFence;
690         const auto unlockStatus = graphicBuffer->unlockAsync(&clientFence);
691         ASSERT_EQ(::android::OK, unlockStatus);
692         mWriter->setClientTarget(getPrimaryDisplayId(), /*slot*/ 0, buffer, clientFence,
693                                  clientDataspace, std::vector<common::Rect>(1, clientFrame), 1.f);
694         clientLayer->setToClientComposition(*mWriter);
695         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
696                                  VtsComposerClient::kNoFrameIntervalNs);
697         execute();
698         changedCompositionTypes = mReader.takeChangedCompositionTypes(getPrimaryDisplayId());
699         ASSERT_TRUE(changedCompositionTypes.empty());
700         ASSERT_TRUE(mReader.takeErrors().empty());
701 
702         mWriter->presentDisplay(getPrimaryDisplayId());
703         execute();
704         ASSERT_TRUE(mReader.takeErrors().empty());
705         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
706     }
707 }
708 
TEST_P(GraphicsCompositionTest,SetLayerDamage)709 TEST_P(GraphicsCompositionTest, SetLayerDamage) {
710     for (ColorMode mode : mTestColorModes) {
711         EXPECT_TRUE(mComposerClient
712                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
713                             .isOk());
714 
715         bool isSupported;
716         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
717         if (!isSupported) {
718             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
719             return;
720         }
721 
722         common::Rect redRect = {0, 0, getDisplayWidth() / 4, getDisplayHeight() / 4};
723 
724         std::vector<Color> expectedColors(
725                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
726         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
727 
728         auto layer = std::make_shared<TestBufferLayer>(
729                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
730                 getDisplayHeight(), PixelFormat::RGBA_8888, *mWriter);
731         layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
732         layer->setZOrder(10);
733         layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
734         ASSERT_NO_FATAL_FAILURE(layer->setBuffer(expectedColors));
735 
736         std::vector<std::shared_ptr<TestLayer>> layers = {layer};
737 
738         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
739                                       getDisplayHeight(), mPixelFormat, mDataspace);
740         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
741 
742         writeLayers(layers);
743         ASSERT_TRUE(mReader.takeErrors().empty());
744         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
745                                  VtsComposerClient::kNoFrameIntervalNs);
746         execute();
747         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
748             GTEST_SUCCEED();
749             return;
750         }
751         ASSERT_TRUE(mReader.takeErrors().empty());
752         mWriter->presentDisplay(getPrimaryDisplayId());
753         execute();
754         ASSERT_TRUE(mReader.takeErrors().empty());
755 
756         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
757 
758         // update surface damage and recheck
759         redRect = {getDisplayWidth() / 4, getDisplayHeight() / 4, getDisplayWidth() / 2,
760                    getDisplayHeight() / 2};
761         ReadbackHelper::clearColors(expectedColors, getDisplayWidth(), getDisplayHeight(),
762                                     getDisplayWidth());
763         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
764 
765         ASSERT_NO_FATAL_FAILURE(layer->fillBuffer(expectedColors));
766         layer->setSurfaceDamage(
767                 std::vector<common::Rect>(1, {0, 0, getDisplayWidth() / 2, getDisplayWidth() / 2}));
768 
769         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
770 
771         writeLayers(layers);
772         ASSERT_TRUE(mReader.takeErrors().empty());
773         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
774                                  VtsComposerClient::kNoFrameIntervalNs);
775         execute();
776         ASSERT_TRUE(mReader.takeErrors().empty());
777         ASSERT_TRUE(mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty());
778         mWriter->presentDisplay(getPrimaryDisplayId());
779         execute();
780         ASSERT_TRUE(mReader.takeErrors().empty());
781 
782         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
783     }
784 }
785 
TEST_P(GraphicsCompositionTest,SetLayerPlaneAlpha)786 TEST_P(GraphicsCompositionTest, SetLayerPlaneAlpha) {
787     for (ColorMode mode : mTestColorModes) {
788         EXPECT_TRUE(mComposerClient
789                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
790                             .isOk());
791 
792         bool isSupported;
793         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
794         if (!isSupported) {
795             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
796             return;
797         }
798 
799         auto layer =
800                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
801         layer->setColor(RED);
802         layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
803         layer->setZOrder(10);
804         layer->setAlpha(0);
805         layer->setBlendMode(BlendMode::PREMULTIPLIED);
806 
807         std::vector<std::shared_ptr<TestLayer>> layers = {layer};
808 
809         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
810                                       getDisplayHeight(), mPixelFormat, mDataspace);
811 
812         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
813 
814         writeLayers(layers);
815         ASSERT_TRUE(mReader.takeErrors().empty());
816         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
817                                  VtsComposerClient::kNoFrameIntervalNs);
818         execute();
819         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
820             GTEST_SUCCEED();
821             return;
822         }
823         ASSERT_TRUE(mReader.takeErrors().empty());
824 
825         mWriter->presentDisplay(getPrimaryDisplayId());
826         execute();
827         ASSERT_TRUE(mReader.takeErrors().empty());
828 
829         std::vector<Color> expectedColors(
830                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
831 
832         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
833         mTestRenderEngine->setRenderLayers(layers);
834         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
835         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
836     }
837 }
838 
TEST_P(GraphicsCompositionTest,SetLayerSourceCrop)839 TEST_P(GraphicsCompositionTest, SetLayerSourceCrop) {
840     for (ColorMode mode : mTestColorModes) {
841         EXPECT_TRUE(mComposerClient
842                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
843                             .isOk());
844 
845         bool isSupported;
846         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
847         if (!isSupported) {
848             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
849             return;
850         }
851 
852         std::vector<Color> expectedColors(
853                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
854         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
855                                        {0, 0, getDisplayWidth(), getDisplayHeight() / 4}, RED);
856         ReadbackHelper::fillColorsArea(
857                 expectedColors, getDisplayWidth(),
858                 {0, getDisplayHeight() / 2, getDisplayWidth(), getDisplayHeight()}, BLUE);
859 
860         auto layer = std::make_shared<TestBufferLayer>(
861                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
862                 getDisplayHeight(), PixelFormat::RGBA_8888, *mWriter);
863         layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
864         layer->setZOrder(10);
865         layer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
866         layer->setSourceCrop({0, static_cast<float>(getDisplayHeight() / 2),
867                               static_cast<float>(getDisplayWidth()),
868                               static_cast<float>(getDisplayHeight())});
869         ASSERT_NO_FATAL_FAILURE(layer->setBuffer(expectedColors));
870 
871         std::vector<std::shared_ptr<TestLayer>> layers = {layer};
872 
873         // update expected colors to match crop
874         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
875                                        {0, 0, getDisplayWidth(), getDisplayHeight()}, BLUE);
876         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
877                                       getDisplayHeight(), mPixelFormat, mDataspace);
878         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
879         writeLayers(layers);
880         ASSERT_TRUE(mReader.takeErrors().empty());
881         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
882                                  VtsComposerClient::kNoFrameIntervalNs);
883         execute();
884         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
885             GTEST_SUCCEED();
886             return;
887         }
888         ASSERT_TRUE(mReader.takeErrors().empty());
889         mWriter->presentDisplay(getPrimaryDisplayId());
890         execute();
891         ASSERT_TRUE(mReader.takeErrors().empty());
892         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
893         mTestRenderEngine->setRenderLayers(layers);
894         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
895         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
896     }
897 }
898 
TEST_P(GraphicsCompositionTest,SetLayerZOrder)899 TEST_P(GraphicsCompositionTest, SetLayerZOrder) {
900     for (ColorMode mode : mTestColorModes) {
901         EXPECT_TRUE(mComposerClient
902                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
903                             .isOk());
904 
905         bool isSupported;
906         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
907         if (!isSupported) {
908             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
909             return;
910         }
911 
912         common::Rect redRect = {0, 0, getDisplayWidth(), getDisplayHeight() / 2};
913         common::Rect blueRect = {0, getDisplayHeight() / 4, getDisplayWidth(), getDisplayHeight()};
914         auto redLayer =
915                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
916         redLayer->setColor(RED);
917         redLayer->setDisplayFrame(redRect);
918 
919         auto blueLayer =
920                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
921         blueLayer->setColor(BLUE);
922         blueLayer->setDisplayFrame(blueRect);
923         blueLayer->setZOrder(5);
924 
925         std::vector<std::shared_ptr<TestLayer>> layers = {redLayer, blueLayer};
926         std::vector<Color> expectedColors(
927                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
928 
929         // red in front of blue
930         redLayer->setZOrder(10);
931 
932         // fill blue first so that red will overwrite on overlap
933         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), blueRect, BLUE);
934         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
935 
936         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
937                                       getDisplayHeight(), mPixelFormat, mDataspace);
938         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
939 
940         writeLayers(layers);
941         ASSERT_TRUE(mReader.takeErrors().empty());
942         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
943                                  VtsComposerClient::kNoFrameIntervalNs);
944         execute();
945         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
946             GTEST_SUCCEED();
947             return;
948         }
949         mWriter->presentDisplay(getPrimaryDisplayId());
950         execute();
951         ASSERT_TRUE(mReader.takeErrors().empty());
952 
953         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
954 
955         redLayer->setZOrder(1);
956         ReadbackHelper::clearColors(expectedColors, getDisplayWidth(), getDisplayHeight(),
957                                     getDisplayWidth());
958         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
959         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), blueRect, BLUE);
960 
961         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
962 
963         writeLayers(layers);
964         ASSERT_TRUE(mReader.takeErrors().empty());
965         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
966                                  VtsComposerClient::kNoFrameIntervalNs);
967         execute();
968         ASSERT_TRUE(mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty());
969         ASSERT_TRUE(mReader.takeErrors().empty());
970         mWriter->presentDisplay(getPrimaryDisplayId());
971         execute();
972         ASSERT_TRUE(mReader.takeErrors().empty());
973 
974         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
975         mTestRenderEngine->setRenderLayers(layers);
976         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
977         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
978     }
979 }
980 
TEST_P(GraphicsCompositionTest,SetLayerBrightnessDims)981 TEST_P(GraphicsCompositionTest, SetLayerBrightnessDims) {
982     for (ColorMode mode : mTestColorModes) {
983         EXPECT_TRUE(mComposerClient
984                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
985                             .isOk());
986 
987         bool isSupported;
988         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
989         if (!isSupported) {
990             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace for "
991                                "color mode: "
992                             << toString(mode);
993             continue;
994         }
995         const common::Rect redRect = {0, 0, getDisplayWidth(), getDisplayHeight() / 2};
996         const common::Rect dimmerRedRect = {0, getDisplayHeight() / 2, getDisplayWidth(),
997                                             getDisplayHeight()};
998 
999         static constexpr float kMaxBrightnessNits = 300.f;
1000 
1001         const auto redLayer =
1002                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
1003         redLayer->setColor(RED);
1004         redLayer->setDisplayFrame(redRect);
1005         redLayer->setWhitePointNits(kMaxBrightnessNits);
1006         redLayer->setBrightness(1.f);
1007 
1008         const auto dimmerRedLayer =
1009                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
1010         dimmerRedLayer->setColor(RED);
1011         dimmerRedLayer->setDisplayFrame(dimmerRedRect);
1012         // Intentionally use a small dimming ratio as some implementations may be more likely to
1013         // kick into GPU composition to apply dithering when the dimming ratio is high.
1014         static constexpr float kDimmingRatio = 0.9f;
1015         dimmerRedLayer->setWhitePointNits(kMaxBrightnessNits * kDimmingRatio);
1016         dimmerRedLayer->setBrightness(kDimmingRatio);
1017 
1018         const std::vector<std::shared_ptr<TestLayer>> layers = {redLayer, dimmerRedLayer};
1019         std::vector<Color> expectedColors(
1020                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1021 
1022         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), redRect, RED);
1023         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(), dimmerRedRect, DIM_RED);
1024 
1025         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1026                                       getDisplayHeight(), mPixelFormat, mDataspace);
1027         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1028 
1029         writeLayers(layers);
1030         ASSERT_TRUE(mReader.takeErrors().empty());
1031         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1032                                  VtsComposerClient::kNoFrameIntervalNs);
1033         execute();
1034         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1035             GTEST_SUCCEED()
1036                     << "Readback verification not supported for GPU composition for color mode: "
1037                     << toString(mode);
1038             continue;
1039         }
1040         mWriter->presentDisplay(getPrimaryDisplayId());
1041         execute();
1042         ASSERT_TRUE(mReader.takeErrors().empty());
1043 
1044         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1045         mTestRenderEngine->setRenderLayers(layers);
1046         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1047         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1048     }
1049 }
1050 
1051 class GraphicsBlendModeCompositionTest
1052     : public GraphicsCompositionTestBase,
1053       public testing::WithParamInterface<std::tuple<std::string, std::string>> {
1054   public:
SetUp()1055     void SetUp() override {
1056         SetUpBase(std::get<0>(GetParam()));
1057         // TODO(b/219590743) we should remove the below SRGB color mode
1058         // once we have the BlendMode test fix for all the versions of the ColorMode
1059         mTestColorModes.erase(
1060                 std::remove_if(mTestColorModes.begin(), mTestColorModes.end(),
1061                                [](ColorMode mode) { return mode != ColorMode::SRGB; }),
1062                 mTestColorModes.end());
1063         mBackgroundColor = BLACK;
1064         mTopLayerColor = RED;
1065     }
1066 
setBackgroundColor(Color color)1067     void setBackgroundColor(Color color) { mBackgroundColor = color; }
1068 
setTopLayerColor(Color color)1069     void setTopLayerColor(Color color) { mTopLayerColor = color; }
1070 
setUpLayers(BlendMode blendMode)1071     void setUpLayers(BlendMode blendMode) {
1072         mLayers.clear();
1073         std::vector<Color> topLayerPixelColors(
1074                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1075         ReadbackHelper::fillColorsArea(topLayerPixelColors, getDisplayWidth(),
1076                                        {0, 0, getDisplayWidth(), getDisplayHeight()},
1077                                        mTopLayerColor);
1078 
1079         auto backgroundLayer =
1080                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
1081         backgroundLayer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
1082         backgroundLayer->setZOrder(0);
1083         backgroundLayer->setColor(mBackgroundColor);
1084 
1085         auto layer = std::make_shared<TestBufferLayer>(
1086                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
1087                 getDisplayHeight(), PixelFormat::RGBA_8888, *mWriter);
1088         layer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
1089         layer->setZOrder(10);
1090         layer->setDataspace(Dataspace::UNKNOWN);
1091         ASSERT_NO_FATAL_FAILURE(layer->setBuffer(topLayerPixelColors));
1092 
1093         layer->setBlendMode(blendMode);
1094         layer->setAlpha(std::stof(std::get<1>(GetParam())));
1095 
1096         mLayers.push_back(backgroundLayer);
1097         mLayers.push_back(layer);
1098     }
1099 
setExpectedColors(std::vector<Color> & expectedColors)1100     void setExpectedColors(std::vector<Color>& expectedColors) {
1101         ASSERT_EQ(2, mLayers.size());
1102         ReadbackHelper::clearColors(expectedColors, getDisplayWidth(), getDisplayHeight(),
1103                                     getDisplayWidth());
1104 
1105         auto layer = mLayers[1];
1106         BlendMode blendMode = layer->getBlendMode();
1107         float alpha = mTopLayerColor.a * layer->getAlpha();
1108         if (blendMode == BlendMode::NONE) {
1109             for (auto& expectedColor : expectedColors) {
1110                 expectedColor.r = mTopLayerColor.r * layer->getAlpha();
1111                 expectedColor.g = mTopLayerColor.g * layer->getAlpha();
1112                 expectedColor.b = mTopLayerColor.b * layer->getAlpha();
1113                 expectedColor.a = alpha;
1114             }
1115         } else if (blendMode == BlendMode::PREMULTIPLIED) {
1116             for (auto& expectedColor : expectedColors) {
1117                 expectedColor.r =
1118                         mTopLayerColor.r * layer->getAlpha() + mBackgroundColor.r * (1.0f - alpha);
1119                 expectedColor.g =
1120                         mTopLayerColor.g * layer->getAlpha() + mBackgroundColor.g * (1.0f - alpha);
1121                 expectedColor.b =
1122                         mTopLayerColor.b * layer->getAlpha() + mBackgroundColor.b * (1.0f - alpha);
1123                 expectedColor.a = alpha + mBackgroundColor.a * (1.0f - alpha);
1124             }
1125         } else if (blendMode == BlendMode::COVERAGE) {
1126             for (auto& expectedColor : expectedColors) {
1127                 expectedColor.r = mTopLayerColor.r * alpha + mBackgroundColor.r * (1.0f - alpha);
1128                 expectedColor.g = mTopLayerColor.g * alpha + mBackgroundColor.g * (1.0f - alpha);
1129                 expectedColor.b = mTopLayerColor.b * alpha + mBackgroundColor.b * (1.0f - alpha);
1130                 expectedColor.a = mTopLayerColor.a * alpha + mBackgroundColor.a * (1.0f - alpha);
1131             }
1132         }
1133     }
1134 
1135   protected:
1136     std::vector<std::shared_ptr<TestLayer>> mLayers;
1137     Color mBackgroundColor;
1138     Color mTopLayerColor;
1139 };
1140 
TEST_P(GraphicsBlendModeCompositionTest,None)1141 TEST_P(GraphicsBlendModeCompositionTest, None) {
1142     for (ColorMode mode : mTestColorModes) {
1143         EXPECT_TRUE(mComposerClient
1144                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1145                             .isOk());
1146 
1147         bool isSupported;
1148         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1149         if (!isSupported) {
1150             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1151             return;
1152         }
1153 
1154         std::vector<Color> expectedColors(
1155                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1156 
1157         setBackgroundColor(BLACK);
1158         setTopLayerColor(TRANSLUCENT_RED);
1159         setUpLayers(BlendMode::NONE);
1160         setExpectedColors(expectedColors);
1161 
1162         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1163                                       getDisplayHeight(), mPixelFormat, mDataspace);
1164         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1165         writeLayers(mLayers);
1166         ASSERT_TRUE(mReader.takeErrors().empty());
1167         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1168                                  VtsComposerClient::kNoFrameIntervalNs);
1169         execute();
1170         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1171             GTEST_SUCCEED();
1172             return;
1173         }
1174         ASSERT_TRUE(mReader.takeErrors().empty());
1175         mWriter->presentDisplay(getPrimaryDisplayId());
1176         execute();
1177         ASSERT_TRUE(mReader.takeErrors().empty());
1178 
1179         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1180         mTestRenderEngine->setRenderLayers(mLayers);
1181         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1182         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1183     }
1184 }
1185 
TEST_P(GraphicsBlendModeCompositionTest,Coverage)1186 TEST_P(GraphicsBlendModeCompositionTest, Coverage) {
1187     for (ColorMode mode : mTestColorModes) {
1188         EXPECT_TRUE(mComposerClient
1189                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1190                             .isOk());
1191 
1192         bool isSupported;
1193         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1194         if (!isSupported) {
1195             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1196             return;
1197         }
1198 
1199         std::vector<Color> expectedColors(
1200                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1201 
1202         setBackgroundColor(BLACK);
1203         setTopLayerColor(TRANSLUCENT_RED);
1204 
1205         setUpLayers(BlendMode::COVERAGE);
1206         setExpectedColors(expectedColors);
1207 
1208         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1209                                       getDisplayHeight(), mPixelFormat, mDataspace);
1210         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1211         writeLayers(mLayers);
1212         ASSERT_TRUE(mReader.takeErrors().empty());
1213         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1214                                  VtsComposerClient::kNoFrameIntervalNs);
1215         execute();
1216         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1217             GTEST_SUCCEED();
1218             return;
1219         }
1220         ASSERT_TRUE(mReader.takeErrors().empty());
1221         mWriter->presentDisplay(getPrimaryDisplayId());
1222         execute();
1223         ASSERT_TRUE(mReader.takeErrors().empty());
1224         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1225     }
1226 }
1227 
TEST_P(GraphicsBlendModeCompositionTest,Premultiplied)1228 TEST_P(GraphicsBlendModeCompositionTest, Premultiplied) {
1229     for (ColorMode mode : mTestColorModes) {
1230         EXPECT_TRUE(mComposerClient
1231                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1232                             .isOk());
1233 
1234         bool isSupported;
1235         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1236         if (!isSupported) {
1237             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1238             return;
1239         }
1240 
1241         std::vector<Color> expectedColors(
1242                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1243 
1244         setBackgroundColor(BLACK);
1245         setTopLayerColor(TRANSLUCENT_RED);
1246         setUpLayers(BlendMode::PREMULTIPLIED);
1247         setExpectedColors(expectedColors);
1248 
1249         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1250                                       getDisplayHeight(), mPixelFormat, mDataspace);
1251         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1252         writeLayers(mLayers);
1253         ASSERT_TRUE(mReader.takeErrors().empty());
1254         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1255                                  VtsComposerClient::kNoFrameIntervalNs);
1256         execute();
1257         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1258             GTEST_SUCCEED();
1259             return;
1260         }
1261         ASSERT_TRUE(mReader.takeErrors().empty());
1262         mWriter->presentDisplay(getPrimaryDisplayId());
1263         execute();
1264         ASSERT_TRUE(mReader.takeErrors().empty());
1265         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1266         mTestRenderEngine->setRenderLayers(mLayers);
1267         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1268         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1269     }
1270 }
1271 
1272 class GraphicsTransformCompositionTest : public GraphicsCompositionTest {
1273   protected:
SetUp()1274     void SetUp() override {
1275         GraphicsCompositionTest::SetUp();
1276 
1277         auto backgroundLayer =
1278                 std::make_shared<TestColorLayer>(mComposerClient, getPrimaryDisplayId(), *mWriter);
1279         backgroundLayer->setColor({0.0f, 0.0f, 0.0f, 0.0f});
1280         backgroundLayer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
1281         backgroundLayer->setZOrder(0);
1282 
1283         mSideLength =
1284                 getDisplayWidth() < getDisplayHeight() ? getDisplayWidth() : getDisplayHeight();
1285         common::Rect redRect = {0, 0, mSideLength / 2, mSideLength / 2};
1286         common::Rect blueRect = {mSideLength / 2, mSideLength / 2, mSideLength, mSideLength};
1287 
1288         mLayer = std::make_shared<TestBufferLayer>(mComposerClient, *mTestRenderEngine,
1289                                                    getPrimaryDisplayId(), mSideLength, mSideLength,
1290                                                    PixelFormat::RGBA_8888, *mWriter);
1291         mLayer->setDisplayFrame({0, 0, mSideLength, mSideLength});
1292         mLayer->setZOrder(10);
1293 
1294         std::vector<Color> baseColors(static_cast<size_t>(mSideLength * mSideLength));
1295         ReadbackHelper::fillColorsArea(baseColors, mSideLength, redRect, RED);
1296         ReadbackHelper::fillColorsArea(baseColors, mSideLength, blueRect, BLUE);
1297         ASSERT_NO_FATAL_FAILURE(mLayer->setBuffer(baseColors));
1298         mLayers = {backgroundLayer, mLayer};
1299     }
1300 
1301   protected:
1302     std::shared_ptr<TestBufferLayer> mLayer;
1303     std::vector<std::shared_ptr<TestLayer>> mLayers;
1304     int mSideLength;
1305 };
1306 
TEST_P(GraphicsTransformCompositionTest,FLIP_H)1307 TEST_P(GraphicsTransformCompositionTest, FLIP_H) {
1308     for (ColorMode mode : mTestColorModes) {
1309         auto status = mComposerClient->setColorMode(getPrimaryDisplayId(), mode,
1310                                                     RenderIntent::COLORIMETRIC);
1311         if (!status.isOk() && status.getExceptionCode() == EX_SERVICE_SPECIFIC &&
1312             (status.getServiceSpecificError() == IComposerClient::EX_UNSUPPORTED ||
1313              status.getServiceSpecificError() == IComposerClient::EX_BAD_PARAMETER)) {
1314             SUCCEED() << "ColorMode not supported, skip test";
1315             return;
1316         }
1317 
1318         bool isSupported;
1319         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1320         if (!isSupported) {
1321             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1322             return;
1323         }
1324         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1325                                       getDisplayHeight(), mPixelFormat, mDataspace);
1326         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1327         mLayer->setTransform(Transform::FLIP_H);
1328         mLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
1329 
1330         std::vector<Color> expectedColors(
1331                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1332         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
1333                                        {mSideLength / 2, 0, mSideLength, mSideLength / 2}, RED);
1334         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
1335                                        {0, mSideLength / 2, mSideLength / 2, mSideLength}, BLUE);
1336 
1337         writeLayers(mLayers);
1338         ASSERT_TRUE(mReader.takeErrors().empty());
1339         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1340                                  VtsComposerClient::kNoFrameIntervalNs);
1341         execute();
1342         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1343             GTEST_SUCCEED();
1344             return;
1345         }
1346         ASSERT_TRUE(mReader.takeErrors().empty());
1347         mWriter->presentDisplay(getPrimaryDisplayId());
1348         execute();
1349         ASSERT_TRUE(mReader.takeErrors().empty());
1350 
1351         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1352         mTestRenderEngine->setRenderLayers(mLayers);
1353         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1354         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1355     }
1356 }
1357 
TEST_P(GraphicsTransformCompositionTest,FLIP_V)1358 TEST_P(GraphicsTransformCompositionTest, FLIP_V) {
1359     for (ColorMode mode : mTestColorModes) {
1360         EXPECT_TRUE(mComposerClient
1361                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1362                             .isOk());
1363 
1364         bool isSupported;
1365         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1366         if (!isSupported) {
1367             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1368             return;
1369         }
1370         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1371                                       getDisplayHeight(), mPixelFormat, mDataspace);
1372         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1373 
1374         mLayer->setTransform(Transform::FLIP_V);
1375         mLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
1376 
1377         std::vector<Color> expectedColors(
1378                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1379         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
1380                                        {0, mSideLength / 2, mSideLength / 2, mSideLength}, RED);
1381         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
1382                                        {mSideLength / 2, 0, mSideLength, mSideLength / 2}, BLUE);
1383 
1384         writeLayers(mLayers);
1385         ASSERT_TRUE(mReader.takeErrors().empty());
1386         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1387                                  VtsComposerClient::kNoFrameIntervalNs);
1388         execute();
1389         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1390             GTEST_SUCCEED();
1391             return;
1392         }
1393         ASSERT_TRUE(mReader.takeErrors().empty());
1394         mWriter->presentDisplay(getPrimaryDisplayId());
1395         execute();
1396         ASSERT_TRUE(mReader.takeErrors().empty());
1397         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1398         mTestRenderEngine->setRenderLayers(mLayers);
1399         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1400         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1401     }
1402 }
1403 
TEST_P(GraphicsTransformCompositionTest,ROT_180)1404 TEST_P(GraphicsTransformCompositionTest, ROT_180) {
1405     for (ColorMode mode : mTestColorModes) {
1406         EXPECT_TRUE(mComposerClient
1407                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1408                             .isOk());
1409 
1410         bool isSupported;
1411         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1412         if (!isSupported) {
1413             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1414             return;
1415         }
1416         ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1417                                       getDisplayHeight(), mPixelFormat, mDataspace);
1418         ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1419 
1420         mLayer->setTransform(Transform::ROT_180);
1421         mLayer->setDataspace(ReadbackHelper::getDataspaceForColorMode(mode));
1422 
1423         std::vector<Color> expectedColors(
1424                 static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1425         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
1426                                        {mSideLength / 2, mSideLength / 2, mSideLength, mSideLength},
1427                                        RED);
1428         ReadbackHelper::fillColorsArea(expectedColors, getDisplayWidth(),
1429                                        {0, 0, mSideLength / 2, mSideLength / 2}, BLUE);
1430 
1431         writeLayers(mLayers);
1432         ASSERT_TRUE(mReader.takeErrors().empty());
1433         mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1434                                  VtsComposerClient::kNoFrameIntervalNs);
1435         execute();
1436         if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1437             GTEST_SUCCEED();
1438             return;
1439         }
1440         ASSERT_TRUE(mReader.takeErrors().empty());
1441         mWriter->presentDisplay(getPrimaryDisplayId());
1442         execute();
1443         ASSERT_TRUE(mReader.takeErrors().empty());
1444         ASSERT_NO_FATAL_FAILURE(readbackBuffer.checkReadbackBuffer(expectedColors));
1445         mTestRenderEngine->setRenderLayers(mLayers);
1446         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1447         ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->checkColorBuffer(expectedColors));
1448     }
1449 }
1450 
1451 class GraphicsColorManagementCompositionTest
1452     : public GraphicsCompositionTestBase,
1453       public testing::WithParamInterface<std::tuple<std::string, Dataspace, Dataspace, Dataspace>> {
1454   public:
SetUp()1455     void SetUp() override {
1456         SetUpBase(std::get<0>(GetParam()));
1457         // for some reason only sRGB reliably works
1458         mTestColorModes.erase(
1459                 std::remove_if(mTestColorModes.begin(), mTestColorModes.end(),
1460                                [](ColorMode mode) { return mode != ColorMode::SRGB; }),
1461                 mTestColorModes.end());
1462         auto standard = std::get<1>(GetParam());
1463         auto transfer = std::get<2>(GetParam());
1464         auto range = std::get<3>(GetParam());
1465 
1466         mLayerDataspace = static_cast<Dataspace>(static_cast<int32_t>(standard) |
1467                                                  static_cast<int32_t>(transfer) |
1468                                                  static_cast<int32_t>(range));
1469         ALOGD("Invoking test for dataspace: {%s, %s, %s}", toString(standard).c_str(),
1470               toString(transfer).c_str(), toString(range).c_str());
1471     }
1472 
makeLayer()1473     void makeLayer() {
1474         mLayer = std::make_shared<TestBufferLayer>(
1475                 mComposerClient, *mTestRenderEngine, getPrimaryDisplayId(), getDisplayWidth(),
1476                 getDisplayHeight(), common::PixelFormat::RGBA_8888, *mWriter);
1477         mLayer->setDisplayFrame({0, 0, getDisplayWidth(), getDisplayHeight()});
1478         mLayer->setZOrder(10);
1479         mLayer->setAlpha(1.f);
1480         mLayer->setDataspace(mLayerDataspace);
1481     }
1482 
fillColor(Color color)1483     void fillColor(Color color) {
1484         std::vector<Color> baseColors(static_cast<size_t>(getDisplayWidth() * getDisplayHeight()));
1485         ReadbackHelper::fillColorsArea(baseColors, getDisplayWidth(),
1486                                        common::Rect{.left = 0,
1487                                                     .top = 0,
1488                                                     .right = getDisplayWidth(),
1489                                                     .bottom = getDisplayHeight()},
1490                                        color);
1491         ASSERT_NO_FATAL_FAILURE(mLayer->setBuffer(baseColors));
1492     }
1493 
1494     Dataspace mLayerDataspace;
1495     std::shared_ptr<TestBufferLayer> mLayer;
1496 };
1497 
TEST_P(GraphicsColorManagementCompositionTest,ColorConversion)1498 TEST_P(GraphicsColorManagementCompositionTest, ColorConversion) {
1499     for (ColorMode mode : mTestColorModes) {
1500         EXPECT_TRUE(mComposerClient
1501                             ->setColorMode(getPrimaryDisplayId(), mode, RenderIntent::COLORIMETRIC)
1502                             .isOk());
1503 
1504         bool isSupported;
1505         ASSERT_NO_FATAL_FAILURE(isSupported = getHasReadbackBuffer());
1506         if (!isSupported) {
1507             GTEST_SUCCEED() << "Readback not supported or unsupported pixelFormat/dataspace";
1508             return;
1509         }
1510 
1511         mClientCompositionDisplaySettings.outputDataspace =
1512                 static_cast<::android::ui::Dataspace>(mDataspace);
1513         mTestRenderEngine->setDisplaySettings(mClientCompositionDisplaySettings);
1514 
1515         makeLayer();
1516         for (auto color : {LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE}) {
1517             ALOGD("Testing color: %f, %f, %f, %f with color mode: %d", color.r, color.g, color.b,
1518                   color.a, mode);
1519             ReadbackBuffer readbackBuffer(getPrimaryDisplayId(), mComposerClient, getDisplayWidth(),
1520                                           getDisplayHeight(), mPixelFormat, mDataspace);
1521             ASSERT_NO_FATAL_FAILURE(readbackBuffer.setReadbackBuffer());
1522             fillColor(color);
1523             writeLayers({mLayer});
1524             EXPECT_TRUE(mComposerClient->setPowerMode(getPrimaryDisplayId(), PowerMode::ON).isOk());
1525 
1526             ASSERT_TRUE(mReader.takeErrors().empty());
1527             mWriter->validateDisplay(getPrimaryDisplayId(), ComposerClientWriter::kNoTimestamp,
1528                                      VtsComposerClient::kNoFrameIntervalNs);
1529             execute();
1530             if (!mReader.takeChangedCompositionTypes(getPrimaryDisplayId()).empty()) {
1531                 continue;
1532             }
1533             ASSERT_TRUE(mReader.takeErrors().empty());
1534             mWriter->presentDisplay(getPrimaryDisplayId());
1535             execute();
1536             ASSERT_TRUE(mReader.takeErrors().empty());
1537 
1538             mTestRenderEngine->setRenderLayers({mLayer});
1539             ASSERT_NO_FATAL_FAILURE(mTestRenderEngine->drawLayers());
1540             ASSERT_NO_FATAL_FAILURE(
1541                     mTestRenderEngine->checkColorBuffer(readbackBuffer.getBuffer()));
1542         }
1543     }
1544 }
1545 
1546 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsCompositionTest);
1547 INSTANTIATE_TEST_SUITE_P(
1548         PerInstance, GraphicsCompositionTest,
1549         testing::ValuesIn(::android::getAidlHalInstanceNames(IComposer::descriptor)),
1550         ::android::PrintInstanceNameToString);
1551 
1552 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsBlendModeCompositionTest);
1553 INSTANTIATE_TEST_SUITE_P(BlendMode, GraphicsBlendModeCompositionTest,
1554                          testing::Combine(testing::ValuesIn(::android::getAidlHalInstanceNames(
1555                                                   IComposer::descriptor)),
1556                                           testing::Values("0.2", "1.0")));
1557 
1558 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsTransformCompositionTest);
1559 INSTANTIATE_TEST_SUITE_P(
1560         PerInstance, GraphicsTransformCompositionTest,
1561         testing::ValuesIn(::android::getAidlHalInstanceNames(IComposer::descriptor)),
1562         ::android::PrintInstanceNameToString);
1563 
1564 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsColorManagementCompositionTest);
1565 INSTANTIATE_TEST_SUITE_P(PerInstance, GraphicsColorManagementCompositionTest,
1566                          testing::Combine(testing::ValuesIn(::android::getAidlHalInstanceNames(
1567                                                   IComposer::descriptor)),
1568                                           // Only check sRGB, but verify that extended range
1569                                           // doesn't trigger any gamma shifts
1570                                           testing::Values(Dataspace::STANDARD_BT709),
1571                                           testing::Values(Dataspace::TRANSFER_SRGB),
1572                                           // Don't test limited range until we send YUV overlays
1573                                           testing::Values(Dataspace::RANGE_FULL,
1574                                                           Dataspace::RANGE_EXTENDED)));
1575 
1576 }  // namespace
1577 }  // namespace aidl::android::hardware::graphics::composer3::vts
1578