blob: 0fe584333119b1fdb196e656967a7d3443275675 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Pique45a165a2018-10-19 11:54:47 -070021#include <cmath>
22
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070023#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070024#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/DisplaySurface.h>
26#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070027#include <compositionengine/impl/Display.h>
Lloyd Piquec6607552019-12-02 17:57:39 -080028#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070029#include <compositionengine/mock/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080030#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Piquec6607552019-12-02 17:57:39 -080031#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080032#include <compositionengine/mock/Layer.h>
33#include <compositionengine/mock/LayerFE.h>
chaviw8beb4142019-04-11 13:09:05 -070034#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080035#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070036#include <compositionengine/mock/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070037#include <gtest/gtest.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070038
Lloyd Pique66d68602019-02-13 14:23:31 -080039#include "MockHWC2.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070040#include "MockHWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include "MockPowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070042
43namespace android::compositionengine {
44namespace {
45
Lloyd Piquef5275482019-01-29 18:42:42 -080046using testing::_;
Lloyd Pique66d68602019-02-13 14:23:31 -080047using testing::DoAll;
Lloyd Piquec6607552019-12-02 17:57:39 -080048using testing::InSequence;
49using testing::NiceMock;
Lloyd Pique31cb2942018-10-19 17:23:03 -070050using testing::Return;
Lloyd Pique45a165a2018-10-19 11:54:47 -070051using testing::ReturnRef;
Lloyd Pique66d68602019-02-13 14:23:31 -080052using testing::Sequence;
53using testing::SetArgPointee;
Lloyd Pique45a165a2018-10-19 11:54:47 -070054using testing::StrictMock;
55
56constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42};
Lloyd Piquec6607552019-12-02 17:57:39 -080057constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
58constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
Lloyd Pique45a165a2018-10-19 11:54:47 -070059
Lloyd Pique66d68602019-02-13 14:23:31 -080060struct DisplayTest : public testing::Test {
Lloyd Pique01c77c12019-04-17 12:48:32 -070061 class Display : public impl::Display {
62 public:
63 explicit Display(const compositionengine::DisplayCreationArgs& args)
64 : impl::Display(args) {}
65
66 using impl::Display::injectOutputLayerForTest;
67 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
68 };
69
70 static std::shared_ptr<Display> createDisplay(
71 const compositionengine::CompositionEngine& compositionEngine,
72 compositionengine::DisplayCreationArgs&& args) {
73 return impl::createDisplayTemplated<Display>(compositionEngine, args);
74 }
75
Lloyd Pique66d68602019-02-13 14:23:31 -080076 DisplayTest() {
77 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
78 EXPECT_CALL(*mLayer1, getHwcLayer()).WillRepeatedly(Return(&mHWC2Layer1));
79 EXPECT_CALL(*mLayer2, getHwcLayer()).WillRepeatedly(Return(&mHWC2Layer2));
80 EXPECT_CALL(*mLayer3, getHwcLayer()).WillRepeatedly(Return(nullptr));
81
Lloyd Pique01c77c12019-04-17 12:48:32 -070082 mDisplay->injectOutputLayerForTest(
83 std::unique_ptr<compositionengine::OutputLayer>(mLayer1));
84 mDisplay->injectOutputLayerForTest(
85 std::unique_ptr<compositionengine::OutputLayer>(mLayer2));
86 mDisplay->injectOutputLayerForTest(
87 std::unique_ptr<compositionengine::OutputLayer>(mLayer3));
Lloyd Pique66d68602019-02-13 14:23:31 -080088 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070089
90 StrictMock<android::mock::HWComposer> mHwComposer;
Lloyd Pique688abd42019-02-15 15:42:24 -080091 StrictMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
Lloyd Pique45a165a2018-10-19 11:54:47 -070092 StrictMock<mock::CompositionEngine> mCompositionEngine;
chaviw8beb4142019-04-11 13:09:05 -070093 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
Lloyd Pique66d68602019-02-13 14:23:31 -080094 StrictMock<HWC2::mock::Layer> mHWC2Layer1;
95 StrictMock<HWC2::mock::Layer> mHWC2Layer2;
96 StrictMock<HWC2::mock::Layer> mHWC2LayerUnknown;
97 mock::OutputLayer* mLayer1 = new StrictMock<mock::OutputLayer>();
98 mock::OutputLayer* mLayer2 = new StrictMock<mock::OutputLayer>();
99 mock::OutputLayer* mLayer3 = new StrictMock<mock::OutputLayer>();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700100 std::shared_ptr<Display> mDisplay = createDisplay(mCompositionEngine,
101 DisplayCreationArgsBuilder()
102 .setDisplayId(DEFAULT_DISPLAY_ID)
103 .setPowerAdvisor(&mPowerAdvisor)
104 .build());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700105};
106
Lloyd Pique66d68602019-02-13 14:23:31 -0800107/*
Lloyd Pique45a165a2018-10-19 11:54:47 -0700108 * Basic construction
109 */
110
111TEST_F(DisplayTest, canInstantiateDisplay) {
112 {
113 constexpr DisplayId display1 = DisplayId{123u};
114 auto display =
115 impl::createDisplay(mCompositionEngine,
116 DisplayCreationArgsBuilder().setDisplayId(display1).build());
117 EXPECT_FALSE(display->isSecure());
118 EXPECT_FALSE(display->isVirtual());
119 EXPECT_EQ(display1, display->getId());
120 }
121
122 {
123 constexpr DisplayId display2 = DisplayId{546u};
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700124 auto display =
125 impl::createDisplay(mCompositionEngine,
126 DisplayCreationArgsBuilder().setDisplayId(display2).build());
127 EXPECT_FALSE(display->isSecure());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700128 EXPECT_FALSE(display->isVirtual());
129 EXPECT_EQ(display2, display->getId());
130 }
131
132 {
133 constexpr DisplayId display3 = DisplayId{789u};
134 auto display = impl::createDisplay(mCompositionEngine,
135 DisplayCreationArgsBuilder()
136 .setIsVirtual(true)
137 .setDisplayId(display3)
138 .build());
139 EXPECT_FALSE(display->isSecure());
140 EXPECT_TRUE(display->isVirtual());
141 EXPECT_EQ(display3, display->getId());
142 }
143}
144
Lloyd Pique66d68602019-02-13 14:23:31 -0800145/*
Lloyd Pique45a165a2018-10-19 11:54:47 -0700146 * Display::disconnect()
147 */
148
149TEST_F(DisplayTest, disconnectDisconnectsDisplay) {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700150 // The first call to disconnect will disconnect the display with the HWC and
151 // set mHwcId to -1.
152 EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700153 mDisplay->disconnect();
154 EXPECT_FALSE(mDisplay->getId());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700155
156 // Subsequent calls will do nothing,
157 EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(0);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700158 mDisplay->disconnect();
159 EXPECT_FALSE(mDisplay->getId());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700160}
161
Lloyd Pique66d68602019-02-13 14:23:31 -0800162/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700163 * Display::setColorTransform()
164 */
165
166TEST_F(DisplayTest, setColorTransformSetsTransform) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800167 // No change does nothing
168 CompositionRefreshArgs refreshArgs;
169 refreshArgs.colorTransformMatrix = std::nullopt;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700170 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800171
Lloyd Pique32cbe282018-10-19 13:09:22 -0700172 // Identity matrix sets an identity state value
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800173 const mat4 kIdentity;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700174
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800175 EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, kIdentity)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700176
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800177 refreshArgs.colorTransformMatrix = kIdentity;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700178 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700179
180 // Non-identity matrix sets a non-identity state value
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800181 const mat4 kNonIdentity = mat4() * 2;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700182
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800183 EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, kNonIdentity)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700184
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800185 refreshArgs.colorTransformMatrix = kNonIdentity;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700186 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700187}
188
Lloyd Pique66d68602019-02-13 14:23:31 -0800189/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700190 * Display::setColorMode()
191 */
192
193TEST_F(DisplayTest, setColorModeSetsModeUnlessNoChange) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800194 using ColorProfile = Output::ColorProfile;
195
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700197 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piquef5275482019-01-29 18:42:42 -0800198 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700199 mDisplay->setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200
Lloyd Piquef5275482019-01-29 18:42:42 -0800201 EXPECT_CALL(*colorProfile, getTargetDataspace(_, _, _))
202 .WillRepeatedly(Return(ui::Dataspace::UNKNOWN));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700203
204 // These values are expected to be the initial state.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700205 ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
206 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
207 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
208 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700209
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700210 // Otherwise if the values are unchanged, nothing happens
211 mDisplay->setColorProfile(ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
212 ui::RenderIntent::COLORIMETRIC, ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700213
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700214 EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
215 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
216 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
217 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700218
219 // Otherwise if the values are different, updates happen
Lloyd Piqueef958122019-02-05 18:00:12 -0800220 EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700221 EXPECT_CALL(mHwComposer,
Lloyd Piqueef958122019-02-05 18:00:12 -0800222 setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700223 ui::RenderIntent::TONE_MAP_COLORIMETRIC))
224 .Times(1);
225
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700226 mDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
227 ui::RenderIntent::TONE_MAP_COLORIMETRIC,
228 ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700229
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700230 EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay->getState().colorMode);
231 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay->getState().dataspace);
232 EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay->getState().renderIntent);
233 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234}
235
236TEST_F(DisplayTest, setColorModeDoesNothingForVirtualDisplay) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800237 using ColorProfile = Output::ColorProfile;
238
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700239 std::shared_ptr<impl::Display> virtualDisplay{
240 impl::createDisplay(mCompositionEngine, DisplayCreationArgs{true, DEFAULT_DISPLAY_ID})};
Lloyd Pique32cbe282018-10-19 13:09:22 -0700241
Lloyd Piquef5275482019-01-29 18:42:42 -0800242 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700243 virtualDisplay->setDisplayColorProfileForTest(
Lloyd Piquef5275482019-01-29 18:42:42 -0800244 std::unique_ptr<DisplayColorProfile>(colorProfile));
245
246 EXPECT_CALL(*colorProfile,
247 getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
248 ui::Dataspace::UNKNOWN))
249 .WillOnce(Return(ui::Dataspace::UNKNOWN));
250
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 virtualDisplay->setColorProfile(
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800252 ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
253 ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700254
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700255 EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay->getState().colorMode);
256 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().dataspace);
257 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay->getState().renderIntent);
258 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700259}
260
Lloyd Pique66d68602019-02-13 14:23:31 -0800261/*
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700262 * Display::createDisplayColorProfile()
263 */
264
265TEST_F(DisplayTest, createDisplayColorProfileSetsDisplayColorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700266 EXPECT_TRUE(mDisplay->getDisplayColorProfile() == nullptr);
267 mDisplay->createDisplayColorProfile(
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700268 DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
269 DisplayColorProfileCreationArgs::HwcColorModes()});
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700270 EXPECT_TRUE(mDisplay->getDisplayColorProfile() != nullptr);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700271}
272
Lloyd Pique66d68602019-02-13 14:23:31 -0800273/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700274 * Display::createRenderSurface()
275 */
276
277TEST_F(DisplayTest, createRenderSurfaceSetsRenderSurface) {
chaviw8beb4142019-04-11 13:09:05 -0700278 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700279 EXPECT_TRUE(mDisplay->getRenderSurface() == nullptr);
280 mDisplay->createRenderSurface(RenderSurfaceCreationArgs{640, 480, mNativeWindow, nullptr});
281 EXPECT_TRUE(mDisplay->getRenderSurface() != nullptr);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700282}
283
Lloyd Pique66d68602019-02-13 14:23:31 -0800284/*
Lloyd Piquedf336d92019-03-07 21:38:42 -0800285 * Display::createOutputLayer()
286 */
287
288TEST_F(DisplayTest, createOutputLayerSetsHwcLayer) {
289 sp<mock::LayerFE> layerFE = new StrictMock<mock::LayerFE>();
290 auto layer = std::make_shared<StrictMock<mock::Layer>>();
291 StrictMock<HWC2::mock::Layer> hwcLayer;
292
293 EXPECT_CALL(mHwComposer, createLayer(DEFAULT_DISPLAY_ID)).WillOnce(Return(&hwcLayer));
294
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700295 auto outputLayer = mDisplay->createOutputLayer(layer, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800296
297 EXPECT_EQ(&hwcLayer, outputLayer->getHwcLayer());
298
299 EXPECT_CALL(mHwComposer, destroyLayer(DEFAULT_DISPLAY_ID, &hwcLayer));
300 outputLayer.reset();
301}
302
303/*
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800304 * Display::setReleasedLayers()
305 */
306
307TEST_F(DisplayTest, setReleasedLayersDoesNothingIfNotHwcDisplay) {
308 std::shared_ptr<impl::Display> nonHwcDisplay{
309 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
310
311 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
312 mock::Layer layerXLayer;
313
314 {
315 Output::ReleasedLayers releasedLayers;
316 releasedLayers.emplace_back(layerXLayerFE);
317 nonHwcDisplay->setReleasedLayers(std::move(releasedLayers));
318 }
319
320 CompositionRefreshArgs refreshArgs;
321 refreshArgs.layersWithQueuedFrames.push_back(&layerXLayer);
322
323 nonHwcDisplay->setReleasedLayers(refreshArgs);
324
325 const auto& releasedLayers = nonHwcDisplay->getReleasedLayersForTest();
326 ASSERT_EQ(1, releasedLayers.size());
327}
328
329TEST_F(DisplayTest, setReleasedLayersDoesNothingIfNoLayersWithQueuedFrames) {
330 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
331
332 {
333 Output::ReleasedLayers releasedLayers;
334 releasedLayers.emplace_back(layerXLayerFE);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700335 mDisplay->setReleasedLayers(std::move(releasedLayers));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800336 }
337
338 CompositionRefreshArgs refreshArgs;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700339 mDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800340
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700341 const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800342 ASSERT_EQ(1, releasedLayers.size());
343}
344
345TEST_F(DisplayTest, setReleasedLayers) {
346 sp<mock::LayerFE> layer1LayerFE = new StrictMock<mock::LayerFE>();
347 sp<mock::LayerFE> layer2LayerFE = new StrictMock<mock::LayerFE>();
348 sp<mock::LayerFE> layer3LayerFE = new StrictMock<mock::LayerFE>();
349 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
350 mock::Layer layer1Layer;
351 mock::Layer layer2Layer;
352 mock::Layer layer3Layer;
353 mock::Layer layerXLayer;
354
355 EXPECT_CALL(*mLayer1, getLayer()).WillRepeatedly(ReturnRef(layer1Layer));
356 EXPECT_CALL(*mLayer1, getLayerFE()).WillRepeatedly(ReturnRef(*layer1LayerFE.get()));
357 EXPECT_CALL(*mLayer2, getLayer()).WillRepeatedly(ReturnRef(layer2Layer));
358 EXPECT_CALL(*mLayer2, getLayerFE()).WillRepeatedly(ReturnRef(*layer2LayerFE.get()));
359 EXPECT_CALL(*mLayer3, getLayer()).WillRepeatedly(ReturnRef(layer3Layer));
360 EXPECT_CALL(*mLayer3, getLayerFE()).WillRepeatedly(ReturnRef(*layer3LayerFE.get()));
361
362 CompositionRefreshArgs refreshArgs;
363 refreshArgs.layersWithQueuedFrames.push_back(&layer1Layer);
364 refreshArgs.layersWithQueuedFrames.push_back(&layer2Layer);
365 refreshArgs.layersWithQueuedFrames.push_back(&layerXLayer);
366 refreshArgs.layersWithQueuedFrames.push_back(nullptr);
367
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700368 mDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800369
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700370 const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800371 ASSERT_EQ(2, releasedLayers.size());
372 ASSERT_EQ(layer1LayerFE.get(), releasedLayers[0].promote().get());
373 ASSERT_EQ(layer2LayerFE.get(), releasedLayers[1].promote().get());
374}
375
376/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800377 * Display::chooseCompositionStrategy()
378 */
379
380struct DisplayChooseCompositionStrategyTest : public testing::Test {
381 struct DisplayPartialMock : public impl::Display {
382 DisplayPartialMock(const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700383 const compositionengine::DisplayCreationArgs& args)
384 : impl::Display(args), mCompositionEngine(compositionEngine) {}
Lloyd Pique66d68602019-02-13 14:23:31 -0800385
386 // Sets up the helper functions called by chooseCompositionStrategy to
387 // use a mock implementations.
388 MOCK_CONST_METHOD0(anyLayersRequireClientComposition, bool());
389 MOCK_CONST_METHOD0(allLayersRequireClientComposition, bool());
390 MOCK_METHOD1(applyChangedTypesToLayers, void(const impl::Display::ChangedTypes&));
391 MOCK_METHOD1(applyDisplayRequests, void(const impl::Display::DisplayRequests&));
392 MOCK_METHOD1(applyLayerRequestsToLayers, void(const impl::Display::LayerRequests&));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700393
394 // compositionengine::Output overrides
395 const OutputCompositionState& getState() const override { return mState; }
396 OutputCompositionState& editState() override { return mState; }
397
398 // compositionengine::impl::Output overrides
399 const CompositionEngine& getCompositionEngine() const override {
400 return mCompositionEngine;
401 };
402
403 // These need implementations though are not expected to be called.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700404 MOCK_CONST_METHOD0(getOutputLayerCount, size_t());
405 MOCK_CONST_METHOD1(getOutputLayerOrderedByZByIndex,
406 compositionengine::OutputLayer*(size_t));
407 MOCK_METHOD3(ensureOutputLayer,
408 compositionengine::OutputLayer*(
409 std::optional<size_t>,
410 const std::shared_ptr<compositionengine::Layer>&, const sp<LayerFE>&));
411 MOCK_METHOD0(finalizePendingOutputLayers, void());
412 MOCK_METHOD0(clearOutputLayers, void());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700413 MOCK_CONST_METHOD1(dumpState, void(std::string&));
Lloyd Pique01c77c12019-04-17 12:48:32 -0700414 MOCK_METHOD2(injectOutputLayerForTest,
415 compositionengine::OutputLayer*(
416 const std::shared_ptr<compositionengine::Layer>&, const sp<LayerFE>&));
417 MOCK_METHOD1(injectOutputLayerForTest, void(std::unique_ptr<OutputLayer>));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700418
419 const compositionengine::CompositionEngine& mCompositionEngine;
420 impl::OutputCompositionState mState;
Lloyd Pique66d68602019-02-13 14:23:31 -0800421 };
422
423 DisplayChooseCompositionStrategyTest() {
424 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
425 }
426
427 StrictMock<android::mock::HWComposer> mHwComposer;
428 StrictMock<mock::CompositionEngine> mCompositionEngine;
429 StrictMock<DisplayPartialMock>
430 mDisplay{mCompositionEngine,
431 DisplayCreationArgsBuilder().setDisplayId(DEFAULT_DISPLAY_ID).build()};
432};
433
434TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutIfNotAHwcDisplay) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700435 std::shared_ptr<impl::Display> nonHwcDisplay{
436 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
437 EXPECT_FALSE(nonHwcDisplay->getId());
Lloyd Pique66d68602019-02-13 14:23:31 -0800438
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700439 nonHwcDisplay->chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800440
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 auto& state = nonHwcDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800442 EXPECT_TRUE(state.usesClientComposition);
443 EXPECT_FALSE(state.usesDeviceComposition);
444}
445
446TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutOnHwcError) {
447 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).WillOnce(Return(false));
448 EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, false, _))
449 .WillOnce(Return(INVALID_OPERATION));
450
451 mDisplay.chooseCompositionStrategy();
452
453 auto& state = mDisplay.getState();
454 EXPECT_TRUE(state.usesClientComposition);
455 EXPECT_FALSE(state.usesDeviceComposition);
456}
457
458TEST_F(DisplayChooseCompositionStrategyTest, normalOperation) {
459 // Since two calls are made to anyLayersRequireClientComposition with different return values,
460 // use a Sequence to control the matching so the values are returned in a known order.
461 Sequence s;
462 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).InSequence(s).WillOnce(Return(true));
463 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition())
464 .InSequence(s)
465 .WillOnce(Return(false));
466
467 EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, true, _))
468 .WillOnce(Return(NO_ERROR));
469 EXPECT_CALL(mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
470
471 mDisplay.chooseCompositionStrategy();
472
473 auto& state = mDisplay.getState();
474 EXPECT_FALSE(state.usesClientComposition);
475 EXPECT_TRUE(state.usesDeviceComposition);
476}
477
478TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithChanges) {
479 android::HWComposer::DeviceRequestedChanges changes{
480 {{nullptr, HWC2::Composition::Client}},
481 HWC2::DisplayRequest::FlipClientTarget,
482 {{nullptr, HWC2::LayerRequest::ClearClientTarget}},
483 };
484
485 // Since two calls are made to anyLayersRequireClientComposition with different return values,
486 // use a Sequence to control the matching so the values are returned in a known order.
487 Sequence s;
488 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).InSequence(s).WillOnce(Return(true));
489 EXPECT_CALL(mDisplay, anyLayersRequireClientComposition())
490 .InSequence(s)
491 .WillOnce(Return(false));
492
493 EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, true, _))
494 .WillOnce(DoAll(SetArgPointee<2>(changes), Return(NO_ERROR)));
495 EXPECT_CALL(mDisplay, applyChangedTypesToLayers(changes.changedTypes)).Times(1);
496 EXPECT_CALL(mDisplay, applyDisplayRequests(changes.displayRequests)).Times(1);
497 EXPECT_CALL(mDisplay, applyLayerRequestsToLayers(changes.layerRequests)).Times(1);
498 EXPECT_CALL(mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
499
500 mDisplay.chooseCompositionStrategy();
501
502 auto& state = mDisplay.getState();
503 EXPECT_FALSE(state.usesClientComposition);
504 EXPECT_TRUE(state.usesDeviceComposition);
505}
506
507/*
Lloyd Pique688abd42019-02-15 15:42:24 -0800508 * Display::getSkipColorTransform()
509 */
510
511TEST_F(DisplayTest, getSkipColorTransformDoesNothingIfNonHwcDisplay) {
512 auto nonHwcDisplay{
513 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
514 EXPECT_FALSE(nonHwcDisplay->getSkipColorTransform());
515}
516
517TEST_F(DisplayTest, getSkipColorTransformChecksHwcCapability) {
518 EXPECT_CALL(mHwComposer,
519 hasDisplayCapability(std::make_optional(DEFAULT_DISPLAY_ID),
520 HWC2::DisplayCapability::SkipClientColorTransform))
521 .WillOnce(Return(true));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700522 EXPECT_TRUE(mDisplay->getSkipColorTransform());
Lloyd Pique688abd42019-02-15 15:42:24 -0800523}
524
525/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800526 * Display::anyLayersRequireClientComposition()
527 */
528
529TEST_F(DisplayTest, anyLayersRequireClientCompositionReturnsFalse) {
530 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(false));
531 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(false));
532 EXPECT_CALL(*mLayer3, requiresClientComposition()).WillOnce(Return(false));
533
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700534 EXPECT_FALSE(mDisplay->anyLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800535}
536
537TEST_F(DisplayTest, anyLayersRequireClientCompositionReturnsTrue) {
538 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(false));
539 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(true));
540
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700541 EXPECT_TRUE(mDisplay->anyLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800542}
543
544/*
545 * Display::allLayersRequireClientComposition()
546 */
547
548TEST_F(DisplayTest, allLayersRequireClientCompositionReturnsTrue) {
549 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(true));
550 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(true));
551 EXPECT_CALL(*mLayer3, requiresClientComposition()).WillOnce(Return(true));
552
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700553 EXPECT_TRUE(mDisplay->allLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800554}
555
556TEST_F(DisplayTest, allLayersRequireClientCompositionReturnsFalse) {
557 EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(true));
558 EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(false));
559
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700560 EXPECT_FALSE(mDisplay->allLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800561}
562
563/*
564 * Display::applyChangedTypesToLayers()
565 */
566
567TEST_F(DisplayTest, applyChangedTypesToLayersTakesEarlyOutIfNoChangedLayers) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700568 mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes());
Lloyd Pique66d68602019-02-13 14:23:31 -0800569}
570
571TEST_F(DisplayTest, applyChangedTypesToLayersAppliesChanges) {
572 EXPECT_CALL(*mLayer1,
573 applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::CLIENT))
574 .Times(1);
575 EXPECT_CALL(*mLayer2,
576 applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::DEVICE))
577 .Times(1);
578
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700579 mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes{
Lloyd Pique66d68602019-02-13 14:23:31 -0800580 {&mHWC2Layer1, HWC2::Composition::Client},
581 {&mHWC2Layer2, HWC2::Composition::Device},
582 {&mHWC2LayerUnknown, HWC2::Composition::SolidColor},
583 });
584}
585
586/*
587 * Display::applyDisplayRequests()
588 */
589
590TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesNoRequests) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700591 mDisplay->applyDisplayRequests(static_cast<HWC2::DisplayRequest>(0));
Lloyd Pique66d68602019-02-13 14:23:31 -0800592
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700593 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800594 EXPECT_FALSE(state.flipClientTarget);
595}
596
597TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesFlipClientTarget) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700598 mDisplay->applyDisplayRequests(HWC2::DisplayRequest::FlipClientTarget);
Lloyd Pique66d68602019-02-13 14:23:31 -0800599
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700600 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800601 EXPECT_TRUE(state.flipClientTarget);
602}
603
604TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesWriteClientTargetToOutput) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700605 mDisplay->applyDisplayRequests(HWC2::DisplayRequest::WriteClientTargetToOutput);
Lloyd Pique66d68602019-02-13 14:23:31 -0800606
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700607 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800608 EXPECT_FALSE(state.flipClientTarget);
609}
610
611TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesAllRequestFlagsSet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700612 mDisplay->applyDisplayRequests(static_cast<HWC2::DisplayRequest>(~0));
Lloyd Pique66d68602019-02-13 14:23:31 -0800613
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700614 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800615 EXPECT_TRUE(state.flipClientTarget);
616}
617
618/*
619 * Display::applyLayerRequestsToLayers()
620 */
621
622TEST_F(DisplayTest, applyLayerRequestsToLayersPreparesAllLayers) {
623 EXPECT_CALL(*mLayer1, prepareForDeviceLayerRequests()).Times(1);
624 EXPECT_CALL(*mLayer2, prepareForDeviceLayerRequests()).Times(1);
625 EXPECT_CALL(*mLayer3, prepareForDeviceLayerRequests()).Times(1);
626
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700627 mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests());
Lloyd Pique66d68602019-02-13 14:23:31 -0800628}
629
630TEST_F(DisplayTest, applyLayerRequestsToLayers2) {
631 EXPECT_CALL(*mLayer1, prepareForDeviceLayerRequests()).Times(1);
632 EXPECT_CALL(*mLayer2, prepareForDeviceLayerRequests()).Times(1);
633 EXPECT_CALL(*mLayer3, prepareForDeviceLayerRequests()).Times(1);
634
635 EXPECT_CALL(*mLayer1,
636 applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET))
637 .Times(1);
638
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700639 mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests{
Lloyd Pique66d68602019-02-13 14:23:31 -0800640 {&mHWC2Layer1, HWC2::LayerRequest::ClearClientTarget},
641 {&mHWC2LayerUnknown, HWC2::LayerRequest::ClearClientTarget},
642 });
643}
644
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800645/*
646 * Display::presentAndGetFrameFences()
647 */
648
649TEST_F(DisplayTest, presentAndGetFrameFencesReturnsNoFencesOnNonHwcDisplay) {
650 auto nonHwcDisplay{
651 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
652
653 auto result = nonHwcDisplay->presentAndGetFrameFences();
654
655 ASSERT_TRUE(result.presentFence.get());
656 EXPECT_FALSE(result.presentFence->isValid());
657 EXPECT_EQ(0u, result.layerFences.size());
658}
659
660TEST_F(DisplayTest, presentAndGetFrameFencesReturnsPresentAndLayerFences) {
661 sp<Fence> presentFence = new Fence();
662 sp<Fence> layer1Fence = new Fence();
663 sp<Fence> layer2Fence = new Fence();
664
665 EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(DEFAULT_DISPLAY_ID)).Times(1);
666 EXPECT_CALL(mHwComposer, getPresentFence(DEFAULT_DISPLAY_ID)).WillOnce(Return(presentFence));
667 EXPECT_CALL(mHwComposer, getLayerReleaseFence(DEFAULT_DISPLAY_ID, &mHWC2Layer1))
668 .WillOnce(Return(layer1Fence));
669 EXPECT_CALL(mHwComposer, getLayerReleaseFence(DEFAULT_DISPLAY_ID, &mHWC2Layer2))
670 .WillOnce(Return(layer2Fence));
671 EXPECT_CALL(mHwComposer, clearReleaseFences(DEFAULT_DISPLAY_ID)).Times(1);
672
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700673 auto result = mDisplay->presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800674
675 EXPECT_EQ(presentFence, result.presentFence);
676
677 EXPECT_EQ(2u, result.layerFences.size());
678 ASSERT_EQ(1, result.layerFences.count(&mHWC2Layer1));
679 EXPECT_EQ(layer1Fence, result.layerFences[&mHWC2Layer1]);
680 ASSERT_EQ(1, result.layerFences.count(&mHWC2Layer2));
681 EXPECT_EQ(layer2Fence, result.layerFences[&mHWC2Layer2]);
682}
683
Lloyd Pique688abd42019-02-15 15:42:24 -0800684/*
685 * Display::setExpensiveRenderingExpected()
686 */
687
688TEST_F(DisplayTest, setExpensiveRenderingExpectedForwardsToPowerAdvisor) {
689 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, true)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700690 mDisplay->setExpensiveRenderingExpected(true);
Lloyd Pique688abd42019-02-15 15:42:24 -0800691
692 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700693 mDisplay->setExpensiveRenderingExpected(false);
Lloyd Pique688abd42019-02-15 15:42:24 -0800694}
695
Lloyd Piqued3d69882019-02-28 16:03:46 -0800696/*
697 * Display::finishFrame()
698 */
699
700TEST_F(DisplayTest, finishFrameDoesNotSkipCompositionIfNotDirtyOnHwcDisplay) {
701 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700702 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800703
704 // We expect no calls to queueBuffer if composition was skipped.
705 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
706
Lloyd Piquea76ce462020-01-14 13:06:37 -0800707 // Expect a call to signal no expensive rendering since there is no client composition.
708 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false));
709
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700710 mDisplay->editState().isEnabled = true;
711 mDisplay->editState().usesClientComposition = false;
712 mDisplay->editState().viewport = Rect(0, 0, 1, 1);
713 mDisplay->editState().dirtyRegion = Region::INVALID_REGION;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800714
715 CompositionRefreshArgs refreshArgs;
716 refreshArgs.repaintEverything = false;
717
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700718 mDisplay->finishFrame(refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800719}
720
721TEST_F(DisplayTest, finishFrameSkipsCompositionIfNotDirty) {
722 std::shared_ptr<impl::Display> nonHwcDisplay{
723 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
724
725 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
726 nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
727
728 // We expect no calls to queueBuffer if composition was skipped.
729 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(0);
730
731 nonHwcDisplay->editState().isEnabled = true;
732 nonHwcDisplay->editState().usesClientComposition = false;
733 nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1);
734 nonHwcDisplay->editState().dirtyRegion = Region::INVALID_REGION;
735
736 CompositionRefreshArgs refreshArgs;
737 refreshArgs.repaintEverything = false;
738
739 nonHwcDisplay->finishFrame(refreshArgs);
740}
741
742TEST_F(DisplayTest, finishFramePerformsCompositionIfDirty) {
743 std::shared_ptr<impl::Display> nonHwcDisplay{
744 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
745
746 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
747 nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
748
749 // We expect a single call to queueBuffer when composition is not skipped.
750 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
751
752 nonHwcDisplay->editState().isEnabled = true;
753 nonHwcDisplay->editState().usesClientComposition = false;
754 nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1);
755 nonHwcDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
756
757 CompositionRefreshArgs refreshArgs;
758 refreshArgs.repaintEverything = false;
759
760 nonHwcDisplay->finishFrame(refreshArgs);
761}
762
763TEST_F(DisplayTest, finishFramePerformsCompositionIfRepaintEverything) {
764 std::shared_ptr<impl::Display> nonHwcDisplay{
765 impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())};
766
767 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
768 nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
769
770 // We expect a single call to queueBuffer when composition is not skipped.
771 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
772
773 nonHwcDisplay->editState().isEnabled = true;
774 nonHwcDisplay->editState().usesClientComposition = false;
775 nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1);
776 nonHwcDisplay->editState().dirtyRegion = Region::INVALID_REGION;
777
778 CompositionRefreshArgs refreshArgs;
779 refreshArgs.repaintEverything = true;
780
781 nonHwcDisplay->finishFrame(refreshArgs);
782}
783
Lloyd Piquec6607552019-12-02 17:57:39 -0800784/*
785 * Display functional tests
786 */
787
788struct DisplayFunctionalTest : public testing::Test {
789 class Display : public impl::Display {
790 public:
791 explicit Display(const compositionengine::DisplayCreationArgs& args)
792 : impl::Display(args) {}
793
794 using impl::Display::injectOutputLayerForTest;
795 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
796 };
797
798 static std::shared_ptr<Display> createDisplay(
799 const compositionengine::CompositionEngine& compositionEngine,
800 compositionengine::DisplayCreationArgs&& args) {
801 return impl::createDisplayTemplated<Display>(compositionEngine, args);
802 }
803
804 DisplayFunctionalTest() {
805 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
806
807 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
808 }
809
810 NiceMock<android::mock::HWComposer> mHwComposer;
811 NiceMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
812 NiceMock<mock::CompositionEngine> mCompositionEngine;
813 sp<mock::NativeWindow> mNativeWindow = new NiceMock<mock::NativeWindow>();
814 sp<mock::DisplaySurface> mDisplaySurface = new NiceMock<mock::DisplaySurface>();
815 std::shared_ptr<Display> mDisplay = createDisplay(mCompositionEngine,
816 DisplayCreationArgsBuilder()
817 .setDisplayId(DEFAULT_DISPLAY_ID)
818 .setPowerAdvisor(&mPowerAdvisor)
819 .build());
820 impl::RenderSurface* mRenderSurface =
821 new impl::RenderSurface{mCompositionEngine, *mDisplay,
822 RenderSurfaceCreationArgs{DEFAULT_DISPLAY_WIDTH,
823 DEFAULT_DISPLAY_HEIGHT, mNativeWindow,
824 mDisplaySurface}};
825};
826
827TEST_F(DisplayFunctionalTest, postFramebufferCriticalCallsAreOrdered) {
828 InSequence seq;
829
830 mDisplay->editState().isEnabled = true;
831
832 EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(_));
833 EXPECT_CALL(*mDisplaySurface, onFrameCommitted());
834
835 mDisplay->postFramebuffer();
836}
837
Lloyd Pique45a165a2018-10-19 11:54:47 -0700838} // namespace
839} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800840
841// TODO(b/129481165): remove the #pragma below and fix conversion issues
842#pragma clang diagnostic pop // ignored "-Wconversion"