blob: e2122d1d30d87aeecfde43c9a0ac80f51cde0bef [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
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
Dan Stoza9e56aa02015-11-02 13:00:03 -080021// #define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "DisplayDevice"
24
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -070025#include <android-base/stringprintf.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070026#include <compositionengine/CompositionEngine.h>
27#include <compositionengine/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070028#include <compositionengine/DisplayColorProfile.h>
29#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070030#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070031#include <compositionengine/DisplaySurface.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070032#include <compositionengine/RenderSurface.h>
33#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070034#include <compositionengine/impl/OutputCompositionState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070035#include <configstore/Utils.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070036#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080037#include <system/window.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070038#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070040#include "DisplayDevice.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080041#include "Layer.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070042#include "SurfaceFlinger.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070043
Peiyong Linfd997e02018-03-28 15:29:00 -070044namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang5434a782018-12-05 18:06:32 -080046using android::base::StringAppendF;
Jaesoo Lee720a7242017-01-31 15:26:18 +090047
Dominik Laskowski718f9602019-11-09 20:01:35 -080048ui::Transform::RotationFlags DisplayDevice::sPrimaryDisplayRotationFlags = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -070049
Lloyd Pique2eef1d22018-09-18 21:30:04 -070050DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
51 const wp<IBinder>& displayToken,
Dominik Laskowski075d3172018-05-24 15:50:06 -070052 const std::optional<DisplayId>& displayId)
53 : flinger(flinger), displayToken(displayToken), displayId(displayId) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070054
Lloyd Pique2eef1d22018-09-18 21:30:04 -070055DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070056 : mFlinger(args.flinger),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070057 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080058 mSequenceId(args.sequenceId),
Dominik Laskowski718f9602019-11-09 20:01:35 -080059 mIsVirtual(args.isVirtual),
Lloyd Pique45a165a2018-10-19 11:54:47 -070060 mCompositionDisplay{mFlinger->getCompositionEngine().createDisplay(
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070061 compositionengine::DisplayCreationArgs{args.isVirtual, args.displayId,
62 args.powerAdvisor})},
Dominik Laskowski718f9602019-11-09 20:01:35 -080063 mPhysicalOrientation(args.physicalOrientation),
Dominik Laskowski075d3172018-05-24 15:50:06 -070064 mIsPrimary(args.isPrimary) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070065 mCompositionDisplay->editState().isSecure = args.isSecure;
Lloyd Pique31cb2942018-10-19 17:23:03 -070066 mCompositionDisplay->createRenderSurface(
67 compositionengine::RenderSurfaceCreationArgs{ANativeWindow_getWidth(
68 args.nativeWindow.get()),
69 ANativeWindow_getHeight(
70 args.nativeWindow.get()),
71 args.nativeWindow, args.displaySurface});
72
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070073 mCompositionDisplay->createDisplayColorProfile(
74 compositionengine::DisplayColorProfileCreationArgs{args.hasWideColorGamut,
75 std::move(args.hdrCapabilities),
76 args.supportedPerFrameMetadata,
77 args.hwcColorModes});
78
Lloyd Pique32cbe282018-10-19 13:09:22 -070079 if (!mCompositionDisplay->isValid()) {
80 ALOGE("Composition Display did not validate!");
81 }
82
Lloyd Pique31cb2942018-10-19 17:23:03 -070083 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -070084
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 setPowerMode(args.initialPowerMode);
Alec Mouriba013fa2018-10-16 12:43:11 -070086
Jesse Hallffe1f192013-03-22 15:13:48 -070087 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -080088 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089}
90
Lloyd Pique09594832018-01-22 17:48:03 -080091DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -070092
Lloyd Pique45a165a2018-10-19 11:54:47 -070093void DisplayDevice::disconnect() {
94 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -070095}
96
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070097int DisplayDevice::getWidth() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070098 return mCompositionDisplay->getState().bounds.getWidth();
Mathias Agopiana4912602012-07-12 14:25:33 -070099}
100
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700101int DisplayDevice::getHeight() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102 return mCompositionDisplay->getState().bounds.getHeight();
Mathias Agopiana4912602012-07-12 14:25:33 -0700103}
104
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700105void DisplayDevice::setDisplayName(const std::string& displayName) {
106 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700107 // never override the name with an empty name
108 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700110 }
111}
112
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700113uint32_t DisplayDevice::getPageFlipCount() const {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114 return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800115}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800116
Mathias Agopian1b031492012-06-20 17:51:20 -0700117// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700118void DisplayDevice::setPowerMode(int mode) {
119 mPowerMode = mode;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120 getCompositionDisplay()->setCompositionEnabled(mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700121}
122
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700123int DisplayDevice::getPowerMode() const {
124 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700125}
126
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700127bool DisplayDevice::isPoweredOn() const {
128 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700129}
130
Ady Abraham2139f732019-11-13 18:56:40 -0800131void DisplayDevice::setActiveConfig(HwcConfigIndexType mode) {
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700132 mActiveConfig = mode;
133}
134
Ady Abraham2139f732019-11-13 18:56:40 -0800135HwcConfigIndexType DisplayDevice::getActiveConfig() const {
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700136 return mActiveConfig;
137}
138
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800139ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700140 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800141}
142
Mathias Agopian28947d72012-08-08 18:51:15 -0700143void DisplayDevice::setLayerStack(uint32_t stack) {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800144 mCompositionDisplay->setLayerStackFilter(stack, isPrimary());
Mathias Agopian28947d72012-08-08 18:51:15 -0700145}
146
Michael Lentine47e45402014-07-18 15:34:25 -0700147void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700148 mCompositionDisplay->setBounds(ui::Size(newWidth, newHeight));
Michael Lentine47e45402014-07-18 15:34:25 -0700149}
150
Dominik Laskowski718f9602019-11-09 20:01:35 -0800151void DisplayDevice::setProjection(ui::Rotation orientation, Rect viewport, Rect frame) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700152 mOrientation = orientation;
153
154 const Rect& displayBounds = getCompositionDisplay()->getState().bounds;
155 const int w = displayBounds.width();
156 const int h = displayBounds.height();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800157
Peiyong Linefefaac2018-08-17 12:27:51 -0700158 ui::Transform R;
Dominik Laskowski718f9602019-11-09 20:01:35 -0800159 if (const auto flags = ui::Transform::toRotationFlags(orientation);
160 flags != ui::Transform::ROT_INVALID) {
161 R.set(flags, w, h);
162 }
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800163
164 if (!frame.isValid()) {
165 // the destination frame can be invalid if it has never been set,
166 // in that case we assume the whole display frame.
167 frame = Rect(w, h);
168 }
169
170 if (viewport.isEmpty()) {
171 // viewport can be invalid if it has never been set, in that case
172 // we assume the whole display size.
173 // it's also invalid to have an empty viewport, so we handle that
174 // case in the same way.
175 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700176 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800177 // viewport is always specified in the logical orientation
178 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700179 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800180 }
181 }
182
Peiyong Linefefaac2018-08-17 12:27:51 -0700183 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800184 float src_width = viewport.width();
185 float src_height = viewport.height();
186 float dst_width = frame.width();
187 float dst_height = frame.height();
188 if (src_width != dst_width || src_height != dst_height) {
189 float sx = dst_width / src_width;
190 float sy = dst_height / src_height;
191 S.set(sx, 0, 0, sy);
192 }
193
194 float src_x = viewport.left;
195 float src_y = viewport.top;
196 float dst_x = frame.left;
197 float dst_y = frame.top;
198 TL.set(-src_x, -src_y);
199 TP.set(dst_x, dst_y);
200
Lloyd Pique32cbe282018-10-19 13:09:22 -0700201 // need to take care of primary display rotation for globalTransform
Iris Chang7501ed62018-04-30 14:45:42 +0800202 // for case if the panel is not installed aligned with device orientation
Dominik Laskowski075d3172018-05-24 15:50:06 -0700203 if (isPrimary()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800204 if (const auto flags = ui::Transform::toRotationFlags(orientation + mPhysicalOrientation);
205 flags != ui::Transform::ROT_INVALID) {
206 R.set(flags, w, h);
207 }
Iris Chang7501ed62018-04-30 14:45:42 +0800208 }
209
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800210 // The viewport and frame are both in the logical orientation.
211 // Apply the logical translation, scale to physical size, apply the
212 // physical translation and finally rotate to the physical orientation.
Lloyd Pique32cbe282018-10-19 13:09:22 -0700213 ui::Transform globalTransform = R * TP * S * TL;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800214
Lloyd Pique32cbe282018-10-19 13:09:22 -0700215 const uint8_t type = globalTransform.getType();
216 const bool needsFiltering =
217 (!globalTransform.preserveRects() || (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800218
Lloyd Pique32cbe282018-10-19 13:09:22 -0700219 Rect scissor = globalTransform.transform(viewport);
220 if (scissor.isEmpty()) {
221 scissor = displayBounds;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800222 }
223
LuK1337a4adfb12019-09-18 18:44:49 +0200224 uint32_t transformOrientation;
225
Dominik Laskowski281644e2018-04-19 15:47:35 -0700226 if (isPrimary()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800227 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
228 transformOrientation = ui::Transform::toRotationFlags(orientation + mPhysicalOrientation);
LuK1337a4adfb12019-09-18 18:44:49 +0200229 } else {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800230 transformOrientation = ui::Transform::toRotationFlags(orientation);
Pablo Ceballos021623b2016-04-15 17:31:51 -0700231 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700232
LuK1337a4adfb12019-09-18 18:44:49 +0200233 getCompositionDisplay()->setProjection(globalTransform, transformOrientation,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234 frame, viewport, scissor, needsFiltering);
Mathias Agopian1b031492012-06-20 17:51:20 -0700235}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700236
Dominik Laskowski718f9602019-11-09 20:01:35 -0800237ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
238 return sPrimaryDisplayRotationFlags;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700239}
240
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700241std::string DisplayDevice::getDebugName() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700242 const auto id = getId() ? to_string(*getId()) + ", " : std::string();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700243 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
244 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
245 mDisplayName.c_str());
246}
247
Yiwei Zhang5434a782018-12-05 18:06:32 -0800248void DisplayDevice::dump(std::string& result) const {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800249 StringAppendF(&result, "+ %s\n", getDebugName().c_str());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700250
251 result.append(" ");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700252 StringAppendF(&result, "powerMode=%d, ", mPowerMode);
Ady Abraham2139f732019-11-13 18:56:40 -0800253 StringAppendF(&result, "activeConfig=%d, ", mActiveConfig.value());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700254 getCompositionDisplay()->dump(result);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700255}
Irvelffc9efc2016-07-27 15:16:37 -0700256
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700257bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
258 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700259}
260
Lloyd Pique45a165a2018-10-19 11:54:47 -0700261// ----------------------------------------------------------------------------
262
263const std::optional<DisplayId>& DisplayDevice::getId() const {
264 return mCompositionDisplay->getId();
265}
266
267bool DisplayDevice::isSecure() const {
268 return mCompositionDisplay->isSecure();
269}
270
Lloyd Pique32cbe282018-10-19 13:09:22 -0700271const Rect& DisplayDevice::getBounds() const {
272 return mCompositionDisplay->getState().bounds;
273}
274
275const Region& DisplayDevice::getUndefinedRegion() const {
276 return mCompositionDisplay->getState().undefinedRegion;
277}
278
279bool DisplayDevice::needsFiltering() const {
280 return mCompositionDisplay->getState().needsFiltering;
281}
282
283uint32_t DisplayDevice::getLayerStack() const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800284 return mCompositionDisplay->getState().layerStackId;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700285}
286
287const ui::Transform& DisplayDevice::getTransform() const {
288 return mCompositionDisplay->getState().transform;
289}
290
291const Rect& DisplayDevice::getViewport() const {
292 return mCompositionDisplay->getState().viewport;
293}
294
295const Rect& DisplayDevice::getFrame() const {
296 return mCompositionDisplay->getState().frame;
297}
298
299const Rect& DisplayDevice::getScissor() const {
300 return mCompositionDisplay->getState().scissor;
301}
302
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700303bool DisplayDevice::hasWideColorGamut() const {
304 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
305}
306
307bool DisplayDevice::hasHDR10PlusSupport() const {
308 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
309}
310
311bool DisplayDevice::hasHDR10Support() const {
312 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
313}
314
315bool DisplayDevice::hasHLGSupport() const {
316 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
317}
318
319bool DisplayDevice::hasDolbyVisionSupport() const {
320 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
321}
322
323int DisplayDevice::getSupportedPerFrameMetadata() const {
324 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
325}
326
327const HdrCapabilities& DisplayDevice::getHdrCapabilities() const {
328 return mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
329}
330
Dominik Laskowski663bd282018-04-19 15:26:54 -0700331std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700332
333} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800334
335// TODO(b/129481165): remove the #pragma below and fix conversion issues
336#pragma clang diagnostic pop // ignored "-Wconversion"