1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // #define LOG_NDEBUG 0
18 #undef LOG_TAG
19 #define LOG_TAG "DisplayDevice"
20
21 #include <android-base/stringprintf.h>
22 #include <compositionengine/CompositionEngine.h>
23 #include <compositionengine/Display.h>
24 #include <compositionengine/DisplayColorProfile.h>
25 #include <compositionengine/DisplayColorProfileCreationArgs.h>
26 #include <compositionengine/DisplayCreationArgs.h>
27 #include <compositionengine/DisplaySurface.h>
28 #include <compositionengine/RenderSurface.h>
29 #include <compositionengine/RenderSurfaceCreationArgs.h>
30 #include <compositionengine/impl/OutputCompositionState.h>
31 #include <configstore/Utils.h>
32 #include <log/log.h>
33 #include <system/window.h>
34 #include <ui/GraphicTypes.h>
35
36 #include "DisplayDevice.h"
37 #include "Layer.h"
38 #include "SurfaceFlinger.h"
39
40 namespace android {
41
42 using android::base::StringAppendF;
43
44 /*
45 * Initialize the display to the specified values.
46 *
47 */
48
49 uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
50
DisplayDeviceCreationArgs(const sp<SurfaceFlinger> & flinger,const wp<IBinder> & displayToken,const std::optional<DisplayId> & displayId)51 DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
52 const wp<IBinder>& displayToken,
53 const std::optional<DisplayId>& displayId)
54 : flinger(flinger), displayToken(displayToken), displayId(displayId) {}
55
DisplayDevice(DisplayDeviceCreationArgs && args)56 DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
57 : mFlinger(args.flinger),
58 mDisplayToken(args.displayToken),
59 mSequenceId(args.sequenceId),
60 mDisplayInstallOrientation(args.displayInstallOrientation),
61 mCompositionDisplay{mFlinger->getCompositionEngine().createDisplay(
62 compositionengine::DisplayCreationArgs{args.isSecure, args.isVirtual,
63 args.displayId})},
64 mIsVirtual(args.isVirtual),
65 mOrientation(),
66 mActiveConfig(0),
67 mIsPrimary(args.isPrimary) {
68 mCompositionDisplay->createRenderSurface(
69 compositionengine::RenderSurfaceCreationArgs{ANativeWindow_getWidth(
70 args.nativeWindow.get()),
71 ANativeWindow_getHeight(
72 args.nativeWindow.get()),
73 args.nativeWindow, args.displaySurface});
74
75 mCompositionDisplay->createDisplayColorProfile(
76 compositionengine::DisplayColorProfileCreationArgs{args.hasWideColorGamut,
77 std::move(args.hdrCapabilities),
78 args.supportedPerFrameMetadata,
79 args.hwcColorModes});
80
81 if (!mCompositionDisplay->isValid()) {
82 ALOGE("Composition Display did not validate!");
83 }
84
85 mCompositionDisplay->getRenderSurface()->initialize();
86
87 setPowerMode(args.initialPowerMode);
88
89 // initialize the display orientation transform.
90 setProjection(DisplayState::eOrientationDefault, Rect::INVALID_RECT, Rect::INVALID_RECT);
91 }
92
93 DisplayDevice::~DisplayDevice() = default;
94
disconnect()95 void DisplayDevice::disconnect() {
96 mCompositionDisplay->disconnect();
97 }
98
getWidth() const99 int DisplayDevice::getWidth() const {
100 return mCompositionDisplay->getState().bounds.getWidth();
101 }
102
getHeight() const103 int DisplayDevice::getHeight() const {
104 return mCompositionDisplay->getState().bounds.getHeight();
105 }
106
setDisplayName(const std::string & displayName)107 void DisplayDevice::setDisplayName(const std::string& displayName) {
108 if (!displayName.empty()) {
109 // never override the name with an empty name
110 mDisplayName = displayName;
111 mCompositionDisplay->setName(displayName);
112 }
113 }
114
getPageFlipCount() const115 uint32_t DisplayDevice::getPageFlipCount() const {
116 return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
117 }
118
119 // ----------------------------------------------------------------------------
120
setVisibleLayersSortedByZ(const Vector<sp<Layer>> & layers)121 void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
122 mVisibleLayersSortedByZ = layers;
123 }
124
getVisibleLayersSortedByZ() const125 const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
126 return mVisibleLayersSortedByZ;
127 }
128
setLayersNeedingFences(const Vector<sp<Layer>> & layers)129 void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
130 mLayersNeedingFences = layers;
131 }
132
getLayersNeedingFences() const133 const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
134 return mLayersNeedingFences;
135 }
136
137 // ----------------------------------------------------------------------------
setPowerMode(int mode)138 void DisplayDevice::setPowerMode(int mode) {
139 mPowerMode = mode;
140 getCompositionDisplay()->setCompositionEnabled(mPowerMode != HWC_POWER_MODE_OFF);
141 }
142
getPowerMode() const143 int DisplayDevice::getPowerMode() const {
144 return mPowerMode;
145 }
146
isPoweredOn() const147 bool DisplayDevice::isPoweredOn() const {
148 return mPowerMode != HWC_POWER_MODE_OFF;
149 }
150
151 // ----------------------------------------------------------------------------
setActiveConfig(int mode)152 void DisplayDevice::setActiveConfig(int mode) {
153 mActiveConfig = mode;
154 }
155
getActiveConfig() const156 int DisplayDevice::getActiveConfig() const {
157 return mActiveConfig;
158 }
159
160 // ----------------------------------------------------------------------------
161
getCompositionDataSpace() const162 ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
163 return mCompositionDisplay->getState().dataspace;
164 }
165
166 // ----------------------------------------------------------------------------
167
setLayerStack(uint32_t stack)168 void DisplayDevice::setLayerStack(uint32_t stack) {
169 mCompositionDisplay->setLayerStackFilter(stack, isPrimary());
170 }
171
172 // ----------------------------------------------------------------------------
173
displayStateOrientationToTransformOrientation(int orientation)174 uint32_t DisplayDevice::displayStateOrientationToTransformOrientation(int orientation) {
175 switch (orientation) {
176 case DisplayState::eOrientationDefault:
177 return ui::Transform::ROT_0;
178 case DisplayState::eOrientation90:
179 return ui::Transform::ROT_90;
180 case DisplayState::eOrientation180:
181 return ui::Transform::ROT_180;
182 case DisplayState::eOrientation270:
183 return ui::Transform::ROT_270;
184 default:
185 return ui::Transform::ROT_INVALID;
186 }
187 }
188
orientationToTransfrom(int orientation,int w,int h,ui::Transform * tr)189 status_t DisplayDevice::orientationToTransfrom(int orientation, int w, int h, ui::Transform* tr) {
190 uint32_t flags = displayStateOrientationToTransformOrientation(orientation);
191 if (flags == ui::Transform::ROT_INVALID) {
192 return BAD_VALUE;
193 }
194 tr->set(flags, w, h);
195 return NO_ERROR;
196 }
197
setDisplaySize(const int newWidth,const int newHeight)198 void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
199 mCompositionDisplay->setBounds(ui::Size(newWidth, newHeight));
200 }
201
setProjection(int orientation,const Rect & newViewport,const Rect & newFrame)202 void DisplayDevice::setProjection(int orientation,
203 const Rect& newViewport, const Rect& newFrame) {
204 Rect viewport(newViewport);
205 Rect frame(newFrame);
206
207 mOrientation = orientation;
208
209 const Rect& displayBounds = getCompositionDisplay()->getState().bounds;
210 const int w = displayBounds.width();
211 const int h = displayBounds.height();
212
213 ui::Transform R;
214 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
215
216 if (!frame.isValid()) {
217 // the destination frame can be invalid if it has never been set,
218 // in that case we assume the whole display frame.
219 frame = Rect(w, h);
220 }
221
222 if (viewport.isEmpty()) {
223 // viewport can be invalid if it has never been set, in that case
224 // we assume the whole display size.
225 // it's also invalid to have an empty viewport, so we handle that
226 // case in the same way.
227 viewport = Rect(w, h);
228 if (R.getOrientation() & ui::Transform::ROT_90) {
229 // viewport is always specified in the logical orientation
230 // of the display (ie: post-rotation).
231 std::swap(viewport.right, viewport.bottom);
232 }
233 }
234
235 ui::Transform TL, TP, S;
236 float src_width = viewport.width();
237 float src_height = viewport.height();
238 float dst_width = frame.width();
239 float dst_height = frame.height();
240 if (src_width != dst_width || src_height != dst_height) {
241 float sx = dst_width / src_width;
242 float sy = dst_height / src_height;
243 S.set(sx, 0, 0, sy);
244 }
245
246 float src_x = viewport.left;
247 float src_y = viewport.top;
248 float dst_x = frame.left;
249 float dst_y = frame.top;
250 TL.set(-src_x, -src_y);
251 TP.set(dst_x, dst_y);
252
253 // need to take care of primary display rotation for globalTransform
254 // for case if the panel is not installed aligned with device orientation
255 if (isPrimary()) {
256 DisplayDevice::orientationToTransfrom(
257 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
258 w, h, &R);
259 }
260
261 // The viewport and frame are both in the logical orientation.
262 // Apply the logical translation, scale to physical size, apply the
263 // physical translation and finally rotate to the physical orientation.
264 ui::Transform globalTransform = R * TP * S * TL;
265
266 const uint8_t type = globalTransform.getType();
267 const bool needsFiltering =
268 (!globalTransform.preserveRects() || (type >= ui::Transform::SCALE));
269
270 Rect scissor = globalTransform.transform(viewport);
271 if (scissor.isEmpty()) {
272 scissor = displayBounds;
273 }
274
275 if (isPrimary()) {
276 sPrimaryDisplayOrientation = displayStateOrientationToTransformOrientation(orientation);
277 }
278
279 getCompositionDisplay()->setProjection(globalTransform,
280 displayStateOrientationToTransformOrientation(
281 orientation),
282 frame, viewport, scissor, needsFiltering);
283 }
284
getPrimaryDisplayOrientationTransform()285 uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
286 return sPrimaryDisplayOrientation;
287 }
288
getDebugName() const289 std::string DisplayDevice::getDebugName() const {
290 const auto id = getId() ? to_string(*getId()) + ", " : std::string();
291 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
292 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
293 mDisplayName.c_str());
294 }
295
dump(std::string & result) const296 void DisplayDevice::dump(std::string& result) const {
297 StringAppendF(&result, "+ %s\n", getDebugName().c_str());
298
299 result.append(" ");
300 StringAppendF(&result, "powerMode=%d, ", mPowerMode);
301 StringAppendF(&result, "activeConfig=%d, ", mActiveConfig);
302 StringAppendF(&result, "numLayers=%zu\n", mVisibleLayersSortedByZ.size());
303 getCompositionDisplay()->dump(result);
304 }
305
hasRenderIntent(ui::RenderIntent intent) const306 bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
307 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
308 }
309
310 // ----------------------------------------------------------------------------
311
getId() const312 const std::optional<DisplayId>& DisplayDevice::getId() const {
313 return mCompositionDisplay->getId();
314 }
315
isSecure() const316 bool DisplayDevice::isSecure() const {
317 return mCompositionDisplay->isSecure();
318 }
319
getBounds() const320 const Rect& DisplayDevice::getBounds() const {
321 return mCompositionDisplay->getState().bounds;
322 }
323
getUndefinedRegion() const324 const Region& DisplayDevice::getUndefinedRegion() const {
325 return mCompositionDisplay->getState().undefinedRegion;
326 }
327
needsFiltering() const328 bool DisplayDevice::needsFiltering() const {
329 return mCompositionDisplay->getState().needsFiltering;
330 }
331
getLayerStack() const332 uint32_t DisplayDevice::getLayerStack() const {
333 return mCompositionDisplay->getState().layerStackId;
334 }
335
getTransform() const336 const ui::Transform& DisplayDevice::getTransform() const {
337 return mCompositionDisplay->getState().transform;
338 }
339
getViewport() const340 const Rect& DisplayDevice::getViewport() const {
341 return mCompositionDisplay->getState().viewport;
342 }
343
getFrame() const344 const Rect& DisplayDevice::getFrame() const {
345 return mCompositionDisplay->getState().frame;
346 }
347
getScissor() const348 const Rect& DisplayDevice::getScissor() const {
349 return mCompositionDisplay->getState().scissor;
350 }
351
hasWideColorGamut() const352 bool DisplayDevice::hasWideColorGamut() const {
353 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
354 }
355
hasHDR10PlusSupport() const356 bool DisplayDevice::hasHDR10PlusSupport() const {
357 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
358 }
359
hasHDR10Support() const360 bool DisplayDevice::hasHDR10Support() const {
361 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
362 }
363
hasHLGSupport() const364 bool DisplayDevice::hasHLGSupport() const {
365 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
366 }
367
hasDolbyVisionSupport() const368 bool DisplayDevice::hasDolbyVisionSupport() const {
369 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
370 }
371
getSupportedPerFrameMetadata() const372 int DisplayDevice::getSupportedPerFrameMetadata() const {
373 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
374 }
375
getHdrCapabilities() const376 const HdrCapabilities& DisplayDevice::getHdrCapabilities() const {
377 return mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
378 }
379
380 std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
381
382 } // namespace android
383