blob: eef00522dc0155ff5d4e609e76c5a24701bd27e2 [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
Ady Abraham690f4612021-07-01 23:24:03 -070025#define ATRACE_TAG ATRACE_TAG_GRAPHICS
26
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -070027#include <android-base/stringprintf.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070028#include <compositionengine/CompositionEngine.h>
29#include <compositionengine/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070030#include <compositionengine/DisplayColorProfile.h>
31#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070032#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070033#include <compositionengine/DisplaySurface.h>
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020034#include <compositionengine/ProjectionSpace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070035#include <compositionengine/RenderSurface.h>
36#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070037#include <compositionengine/impl/OutputCompositionState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070038#include <configstore/Utils.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070039#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080040#include <system/window.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070041#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070043#include "DisplayDevice.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080044#include "Layer.h"
Ady Abraham1b11bc62021-06-03 19:51:19 -070045#include "RefreshRateOverlay.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070046#include "SurfaceFlinger.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070047
Peiyong Linfd997e02018-03-28 15:29:00 -070048namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Peiyong Lin65248e02020-04-18 21:15:07 -070050namespace hal = hardware::graphics::composer::hal;
51
Yiwei Zhang5434a782018-12-05 18:06:32 -080052using android::base::StringAppendF;
Jaesoo Lee720a7242017-01-31 15:26:18 +090053
Dominik Laskowski718f9602019-11-09 20:01:35 -080054ui::Transform::RotationFlags DisplayDevice::sPrimaryDisplayRotationFlags = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -070055
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070056DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010057 const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070058 std::shared_ptr<compositionengine::Display> compositionDisplay)
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010059 : flinger(flinger),
60 hwComposer(hwComposer),
61 displayToken(displayToken),
62 compositionDisplay(compositionDisplay) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070063
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070064DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070065 : mFlinger(args.flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010066 mHwComposer(args.hwComposer),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070067 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080068 mSequenceId(args.sequenceId),
Dominik Laskowski55c85402020-01-21 16:25:47 -080069 mConnectionType(args.connectionType),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070070 mCompositionDisplay{args.compositionDisplay},
Ady Abraham690f4612021-07-01 23:24:03 -070071 mActiveModeFPSTrace("ActiveModeFPS -" + to_string(getId())),
72 mActiveModeFPSHwcTrace("ActiveModeFPS_HWC -" + to_string(getId())),
Dominik Laskowski718f9602019-11-09 20:01:35 -080073 mPhysicalOrientation(args.physicalOrientation),
Marin Shalamanov5801c942020-12-17 17:00:13 +010074 mSupportedModes(std::move(args.supportedModes)),
Ady Abraham3efa3942021-06-24 19:01:25 -070075 mIsPrimary(args.isPrimary),
76 mRefreshRateConfigs(std::move(args.refreshRateConfigs)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070077 mCompositionDisplay->editState().isSecure = args.isSecure;
Lloyd Pique31cb2942018-10-19 17:23:03 -070078 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070079 compositionengine::RenderSurfaceCreationArgsBuilder()
80 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
81 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
82 .setNativeWindow(std::move(args.nativeWindow))
83 .setDisplaySurface(std::move(args.displaySurface))
84 .setMaxTextureCacheSize(
85 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
86 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070087
Vishnu Nair9b079a22020-01-21 14:36:08 -080088 if (!mFlinger->mDisableClientCompositionCache &&
89 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
90 mCompositionDisplay->createClientCompositionCache(
91 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
92 }
93
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070094 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010095 compositionengine::DisplayColorProfileCreationArgsBuilder()
96 .setHasWideColorGamut(args.hasWideColorGamut)
97 .setHdrCapabilities(std::move(args.hdrCapabilities))
98 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
99 .setHwcColorModes(std::move(args.hwcColorModes))
100 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700101
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102 if (!mCompositionDisplay->isValid()) {
103 ALOGE("Composition Display did not validate!");
104 }
105
Lloyd Pique31cb2942018-10-19 17:23:03 -0700106 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700107
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108 setPowerMode(args.initialPowerMode);
Alec Mouriba013fa2018-10-16 12:43:11 -0700109
Jesse Hallffe1f192013-03-22 15:13:48 -0700110 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800111 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112}
113
Lloyd Pique09594832018-01-22 17:48:03 -0800114DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700115
Lloyd Pique45a165a2018-10-19 11:54:47 -0700116void DisplayDevice::disconnect() {
117 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700118}
119
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700120int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000121 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700122}
123
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700124int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000125 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700126}
127
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700128void DisplayDevice::setDisplayName(const std::string& displayName) {
129 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700130 // never override the name with an empty name
131 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700132 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700133 }
134}
135
Marin Shalamanove5cceea2020-04-30 18:13:10 +0200136void DisplayDevice::setDeviceProductInfo(std::optional<DeviceProductInfo> info) {
137 mDeviceProductInfo = std::move(info);
138}
139
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700140uint32_t DisplayDevice::getPageFlipCount() const {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700141 return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800142}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800143
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800144auto DisplayDevice::getInputInfo() const -> InputInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700145 gui::DisplayInfo info;
146 info.displayId = getLayerStack().id;
147
148 // The physical orientation is set when the orientation of the display panel is
149 // different than the default orientation of the device. Other services like
150 // InputFlinger do not know about this, so we do not need to expose the physical
151 // orientation of the panel outside of SurfaceFlinger.
152 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
153 auto width = getWidth();
154 auto height = getHeight();
155 if (inversePhysicalOrientation == ui::ROTATION_90 ||
156 inversePhysicalOrientation == ui::ROTATION_270) {
157 std::swap(width, height);
158 }
159 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
160 inversePhysicalOrientation),
161 width, height);
162 const auto& displayTransform = undoPhysicalOrientation * getTransform();
163 // Send the inverse display transform to input so it can convert display coordinates to
164 // logical display.
165 info.transform = displayTransform.inverse();
166
167 info.logicalWidth = getLayerStackSpaceRect().width();
168 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800169
170 return {.info = info,
171 .transform = displayTransform,
172 .receivesInput = receivesInput(),
173 .isSecure = isSecure()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700174}
175
Peiyong Lin65248e02020-04-18 21:15:07 -0700176void DisplayDevice::setPowerMode(hal::PowerMode mode) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700177 mPowerMode = mode;
Peiyong Lin65248e02020-04-18 21:15:07 -0700178 getCompositionDisplay()->setCompositionEnabled(mPowerMode != hal::PowerMode::OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700179}
180
Alec Mouri023c1882021-05-08 16:36:33 -0700181void DisplayDevice::enableLayerCaching(bool enable) {
182 getCompositionDisplay()->setLayerCachingEnabled(enable);
183}
184
Peiyong Lin65248e02020-04-18 21:15:07 -0700185hal::PowerMode DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700186 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700187}
188
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700189bool DisplayDevice::isPoweredOn() const {
Peiyong Lin65248e02020-04-18 21:15:07 -0700190 return mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700191}
192
Marin Shalamanov5801c942020-12-17 17:00:13 +0100193void DisplayDevice::setActiveMode(DisplayModeId id) {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100194 const auto mode = getMode(id);
195 LOG_FATAL_IF(!mode, "Cannot set active mode which is not supported.");
Ady Abraham690f4612021-07-01 23:24:03 -0700196 ATRACE_INT(mActiveModeFPSTrace.c_str(), mode->getFps().getIntValue());
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100197 mActiveMode = mode;
Ady Abraham3efa3942021-06-24 19:01:25 -0700198 if (mRefreshRateConfigs) {
199 mRefreshRateConfigs->setCurrentModeId(mActiveMode->getId());
200 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700201 if (mRefreshRateOverlay) {
202 mRefreshRateOverlay->changeRefreshRate(mActiveMode->getFps());
203 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700204}
205
Ady Abraham690f4612021-07-01 23:24:03 -0700206status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100207 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700208 hal::VsyncPeriodChangeTimeline* outTimeline) {
209 if (!info.mode || info.mode->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100210 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abraham690f4612021-07-01 23:24:03 -0700211 info.mode ? std::to_string(info.mode->getId().value()).c_str() : "null",
212 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100213 return BAD_VALUE;
214 }
Ady Abraham690f4612021-07-01 23:24:03 -0700215 mUpcomingActiveMode = info;
216 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.mode->getFps().getIntValue());
217 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(), info.mode->getHwcId(),
218 constraints, outTimeline);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100219}
220
Marin Shalamanov5801c942020-12-17 17:00:13 +0100221const DisplayModePtr& DisplayDevice::getActiveMode() const {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100222 return mActiveMode;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100223}
224
225const DisplayModes& DisplayDevice::getSupportedModes() const {
226 return mSupportedModes;
227}
228
229DisplayModePtr DisplayDevice::getMode(DisplayModeId modeId) const {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100230 const auto it = std::find_if(mSupportedModes.begin(), mSupportedModes.end(),
231 [&](DisplayModePtr mode) { return mode->getId() == modeId; });
232 if (it != mSupportedModes.end()) {
233 return *it;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100234 }
235 return nullptr;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700236}
237
Marin Shalamanov045b7002021-01-07 16:56:24 +0100238nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
239 const auto physicalId = getPhysicalId();
240 if (!mHwComposer.isConnected(physicalId)) {
241 return 0;
242 }
243
244 nsecs_t vsyncPeriod;
245 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
246 if (status == NO_ERROR) {
247 return vsyncPeriod;
248 }
249
250 return getActiveMode()->getFps().getPeriodNsecs();
251}
252
253nsecs_t DisplayDevice::getRefreshTimestamp() const {
254 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
255 const auto vsyncPeriodNanos = getVsyncPeriodFromHWC();
256 return now - ((now - mLastHwVsync) % vsyncPeriodNanos);
257}
258
259void DisplayDevice::onVsync(nsecs_t timestamp) {
260 mLastHwVsync = timestamp;
261}
262
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800263ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700264 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800265}
266
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800267void DisplayDevice::setLayerStack(ui::LayerStack stack) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700268 mCompositionDisplay->setLayerFilter({stack, isInternal()});
Ady Abraham1b11bc62021-06-03 19:51:19 -0700269 if (mRefreshRateOverlay) {
270 mRefreshRateOverlay->setLayerStack(stack);
271 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700272}
273
Evan Rosky2239b372021-05-20 13:43:47 -0700274void DisplayDevice::setFlags(uint32_t flags) {
275 mFlags = flags;
276}
277
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800278void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200279 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700280 const auto size = ui::Size(width, height);
281 mCompositionDisplay->setDisplaySize(size);
282 if (mRefreshRateOverlay) {
283 mRefreshRateOverlay->setViewport(size);
284 }
Michael Lentine47e45402014-07-18 15:34:25 -0700285}
286
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200287void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
288 Rect orientedDisplaySpaceRect) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700289 mOrientation = orientation;
290
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200291 if (isPrimary()) {
292 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
293 }
294
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200295 if (!orientedDisplaySpaceRect.isValid()) {
Marin Shalamanov4429cac2020-10-01 12:56:24 +0200296 // The destination frame can be invalid if it has never been set,
297 // in that case we assume the whole display size.
Angel Aguayob108a6d2021-08-06 21:34:57 +0000298 orientedDisplaySpaceRect =
299 getCompositionDisplay()->getState().displaySpace.getBoundsAsRect();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800300 }
301
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200302 if (layerStackSpaceRect.isEmpty()) {
Marin Shalamanov4429cac2020-10-01 12:56:24 +0200303 // The layerStackSpaceRect can be invalid if it has never been set, in that case
304 // we assume the whole framebuffer size.
Angel Aguayob108a6d2021-08-06 21:34:57 +0000305 layerStackSpaceRect =
306 getCompositionDisplay()->getState().framebufferSpace.getBoundsAsRect();
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200307 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200308 std::swap(layerStackSpaceRect.right, layerStackSpaceRect.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800309 }
310 }
311
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200312 // We need to take care of display rotation for globalTransform for case if the panel is not
313 // installed aligned with device orientation.
314 const auto transformOrientation = orientation + mPhysicalOrientation;
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200315 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
316 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700317}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700318
Alec Mouricdf16792021-12-10 13:16:06 -0800319void DisplayDevice::stageBrightness(float brightness) {
320 mStagedBrightness = brightness;
321}
322
323void DisplayDevice::persistBrightness(bool needsComposite) {
324 if (needsComposite && mStagedBrightness && mBrightness != *mStagedBrightness) {
325 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
326 mBrightness = *mStagedBrightness;
327 }
328 mStagedBrightness = std::nullopt;
329}
330
331std::optional<float> DisplayDevice::getStagedBrightness() const {
332 return mStagedBrightness;
333}
334
Dominik Laskowski718f9602019-11-09 20:01:35 -0800335ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
336 return sPrimaryDisplayRotationFlags;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700337}
338
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700339std::string DisplayDevice::getDebugName() const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800340 const char* type = "virtual";
341 if (mConnectionType) {
Ady Abraham7825c682021-05-17 15:12:14 -0700342 type = isInternal() ? "internal" : "external";
Dominik Laskowski55c85402020-01-21 16:25:47 -0800343 }
344
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200345 return base::StringPrintf("DisplayDevice{%s, %s%s, \"%s\"}", to_string(getId()).c_str(), type,
Dominik Laskowski55c85402020-01-21 16:25:47 -0800346 isPrimary() ? ", primary" : "", mDisplayName.c_str());
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700347}
348
Yiwei Zhang5434a782018-12-05 18:06:32 -0800349void DisplayDevice::dump(std::string& result) const {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800350 StringAppendF(&result, "+ %s\n", getDebugName().c_str());
Marin Shalamanov5801c942020-12-17 17:00:13 +0100351 StringAppendF(&result, " powerMode=%s (%d)\n", to_string(mPowerMode).c_str(),
Peiyong Lin65248e02020-04-18 21:15:07 -0700352 static_cast<int32_t>(mPowerMode));
Marin Shalamanov84b85482021-05-05 13:54:35 +0200353 const auto activeMode = getActiveMode();
Alec Mouri934e0cf2021-02-09 14:54:10 -0800354 StringAppendF(&result, " activeMode=%s\n",
Marin Shalamanov84b85482021-05-05 13:54:35 +0200355 activeMode ? to_string(*activeMode).c_str() : "none");
Marin Shalamanov5801c942020-12-17 17:00:13 +0100356
357 result.append(" supportedModes=\n");
358
359 for (const auto& mode : mSupportedModes) {
360 result.append(" ");
361 result.append(to_string(*mode));
362 result.append("\n");
363 }
364 StringAppendF(&result, " deviceProductInfo=");
Marin Shalamanove5cceea2020-04-30 18:13:10 +0200365 if (mDeviceProductInfo) {
366 mDeviceProductInfo->dump(result);
367 } else {
368 result.append("{}");
369 }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100370 result.append("\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700371 getCompositionDisplay()->dump(result);
Ady Abraham3efa3942021-06-24 19:01:25 -0700372
373 if (mRefreshRateConfigs) {
374 mRefreshRateConfigs->dump(result);
375 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700376}
Irvelffc9efc2016-07-27 15:16:37 -0700377
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700378bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
379 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700380}
381
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200382DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700383 return mCompositionDisplay->getId();
384}
385
386bool DisplayDevice::isSecure() const {
387 return mCompositionDisplay->isSecure();
388}
389
Angel Aguayob108a6d2021-08-06 21:34:57 +0000390const Rect DisplayDevice::getBounds() const {
391 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700392}
393
394const Region& DisplayDevice::getUndefinedRegion() const {
395 return mCompositionDisplay->getState().undefinedRegion;
396}
397
398bool DisplayDevice::needsFiltering() const {
399 return mCompositionDisplay->getState().needsFiltering;
400}
401
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800402ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700403 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700404}
405
Garfield Tan54edd912020-10-21 16:31:41 -0700406ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
407 return mCompositionDisplay->getTransformHint();
408}
409
Lloyd Pique32cbe282018-10-19 13:09:22 -0700410const ui::Transform& DisplayDevice::getTransform() const {
411 return mCompositionDisplay->getState().transform;
412}
413
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200414const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000415 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700416}
417
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200418const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000419 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700420}
421
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700422bool DisplayDevice::hasWideColorGamut() const {
423 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
424}
425
426bool DisplayDevice::hasHDR10PlusSupport() const {
427 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
428}
429
430bool DisplayDevice::hasHDR10Support() const {
431 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
432}
433
434bool DisplayDevice::hasHLGSupport() const {
435 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
436}
437
438bool DisplayDevice::hasDolbyVisionSupport() const {
439 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
440}
441
442int DisplayDevice::getSupportedPerFrameMetadata() const {
443 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
444}
445
Kriti Dang49ad4132021-01-08 11:49:56 +0100446void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
447 mOverrideHdrTypes = hdrTypes;
448}
449
450HdrCapabilities DisplayDevice::getHdrCapabilities() const {
451 const HdrCapabilities& capabilities =
452 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
453 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
454 if (!mOverrideHdrTypes.empty()) {
455 hdrTypes = mOverrideHdrTypes;
456 }
457 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
458 capabilities.getDesiredMaxAverageLuminance(),
459 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700460}
461
Kriti Dang646f8ec2022-01-18 14:35:02 +0100462ui::DisplayModeId DisplayDevice::getPreferredBootModeId() const {
463 return mCompositionDisplay->getPreferredBootModeId();
464}
465
Ady Abraham1b11bc62021-06-03 19:51:19 -0700466void DisplayDevice::enableRefreshRateOverlay(bool enable, bool showSpinnner) {
467 if (!enable) {
468 mRefreshRateOverlay.reset();
469 return;
470 }
471
472 const auto [lowFps, highFps] = mRefreshRateConfigs->getSupportedRefreshRateRange();
473 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(*mFlinger, lowFps.getIntValue(),
474 highFps.getIntValue(), showSpinnner);
475 mRefreshRateOverlay->setLayerStack(getLayerStack());
476 mRefreshRateOverlay->setViewport(getSize());
477 mRefreshRateOverlay->changeRefreshRate(getActiveMode()->getFps());
478}
479
480bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
481 bool timerExpired) {
482 if (mRefreshRateConfigs && mRefreshRateOverlay) {
483 const auto newRefreshRate =
484 mRefreshRateConfigs->onKernelTimerChanged(desiredModeId, timerExpired);
485 if (newRefreshRate) {
486 mRefreshRateOverlay->changeRefreshRate(*newRefreshRate);
487 return true;
488 }
489 }
490
491 return false;
492}
493
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700494void DisplayDevice::animateRefreshRateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700495 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700496 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700497 }
498}
499
Ady Abraham690f4612021-07-01 23:24:03 -0700500bool DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info) {
501 ATRACE_CALL();
502
503 LOG_ALWAYS_FATAL_IF(!info.mode, "desired mode not provided");
504 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.mode->getPhysicalDisplayId(), "DisplayId mismatch");
505
506 ALOGV("%s(%s)", __func__, to_string(*info.mode).c_str());
507
508 std::scoped_lock lock(mActiveModeLock);
509 if (mDesiredActiveModeChanged) {
510 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700511 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700512 mDesiredActiveMode = info;
513 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
514 return false;
515 }
516
517 // Check if we are already at the desired mode
518 if (getActiveMode()->getId() == info.mode->getId()) {
519 return false;
520 }
521
522 // Initiate a mode change.
523 mDesiredActiveModeChanged = true;
524 mDesiredActiveMode = info;
525 return true;
526}
527
528std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
529 std::scoped_lock lock(mActiveModeLock);
530 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
531 return std::nullopt;
532}
533
534void DisplayDevice::clearDesiredActiveModeState() {
535 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700536 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700537 mDesiredActiveModeChanged = false;
538}
539
Dominik Laskowski663bd282018-04-19 15:26:54 -0700540std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700541
542} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800543
544// TODO(b/129481165): remove the #pragma below and fix conversion issues
545#pragma clang diagnostic pop // ignored "-Wconversion"