1 /*
2  * Copyright (C) 2016 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"
18 
19 #include <IComposerCommandBuffer.h>
20 #include <android-base/logging.h>
21 #include "VtsHalGraphicsComposerTestUtils.h"
22 #include "VtsHalGraphicsMapperTestUtils.h"
23 
24 #include <VtsHalHidlTargetTestBase.h>
25 #include <unistd.h>
26 
27 #include <algorithm>
28 #include <array>
29 #include <memory>
30 #include <mutex>
31 #include <unordered_set>
32 #include <vector>
33 
34 namespace android {
35 namespace hardware {
36 namespace graphics {
37 namespace composer {
38 namespace V2_1 {
39 namespace tests {
40 namespace {
41 
42 using android::hardware::graphics::common::V1_0::BufferUsage;
43 using android::hardware::graphics::common::V1_0::ColorMode;
44 using android::hardware::graphics::common::V1_0::ColorTransform;
45 using android::hardware::graphics::common::V1_0::Dataspace;
46 using android::hardware::graphics::common::V1_0::PixelFormat;
47 using android::hardware::graphics::common::V1_0::Transform;
48 using android::hardware::graphics::mapper::V2_0::IMapper;
49 using android::hardware::graphics::mapper::V2_0::tests::Gralloc;
50 using GrallocError = android::hardware::graphics::mapper::V2_0::Error;
51 
52 // IComposerCallback to be installed with IComposerClient::registerCallback.
53 class GraphicsComposerCallback : public IComposerCallback {
54  public:
setVsyncAllowed(bool allowed)55   void setVsyncAllowed(bool allowed) {
56     std::lock_guard<std::mutex> lock(mMutex);
57     mVsyncAllowed = allowed;
58   }
59 
getDisplays() const60   std::vector<Display> getDisplays() const {
61     std::lock_guard<std::mutex> lock(mMutex);
62     return std::vector<Display>(mDisplays.begin(), mDisplays.end());
63   }
64 
getInvalidHotplugCount() const65   int getInvalidHotplugCount() const {
66     std::lock_guard<std::mutex> lock(mMutex);
67     return mInvalidHotplugCount;
68   }
69 
getInvalidRefreshCount() const70   int getInvalidRefreshCount() const {
71     std::lock_guard<std::mutex> lock(mMutex);
72     return mInvalidRefreshCount;
73   }
74 
getInvalidVsyncCount() const75   int getInvalidVsyncCount() const {
76     std::lock_guard<std::mutex> lock(mMutex);
77     return mInvalidVsyncCount;
78   }
79 
80  private:
onHotplug(Display display,Connection connection)81   Return<void> onHotplug(Display display, Connection connection) override {
82     std::lock_guard<std::mutex> lock(mMutex);
83 
84     if (connection == Connection::CONNECTED) {
85       if (!mDisplays.insert(display).second) {
86         mInvalidHotplugCount++;
87       }
88     } else if (connection == Connection::DISCONNECTED) {
89       if (!mDisplays.erase(display)) {
90         mInvalidHotplugCount++;
91       }
92     }
93 
94     return Void();
95   }
96 
onRefresh(Display display)97   Return<void> onRefresh(Display display) override {
98     std::lock_guard<std::mutex> lock(mMutex);
99 
100     if (mDisplays.count(display) == 0) {
101       mInvalidRefreshCount++;
102     }
103 
104     return Void();
105   }
106 
onVsync(Display display,int64_t)107   Return<void> onVsync(Display display, int64_t) override {
108     std::lock_guard<std::mutex> lock(mMutex);
109 
110     if (!mVsyncAllowed || mDisplays.count(display) == 0) {
111       mInvalidVsyncCount++;
112     }
113 
114     return Void();
115   }
116 
117   mutable std::mutex mMutex;
118   // the set of all currently connected displays
119   std::unordered_set<Display> mDisplays;
120   // true only when vsync is enabled
121   bool mVsyncAllowed = false;
122 
123   // track invalid callbacks
124   int mInvalidHotplugCount = 0;
125   int mInvalidRefreshCount = 0;
126   int mInvalidVsyncCount = 0;
127 };
128 
129 class GraphicsComposerHidlTest : public ::testing::VtsHalHidlTargetTestBase {
130  protected:
SetUp()131   void SetUp() override {
132     ASSERT_NO_FATAL_FAILURE(mComposer = std::make_unique<Composer>());
133     ASSERT_NO_FATAL_FAILURE(mComposerClient = mComposer->createClient());
134 
135     mComposerCallback = new GraphicsComposerCallback;
136     mComposerClient->registerCallback(mComposerCallback);
137 
138     // assume the first display is primary and is never removed
139     mPrimaryDisplay = waitForFirstDisplay();
140   }
141 
TearDown()142   void TearDown() override {
143     if (mComposerCallback != nullptr) {
144       EXPECT_EQ(0, mComposerCallback->getInvalidHotplugCount());
145       EXPECT_EQ(0, mComposerCallback->getInvalidRefreshCount());
146       EXPECT_EQ(0, mComposerCallback->getInvalidVsyncCount());
147     }
148   }
149 
150   // use the slot count usually set by SF
151   static constexpr uint32_t kBufferSlotCount = 64;
152 
153   std::unique_ptr<Composer> mComposer;
154   std::unique_ptr<ComposerClient> mComposerClient;
155   sp<GraphicsComposerCallback> mComposerCallback;
156   // the first display and is assumed never to be removed
157   Display mPrimaryDisplay;
158 
159  private:
waitForFirstDisplay()160   Display waitForFirstDisplay() {
161     while (true) {
162       std::vector<Display> displays = mComposerCallback->getDisplays();
163       if (displays.empty()) {
164         usleep(5 * 1000);
165         continue;
166       }
167 
168       return displays[0];
169     }
170   }
171 };
172 
173 /**
174  * Test IComposer::getCapabilities.
175  *
176  * Test that IComposer::getCapabilities returns no invalid capabilities.
177  */
TEST_F(GraphicsComposerHidlTest,GetCapabilities)178 TEST_F(GraphicsComposerHidlTest, GetCapabilities) {
179   auto capabilities = mComposer->getCapabilities();
180   ASSERT_EQ(capabilities.end(),
181             std::find(capabilities.begin(), capabilities.end(),
182                       IComposer::Capability::INVALID));
183 }
184 
185 /**
186  * Test IComposer::dumpDebugInfo.
187  */
TEST_F(GraphicsComposerHidlTest,DumpDebugInfo)188 TEST_F(GraphicsComposerHidlTest, DumpDebugInfo) { mComposer->dumpDebugInfo(); }
189 
190 /**
191  * Test IComposer::createClient.
192  *
193  * Test that IComposerClient is a singleton.
194  */
TEST_F(GraphicsComposerHidlTest,CreateClientSingleton)195 TEST_F(GraphicsComposerHidlTest, CreateClientSingleton) {
196   mComposer->getRaw()->createClient([&](const auto& tmpError, const auto&) {
197     EXPECT_EQ(Error::NO_RESOURCES, tmpError);
198   });
199 }
200 
201 /**
202  * Test IComposerClient::createVirtualDisplay and
203  * IComposerClient::destroyVirtualDisplay.
204  *
205  * Test that virtual displays can be created and has the correct display type.
206  */
TEST_F(GraphicsComposerHidlTest,CreateVirtualDisplay)207 TEST_F(GraphicsComposerHidlTest, CreateVirtualDisplay) {
208   if (mComposerClient->getMaxVirtualDisplayCount() == 0) {
209     GTEST_SUCCEED() << "no virtual display support";
210     return;
211   }
212 
213   Display display;
214   PixelFormat format;
215   ASSERT_NO_FATAL_FAILURE(display = mComposerClient->createVirtualDisplay(
216                               64, 64, PixelFormat::IMPLEMENTATION_DEFINED,
217                               kBufferSlotCount, &format));
218 
219   // test display type
220   IComposerClient::DisplayType type = mComposerClient->getDisplayType(display);
221   EXPECT_EQ(IComposerClient::DisplayType::VIRTUAL, type);
222 
223   mComposerClient->destroyVirtualDisplay(display);
224 }
225 
226 /**
227  * Test IComposerClient::createLayer and IComposerClient::destroyLayer.
228  *
229  * Test that layers can be created and destroyed.
230  */
TEST_F(GraphicsComposerHidlTest,CreateLayer)231 TEST_F(GraphicsComposerHidlTest, CreateLayer) {
232   Layer layer;
233   ASSERT_NO_FATAL_FAILURE(
234       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
235 
236   mComposerClient->destroyLayer(mPrimaryDisplay, layer);
237 }
238 
239 /**
240  * Test IComposerClient::getDisplayName.
241  */
TEST_F(GraphicsComposerHidlTest,GetDisplayName)242 TEST_F(GraphicsComposerHidlTest, GetDisplayName) {
243   mComposerClient->getDisplayName(mPrimaryDisplay);
244 }
245 
246 /**
247  * Test IComposerClient::getDisplayType.
248  *
249  * Test that IComposerClient::getDisplayType returns the correct display type
250  * for the primary display.
251  */
TEST_F(GraphicsComposerHidlTest,GetDisplayType)252 TEST_F(GraphicsComposerHidlTest, GetDisplayType) {
253   ASSERT_EQ(IComposerClient::DisplayType::PHYSICAL,
254             mComposerClient->getDisplayType(mPrimaryDisplay));
255 }
256 
257 /**
258  * Test IComposerClient::getClientTargetSupport.
259  *
260  * Test that IComposerClient::getClientTargetSupport returns true for the
261  * required client targets.
262  */
TEST_F(GraphicsComposerHidlTest,GetClientTargetSupport)263 TEST_F(GraphicsComposerHidlTest, GetClientTargetSupport) {
264   std::vector<Config> configs =
265       mComposerClient->getDisplayConfigs(mPrimaryDisplay);
266   for (auto config : configs) {
267     int32_t width = mComposerClient->getDisplayAttribute(
268         mPrimaryDisplay, config, IComposerClient::Attribute::WIDTH);
269     int32_t height = mComposerClient->getDisplayAttribute(
270         mPrimaryDisplay, config, IComposerClient::Attribute::HEIGHT);
271     ASSERT_LT(0, width);
272     ASSERT_LT(0, height);
273 
274     mComposerClient->setActiveConfig(mPrimaryDisplay, config);
275 
276     ASSERT_TRUE(mComposerClient->getClientTargetSupport(
277         mPrimaryDisplay, width, height, PixelFormat::RGBA_8888,
278         Dataspace::UNKNOWN));
279   }
280 }
281 
282 /**
283  * Test IComposerClient::getDisplayAttribute.
284  *
285  * Test that IComposerClient::getDisplayAttribute succeeds for the required
286  * formats, and succeeds or fails correctly for optional attributes.
287  */
TEST_F(GraphicsComposerHidlTest,GetDisplayAttribute)288 TEST_F(GraphicsComposerHidlTest, GetDisplayAttribute) {
289   std::vector<Config> configs =
290       mComposerClient->getDisplayConfigs(mPrimaryDisplay);
291   for (auto config : configs) {
292     const std::array<IComposerClient::Attribute, 3> requiredAttributes = {{
293         IComposerClient::Attribute::WIDTH, IComposerClient::Attribute::HEIGHT,
294         IComposerClient::Attribute::VSYNC_PERIOD,
295     }};
296     for (auto attribute : requiredAttributes) {
297       mComposerClient->getDisplayAttribute(mPrimaryDisplay, config, attribute);
298     }
299 
300     const std::array<IComposerClient::Attribute, 2> optionalAttributes = {{
301         IComposerClient::Attribute::DPI_X, IComposerClient::Attribute::DPI_Y,
302     }};
303     for (auto attribute : optionalAttributes) {
304       mComposerClient->getRaw()->getDisplayAttribute(
305           mPrimaryDisplay, config, attribute,
306           [&](const auto& tmpError, const auto&) {
307             EXPECT_TRUE(tmpError == Error::NONE ||
308                         tmpError == Error::UNSUPPORTED);
309           });
310     }
311   }
312 }
313 
314 /**
315  * Test IComposerClient::getHdrCapabilities.
316  */
TEST_F(GraphicsComposerHidlTest,GetHdrCapabilities)317 TEST_F(GraphicsComposerHidlTest, GetHdrCapabilities) {
318   float maxLuminance;
319   float maxAverageLuminance;
320   float minLuminance;
321   mComposerClient->getHdrCapabilities(mPrimaryDisplay, &maxLuminance,
322                                       &maxAverageLuminance, &minLuminance);
323 }
324 
325 /**
326  * Test IComposerClient::setClientTargetSlotCount.
327  */
TEST_F(GraphicsComposerHidlTest,SetClientTargetSlotCount)328 TEST_F(GraphicsComposerHidlTest, SetClientTargetSlotCount) {
329   mComposerClient->setClientTargetSlotCount(mPrimaryDisplay, kBufferSlotCount);
330 }
331 
332 /**
333  * Test IComposerClient::setActiveConfig.
334  *
335  * Test that IComposerClient::setActiveConfig succeeds for all display
336  * configs.
337  */
TEST_F(GraphicsComposerHidlTest,SetActiveConfig)338 TEST_F(GraphicsComposerHidlTest, SetActiveConfig) {
339   std::vector<Config> configs =
340       mComposerClient->getDisplayConfigs(mPrimaryDisplay);
341   for (auto config : configs) {
342     mComposerClient->setActiveConfig(mPrimaryDisplay, config);
343     ASSERT_EQ(config, mComposerClient->getActiveConfig(mPrimaryDisplay));
344   }
345 }
346 
347 /**
348  * Test IComposerClient::setColorMode.
349  *
350  * Test that IComposerClient::setColorMode succeeds for all color modes.
351  */
TEST_F(GraphicsComposerHidlTest,SetColorMode)352 TEST_F(GraphicsComposerHidlTest, SetColorMode) {
353   std::vector<ColorMode> modes =
354       mComposerClient->getColorModes(mPrimaryDisplay);
355   for (auto mode : modes) {
356     mComposerClient->setColorMode(mPrimaryDisplay, mode);
357   }
358 }
359 
360 /**
361  * Test IComposerClient::setPowerMode.
362  *
363  * Test that IComposerClient::setPowerMode succeeds for all power modes.
364  */
TEST_F(GraphicsComposerHidlTest,SetPowerMode)365 TEST_F(GraphicsComposerHidlTest, SetPowerMode) {
366   std::vector<IComposerClient::PowerMode> modes;
367   modes.push_back(IComposerClient::PowerMode::OFF);
368 
369   if (mComposerClient->getDozeSupport(mPrimaryDisplay)) {
370     modes.push_back(IComposerClient::PowerMode::DOZE);
371     modes.push_back(IComposerClient::PowerMode::DOZE_SUSPEND);
372   }
373 
374   // push ON last
375   modes.push_back(IComposerClient::PowerMode::ON);
376 
377   for (auto mode : modes) {
378     mComposerClient->setPowerMode(mPrimaryDisplay, mode);
379   }
380 }
381 
382 /**
383  * Test IComposerClient::setVsyncEnabled.
384  *
385  * Test that IComposerClient::setVsyncEnabled succeeds and there is no
386  * spurious vsync events.
387  */
TEST_F(GraphicsComposerHidlTest,SetVsyncEnabled)388 TEST_F(GraphicsComposerHidlTest, SetVsyncEnabled) {
389   mComposerCallback->setVsyncAllowed(true);
390 
391   mComposerClient->setVsyncEnabled(mPrimaryDisplay, true);
392   usleep(60 * 1000);
393   mComposerClient->setVsyncEnabled(mPrimaryDisplay, false);
394 
395   mComposerCallback->setVsyncAllowed(false);
396 }
397 
398 // Tests for IComposerClient::Command.
399 class GraphicsComposerHidlCommandTest : public GraphicsComposerHidlTest {
400  protected:
SetUp()401   void SetUp() override {
402     ASSERT_NO_FATAL_FAILURE(GraphicsComposerHidlTest::SetUp());
403 
404     ASSERT_NO_FATAL_FAILURE(mGralloc = std::make_unique<Gralloc>());
405 
406     mWriter = std::make_unique<CommandWriterBase>(1024);
407     mReader = std::make_unique<CommandReader>();
408   }
409 
TearDown()410   void TearDown() override {
411     ASSERT_NO_FATAL_FAILURE(GraphicsComposerHidlTest::TearDown());
412   }
413 
allocate()414   const native_handle_t* allocate() {
415       IMapper::BufferDescriptorInfo info{};
416       info.width = 64;
417       info.height = 64;
418       info.layerCount = 1;
419       info.format = PixelFormat::RGBA_8888;
420       info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN |
421                                          BufferUsage::CPU_READ_OFTEN);
422 
423       return mGralloc->allocate(info);
424   }
425 
execute()426   void execute() {
427     bool queueChanged = false;
428     uint32_t commandLength = 0;
429     hidl_vec<hidl_handle> commandHandles;
430     ASSERT_TRUE(
431         mWriter->writeQueue(&queueChanged, &commandLength, &commandHandles));
432 
433     if (queueChanged) {
434       auto ret = mComposerClient->getRaw()->setInputCommandQueue(
435           *mWriter->getMQDescriptor());
436       ASSERT_EQ(Error::NONE, static_cast<Error>(ret));
437       return;
438     }
439 
440     mComposerClient->getRaw()->executeCommands(
441         commandLength, commandHandles,
442         [&](const auto& tmpError, const auto& tmpOutQueueChanged,
443             const auto& tmpOutLength, const auto& tmpOutHandles) {
444           ASSERT_EQ(Error::NONE, tmpError);
445 
446           if (tmpOutQueueChanged) {
447             mComposerClient->getRaw()->getOutputCommandQueue(
448                 [&](const auto& tmpError, const auto& tmpDescriptor) {
449                   ASSERT_EQ(Error::NONE, tmpError);
450                   mReader->setMQDescriptor(tmpDescriptor);
451                 });
452           }
453 
454           ASSERT_TRUE(mReader->readQueue(tmpOutLength, tmpOutHandles));
455           mReader->parse();
456         });
457   }
458 
459   // A command parser that checks that no error nor unexpected commands are
460   // returned.
461   class CommandReader : public CommandReaderBase {
462    public:
463     // Parse all commands in the return command queue.  Call GTEST_FAIL() for
464     // unexpected errors or commands.
parse()465     void parse() {
466       while (!isEmpty()) {
467         IComposerClient::Command command;
468         uint16_t length;
469         ASSERT_TRUE(beginCommand(&command, &length));
470 
471         switch (command) {
472           case IComposerClient::Command::SET_ERROR: {
473             ASSERT_EQ(2, length);
474             auto loc = read();
475             auto err = readSigned();
476             GTEST_FAIL() << "unexpected error " << err << " at location "
477                          << loc;
478           } break;
479           case IComposerClient::Command::SELECT_DISPLAY:
480           case IComposerClient::Command::SET_CHANGED_COMPOSITION_TYPES:
481           case IComposerClient::Command::SET_DISPLAY_REQUESTS:
482           case IComposerClient::Command::SET_PRESENT_FENCE:
483           case IComposerClient::Command::SET_RELEASE_FENCES:
484             break;
485           default:
486             GTEST_FAIL() << "unexpected return command " << std::hex
487                          << static_cast<int>(command);
488             break;
489         }
490 
491         endCommand();
492       }
493     }
494   };
495 
496   std::unique_ptr<CommandWriterBase> mWriter;
497   std::unique_ptr<CommandReader> mReader;
498 
499  private:
500   std::unique_ptr<Gralloc> mGralloc;
501 };
502 
503 /**
504  * Test IComposerClient::Command::SET_COLOR_TRANSFORM.
505  */
TEST_F(GraphicsComposerHidlCommandTest,SET_COLOR_TRANSFORM)506 TEST_F(GraphicsComposerHidlCommandTest, SET_COLOR_TRANSFORM) {
507   const std::array<float, 16> identity = {{
508       1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
509       0.0f, 0.0f, 0.0f, 1.0f,
510   }};
511 
512   mWriter->selectDisplay(mPrimaryDisplay);
513   mWriter->setColorTransform(identity.data(), ColorTransform::IDENTITY);
514 
515   execute();
516 }
517 
518 /**
519  * Test IComposerClient::Command::SET_CLIENT_TARGET.
520  */
TEST_F(GraphicsComposerHidlCommandTest,SET_CLIENT_TARGET)521 TEST_F(GraphicsComposerHidlCommandTest, SET_CLIENT_TARGET) {
522   mComposerClient->setClientTargetSlotCount(mPrimaryDisplay, kBufferSlotCount);
523 
524   mWriter->selectDisplay(mPrimaryDisplay);
525   mWriter->setClientTarget(0, nullptr, -1, Dataspace::UNKNOWN,
526                            std::vector<IComposerClient::Rect>());
527 
528   execute();
529 }
530 
531 /**
532  * Test IComposerClient::Command::SET_OUTPUT_BUFFER.
533  */
TEST_F(GraphicsComposerHidlCommandTest,SET_OUTPUT_BUFFER)534 TEST_F(GraphicsComposerHidlCommandTest, SET_OUTPUT_BUFFER) {
535   if (mComposerClient->getMaxVirtualDisplayCount() == 0) {
536     GTEST_SUCCEED() << "no virtual display support";
537     return;
538   }
539 
540   Display display;
541   PixelFormat format;
542   ASSERT_NO_FATAL_FAILURE(display = mComposerClient->createVirtualDisplay(
543                               64, 64, PixelFormat::IMPLEMENTATION_DEFINED,
544                               kBufferSlotCount, &format));
545 
546   const native_handle_t* handle;
547   ASSERT_NO_FATAL_FAILURE(handle = allocate());
548 
549   mWriter->selectDisplay(display);
550   mWriter->setOutputBuffer(0, handle, -1);
551   execute();
552 }
553 
554 /**
555  * Test IComposerClient::Command::VALIDATE_DISPLAY.
556  */
TEST_F(GraphicsComposerHidlCommandTest,VALIDATE_DISPLAY)557 TEST_F(GraphicsComposerHidlCommandTest, VALIDATE_DISPLAY) {
558   mWriter->selectDisplay(mPrimaryDisplay);
559   mWriter->validateDisplay();
560   execute();
561 }
562 
563 /**
564  * Test IComposerClient::Command::ACCEPT_DISPLAY_CHANGES.
565  */
TEST_F(GraphicsComposerHidlCommandTest,ACCEPT_DISPLAY_CHANGES)566 TEST_F(GraphicsComposerHidlCommandTest, ACCEPT_DISPLAY_CHANGES) {
567   mWriter->selectDisplay(mPrimaryDisplay);
568   mWriter->validateDisplay();
569   mWriter->acceptDisplayChanges();
570   execute();
571 }
572 
573 /**
574  * Test IComposerClient::Command::PRESENT_DISPLAY.
575  */
TEST_F(GraphicsComposerHidlCommandTest,PRESENT_DISPLAY)576 TEST_F(GraphicsComposerHidlCommandTest, PRESENT_DISPLAY) {
577   mWriter->selectDisplay(mPrimaryDisplay);
578   mWriter->validateDisplay();
579   mWriter->presentDisplay();
580   execute();
581 }
582 
583 /**
584  * Test IComposerClient::Command::SET_LAYER_CURSOR_POSITION.
585  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_CURSOR_POSITION)586 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_CURSOR_POSITION) {
587   Layer layer;
588   ASSERT_NO_FATAL_FAILURE(
589       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
590 
591   mWriter->selectDisplay(mPrimaryDisplay);
592   mWriter->selectLayer(layer);
593   mWriter->setLayerCursorPosition(1, 1);
594   mWriter->setLayerCursorPosition(0, 0);
595   execute();
596 }
597 
598 /**
599  * Test IComposerClient::Command::SET_LAYER_BUFFER.
600  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_BUFFER)601 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_BUFFER) {
602   auto handle = allocate();
603   ASSERT_NE(nullptr, handle);
604 
605   Layer layer;
606   ASSERT_NO_FATAL_FAILURE(
607       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
608 
609   mWriter->selectDisplay(mPrimaryDisplay);
610   mWriter->selectLayer(layer);
611   mWriter->setLayerBuffer(0, handle, -1);
612   execute();
613 }
614 
615 /**
616  * Test IComposerClient::Command::SET_LAYER_SURFACE_DAMAGE.
617  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_SURFACE_DAMAGE)618 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_SURFACE_DAMAGE) {
619   Layer layer;
620   ASSERT_NO_FATAL_FAILURE(
621       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
622 
623   IComposerClient::Rect empty{0, 0, 0, 0};
624   IComposerClient::Rect unit{0, 0, 1, 1};
625 
626   mWriter->selectDisplay(mPrimaryDisplay);
627   mWriter->selectLayer(layer);
628   mWriter->setLayerSurfaceDamage(std::vector<IComposerClient::Rect>(1, empty));
629   mWriter->setLayerSurfaceDamage(std::vector<IComposerClient::Rect>(1, unit));
630   mWriter->setLayerSurfaceDamage(std::vector<IComposerClient::Rect>());
631   execute();
632 }
633 
634 /**
635  * Test IComposerClient::Command::SET_LAYER_BLEND_MODE.
636  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_BLEND_MODE)637 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_BLEND_MODE) {
638   Layer layer;
639   ASSERT_NO_FATAL_FAILURE(
640       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
641 
642   mWriter->selectDisplay(mPrimaryDisplay);
643   mWriter->selectLayer(layer);
644   mWriter->setLayerBlendMode(IComposerClient::BlendMode::NONE);
645   mWriter->setLayerBlendMode(IComposerClient::BlendMode::PREMULTIPLIED);
646   mWriter->setLayerBlendMode(IComposerClient::BlendMode::COVERAGE);
647   execute();
648 }
649 
650 /**
651  * Test IComposerClient::Command::SET_LAYER_COLOR.
652  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_COLOR)653 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_COLOR) {
654   Layer layer;
655   ASSERT_NO_FATAL_FAILURE(
656       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
657 
658   mWriter->selectDisplay(mPrimaryDisplay);
659   mWriter->selectLayer(layer);
660   mWriter->setLayerColor(IComposerClient::Color{0xff, 0xff, 0xff, 0xff});
661   mWriter->setLayerColor(IComposerClient::Color{0, 0, 0, 0});
662   execute();
663 }
664 
665 /**
666  * Test IComposerClient::Command::SET_LAYER_COMPOSITION_TYPE.
667  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_COMPOSITION_TYPE)668 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_COMPOSITION_TYPE) {
669   Layer layer;
670   ASSERT_NO_FATAL_FAILURE(
671       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
672 
673   mWriter->selectDisplay(mPrimaryDisplay);
674   mWriter->selectLayer(layer);
675   mWriter->setLayerCompositionType(IComposerClient::Composition::CLIENT);
676   mWriter->setLayerCompositionType(IComposerClient::Composition::DEVICE);
677   mWriter->setLayerCompositionType(IComposerClient::Composition::SOLID_COLOR);
678   mWriter->setLayerCompositionType(IComposerClient::Composition::CURSOR);
679   execute();
680 }
681 
682 /**
683  * Test IComposerClient::Command::SET_LAYER_DATASPACE.
684  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_DATASPACE)685 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_DATASPACE) {
686   Layer layer;
687   ASSERT_NO_FATAL_FAILURE(
688       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
689 
690   mWriter->selectDisplay(mPrimaryDisplay);
691   mWriter->selectLayer(layer);
692   mWriter->setLayerDataspace(Dataspace::UNKNOWN);
693   execute();
694 }
695 
696 /**
697  * Test IComposerClient::Command::SET_LAYER_DISPLAY_FRAME.
698  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_DISPLAY_FRAME)699 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_DISPLAY_FRAME) {
700   Layer layer;
701   ASSERT_NO_FATAL_FAILURE(
702       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
703 
704   mWriter->selectDisplay(mPrimaryDisplay);
705   mWriter->selectLayer(layer);
706   mWriter->setLayerDisplayFrame(IComposerClient::Rect{0, 0, 1, 1});
707   execute();
708 }
709 
710 /**
711  * Test IComposerClient::Command::SET_LAYER_PLANE_ALPHA.
712  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_PLANE_ALPHA)713 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_PLANE_ALPHA) {
714   Layer layer;
715   ASSERT_NO_FATAL_FAILURE(
716       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
717 
718   mWriter->selectDisplay(mPrimaryDisplay);
719   mWriter->selectLayer(layer);
720   mWriter->setLayerPlaneAlpha(0.0f);
721   mWriter->setLayerPlaneAlpha(1.0f);
722   execute();
723 }
724 
725 /**
726  * Test IComposerClient::Command::SET_LAYER_SIDEBAND_STREAM.
727  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_SIDEBAND_STREAM)728 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_SIDEBAND_STREAM) {
729   if (!mComposer->hasCapability(IComposer::Capability::SIDEBAND_STREAM)) {
730     GTEST_SUCCEED() << "no sideband stream support";
731     return;
732   }
733 
734   auto handle = allocate();
735   ASSERT_NE(nullptr, handle);
736 
737   Layer layer;
738   ASSERT_NO_FATAL_FAILURE(
739       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
740 
741   mWriter->selectDisplay(mPrimaryDisplay);
742   mWriter->selectLayer(layer);
743   mWriter->setLayerSidebandStream(handle);
744   execute();
745 }
746 
747 /**
748  * Test IComposerClient::Command::SET_LAYER_SOURCE_CROP.
749  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_SOURCE_CROP)750 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_SOURCE_CROP) {
751   Layer layer;
752   ASSERT_NO_FATAL_FAILURE(
753       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
754 
755   mWriter->selectDisplay(mPrimaryDisplay);
756   mWriter->selectLayer(layer);
757   mWriter->setLayerSourceCrop(IComposerClient::FRect{0.0f, 0.0f, 1.0f, 1.0f});
758   execute();
759 }
760 
761 /**
762  * Test IComposerClient::Command::SET_LAYER_TRANSFORM.
763  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_TRANSFORM)764 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_TRANSFORM) {
765   Layer layer;
766   ASSERT_NO_FATAL_FAILURE(
767       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
768 
769   mWriter->selectDisplay(mPrimaryDisplay);
770   mWriter->selectLayer(layer);
771   mWriter->setLayerTransform(static_cast<Transform>(0));
772   mWriter->setLayerTransform(Transform::FLIP_H);
773   mWriter->setLayerTransform(Transform::FLIP_V);
774   mWriter->setLayerTransform(Transform::ROT_90);
775   mWriter->setLayerTransform(Transform::ROT_180);
776   mWriter->setLayerTransform(Transform::ROT_270);
777   mWriter->setLayerTransform(
778       static_cast<Transform>(Transform::FLIP_H | Transform::ROT_90));
779   mWriter->setLayerTransform(
780       static_cast<Transform>(Transform::FLIP_V | Transform::ROT_90));
781   execute();
782 }
783 
784 /**
785  * Test IComposerClient::Command::SET_LAYER_VISIBLE_REGION.
786  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_VISIBLE_REGION)787 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_VISIBLE_REGION) {
788   Layer layer;
789   ASSERT_NO_FATAL_FAILURE(
790       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
791 
792   IComposerClient::Rect empty{0, 0, 0, 0};
793   IComposerClient::Rect unit{0, 0, 1, 1};
794 
795   mWriter->selectDisplay(mPrimaryDisplay);
796   mWriter->selectLayer(layer);
797   mWriter->setLayerVisibleRegion(std::vector<IComposerClient::Rect>(1, empty));
798   mWriter->setLayerVisibleRegion(std::vector<IComposerClient::Rect>(1, unit));
799   mWriter->setLayerVisibleRegion(std::vector<IComposerClient::Rect>());
800   execute();
801 }
802 
803 /**
804  * Test IComposerClient::Command::SET_LAYER_Z_ORDER.
805  */
TEST_F(GraphicsComposerHidlCommandTest,SET_LAYER_Z_ORDER)806 TEST_F(GraphicsComposerHidlCommandTest, SET_LAYER_Z_ORDER) {
807   Layer layer;
808   ASSERT_NO_FATAL_FAILURE(
809       layer = mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));
810 
811   mWriter->selectDisplay(mPrimaryDisplay);
812   mWriter->selectLayer(layer);
813   mWriter->setLayerZOrder(10);
814   mWriter->setLayerZOrder(0);
815   execute();
816 }
817 
818 }  // namespace anonymous
819 }  // namespace tests
820 }  // namespace V2_1
821 }  // namespace composer
822 }  // namespace graphics
823 }  // namespace hardware
824 }  // namespace android
825 
main(int argc,char ** argv)826 int main(int argc, char** argv) {
827   ::testing::InitGoogleTest(&argc, argv);
828 
829   int status = RUN_ALL_TESTS();
830   LOG(INFO) << "Test result = " << status;
831 
832   return status;
833 }
834