blob: 2866a34a57500a23d969d8cd40ef486c275ba863 [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
Lloyd Pique45a165a2018-10-19 11:54:47 -070027#include <compositionengine/CompositionEngine.h>
28#include <compositionengine/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070029#include <compositionengine/DisplayColorProfile.h>
30#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070031#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070032#include <compositionengine/DisplaySurface.h>
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020033#include <compositionengine/ProjectionSpace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070034#include <compositionengine/RenderSurface.h>
35#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070036#include <compositionengine/impl/OutputCompositionState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070037#include <configstore/Utils.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070038#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080039#include <system/window.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070040#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070042#include "DisplayDevice.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080043#include "Layer.h"
Ady Abraham1b11bc62021-06-03 19:51:19 -070044#include "RefreshRateOverlay.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070045#include "SurfaceFlinger.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070046
Peiyong Linfd997e02018-03-28 15:29:00 -070047namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Peiyong Lin65248e02020-04-18 21:15:07 -070049namespace hal = hardware::graphics::composer::hal;
50
Dominik Laskowski718f9602019-11-09 20:01:35 -080051ui::Transform::RotationFlags DisplayDevice::sPrimaryDisplayRotationFlags = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -070052
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070053DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010054 const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070055 std::shared_ptr<compositionengine::Display> compositionDisplay)
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010056 : flinger(flinger),
57 hwComposer(hwComposer),
58 displayToken(displayToken),
59 compositionDisplay(compositionDisplay) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070060
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070061DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070062 : mFlinger(args.flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010063 mHwComposer(args.hwComposer),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070064 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080065 mSequenceId(args.sequenceId),
Dominik Laskowski55c85402020-01-21 16:25:47 -080066 mConnectionType(args.connectionType),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070067 mCompositionDisplay{args.compositionDisplay},
Ady Abraham690f4612021-07-01 23:24:03 -070068 mActiveModeFPSTrace("ActiveModeFPS -" + to_string(getId())),
69 mActiveModeFPSHwcTrace("ActiveModeFPS_HWC -" + to_string(getId())),
Dominik Laskowski718f9602019-11-09 20:01:35 -080070 mPhysicalOrientation(args.physicalOrientation),
Marin Shalamanov5801c942020-12-17 17:00:13 +010071 mSupportedModes(std::move(args.supportedModes)),
Ady Abraham3efa3942021-06-24 19:01:25 -070072 mIsPrimary(args.isPrimary),
73 mRefreshRateConfigs(std::move(args.refreshRateConfigs)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070074 mCompositionDisplay->editState().isSecure = args.isSecure;
Lloyd Pique31cb2942018-10-19 17:23:03 -070075 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070076 compositionengine::RenderSurfaceCreationArgsBuilder()
77 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
78 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
79 .setNativeWindow(std::move(args.nativeWindow))
80 .setDisplaySurface(std::move(args.displaySurface))
81 .setMaxTextureCacheSize(
82 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
83 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070084
Vishnu Nair9b079a22020-01-21 14:36:08 -080085 if (!mFlinger->mDisableClientCompositionCache &&
86 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
87 mCompositionDisplay->createClientCompositionCache(
88 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
89 }
90
Vishnu Naira3140382022-02-24 14:07:11 -080091 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
Alec Mouridda07d92022-04-25 22:39:25 +000092 mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070093 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010094 compositionengine::DisplayColorProfileCreationArgsBuilder()
95 .setHasWideColorGamut(args.hasWideColorGamut)
96 .setHdrCapabilities(std::move(args.hdrCapabilities))
97 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
98 .setHwcColorModes(std::move(args.hwcColorModes))
99 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700100
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101 if (!mCompositionDisplay->isValid()) {
102 ALOGE("Composition Display did not validate!");
103 }
104
Lloyd Pique31cb2942018-10-19 17:23:03 -0700105 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700106
linpeter1c69cd32022-07-13 19:20:48 +0800107 if (args.initialPowerMode.has_value()) setPowerMode(args.initialPowerMode.value());
Alec Mouriba013fa2018-10-16 12:43:11 -0700108
Jesse Hallffe1f192013-03-22 15:13:48 -0700109 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800110 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111}
112
Lloyd Pique09594832018-01-22 17:48:03 -0800113DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700114
Lloyd Pique45a165a2018-10-19 11:54:47 -0700115void DisplayDevice::disconnect() {
116 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700117}
118
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700119int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000120 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700121}
122
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700123int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000124 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700125}
126
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700127void DisplayDevice::setDisplayName(const std::string& displayName) {
128 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700129 // never override the name with an empty name
130 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700131 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700132 }
133}
134
Marin Shalamanove5cceea2020-04-30 18:13:10 +0200135void DisplayDevice::setDeviceProductInfo(std::optional<DeviceProductInfo> info) {
136 mDeviceProductInfo = std::move(info);
137}
138
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800139auto DisplayDevice::getInputInfo() const -> InputInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700140 gui::DisplayInfo info;
141 info.displayId = getLayerStack().id;
142
143 // The physical orientation is set when the orientation of the display panel is
144 // different than the default orientation of the device. Other services like
145 // InputFlinger do not know about this, so we do not need to expose the physical
146 // orientation of the panel outside of SurfaceFlinger.
147 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
148 auto width = getWidth();
149 auto height = getHeight();
150 if (inversePhysicalOrientation == ui::ROTATION_90 ||
151 inversePhysicalOrientation == ui::ROTATION_270) {
152 std::swap(width, height);
153 }
154 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
155 inversePhysicalOrientation),
156 width, height);
157 const auto& displayTransform = undoPhysicalOrientation * getTransform();
158 // Send the inverse display transform to input so it can convert display coordinates to
159 // logical display.
160 info.transform = displayTransform.inverse();
161
162 info.logicalWidth = getLayerStackSpaceRect().width();
163 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800164
165 return {.info = info,
166 .transform = displayTransform,
167 .receivesInput = receivesInput(),
168 .isSecure = isSecure()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700169}
170
Peiyong Lin65248e02020-04-18 21:15:07 -0700171void DisplayDevice::setPowerMode(hal::PowerMode mode) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700172 mPowerMode = mode;
linpeter1c69cd32022-07-13 19:20:48 +0800173
174 getCompositionDisplay()->setCompositionEnabled(mPowerMode.has_value() &&
175 *mPowerMode != hal::PowerMode::OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700176}
177
Alec Mouri023c1882021-05-08 16:36:33 -0700178void DisplayDevice::enableLayerCaching(bool enable) {
179 getCompositionDisplay()->setLayerCachingEnabled(enable);
180}
181
linpeter1c69cd32022-07-13 19:20:48 +0800182std::optional<hal::PowerMode> DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700183 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700184}
185
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700186bool DisplayDevice::isPoweredOn() const {
linpeter1c69cd32022-07-13 19:20:48 +0800187 return mPowerMode && *mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700188}
189
Marin Shalamanov5801c942020-12-17 17:00:13 +0100190void DisplayDevice::setActiveMode(DisplayModeId id) {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100191 const auto mode = getMode(id);
192 LOG_FATAL_IF(!mode, "Cannot set active mode which is not supported.");
Ady Abraham690f4612021-07-01 23:24:03 -0700193 ATRACE_INT(mActiveModeFPSTrace.c_str(), mode->getFps().getIntValue());
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100194 mActiveMode = mode;
Ady Abraham3efa3942021-06-24 19:01:25 -0700195 if (mRefreshRateConfigs) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800196 mRefreshRateConfigs->setActiveModeId(mActiveMode->getId());
Ady Abraham3efa3942021-06-24 19:01:25 -0700197 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700198 if (mRefreshRateOverlay) {
199 mRefreshRateOverlay->changeRefreshRate(mActiveMode->getFps());
200 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700201}
202
Ady Abraham690f4612021-07-01 23:24:03 -0700203status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100204 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700205 hal::VsyncPeriodChangeTimeline* outTimeline) {
206 if (!info.mode || info.mode->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100207 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abraham690f4612021-07-01 23:24:03 -0700208 info.mode ? std::to_string(info.mode->getId().value()).c_str() : "null",
209 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100210 return BAD_VALUE;
211 }
Ady Abrahama92a5682022-06-07 23:35:38 +0000212 mNumModeSwitchesInPolicy++;
Ady Abraham690f4612021-07-01 23:24:03 -0700213 mUpcomingActiveMode = info;
214 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.mode->getFps().getIntValue());
215 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(), info.mode->getHwcId(),
216 constraints, outTimeline);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100217}
218
Marin Shalamanov5801c942020-12-17 17:00:13 +0100219const DisplayModePtr& DisplayDevice::getActiveMode() const {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100220 return mActiveMode;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100221}
222
223const DisplayModes& DisplayDevice::getSupportedModes() const {
224 return mSupportedModes;
225}
226
227DisplayModePtr DisplayDevice::getMode(DisplayModeId modeId) const {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800228 const DisplayModePtr nullMode;
229 return mSupportedModes.get(modeId).value_or(std::cref(nullMode));
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700230}
231
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800232std::optional<DisplayModeId> DisplayDevice::translateModeId(hal::HWConfigId hwcId) const {
233 const auto it =
234 std::find_if(mSupportedModes.begin(), mSupportedModes.end(),
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800235 [hwcId](const auto& pair) { return pair.second->getHwcId() == hwcId; });
Kriti Dang30e378d2022-02-18 15:09:12 +0100236 if (it != mSupportedModes.end()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800237 return it->second->getId();
Kriti Dang30e378d2022-02-18 15:09:12 +0100238 }
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800239 return {};
Kriti Dang30e378d2022-02-18 15:09:12 +0100240}
241
Marin Shalamanov045b7002021-01-07 16:56:24 +0100242nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
243 const auto physicalId = getPhysicalId();
244 if (!mHwComposer.isConnected(physicalId)) {
245 return 0;
246 }
247
248 nsecs_t vsyncPeriod;
249 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
250 if (status == NO_ERROR) {
251 return vsyncPeriod;
252 }
253
254 return getActiveMode()->getFps().getPeriodNsecs();
255}
256
257nsecs_t DisplayDevice::getRefreshTimestamp() const {
258 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
259 const auto vsyncPeriodNanos = getVsyncPeriodFromHWC();
260 return now - ((now - mLastHwVsync) % vsyncPeriodNanos);
261}
262
263void DisplayDevice::onVsync(nsecs_t timestamp) {
264 mLastHwVsync = timestamp;
265}
266
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800267ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700268 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800269}
270
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800271void DisplayDevice::setLayerStack(ui::LayerStack stack) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700272 mCompositionDisplay->setLayerFilter({stack, isInternal()});
Ady Abraham1b11bc62021-06-03 19:51:19 -0700273 if (mRefreshRateOverlay) {
274 mRefreshRateOverlay->setLayerStack(stack);
275 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700276}
277
Evan Rosky2239b372021-05-20 13:43:47 -0700278void DisplayDevice::setFlags(uint32_t flags) {
279 mFlags = flags;
280}
281
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800282void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200283 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700284 const auto size = ui::Size(width, height);
285 mCompositionDisplay->setDisplaySize(size);
286 if (mRefreshRateOverlay) {
287 mRefreshRateOverlay->setViewport(size);
288 }
Michael Lentine47e45402014-07-18 15:34:25 -0700289}
290
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200291void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
292 Rect orientedDisplaySpaceRect) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700293 mOrientation = orientation;
294
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200295 if (isPrimary()) {
296 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
297 }
298
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200299 // We need to take care of display rotation for globalTransform for case if the panel is not
300 // installed aligned with device orientation.
301 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000302
303 const auto& state = getCompositionDisplay()->getState();
304
305 // If the layer stack and destination frames have never been set, then configure them to be the
306 // same as the physical device, taking into account the total transform.
307 if (!orientedDisplaySpaceRect.isValid()) {
308 ui::Size bounds = state.displaySpace.getBounds();
309 bounds.rotate(transformOrientation);
310 orientedDisplaySpaceRect = Rect(bounds);
311 }
312 if (layerStackSpaceRect.isEmpty()) {
313 ui::Size bounds = state.framebufferSpace.getBounds();
314 bounds.rotate(transformOrientation);
315 layerStackSpaceRect = Rect(bounds);
316 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200317 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
318 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700319}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700320
Alec Mouricdf16792021-12-10 13:16:06 -0800321void DisplayDevice::stageBrightness(float brightness) {
322 mStagedBrightness = brightness;
323}
324
325void DisplayDevice::persistBrightness(bool needsComposite) {
326 if (needsComposite && mStagedBrightness && mBrightness != *mStagedBrightness) {
327 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
328 mBrightness = *mStagedBrightness;
329 }
330 mStagedBrightness = std::nullopt;
331}
332
333std::optional<float> DisplayDevice::getStagedBrightness() const {
334 return mStagedBrightness;
335}
336
Dominik Laskowski718f9602019-11-09 20:01:35 -0800337ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
338 return sPrimaryDisplayRotationFlags;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700339}
340
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700341std::string DisplayDevice::getDebugName() const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700342 using namespace std::string_literals;
343
344 std::string name = "Display "s + to_string(getId()) + " ("s;
345
Dominik Laskowski55c85402020-01-21 16:25:47 -0800346 if (mConnectionType) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700347 name += isInternal() ? "internal"s : "external"s;
348 } else {
349 name += "virtual"s;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800350 }
351
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700352 if (isPrimary()) {
353 name += ", primary"s;
354 }
355
356 return name + ", \""s + mDisplayName + "\")"s;
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700357}
358
Yiwei Zhang5434a782018-12-05 18:06:32 -0800359void DisplayDevice::dump(std::string& result) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700360 using namespace std::string_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100361
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700362 result += getDebugName();
363
364 if (!isVirtual()) {
365 result += "\n deviceProductInfo="s;
366 if (mDeviceProductInfo) {
367 mDeviceProductInfo->dump(result);
368 } else {
369 result += "{}"s;
370 }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100371 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800372
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700373 result += "\n powerMode="s;
linpeter1c69cd32022-07-13 19:20:48 +0800374 result += mPowerMode.has_value() ? to_string(mPowerMode.value()) : "OFF(reset)";
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700375 result += '\n';
Ady Abraham3efa3942021-06-24 19:01:25 -0700376
377 if (mRefreshRateConfigs) {
378 mRefreshRateConfigs->dump(result);
379 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700380}
Irvelffc9efc2016-07-27 15:16:37 -0700381
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700382bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
383 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700384}
385
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200386DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700387 return mCompositionDisplay->getId();
388}
389
390bool DisplayDevice::isSecure() const {
391 return mCompositionDisplay->isSecure();
392}
393
Angel Aguayob108a6d2021-08-06 21:34:57 +0000394const Rect DisplayDevice::getBounds() const {
395 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700396}
397
398const Region& DisplayDevice::getUndefinedRegion() const {
399 return mCompositionDisplay->getState().undefinedRegion;
400}
401
402bool DisplayDevice::needsFiltering() const {
403 return mCompositionDisplay->getState().needsFiltering;
404}
405
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800406ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700407 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700408}
409
Garfield Tan54edd912020-10-21 16:31:41 -0700410ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
411 return mCompositionDisplay->getTransformHint();
412}
413
Lloyd Pique32cbe282018-10-19 13:09:22 -0700414const ui::Transform& DisplayDevice::getTransform() const {
415 return mCompositionDisplay->getState().transform;
416}
417
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200418const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000419 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700420}
421
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200422const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000423 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700424}
425
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700426bool DisplayDevice::hasWideColorGamut() const {
427 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
428}
429
430bool DisplayDevice::hasHDR10PlusSupport() const {
431 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
432}
433
434bool DisplayDevice::hasHDR10Support() const {
435 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
436}
437
438bool DisplayDevice::hasHLGSupport() const {
439 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
440}
441
442bool DisplayDevice::hasDolbyVisionSupport() const {
443 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
444}
445
446int DisplayDevice::getSupportedPerFrameMetadata() const {
447 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
448}
449
Kriti Dang49ad4132021-01-08 11:49:56 +0100450void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
451 mOverrideHdrTypes = hdrTypes;
452}
453
454HdrCapabilities DisplayDevice::getHdrCapabilities() const {
455 const HdrCapabilities& capabilities =
456 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
457 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
458 if (!mOverrideHdrTypes.empty()) {
459 hdrTypes = mOverrideHdrTypes;
460 }
461 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
462 capabilities.getDesiredMaxAverageLuminance(),
463 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700464}
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
Dominik Laskowski8c4356c2022-03-21 08:19:54 -0700472 const auto fpsRange = mRefreshRateConfigs->getSupportedRefreshRateRange();
473 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(fpsRange, showSpinnner);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700474 mRefreshRateOverlay->setLayerStack(getLayerStack());
475 mRefreshRateOverlay->setViewport(getSize());
476 mRefreshRateOverlay->changeRefreshRate(getActiveMode()->getFps());
477}
478
479bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
480 bool timerExpired) {
481 if (mRefreshRateConfigs && mRefreshRateOverlay) {
482 const auto newRefreshRate =
483 mRefreshRateConfigs->onKernelTimerChanged(desiredModeId, timerExpired);
484 if (newRefreshRate) {
485 mRefreshRateOverlay->changeRefreshRate(*newRefreshRate);
486 return true;
487 }
488 }
489
490 return false;
491}
492
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700493void DisplayDevice::animateRefreshRateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700494 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700495 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700496 }
497}
498
Ady Abraham690f4612021-07-01 23:24:03 -0700499bool DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info) {
500 ATRACE_CALL();
501
502 LOG_ALWAYS_FATAL_IF(!info.mode, "desired mode not provided");
503 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.mode->getPhysicalDisplayId(), "DisplayId mismatch");
504
505 ALOGV("%s(%s)", __func__, to_string(*info.mode).c_str());
506
507 std::scoped_lock lock(mActiveModeLock);
508 if (mDesiredActiveModeChanged) {
509 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700510 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700511 mDesiredActiveMode = info;
512 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
513 return false;
514 }
515
516 // Check if we are already at the desired mode
517 if (getActiveMode()->getId() == info.mode->getId()) {
518 return false;
519 }
520
521 // Initiate a mode change.
522 mDesiredActiveModeChanged = true;
523 mDesiredActiveMode = info;
524 return true;
525}
526
527std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
528 std::scoped_lock lock(mActiveModeLock);
529 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
530 return std::nullopt;
531}
532
533void DisplayDevice::clearDesiredActiveModeState() {
534 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700535 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700536 mDesiredActiveModeChanged = false;
537}
538
Ady Abrahama92a5682022-06-07 23:35:38 +0000539status_t DisplayDevice::setRefreshRatePolicy(
540 const std::optional<scheduler::RefreshRateConfigs::Policy>& policy, bool overridePolicy) {
541 const auto oldPolicy = mRefreshRateConfigs->getCurrentPolicy();
542 const status_t setPolicyResult = overridePolicy
543 ? mRefreshRateConfigs->setOverridePolicy(policy)
544 : mRefreshRateConfigs->setDisplayManagerPolicy(*policy);
545
546 if (setPolicyResult == OK) {
547 const int numModeChanges = mNumModeSwitchesInPolicy.exchange(0);
548
549 ALOGI("Display %s policy changed\n"
550 "Previous: {%s}\n"
551 "Current: {%s}\n"
552 "%d mode changes were performed under the previous policy",
553 to_string(getId()).c_str(), oldPolicy.toString().c_str(),
554 policy ? policy->toString().c_str() : "null", numModeChanges);
555 }
556
557 return setPolicyResult;
558}
559
Dominik Laskowski663bd282018-04-19 15:26:54 -0700560std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700561
562} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800563
564// TODO(b/129481165): remove the #pragma below and fix conversion issues
565#pragma clang diagnostic pop // ignored "-Wconversion"