blob: 5b6591aeacdfd94f328e2604e70db2923e314e9e [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070041#include "DisplayDevice.h"
Vishnu Nairaf6d2972022-11-18 06:26:38 +000042#include "FrontEnd/DisplayInfo.h"
Sally Qi147581b2023-06-27 11:55:34 -070043#include "HdrSdrRatioOverlay.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
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070052DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010053 const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070054 std::shared_ptr<compositionengine::Display> compositionDisplay)
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010055 : flinger(flinger),
56 hwComposer(hwComposer),
57 displayToken(displayToken),
58 compositionDisplay(compositionDisplay) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070059
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070060DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070061 : mFlinger(args.flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010062 mHwComposer(args.hwComposer),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070063 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080064 mSequenceId(args.sequenceId),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070065 mCompositionDisplay{args.compositionDisplay},
Ady Abraham690f4612021-07-01 23:24:03 -070066 mActiveModeFPSTrace("ActiveModeFPS -" + to_string(getId())),
67 mActiveModeFPSHwcTrace("ActiveModeFPS_HWC -" + to_string(getId())),
Ady Abrahamace3d052022-11-17 16:25:05 -080068 mRenderFrameRateFPSTrace("RenderRateFPS -" + to_string(getId())),
Dominik Laskowski718f9602019-11-09 20:01:35 -080069 mPhysicalOrientation(args.physicalOrientation),
Ady Abraham3efa3942021-06-24 19:01:25 -070070 mIsPrimary(args.isPrimary),
Huihong Luo1768cb02022-10-11 11:10:34 -070071 mRequestedRefreshRate(args.requestedRefreshRate),
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040072 mRefreshRateSelector(std::move(args.refreshRateSelector)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070073 mCompositionDisplay->editState().isSecure = args.isSecure;
Lloyd Pique31cb2942018-10-19 17:23:03 -070074 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070075 compositionengine::RenderSurfaceCreationArgsBuilder()
76 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
77 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
78 .setNativeWindow(std::move(args.nativeWindow))
79 .setDisplaySurface(std::move(args.displaySurface))
Lloyd Pique30db6402023-06-26 18:56:51 +000080 .setMaxTextureCacheSize(
81 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070082 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070083
Lloyd Pique30db6402023-06-26 18:56:51 +000084 if (!mFlinger->mDisableClientCompositionCache &&
85 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
Vishnu Nair9b079a22020-01-21 14:36:08 -080086 mCompositionDisplay->createClientCompositionCache(
Lloyd Pique30db6402023-06-26 18:56:51 +000087 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
Vishnu Nair9b079a22020-01-21 14:36:08 -080088 }
89
Lloyd Pique30db6402023-06-26 18:56:51 +000090 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
91 mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070092 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010093 compositionengine::DisplayColorProfileCreationArgsBuilder()
94 .setHasWideColorGamut(args.hasWideColorGamut)
95 .setHdrCapabilities(std::move(args.hdrCapabilities))
96 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
97 .setHwcColorModes(std::move(args.hwcColorModes))
98 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070099
Lloyd Pique32cbe282018-10-19 13:09:22 -0700100 if (!mCompositionDisplay->isValid()) {
101 ALOGE("Composition Display did not validate!");
102 }
103
Lloyd Pique31cb2942018-10-19 17:23:03 -0700104 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700105
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500106 if (const auto powerModeOpt = args.initialPowerMode) {
107 setPowerMode(*powerModeOpt);
108 }
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
Vishnu Nairaf6d2972022-11-18 06:26:38 +0000136auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700137 gui::DisplayInfo info;
138 info.displayId = getLayerStack().id;
139
140 // The physical orientation is set when the orientation of the display panel is
141 // different than the default orientation of the device. Other services like
142 // InputFlinger do not know about this, so we do not need to expose the physical
143 // orientation of the panel outside of SurfaceFlinger.
144 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
145 auto width = getWidth();
146 auto height = getHeight();
147 if (inversePhysicalOrientation == ui::ROTATION_90 ||
148 inversePhysicalOrientation == ui::ROTATION_270) {
149 std::swap(width, height);
150 }
151 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
152 inversePhysicalOrientation),
153 width, height);
154 const auto& displayTransform = undoPhysicalOrientation * getTransform();
155 // Send the inverse display transform to input so it can convert display coordinates to
156 // logical display.
157 info.transform = displayTransform.inverse();
158
159 info.logicalWidth = getLayerStackSpaceRect().width();
160 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800161
162 return {.info = info,
163 .transform = displayTransform,
164 .receivesInput = receivesInput(),
Vishnu Nair67b431c2022-11-16 01:54:05 +0000165 .isSecure = isSecure(),
166 .isPrimary = isPrimary(),
Vishnu Nair81750622023-03-08 15:02:06 -0800167 .isVirtual = isVirtual(),
Vishnu Naircfb2d252023-01-19 04:44:02 +0000168 .rotationFlags = ui::Transform::toRotationFlags(mOrientation),
169 .transformHint = getTransformHint()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700170}
171
Peiyong Lin65248e02020-04-18 21:15:07 -0700172void DisplayDevice::setPowerMode(hal::PowerMode mode) {
joenchene72ba5e2022-08-24 13:08:58 +0000173 if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
linpeterdcfd4e62022-12-29 09:39:14 +0800174 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchene72ba5e2022-08-24 13:08:58 +0000175 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
176 mBrightness = *mStagedBrightness;
177 }
178 mStagedBrightness = std::nullopt;
179 getCompositionDisplay()->applyDisplayBrightness(true);
180 }
181
Ady Abraham66452722023-03-14 17:41:47 -0700182 if (mPowerMode) {
183 *mPowerMode = mode;
184 } else {
185 mPowerMode.emplace("PowerMode -" + to_string(getId()), mode);
186 }
linpeter1c69cd32022-07-13 19:20:48 +0800187
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500188 getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700189}
190
Ady Abraham66452722023-03-14 17:41:47 -0700191void DisplayDevice::tracePowerMode() {
192 // assign the same value for tracing
193 if (mPowerMode) {
194 const hal::PowerMode powerMode = *mPowerMode;
195 *mPowerMode = powerMode;
196 }
197}
198
Alec Mouri023c1882021-05-08 16:36:33 -0700199void DisplayDevice::enableLayerCaching(bool enable) {
200 getCompositionDisplay()->setLayerCachingEnabled(enable);
201}
202
linpeter1c69cd32022-07-13 19:20:48 +0800203std::optional<hal::PowerMode> DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700204 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700205}
206
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700207bool DisplayDevice::isPoweredOn() const {
linpeter1c69cd32022-07-13 19:20:48 +0800208 return mPowerMode && *mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700209}
210
ramindania04b8a52023-08-07 18:49:47 -0700211void DisplayDevice::setActiveMode(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
212 ATRACE_INT(mActiveModeFPSTrace.c_str(), vsyncRate.getIntValue());
Ady Abrahamace3d052022-11-17 16:25:05 -0800213 ATRACE_INT(mRenderFrameRateFPSTrace.c_str(), renderFps.getIntValue());
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700214
Ady Abrahamace3d052022-11-17 16:25:05 -0800215 mRefreshRateSelector->setActiveMode(modeId, renderFps);
ramindania04b8a52023-08-07 18:49:47 -0700216 updateRefreshRateOverlayRate(vsyncRate, renderFps);
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700217}
218
Ady Abraham690f4612021-07-01 23:24:03 -0700219status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100220 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700221 hal::VsyncPeriodChangeTimeline* outTimeline) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800222 if (!info.modeOpt || info.modeOpt->modePtr->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100223 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abrahamace3d052022-11-17 16:25:05 -0800224 info.modeOpt ? std::to_string(info.modeOpt->modePtr->getId().value()).c_str()
225 : "null",
Ady Abraham690f4612021-07-01 23:24:03 -0700226 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100227 return BAD_VALUE;
228 }
Ady Abraham690f4612021-07-01 23:24:03 -0700229 mUpcomingActiveMode = info;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400230 mIsModeSetPending = true;
231
232 const auto& pendingMode = *info.modeOpt->modePtr;
ramindania04b8a52023-08-07 18:49:47 -0700233 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), pendingMode.getVsyncRate().getIntValue());
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400234
235 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(), pendingMode.getHwcId(),
236 constraints, outTimeline);
237}
238
ramindania04b8a52023-08-07 18:49:47 -0700239void DisplayDevice::finalizeModeChange(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
240 setActiveMode(modeId, vsyncRate, renderFps);
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400241 mIsModeSetPending = false;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +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
ramindania04b8a52023-08-07 18:49:47 -0700256 return refreshRateSelector().getActiveMode().modePtr->getVsyncRate().getPeriodNsecs();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100257}
258
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800259ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700260 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800261}
262
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700263void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
264 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700265 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700266 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700267 }
Sally Qi147581b2023-06-27 11:55:34 -0700268 if (mHdrSdrRatioOverlay) {
269 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
270 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700271}
272
Evan Rosky2239b372021-05-20 13:43:47 -0700273void DisplayDevice::setFlags(uint32_t flags) {
274 mFlags = flags;
275}
276
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800277void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200278 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700279 const auto size = ui::Size(width, height);
280 mCompositionDisplay->setDisplaySize(size);
281 if (mRefreshRateOverlay) {
282 mRefreshRateOverlay->setViewport(size);
283 }
Sally Qi147581b2023-06-27 11:55:34 -0700284 if (mHdrSdrRatioOverlay) {
285 mHdrSdrRatioOverlay->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) {
Sally Qi147581b2023-06-27 11:55:34 -0700291 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700292 mOrientation = orientation;
293
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200294 // We need to take care of display rotation for globalTransform for case if the panel is not
295 // installed aligned with device orientation.
296 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000297
298 const auto& state = getCompositionDisplay()->getState();
299
300 // If the layer stack and destination frames have never been set, then configure them to be the
301 // same as the physical device, taking into account the total transform.
302 if (!orientedDisplaySpaceRect.isValid()) {
303 ui::Size bounds = state.displaySpace.getBounds();
304 bounds.rotate(transformOrientation);
305 orientedDisplaySpaceRect = Rect(bounds);
306 }
307 if (layerStackSpaceRect.isEmpty()) {
308 ui::Size bounds = state.framebufferSpace.getBounds();
309 bounds.rotate(transformOrientation);
310 layerStackSpaceRect = Rect(bounds);
311 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200312 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
313 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700314}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700315
Alec Mouricdf16792021-12-10 13:16:06 -0800316void DisplayDevice::stageBrightness(float brightness) {
317 mStagedBrightness = brightness;
318}
319
320void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800321 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000322 if (needsComposite) {
323 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
324 }
Alec Mouricdf16792021-12-10 13:16:06 -0800325 mBrightness = *mStagedBrightness;
326 }
327 mStagedBrightness = std::nullopt;
328}
329
330std::optional<float> DisplayDevice::getStagedBrightness() const {
331 return mStagedBrightness;
332}
333
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700334void DisplayDevice::dump(utils::Dumper& dumper) const {
335 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100336
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400337 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700338 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700339
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400340 if (mRefreshRateSelector) {
341 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700342 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700343}
Irvelffc9efc2016-07-27 15:16:37 -0700344
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700345bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
346 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700347}
348
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200349DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700350 return mCompositionDisplay->getId();
351}
352
353bool DisplayDevice::isSecure() const {
354 return mCompositionDisplay->isSecure();
355}
356
Angel Aguayob108a6d2021-08-06 21:34:57 +0000357const Rect DisplayDevice::getBounds() const {
358 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700359}
360
361const Region& DisplayDevice::getUndefinedRegion() const {
362 return mCompositionDisplay->getState().undefinedRegion;
363}
364
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800365ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700366 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700367}
368
Garfield Tan54edd912020-10-21 16:31:41 -0700369ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
370 return mCompositionDisplay->getTransformHint();
371}
372
Lloyd Pique32cbe282018-10-19 13:09:22 -0700373const ui::Transform& DisplayDevice::getTransform() const {
374 return mCompositionDisplay->getState().transform;
375}
376
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200377const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000378 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700379}
380
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200381const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000382 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700383}
384
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700385bool DisplayDevice::hasWideColorGamut() const {
386 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
387}
388
389bool DisplayDevice::hasHDR10PlusSupport() const {
390 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
391}
392
393bool DisplayDevice::hasHDR10Support() const {
394 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
395}
396
397bool DisplayDevice::hasHLGSupport() const {
398 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
399}
400
401bool DisplayDevice::hasDolbyVisionSupport() const {
402 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
403}
404
405int DisplayDevice::getSupportedPerFrameMetadata() const {
406 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
407}
408
Kriti Dang49ad4132021-01-08 11:49:56 +0100409void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
410 mOverrideHdrTypes = hdrTypes;
411}
412
413HdrCapabilities DisplayDevice::getHdrCapabilities() const {
414 const HdrCapabilities& capabilities =
415 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
416 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
417 if (!mOverrideHdrTypes.empty()) {
418 hdrTypes = mOverrideHdrTypes;
419 }
420 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
421 capabilities.getDesiredMaxAverageLuminance(),
422 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700423}
424
Sally Qi147581b2023-06-27 11:55:34 -0700425void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
426 if (!enable) {
427 mHdrSdrRatioOverlay.reset();
428 return;
429 }
430
Ady Abrahamddba9342023-10-02 16:28:03 -0700431 mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
432 if (mHdrSdrRatioOverlay) {
433 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
434 mHdrSdrRatioOverlay->setViewport(getSize());
435 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
436 }
Sally Qi147581b2023-06-27 11:55:34 -0700437}
438
439void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
440 ATRACE_CALL();
441 mHdrSdrRatio = currentHdrSdrRatio;
442 if (mHdrSdrRatioOverlay) {
443 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
444 }
445}
446
Lloyd Pique30db6402023-06-26 18:56:51 +0000447void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, bool showSpinner,
448 bool showRenderRate, bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700449 if (!enable) {
450 mRefreshRateOverlay.reset();
451 return;
452 }
453
Ady Abraham0aa373a2022-11-22 13:56:50 -0800454 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000455 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800456 features |= RefreshRateOverlay::Features::Spinner;
457 }
458
Lloyd Pique30db6402023-06-26 18:56:51 +0000459 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800460 features |= RefreshRateOverlay::Features::RenderRate;
461 }
462
Lloyd Pique30db6402023-06-26 18:56:51 +0000463 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800464 features |= RefreshRateOverlay::Features::ShowInMiddle;
465 }
466
ramindanib2158ee2023-02-13 20:29:59 -0800467 if (setByHwc) {
468 features |= RefreshRateOverlay::Features::SetByHwc;
469 }
470
ramindania04b8a52023-08-07 18:49:47 -0700471 // TODO(b/296636258) Update to use the render rate range in VRR mode.
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400472 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abrahamddba9342023-10-02 16:28:03 -0700473 mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
474 if (mRefreshRateOverlay) {
475 mRefreshRateOverlay->setLayerStack(getLayerStack());
476 mRefreshRateOverlay->setViewport(getSize());
477 updateRefreshRateOverlayRate(getActiveMode().modePtr->getVsyncRate(), getActiveMode().fps,
478 setByHwc);
479 }
ramindanib2158ee2023-02-13 20:29:59 -0800480}
481
ramindania04b8a52023-08-07 18:49:47 -0700482void DisplayDevice::updateRefreshRateOverlayRate(Fps vsyncRate, Fps renderFps, bool setByHwc) {
ramindanib2158ee2023-02-13 20:29:59 -0800483 ATRACE_CALL();
ramindani3faf5742023-09-21 13:45:12 -0700484 if (mRefreshRateOverlay) {
485 if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
486 mRefreshRateOverlay->changeRefreshRate(vsyncRate, renderFps);
487 } else {
488 mRefreshRateOverlay->changeRenderRate(renderFps);
489 }
ramindanib2158ee2023-02-13 20:29:59 -0800490 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700491}
492
493bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
494 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400495 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800496 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400497 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800498 if (newMode) {
ramindania04b8a52023-08-07 18:49:47 -0700499 updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700500 return true;
501 }
502 }
503
504 return false;
505}
506
Sally Qi147581b2023-06-27 11:55:34 -0700507void DisplayDevice::animateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700508 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700509 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700510 }
Sally Qi147581b2023-06-27 11:55:34 -0700511 if (mHdrSdrRatioOverlay) {
512 // hdr sdr ratio is designed to be on the top right of the screen,
513 // therefore, we need to re-calculate the display's width and height
514 if (mIsOrientationChanged) {
515 auto width = getWidth();
516 auto height = getHeight();
517 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
518 std::swap(width, height);
519 }
520 mHdrSdrRatioOverlay->setViewport({width, height});
521 }
522 mHdrSdrRatioOverlay->animate();
523 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700524}
525
Dominik Laskowskifc378b02022-12-02 14:56:05 -0500526auto DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info, bool force)
527 -> DesiredActiveModeAction {
Ady Abraham690f4612021-07-01 23:24:03 -0700528 ATRACE_CALL();
529
Ady Abrahamace3d052022-11-17 16:25:05 -0800530 LOG_ALWAYS_FATAL_IF(!info.modeOpt, "desired mode not provided");
531 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.modeOpt->modePtr->getPhysicalDisplayId(),
532 "DisplayId mismatch");
Ady Abraham690f4612021-07-01 23:24:03 -0700533
Ady Abrahamace3d052022-11-17 16:25:05 -0800534 ALOGV("%s(%s)", __func__, to_string(*info.modeOpt->modePtr).c_str());
Ady Abraham690f4612021-07-01 23:24:03 -0700535
536 std::scoped_lock lock(mActiveModeLock);
537 if (mDesiredActiveModeChanged) {
538 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700539 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700540 mDesiredActiveMode = info;
541 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
Ady Abrahamace3d052022-11-17 16:25:05 -0800542 return DesiredActiveModeAction::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700543 }
544
Ady Abrahamace3d052022-11-17 16:25:05 -0800545 const auto& desiredMode = *info.modeOpt->modePtr;
546
Ady Abraham690f4612021-07-01 23:24:03 -0700547 // Check if we are already at the desired mode
Ady Abraham95a37e22023-01-18 22:57:28 -0800548 const auto currentMode = refreshRateSelector().getActiveMode();
549 if (!force && currentMode.modePtr->getId() == desiredMode.getId()) {
550 if (currentMode == info.modeOpt) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800551 return DesiredActiveModeAction::None;
552 }
553
ramindania04b8a52023-08-07 18:49:47 -0700554 setActiveMode(desiredMode.getId(), desiredMode.getVsyncRate(), info.modeOpt->fps);
Ady Abrahamace3d052022-11-17 16:25:05 -0800555 return DesiredActiveModeAction::InitiateRenderRateSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700556 }
557
Ady Abraham95a37e22023-01-18 22:57:28 -0800558 // Set the render frame rate to the current physical refresh rate to schedule the next
559 // frame as soon as possible.
ramindania04b8a52023-08-07 18:49:47 -0700560 setActiveMode(currentMode.modePtr->getId(), currentMode.modePtr->getVsyncRate(),
561 currentMode.modePtr->getVsyncRate());
Ady Abraham95a37e22023-01-18 22:57:28 -0800562
Ady Abraham690f4612021-07-01 23:24:03 -0700563 // Initiate a mode change.
564 mDesiredActiveModeChanged = true;
565 mDesiredActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800566 return DesiredActiveModeAction::InitiateDisplayModeSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700567}
568
569std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
570 std::scoped_lock lock(mActiveModeLock);
571 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
572 return std::nullopt;
573}
574
575void DisplayDevice::clearDesiredActiveModeState() {
576 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700577 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700578 mDesiredActiveModeChanged = false;
579}
580
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500581void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000582 using fps_approx_ops::operator<=;
583 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700584 return;
585 }
586
587 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500588 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
589 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700590 return;
591 }
592
593 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000594 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
595 if (divisor == 0) {
596 mAdjustedRefreshRate = 0_Hz;
597 return;
598 }
599
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500600 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700601}
602
Dominik Laskowski663bd282018-04-19 15:26:54 -0700603std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700604
605} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800606
607// TODO(b/129481165): remove the #pragma below and fix conversion issues
608#pragma clang diagnostic pop // ignored "-Wconversion"