blob: 3a4df7404a429498ee5ea27b8d3b6bbbc08464ad [file] [log] [blame]
Lloyd Pique45a165a2018-10-19 11:54:47 -07001/*
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
Lloyd Pique45a165a2018-10-19 11:54:47 -070017#include <cmath>
18
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070019#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070020#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070021#include <compositionengine/DisplaySurface.h>
22#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070023#include <compositionengine/impl/Display.h>
Lloyd Piquec6607552019-12-02 17:57:39 -080024#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070025#include <compositionengine/mock/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080026#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Piquec6607552019-12-02 17:57:39 -080027#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080028#include <compositionengine/mock/Layer.h>
29#include <compositionengine/mock/LayerFE.h>
chaviw8beb4142019-04-11 13:09:05 -070030#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080031#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070032#include <compositionengine/mock/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070033#include <gtest/gtest.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070034
Lloyd Pique66d68602019-02-13 14:23:31 -080035#include "MockHWC2.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070036#include "MockHWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080037#include "MockPowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070038
39namespace android::compositionengine {
40namespace {
41
Lloyd Piquef5275482019-01-29 18:42:42 -080042using testing::_;
Lloyd Pique66d68602019-02-13 14:23:31 -080043using testing::DoAll;
Lloyd Piquec6607552019-12-02 17:57:39 -080044using testing::InSequence;
45using testing::NiceMock;
Lloyd Pique31cb2942018-10-19 17:23:03 -070046using testing::Return;
Lloyd Pique45a165a2018-10-19 11:54:47 -070047using testing::ReturnRef;
Lloyd Pique66d68602019-02-13 14:23:31 -080048using testing::Sequence;
49using testing::SetArgPointee;
Lloyd Pique45a165a2018-10-19 11:54:47 -070050using testing::StrictMock;
51
52constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42};
Lloyd Piquec6607552019-12-02 17:57:39 -080053constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
54constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
Lloyd Pique45a165a2018-10-19 11:54:47 -070055
Lloyd Pique66d68602019-02-13 14:23:31 -080056struct DisplayTest : public testing::Test {
Lloyd Pique01c77c12019-04-17 12:48:32 -070057 class Display : public impl::Display {
58 public:
59 explicit Display(const compositionengine::DisplayCreationArgs& args)
60 : impl::Display(args) {}
61
62 using impl::Display::injectOutputLayerForTest;
63 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
64 };
65
66 static std::shared_ptr<Display> createDisplay(
67 const compositionengine::CompositionEngine& compositionEngine,
68 compositionengine::DisplayCreationArgs&& args) {
69 return impl::createDisplayTemplated<Display>(compositionEngine, args);
70 }
71
Lloyd Pique66d68602019-02-13 14:23:31 -080072 DisplayTest() {
73 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
74 EXPECT_CALL(*mLayer1, getHwcLayer()).WillRepeatedly(Return(&mHWC2Layer1));
75 EXPECT_CALL(*mLayer2, getHwcLayer()).WillRepeatedly(Return(&mHWC2Layer2));
76 EXPECT_CALL(*mLayer3, getHwcLayer()).WillRepeatedly(Return(nullptr));
77
Lloyd Pique01c77c12019-04-17 12:48:32 -070078 mDisplay->injectOutputLayerForTest(
79 std::unique_ptr<compositionengine::OutputLayer>(mLayer1));
80 mDisplay->injectOutputLayerForTest(
81 std::unique_ptr<compositionengine::OutputLayer>(mLayer2));
82 mDisplay->injectOutputLayerForTest(
83 std::unique_ptr<compositionengine::OutputLayer>(mLayer3));
Lloyd Pique66d68602019-02-13 14:23:31 -080084 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070085
86 StrictMock<android::mock::HWComposer> mHwComposer;
Lloyd Pique688abd42019-02-15 15:42:24 -080087 StrictMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
Lloyd Pique45a165a2018-10-19 11:54:47 -070088 StrictMock<mock::CompositionEngine> mCompositionEngine;
chaviw8beb4142019-04-11 13:09:05 -070089 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
Lloyd Pique66d68602019-02-13 14:23:31 -080090 StrictMock<HWC2::mock::Layer> mHWC2Layer1;
91 StrictMock<HWC2::mock::Layer> mHWC2Layer2;
92 StrictMock<HWC2::mock::Layer> mHWC2LayerUnknown;
93 mock::OutputLayer* mLayer1 = new StrictMock<mock::OutputLayer>();
94 mock::OutputLayer* mLayer2 = new StrictMock<mock::OutputLayer>();
95 mock::OutputLayer* mLayer3 = new StrictMock<mock::OutputLayer>();
Lloyd Pique01c77c12019-04-17 12:48:32 -070096 std::shared_ptr<Display> mDisplay = createDisplay(mCompositionEngine,
97 DisplayCreationArgsBuilder()
98 .setDisplayId(DEFAULT_DISPLAY_ID)
99 .setPowerAdvisor(&mPowerAdvisor)
100 .build());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700101};
102
Lloyd Pique66d68602019-02-13 14:23:31 -0800103/*
Lloyd Pique45a165a2018-10-19 11:54:47 -0700104 * Basic construction
105 */
106
107TEST_F(DisplayTest, canInstantiateDisplay) {
108 {
109 constexpr DisplayId display1 = DisplayId{123u};
110 auto display =
111 impl::createDisplay(mCompositionEngine,
112 DisplayCreationArgsBuilder().setDisplayId(display1).build());
113 EXPECT_FALSE(display->isSecure());
114 EXPECT_FALSE(display->isVirtual());
115 EXPECT_EQ(display1, display->getId());
116 }
117
118 {
119 constexpr DisplayId display2 = DisplayId{546u};
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700120 auto display =
121 impl::createDisplay(mCompositionEngine,
122 DisplayCreationArgsBuilder().setDisplayId(display2).build());
123 EXPECT_FALSE(display->isSecure());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700124 EXPECT_FALSE(display->isVirtual());
125 EXPECT_EQ(display2, display->getId());
126 }
127
128 {
129 constexpr DisplayId display3 = DisplayId{789u};
130 auto display = impl::createDisplay(mCompositionEngine,
131 DisplayCreationArgsBuilder()
132 .setIsVirtual(true)
133 .setDisplayId(display3)
134 .build());
135 EXPECT_FALSE(display->isSecure());
136 EXPECT_TRUE(display->isVirtual());
137 EXPECT_EQ(display3, display->getId());
138 }
139}
140
Lloyd Pique66d68602019-02-13 14:23:31 -0800141/*
Lloyd Pique45a165a2018-10-19 11:54:47 -0700142 * Display::disconnect()
143 */
144
145TEST_F(DisplayTest, disconnectDisconnectsDisplay) {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700146 // The first call to disconnect will disconnect the display with the HWC and
147 // set mHwcId to -1.
148 EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700149 mDisplay->disconnect();
150 EXPECT_FALSE(mDisplay->getId());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700151
152 // Subsequent calls will do nothing,
153 EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(0);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700154 mDisplay->disconnect();
155 EXPECT_FALSE(mDisplay->getId());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700156}
157
Lloyd Pique66d68602019-02-13 14:23:31 -0800158/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700159 * Display::setColorTransform()
160 */
161
162TEST_F(DisplayTest, setColorTransformSetsTransform) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800163 // No change does nothing
164 CompositionRefreshArgs refreshArgs;
165 refreshArgs.colorTransformMatrix = std::nullopt;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700166 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800167
Lloyd Pique32cbe282018-10-19 13:09:22 -0700168 // Identity matrix sets an identity state value
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800169 const mat4 kIdentity;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700170
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800171 EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, kIdentity)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700172
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800173 refreshArgs.colorTransformMatrix = kIdentity;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700174 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700175
176 // Non-identity matrix sets a non-identity state value
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800177 const mat4 kNonIdentity = mat4() * 2;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700178
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800179 EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, kNonIdentity)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700180
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800181 refreshArgs.colorTransformMatrix = kNonIdentity;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700182 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700183}
184
Lloyd Pique66d68602019-02-13 14:23:31 -0800185/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700186 * Display::setColorMode()
187 */
188
189TEST_F(DisplayTest, setColorModeSetsModeUnlessNoChange) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800190 using ColorProfile = Output::ColorProfile;
191
Lloyd Pique31cb2942018-10-19 17:23:03 -0700192 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700193 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piquef5275482019-01-29 18:42:42 -0800194 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700195 mDisplay->setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196
Lloyd Piquef5275482019-01-29 18:42:42 -0800197 EXPECT_CALL(*colorProfile, getTargetDataspace(_, _, _))
198 .WillRepeatedly(Return(ui::Dataspace::UNKNOWN));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700199
200 // These values are expected to be the initial state.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700201 ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
202 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
203 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
204 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700205
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700206 // Otherwise if the values are unchanged, nothing happens
207 mDisplay->setColorProfile(ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
208 ui::RenderIntent::COLORIMETRIC, ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700209
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700210 EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
211 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
212 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
213 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700214
215 // Otherwise if the values are different, updates happen
Lloyd Piqueef958122019-02-05 18:00:12 -0800216 EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700217 EXPECT_CALL(mHwComposer,
Lloyd Piqueef958122019-02-05 18:00:12 -0800218 setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700219 ui::RenderIntent::TONE_MAP_COLORIMETRIC))
220 .Times(1);
221
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700222 mDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
223 ui::RenderIntent::TONE_MAP_COLORIMETRIC,
224 ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700225
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700226 EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay->getState().colorMode);
227 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay->getState().dataspace);
228 EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay->getState().renderIntent);
229 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700230}
231
232TEST_F(DisplayTest, setColorModeDoesNothingForVirtualDisplay) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800233 using ColorProfile = Output::ColorProfile;
234
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700235 std::shared_ptr<impl::Display> virtualDisplay{
236 impl::createDisplay(mCompositionEngine, DisplayCreationArgs{true, DEFAULT_DISPLAY_ID})};
Lloyd Pique32cbe282018-10-19 13:09:22 -0700237
Lloyd Piquef5275482019-01-29 18:42:42 -0800238 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700239 virtualDisplay->setDisplayColorProfileForTest(
Lloyd Piquef5275482019-01-29 18:42:42 -0800240 std::unique_ptr<DisplayColorProfile>(colorProfile));
241
242 EXPECT_CALL(*colorProfile,
243 getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
244 ui::Dataspace::UNKNOWN))
245 .WillOnce(Return(ui::Dataspace::UNKNOWN));
246
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700247 virtualDisplay->setColorProfile(
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800248 ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
249 ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700250
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay->getState().colorMode);
252 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().dataspace);
253 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay->getState().renderIntent);
254 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255}
256
Lloyd Pique66d68602019-02-13 14:23:31 -0800257/*
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700258 * Display::createDisplayColorProfile()
259 */
260
261TEST_F(DisplayTest, createDisplayColorProfileSetsDisplayColorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 EXPECT_TRUE(mDisplay->getDisplayColorProfile() == nullptr);
263 mDisplay->createDisplayColorProfile(
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700264 DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
265 DisplayColorProfileCreationArgs::HwcColorModes()});
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700266 EXPECT_TRUE(mDisplay->getDisplayColorProfile() != nullptr);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700267}
268
Lloyd Pique66d68602019-02-13 14:23:31 -0800269/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700270 * Display::createRenderSurface()
271 */
272
273TEST_F(DisplayTest, createRenderSurfaceSetsRenderSurface) {
chaviw8beb4142019-04-11 13:09:05 -0700274 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700275 EXPECT_TRUE(mDisplay->getRenderSurface() == nullptr);
276 mDisplay->createRenderSurface(RenderSurfaceCreationArgs{640, 480, mNativeWindow, nullptr});
277 EXPECT_TRUE(mDisplay->getRenderSurface() != nullptr);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700278}
279
Lloyd Pique66d68602019-02-13 14:23:31 -0800280/*
Lloyd Piquedf336d92019-03-07 21:38:42 -0800281 * Display::createOutputLayer()
282 */
283
284TEST_F(DisplayTest, createOutputLayerSetsHwcLayer) {
285 sp<mock::LayerFE> layerFE = new StrictMock<mock::LayerFE>();
286 auto layer = std::make_shared<StrictMock<mock::Layer>>();
287 StrictMock<HWC2::mock::Layer> hwcLayer;
288
289 EXPECT_CALL(mHwComposer, createLayer(DEFAULT_DISPLAY_ID)).WillOnce(Return(&hwcLayer));
290
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700291 auto outputLayer = mDisplay->createOutputLayer(layer, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800292
293 EXPECT_EQ(&hwcLayer, outputLayer->getHwcLayer());
294
295 EXPECT_CALL(mHwComposer, destroyLayer(DEFAULT_DISPLAY_ID, &hwcLayer));
296 outputLayer.reset();
297}
298
299/*
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800300 * Display::setReleasedLayers()
301 */
302
303TEST_F(DisplayTest, setReleasedLayersDoesNothingIfNotHwcDisplay) {
304 std::shared_ptr<impl::Display> nonHwcDisplay{
305 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
306
307 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
308 mock::Layer layerXLayer;
309
310 {
311 Output::ReleasedLayers releasedLayers;
312 releasedLayers.emplace_back(layerXLayerFE);
313 nonHwcDisplay->setReleasedLayers(std::move(releasedLayers));
314 }
315
316 CompositionRefreshArgs refreshArgs;
317 refreshArgs.layersWithQueuedFrames.push_back(&layerXLayer);
318
319 nonHwcDisplay->setReleasedLayers(refreshArgs);
320
321 const auto& releasedLayers = nonHwcDisplay->getReleasedLayersForTest();
322 ASSERT_EQ(1, releasedLayers.size());
323}
324
325TEST_F(DisplayTest, setReleasedLayersDoesNothingIfNoLayersWithQueuedFrames) {
326 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
327
328 {
329 Output::ReleasedLayers releasedLayers;
330 releasedLayers.emplace_back(layerXLayerFE);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700331 mDisplay->setReleasedLayers(std::move(releasedLayers));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800332 }
333
334 CompositionRefreshArgs refreshArgs;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700335 mDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800336
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700337 const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800338 ASSERT_EQ(1, releasedLayers.size());
339}
340
341TEST_F(DisplayTest, setReleasedLayers) {
342 sp<mock::LayerFE> layer1LayerFE = new StrictMock<mock::LayerFE>();
343 sp<mock::LayerFE> layer2LayerFE = new StrictMock<mock::LayerFE>();
344 sp<mock::LayerFE> layer3LayerFE = new StrictMock<mock::LayerFE>();
345 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
346 mock::Layer layer1Layer;
347 mock::Layer layer2Layer;
348 mock::Layer layer3Layer;
349 mock::Layer layerXLayer;
350
351 EXPECT_CALL(*mLayer1, getLayer()).WillRepeatedly(ReturnRef(layer1Layer));
352 EXPECT_CALL(*mLayer1, getLayerFE()).WillRepeatedly(ReturnRef(*layer1LayerFE.get()));
353 EXPECT_CALL(*mLayer2, getLayer()).WillRepeatedly(ReturnRef(layer2Layer));
354 EXPECT_CALL(*mLayer2, getLayerFE()).WillRepeatedly(ReturnRef(*layer2LayerFE.get()));
355 EXPECT_CALL(*mLayer3, getLayer()).WillRepeatedly(ReturnRef(layer3Layer));
356 EXPECT_CALL(*mLayer3, getLayerFE()).WillRepeatedly(ReturnRef(*layer3LayerFE.get()));
357
358 CompositionRefreshArgs refreshArgs;
359 refreshArgs.layersWithQueuedFrames.push_back(&layer1Layer);
360 refreshArgs.layersWithQueuedFrames.push_back(&layer2Layer);
361 refreshArgs.layersWithQueuedFrames.push_back(&layerXLayer);
362 refreshArgs.layersWithQueuedFrames.push_back(nullptr);
363
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700364 mDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800365
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700366 const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800367 ASSERT_EQ(2, releasedLayers.size());
368 ASSERT_EQ(layer1LayerFE.get(), releasedLayers[0].promote().get());
369 ASSERT_EQ(layer2LayerFE.get(), releasedLayers[1].promote().get());
370}
371
372/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800373 * Display::chooseCompositionStrategy()
374 */
375
376struct DisplayChooseCompositionStrategyTest : public testing::Test {
377 struct DisplayPartialMock : public impl::Display {
378 DisplayPartialMock(const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700379 const compositionengine::DisplayCreationArgs& args)
380 : impl::Display(args), mCompositionEngine(compositionEngine) {}
Lloyd Pique66d68602019-02-13 14:23:31 -0800381
382 // Sets up the helper functions called by chooseCompositionStrategy to
383 // use a mock implementations.
384 MOCK_CONST_METHOD0(anyLayersRequireClientComposition, bool());
385 MOCK_CONST_METHOD0(allLayersRequireClientComposition, bool());
386 MOCK_METHOD1(applyChangedTypesToLayers, void(const impl::Display::ChangedTypes&));
387 MOCK_METHOD1(applyDisplayRequests, void(const impl::Display::DisplayRequests&));
388 MOCK_METHOD1(applyLayerRequestsToLayers, void(const impl::Display::LayerRequests&));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700389
390 // compositionengine::Output overrides
391 const OutputCompositionState& getState() const override { return mState; }
392 OutputCompositionState& editState() override { return mState; }
393
394 // compositionengine::impl::Output overrides
395 const CompositionEngine& getCompositionEngine() const override {
396 return mCompositionEngine;
397 };
398
399 // These need implementations though are not expected to be called.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700400 MOCK_CONST_METHOD0(getOutputLayerCount, size_t());
401 MOCK_CONST_METHOD1(getOutputLayerOrderedByZByIndex,
402 compositionengine::OutputLayer*(size_t));
403 MOCK_METHOD3(ensureOutputLayer,
404 compositionengine::OutputLayer*(
405 std::optional<size_t>,
406 const std::shared_ptr<compositionengine::Layer>&, const sp<LayerFE>&));
407 MOCK_METHOD0(finalizePendingOutputLayers, void());
408 MOCK_METHOD0(clearOutputLayers, void());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700409 MOCK_CONST_METHOD1(dumpState, void(std::string&));
Lloyd Pique01c77c12019-04-17 12:48:32 -0700410 MOCK_METHOD2(injectOutputLayerForTest,
411 compositionengine::OutputLayer*(
412 const std::shared_ptr<compositionengine::Layer>&, const sp<LayerFE>&));
413 MOCK_METHOD1(injectOutputLayerForTest, void(std::unique_ptr<OutputLayer>));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700414
415 const compositionengine::CompositionEngine& mCompositionEngine;
416 impl::OutputCompositionState mState;
Lloyd Pique66d68602019-02-13 14:23:31 -0800417 };
418
419 DisplayChooseCompositionStrategyTest() {
420 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
421 }
422
423 StrictMock<android::mock::HWComposer> mHwComposer;
424 StrictMock<mock::CompositionEngine> mCompositionEngine;
425 StrictMock<DisplayPartialMock>
426 mDisplay{mCompositionEngine,
427 DisplayCreationArgsBuilder().setDisplayId(DEFAULT_DISPLAY_ID).build()};
428};
429
430TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutIfNotAHwcDisplay) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700431 std::shared_ptr<impl::Display> nonHwcDisplay{
432 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
433 EXPECT_FALSE(nonHwcDisplay->getId());
Lloyd Pique66d68602019-02-13 14:23:31 -0800434
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700435 nonHwcDisplay->chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800436
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 auto& state = nonHwcDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800438 EXPECT_TRUE(state.usesClientComposition);
439 EXPECT_FALSE(state.usesDeviceComposition);
440}
441
442TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutOnHwcError) {
443 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).WillOnce(Return(false));
444 EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, false, _))
445 .WillOnce(Return(INVALID_OPERATION));
446
447 mDisplay.chooseCompositionStrategy();
448
449 auto& state = mDisplay.getState();
450 EXPECT_TRUE(state.usesClientComposition);
451 EXPECT_FALSE(state.usesDeviceComposition);
452}
453
454TEST_F(DisplayChooseCompositionStrategyTest, normalOperation) {
455 // Since two calls are made to anyLayersRequireClientComposition with different return values,
456 // use a Sequence to control the matching so the values are returned in a known order.
457 Sequence s;
458 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).InSequence(s).WillOnce(Return(true));
459 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition())
460 .InSequence(s)
461 .WillOnce(Return(false));
462
463 EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, true, _))
464 .WillOnce(Return(NO_ERROR));
465 EXPECT_CALL(mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
466
467 mDisplay.chooseCompositionStrategy();
468
469 auto& state = mDisplay.getState();
470 EXPECT_FALSE(state.usesClientComposition);
471 EXPECT_TRUE(state.usesDeviceComposition);
472}
473
474TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithChanges) {
475 android::HWComposer::DeviceRequestedChanges changes{
476 {{nullptr, HWC2::Composition::Client}},
477 HWC2::DisplayRequest::FlipClientTarget,
478 {{nullptr, HWC2::LayerRequest::ClearClientTarget}},
479 };
480
481 // Since two calls are made to anyLayersRequireClientComposition with different return values,
482 // use a Sequence to control the matching so the values are returned in a known order.
483 Sequence s;
484 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).InSequence(s).WillOnce(Return(true));
485 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition())
486 .InSequence(s)
487 .WillOnce(Return(false));
488
489 EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, true, _))
490 .WillOnce(DoAll(SetArgPointee<2>(changes), Return(NO_ERROR)));
491 EXPECT_CALL(mDisplay, applyChangedTypesToLayers(changes.changedTypes)).Times(1);
492 EXPECT_CALL(mDisplay, applyDisplayRequests(changes.displayRequests)).Times(1);
493 EXPECT_CALL(mDisplay, applyLayerRequestsToLayers(changes.layerRequests)).Times(1);
494 EXPECT_CALL(mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
495
496 mDisplay.chooseCompositionStrategy();
497
498 auto& state = mDisplay.getState();
499 EXPECT_FALSE(state.usesClientComposition);
500 EXPECT_TRUE(state.usesDeviceComposition);
501}
502
503/*
Lloyd Pique688abd42019-02-15 15:42:24 -0800504 * Display::getSkipColorTransform()
505 */
506
507TEST_F(DisplayTest, getSkipColorTransformDoesNothingIfNonHwcDisplay) {
508 auto nonHwcDisplay{
509 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
510 EXPECT_FALSE(nonHwcDisplay->getSkipColorTransform());
511}
512
513TEST_F(DisplayTest, getSkipColorTransformChecksHwcCapability) {
514 EXPECT_CALL(mHwComposer,
515 hasDisplayCapability(std::make_optional(DEFAULT_DISPLAY_ID),
516 HWC2::DisplayCapability::SkipClientColorTransform))
517 .WillOnce(Return(true));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700518 EXPECT_TRUE(mDisplay->getSkipColorTransform());
Lloyd Pique688abd42019-02-15 15:42:24 -0800519}
520
521/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800522 * Display::anyLayersRequireClientComposition()
523 */
524
525TEST_F(DisplayTest, anyLayersRequireClientCompositionReturnsFalse) {
526 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(false));
527 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(false));
528 EXPECT_CALL(*mLayer3, requiresClientComposition()).WillOnce(Return(false));
529
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700530 EXPECT_FALSE(mDisplay->anyLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800531}
532
533TEST_F(DisplayTest, anyLayersRequireClientCompositionReturnsTrue) {
534 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(false));
535 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(true));
536
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700537 EXPECT_TRUE(mDisplay->anyLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800538}
539
540/*
541 * Display::allLayersRequireClientComposition()
542 */
543
544TEST_F(DisplayTest, allLayersRequireClientCompositionReturnsTrue) {
545 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(true));
546 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(true));
547 EXPECT_CALL(*mLayer3, requiresClientComposition()).WillOnce(Return(true));
548
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700549 EXPECT_TRUE(mDisplay->allLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800550}
551
552TEST_F(DisplayTest, allLayersRequireClientCompositionReturnsFalse) {
553 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(true));
554 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(false));
555
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700556 EXPECT_FALSE(mDisplay->allLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800557}
558
559/*
560 * Display::applyChangedTypesToLayers()
561 */
562
563TEST_F(DisplayTest, applyChangedTypesToLayersTakesEarlyOutIfNoChangedLayers) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700564 mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes());
Lloyd Pique66d68602019-02-13 14:23:31 -0800565}
566
567TEST_F(DisplayTest, applyChangedTypesToLayersAppliesChanges) {
568 EXPECT_CALL(*mLayer1,
569 applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::CLIENT))
570 .Times(1);
571 EXPECT_CALL(*mLayer2,
572 applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::DEVICE))
573 .Times(1);
574
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700575 mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes{
Lloyd Pique66d68602019-02-13 14:23:31 -0800576 {&mHWC2Layer1, HWC2::Composition::Client},
577 {&mHWC2Layer2, HWC2::Composition::Device},
578 {&mHWC2LayerUnknown, HWC2::Composition::SolidColor},
579 });
580}
581
582/*
583 * Display::applyDisplayRequests()
584 */
585
586TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesNoRequests) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700587 mDisplay->applyDisplayRequests(static_cast<HWC2::DisplayRequest>(0));
Lloyd Pique66d68602019-02-13 14:23:31 -0800588
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700589 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800590 EXPECT_FALSE(state.flipClientTarget);
591}
592
593TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesFlipClientTarget) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700594 mDisplay->applyDisplayRequests(HWC2::DisplayRequest::FlipClientTarget);
Lloyd Pique66d68602019-02-13 14:23:31 -0800595
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700596 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800597 EXPECT_TRUE(state.flipClientTarget);
598}
599
600TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesWriteClientTargetToOutput) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700601 mDisplay->applyDisplayRequests(HWC2::DisplayRequest::WriteClientTargetToOutput);
Lloyd Pique66d68602019-02-13 14:23:31 -0800602
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700603 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800604 EXPECT_FALSE(state.flipClientTarget);
605}
606
607TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesAllRequestFlagsSet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700608 mDisplay->applyDisplayRequests(static_cast<HWC2::DisplayRequest>(~0));
Lloyd Pique66d68602019-02-13 14:23:31 -0800609
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700610 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800611 EXPECT_TRUE(state.flipClientTarget);
612}
613
614/*
615 * Display::applyLayerRequestsToLayers()
616 */
617
618TEST_F(DisplayTest, applyLayerRequestsToLayersPreparesAllLayers) {
619 EXPECT_CALL(*mLayer1, prepareForDeviceLayerRequests()).Times(1);
620 EXPECT_CALL(*mLayer2, prepareForDeviceLayerRequests()).Times(1);
621 EXPECT_CALL(*mLayer3, prepareForDeviceLayerRequests()).Times(1);
622
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700623 mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests());
Lloyd Pique66d68602019-02-13 14:23:31 -0800624}
625
626TEST_F(DisplayTest, applyLayerRequestsToLayers2) {
627 EXPECT_CALL(*mLayer1, prepareForDeviceLayerRequests()).Times(1);
628 EXPECT_CALL(*mLayer2, prepareForDeviceLayerRequests()).Times(1);
629 EXPECT_CALL(*mLayer3, prepareForDeviceLayerRequests()).Times(1);
630
631 EXPECT_CALL(*mLayer1,
632 applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET))
633 .Times(1);
634
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700635 mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests{
Lloyd Pique66d68602019-02-13 14:23:31 -0800636 {&mHWC2Layer1, HWC2::LayerRequest::ClearClientTarget},
637 {&mHWC2LayerUnknown, HWC2::LayerRequest::ClearClientTarget},
638 });
639}
640
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800641/*
642 * Display::presentAndGetFrameFences()
643 */
644
645TEST_F(DisplayTest, presentAndGetFrameFencesReturnsNoFencesOnNonHwcDisplay) {
646 auto nonHwcDisplay{
647 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
648
649 auto result = nonHwcDisplay->presentAndGetFrameFences();
650
651 ASSERT_TRUE(result.presentFence.get());
652 EXPECT_FALSE(result.presentFence->isValid());
653 EXPECT_EQ(0u, result.layerFences.size());
654}
655
656TEST_F(DisplayTest, presentAndGetFrameFencesReturnsPresentAndLayerFences) {
657 sp<Fence> presentFence = new Fence();
658 sp<Fence> layer1Fence = new Fence();
659 sp<Fence> layer2Fence = new Fence();
660
661 EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(DEFAULT_DISPLAY_ID)).Times(1);
662 EXPECT_CALL(mHwComposer, getPresentFence(DEFAULT_DISPLAY_ID)).WillOnce(Return(presentFence));
663 EXPECT_CALL(mHwComposer, getLayerReleaseFence(DEFAULT_DISPLAY_ID, &mHWC2Layer1))
664 .WillOnce(Return(layer1Fence));
665 EXPECT_CALL(mHwComposer, getLayerReleaseFence(DEFAULT_DISPLAY_ID, &mHWC2Layer2))
666 .WillOnce(Return(layer2Fence));
667 EXPECT_CALL(mHwComposer, clearReleaseFences(DEFAULT_DISPLAY_ID)).Times(1);
668
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700669 auto result = mDisplay->presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800670
671 EXPECT_EQ(presentFence, result.presentFence);
672
673 EXPECT_EQ(2u, result.layerFences.size());
674 ASSERT_EQ(1, result.layerFences.count(&mHWC2Layer1));
675 EXPECT_EQ(layer1Fence, result.layerFences[&mHWC2Layer1]);
676 ASSERT_EQ(1, result.layerFences.count(&mHWC2Layer2));
677 EXPECT_EQ(layer2Fence, result.layerFences[&mHWC2Layer2]);
678}
679
Lloyd Pique688abd42019-02-15 15:42:24 -0800680/*
681 * Display::setExpensiveRenderingExpected()
682 */
683
684TEST_F(DisplayTest, setExpensiveRenderingExpectedForwardsToPowerAdvisor) {
685 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, true)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700686 mDisplay->setExpensiveRenderingExpected(true);
Lloyd Pique688abd42019-02-15 15:42:24 -0800687
688 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700689 mDisplay->setExpensiveRenderingExpected(false);
Lloyd Pique688abd42019-02-15 15:42:24 -0800690}
691
Lloyd Piqued3d69882019-02-28 16:03:46 -0800692/*
693 * Display::finishFrame()
694 */
695
696TEST_F(DisplayTest, finishFrameDoesNotSkipCompositionIfNotDirtyOnHwcDisplay) {
697 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700698 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800699
700 // We expect no calls to queueBuffer if composition was skipped.
701 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
702
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700703 mDisplay->editState().isEnabled = true;
704 mDisplay->editState().usesClientComposition = false;
705 mDisplay->editState().viewport = Rect(0, 0, 1, 1);
706 mDisplay->editState().dirtyRegion = Region::INVALID_REGION;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800707
708 CompositionRefreshArgs refreshArgs;
709 refreshArgs.repaintEverything = false;
710
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700711 mDisplay->finishFrame(refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800712}
713
714TEST_F(DisplayTest, finishFrameSkipsCompositionIfNotDirty) {
715 std::shared_ptr<impl::Display> nonHwcDisplay{
716 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
717
718 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
719 nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
720
721 // We expect no calls to queueBuffer if composition was skipped.
722 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(0);
723
724 nonHwcDisplay->editState().isEnabled = true;
725 nonHwcDisplay->editState().usesClientComposition = false;
726 nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1);
727 nonHwcDisplay->editState().dirtyRegion = Region::INVALID_REGION;
728
729 CompositionRefreshArgs refreshArgs;
730 refreshArgs.repaintEverything = false;
731
732 nonHwcDisplay->finishFrame(refreshArgs);
733}
734
735TEST_F(DisplayTest, finishFramePerformsCompositionIfDirty) {
736 std::shared_ptr<impl::Display> nonHwcDisplay{
737 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
738
739 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
740 nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
741
742 // We expect a single call to queueBuffer when composition is not skipped.
743 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
744
745 nonHwcDisplay->editState().isEnabled = true;
746 nonHwcDisplay->editState().usesClientComposition = false;
747 nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1);
748 nonHwcDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
749
750 CompositionRefreshArgs refreshArgs;
751 refreshArgs.repaintEverything = false;
752
753 nonHwcDisplay->finishFrame(refreshArgs);
754}
755
756TEST_F(DisplayTest, finishFramePerformsCompositionIfRepaintEverything) {
757 std::shared_ptr<impl::Display> nonHwcDisplay{
758 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
759
760 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
761 nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
762
763 // We expect a single call to queueBuffer when composition is not skipped.
764 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
765
766 nonHwcDisplay->editState().isEnabled = true;
767 nonHwcDisplay->editState().usesClientComposition = false;
768 nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1);
769 nonHwcDisplay->editState().dirtyRegion = Region::INVALID_REGION;
770
771 CompositionRefreshArgs refreshArgs;
772 refreshArgs.repaintEverything = true;
773
774 nonHwcDisplay->finishFrame(refreshArgs);
775}
776
Lloyd Piquec6607552019-12-02 17:57:39 -0800777/*
778 * Display functional tests
779 */
780
781struct DisplayFunctionalTest : public testing::Test {
782 class Display : public impl::Display {
783 public:
784 explicit Display(const compositionengine::DisplayCreationArgs& args)
785 : impl::Display(args) {}
786
787 using impl::Display::injectOutputLayerForTest;
788 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
789 };
790
791 static std::shared_ptr<Display> createDisplay(
792 const compositionengine::CompositionEngine& compositionEngine,
793 compositionengine::DisplayCreationArgs&& args) {
794 return impl::createDisplayTemplated<Display>(compositionEngine, args);
795 }
796
797 DisplayFunctionalTest() {
798 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
799
800 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
801 }
802
803 NiceMock<android::mock::HWComposer> mHwComposer;
804 NiceMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
805 NiceMock<mock::CompositionEngine> mCompositionEngine;
806 sp<mock::NativeWindow> mNativeWindow = new NiceMock<mock::NativeWindow>();
807 sp<mock::DisplaySurface> mDisplaySurface = new NiceMock<mock::DisplaySurface>();
808 std::shared_ptr<Display> mDisplay = createDisplay(mCompositionEngine,
809 DisplayCreationArgsBuilder()
810 .setDisplayId(DEFAULT_DISPLAY_ID)
811 .setPowerAdvisor(&mPowerAdvisor)
812 .build());
813 impl::RenderSurface* mRenderSurface =
814 new impl::RenderSurface{mCompositionEngine, *mDisplay,
815 RenderSurfaceCreationArgs{DEFAULT_DISPLAY_WIDTH,
816 DEFAULT_DISPLAY_HEIGHT, mNativeWindow,
817 mDisplaySurface}};
818};
819
820TEST_F(DisplayFunctionalTest, postFramebufferCriticalCallsAreOrdered) {
821 InSequence seq;
822
823 mDisplay->editState().isEnabled = true;
824
825 EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(_));
826 EXPECT_CALL(*mDisplaySurface, onFrameCommitted());
827
828 mDisplay->postFramebuffer();
829}
830
Lloyd Pique45a165a2018-10-19 11:54:47 -0700831} // namespace
832} // namespace android::compositionengine