blob: bff301edd126e1b6c5044c4d3072c6b50e61e41b [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
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;
Peiyong Lin65248e02020-04-18 21:15:07 -0700173 getCompositionDisplay()->setCompositionEnabled(mPowerMode != hal::PowerMode::OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700174}
175
Alec Mouri023c1882021-05-08 16:36:33 -0700176void DisplayDevice::enableLayerCaching(bool enable) {
177 getCompositionDisplay()->setLayerCachingEnabled(enable);
178}
179
Peiyong Lin65248e02020-04-18 21:15:07 -0700180hal::PowerMode DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700181 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700182}
183
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700184bool DisplayDevice::isPoweredOn() const {
Peiyong Lin65248e02020-04-18 21:15:07 -0700185 return mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700186}
187
Marin Shalamanov5801c942020-12-17 17:00:13 +0100188void DisplayDevice::setActiveMode(DisplayModeId id) {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100189 const auto mode = getMode(id);
190 LOG_FATAL_IF(!mode, "Cannot set active mode which is not supported.");
Ady Abraham690f4612021-07-01 23:24:03 -0700191 ATRACE_INT(mActiveModeFPSTrace.c_str(), mode->getFps().getIntValue());
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100192 mActiveMode = mode;
Ady Abraham3efa3942021-06-24 19:01:25 -0700193 if (mRefreshRateConfigs) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800194 mRefreshRateConfigs->setActiveModeId(mActiveMode->getId());
Ady Abraham3efa3942021-06-24 19:01:25 -0700195 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700196 if (mRefreshRateOverlay) {
197 mRefreshRateOverlay->changeRefreshRate(mActiveMode->getFps());
198 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700199}
200
Ady Abraham690f4612021-07-01 23:24:03 -0700201status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100202 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700203 hal::VsyncPeriodChangeTimeline* outTimeline) {
204 if (!info.mode || info.mode->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100205 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abraham690f4612021-07-01 23:24:03 -0700206 info.mode ? std::to_string(info.mode->getId().value()).c_str() : "null",
207 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100208 return BAD_VALUE;
209 }
Ady Abrahama92a5682022-06-07 23:35:38 +0000210 mNumModeSwitchesInPolicy++;
Ady Abraham690f4612021-07-01 23:24:03 -0700211 mUpcomingActiveMode = info;
212 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.mode->getFps().getIntValue());
213 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(), info.mode->getHwcId(),
214 constraints, outTimeline);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100215}
216
Marin Shalamanov5801c942020-12-17 17:00:13 +0100217const DisplayModePtr& DisplayDevice::getActiveMode() const {
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100218 return mActiveMode;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100219}
220
221const DisplayModes& DisplayDevice::getSupportedModes() const {
222 return mSupportedModes;
223}
224
225DisplayModePtr DisplayDevice::getMode(DisplayModeId modeId) const {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800226 const DisplayModePtr nullMode;
227 return mSupportedModes.get(modeId).value_or(std::cref(nullMode));
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700228}
229
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800230std::optional<DisplayModeId> DisplayDevice::translateModeId(hal::HWConfigId hwcId) const {
231 const auto it =
232 std::find_if(mSupportedModes.begin(), mSupportedModes.end(),
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800233 [hwcId](const auto& pair) { return pair.second->getHwcId() == hwcId; });
Kriti Dang30e378d2022-02-18 15:09:12 +0100234 if (it != mSupportedModes.end()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800235 return it->second->getId();
Kriti Dang30e378d2022-02-18 15:09:12 +0100236 }
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800237 return {};
Kriti Dang30e378d2022-02-18 15:09:12 +0100238}
239
Marin Shalamanov045b7002021-01-07 16:56:24 +0100240nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
241 const auto physicalId = getPhysicalId();
242 if (!mHwComposer.isConnected(physicalId)) {
243 return 0;
244 }
245
246 nsecs_t vsyncPeriod;
247 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
248 if (status == NO_ERROR) {
249 return vsyncPeriod;
250 }
251
252 return getActiveMode()->getFps().getPeriodNsecs();
253}
254
255nsecs_t DisplayDevice::getRefreshTimestamp() const {
256 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
257 const auto vsyncPeriodNanos = getVsyncPeriodFromHWC();
258 return now - ((now - mLastHwVsync) % vsyncPeriodNanos);
259}
260
261void DisplayDevice::onVsync(nsecs_t timestamp) {
262 mLastHwVsync = timestamp;
263}
264
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800265ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800267}
268
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800269void DisplayDevice::setLayerStack(ui::LayerStack stack) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700270 mCompositionDisplay->setLayerFilter({stack, isInternal()});
Ady Abraham1b11bc62021-06-03 19:51:19 -0700271 if (mRefreshRateOverlay) {
272 mRefreshRateOverlay->setLayerStack(stack);
273 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700274}
275
Evan Rosky2239b372021-05-20 13:43:47 -0700276void DisplayDevice::setFlags(uint32_t flags) {
277 mFlags = flags;
278}
279
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800280void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200281 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700282 const auto size = ui::Size(width, height);
283 mCompositionDisplay->setDisplaySize(size);
284 if (mRefreshRateOverlay) {
285 mRefreshRateOverlay->setViewport(size);
286 }
Michael Lentine47e45402014-07-18 15:34:25 -0700287}
288
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200289void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
290 Rect orientedDisplaySpaceRect) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700291 mOrientation = orientation;
292
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200293 if (isPrimary()) {
294 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
295 }
296
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200297 // We need to take care of display rotation for globalTransform for case if the panel is not
298 // installed aligned with device orientation.
299 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000300
301 const auto& state = getCompositionDisplay()->getState();
302
303 // If the layer stack and destination frames have never been set, then configure them to be the
304 // same as the physical device, taking into account the total transform.
305 if (!orientedDisplaySpaceRect.isValid()) {
306 ui::Size bounds = state.displaySpace.getBounds();
307 bounds.rotate(transformOrientation);
308 orientedDisplaySpaceRect = Rect(bounds);
309 }
310 if (layerStackSpaceRect.isEmpty()) {
311 ui::Size bounds = state.framebufferSpace.getBounds();
312 bounds.rotate(transformOrientation);
313 layerStackSpaceRect = Rect(bounds);
314 }
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 Laskowski0acc3842022-04-07 11:23:42 -0700340 using namespace std::string_literals;
341
342 std::string name = "Display "s + to_string(getId()) + " ("s;
343
Dominik Laskowski55c85402020-01-21 16:25:47 -0800344 if (mConnectionType) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700345 name += isInternal() ? "internal"s : "external"s;
346 } else {
347 name += "virtual"s;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800348 }
349
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700350 if (isPrimary()) {
351 name += ", primary"s;
352 }
353
354 return name + ", \""s + mDisplayName + "\")"s;
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700355}
356
Yiwei Zhang5434a782018-12-05 18:06:32 -0800357void DisplayDevice::dump(std::string& result) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700358 using namespace std::string_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100359
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700360 result += getDebugName();
361
362 if (!isVirtual()) {
363 result += "\n deviceProductInfo="s;
364 if (mDeviceProductInfo) {
365 mDeviceProductInfo->dump(result);
366 } else {
367 result += "{}"s;
368 }
Marin Shalamanov5801c942020-12-17 17:00:13 +0100369 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800370
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700371 result += "\n powerMode="s;
372 result += to_string(mPowerMode);
373 result += '\n';
Ady Abraham3efa3942021-06-24 19:01:25 -0700374
375 if (mRefreshRateConfigs) {
376 mRefreshRateConfigs->dump(result);
377 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700378}
Irvelffc9efc2016-07-27 15:16:37 -0700379
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700380bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
381 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700382}
383
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200384DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700385 return mCompositionDisplay->getId();
386}
387
388bool DisplayDevice::isSecure() const {
389 return mCompositionDisplay->isSecure();
390}
391
Angel Aguayob108a6d2021-08-06 21:34:57 +0000392const Rect DisplayDevice::getBounds() const {
393 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700394}
395
396const Region& DisplayDevice::getUndefinedRegion() const {
397 return mCompositionDisplay->getState().undefinedRegion;
398}
399
400bool DisplayDevice::needsFiltering() const {
401 return mCompositionDisplay->getState().needsFiltering;
402}
403
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800404ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700405 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700406}
407
Garfield Tan54edd912020-10-21 16:31:41 -0700408ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
409 return mCompositionDisplay->getTransformHint();
410}
411
Lloyd Pique32cbe282018-10-19 13:09:22 -0700412const ui::Transform& DisplayDevice::getTransform() const {
413 return mCompositionDisplay->getState().transform;
414}
415
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200416const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000417 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700418}
419
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200420const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000421 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700422}
423
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700424bool DisplayDevice::hasWideColorGamut() const {
425 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
426}
427
428bool DisplayDevice::hasHDR10PlusSupport() const {
429 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
430}
431
432bool DisplayDevice::hasHDR10Support() const {
433 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
434}
435
436bool DisplayDevice::hasHLGSupport() const {
437 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
438}
439
440bool DisplayDevice::hasDolbyVisionSupport() const {
441 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
442}
443
444int DisplayDevice::getSupportedPerFrameMetadata() const {
445 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
446}
447
Kriti Dang49ad4132021-01-08 11:49:56 +0100448void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
449 mOverrideHdrTypes = hdrTypes;
450}
451
452HdrCapabilities DisplayDevice::getHdrCapabilities() const {
453 const HdrCapabilities& capabilities =
454 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
455 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
456 if (!mOverrideHdrTypes.empty()) {
457 hdrTypes = mOverrideHdrTypes;
458 }
459 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
460 capabilities.getDesiredMaxAverageLuminance(),
461 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700462}
463
Ady Abraham1b11bc62021-06-03 19:51:19 -0700464void DisplayDevice::enableRefreshRateOverlay(bool enable, bool showSpinnner) {
465 if (!enable) {
466 mRefreshRateOverlay.reset();
467 return;
468 }
469
Dominik Laskowski8c4356c2022-03-21 08:19:54 -0700470 const auto fpsRange = mRefreshRateConfigs->getSupportedRefreshRateRange();
471 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(fpsRange, showSpinnner);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700472 mRefreshRateOverlay->setLayerStack(getLayerStack());
473 mRefreshRateOverlay->setViewport(getSize());
474 mRefreshRateOverlay->changeRefreshRate(getActiveMode()->getFps());
475}
476
477bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
478 bool timerExpired) {
479 if (mRefreshRateConfigs && mRefreshRateOverlay) {
480 const auto newRefreshRate =
481 mRefreshRateConfigs->onKernelTimerChanged(desiredModeId, timerExpired);
482 if (newRefreshRate) {
483 mRefreshRateOverlay->changeRefreshRate(*newRefreshRate);
484 return true;
485 }
486 }
487
488 return false;
489}
490
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700491void DisplayDevice::animateRefreshRateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700492 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700493 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700494 }
495}
496
Ady Abraham690f4612021-07-01 23:24:03 -0700497bool DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info) {
498 ATRACE_CALL();
499
500 LOG_ALWAYS_FATAL_IF(!info.mode, "desired mode not provided");
501 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.mode->getPhysicalDisplayId(), "DisplayId mismatch");
502
503 ALOGV("%s(%s)", __func__, to_string(*info.mode).c_str());
504
505 std::scoped_lock lock(mActiveModeLock);
506 if (mDesiredActiveModeChanged) {
507 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700508 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700509 mDesiredActiveMode = info;
510 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
511 return false;
512 }
513
514 // Check if we are already at the desired mode
515 if (getActiveMode()->getId() == info.mode->getId()) {
516 return false;
517 }
518
519 // Initiate a mode change.
520 mDesiredActiveModeChanged = true;
521 mDesiredActiveMode = info;
522 return true;
523}
524
525std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
526 std::scoped_lock lock(mActiveModeLock);
527 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
528 return std::nullopt;
529}
530
531void DisplayDevice::clearDesiredActiveModeState() {
532 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700533 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700534 mDesiredActiveModeChanged = false;
535}
536
Ady Abrahama92a5682022-06-07 23:35:38 +0000537status_t DisplayDevice::setRefreshRatePolicy(
538 const std::optional<scheduler::RefreshRateConfigs::Policy>& policy, bool overridePolicy) {
539 const auto oldPolicy = mRefreshRateConfigs->getCurrentPolicy();
540 const status_t setPolicyResult = overridePolicy
541 ? mRefreshRateConfigs->setOverridePolicy(policy)
542 : mRefreshRateConfigs->setDisplayManagerPolicy(*policy);
543
544 if (setPolicyResult == OK) {
545 const int numModeChanges = mNumModeSwitchesInPolicy.exchange(0);
546
547 ALOGI("Display %s policy changed\n"
548 "Previous: {%s}\n"
549 "Current: {%s}\n"
550 "%d mode changes were performed under the previous policy",
551 to_string(getId()).c_str(), oldPolicy.toString().c_str(),
552 policy ? policy->toString().c_str() : "null", numModeChanges);
553 }
554
555 return setPolicyResult;
556}
557
Dominik Laskowski663bd282018-04-19 15:26:54 -0700558std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700559
560} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800561
562// TODO(b/129481165): remove the #pragma below and fix conversion issues
563#pragma clang diagnostic pop // ignored "-Wconversion"