• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #include <cmath>
18 
19 #include <compositionengine/DisplayColorProfileCreationArgs.h>
20 #include <compositionengine/DisplayCreationArgs.h>
21 #include <compositionengine/DisplaySurface.h>
22 #include <compositionengine/RenderSurfaceCreationArgs.h>
23 #include <compositionengine/impl/Display.h>
24 #include <compositionengine/impl/RenderSurface.h>
25 #include <compositionengine/mock/CompositionEngine.h>
26 #include <compositionengine/mock/DisplayColorProfile.h>
27 #include <compositionengine/mock/DisplaySurface.h>
28 #include <compositionengine/mock/LayerFE.h>
29 #include <compositionengine/mock/NativeWindow.h>
30 #include <compositionengine/mock/OutputLayer.h>
31 #include <compositionengine/mock/RenderSurface.h>
32 #include <gtest/gtest.h>
33 #include <renderengine/mock/FakeExternalTexture.h>
34 #include <renderengine/mock/RenderEngine.h>
35 
36 #include <ui/Rect.h>
37 #include <ui/StaticDisplayInfo.h>
38 
39 #include "ftl/future.h"
40 #include "mock/DisplayHardware/MockHWC2.h"
41 #include "mock/DisplayHardware/MockHWComposer.h"
42 #include "mock/PowerAdvisor/MockPowerAdvisor.h"
43 
44 #include <aidl/android/hardware/graphics/composer3/Composition.h>
45 
46 using aidl::android::hardware::graphics::composer3::Capability;
47 using aidl::android::hardware::graphics::composer3::Composition;
48 using aidl::android::hardware::graphics::composer3::DimmingStage;
49 
50 namespace android::compositionengine {
51 namespace {
52 
53 namespace hal = android::hardware::graphics::composer::hal;
54 
55 using testing::_;
56 using testing::ByMove;
57 using testing::DoAll;
58 using testing::Eq;
59 using testing::InSequence;
60 using testing::NiceMock;
61 using testing::Pointee;
62 using testing::Ref;
63 using testing::Return;
64 using testing::ReturnRef;
65 using testing::Sequence;
66 using testing::SetArgPointee;
67 using testing::StrictMock;
68 
69 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(123u);
70 constexpr HalVirtualDisplayId HAL_VIRTUAL_DISPLAY_ID{456u};
71 constexpr GpuVirtualDisplayId GPU_VIRTUAL_DISPLAY_ID{789u};
72 
73 constexpr ui::Size DEFAULT_RESOLUTION{1920, 1080};
74 
75 struct Layer {
Layerandroid::compositionengine::__anon153eb9cf0111::Layer76     Layer() {
77         EXPECT_CALL(*outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE));
78         EXPECT_CALL(*outputLayer, getHwcLayer()).WillRepeatedly(Return(&hwc2Layer));
79     }
80 
81     sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
82     StrictMock<mock::OutputLayer>* outputLayer = new StrictMock<mock::OutputLayer>();
83     StrictMock<HWC2::mock::Layer> hwc2Layer;
84 };
85 
86 struct LayerNoHWC2Layer {
LayerNoHWC2Layerandroid::compositionengine::__anon153eb9cf0111::LayerNoHWC2Layer87     LayerNoHWC2Layer() {
88         EXPECT_CALL(*outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE));
89         EXPECT_CALL(*outputLayer, getHwcLayer()).WillRepeatedly(Return(nullptr));
90     }
91 
92     sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
93     StrictMock<mock::OutputLayer>* outputLayer = new StrictMock<mock::OutputLayer>();
94 };
95 
96 struct DisplayTestCommon : public testing::Test {
97     // Uses the full implementation of a display
98     class FullImplDisplay : public impl::Display {
99     public:
100         using impl::Display::injectOutputLayerForTest;
101         virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
102     };
103 
104     // Uses a special implementation with key internal member functions set up
105     // as mock implementations, to allow for easier testing.
106     struct PartialMockDisplay : public impl::Display {
PartialMockDisplayandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon::PartialMockDisplay107         PartialMockDisplay(const compositionengine::CompositionEngine& compositionEngine)
108               : mCompositionEngine(compositionEngine) {}
109 
110         // compositionengine::Output overrides
getStateandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon::PartialMockDisplay111         const OutputCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon::PartialMockDisplay112         OutputCompositionState& editState() override { return mState; }
113 
114         // compositionengine::impl::Output overrides
getCompositionEngineandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon::PartialMockDisplay115         const CompositionEngine& getCompositionEngine() const override {
116             return mCompositionEngine;
117         };
118 
getOutputLayerCountandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon::PartialMockDisplay119         size_t getOutputLayerCount() const override { return 1u; }
120 
121         // Mock implementation overrides
122         MOCK_CONST_METHOD1(getOutputLayerOrderedByZByIndex,
123                            compositionengine::OutputLayer*(size_t));
124         MOCK_METHOD2(ensureOutputLayer,
125                      compositionengine::OutputLayer*(std::optional<size_t>, const sp<LayerFE>&));
126         MOCK_METHOD0(finalizePendingOutputLayers, void());
127         MOCK_METHOD0(clearOutputLayers, void());
128         MOCK_CONST_METHOD1(dumpState, void(std::string&));
129         MOCK_METHOD1(injectOutputLayerForTest, compositionengine::OutputLayer*(const sp<LayerFE>&));
130         MOCK_METHOD1(injectOutputLayerForTest, void(std::unique_ptr<OutputLayer>));
131         MOCK_CONST_METHOD0(anyLayersRequireClientComposition, bool());
132         MOCK_CONST_METHOD0(allLayersRequireClientComposition, bool());
133         MOCK_METHOD1(applyChangedTypesToLayers, void(const impl::Display::ChangedTypes&));
134         MOCK_METHOD1(applyDisplayRequests, void(const impl::Display::DisplayRequests&));
135         MOCK_METHOD1(applyLayerRequestsToLayers, void(const impl::Display::LayerRequests&));
136         MOCK_METHOD1(applyLayerLutsToLayers, void(const impl::Display::LayerLuts&));
137 
138         const compositionengine::CompositionEngine& mCompositionEngine;
139         impl::OutputCompositionState mState;
140     };
141 
getDisplayNameFromCurrentTestandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon142     static std::string getDisplayNameFromCurrentTest() {
143         const ::testing::TestInfo* const test_info =
144                 ::testing::UnitTest::GetInstance()->current_test_info();
145         return std::string("display for ") + test_info->test_case_name() + "." + test_info->name();
146     }
147 
148     template <typename Display>
createDisplayandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon149     static std::shared_ptr<Display> createDisplay(
150             const compositionengine::CompositionEngine& compositionEngine,
151             compositionengine::DisplayCreationArgs args) {
152         args.name = getDisplayNameFromCurrentTest();
153         return impl::createDisplayTemplated<Display>(compositionEngine, args);
154     }
155 
156     template <typename Display>
createPartialMockDisplayandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon157     static std::shared_ptr<StrictMock<Display>> createPartialMockDisplay(
158             const compositionengine::CompositionEngine& compositionEngine,
159             compositionengine::DisplayCreationArgs args) {
160         args.name = getDisplayNameFromCurrentTest();
161         auto display = std::make_shared<StrictMock<Display>>(compositionEngine);
162 
163         display->setConfiguration(args);
164 
165         return display;
166     }
167 
DisplayTestCommonandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon168     DisplayTestCommon() {
169         EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
170         EXPECT_CALL(mCompositionEngine, getRenderEngine()).WillRepeatedly(ReturnRef(mRenderEngine));
171         EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
172         EXPECT_CALL(mRenderEngine, isProtected()).WillRepeatedly(Return(false));
173         EXPECT_CALL(mPowerAdvisor, usePowerHintSession()).WillRepeatedly(Return(false));
174     }
175 
getDisplayCreationArgsForPhysicalDisplayandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon176     DisplayCreationArgs getDisplayCreationArgsForPhysicalDisplay() {
177         return DisplayCreationArgsBuilder()
178                 .setId(DEFAULT_DISPLAY_ID)
179                 .setPixels(DEFAULT_RESOLUTION)
180                 .setIsSecure(true)
181                 .setPowerAdvisor(&mPowerAdvisor)
182                 .build();
183     }
184 
getDisplayCreationArgsForGpuVirtualDisplayandroid::compositionengine::__anon153eb9cf0111::DisplayTestCommon185     DisplayCreationArgs getDisplayCreationArgsForGpuVirtualDisplay() {
186         return DisplayCreationArgsBuilder()
187                 .setId(GPU_VIRTUAL_DISPLAY_ID)
188                 .setPixels(DEFAULT_RESOLUTION)
189                 .setIsSecure(false)
190                 .setPowerAdvisor(&mPowerAdvisor)
191                 .build();
192     }
193 
194     StrictMock<android::mock::HWComposer> mHwComposer;
195     StrictMock<adpf::mock::PowerAdvisor> mPowerAdvisor;
196     StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
197     StrictMock<mock::CompositionEngine> mCompositionEngine;
198     sp<mock::NativeWindow> mNativeWindow = sp<StrictMock<mock::NativeWindow>>::make();
199 };
200 
201 struct PartialMockDisplayTestCommon : public DisplayTestCommon {
202     using Display = DisplayTestCommon::PartialMockDisplay;
203     std::shared_ptr<Display> mDisplay =
204             createPartialMockDisplay<Display>(mCompositionEngine,
205                                               getDisplayCreationArgsForPhysicalDisplay());
206 
207     android::HWComposer::DeviceRequestedChanges mDeviceRequestedChanges{
208             {{nullptr, Composition::CLIENT}},
209             hal::DisplayRequest::FLIP_CLIENT_TARGET,
210             {{nullptr, hal::LayerRequest::CLEAR_CLIENT_TARGET}},
211             {DEFAULT_DISPLAY_ID.value,
212              {aidl::android::hardware::graphics::common::PixelFormat::RGBA_8888,
213               aidl::android::hardware::graphics::common::Dataspace::UNKNOWN},
214              -1.f,
215              DimmingStage::NONE},
216             {},
217     };
218 
chooseCompositionStrategyandroid::compositionengine::__anon153eb9cf0111::PartialMockDisplayTestCommon219     void chooseCompositionStrategy(Display* display) {
220         std::optional<android::HWComposer::DeviceRequestedChanges> changes;
221         bool success = display->chooseCompositionStrategy(&changes);
222         display->resetCompositionStrategy();
223         if (success) {
224             display->applyCompositionStrategy(changes);
225         }
226     }
227 };
228 
229 struct FullDisplayImplTestCommon : public DisplayTestCommon {
230     using Display = DisplayTestCommon::FullImplDisplay;
231     std::shared_ptr<Display> mDisplay =
232             createDisplay<Display>(mCompositionEngine, getDisplayCreationArgsForPhysicalDisplay());
233 };
234 
235 struct DisplayWithLayersTestCommon : public FullDisplayImplTestCommon {
DisplayWithLayersTestCommonandroid::compositionengine::__anon153eb9cf0111::DisplayWithLayersTestCommon236     DisplayWithLayersTestCommon() {
237         mDisplay->injectOutputLayerForTest(
238                 std::unique_ptr<compositionengine::OutputLayer>(mLayer1.outputLayer));
239         mDisplay->injectOutputLayerForTest(
240                 std::unique_ptr<compositionengine::OutputLayer>(mLayer2.outputLayer));
241         mDisplay->injectOutputLayerForTest(
242                 std::unique_ptr<compositionengine::OutputLayer>(mLayer3.outputLayer));
243         mResultWithBuffer.buffer = std::make_shared<
244                 renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/,
245                                                          1ULL /* bufferId */,
246                                                          HAL_PIXEL_FORMAT_RGBA_8888,
247                                                          0ULL /*usage*/);
248     }
249 
250     Layer mLayer1;
251     Layer mLayer2;
252     LayerNoHWC2Layer mLayer3;
253     StrictMock<HWC2::mock::Layer> hwc2LayerUnknown;
254     std::shared_ptr<Display> mDisplay =
255             createDisplay<Display>(mCompositionEngine, getDisplayCreationArgsForPhysicalDisplay());
256     impl::GpuCompositionResult mResultWithBuffer;
257     impl::GpuCompositionResult mResultWithoutBuffer;
258 };
259 
260 /*
261  * Basic construction
262  */
263 
264 struct DisplayCreationTest : public DisplayTestCommon {
265     using Display = DisplayTestCommon::FullImplDisplay;
266 };
267 
TEST_F(DisplayCreationTest,createPhysicalInternalDisplay)268 TEST_F(DisplayCreationTest, createPhysicalInternalDisplay) {
269     auto display =
270             impl::createDisplay(mCompositionEngine, getDisplayCreationArgsForPhysicalDisplay());
271     EXPECT_TRUE(display->isSecure());
272     EXPECT_FALSE(display->isVirtual());
273     EXPECT_EQ(DEFAULT_DISPLAY_ID, display->getId());
274 }
275 
TEST_F(DisplayCreationTest,createGpuVirtualDisplay)276 TEST_F(DisplayCreationTest, createGpuVirtualDisplay) {
277     auto display =
278             impl::createDisplay(mCompositionEngine, getDisplayCreationArgsForGpuVirtualDisplay());
279     EXPECT_FALSE(display->isSecure());
280     EXPECT_TRUE(display->isVirtual());
281     EXPECT_TRUE(display->getDisplayIdVariant().and_then(asDisplayIdOfType<GpuVirtualDisplayId>));
282 }
283 
284 /*
285  * Display::setConfiguration()
286  */
287 
288 using DisplaySetConfigurationTest = PartialMockDisplayTestCommon;
289 
TEST_F(DisplaySetConfigurationTest,configuresPhysicalDisplay)290 TEST_F(DisplaySetConfigurationTest, configuresPhysicalDisplay) {
291     mDisplay->setConfiguration(DisplayCreationArgsBuilder()
292                                        .setId(DEFAULT_DISPLAY_ID)
293                                        .setPixels(DEFAULT_RESOLUTION)
294                                        .setIsSecure(true)
295                                        .setPowerAdvisor(&mPowerAdvisor)
296                                        .setName(getDisplayNameFromCurrentTest())
297                                        .build());
298 
299     EXPECT_EQ(DEFAULT_DISPLAY_ID, mDisplay->getId());
300     EXPECT_TRUE(mDisplay->isSecure());
301     EXPECT_FALSE(mDisplay->isVirtual());
302     EXPECT_FALSE(mDisplay->isValid());
303 
304     const auto& filter = mDisplay->getState().layerFilter;
305     EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
306     EXPECT_FALSE(filter.toInternalDisplay);
307 }
308 
TEST_F(DisplaySetConfigurationTest,configuresHalVirtualDisplay)309 TEST_F(DisplaySetConfigurationTest, configuresHalVirtualDisplay) {
310     mDisplay->setConfiguration(DisplayCreationArgsBuilder()
311                                        .setId(HAL_VIRTUAL_DISPLAY_ID)
312                                        .setPixels(DEFAULT_RESOLUTION)
313                                        .setIsSecure(false)
314                                        .setPowerAdvisor(&mPowerAdvisor)
315                                        .setName(getDisplayNameFromCurrentTest())
316                                        .build());
317 
318     EXPECT_EQ(HAL_VIRTUAL_DISPLAY_ID, mDisplay->getId());
319     EXPECT_FALSE(mDisplay->isSecure());
320     EXPECT_TRUE(mDisplay->isVirtual());
321     EXPECT_TRUE(mDisplay->getDisplayIdVariant().and_then(asDisplayIdOfType<HalVirtualDisplayId>));
322     EXPECT_FALSE(mDisplay->isValid());
323 
324     const auto& filter = mDisplay->getState().layerFilter;
325     EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
326     EXPECT_FALSE(filter.toInternalDisplay);
327 }
328 
TEST_F(DisplaySetConfigurationTest,configuresGpuVirtualDisplay)329 TEST_F(DisplaySetConfigurationTest, configuresGpuVirtualDisplay) {
330     mDisplay->setConfiguration(DisplayCreationArgsBuilder()
331                                        .setId(GPU_VIRTUAL_DISPLAY_ID)
332                                        .setPixels(DEFAULT_RESOLUTION)
333                                        .setIsSecure(false)
334                                        .setPowerAdvisor(&mPowerAdvisor)
335                                        .setName(getDisplayNameFromCurrentTest())
336                                        .build());
337 
338     EXPECT_EQ(GPU_VIRTUAL_DISPLAY_ID, mDisplay->getId());
339     EXPECT_FALSE(mDisplay->isSecure());
340     EXPECT_TRUE(mDisplay->isVirtual());
341     EXPECT_TRUE(mDisplay->getDisplayIdVariant().and_then(asDisplayIdOfType<GpuVirtualDisplayId>));
342     EXPECT_FALSE(mDisplay->isValid());
343 
344     const auto& filter = mDisplay->getState().layerFilter;
345     EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
346     EXPECT_FALSE(filter.toInternalDisplay);
347 }
348 
349 /*
350  * Display::disconnect()
351  */
352 
353 using DisplayDisconnectTest = PartialMockDisplayTestCommon;
354 
TEST_F(DisplayDisconnectTest,disconnectsDisplay)355 TEST_F(DisplayDisconnectTest, disconnectsDisplay) {
356     // The first call to disconnect will disconnect the display with the HWC.
357     EXPECT_CALL(mHwComposer, disconnectDisplay(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(1);
358     mDisplay->disconnect();
359 
360     // Subsequent calls will do nothing,
361     EXPECT_CALL(mHwComposer, disconnectDisplay(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(0);
362     mDisplay->disconnect();
363 }
364 
365 /*
366  * Display::setColorTransform()
367  */
368 
369 using DisplaySetColorTransformTest = PartialMockDisplayTestCommon;
370 
TEST_F(DisplaySetColorTransformTest,setsTransform)371 TEST_F(DisplaySetColorTransformTest, setsTransform) {
372     // No change does nothing
373     CompositionRefreshArgs refreshArgs;
374     refreshArgs.colorTransformMatrix = std::nullopt;
375     mDisplay->setColorTransform(refreshArgs);
376 
377     // Identity matrix sets an identity state value
378     const mat4 kIdentity;
379 
380     EXPECT_CALL(mHwComposer, setColorTransform(HalDisplayId(DEFAULT_DISPLAY_ID), kIdentity))
381             .Times(1);
382 
383     refreshArgs.colorTransformMatrix = kIdentity;
384     mDisplay->setColorTransform(refreshArgs);
385 
386     // Non-identity matrix sets a non-identity state value
387     const mat4 kNonIdentity = mat4() * 2;
388 
389     EXPECT_CALL(mHwComposer, setColorTransform(HalDisplayId(DEFAULT_DISPLAY_ID), kNonIdentity))
390             .Times(1);
391 
392     refreshArgs.colorTransformMatrix = kNonIdentity;
393     mDisplay->setColorTransform(refreshArgs);
394 }
395 
396 /*
397  * Display::setColorMode()
398  */
399 
400 using DisplaySetColorModeTest = PartialMockDisplayTestCommon;
401 
TEST_F(DisplaySetColorModeTest,setsModeUnlessNoChange)402 TEST_F(DisplaySetColorModeTest, setsModeUnlessNoChange) {
403     using ColorProfile = Output::ColorProfile;
404 
405     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
406     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
407     mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
408     mDisplay->setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile));
409 
410     // These values are expected to be the initial state.
411     ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
412     ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
413     ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
414 
415     // Otherwise if the values are unchanged, nothing happens
416     mDisplay->setColorProfile(ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
417                                            ui::RenderIntent::COLORIMETRIC});
418 
419     EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
420     EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
421     EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
422 
423     // Otherwise if the values are different, updates happen
424     EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
425     EXPECT_CALL(mHwComposer,
426                 setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
427                                    ui::RenderIntent::TONE_MAP_COLORIMETRIC))
428             .Times(1);
429 
430     mDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
431                                            ui::RenderIntent::TONE_MAP_COLORIMETRIC});
432 
433     EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay->getState().colorMode);
434     EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay->getState().dataspace);
435     EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay->getState().renderIntent);
436 }
437 
TEST_F(DisplaySetColorModeTest,doesNothingForVirtualDisplay)438 TEST_F(DisplaySetColorModeTest, doesNothingForVirtualDisplay) {
439     using ColorProfile = Output::ColorProfile;
440 
441     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
442     std::shared_ptr<impl::Display> virtualDisplay = impl::createDisplay(mCompositionEngine, args);
443 
444     mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
445     virtualDisplay->setDisplayColorProfileForTest(
446             std::unique_ptr<DisplayColorProfile>(colorProfile));
447 
448     virtualDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3,
449                                                  ui::Dataspace::DISPLAY_P3,
450                                                  ui::RenderIntent::TONE_MAP_COLORIMETRIC});
451 
452     EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay->getState().colorMode);
453     EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().dataspace);
454     EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay->getState().renderIntent);
455 }
456 
457 /*
458  * Display::createDisplayColorProfile()
459  */
460 
461 using DisplayCreateColorProfileTest = PartialMockDisplayTestCommon;
462 
TEST_F(DisplayCreateColorProfileTest,setsDisplayColorProfile)463 TEST_F(DisplayCreateColorProfileTest, setsDisplayColorProfile) {
464     EXPECT_TRUE(mDisplay->getDisplayColorProfile() == nullptr);
465     mDisplay->createDisplayColorProfile(
466             DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
467                                             DisplayColorProfileCreationArgs::HwcColorModes()});
468     EXPECT_TRUE(mDisplay->getDisplayColorProfile() != nullptr);
469 }
470 
471 /*
472  * Display::createRenderSurface()
473  */
474 
475 using DisplayCreateRenderSurfaceTest = PartialMockDisplayTestCommon;
476 
TEST_F(DisplayCreateRenderSurfaceTest,setsRenderSurface)477 TEST_F(DisplayCreateRenderSurfaceTest, setsRenderSurface) {
478     EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
479     EXPECT_TRUE(mDisplay->getRenderSurface() == nullptr);
480     mDisplay->createRenderSurface(RenderSurfaceCreationArgsBuilder()
481                                           .setDisplayWidth(640)
482                                           .setDisplayHeight(480)
483                                           .setNativeWindow(mNativeWindow)
484                                           .build());
485     EXPECT_TRUE(mDisplay->getRenderSurface() != nullptr);
486 }
487 
488 /*
489  * Display::createOutputLayer()
490  */
491 
492 using DisplayCreateOutputLayerTest = FullDisplayImplTestCommon;
493 
TEST_F(DisplayCreateOutputLayerTest,setsHwcLayer)494 TEST_F(DisplayCreateOutputLayerTest, setsHwcLayer) {
495     sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
496     auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
497 
498     EXPECT_CALL(mHwComposer, createLayer(HalDisplayId(DEFAULT_DISPLAY_ID)))
499             .WillOnce(Return(hwcLayer));
500 
501     auto outputLayer = mDisplay->createOutputLayer(layerFE);
502 
503     EXPECT_EQ(hwcLayer.get(), outputLayer->getHwcLayer());
504 
505     outputLayer.reset();
506 }
507 
508 /*
509  * Display::setReleasedLayers()
510  */
511 
512 using DisplaySetReleasedLayersTest = DisplayWithLayersTestCommon;
513 
TEST_F(DisplaySetReleasedLayersTest,doesNothingIfGpuDisplay)514 TEST_F(DisplaySetReleasedLayersTest, doesNothingIfGpuDisplay) {
515     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
516     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
517 
518     sp<mock::LayerFE> layerXLayerFE = sp<StrictMock<mock::LayerFE>>::make();
519 
520     {
521         Output::ReleasedLayers releasedLayers;
522         releasedLayers.emplace_back(layerXLayerFE);
523         gpuDisplay->setReleasedLayers(std::move(releasedLayers));
524     }
525 
526     CompositionRefreshArgs refreshArgs;
527     refreshArgs.layersWithQueuedFrames.push_back(layerXLayerFE);
528 
529     gpuDisplay->setReleasedLayers(refreshArgs);
530 
531     const auto& releasedLayers = gpuDisplay->getReleasedLayersForTest();
532     ASSERT_EQ(1u, releasedLayers.size());
533 }
534 
TEST_F(DisplaySetReleasedLayersTest,doesNothingIfNoLayersWithQueuedFrames)535 TEST_F(DisplaySetReleasedLayersTest, doesNothingIfNoLayersWithQueuedFrames) {
536     sp<mock::LayerFE> layerXLayerFE = sp<StrictMock<mock::LayerFE>>::make();
537 
538     {
539         Output::ReleasedLayers releasedLayers;
540         releasedLayers.emplace_back(layerXLayerFE);
541         mDisplay->setReleasedLayers(std::move(releasedLayers));
542     }
543 
544     CompositionRefreshArgs refreshArgs;
545     mDisplay->setReleasedLayers(refreshArgs);
546 
547     const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
548     ASSERT_EQ(1u, releasedLayers.size());
549 }
550 
TEST_F(DisplaySetReleasedLayersTest,setReleasedLayers)551 TEST_F(DisplaySetReleasedLayersTest, setReleasedLayers) {
552     sp<mock::LayerFE> unknownLayer = sp<StrictMock<mock::LayerFE>>::make();
553 
554     CompositionRefreshArgs refreshArgs;
555     refreshArgs.layersWithQueuedFrames.push_back(mLayer1.layerFE);
556     refreshArgs.layersWithQueuedFrames.push_back(mLayer2.layerFE);
557     refreshArgs.layersWithQueuedFrames.push_back(unknownLayer);
558 
559     mDisplay->setReleasedLayers(refreshArgs);
560 
561     const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
562     ASSERT_EQ(2u, releasedLayers.size());
563     ASSERT_EQ(mLayer1.layerFE.get(), releasedLayers[0].promote().get());
564     ASSERT_EQ(mLayer2.layerFE.get(), releasedLayers[1].promote().get());
565 }
566 
567 /*
568  * Display::chooseCompositionStrategy()
569  */
570 
571 using DisplayChooseCompositionStrategyTest = PartialMockDisplayTestCommon;
572 
TEST_F(DisplayChooseCompositionStrategyTest,takesEarlyOutIfGpuDisplay)573 TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutIfGpuDisplay) {
574     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
575     std::shared_ptr<Display> gpuDisplay =
576             createPartialMockDisplay<Display>(mCompositionEngine, args);
577     EXPECT_TRUE(gpuDisplay->getDisplayIdVariant().and_then(asDisplayIdOfType<GpuVirtualDisplayId>));
578 
579     chooseCompositionStrategy(gpuDisplay.get());
580 
581     auto& state = gpuDisplay->getState();
582     EXPECT_TRUE(state.usesClientComposition);
583     EXPECT_FALSE(state.usesDeviceComposition);
584 }
585 
TEST_F(DisplayChooseCompositionStrategyTest,takesEarlyOutOnHwcError)586 TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutOnHwcError) {
587     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition()).WillOnce(Return(false));
588     EXPECT_CALL(mHwComposer,
589                 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), false, _, _, _, _))
590             .WillOnce(Return(INVALID_OPERATION));
591 
592     chooseCompositionStrategy(mDisplay.get());
593 
594     auto& state = mDisplay->getState();
595     EXPECT_TRUE(state.usesClientComposition);
596     EXPECT_FALSE(state.usesDeviceComposition);
597     EXPECT_FALSE(state.previousDeviceRequestedChanges.has_value());
598 }
599 
TEST_F(DisplayChooseCompositionStrategyTest,normalOperation)600 TEST_F(DisplayChooseCompositionStrategyTest, normalOperation) {
601     // Since two calls are made to anyLayersRequireClientComposition with different return
602     // values, use a Sequence to control the matching so the values are returned in a known
603     // order.
604     Sequence s;
605     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
606             .InSequence(s)
607             .WillOnce(Return(true));
608     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
609             .InSequence(s)
610             .WillOnce(Return(false));
611 
612     EXPECT_CALL(mHwComposer,
613                 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
614             .WillOnce(testing::DoAll(testing::SetArgPointee<5>(mDeviceRequestedChanges),
615                                      Return(NO_ERROR)));
616     EXPECT_CALL(*mDisplay, applyChangedTypesToLayers(mDeviceRequestedChanges.changedTypes))
617             .Times(1);
618     EXPECT_CALL(*mDisplay, applyDisplayRequests(mDeviceRequestedChanges.displayRequests)).Times(1);
619     EXPECT_CALL(*mDisplay, applyLayerRequestsToLayers(mDeviceRequestedChanges.layerRequests))
620             .Times(1);
621     EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
622     EXPECT_CALL(*mDisplay, applyLayerLutsToLayers(mDeviceRequestedChanges.layerLuts)).Times(1);
623 
624     chooseCompositionStrategy(mDisplay.get());
625 
626     auto& state = mDisplay->getState();
627     EXPECT_FALSE(state.usesClientComposition);
628     EXPECT_TRUE(state.usesDeviceComposition);
629 }
630 
TEST_F(DisplayChooseCompositionStrategyTest,normalOperationWithDisplayBrightness)631 TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithDisplayBrightness) {
632     // Since two calls are made to anyLayersRequireClientComposition with different return
633     // values, use a Sequence to control the matching so the values are returned in a known
634     // order.
635     constexpr float kDisplayBrightness = 0.5f;
636     constexpr float kDisplayBrightnessNits = 200.f;
637     EXPECT_CALL(mHwComposer,
638                 setDisplayBrightness(DEFAULT_DISPLAY_ID, kDisplayBrightness, kDisplayBrightnessNits,
639                                      Hwc2::Composer::DisplayBrightnessOptions{.applyImmediately =
640                                                                                       false}))
641             .WillOnce(Return(ByMove(ftl::yield<status_t>(NO_ERROR))));
642 
643     mDisplay->setNextBrightness(kDisplayBrightness);
644     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
645     EXPECT_CALL(*renderSurface, beginFrame(_)).Times(1);
646     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
647     mDisplay->editState().displayBrightnessNits = kDisplayBrightnessNits;
648     mDisplay->beginFrame();
649 
650     auto& state = mDisplay->getState();
651     EXPECT_FALSE(state.displayBrightness.has_value());
652 }
653 
TEST_F(DisplayChooseCompositionStrategyTest,normalOperationWithChanges)654 TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithChanges) {
655     // Since two calls are made to anyLayersRequireClientComposition with different return
656     // values, use a Sequence to control the matching so the values are returned in a known
657     // order.
658     Sequence s;
659     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
660             .InSequence(s)
661             .WillOnce(Return(true));
662     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
663             .InSequence(s)
664             .WillOnce(Return(false));
665 
666     EXPECT_CALL(mHwComposer,
667                 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
668             .WillOnce(DoAll(SetArgPointee<5>(mDeviceRequestedChanges), Return(NO_ERROR)));
669     EXPECT_CALL(*mDisplay, applyChangedTypesToLayers(mDeviceRequestedChanges.changedTypes))
670             .Times(1);
671     EXPECT_CALL(*mDisplay, applyDisplayRequests(mDeviceRequestedChanges.displayRequests)).Times(1);
672     EXPECT_CALL(*mDisplay, applyLayerRequestsToLayers(mDeviceRequestedChanges.layerRequests))
673             .Times(1);
674     EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
675     EXPECT_CALL(*mDisplay, applyLayerLutsToLayers(mDeviceRequestedChanges.layerLuts)).Times(1);
676 
677     chooseCompositionStrategy(mDisplay.get());
678 
679     auto& state = mDisplay->getState();
680     EXPECT_FALSE(state.usesClientComposition);
681     EXPECT_TRUE(state.usesDeviceComposition);
682 }
683 
684 /*
685  * Display::getSkipColorTransform()
686  */
687 
688 using DisplayGetSkipColorTransformTest = DisplayWithLayersTestCommon;
689 using aidl::android::hardware::graphics::composer3::DisplayCapability;
690 
TEST_F(DisplayGetSkipColorTransformTest,checksCapabilityIfGpuDisplay)691 TEST_F(DisplayGetSkipColorTransformTest, checksCapabilityIfGpuDisplay) {
692     EXPECT_CALL(mHwComposer, hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM))
693             .WillOnce(Return(true));
694     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
695     auto gpuDisplay{impl::createDisplay(mCompositionEngine, args)};
696     EXPECT_TRUE(gpuDisplay->getSkipColorTransform());
697 }
698 
TEST_F(DisplayGetSkipColorTransformTest,checksDisplayCapability)699 TEST_F(DisplayGetSkipColorTransformTest, checksDisplayCapability) {
700     EXPECT_CALL(mHwComposer,
701                 hasDisplayCapability(HalDisplayId(DEFAULT_DISPLAY_ID),
702                                      DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM))
703             .WillOnce(Return(true));
704     EXPECT_TRUE(mDisplay->getSkipColorTransform());
705 }
706 
707 /*
708  * Display::anyLayersRequireClientComposition()
709  */
710 
711 using DisplayAnyLayersRequireClientCompositionTest = DisplayWithLayersTestCommon;
712 
TEST_F(DisplayAnyLayersRequireClientCompositionTest,returnsFalse)713 TEST_F(DisplayAnyLayersRequireClientCompositionTest, returnsFalse) {
714     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(false));
715     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(false));
716     EXPECT_CALL(*mLayer3.outputLayer, requiresClientComposition()).WillOnce(Return(false));
717 
718     EXPECT_FALSE(mDisplay->anyLayersRequireClientComposition());
719 }
720 
TEST_F(DisplayAnyLayersRequireClientCompositionTest,returnsTrue)721 TEST_F(DisplayAnyLayersRequireClientCompositionTest, returnsTrue) {
722     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(false));
723     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(true));
724 
725     EXPECT_TRUE(mDisplay->anyLayersRequireClientComposition());
726 }
727 
728 /*
729  * Display::allLayersRequireClientComposition()
730  */
731 
732 using DisplayAllLayersRequireClientCompositionTest = DisplayWithLayersTestCommon;
733 
TEST_F(DisplayAllLayersRequireClientCompositionTest,returnsTrue)734 TEST_F(DisplayAllLayersRequireClientCompositionTest, returnsTrue) {
735     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(true));
736     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(true));
737     EXPECT_CALL(*mLayer3.outputLayer, requiresClientComposition()).WillOnce(Return(true));
738 
739     EXPECT_TRUE(mDisplay->allLayersRequireClientComposition());
740 }
741 
TEST_F(DisplayAllLayersRequireClientCompositionTest,returnsFalse)742 TEST_F(DisplayAllLayersRequireClientCompositionTest, returnsFalse) {
743     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(true));
744     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(false));
745 
746     EXPECT_FALSE(mDisplay->allLayersRequireClientComposition());
747 }
748 
749 /*
750  * Display::applyChangedTypesToLayers()
751  */
752 
753 using DisplayApplyChangedTypesToLayersTest = DisplayWithLayersTestCommon;
754 
TEST_F(DisplayApplyChangedTypesToLayersTest,takesEarlyOutIfNoChangedLayers)755 TEST_F(DisplayApplyChangedTypesToLayersTest, takesEarlyOutIfNoChangedLayers) {
756     mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes());
757 }
758 
TEST_F(DisplayApplyChangedTypesToLayersTest,appliesChanges)759 TEST_F(DisplayApplyChangedTypesToLayersTest, appliesChanges) {
760     EXPECT_CALL(*mLayer1.outputLayer, applyDeviceCompositionTypeChange(Composition::CLIENT))
761             .Times(1);
762     EXPECT_CALL(*mLayer2.outputLayer, applyDeviceCompositionTypeChange(Composition::DEVICE))
763             .Times(1);
764 
765     mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes{
766             {&mLayer1.hwc2Layer, Composition::CLIENT},
767             {&mLayer2.hwc2Layer, Composition::DEVICE},
768             {&hwc2LayerUnknown, Composition::SOLID_COLOR},
769     });
770 }
771 
772 /*
773  * Display::applyDisplayRequests()
774  */
775 
776 using DisplayApplyDisplayRequestsTest = DisplayWithLayersTestCommon;
777 
TEST_F(DisplayApplyDisplayRequestsTest,handlesNoRequests)778 TEST_F(DisplayApplyDisplayRequestsTest, handlesNoRequests) {
779     mDisplay->applyDisplayRequests(static_cast<hal::DisplayRequest>(0));
780 
781     auto& state = mDisplay->getState();
782     EXPECT_FALSE(state.flipClientTarget);
783 }
784 
TEST_F(DisplayApplyDisplayRequestsTest,handlesFlipClientTarget)785 TEST_F(DisplayApplyDisplayRequestsTest, handlesFlipClientTarget) {
786     mDisplay->applyDisplayRequests(hal::DisplayRequest::FLIP_CLIENT_TARGET);
787 
788     auto& state = mDisplay->getState();
789     EXPECT_TRUE(state.flipClientTarget);
790 }
791 
TEST_F(DisplayApplyDisplayRequestsTest,handlesWriteClientTargetToOutput)792 TEST_F(DisplayApplyDisplayRequestsTest, handlesWriteClientTargetToOutput) {
793     mDisplay->applyDisplayRequests(hal::DisplayRequest::WRITE_CLIENT_TARGET_TO_OUTPUT);
794 
795     auto& state = mDisplay->getState();
796     EXPECT_FALSE(state.flipClientTarget);
797 }
798 
TEST_F(DisplayApplyDisplayRequestsTest,handlesAllRequestFlagsSet)799 TEST_F(DisplayApplyDisplayRequestsTest, handlesAllRequestFlagsSet) {
800     mDisplay->applyDisplayRequests(static_cast<hal::DisplayRequest>(~0));
801 
802     auto& state = mDisplay->getState();
803     EXPECT_TRUE(state.flipClientTarget);
804 }
805 
806 /*
807  * Display::applyLayerRequestsToLayers()
808  */
809 
810 using DisplayApplyLayerRequestsToLayersTest = DisplayWithLayersTestCommon;
811 
TEST_F(DisplayApplyLayerRequestsToLayersTest,preparesAllLayers)812 TEST_F(DisplayApplyLayerRequestsToLayersTest, preparesAllLayers) {
813     EXPECT_CALL(*mLayer1.outputLayer, prepareForDeviceLayerRequests()).Times(1);
814     EXPECT_CALL(*mLayer2.outputLayer, prepareForDeviceLayerRequests()).Times(1);
815     EXPECT_CALL(*mLayer3.outputLayer, prepareForDeviceLayerRequests()).Times(1);
816 
817     mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests());
818 }
819 
TEST_F(DisplayApplyLayerRequestsToLayersTest,appliesDeviceLayerRequests)820 TEST_F(DisplayApplyLayerRequestsToLayersTest, appliesDeviceLayerRequests) {
821     EXPECT_CALL(*mLayer1.outputLayer, prepareForDeviceLayerRequests()).Times(1);
822     EXPECT_CALL(*mLayer2.outputLayer, prepareForDeviceLayerRequests()).Times(1);
823     EXPECT_CALL(*mLayer3.outputLayer, prepareForDeviceLayerRequests()).Times(1);
824 
825     EXPECT_CALL(*mLayer1.outputLayer,
826                 applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET))
827             .Times(1);
828 
829     mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests{
830             {&mLayer1.hwc2Layer, hal::LayerRequest::CLEAR_CLIENT_TARGET},
831             {&hwc2LayerUnknown, hal::LayerRequest::CLEAR_CLIENT_TARGET},
832     });
833 }
834 
835 /*
836  * Display::applyClientTargetRequests()
837  */
838 
839 using DisplayApplyClientTargetRequests = DisplayWithLayersTestCommon;
840 
TEST_F(DisplayApplyLayerRequestsToLayersTest,applyClientTargetRequests)841 TEST_F(DisplayApplyLayerRequestsToLayersTest, applyClientTargetRequests) {
842     static constexpr float kWhitePointNits = 800.f;
843 
844     Display::ClientTargetProperty clientTargetProperty = {
845             .clientTargetProperty =
846                     {
847                             .pixelFormat =
848                                     aidl::android::hardware::graphics::common::PixelFormat::RGB_565,
849                             .dataspace = aidl::android::hardware::graphics::common::Dataspace::
850                                     STANDARD_BT470M,
851                     },
852             .brightness = kWhitePointNits,
853             .dimmingStage = aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF,
854     };
855 
856     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
857     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
858 
859     EXPECT_CALL(*renderSurface,
860                 setBufferPixelFormat(static_cast<ui::PixelFormat>(
861                         clientTargetProperty.clientTargetProperty.pixelFormat)));
862     EXPECT_CALL(*renderSurface,
863                 setBufferDataspace(static_cast<ui::Dataspace>(
864                         clientTargetProperty.clientTargetProperty.dataspace)));
865     mDisplay->applyClientTargetRequests(clientTargetProperty);
866 
867     auto& state = mDisplay->getState();
868     EXPECT_EQ(clientTargetProperty.clientTargetProperty.dataspace,
869               static_cast<aidl::android::hardware::graphics::common::Dataspace>(state.dataspace));
870     EXPECT_EQ(kWhitePointNits, state.clientTargetBrightness);
871     EXPECT_EQ(aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF,
872               state.clientTargetDimmingStage);
873 }
874 
875 /*
876  * Display::presentFrame()
877  */
878 
879 using DisplayPresentAndGetFrameFencesTest = DisplayWithLayersTestCommon;
880 
TEST_F(DisplayPresentAndGetFrameFencesTest,returnsNoFencesOnGpuDisplay)881 TEST_F(DisplayPresentAndGetFrameFencesTest, returnsNoFencesOnGpuDisplay) {
882     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
883     auto gpuDisplay{impl::createDisplay(mCompositionEngine, args)};
884 
885     auto result = gpuDisplay->presentFrame();
886 
887     ASSERT_TRUE(result.presentFence.get());
888     EXPECT_FALSE(result.presentFence->isValid());
889     EXPECT_EQ(0u, result.layerFences.size());
890 }
891 
TEST_F(DisplayPresentAndGetFrameFencesTest,returnsPresentAndLayerFences)892 TEST_F(DisplayPresentAndGetFrameFencesTest, returnsPresentAndLayerFences) {
893     sp<Fence> presentFence = sp<Fence>::make();
894     sp<Fence> layer1Fence = sp<Fence>::make();
895     sp<Fence> layer2Fence = sp<Fence>::make();
896 
897     EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(HalDisplayId(DEFAULT_DISPLAY_ID), _))
898             .Times(1);
899     EXPECT_CALL(mHwComposer, getPresentFence(HalDisplayId(DEFAULT_DISPLAY_ID)))
900             .WillOnce(Return(presentFence));
901     EXPECT_CALL(mHwComposer,
902                 getLayerReleaseFence(HalDisplayId(DEFAULT_DISPLAY_ID), &mLayer1.hwc2Layer))
903             .WillOnce(Return(layer1Fence));
904     EXPECT_CALL(mHwComposer,
905                 getLayerReleaseFence(HalDisplayId(DEFAULT_DISPLAY_ID), &mLayer2.hwc2Layer))
906             .WillOnce(Return(layer2Fence));
907     EXPECT_CALL(mHwComposer, clearReleaseFences(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(1);
908 
909     auto result = mDisplay->presentFrame();
910 
911     EXPECT_EQ(presentFence, result.presentFence);
912 
913     EXPECT_EQ(2u, result.layerFences.size());
914     ASSERT_EQ(1u, result.layerFences.count(&mLayer1.hwc2Layer));
915     EXPECT_EQ(layer1Fence, result.layerFences[&mLayer1.hwc2Layer]);
916     ASSERT_EQ(1u, result.layerFences.count(&mLayer2.hwc2Layer));
917     EXPECT_EQ(layer2Fence, result.layerFences[&mLayer2.hwc2Layer]);
918 }
919 
920 /*
921  * Display::setExpensiveRenderingExpected()
922  */
923 
924 using DisplaySetExpensiveRenderingExpectedTest = DisplayWithLayersTestCommon;
925 
TEST_F(DisplaySetExpensiveRenderingExpectedTest,forwardsToPowerAdvisor)926 TEST_F(DisplaySetExpensiveRenderingExpectedTest, forwardsToPowerAdvisor) {
927     EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, true)).Times(1);
928     mDisplay->setExpensiveRenderingExpected(true);
929 
930     EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false)).Times(1);
931     mDisplay->setExpensiveRenderingExpected(false);
932 }
933 
934 /*
935  * Display::finishFrame()
936  */
937 
938 using DisplayFinishFrameTest = DisplayWithLayersTestCommon;
939 
TEST_F(DisplayFinishFrameTest,doesNotSkipCompositionIfNotDirtyOnHwcDisplay)940 TEST_F(DisplayFinishFrameTest, doesNotSkipCompositionIfNotDirtyOnHwcDisplay) {
941     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
942     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
943 
944     // We expect no calls to queueBuffer if composition was skipped.
945     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(1);
946 
947     // Expect a call to signal no expensive rendering since there is no client composition.
948     EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false));
949 
950     mDisplay->editState().isEnabled = true;
951     mDisplay->editState().usesClientComposition = false;
952     mDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
953     mDisplay->editState().dirtyRegion = Region::INVALID_REGION;
954 
955     mDisplay->finishFrame(std::move(mResultWithBuffer));
956 }
957 
TEST_F(DisplayFinishFrameTest,skipsCompositionIfNotDirty)958 TEST_F(DisplayFinishFrameTest, skipsCompositionIfNotDirty) {
959     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
960     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
961 
962     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
963     gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
964 
965     // We expect no calls to queueBuffer if composition was skipped.
966     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(0);
967     EXPECT_CALL(*renderSurface, beginFrame(false));
968 
969     gpuDisplay->editState().isEnabled = true;
970     gpuDisplay->editState().usesClientComposition = false;
971     gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
972     gpuDisplay->editState().dirtyRegion = Region::INVALID_REGION;
973     gpuDisplay->editState().lastCompositionHadVisibleLayers = true;
974 
975     gpuDisplay->beginFrame();
976     gpuDisplay->finishFrame(std::move(mResultWithoutBuffer));
977 }
978 
TEST_F(DisplayFinishFrameTest,skipsCompositionIfEmpty)979 TEST_F(DisplayFinishFrameTest, skipsCompositionIfEmpty) {
980     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
981     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
982 
983     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
984     gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
985 
986     // We expect no calls to queueBuffer if composition was skipped.
987     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(0);
988     EXPECT_CALL(*renderSurface, beginFrame(false));
989 
990     gpuDisplay->editState().isEnabled = true;
991     gpuDisplay->editState().usesClientComposition = false;
992     gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
993     gpuDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
994     gpuDisplay->editState().lastCompositionHadVisibleLayers = false;
995 
996     gpuDisplay->beginFrame();
997     gpuDisplay->finishFrame(std::move(mResultWithoutBuffer));
998 }
999 
TEST_F(DisplayFinishFrameTest,performsCompositionIfDirtyAndNotEmpty)1000 TEST_F(DisplayFinishFrameTest, performsCompositionIfDirtyAndNotEmpty) {
1001     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
1002     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
1003 
1004     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
1005     gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
1006 
1007     // We expect a single call to queueBuffer when composition is not skipped.
1008     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(1);
1009     EXPECT_CALL(*renderSurface, beginFrame(true));
1010 
1011     gpuDisplay->editState().isEnabled = true;
1012     gpuDisplay->editState().usesClientComposition = false;
1013     gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
1014     gpuDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
1015     gpuDisplay->editState().lastCompositionHadVisibleLayers = true;
1016 
1017     gpuDisplay->beginFrame();
1018     gpuDisplay->finishFrame(std::move(mResultWithBuffer));
1019 }
1020 
1021 /*
1022  * Display functional tests
1023  */
1024 
1025 struct DisplayFunctionalTest : public testing::Test {
1026     class Display : public impl::Display {
1027     public:
1028         using impl::Display::injectOutputLayerForTest;
1029         virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
1030     };
1031 
DisplayFunctionalTestandroid::compositionengine::__anon153eb9cf0111::DisplayFunctionalTest1032     DisplayFunctionalTest() {
1033         EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
1034         mDisplay = createDisplay();
1035         mRenderSurface = createRenderSurface();
1036         mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
1037     }
1038 
1039     NiceMock<android::mock::HWComposer> mHwComposer;
1040     NiceMock<adpf::mock::PowerAdvisor> mPowerAdvisor;
1041     NiceMock<mock::CompositionEngine> mCompositionEngine;
1042     sp<mock::NativeWindow> mNativeWindow = sp<NiceMock<mock::NativeWindow>>::make();
1043     sp<mock::DisplaySurface> mDisplaySurface = sp<NiceMock<mock::DisplaySurface>>::make();
1044     std::shared_ptr<Display> mDisplay;
1045     impl::RenderSurface* mRenderSurface;
1046 
createDisplayandroid::compositionengine::__anon153eb9cf0111::DisplayFunctionalTest1047     std::shared_ptr<Display> createDisplay() {
1048         return impl::createDisplayTemplated<Display>(mCompositionEngine,
1049                                                      DisplayCreationArgsBuilder()
1050                                                              .setId(DEFAULT_DISPLAY_ID)
1051                                                              .setPixels(DEFAULT_RESOLUTION)
1052                                                              .setIsSecure(true)
1053                                                              .setPowerAdvisor(&mPowerAdvisor)
1054                                                              .build());
1055         ;
1056     }
1057 
createRenderSurfaceandroid::compositionengine::__anon153eb9cf0111::DisplayFunctionalTest1058     impl::RenderSurface* createRenderSurface() {
1059         return new impl::RenderSurface{mCompositionEngine, *mDisplay,
1060                                        RenderSurfaceCreationArgsBuilder()
1061                                                .setDisplayWidth(DEFAULT_RESOLUTION.width)
1062                                                .setDisplayHeight(DEFAULT_RESOLUTION.height)
1063                                                .setNativeWindow(mNativeWindow)
1064                                                .setDisplaySurface(mDisplaySurface)
1065                                                .build()};
1066     }
1067 };
1068 
TEST_F(DisplayFunctionalTest,presentFrameAndReleaseLayersCriticalCallsAreOrdered)1069 TEST_F(DisplayFunctionalTest, presentFrameAndReleaseLayersCriticalCallsAreOrdered) {
1070     InSequence seq;
1071 
1072     mDisplay->editState().isEnabled = true;
1073 
1074     EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(_, _));
1075     EXPECT_CALL(*mDisplaySurface, onFrameCommitted());
1076     constexpr bool kFlushEvenWhenDisabled = false;
1077     mDisplay->presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
1078 }
1079 
1080 } // namespace
1081 } // namespace android::compositionengine
1082