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