blob: 7fdf9e7672aa09b7591fcba1b5a0d4ddd20d92b9 [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},
Dominik Laskowski59746512023-11-19 09:30:24 -050066 mPendingModeFpsTrace(concatId("PendingModeFps")),
67 mActiveModeFpsTrace(concatId("ActiveModeFps")),
68 mRenderRateFpsTrace(concatId("RenderRateFps")),
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 Laskowski0784adc2023-10-13 18:35:37 -040072 mRefreshRateSelector(std::move(args.refreshRateSelector)),
Dominik Laskowski59746512023-11-19 09:30:24 -050073 mDesiredModeChanged(concatId("DesiredModeChanged"), false) {
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))
Lloyd Pique30db6402023-06-26 18:56:51 +000081 .setMaxTextureCacheSize(
82 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070083 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070084
Lloyd Pique30db6402023-06-26 18:56:51 +000085 if (!mFlinger->mDisableClientCompositionCache &&
86 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
Vishnu Nair9b079a22020-01-21 14:36:08 -080087 mCompositionDisplay->createClientCompositionCache(
Lloyd Pique30db6402023-06-26 18:56:51 +000088 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
Vishnu Nair9b079a22020-01-21 14:36:08 -080089 }
90
Lloyd Pique30db6402023-06-26 18:56:51 +000091 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
92 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
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500107 if (const auto powerModeOpt = args.initialPowerMode) {
108 setPowerMode(*powerModeOpt);
109 }
Alec Mouriba013fa2018-10-16 12:43:11 -0700110
Jesse Hallffe1f192013-03-22 15:13:48 -0700111 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800112 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
Lloyd Pique09594832018-01-22 17:48:03 -0800115DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700116
Lloyd Pique45a165a2018-10-19 11:54:47 -0700117void DisplayDevice::disconnect() {
118 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700119}
120
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700121int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000122 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700123}
124
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700125int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000126 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700127}
128
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700129void DisplayDevice::setDisplayName(const std::string& displayName) {
130 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700131 // never override the name with an empty name
132 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700133 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700134 }
135}
136
Vishnu Nairaf6d2972022-11-18 06:26:38 +0000137auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700138 gui::DisplayInfo info;
139 info.displayId = getLayerStack().id;
140
141 // The physical orientation is set when the orientation of the display panel is
142 // different than the default orientation of the device. Other services like
143 // InputFlinger do not know about this, so we do not need to expose the physical
144 // orientation of the panel outside of SurfaceFlinger.
145 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
146 auto width = getWidth();
147 auto height = getHeight();
148 if (inversePhysicalOrientation == ui::ROTATION_90 ||
149 inversePhysicalOrientation == ui::ROTATION_270) {
150 std::swap(width, height);
151 }
152 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
153 inversePhysicalOrientation),
154 width, height);
155 const auto& displayTransform = undoPhysicalOrientation * getTransform();
156 // Send the inverse display transform to input so it can convert display coordinates to
157 // logical display.
158 info.transform = displayTransform.inverse();
159
160 info.logicalWidth = getLayerStackSpaceRect().width();
161 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800162
163 return {.info = info,
164 .transform = displayTransform,
165 .receivesInput = receivesInput(),
Vishnu Nair67b431c2022-11-16 01:54:05 +0000166 .isSecure = isSecure(),
167 .isPrimary = isPrimary(),
Vishnu Nair81750622023-03-08 15:02:06 -0800168 .isVirtual = isVirtual(),
Vishnu Naircfb2d252023-01-19 04:44:02 +0000169 .rotationFlags = ui::Transform::toRotationFlags(mOrientation),
170 .transformHint = getTransformHint()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700171}
172
Peiyong Lin65248e02020-04-18 21:15:07 -0700173void DisplayDevice::setPowerMode(hal::PowerMode mode) {
joenchene72ba5e2022-08-24 13:08:58 +0000174 if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
linpeterdcfd4e62022-12-29 09:39:14 +0800175 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchene72ba5e2022-08-24 13:08:58 +0000176 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
177 mBrightness = *mStagedBrightness;
178 }
179 mStagedBrightness = std::nullopt;
180 getCompositionDisplay()->applyDisplayBrightness(true);
181 }
182
Ady Abraham66452722023-03-14 17:41:47 -0700183 if (mPowerMode) {
184 *mPowerMode = mode;
185 } else {
186 mPowerMode.emplace("PowerMode -" + to_string(getId()), mode);
187 }
linpeter1c69cd32022-07-13 19:20:48 +0800188
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500189 getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700190}
191
Ady Abraham66452722023-03-14 17:41:47 -0700192void DisplayDevice::tracePowerMode() {
193 // assign the same value for tracing
194 if (mPowerMode) {
195 const hal::PowerMode powerMode = *mPowerMode;
196 *mPowerMode = powerMode;
197 }
198}
199
Alec Mouri023c1882021-05-08 16:36:33 -0700200void DisplayDevice::enableLayerCaching(bool enable) {
201 getCompositionDisplay()->setLayerCachingEnabled(enable);
202}
203
linpeter1c69cd32022-07-13 19:20:48 +0800204std::optional<hal::PowerMode> DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700205 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700206}
207
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700208bool DisplayDevice::isPoweredOn() const {
linpeter1c69cd32022-07-13 19:20:48 +0800209 return mPowerMode && *mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700210}
211
ramindania04b8a52023-08-07 18:49:47 -0700212void DisplayDevice::setActiveMode(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
Dominik Laskowski59746512023-11-19 09:30:24 -0500213 ATRACE_INT(mActiveModeFpsTrace.c_str(), vsyncRate.getIntValue());
214 ATRACE_INT(mRenderRateFpsTrace.c_str(), renderFps.getIntValue());
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700215
Ady Abrahamace3d052022-11-17 16:25:05 -0800216 mRefreshRateSelector->setActiveMode(modeId, renderFps);
ramindania04b8a52023-08-07 18:49:47 -0700217 updateRefreshRateOverlayRate(vsyncRate, renderFps);
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700218}
219
Dominik Laskowski4b32e4b2023-11-19 10:24:51 -0500220bool DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
221 const hal::VsyncPeriodChangeConstraints& constraints,
222 hal::VsyncPeriodChangeTimeline& outTimeline) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800223 if (!info.modeOpt || info.modeOpt->modePtr->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100224 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abrahamace3d052022-11-17 16:25:05 -0800225 info.modeOpt ? std::to_string(info.modeOpt->modePtr->getId().value()).c_str()
226 : "null",
Ady Abraham690f4612021-07-01 23:24:03 -0700227 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100228 return BAD_VALUE;
229 }
Dominik Laskowski59746512023-11-19 09:30:24 -0500230 mPendingMode = info;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400231 mIsModeSetPending = true;
232
Dominik Laskowski4b32e4b2023-11-19 10:24:51 -0500233 const auto& mode = *info.modeOpt->modePtr;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400234
Dominik Laskowski4b32e4b2023-11-19 10:24:51 -0500235 if (mHwComposer.setActiveModeWithConstraints(getPhysicalId(), mode.getHwcId(), constraints,
236 &outTimeline) != OK) {
237 return false;
238 }
239
240 ATRACE_INT(mPendingModeFpsTrace.c_str(), mode.getVsyncRate().getIntValue());
241 return true;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400242}
243
ramindania04b8a52023-08-07 18:49:47 -0700244void DisplayDevice::finalizeModeChange(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
245 setActiveMode(modeId, vsyncRate, renderFps);
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400246 mIsModeSetPending = false;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100247}
248
Marin Shalamanov045b7002021-01-07 16:56:24 +0100249nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
250 const auto physicalId = getPhysicalId();
251 if (!mHwComposer.isConnected(physicalId)) {
252 return 0;
253 }
254
255 nsecs_t vsyncPeriod;
256 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
257 if (status == NO_ERROR) {
258 return vsyncPeriod;
259 }
260
ramindania04b8a52023-08-07 18:49:47 -0700261 return refreshRateSelector().getActiveMode().modePtr->getVsyncRate().getPeriodNsecs();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100262}
263
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800264ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700265 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800266}
267
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700268void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
269 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700270 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700271 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700272 }
Sally Qi147581b2023-06-27 11:55:34 -0700273 if (mHdrSdrRatioOverlay) {
274 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
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 }
Sally Qi147581b2023-06-27 11:55:34 -0700289 if (mHdrSdrRatioOverlay) {
290 mHdrSdrRatioOverlay->setViewport(size);
291 }
Michael Lentine47e45402014-07-18 15:34:25 -0700292}
293
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200294void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
295 Rect orientedDisplaySpaceRect) {
Sally Qi147581b2023-06-27 11:55:34 -0700296 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700297 mOrientation = orientation;
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) {
linpeterdcfd4e62022-12-29 09:39:14 +0800326 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000327 if (needsComposite) {
328 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
329 }
Alec Mouricdf16792021-12-10 13:16:06 -0800330 mBrightness = *mStagedBrightness;
331 }
332 mStagedBrightness = std::nullopt;
333}
334
335std::optional<float> DisplayDevice::getStagedBrightness() const {
336 return mStagedBrightness;
337}
338
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700339void DisplayDevice::dump(utils::Dumper& dumper) const {
340 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100341
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400342 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700343 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700344
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400345 if (mRefreshRateSelector) {
346 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700347 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700348}
Irvelffc9efc2016-07-27 15:16:37 -0700349
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700350bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
351 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700352}
353
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200354DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700355 return mCompositionDisplay->getId();
356}
357
358bool DisplayDevice::isSecure() const {
359 return mCompositionDisplay->isSecure();
360}
361
Angel Aguayob108a6d2021-08-06 21:34:57 +0000362const Rect DisplayDevice::getBounds() const {
363 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700364}
365
366const Region& DisplayDevice::getUndefinedRegion() const {
367 return mCompositionDisplay->getState().undefinedRegion;
368}
369
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800370ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700371 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700372}
373
Garfield Tan54edd912020-10-21 16:31:41 -0700374ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
375 return mCompositionDisplay->getTransformHint();
376}
377
Lloyd Pique32cbe282018-10-19 13:09:22 -0700378const ui::Transform& DisplayDevice::getTransform() const {
379 return mCompositionDisplay->getState().transform;
380}
381
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200382const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000383 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700384}
385
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200386const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000387 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700388}
389
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700390bool DisplayDevice::hasWideColorGamut() const {
391 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
392}
393
394bool DisplayDevice::hasHDR10PlusSupport() const {
395 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
396}
397
398bool DisplayDevice::hasHDR10Support() const {
399 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
400}
401
402bool DisplayDevice::hasHLGSupport() const {
403 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
404}
405
406bool DisplayDevice::hasDolbyVisionSupport() const {
407 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
408}
409
410int DisplayDevice::getSupportedPerFrameMetadata() const {
411 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
412}
413
Kriti Dang49ad4132021-01-08 11:49:56 +0100414void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
415 mOverrideHdrTypes = hdrTypes;
416}
417
418HdrCapabilities DisplayDevice::getHdrCapabilities() const {
419 const HdrCapabilities& capabilities =
420 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
421 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
422 if (!mOverrideHdrTypes.empty()) {
423 hdrTypes = mOverrideHdrTypes;
424 }
425 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
426 capabilities.getDesiredMaxAverageLuminance(),
427 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700428}
429
Sally Qi147581b2023-06-27 11:55:34 -0700430void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
431 if (!enable) {
432 mHdrSdrRatioOverlay.reset();
433 return;
434 }
435
Ady Abrahamddba9342023-10-02 16:28:03 -0700436 mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
437 if (mHdrSdrRatioOverlay) {
438 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
439 mHdrSdrRatioOverlay->setViewport(getSize());
440 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
441 }
Sally Qi147581b2023-06-27 11:55:34 -0700442}
443
444void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
445 ATRACE_CALL();
446 mHdrSdrRatio = currentHdrSdrRatio;
447 if (mHdrSdrRatioOverlay) {
448 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
449 }
450}
451
Lloyd Pique30db6402023-06-26 18:56:51 +0000452void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, bool showSpinner,
453 bool showRenderRate, bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700454 if (!enable) {
455 mRefreshRateOverlay.reset();
456 return;
457 }
458
Ady Abraham0aa373a2022-11-22 13:56:50 -0800459 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000460 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800461 features |= RefreshRateOverlay::Features::Spinner;
462 }
463
Lloyd Pique30db6402023-06-26 18:56:51 +0000464 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800465 features |= RefreshRateOverlay::Features::RenderRate;
466 }
467
Lloyd Pique30db6402023-06-26 18:56:51 +0000468 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800469 features |= RefreshRateOverlay::Features::ShowInMiddle;
470 }
471
ramindanib2158ee2023-02-13 20:29:59 -0800472 if (setByHwc) {
473 features |= RefreshRateOverlay::Features::SetByHwc;
474 }
475
ramindania04b8a52023-08-07 18:49:47 -0700476 // TODO(b/296636258) Update to use the render rate range in VRR mode.
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400477 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abrahamddba9342023-10-02 16:28:03 -0700478 mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
479 if (mRefreshRateOverlay) {
480 mRefreshRateOverlay->setLayerStack(getLayerStack());
481 mRefreshRateOverlay->setViewport(getSize());
482 updateRefreshRateOverlayRate(getActiveMode().modePtr->getVsyncRate(), getActiveMode().fps,
483 setByHwc);
484 }
ramindanib2158ee2023-02-13 20:29:59 -0800485}
486
ramindania04b8a52023-08-07 18:49:47 -0700487void DisplayDevice::updateRefreshRateOverlayRate(Fps vsyncRate, Fps renderFps, bool setByHwc) {
ramindanib2158ee2023-02-13 20:29:59 -0800488 ATRACE_CALL();
ramindani3faf5742023-09-21 13:45:12 -0700489 if (mRefreshRateOverlay) {
490 if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
491 mRefreshRateOverlay->changeRefreshRate(vsyncRate, renderFps);
492 } else {
493 mRefreshRateOverlay->changeRenderRate(renderFps);
494 }
ramindanib2158ee2023-02-13 20:29:59 -0800495 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700496}
497
498bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
499 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400500 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800501 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400502 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800503 if (newMode) {
ramindania04b8a52023-08-07 18:49:47 -0700504 updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700505 return true;
506 }
507 }
508
509 return false;
510}
511
Sally Qi147581b2023-06-27 11:55:34 -0700512void DisplayDevice::animateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700513 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700514 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700515 }
Sally Qi147581b2023-06-27 11:55:34 -0700516 if (mHdrSdrRatioOverlay) {
517 // hdr sdr ratio is designed to be on the top right of the screen,
518 // therefore, we need to re-calculate the display's width and height
519 if (mIsOrientationChanged) {
520 auto width = getWidth();
521 auto height = getHeight();
522 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
523 std::swap(width, height);
524 }
525 mHdrSdrRatioOverlay->setViewport({width, height});
526 }
527 mHdrSdrRatioOverlay->animate();
528 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700529}
530
Dominik Laskowski59746512023-11-19 09:30:24 -0500531auto DisplayDevice::setDesiredMode(const ActiveModeInfo& info, bool force) -> DesiredModeAction {
Ady Abraham690f4612021-07-01 23:24:03 -0700532 ATRACE_CALL();
533
Ady Abrahamace3d052022-11-17 16:25:05 -0800534 LOG_ALWAYS_FATAL_IF(!info.modeOpt, "desired mode not provided");
535 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.modeOpt->modePtr->getPhysicalDisplayId(),
536 "DisplayId mismatch");
Ady Abraham690f4612021-07-01 23:24:03 -0700537
Ady Abrahamace3d052022-11-17 16:25:05 -0800538 ALOGV("%s(%s)", __func__, to_string(*info.modeOpt->modePtr).c_str());
Ady Abraham690f4612021-07-01 23:24:03 -0700539
Dominik Laskowski59746512023-11-19 09:30:24 -0500540 std::scoped_lock lock(mDesiredModeLock);
541 if (mDesiredModeChanged) {
542 // A mode transition was already scheduled, so just override the desired mode.
543 const auto event = mDesiredMode.event;
544 mDesiredMode = info;
545 mDesiredMode.event = mDesiredMode.event | event;
546 return DesiredModeAction::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700547 }
548
Ady Abrahamace3d052022-11-17 16:25:05 -0800549 const auto& desiredMode = *info.modeOpt->modePtr;
550
Dominik Laskowski59746512023-11-19 09:30:24 -0500551 // If the desired mode is already active...
552 const auto activeMode = refreshRateSelector().getActiveMode();
553 if (!force && activeMode.modePtr->getId() == desiredMode.getId()) {
554 if (activeMode == info.modeOpt) {
555 return DesiredModeAction::None;
Ady Abrahamace3d052022-11-17 16:25:05 -0800556 }
557
Dominik Laskowski59746512023-11-19 09:30:24 -0500558 // ...but the render rate changed:
ramindania04b8a52023-08-07 18:49:47 -0700559 setActiveMode(desiredMode.getId(), desiredMode.getVsyncRate(), info.modeOpt->fps);
Dominik Laskowski59746512023-11-19 09:30:24 -0500560 return DesiredModeAction::InitiateRenderRateSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700561 }
562
Dominik Laskowski59746512023-11-19 09:30:24 -0500563 // Set the render frame rate to the active physical refresh rate to schedule the next
Ady Abraham95a37e22023-01-18 22:57:28 -0800564 // frame as soon as possible.
Dominik Laskowski59746512023-11-19 09:30:24 -0500565 setActiveMode(activeMode.modePtr->getId(), activeMode.modePtr->getVsyncRate(),
566 activeMode.modePtr->getVsyncRate());
Ady Abraham95a37e22023-01-18 22:57:28 -0800567
Ady Abraham690f4612021-07-01 23:24:03 -0700568 // Initiate a mode change.
Dominik Laskowski59746512023-11-19 09:30:24 -0500569 mDesiredModeChanged = true;
570 mDesiredMode = info;
571 return DesiredModeAction::InitiateDisplayModeSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700572}
573
Dominik Laskowski59746512023-11-19 09:30:24 -0500574auto DisplayDevice::getDesiredMode() const -> ftl::Optional<ActiveModeInfo> {
575 std::scoped_lock lock(mDesiredModeLock);
576 if (mDesiredModeChanged) return mDesiredMode;
Ady Abraham690f4612021-07-01 23:24:03 -0700577 return std::nullopt;
578}
579
Dominik Laskowski59746512023-11-19 09:30:24 -0500580void DisplayDevice::clearDesiredMode() {
581 std::scoped_lock lock(mDesiredModeLock);
582 mDesiredMode.event = scheduler::DisplayModeEvent::None;
583 mDesiredModeChanged = false;
Ady Abraham690f4612021-07-01 23:24:03 -0700584}
585
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500586void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000587 using fps_approx_ops::operator<=;
588 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700589 return;
590 }
591
592 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500593 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
594 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700595 return;
596 }
597
598 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000599 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
600 if (divisor == 0) {
601 mAdjustedRefreshRate = 0_Hz;
602 return;
603 }
604
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500605 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700606}
607
Dominik Laskowski663bd282018-04-19 15:26:54 -0700608std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700609
610} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800611
612// TODO(b/129481165): remove the #pragma below and fix conversion issues
613#pragma clang diagnostic pop // ignored "-Wconversion"