blob: f0aea255669798b5b8fa3854be35a676c806c5da [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>
24#include <compositionengine/mock/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080025#include <compositionengine/mock/DisplayColorProfile.h>
chaviw8beb4142019-04-11 13:09:05 -070026#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070027#include <compositionengine/mock/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070028#include <gtest/gtest.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070029
30#include "MockHWComposer.h"
31
32namespace android::compositionengine {
33namespace {
34
Lloyd Piquef5275482019-01-29 18:42:42 -080035using testing::_;
Lloyd Pique31cb2942018-10-19 17:23:03 -070036using testing::Return;
Lloyd Pique45a165a2018-10-19 11:54:47 -070037using testing::ReturnRef;
38using testing::StrictMock;
39
40constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42};
41
42class DisplayTest : public testing::Test {
43public:
44 ~DisplayTest() override = default;
45
46 StrictMock<android::mock::HWComposer> mHwComposer;
47 StrictMock<mock::CompositionEngine> mCompositionEngine;
chaviw8beb4142019-04-11 13:09:05 -070048 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
Lloyd Pique45a165a2018-10-19 11:54:47 -070049 impl::Display mDisplay{mCompositionEngine,
50 DisplayCreationArgsBuilder().setDisplayId(DEFAULT_DISPLAY_ID).build()};
51};
52
53/* ------------------------------------------------------------------------
54 * Basic construction
55 */
56
57TEST_F(DisplayTest, canInstantiateDisplay) {
58 {
59 constexpr DisplayId display1 = DisplayId{123u};
60 auto display =
61 impl::createDisplay(mCompositionEngine,
62 DisplayCreationArgsBuilder().setDisplayId(display1).build());
63 EXPECT_FALSE(display->isSecure());
64 EXPECT_FALSE(display->isVirtual());
65 EXPECT_EQ(display1, display->getId());
66 }
67
68 {
69 constexpr DisplayId display2 = DisplayId{546u};
70 auto display = impl::createDisplay(mCompositionEngine,
71 DisplayCreationArgsBuilder()
72 .setIsSecure(true)
73 .setDisplayId(display2)
74 .build());
75 EXPECT_TRUE(display->isSecure());
76 EXPECT_FALSE(display->isVirtual());
77 EXPECT_EQ(display2, display->getId());
78 }
79
80 {
81 constexpr DisplayId display3 = DisplayId{789u};
82 auto display = impl::createDisplay(mCompositionEngine,
83 DisplayCreationArgsBuilder()
84 .setIsVirtual(true)
85 .setDisplayId(display3)
86 .build());
87 EXPECT_FALSE(display->isSecure());
88 EXPECT_TRUE(display->isVirtual());
89 EXPECT_EQ(display3, display->getId());
90 }
91}
92
93/* ------------------------------------------------------------------------
94 * Display::disconnect()
95 */
96
97TEST_F(DisplayTest, disconnectDisconnectsDisplay) {
98 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
99
100 // The first call to disconnect will disconnect the display with the HWC and
101 // set mHwcId to -1.
102 EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(1);
103 mDisplay.disconnect();
104 EXPECT_FALSE(mDisplay.getId());
105
106 // Subsequent calls will do nothing,
107 EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(0);
108 mDisplay.disconnect();
109 EXPECT_FALSE(mDisplay.getId());
110}
111
Lloyd Pique32cbe282018-10-19 13:09:22 -0700112/* ------------------------------------------------------------------------
113 * Display::setColorTransform()
114 */
115
116TEST_F(DisplayTest, setColorTransformSetsTransform) {
117 // Identity matrix sets an identity state value
118 const mat4 identity;
119
120 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
121
122 EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, identity)).Times(1);
123
124 mDisplay.setColorTransform(identity);
125
126 EXPECT_EQ(HAL_COLOR_TRANSFORM_IDENTITY, mDisplay.getState().colorTransform);
127
128 // Non-identity matrix sets a non-identity state value
129 const mat4 nonIdentity = mat4() * 2;
130
131 EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, nonIdentity)).Times(1);
132
133 mDisplay.setColorTransform(nonIdentity);
134
135 EXPECT_EQ(HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX, mDisplay.getState().colorTransform);
136}
137
138/* ------------------------------------------------------------------------
139 * Display::setColorMode()
140 */
141
142TEST_F(DisplayTest, setColorModeSetsModeUnlessNoChange) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700143 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
144 mDisplay.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piquef5275482019-01-29 18:42:42 -0800145 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
146 mDisplay.setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700147
Lloyd Pique32cbe282018-10-19 13:09:22 -0700148 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
Lloyd Piquef5275482019-01-29 18:42:42 -0800149 EXPECT_CALL(*colorProfile, getTargetDataspace(_, _, _))
150 .WillRepeatedly(Return(ui::Dataspace::UNKNOWN));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700151
152 // These values are expected to be the initial state.
153 ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode);
154 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace);
155 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent);
Lloyd Piquef5275482019-01-29 18:42:42 -0800156 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700157
Lloyd Piquef5275482019-01-29 18:42:42 -0800158 // If the set values are unchanged, nothing happens
Lloyd Pique32cbe282018-10-19 13:09:22 -0700159 mDisplay.setColorMode(ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Lloyd Piquef5275482019-01-29 18:42:42 -0800160 ui::RenderIntent::COLORIMETRIC, ui::Dataspace::UNKNOWN);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700161
162 EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode);
163 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace);
164 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent);
Lloyd Piquef5275482019-01-29 18:42:42 -0800165 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700166
167 // Otherwise if the values are different, updates happen
Lloyd Piqueef958122019-02-05 18:00:12 -0800168 EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700169 EXPECT_CALL(mHwComposer,
Lloyd Piqueef958122019-02-05 18:00:12 -0800170 setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700171 ui::RenderIntent::TONE_MAP_COLORIMETRIC))
172 .Times(1);
173
Lloyd Piqueef958122019-02-05 18:00:12 -0800174 mDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
Lloyd Piquef5275482019-01-29 18:42:42 -0800175 ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700176
Lloyd Piqueef958122019-02-05 18:00:12 -0800177 EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay.getState().colorMode);
178 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay.getState().dataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700179 EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay.getState().renderIntent);
Lloyd Piquef5275482019-01-29 18:42:42 -0800180 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700181}
182
183TEST_F(DisplayTest, setColorModeDoesNothingForVirtualDisplay) {
184 impl::Display virtualDisplay{mCompositionEngine,
185 DisplayCreationArgs{false, true, DEFAULT_DISPLAY_ID}};
186
Lloyd Piquef5275482019-01-29 18:42:42 -0800187 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
188 virtualDisplay.setDisplayColorProfileForTest(
189 std::unique_ptr<DisplayColorProfile>(colorProfile));
190
191 EXPECT_CALL(*colorProfile,
192 getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
193 ui::Dataspace::UNKNOWN))
194 .WillOnce(Return(ui::Dataspace::UNKNOWN));
195
Lloyd Piqueef958122019-02-05 18:00:12 -0800196 virtualDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
Lloyd Piquef5275482019-01-29 18:42:42 -0800197 ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700198
199 EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay.getState().colorMode);
200 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay.getState().dataspace);
201 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay.getState().renderIntent);
Lloyd Piquef5275482019-01-29 18:42:42 -0800202 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700203}
204
Lloyd Pique31cb2942018-10-19 17:23:03 -0700205/* ------------------------------------------------------------------------
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700206 * Display::createDisplayColorProfile()
207 */
208
209TEST_F(DisplayTest, createDisplayColorProfileSetsDisplayColorProfile) {
210 EXPECT_TRUE(mDisplay.getDisplayColorProfile() == nullptr);
211 mDisplay.createDisplayColorProfile(
212 DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
213 DisplayColorProfileCreationArgs::HwcColorModes()});
214 EXPECT_TRUE(mDisplay.getDisplayColorProfile() != nullptr);
215}
216
217/* ------------------------------------------------------------------------
Lloyd Pique31cb2942018-10-19 17:23:03 -0700218 * Display::createRenderSurface()
219 */
220
221TEST_F(DisplayTest, createRenderSurfaceSetsRenderSurface) {
chaviw8beb4142019-04-11 13:09:05 -0700222 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700223 EXPECT_TRUE(mDisplay.getRenderSurface() == nullptr);
chaviw8beb4142019-04-11 13:09:05 -0700224 mDisplay.createRenderSurface(RenderSurfaceCreationArgs{640, 480, mNativeWindow, nullptr});
Lloyd Pique31cb2942018-10-19 17:23:03 -0700225 EXPECT_TRUE(mDisplay.getRenderSurface() != nullptr);
226}
227
Lloyd Pique45a165a2018-10-19 11:54:47 -0700228} // namespace
229} // namespace android::compositionengine