xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/CompositionEngine/tests/DisplayTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::DisplayTestCommon::PartialMockDisplay107         PartialMockDisplay(const compositionengine::CompositionEngine& compositionEngine)
108               : mCompositionEngine(compositionEngine) {}
109 
110         // compositionengine::Output overrides
getStateandroid::compositionengine::__anon67b74f4f0111::DisplayTestCommon::PartialMockDisplay111         const OutputCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon67b74f4f0111::DisplayTestCommon::PartialMockDisplay112         OutputCompositionState& editState() override { return mState; }
113 
114         // compositionengine::impl::Output overrides
getCompositionEngineandroid::compositionengine::__anon67b74f4f0111::DisplayTestCommon::PartialMockDisplay115         const CompositionEngine& getCompositionEngine() const override {
116             return mCompositionEngine;
117         };
118 
getOutputLayerCountandroid::compositionengine::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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::__anon67b74f4f0111::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(GpuVirtualDisplayId::tryCast(display->getId()));
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_FALSE(mDisplay->isValid());
322 
323     const auto& filter = mDisplay->getState().layerFilter;
324     EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
325     EXPECT_FALSE(filter.toInternalDisplay);
326 }
327 
TEST_F(DisplaySetConfigurationTest,configuresGpuVirtualDisplay)328 TEST_F(DisplaySetConfigurationTest, configuresGpuVirtualDisplay) {
329     mDisplay->setConfiguration(DisplayCreationArgsBuilder()
330                                        .setId(GPU_VIRTUAL_DISPLAY_ID)
331                                        .setPixels(DEFAULT_RESOLUTION)
332                                        .setIsSecure(false)
333                                        .setPowerAdvisor(&mPowerAdvisor)
334                                        .setName(getDisplayNameFromCurrentTest())
335                                        .build());
336 
337     EXPECT_EQ(GPU_VIRTUAL_DISPLAY_ID, mDisplay->getId());
338     EXPECT_FALSE(mDisplay->isSecure());
339     EXPECT_TRUE(mDisplay->isVirtual());
340     EXPECT_FALSE(mDisplay->isValid());
341 
342     const auto& filter = mDisplay->getState().layerFilter;
343     EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
344     EXPECT_FALSE(filter.toInternalDisplay);
345 }
346 
347 /*
348  * Display::disconnect()
349  */
350 
351 using DisplayDisconnectTest = PartialMockDisplayTestCommon;
352 
TEST_F(DisplayDisconnectTest,disconnectsDisplay)353 TEST_F(DisplayDisconnectTest, disconnectsDisplay) {
354     // The first call to disconnect will disconnect the display with the HWC.
355     EXPECT_CALL(mHwComposer, disconnectDisplay(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(1);
356     mDisplay->disconnect();
357 
358     // Subsequent calls will do nothing,
359     EXPECT_CALL(mHwComposer, disconnectDisplay(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(0);
360     mDisplay->disconnect();
361 }
362 
363 /*
364  * Display::setColorTransform()
365  */
366 
367 using DisplaySetColorTransformTest = PartialMockDisplayTestCommon;
368 
TEST_F(DisplaySetColorTransformTest,setsTransform)369 TEST_F(DisplaySetColorTransformTest, setsTransform) {
370     // No change does nothing
371     CompositionRefreshArgs refreshArgs;
372     refreshArgs.colorTransformMatrix = std::nullopt;
373     mDisplay->setColorTransform(refreshArgs);
374 
375     // Identity matrix sets an identity state value
376     const mat4 kIdentity;
377 
378     EXPECT_CALL(mHwComposer, setColorTransform(HalDisplayId(DEFAULT_DISPLAY_ID), kIdentity))
379             .Times(1);
380 
381     refreshArgs.colorTransformMatrix = kIdentity;
382     mDisplay->setColorTransform(refreshArgs);
383 
384     // Non-identity matrix sets a non-identity state value
385     const mat4 kNonIdentity = mat4() * 2;
386 
387     EXPECT_CALL(mHwComposer, setColorTransform(HalDisplayId(DEFAULT_DISPLAY_ID), kNonIdentity))
388             .Times(1);
389 
390     refreshArgs.colorTransformMatrix = kNonIdentity;
391     mDisplay->setColorTransform(refreshArgs);
392 }
393 
394 /*
395  * Display::setColorMode()
396  */
397 
398 using DisplaySetColorModeTest = PartialMockDisplayTestCommon;
399 
TEST_F(DisplaySetColorModeTest,setsModeUnlessNoChange)400 TEST_F(DisplaySetColorModeTest, setsModeUnlessNoChange) {
401     using ColorProfile = Output::ColorProfile;
402 
403     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
404     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
405     mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
406     mDisplay->setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile));
407 
408     // These values are expected to be the initial state.
409     ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
410     ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
411     ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
412 
413     // Otherwise if the values are unchanged, nothing happens
414     mDisplay->setColorProfile(ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
415                                            ui::RenderIntent::COLORIMETRIC});
416 
417     EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
418     EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
419     EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
420 
421     // Otherwise if the values are different, updates happen
422     EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
423     EXPECT_CALL(mHwComposer,
424                 setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
425                                    ui::RenderIntent::TONE_MAP_COLORIMETRIC))
426             .Times(1);
427 
428     mDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
429                                            ui::RenderIntent::TONE_MAP_COLORIMETRIC});
430 
431     EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay->getState().colorMode);
432     EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay->getState().dataspace);
433     EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay->getState().renderIntent);
434 }
435 
TEST_F(DisplaySetColorModeTest,doesNothingForVirtualDisplay)436 TEST_F(DisplaySetColorModeTest, doesNothingForVirtualDisplay) {
437     using ColorProfile = Output::ColorProfile;
438 
439     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
440     std::shared_ptr<impl::Display> virtualDisplay = impl::createDisplay(mCompositionEngine, args);
441 
442     mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
443     virtualDisplay->setDisplayColorProfileForTest(
444             std::unique_ptr<DisplayColorProfile>(colorProfile));
445 
446     virtualDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3,
447                                                  ui::Dataspace::DISPLAY_P3,
448                                                  ui::RenderIntent::TONE_MAP_COLORIMETRIC});
449 
450     EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay->getState().colorMode);
451     EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().dataspace);
452     EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay->getState().renderIntent);
453 }
454 
455 /*
456  * Display::createDisplayColorProfile()
457  */
458 
459 using DisplayCreateColorProfileTest = PartialMockDisplayTestCommon;
460 
TEST_F(DisplayCreateColorProfileTest,setsDisplayColorProfile)461 TEST_F(DisplayCreateColorProfileTest, setsDisplayColorProfile) {
462     EXPECT_TRUE(mDisplay->getDisplayColorProfile() == nullptr);
463     mDisplay->createDisplayColorProfile(
464             DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
465                                             DisplayColorProfileCreationArgs::HwcColorModes()});
466     EXPECT_TRUE(mDisplay->getDisplayColorProfile() != nullptr);
467 }
468 
469 /*
470  * Display::createRenderSurface()
471  */
472 
473 using DisplayCreateRenderSurfaceTest = PartialMockDisplayTestCommon;
474 
TEST_F(DisplayCreateRenderSurfaceTest,setsRenderSurface)475 TEST_F(DisplayCreateRenderSurfaceTest, setsRenderSurface) {
476     EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
477     EXPECT_TRUE(mDisplay->getRenderSurface() == nullptr);
478     mDisplay->createRenderSurface(RenderSurfaceCreationArgsBuilder()
479                                           .setDisplayWidth(640)
480                                           .setDisplayHeight(480)
481                                           .setNativeWindow(mNativeWindow)
482                                           .build());
483     EXPECT_TRUE(mDisplay->getRenderSurface() != nullptr);
484 }
485 
486 /*
487  * Display::createOutputLayer()
488  */
489 
490 using DisplayCreateOutputLayerTest = FullDisplayImplTestCommon;
491 
TEST_F(DisplayCreateOutputLayerTest,setsHwcLayer)492 TEST_F(DisplayCreateOutputLayerTest, setsHwcLayer) {
493     sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
494     auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
495 
496     EXPECT_CALL(mHwComposer, createLayer(HalDisplayId(DEFAULT_DISPLAY_ID)))
497             .WillOnce(Return(hwcLayer));
498 
499     auto outputLayer = mDisplay->createOutputLayer(layerFE);
500 
501     EXPECT_EQ(hwcLayer.get(), outputLayer->getHwcLayer());
502 
503     outputLayer.reset();
504 }
505 
506 /*
507  * Display::setReleasedLayers()
508  */
509 
510 using DisplaySetReleasedLayersTest = DisplayWithLayersTestCommon;
511 
TEST_F(DisplaySetReleasedLayersTest,doesNothingIfGpuDisplay)512 TEST_F(DisplaySetReleasedLayersTest, doesNothingIfGpuDisplay) {
513     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
514     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
515 
516     sp<mock::LayerFE> layerXLayerFE = sp<StrictMock<mock::LayerFE>>::make();
517 
518     {
519         Output::ReleasedLayers releasedLayers;
520         releasedLayers.emplace_back(layerXLayerFE);
521         gpuDisplay->setReleasedLayers(std::move(releasedLayers));
522     }
523 
524     CompositionRefreshArgs refreshArgs;
525     refreshArgs.layersWithQueuedFrames.push_back(layerXLayerFE);
526 
527     gpuDisplay->setReleasedLayers(refreshArgs);
528 
529     const auto& releasedLayers = gpuDisplay->getReleasedLayersForTest();
530     ASSERT_EQ(1u, releasedLayers.size());
531 }
532 
TEST_F(DisplaySetReleasedLayersTest,doesNothingIfNoLayersWithQueuedFrames)533 TEST_F(DisplaySetReleasedLayersTest, doesNothingIfNoLayersWithQueuedFrames) {
534     sp<mock::LayerFE> layerXLayerFE = sp<StrictMock<mock::LayerFE>>::make();
535 
536     {
537         Output::ReleasedLayers releasedLayers;
538         releasedLayers.emplace_back(layerXLayerFE);
539         mDisplay->setReleasedLayers(std::move(releasedLayers));
540     }
541 
542     CompositionRefreshArgs refreshArgs;
543     mDisplay->setReleasedLayers(refreshArgs);
544 
545     const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
546     ASSERT_EQ(1u, releasedLayers.size());
547 }
548 
TEST_F(DisplaySetReleasedLayersTest,setReleasedLayers)549 TEST_F(DisplaySetReleasedLayersTest, setReleasedLayers) {
550     sp<mock::LayerFE> unknownLayer = sp<StrictMock<mock::LayerFE>>::make();
551 
552     CompositionRefreshArgs refreshArgs;
553     refreshArgs.layersWithQueuedFrames.push_back(mLayer1.layerFE);
554     refreshArgs.layersWithQueuedFrames.push_back(mLayer2.layerFE);
555     refreshArgs.layersWithQueuedFrames.push_back(unknownLayer);
556 
557     mDisplay->setReleasedLayers(refreshArgs);
558 
559     const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
560     ASSERT_EQ(2u, releasedLayers.size());
561     ASSERT_EQ(mLayer1.layerFE.get(), releasedLayers[0].promote().get());
562     ASSERT_EQ(mLayer2.layerFE.get(), releasedLayers[1].promote().get());
563 }
564 
565 /*
566  * Display::chooseCompositionStrategy()
567  */
568 
569 using DisplayChooseCompositionStrategyTest = PartialMockDisplayTestCommon;
570 
TEST_F(DisplayChooseCompositionStrategyTest,takesEarlyOutIfGpuDisplay)571 TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutIfGpuDisplay) {
572     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
573     std::shared_ptr<Display> gpuDisplay =
574             createPartialMockDisplay<Display>(mCompositionEngine, args);
575     EXPECT_TRUE(GpuVirtualDisplayId::tryCast(gpuDisplay->getId()));
576 
577     chooseCompositionStrategy(gpuDisplay.get());
578 
579     auto& state = gpuDisplay->getState();
580     EXPECT_TRUE(state.usesClientComposition);
581     EXPECT_FALSE(state.usesDeviceComposition);
582 }
583 
TEST_F(DisplayChooseCompositionStrategyTest,takesEarlyOutOnHwcError)584 TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutOnHwcError) {
585     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition()).WillOnce(Return(false));
586     EXPECT_CALL(mHwComposer,
587                 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), false, _, _, _, _))
588             .WillOnce(Return(INVALID_OPERATION));
589 
590     chooseCompositionStrategy(mDisplay.get());
591 
592     auto& state = mDisplay->getState();
593     EXPECT_TRUE(state.usesClientComposition);
594     EXPECT_FALSE(state.usesDeviceComposition);
595     EXPECT_FALSE(state.previousDeviceRequestedChanges.has_value());
596 }
597 
TEST_F(DisplayChooseCompositionStrategyTest,normalOperation)598 TEST_F(DisplayChooseCompositionStrategyTest, normalOperation) {
599     // Since two calls are made to anyLayersRequireClientComposition with different return
600     // values, use a Sequence to control the matching so the values are returned in a known
601     // order.
602     Sequence s;
603     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
604             .InSequence(s)
605             .WillOnce(Return(true));
606     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
607             .InSequence(s)
608             .WillOnce(Return(false));
609 
610     EXPECT_CALL(mHwComposer,
611                 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
612             .WillOnce(testing::DoAll(testing::SetArgPointee<5>(mDeviceRequestedChanges),
613                                      Return(NO_ERROR)));
614     EXPECT_CALL(*mDisplay, applyChangedTypesToLayers(mDeviceRequestedChanges.changedTypes))
615             .Times(1);
616     EXPECT_CALL(*mDisplay, applyDisplayRequests(mDeviceRequestedChanges.displayRequests)).Times(1);
617     EXPECT_CALL(*mDisplay, applyLayerRequestsToLayers(mDeviceRequestedChanges.layerRequests))
618             .Times(1);
619     EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
620     EXPECT_CALL(*mDisplay, applyLayerLutsToLayers(mDeviceRequestedChanges.layerLuts)).Times(1);
621 
622     chooseCompositionStrategy(mDisplay.get());
623 
624     auto& state = mDisplay->getState();
625     EXPECT_FALSE(state.usesClientComposition);
626     EXPECT_TRUE(state.usesDeviceComposition);
627 }
628 
TEST_F(DisplayChooseCompositionStrategyTest,normalOperationWithDisplayBrightness)629 TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithDisplayBrightness) {
630     // Since two calls are made to anyLayersRequireClientComposition with different return
631     // values, use a Sequence to control the matching so the values are returned in a known
632     // order.
633     constexpr float kDisplayBrightness = 0.5f;
634     constexpr float kDisplayBrightnessNits = 200.f;
635     EXPECT_CALL(mHwComposer,
636                 setDisplayBrightness(DEFAULT_DISPLAY_ID, kDisplayBrightness, kDisplayBrightnessNits,
637                                      Hwc2::Composer::DisplayBrightnessOptions{.applyImmediately =
638                                                                                       false}))
639             .WillOnce(Return(ByMove(ftl::yield<status_t>(NO_ERROR))));
640 
641     mDisplay->setNextBrightness(kDisplayBrightness);
642     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
643     EXPECT_CALL(*renderSurface, beginFrame(_)).Times(1);
644     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
645     mDisplay->editState().displayBrightnessNits = kDisplayBrightnessNits;
646     mDisplay->beginFrame();
647 
648     auto& state = mDisplay->getState();
649     EXPECT_FALSE(state.displayBrightness.has_value());
650 }
651 
TEST_F(DisplayChooseCompositionStrategyTest,normalOperationWithChanges)652 TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithChanges) {
653     // Since two calls are made to anyLayersRequireClientComposition with different return
654     // values, use a Sequence to control the matching so the values are returned in a known
655     // order.
656     Sequence s;
657     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
658             .InSequence(s)
659             .WillOnce(Return(true));
660     EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
661             .InSequence(s)
662             .WillOnce(Return(false));
663 
664     EXPECT_CALL(mHwComposer,
665                 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
666             .WillOnce(DoAll(SetArgPointee<5>(mDeviceRequestedChanges), Return(NO_ERROR)));
667     EXPECT_CALL(*mDisplay, applyChangedTypesToLayers(mDeviceRequestedChanges.changedTypes))
668             .Times(1);
669     EXPECT_CALL(*mDisplay, applyDisplayRequests(mDeviceRequestedChanges.displayRequests)).Times(1);
670     EXPECT_CALL(*mDisplay, applyLayerRequestsToLayers(mDeviceRequestedChanges.layerRequests))
671             .Times(1);
672     EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
673     EXPECT_CALL(*mDisplay, applyLayerLutsToLayers(mDeviceRequestedChanges.layerLuts)).Times(1);
674 
675     chooseCompositionStrategy(mDisplay.get());
676 
677     auto& state = mDisplay->getState();
678     EXPECT_FALSE(state.usesClientComposition);
679     EXPECT_TRUE(state.usesDeviceComposition);
680 }
681 
682 /*
683  * Display::getSkipColorTransform()
684  */
685 
686 using DisplayGetSkipColorTransformTest = DisplayWithLayersTestCommon;
687 using aidl::android::hardware::graphics::composer3::DisplayCapability;
688 
TEST_F(DisplayGetSkipColorTransformTest,checksCapabilityIfGpuDisplay)689 TEST_F(DisplayGetSkipColorTransformTest, checksCapabilityIfGpuDisplay) {
690     EXPECT_CALL(mHwComposer, hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM))
691             .WillOnce(Return(true));
692     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
693     auto gpuDisplay{impl::createDisplay(mCompositionEngine, args)};
694     EXPECT_TRUE(gpuDisplay->getSkipColorTransform());
695 }
696 
TEST_F(DisplayGetSkipColorTransformTest,checksDisplayCapability)697 TEST_F(DisplayGetSkipColorTransformTest, checksDisplayCapability) {
698     EXPECT_CALL(mHwComposer,
699                 hasDisplayCapability(HalDisplayId(DEFAULT_DISPLAY_ID),
700                                      DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM))
701             .WillOnce(Return(true));
702     EXPECT_TRUE(mDisplay->getSkipColorTransform());
703 }
704 
705 /*
706  * Display::anyLayersRequireClientComposition()
707  */
708 
709 using DisplayAnyLayersRequireClientCompositionTest = DisplayWithLayersTestCommon;
710 
TEST_F(DisplayAnyLayersRequireClientCompositionTest,returnsFalse)711 TEST_F(DisplayAnyLayersRequireClientCompositionTest, returnsFalse) {
712     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(false));
713     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(false));
714     EXPECT_CALL(*mLayer3.outputLayer, requiresClientComposition()).WillOnce(Return(false));
715 
716     EXPECT_FALSE(mDisplay->anyLayersRequireClientComposition());
717 }
718 
TEST_F(DisplayAnyLayersRequireClientCompositionTest,returnsTrue)719 TEST_F(DisplayAnyLayersRequireClientCompositionTest, returnsTrue) {
720     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(false));
721     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(true));
722 
723     EXPECT_TRUE(mDisplay->anyLayersRequireClientComposition());
724 }
725 
726 /*
727  * Display::allLayersRequireClientComposition()
728  */
729 
730 using DisplayAllLayersRequireClientCompositionTest = DisplayWithLayersTestCommon;
731 
TEST_F(DisplayAllLayersRequireClientCompositionTest,returnsTrue)732 TEST_F(DisplayAllLayersRequireClientCompositionTest, returnsTrue) {
733     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(true));
734     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(true));
735     EXPECT_CALL(*mLayer3.outputLayer, requiresClientComposition()).WillOnce(Return(true));
736 
737     EXPECT_TRUE(mDisplay->allLayersRequireClientComposition());
738 }
739 
TEST_F(DisplayAllLayersRequireClientCompositionTest,returnsFalse)740 TEST_F(DisplayAllLayersRequireClientCompositionTest, returnsFalse) {
741     EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(true));
742     EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(false));
743 
744     EXPECT_FALSE(mDisplay->allLayersRequireClientComposition());
745 }
746 
747 /*
748  * Display::applyChangedTypesToLayers()
749  */
750 
751 using DisplayApplyChangedTypesToLayersTest = DisplayWithLayersTestCommon;
752 
TEST_F(DisplayApplyChangedTypesToLayersTest,takesEarlyOutIfNoChangedLayers)753 TEST_F(DisplayApplyChangedTypesToLayersTest, takesEarlyOutIfNoChangedLayers) {
754     mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes());
755 }
756 
TEST_F(DisplayApplyChangedTypesToLayersTest,appliesChanges)757 TEST_F(DisplayApplyChangedTypesToLayersTest, appliesChanges) {
758     EXPECT_CALL(*mLayer1.outputLayer, applyDeviceCompositionTypeChange(Composition::CLIENT))
759             .Times(1);
760     EXPECT_CALL(*mLayer2.outputLayer, applyDeviceCompositionTypeChange(Composition::DEVICE))
761             .Times(1);
762 
763     mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes{
764             {&mLayer1.hwc2Layer, Composition::CLIENT},
765             {&mLayer2.hwc2Layer, Composition::DEVICE},
766             {&hwc2LayerUnknown, Composition::SOLID_COLOR},
767     });
768 }
769 
770 /*
771  * Display::applyDisplayRequests()
772  */
773 
774 using DisplayApplyDisplayRequestsTest = DisplayWithLayersTestCommon;
775 
TEST_F(DisplayApplyDisplayRequestsTest,handlesNoRequests)776 TEST_F(DisplayApplyDisplayRequestsTest, handlesNoRequests) {
777     mDisplay->applyDisplayRequests(static_cast<hal::DisplayRequest>(0));
778 
779     auto& state = mDisplay->getState();
780     EXPECT_FALSE(state.flipClientTarget);
781 }
782 
TEST_F(DisplayApplyDisplayRequestsTest,handlesFlipClientTarget)783 TEST_F(DisplayApplyDisplayRequestsTest, handlesFlipClientTarget) {
784     mDisplay->applyDisplayRequests(hal::DisplayRequest::FLIP_CLIENT_TARGET);
785 
786     auto& state = mDisplay->getState();
787     EXPECT_TRUE(state.flipClientTarget);
788 }
789 
TEST_F(DisplayApplyDisplayRequestsTest,handlesWriteClientTargetToOutput)790 TEST_F(DisplayApplyDisplayRequestsTest, handlesWriteClientTargetToOutput) {
791     mDisplay->applyDisplayRequests(hal::DisplayRequest::WRITE_CLIENT_TARGET_TO_OUTPUT);
792 
793     auto& state = mDisplay->getState();
794     EXPECT_FALSE(state.flipClientTarget);
795 }
796 
TEST_F(DisplayApplyDisplayRequestsTest,handlesAllRequestFlagsSet)797 TEST_F(DisplayApplyDisplayRequestsTest, handlesAllRequestFlagsSet) {
798     mDisplay->applyDisplayRequests(static_cast<hal::DisplayRequest>(~0));
799 
800     auto& state = mDisplay->getState();
801     EXPECT_TRUE(state.flipClientTarget);
802 }
803 
804 /*
805  * Display::applyLayerRequestsToLayers()
806  */
807 
808 using DisplayApplyLayerRequestsToLayersTest = DisplayWithLayersTestCommon;
809 
TEST_F(DisplayApplyLayerRequestsToLayersTest,preparesAllLayers)810 TEST_F(DisplayApplyLayerRequestsToLayersTest, preparesAllLayers) {
811     EXPECT_CALL(*mLayer1.outputLayer, prepareForDeviceLayerRequests()).Times(1);
812     EXPECT_CALL(*mLayer2.outputLayer, prepareForDeviceLayerRequests()).Times(1);
813     EXPECT_CALL(*mLayer3.outputLayer, prepareForDeviceLayerRequests()).Times(1);
814 
815     mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests());
816 }
817 
TEST_F(DisplayApplyLayerRequestsToLayersTest,appliesDeviceLayerRequests)818 TEST_F(DisplayApplyLayerRequestsToLayersTest, appliesDeviceLayerRequests) {
819     EXPECT_CALL(*mLayer1.outputLayer, prepareForDeviceLayerRequests()).Times(1);
820     EXPECT_CALL(*mLayer2.outputLayer, prepareForDeviceLayerRequests()).Times(1);
821     EXPECT_CALL(*mLayer3.outputLayer, prepareForDeviceLayerRequests()).Times(1);
822 
823     EXPECT_CALL(*mLayer1.outputLayer,
824                 applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET))
825             .Times(1);
826 
827     mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests{
828             {&mLayer1.hwc2Layer, hal::LayerRequest::CLEAR_CLIENT_TARGET},
829             {&hwc2LayerUnknown, hal::LayerRequest::CLEAR_CLIENT_TARGET},
830     });
831 }
832 
833 /*
834  * Display::applyClientTargetRequests()
835  */
836 
837 using DisplayApplyClientTargetRequests = DisplayWithLayersTestCommon;
838 
TEST_F(DisplayApplyLayerRequestsToLayersTest,applyClientTargetRequests)839 TEST_F(DisplayApplyLayerRequestsToLayersTest, applyClientTargetRequests) {
840     static constexpr float kWhitePointNits = 800.f;
841 
842     Display::ClientTargetProperty clientTargetProperty = {
843             .clientTargetProperty =
844                     {
845                             .pixelFormat =
846                                     aidl::android::hardware::graphics::common::PixelFormat::RGB_565,
847                             .dataspace = aidl::android::hardware::graphics::common::Dataspace::
848                                     STANDARD_BT470M,
849                     },
850             .brightness = kWhitePointNits,
851             .dimmingStage = aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF,
852     };
853 
854     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
855     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
856 
857     EXPECT_CALL(*renderSurface,
858                 setBufferPixelFormat(static_cast<ui::PixelFormat>(
859                         clientTargetProperty.clientTargetProperty.pixelFormat)));
860     EXPECT_CALL(*renderSurface,
861                 setBufferDataspace(static_cast<ui::Dataspace>(
862                         clientTargetProperty.clientTargetProperty.dataspace)));
863     mDisplay->applyClientTargetRequests(clientTargetProperty);
864 
865     auto& state = mDisplay->getState();
866     EXPECT_EQ(clientTargetProperty.clientTargetProperty.dataspace,
867               static_cast<aidl::android::hardware::graphics::common::Dataspace>(state.dataspace));
868     EXPECT_EQ(kWhitePointNits, state.clientTargetBrightness);
869     EXPECT_EQ(aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF,
870               state.clientTargetDimmingStage);
871 }
872 
873 /*
874  * Display::presentFrame()
875  */
876 
877 using DisplayPresentAndGetFrameFencesTest = DisplayWithLayersTestCommon;
878 
TEST_F(DisplayPresentAndGetFrameFencesTest,returnsNoFencesOnGpuDisplay)879 TEST_F(DisplayPresentAndGetFrameFencesTest, returnsNoFencesOnGpuDisplay) {
880     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
881     auto gpuDisplay{impl::createDisplay(mCompositionEngine, args)};
882 
883     auto result = gpuDisplay->presentFrame();
884 
885     ASSERT_TRUE(result.presentFence.get());
886     EXPECT_FALSE(result.presentFence->isValid());
887     EXPECT_EQ(0u, result.layerFences.size());
888 }
889 
TEST_F(DisplayPresentAndGetFrameFencesTest,returnsPresentAndLayerFences)890 TEST_F(DisplayPresentAndGetFrameFencesTest, returnsPresentAndLayerFences) {
891     sp<Fence> presentFence = sp<Fence>::make();
892     sp<Fence> layer1Fence = sp<Fence>::make();
893     sp<Fence> layer2Fence = sp<Fence>::make();
894 
895     EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(HalDisplayId(DEFAULT_DISPLAY_ID), _))
896             .Times(1);
897     EXPECT_CALL(mHwComposer, getPresentFence(HalDisplayId(DEFAULT_DISPLAY_ID)))
898             .WillOnce(Return(presentFence));
899     EXPECT_CALL(mHwComposer,
900                 getLayerReleaseFence(HalDisplayId(DEFAULT_DISPLAY_ID), &mLayer1.hwc2Layer))
901             .WillOnce(Return(layer1Fence));
902     EXPECT_CALL(mHwComposer,
903                 getLayerReleaseFence(HalDisplayId(DEFAULT_DISPLAY_ID), &mLayer2.hwc2Layer))
904             .WillOnce(Return(layer2Fence));
905     EXPECT_CALL(mHwComposer, clearReleaseFences(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(1);
906 
907     auto result = mDisplay->presentFrame();
908 
909     EXPECT_EQ(presentFence, result.presentFence);
910 
911     EXPECT_EQ(2u, result.layerFences.size());
912     ASSERT_EQ(1u, result.layerFences.count(&mLayer1.hwc2Layer));
913     EXPECT_EQ(layer1Fence, result.layerFences[&mLayer1.hwc2Layer]);
914     ASSERT_EQ(1u, result.layerFences.count(&mLayer2.hwc2Layer));
915     EXPECT_EQ(layer2Fence, result.layerFences[&mLayer2.hwc2Layer]);
916 }
917 
918 /*
919  * Display::setExpensiveRenderingExpected()
920  */
921 
922 using DisplaySetExpensiveRenderingExpectedTest = DisplayWithLayersTestCommon;
923 
TEST_F(DisplaySetExpensiveRenderingExpectedTest,forwardsToPowerAdvisor)924 TEST_F(DisplaySetExpensiveRenderingExpectedTest, forwardsToPowerAdvisor) {
925     EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, true)).Times(1);
926     mDisplay->setExpensiveRenderingExpected(true);
927 
928     EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false)).Times(1);
929     mDisplay->setExpensiveRenderingExpected(false);
930 }
931 
932 /*
933  * Display::finishFrame()
934  */
935 
936 using DisplayFinishFrameTest = DisplayWithLayersTestCommon;
937 
TEST_F(DisplayFinishFrameTest,doesNotSkipCompositionIfNotDirtyOnHwcDisplay)938 TEST_F(DisplayFinishFrameTest, doesNotSkipCompositionIfNotDirtyOnHwcDisplay) {
939     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
940     mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
941 
942     // We expect no calls to queueBuffer if composition was skipped.
943     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(1);
944 
945     // Expect a call to signal no expensive rendering since there is no client composition.
946     EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false));
947 
948     mDisplay->editState().isEnabled = true;
949     mDisplay->editState().usesClientComposition = false;
950     mDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
951     mDisplay->editState().dirtyRegion = Region::INVALID_REGION;
952 
953     mDisplay->finishFrame(std::move(mResultWithBuffer));
954 }
955 
TEST_F(DisplayFinishFrameTest,skipsCompositionIfNotDirty)956 TEST_F(DisplayFinishFrameTest, skipsCompositionIfNotDirty) {
957     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
958     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
959 
960     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
961     gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
962 
963     // We expect no calls to queueBuffer if composition was skipped.
964     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(0);
965     EXPECT_CALL(*renderSurface, beginFrame(false));
966 
967     gpuDisplay->editState().isEnabled = true;
968     gpuDisplay->editState().usesClientComposition = false;
969     gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
970     gpuDisplay->editState().dirtyRegion = Region::INVALID_REGION;
971     gpuDisplay->editState().lastCompositionHadVisibleLayers = true;
972 
973     gpuDisplay->beginFrame();
974     gpuDisplay->finishFrame(std::move(mResultWithoutBuffer));
975 }
976 
TEST_F(DisplayFinishFrameTest,skipsCompositionIfEmpty)977 TEST_F(DisplayFinishFrameTest, skipsCompositionIfEmpty) {
978     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
979     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
980 
981     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
982     gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
983 
984     // We expect no calls to queueBuffer if composition was skipped.
985     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(0);
986     EXPECT_CALL(*renderSurface, beginFrame(false));
987 
988     gpuDisplay->editState().isEnabled = true;
989     gpuDisplay->editState().usesClientComposition = false;
990     gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
991     gpuDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
992     gpuDisplay->editState().lastCompositionHadVisibleLayers = false;
993 
994     gpuDisplay->beginFrame();
995     gpuDisplay->finishFrame(std::move(mResultWithoutBuffer));
996 }
997 
TEST_F(DisplayFinishFrameTest,performsCompositionIfDirtyAndNotEmpty)998 TEST_F(DisplayFinishFrameTest, performsCompositionIfDirtyAndNotEmpty) {
999     auto args = getDisplayCreationArgsForGpuVirtualDisplay();
1000     std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
1001 
1002     mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
1003     gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
1004 
1005     // We expect a single call to queueBuffer when composition is not skipped.
1006     EXPECT_CALL(*renderSurface, queueBuffer(_, _)).Times(1);
1007     EXPECT_CALL(*renderSurface, beginFrame(true));
1008 
1009     gpuDisplay->editState().isEnabled = true;
1010     gpuDisplay->editState().usesClientComposition = false;
1011     gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
1012     gpuDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
1013     gpuDisplay->editState().lastCompositionHadVisibleLayers = true;
1014 
1015     gpuDisplay->beginFrame();
1016     gpuDisplay->finishFrame(std::move(mResultWithBuffer));
1017 }
1018 
1019 /*
1020  * Display functional tests
1021  */
1022 
1023 struct DisplayFunctionalTest : public testing::Test {
1024     class Display : public impl::Display {
1025     public:
1026         using impl::Display::injectOutputLayerForTest;
1027         virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
1028     };
1029 
DisplayFunctionalTestandroid::compositionengine::__anon67b74f4f0111::DisplayFunctionalTest1030     DisplayFunctionalTest() {
1031         EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
1032         mDisplay = createDisplay();
1033         mRenderSurface = createRenderSurface();
1034         mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
1035     }
1036 
1037     NiceMock<android::mock::HWComposer> mHwComposer;
1038     NiceMock<adpf::mock::PowerAdvisor> mPowerAdvisor;
1039     NiceMock<mock::CompositionEngine> mCompositionEngine;
1040     sp<mock::NativeWindow> mNativeWindow = sp<NiceMock<mock::NativeWindow>>::make();
1041     sp<mock::DisplaySurface> mDisplaySurface = sp<NiceMock<mock::DisplaySurface>>::make();
1042     std::shared_ptr<Display> mDisplay;
1043     impl::RenderSurface* mRenderSurface;
1044 
createDisplayandroid::compositionengine::__anon67b74f4f0111::DisplayFunctionalTest1045     std::shared_ptr<Display> createDisplay() {
1046         return impl::createDisplayTemplated<Display>(mCompositionEngine,
1047                                                      DisplayCreationArgsBuilder()
1048                                                              .setId(DEFAULT_DISPLAY_ID)
1049                                                              .setPixels(DEFAULT_RESOLUTION)
1050                                                              .setIsSecure(true)
1051                                                              .setPowerAdvisor(&mPowerAdvisor)
1052                                                              .build());
1053         ;
1054     }
1055 
createRenderSurfaceandroid::compositionengine::__anon67b74f4f0111::DisplayFunctionalTest1056     impl::RenderSurface* createRenderSurface() {
1057         return new impl::RenderSurface{mCompositionEngine, *mDisplay,
1058                                        RenderSurfaceCreationArgsBuilder()
1059                                                .setDisplayWidth(DEFAULT_RESOLUTION.width)
1060                                                .setDisplayHeight(DEFAULT_RESOLUTION.height)
1061                                                .setNativeWindow(mNativeWindow)
1062                                                .setDisplaySurface(mDisplaySurface)
1063                                                .build()};
1064     }
1065 };
1066 
TEST_F(DisplayFunctionalTest,presentFrameAndReleaseLayersCriticalCallsAreOrdered)1067 TEST_F(DisplayFunctionalTest, presentFrameAndReleaseLayersCriticalCallsAreOrdered) {
1068     InSequence seq;
1069 
1070     mDisplay->editState().isEnabled = true;
1071 
1072     EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(_, _));
1073     EXPECT_CALL(*mDisplaySurface, onFrameCommitted());
1074     constexpr bool kFlushEvenWhenDisabled = false;
1075     mDisplay->presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
1076 }
1077 
1078 } // namespace
1079 } // namespace android::compositionengine
1080