1 /*
2  * Copyright (C) 2018 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_hidl_hal_test@2.3"
18 
19 #include <algorithm>
20 #include <numeric>
21 
22 #include <android-base/logging.h>
23 #include <android-base/properties.h>
24 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
25 #include <composer-command-buffer/2.3/ComposerCommandBuffer.h>
26 #include <composer-vts/2.1/GraphicsComposerCallback.h>
27 #include <composer-vts/2.1/TestCommandReader.h>
28 #include <composer-vts/2.3/ComposerVts.h>
29 #include <gtest/gtest.h>
30 #include <hidl/GtestPrinter.h>
31 #include <hidl/ServiceManagement.h>
32 #include <mapper-vts/2.0/MapperVts.h>
33 
34 namespace android {
35 namespace hardware {
36 namespace graphics {
37 namespace composer {
38 namespace V2_3 {
39 namespace vts {
40 namespace {
41 
42 using common::V1_1::RenderIntent;
43 using common::V1_2::ColorMode;
44 using common::V1_2::Dataspace;
45 using common::V1_2::PixelFormat;
46 using V2_2::vts::Gralloc;
47 
48 class GraphicsComposerHidlTest : public ::testing::TestWithParam<std::string> {
49   protected:
SetUp()50     void SetUp() override {
51         ASSERT_NO_FATAL_FAILURE(
52                 mComposer = std::make_unique<Composer>(IComposer::getService(GetParam())));
53         ASSERT_NO_FATAL_FAILURE(mComposerClient = mComposer->createClient());
54 
55         mComposerCallback = new V2_1::vts::GraphicsComposerCallback;
56         mComposerClient->registerCallback(mComposerCallback);
57 
58         // assume the first display is primary and is never removed
59         mPrimaryDisplay = waitForFirstDisplay();
60 
61         mInvalidDisplayId = GetInvalidDisplayId();
62 
63         // explicitly disable vsync
64         mComposerClient->setVsyncEnabled(mPrimaryDisplay, false);
65         mComposerCallback->setVsyncAllowed(false);
66 
67         mWriter = std::make_unique<CommandWriterBase>(1024);
68         mReader = std::make_unique<V2_1::vts::TestCommandReader>();
69     }
70 
TearDown()71     void TearDown() override {
72         ASSERT_EQ(0, mReader->mErrors.size());
73         ASSERT_EQ(0, mReader->mCompositionChanges.size());
74         if (mComposerCallback != nullptr) {
75             EXPECT_EQ(0, mComposerCallback->getInvalidHotplugCount());
76             EXPECT_EQ(0, mComposerCallback->getInvalidRefreshCount());
77             EXPECT_EQ(0, mComposerCallback->getInvalidVsyncCount());
78         }
79     }
80 
81     // returns an invalid display id (one that has not been registered to a
82     // display.  Currently assuming that a device will never have close to
83     // std::numeric_limit<uint64_t>::max() displays registered while running tests
GetInvalidDisplayId()84     Display GetInvalidDisplayId() {
85         std::vector<Display> validDisplays = mComposerCallback->getDisplays();
86         uint64_t id = std::numeric_limits<uint64_t>::max();
87         while (id > 0) {
88             if (std::find(validDisplays.begin(), validDisplays.end(), id) == validDisplays.end()) {
89                 return id;
90             }
91             id--;
92         }
93 
94         return 0;
95     }
96 
execute()97     void execute() { mComposerClient->execute(mReader.get(), mWriter.get()); }
98 
99     // use the slot count usually set by SF
100     static constexpr uint32_t kBufferSlotCount = 64;
101 
102     std::unique_ptr<Composer> mComposer;
103     std::unique_ptr<ComposerClient> mComposerClient;
104     sp<V2_1::vts::GraphicsComposerCallback> mComposerCallback;
105     // the first display and is assumed never to be removed
106     Display mPrimaryDisplay;
107     Display mInvalidDisplayId;
108     std::unique_ptr<CommandWriterBase> mWriter;
109     std::unique_ptr<V2_1::vts::TestCommandReader> mReader;
110 
111    private:
waitForFirstDisplay()112     Display waitForFirstDisplay() {
113         while (true) {
114             std::vector<Display> displays = mComposerCallback->getDisplays();
115             if (displays.empty()) {
116                 usleep(5 * 1000);
117                 continue;
118             }
119 
120             return displays[0];
121         }
122     }
123 };
124 
125 // Tests for IComposerClient::Command.
126 class GraphicsComposerHidlCommandTest : public GraphicsComposerHidlTest {
127    protected:
SetUp()128     void SetUp() override {
129         ASSERT_NO_FATAL_FAILURE(GraphicsComposerHidlTest::SetUp());
130 
131         ASSERT_NO_FATAL_FAILURE(mGralloc = std::make_unique<Gralloc>());
132 
133         mWriter = std::make_unique<CommandWriterBase>(1024);
134         mReader = std::make_unique<V2_1::vts::TestCommandReader>();
135     }
136 
TearDown()137     void TearDown() override {
138         ASSERT_EQ(0, mReader->mErrors.size());
139         ASSERT_NO_FATAL_FAILURE(GraphicsComposerHidlTest::TearDown());
140     }
141 
execute()142     void execute() { mComposerClient->execute(mReader.get(), mWriter.get()); }
143 
144     std::unique_ptr<CommandWriterBase> mWriter;
145     std::unique_ptr<V2_1::vts::TestCommandReader> mReader;
146 
147    private:
148     std::unique_ptr<Gralloc> mGralloc;
149 };
150 
151 /**
152  * Test IComposerClient::getDisplayIdentificationData.
153  *
154  * TODO: Check that ports are unique for multiple displays.
155  */
TEST_P(GraphicsComposerHidlTest,GetDisplayIdentificationData)156 TEST_P(GraphicsComposerHidlTest, GetDisplayIdentificationData) {
157     uint8_t port0;
158     std::vector<uint8_t> data0;
159 
160     if (!mComposerClient->getDisplayIdentificationData(mPrimaryDisplay, &port0, &data0)) {
161         return;
162     }
163 
164     ASSERT_FALSE(data0.empty());
165     constexpr size_t kEdidBlockSize = 128;
166     ASSERT_TRUE(data0.size() % kEdidBlockSize == 0)
167             << "EDID blob length is not a multiple of " << kEdidBlockSize;
168 
169     const uint8_t kEdidHeader[] = {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00};
170     ASSERT_TRUE(std::equal(std::begin(kEdidHeader), std::end(kEdidHeader), data0.begin()))
171             << "EDID blob doesn't start with the fixed EDID header";
172     ASSERT_EQ(0, std::accumulate(data0.begin(), data0.begin() + kEdidBlockSize,
173                                  static_cast<uint8_t>(0)))
174             << "EDID base block doesn't checksum";
175 
176     uint8_t port1;
177     std::vector<uint8_t> data1;
178     ASSERT_TRUE(mComposerClient->getDisplayIdentificationData(mPrimaryDisplay, &port1, &data1));
179 
180     ASSERT_EQ(port0, port1) << "ports are not stable";
181     ASSERT_TRUE(data0.size() == data1.size() &&
182                 std::equal(data0.begin(), data0.end(), data1.begin()))
183             << "data is not stable";
184 }
185 
186 /**
187  * Test IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA.
188  */
TEST_P(GraphicsComposerHidlCommandTest,SET_LAYER_PER_FRAME_METADATA)189 TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_PER_FRAME_METADATA) {
190     Layer layer;
191     ASSERT_NO_FATAL_FAILURE(layer =
192                                 mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
193 
194     mWriter->selectDisplay(mPrimaryDisplay);
195     mWriter->selectLayer(layer);
196 
197     /**
198      * DISPLAY_P3 is a color space that uses the DCI_P3 primaries,
199      * the D65 white point and the SRGB transfer functions.
200      * Rendering Intent: Colorimetric
201      * Primaries:
202      *                  x       y
203      *  green           0.265   0.690
204      *  blue            0.150   0.060
205      *  red             0.680   0.320
206      *  white (D65)     0.3127  0.3290
207      */
208 
209     std::vector<IComposerClient::PerFrameMetadata> hidlMetadata;
210     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X, 0.680});
211     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y, 0.320});
212     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X, 0.265});
213     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y, 0.690});
214     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X, 0.150});
215     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y, 0.060});
216     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::WHITE_POINT_X, 0.3127});
217     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::WHITE_POINT_Y, 0.3290});
218     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::MAX_LUMINANCE, 100.0});
219     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::MIN_LUMINANCE, 0.1});
220     hidlMetadata.push_back({IComposerClient::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL, 78.0});
221     hidlMetadata.push_back(
222         {IComposerClient::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL, 62.0});
223     mWriter->setLayerPerFrameMetadata(hidlMetadata);
224     execute();
225 
226     if (mReader->mErrors.size() == 1 &&
227         static_cast<Error>(mReader->mErrors[0].second) == Error::UNSUPPORTED) {
228         mReader->mErrors.clear();
229         GTEST_SUCCEED() << "SetLayerPerFrameMetadata is not supported";
230         ASSERT_NO_FATAL_FAILURE(mComposerClient->destroyLayer(mPrimaryDisplay, layer));
231         return;
232     }
233 
234     ASSERT_NO_FATAL_FAILURE(mComposerClient->destroyLayer(mPrimaryDisplay, layer));
235 }
236 
237 /**
238  * Test IComposerClient::getHdrCapabilities_2_3
239  */
TEST_P(GraphicsComposerHidlTest,GetHdrCapabilities_2_3)240 TEST_P(GraphicsComposerHidlTest, GetHdrCapabilities_2_3) {
241     float maxLuminance;
242     float maxAverageLuminance;
243     float minLuminance;
244     ASSERT_NO_FATAL_FAILURE(mComposerClient->getHdrCapabilities_2_3(
245         mPrimaryDisplay, &maxLuminance, &maxAverageLuminance, &minLuminance));
246     ASSERT_TRUE(maxLuminance >= minLuminance);
247 }
248 
249 /**
250  * Test IComposerClient::getPerFrameMetadataKeys_2_3
251  */
TEST_P(GraphicsComposerHidlTest,GetPerFrameMetadataKeys_2_3)252 TEST_P(GraphicsComposerHidlTest, GetPerFrameMetadataKeys_2_3) {
253     std::vector<IComposerClient::PerFrameMetadataKey> keys;
254     mComposerClient->getRaw()->getPerFrameMetadataKeys_2_3(
255         mPrimaryDisplay, [&](const auto tmpError, const auto outKeys) {
256             if (tmpError != Error::UNSUPPORTED) {
257                 ASSERT_EQ(Error::NONE, tmpError);
258                 keys = outKeys;
259             }
260         });
261 }
262 
263 /**
264  * TestIComposerClient::getReadbackBufferAttributes_2_3
265  */
TEST_P(GraphicsComposerHidlTest,GetReadbackBufferAttributes_2_3)266 TEST_P(GraphicsComposerHidlTest, GetReadbackBufferAttributes_2_3) {
267     Dataspace dataspace;
268     PixelFormat pixelFormat;
269 
270     mComposerClient->getRaw()->getReadbackBufferAttributes_2_3(
271         mPrimaryDisplay,
272         [&](const auto tmpError, const auto outPixelFormat, const auto outDataspace) {
273             if (tmpError != Error::UNSUPPORTED) {
274                 ASSERT_EQ(Error::NONE, tmpError);
275                 dataspace = outDataspace;
276                 pixelFormat = outPixelFormat;
277             }
278         });
279 }
280 
281 /**
282  * Test IComposerClient::getClientTargetSupport_2_3
283  */
TEST_P(GraphicsComposerHidlTest,GetClientTargetSupport_2_3)284 TEST_P(GraphicsComposerHidlTest, GetClientTargetSupport_2_3) {
285     std::vector<V2_1::Config> configs = mComposerClient->getDisplayConfigs(mPrimaryDisplay);
286     for (auto config : configs) {
287         int32_t width = mComposerClient->getDisplayAttribute(mPrimaryDisplay, config,
288                                                              IComposerClient::Attribute::WIDTH);
289         int32_t height = mComposerClient->getDisplayAttribute(mPrimaryDisplay, config,
290                                                               IComposerClient::Attribute::HEIGHT);
291         ASSERT_LT(0, width);
292         ASSERT_LT(0, height);
293 
294         mComposerClient->setActiveConfig(mPrimaryDisplay, config);
295 
296         ASSERT_TRUE(mComposerClient->getClientTargetSupport_2_3(
297             mPrimaryDisplay, width, height, PixelFormat::RGBA_8888, Dataspace::UNKNOWN));
298     }
299 }
300 /**
301  * Test IComposerClient::getClientTargetSupport_2_3
302  *
303  * Test that IComposerClient::getClientTargetSupport_2_3 returns
304  * Error::BAD_DISPLAY when passed in an invalid display handle
305  */
306 
TEST_P(GraphicsComposerHidlTest,GetClientTargetSupport_2_3BadDisplay)307 TEST_P(GraphicsComposerHidlTest, GetClientTargetSupport_2_3BadDisplay) {
308     std::vector<V2_1::Config> configs = mComposerClient->getDisplayConfigs(mPrimaryDisplay);
309     for (auto config : configs) {
310         int32_t width = mComposerClient->getDisplayAttribute(mPrimaryDisplay, config,
311                                                              IComposerClient::Attribute::WIDTH);
312         int32_t height = mComposerClient->getDisplayAttribute(mPrimaryDisplay, config,
313                                                               IComposerClient::Attribute::HEIGHT);
314         ASSERT_LT(0, width);
315         ASSERT_LT(0, height);
316 
317         mComposerClient->setActiveConfig(mPrimaryDisplay, config);
318 
319         Error error = mComposerClient->getRaw()->getClientTargetSupport_2_3(
320             mInvalidDisplayId, width, height, PixelFormat::RGBA_8888, Dataspace::UNKNOWN);
321 
322         EXPECT_EQ(Error::BAD_DISPLAY, error);
323     }
324 }
325 
326 /**
327  * Test IComposerClient::getRenderIntents_2_3
328  */
TEST_P(GraphicsComposerHidlTest,GetRenderIntents_2_3)329 TEST_P(GraphicsComposerHidlTest, GetRenderIntents_2_3) {
330     std::vector<ColorMode> modes = mComposerClient->getColorModes_2_3(mPrimaryDisplay);
331     for (auto mode : modes) {
332         std::vector<RenderIntent> intents =
333             mComposerClient->getRenderIntents_2_3(mPrimaryDisplay, mode);
334 
335         bool isHdr;
336         switch (mode) {
337             case ColorMode::BT2100_PQ:
338             case ColorMode::BT2100_HLG:
339                 isHdr = true;
340                 break;
341             default:
342                 isHdr = false;
343                 break;
344         }
345         RenderIntent requiredIntent =
346             isHdr ? RenderIntent::TONE_MAP_COLORIMETRIC : RenderIntent::COLORIMETRIC;
347 
348         auto iter = std::find(intents.cbegin(), intents.cend(), requiredIntent);
349         EXPECT_NE(intents.cend(), iter);
350     }
351 }
352 
353 /*
354  * Test IComposerClient::getRenderIntents_2_3
355  *
356  * Test that IComposerClient::getRenderIntents_2_3 returns Error::BAD_DISPLAY when
357  * passed an invalid display handle
358  */
TEST_P(GraphicsComposerHidlTest,GetRenderIntents_2_3BadDisplay)359 TEST_P(GraphicsComposerHidlTest, GetRenderIntents_2_3BadDisplay) {
360     std::vector<ColorMode> modes = mComposerClient->getColorModes_2_3(mPrimaryDisplay);
361     for (auto mode : modes) {
362         mComposerClient->getRaw()->getRenderIntents_2_3(
363             mInvalidDisplayId, mode,
364             [&](const auto& tmpError, const auto&) { EXPECT_EQ(Error::BAD_DISPLAY, tmpError); });
365     }
366 }
367 
368 /*
369  * Test IComposerClient::getRenderIntents_2_3
370  *
371  * Test that IComposerClient::getRenderIntents_2_3 returns Error::BAD_PARAMETER when
372  * pased either an invalid Color mode or an invalid Render Intent
373  */
TEST_P(GraphicsComposerHidlTest,GetRenderIntents_2_3BadParameter)374 TEST_P(GraphicsComposerHidlTest, GetRenderIntents_2_3BadParameter) {
375     mComposerClient->getRaw()->getRenderIntents_2_3(
376         mPrimaryDisplay, static_cast<ColorMode>(-1),
377         [&](const auto& tmpError, const auto&) { EXPECT_EQ(Error::BAD_PARAMETER, tmpError); });
378 }
379 
380 /**
381  * IComposerClient::getColorModes_2_3
382  */
TEST_P(GraphicsComposerHidlTest,GetColorModes_2_3)383 TEST_P(GraphicsComposerHidlTest, GetColorModes_2_3) {
384     std::vector<ColorMode> colorModes = mComposerClient->getColorModes_2_3(mPrimaryDisplay);
385 
386     auto native = std::find(colorModes.cbegin(), colorModes.cend(), ColorMode::NATIVE);
387     ASSERT_NE(colorModes.cend(), native);
388 }
389 
390 /*
391  * Test IComposerClient::getColorModes_2_3
392  *
393  * Test that IComposerClient::getColorModes_2_3 returns Error::BAD_DISPLAY when
394  * passed an invalid display handle
395  */
TEST_P(GraphicsComposerHidlTest,GetColorMode_2_3BadDisplay)396 TEST_P(GraphicsComposerHidlTest, GetColorMode_2_3BadDisplay) {
397     mComposerClient->getRaw()->getColorModes_2_3(
398         mInvalidDisplayId,
399         [&](const auto& tmpError, const auto&) { ASSERT_EQ(Error::BAD_DISPLAY, tmpError); });
400 }
401 
402 /**
403  * IComposerClient::setColorMode_2_3
404  */
TEST_P(GraphicsComposerHidlTest,SetColorMode_2_3)405 TEST_P(GraphicsComposerHidlTest, SetColorMode_2_3) {
406     std::vector<ColorMode> colorModes = mComposerClient->getColorModes_2_3(mPrimaryDisplay);
407     for (auto mode : colorModes) {
408         std::vector<RenderIntent> intents =
409             mComposerClient->getRenderIntents_2_3(mPrimaryDisplay, mode);
410         for (auto intent : intents) {
411             ASSERT_NO_FATAL_FAILURE(
412                 mComposerClient->setColorMode_2_3(mPrimaryDisplay, mode, intent));
413         }
414     }
415 
416     ASSERT_NO_FATAL_FAILURE(mComposerClient->setColorMode_2_3(mPrimaryDisplay, ColorMode::NATIVE,
417                                                               RenderIntent::COLORIMETRIC));
418 }
419 
420 /*
421  * Test IComposerClient::setColorMode_2_3
422  *
423  * Test that IComposerClient::setColorMode_2_3 returns an Error::BAD_DISPLAY
424  * when passed an invalid display handle
425  */
TEST_P(GraphicsComposerHidlTest,SetColorMode_2_3BadDisplay)426 TEST_P(GraphicsComposerHidlTest, SetColorMode_2_3BadDisplay) {
427     Error error = mComposerClient->getRaw()->setColorMode_2_3(mInvalidDisplayId, ColorMode::NATIVE,
428                                                               RenderIntent::COLORIMETRIC);
429 
430     ASSERT_EQ(Error::BAD_DISPLAY, error);
431 }
432 
433 /*
434  * Test IComposerClient::setColorMode_2_3
435  *
436  * Test that IComposerClient::setColorMode_2_3 returns Error::BAD_PARAMETER when
437  * passed an invalid Color mode or an invalid render intent
438  */
TEST_P(GraphicsComposerHidlTest,SetColorMode_2_3BadParameter)439 TEST_P(GraphicsComposerHidlTest, SetColorMode_2_3BadParameter) {
440     Error colorModeError = mComposerClient->getRaw()->setColorMode_2_3(
441         mPrimaryDisplay, static_cast<ColorMode>(-1), RenderIntent::COLORIMETRIC);
442     EXPECT_EQ(Error::BAD_PARAMETER, colorModeError);
443 
444     Error renderIntentError = mComposerClient->getRaw()->setColorMode_2_3(
445         mPrimaryDisplay, ColorMode::NATIVE, static_cast<RenderIntent>(-1));
446     EXPECT_EQ(Error::BAD_PARAMETER, renderIntentError);
447 }
448 
449 /**
450  * Test IComposerClient::Command::SET_LAYER_COLOR_TRANSFORM.
451  * TODO Add color to the layer, use matrix to keep only red component,
452  * and check.
453  */
TEST_P(GraphicsComposerHidlTest,SetLayerColorTransform)454 TEST_P(GraphicsComposerHidlTest, SetLayerColorTransform) {
455     Layer layer;
456     ASSERT_NO_FATAL_FAILURE(layer =
457                                 mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
458     mWriter->selectDisplay(mPrimaryDisplay);
459     mWriter->selectLayer(layer);
460 
461     // clang-format off
462     const std::array<float, 16> matrix = {{
463         1.0f, 0.0f, 0.0f, 0.0f,
464         0.0f, 1.0f, 0.0f, 0.0f,
465         0.0f, 0.0f, 1.0f, 0.0f,
466         0.0f, 0.0f, 0.0f, 1.0f,
467     }};
468     // clang-format on
469 
470     mWriter->setLayerColorTransform(matrix.data());
471     execute();
472 
473     if (mReader->mErrors.size() == 1 &&
474         static_cast<Error>(mReader->mErrors[0].second) == Error::UNSUPPORTED) {
475         mReader->mErrors.clear();
476         GTEST_SUCCEED() << "setLayerColorTransform is not supported";
477         return;
478     }
479 }
480 
TEST_P(GraphicsComposerHidlTest,GetDisplayedContentSamplingAttributes)481 TEST_P(GraphicsComposerHidlTest, GetDisplayedContentSamplingAttributes) {
482     int constexpr invalid = -1;
483     auto format = static_cast<PixelFormat>(invalid);
484     auto dataspace = static_cast<Dataspace>(invalid);
485     auto componentMask = static_cast<hidl_bitfield<IComposerClient::FormatColorComponent>>(invalid);
486     auto error = mComposerClient->getDisplayedContentSamplingAttributes(mPrimaryDisplay, format,
487                                                                         dataspace, componentMask);
488 
489     if (error == Error::UNSUPPORTED) {
490         SUCCEED() << "Device does not support optional extension. Test skipped";
491         return;
492     }
493 
494     EXPECT_EQ(error, Error::NONE);
495     EXPECT_NE(format, static_cast<PixelFormat>(invalid));
496     EXPECT_NE(dataspace, static_cast<Dataspace>(invalid));
497     EXPECT_NE(componentMask,
498               static_cast<hidl_bitfield<IComposerClient::FormatColorComponent>>(invalid));
499 };
500 
TEST_P(GraphicsComposerHidlTest,SetDisplayedContentSamplingEnabled)501 TEST_P(GraphicsComposerHidlTest, SetDisplayedContentSamplingEnabled) {
502     auto const maxFrames = 10;
503     auto const enableAllComponents = 0;
504     auto error = mComposerClient->setDisplayedContentSamplingEnabled(
505         mPrimaryDisplay, IComposerClient::DisplayedContentSampling::ENABLE, enableAllComponents,
506         maxFrames);
507     if (error == Error::UNSUPPORTED) {
508         SUCCEED() << "Device does not support optional extension. Test skipped";
509         return;
510     }
511     EXPECT_EQ(error, Error::NONE);
512 
513     error = mComposerClient->setDisplayedContentSamplingEnabled(
514         mPrimaryDisplay, IComposerClient::DisplayedContentSampling::DISABLE, enableAllComponents,
515         maxFrames);
516     EXPECT_EQ(error, Error::NONE);
517 }
518 
TEST_P(GraphicsComposerHidlTest,GetDisplayedContentSample)519 TEST_P(GraphicsComposerHidlTest, GetDisplayedContentSample) {
520     int constexpr invalid = -1;
521     auto format = static_cast<PixelFormat>(invalid);
522     auto dataspace = static_cast<Dataspace>(invalid);
523     auto componentMask = static_cast<hidl_bitfield<IComposerClient::FormatColorComponent>>(invalid);
524     auto error = mComposerClient->getDisplayedContentSamplingAttributes(mPrimaryDisplay, format,
525                                                                         dataspace, componentMask);
526 
527     uint64_t maxFrames = 10;
528     uint64_t timestamp = 0;
529     uint64_t frameCount = 0;
530     hidl_array<hidl_vec<uint64_t>, 4> histogram;
531     error = mComposerClient->getDisplayedContentSample(mPrimaryDisplay, maxFrames, timestamp,
532                                                        frameCount, histogram[0], histogram[1],
533                                                        histogram[2], histogram[3]);
534     if (error == Error::UNSUPPORTED) {
535         SUCCEED() << "Device does not support optional extension. Test skipped";
536         return;
537     }
538 
539     EXPECT_EQ(error, Error::NONE);
540     EXPECT_LE(frameCount, maxFrames);
541     for (auto i = 0; i < histogram.size(); i++) {
542         if (componentMask & (1 << i)) {
543             EXPECT_NE(histogram[i].size(), 0);
544         } else {
545             EXPECT_EQ(histogram[i].size(), 0);
546         }
547     }
548 }
549 
550 /*
551  * getDisplayCapabilities is required in composer 2.3
552  * Test some constraints.
553  */
TEST_P(GraphicsComposerHidlTest,getDisplayCapabilitiesBasic)554 TEST_P(GraphicsComposerHidlTest, getDisplayCapabilitiesBasic) {
555     std::vector<IComposerClient::DisplayCapability> capabilities;
556     const auto error = mComposerClient->getDisplayCapabilities(mPrimaryDisplay, &capabilities);
557     ASSERT_EQ(Error::NONE, error);
558     const bool hasDozeSupport =
559             std::find(capabilities.begin(), capabilities.end(),
560                       IComposerClient::DisplayCapability::DOZE) != capabilities.end();
561     EXPECT_EQ(mComposerClient->getDozeSupport(mPrimaryDisplay), hasDozeSupport);
562     bool hasBrightnessSupport =
563             std::find(capabilities.begin(), capabilities.end(),
564                       IComposerClient::DisplayCapability::BRIGHTNESS) != capabilities.end();
565     EXPECT_EQ(mComposerClient->getDisplayBrightnessSupport(mPrimaryDisplay), hasBrightnessSupport);
566 }
567 
TEST_P(GraphicsComposerHidlTest,getDisplayCapabilitiesBadDisplay)568 TEST_P(GraphicsComposerHidlTest, getDisplayCapabilitiesBadDisplay) {
569     std::vector<IComposerClient::DisplayCapability> capabilities;
570     const auto error = mComposerClient->getDisplayCapabilities(mInvalidDisplayId, &capabilities);
571     EXPECT_EQ(Error::BAD_DISPLAY, error);
572 }
573 
TEST_P(GraphicsComposerHidlTest,SetLayerPerFrameMetadataBlobs)574 TEST_P(GraphicsComposerHidlTest, SetLayerPerFrameMetadataBlobs) {
575     Layer layer;
576     ASSERT_NO_FATAL_FAILURE(layer =
577                                 mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
578 
579     mWriter->selectDisplay(mPrimaryDisplay);
580     mWriter->selectLayer(layer);
581 
582     std::vector<IComposerClient::PerFrameMetadataBlob> metadata;
583     metadata.push_back(
584         {IComposerClient::PerFrameMetadataKey::HDR10_PLUS_SEI, std::vector<uint8_t>(1, 0xff)});
585 
586     mWriter->setLayerPerFrameMetadataBlobs(metadata);
587     execute();
588 
589     if (mReader->mErrors.size() == 1 &&
590         static_cast<Error>(mReader->mErrors[0].second) == Error::UNSUPPORTED) {
591         mReader->mErrors.clear();
592         GTEST_SUCCEED() << "setLayerDynamicPerFrameMetadata is not supported";
593         return;
594     }
595 }
596 
597 /*
598  * Test that if brightness operations are supported, setDisplayBrightness works as expected.
599  */
TEST_P(GraphicsComposerHidlTest,setDisplayBrightness)600 TEST_P(GraphicsComposerHidlTest, setDisplayBrightness) {
601     std::vector<IComposerClient::DisplayCapability> capabilities;
602     const auto error = mComposerClient->getDisplayCapabilities(mPrimaryDisplay, &capabilities);
603     ASSERT_EQ(Error::NONE, error);
604     bool brightnessSupport =
605             std::find(capabilities.begin(), capabilities.end(),
606                       IComposerClient::DisplayCapability::BRIGHTNESS) != capabilities.end();
607     if (!brightnessSupport) {
608         EXPECT_EQ(mComposerClient->getRaw()->setDisplayBrightness(mPrimaryDisplay, 0.5f),
609                   Error::UNSUPPORTED);
610         GTEST_SUCCEED() << "Brightness operations are not supported";
611         return;
612     }
613 
614     EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, 0.0f), Error::NONE);
615     EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, 0.5f), Error::NONE);
616     EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, 1.0f), Error::NONE);
617     EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, -1.0f), Error::NONE);
618 
619     EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, +2.0f), Error::BAD_PARAMETER);
620     EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, -2.0f), Error::BAD_PARAMETER);
621 }
622 
623 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsComposerHidlTest);
624 INSTANTIATE_TEST_SUITE_P(
625         PerInstance, GraphicsComposerHidlTest,
626         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IComposer::descriptor)),
627         android::hardware::PrintInstanceNameToString);
628 
629 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsComposerHidlCommandTest);
630 INSTANTIATE_TEST_SUITE_P(
631         PerInstance, GraphicsComposerHidlCommandTest,
632         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IComposer::descriptor)),
633         android::hardware::PrintInstanceNameToString);
634 
635 }  // namespace
636 }  // namespace vts
637 }  // namespace V2_3
638 }  // namespace composer
639 }  // namespace graphics
640 }  // namespace hardware
641 }  // namespace android
642 
main(int argc,char ** argv)643 int main(int argc, char** argv) {
644     ::testing::InitGoogleTest(&argc, argv);
645 
646     using namespace std::chrono_literals;
647     if (!android::base::WaitForProperty("init.svc.surfaceflinger", "stopped", 10s)) {
648         ALOGE("Failed to stop init.svc.surfaceflinger");
649         return -1;
650     }
651 
652     return RUN_ALL_TESTS();
653 }