blob: 269a1fa17b38c5592a26c004980fd470a2280686 [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
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107 setPowerMode(args.initialPowerMode);
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
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700139uint32_t DisplayDevice::getPageFlipCount() const {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700140 return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800141}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800142
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800143auto DisplayDevice::getInputInfo() const -> InputInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700144 gui::DisplayInfo info;
145 info.displayId = getLayerStack().id;
146
147 // The physical orientation is set when the orientation of the display panel is
148 // different than the default orientation of the device. Other services like
149 // InputFlinger do not know about this, so we do not need to expose the physical
150 // orientation of the panel outside of SurfaceFlinger.
151 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
152 auto width = getWidth();
153 auto height = getHeight();
154 if (inversePhysicalOrientation == ui::ROTATION_90 ||
155 inversePhysicalOrientation == ui::ROTATION_270) {
156 std::swap(width, height);
157 }
158 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
159 inversePhysicalOrientation),
160 width, height);
161 const auto& displayTransform = undoPhysicalOrientation * getTransform();
162 // Send the inverse display transform to input so it can convert display coordinates to
163 // logical display.
164 info.transform = displayTransform.inverse();
165
166 info.logicalWidth = getLayerStackSpaceRect().width();
167 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800168
169 return {.info = info,
170 .transform = displayTransform,
171 .receivesInput = receivesInput(),
172 .isSecure = isSecure()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700173}
174
Peiyong Lin65248e02020-04-18 21:15:07 -0700175void DisplayDevice::setPowerMode(hal::PowerMode mode) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700176 mPowerMode = mode;
Peiyong Lin65248e02020-04-18 21:15:07 -0700177 getCompositionDisplay()->setCompositionEnabled(mPowerMode != hal::PowerMode::OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700178}
179
Alec Mouri023c1882021-05-08 16:36:33 -0700180void DisplayDevice::enableLayerCaching(bool enable) {
181 getCompositionDisplay()->setLayerCachingEnabled(enable);
182}
183
Peiyong Lin65248e02020-04-18 21:15:07 -0700184hal::PowerMode DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700185 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700186}
187
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700188bool DisplayDevice::isPoweredOn() const {
Peiyong Lin65248e02020-04-18 21:15:07 -0700189 return mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700190}
191
Marin Shalamanov5801c942020-12-17 17:00:13 +0100192void DisplayDevice::setActiveMode(DisplayModeId id) {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100193 const auto mode = getMode(id);
194 LOG_FATAL_IF(!mode, "Cannot set active mode which is not supported.");
Ady Abraham690f4612021-07-01 23:24:03 -0700195 ATRACE_INT(mActiveModeFPSTrace.c_str(), mode->getFps().getIntValue());
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100196 mActiveMode = mode;
Ady Abraham3efa3942021-06-24 19:01:25 -0700197 if (mRefreshRateConfigs) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800198 mRefreshRateConfigs->setActiveModeId(mActiveMode->getId());
Ady Abraham3efa3942021-06-24 19:01:25 -0700199 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700200 if (mRefreshRateOverlay) {
201 mRefreshRateOverlay->changeRefreshRate(mActiveMode->getFps());
202 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700203}
204
Ady Abraham690f4612021-07-01 23:24:03 -0700205status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100206 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700207 hal::VsyncPeriodChangeTimeline* outTimeline) {
208 if (!info.mode || info.mode->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100209 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abraham690f4612021-07-01 23:24:03 -0700210 info.mode ? std::to_string(info.mode->getId().value()).c_str() : "null",
211 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100212 return BAD_VALUE;
213 }
Ady Abrahama92a5682022-06-07 23:35:38 +0000214 mNumModeSwitchesInPolicy++;
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 {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800230 const DisplayModePtr nullMode;
231 return mSupportedModes.get(modeId).value_or(std::cref(nullMode));
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700232}
233
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800234std::optional<DisplayModeId> DisplayDevice::translateModeId(hal::HWConfigId hwcId) const {
235 const auto it =
236 std::find_if(mSupportedModes.begin(), mSupportedModes.end(),
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800237 [hwcId](const auto& pair) { return pair.second->getHwcId() == hwcId; });
Kriti Dang30e378d2022-02-18 15:09:12 +0100238 if (it != mSupportedModes.end()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800239 return it->second->getId();
Kriti Dang30e378d2022-02-18 15:09:12 +0100240 }
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800241 return {};
Kriti Dang30e378d2022-02-18 15:09:12 +0100242}
243
Marin Shalamanov045b7002021-01-07 16:56:24 +0100244nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
245 const auto physicalId = getPhysicalId();
246 if (!mHwComposer.isConnected(physicalId)) {
247 return 0;
248 }
249
250 nsecs_t vsyncPeriod;
251 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
252 if (status == NO_ERROR) {
253 return vsyncPeriod;
254 }
255
256 return getActiveMode()->getFps().getPeriodNsecs();
257}
258
259nsecs_t DisplayDevice::getRefreshTimestamp() const {
260 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
261 const auto vsyncPeriodNanos = getVsyncPeriodFromHWC();
262 return now - ((now - mLastHwVsync) % vsyncPeriodNanos);
263}
264
265void DisplayDevice::onVsync(nsecs_t timestamp) {
266 mLastHwVsync = timestamp;
267}
268
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800269ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700270 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800271}
272
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800273void DisplayDevice::setLayerStack(ui::LayerStack stack) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700274 mCompositionDisplay->setLayerFilter({stack, isInternal()});
Ady Abraham1b11bc62021-06-03 19:51:19 -0700275 if (mRefreshRateOverlay) {
276 mRefreshRateOverlay->setLayerStack(stack);
277 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700278}
279
Evan Rosky2239b372021-05-20 13:43:47 -0700280void DisplayDevice::setFlags(uint32_t flags) {
281 mFlags = flags;
282}
283
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800284void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200285 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700286 const auto size = ui::Size(width, height);
287 mCompositionDisplay->setDisplaySize(size);
288 if (mRefreshRateOverlay) {
289 mRefreshRateOverlay->setViewport(size);
290 }
Michael Lentine47e45402014-07-18 15:34:25 -0700291}
292
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200293void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
294 Rect orientedDisplaySpaceRect) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700295 mOrientation = orientation;
296
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200297 if (isPrimary()) {
298 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
299 }
300
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200301 // We need to take care of display rotation for globalTransform for case if the panel is not
302 // installed aligned with device orientation.
303 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000304
305 const auto& state = getCompositionDisplay()->getState();
306
307 // If the layer stack and destination frames have never been set, then configure them to be the
308 // same as the physical device, taking into account the total transform.
309 if (!orientedDisplaySpaceRect.isValid()) {
310 ui::Size bounds = state.displaySpace.getBounds();
311 bounds.rotate(transformOrientation);
312 orientedDisplaySpaceRect = Rect(bounds);
313 }
314 if (layerStackSpaceRect.isEmpty()) {
315 ui::Size bounds = state.framebufferSpace.getBounds();
316 bounds.rotate(transformOrientation);
317 layerStackSpaceRect = Rect(bounds);
318 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200319 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
320 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700321}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700322
Alec Mouricdf16792021-12-10 13:16:06 -0800323void DisplayDevice::stageBrightness(float brightness) {
324 mStagedBrightness = brightness;
325}
326
327void DisplayDevice::persistBrightness(bool needsComposite) {
joenchen42928102022-09-06 18:03:57 +0000328 if (mStagedBrightness && mBrightness != *mStagedBrightness) {
329 if (needsComposite) {
330 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
331 }
Alec Mouricdf16792021-12-10 13:16:06 -0800332 mBrightness = *mStagedBrightness;
333 }
334 mStagedBrightness = std::nullopt;
335}
336
337std::optional<float> DisplayDevice::getStagedBrightness() const {
338 return mStagedBrightness;
339}
340
Dominik Laskowski718f9602019-11-09 20:01:35 -0800341ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
342 return sPrimaryDisplayRotationFlags;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700343}
344
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700345std::string DisplayDevice::getDebugName() const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700346 using namespace std::string_literals;
347
348 std::string name = "Display "s + to_string(getId()) + " ("s;
349
Dominik Laskowski55c85402020-01-21 16:25:47 -0800350 if (mConnectionType) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700351 name += isInternal() ? "internal"s : "external"s;
352 } else {
353 name += "virtual"s;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800354 }
355
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700356 if (isPrimary()) {
357 name += ", primary"s;
358 }
359
360 return name + ", \""s + mDisplayName + "\")"s;
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700361}
362
Yiwei Zhang5434a782018-12-05 18:06:32 -0800363void DisplayDevice::dump(std::string& result) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700364 using namespace std::string_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100365
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700366 result += getDebugName();
367
368 if (!isVirtual()) {
369 result += "\n deviceProductInfo="s;
370 if (mDeviceProductInfo) {
371 mDeviceProductInfo->dump(result);
372 } else {
373 result += "{}"s;
374 }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100375 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800376
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700377 result += "\n powerMode="s;
378 result += to_string(mPowerMode);
379 result += '\n';
Ady Abraham3efa3942021-06-24 19:01:25 -0700380
381 if (mRefreshRateConfigs) {
382 mRefreshRateConfigs->dump(result);
383 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700384}
Irvelffc9efc2016-07-27 15:16:37 -0700385
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700386bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
387 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700388}
389
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200390DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700391 return mCompositionDisplay->getId();
392}
393
394bool DisplayDevice::isSecure() const {
395 return mCompositionDisplay->isSecure();
396}
397
Angel Aguayob108a6d2021-08-06 21:34:57 +0000398const Rect DisplayDevice::getBounds() const {
399 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700400}
401
402const Region& DisplayDevice::getUndefinedRegion() const {
403 return mCompositionDisplay->getState().undefinedRegion;
404}
405
406bool DisplayDevice::needsFiltering() const {
407 return mCompositionDisplay->getState().needsFiltering;
408}
409
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800410ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700411 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700412}
413
Garfield Tan54edd912020-10-21 16:31:41 -0700414ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
415 return mCompositionDisplay->getTransformHint();
416}
417
Lloyd Pique32cbe282018-10-19 13:09:22 -0700418const ui::Transform& DisplayDevice::getTransform() const {
419 return mCompositionDisplay->getState().transform;
420}
421
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200422const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000423 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700424}
425
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200426const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000427 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700428}
429
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700430bool DisplayDevice::hasWideColorGamut() const {
431 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
432}
433
434bool DisplayDevice::hasHDR10PlusSupport() const {
435 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
436}
437
438bool DisplayDevice::hasHDR10Support() const {
439 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
440}
441
442bool DisplayDevice::hasHLGSupport() const {
443 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
444}
445
446bool DisplayDevice::hasDolbyVisionSupport() const {
447 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
448}
449
450int DisplayDevice::getSupportedPerFrameMetadata() const {
451 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
452}
453
Kriti Dang49ad4132021-01-08 11:49:56 +0100454void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
455 mOverrideHdrTypes = hdrTypes;
456}
457
458HdrCapabilities DisplayDevice::getHdrCapabilities() const {
459 const HdrCapabilities& capabilities =
460 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
461 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
462 if (!mOverrideHdrTypes.empty()) {
463 hdrTypes = mOverrideHdrTypes;
464 }
465 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
466 capabilities.getDesiredMaxAverageLuminance(),
467 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700468}
469
Ady Abraham1b11bc62021-06-03 19:51:19 -0700470void DisplayDevice::enableRefreshRateOverlay(bool enable, bool showSpinnner) {
471 if (!enable) {
472 mRefreshRateOverlay.reset();
473 return;
474 }
475
Dominik Laskowski8c4356c2022-03-21 08:19:54 -0700476 const auto fpsRange = mRefreshRateConfigs->getSupportedRefreshRateRange();
477 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(fpsRange, showSpinnner);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700478 mRefreshRateOverlay->setLayerStack(getLayerStack());
479 mRefreshRateOverlay->setViewport(getSize());
480 mRefreshRateOverlay->changeRefreshRate(getActiveMode()->getFps());
481}
482
483bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
484 bool timerExpired) {
485 if (mRefreshRateConfigs && mRefreshRateOverlay) {
486 const auto newRefreshRate =
487 mRefreshRateConfigs->onKernelTimerChanged(desiredModeId, timerExpired);
488 if (newRefreshRate) {
489 mRefreshRateOverlay->changeRefreshRate(*newRefreshRate);
490 return true;
491 }
492 }
493
494 return false;
495}
496
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700497void DisplayDevice::animateRefreshRateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700498 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700499 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700500 }
501}
502
Ady Abraham690f4612021-07-01 23:24:03 -0700503bool DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info) {
504 ATRACE_CALL();
505
506 LOG_ALWAYS_FATAL_IF(!info.mode, "desired mode not provided");
507 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.mode->getPhysicalDisplayId(), "DisplayId mismatch");
508
509 ALOGV("%s(%s)", __func__, to_string(*info.mode).c_str());
510
511 std::scoped_lock lock(mActiveModeLock);
512 if (mDesiredActiveModeChanged) {
513 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700514 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700515 mDesiredActiveMode = info;
516 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
517 return false;
518 }
519
520 // Check if we are already at the desired mode
521 if (getActiveMode()->getId() == info.mode->getId()) {
522 return false;
523 }
524
525 // Initiate a mode change.
526 mDesiredActiveModeChanged = true;
527 mDesiredActiveMode = info;
528 return true;
529}
530
531std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
532 std::scoped_lock lock(mActiveModeLock);
533 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
534 return std::nullopt;
535}
536
537void DisplayDevice::clearDesiredActiveModeState() {
538 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700539 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700540 mDesiredActiveModeChanged = false;
541}
542
Ady Abrahama92a5682022-06-07 23:35:38 +0000543status_t DisplayDevice::setRefreshRatePolicy(
544 const std::optional<scheduler::RefreshRateConfigs::Policy>& policy, bool overridePolicy) {
545 const auto oldPolicy = mRefreshRateConfigs->getCurrentPolicy();
546 const status_t setPolicyResult = overridePolicy
547 ? mRefreshRateConfigs->setOverridePolicy(policy)
548 : mRefreshRateConfigs->setDisplayManagerPolicy(*policy);
549
550 if (setPolicyResult == OK) {
551 const int numModeChanges = mNumModeSwitchesInPolicy.exchange(0);
552
553 ALOGI("Display %s policy changed\n"
554 "Previous: {%s}\n"
555 "Current: {%s}\n"
556 "%d mode changes were performed under the previous policy",
557 to_string(getId()).c_str(), oldPolicy.toString().c_str(),
558 policy ? policy->toString().c_str() : "null", numModeChanges);
559 }
560
561 return setPolicyResult;
562}
563
Dominik Laskowski663bd282018-04-19 15:26:54 -0700564std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700565
566} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800567
568// TODO(b/129481165): remove the #pragma below and fix conversion issues
569#pragma clang diagnostic pop // ignored "-Wconversion"