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