blob: ef9d1184dae706f99fd4648f2c85ce997cffa4fb [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
Dominik Laskowskifb078ab2023-12-08 15:26:04 -050027#include <common/FlagManager.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070028#include <compositionengine/CompositionEngine.h>
29#include <compositionengine/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070030#include <compositionengine/DisplayColorProfile.h>
31#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070032#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070033#include <compositionengine/DisplaySurface.h>
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020034#include <compositionengine/ProjectionSpace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070035#include <compositionengine/RenderSurface.h>
36#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070037#include <compositionengine/impl/OutputCompositionState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070038#include <configstore/Utils.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070039#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080040#include <system/window.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070042#include "DisplayDevice.h"
Vishnu Nairaf6d2972022-11-18 06:26:38 +000043#include "FrontEnd/DisplayInfo.h"
Sally Qi147581b2023-06-27 11:55:34 -070044#include "HdrSdrRatioOverlay.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080045#include "Layer.h"
Ady Abraham1b11bc62021-06-03 19:51:19 -070046#include "RefreshRateOverlay.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070047#include "SurfaceFlinger.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048
Peiyong Linfd997e02018-03-28 15:29:00 -070049namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Peiyong Lin65248e02020-04-18 21:15:07 -070051namespace hal = hardware::graphics::composer::hal;
52
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),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070066 mCompositionDisplay{args.compositionDisplay},
Dominik Laskowski59746512023-11-19 09:30:24 -050067 mPendingModeFpsTrace(concatId("PendingModeFps")),
68 mActiveModeFpsTrace(concatId("ActiveModeFps")),
69 mRenderRateFpsTrace(concatId("RenderRateFps")),
Dominik Laskowski718f9602019-11-09 20:01:35 -080070 mPhysicalOrientation(args.physicalOrientation),
Dominik Laskowskia42d5392022-09-29 14:26:53 -040071 mPowerMode(ftl::Concat("PowerMode ", getId().value).c_str(), args.initialPowerMode),
Ady Abraham3efa3942021-06-24 19:01:25 -070072 mIsPrimary(args.isPrimary),
Huihong Luo1768cb02022-10-11 11:10:34 -070073 mRequestedRefreshRate(args.requestedRefreshRate),
Dominik Laskowski0784adc2023-10-13 18:35:37 -040074 mRefreshRateSelector(std::move(args.refreshRateSelector)),
Dominik Laskowski1ab20c52023-11-27 11:45:57 -050075 mHasDesiredModeTrace(concatId("HasDesiredMode"), false) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070076 mCompositionDisplay->editState().isSecure = args.isSecure;
Chavi Weingarten18fa7c62023-11-28 21:16:03 +000077 mCompositionDisplay->editState().isProtected = args.isProtected;
Lloyd Pique31cb2942018-10-19 17:23:03 -070078 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070079 compositionengine::RenderSurfaceCreationArgsBuilder()
80 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
81 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
82 .setNativeWindow(std::move(args.nativeWindow))
83 .setDisplaySurface(std::move(args.displaySurface))
Lloyd Pique30db6402023-06-26 18:56:51 +000084 .setMaxTextureCacheSize(
85 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070086 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070087
Lloyd Pique30db6402023-06-26 18:56:51 +000088 if (!mFlinger->mDisableClientCompositionCache &&
89 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
Vishnu Nair9b079a22020-01-21 14:36:08 -080090 mCompositionDisplay->createClientCompositionCache(
Lloyd Pique30db6402023-06-26 18:56:51 +000091 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
Vishnu Nair9b079a22020-01-21 14:36:08 -080092 }
93
Lloyd Pique30db6402023-06-26 18:56:51 +000094 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
95 mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070096 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010097 compositionengine::DisplayColorProfileCreationArgsBuilder()
98 .setHasWideColorGamut(args.hasWideColorGamut)
99 .setHdrCapabilities(std::move(args.hdrCapabilities))
100 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
101 .setHwcColorModes(std::move(args.hwcColorModes))
102 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700103
Lloyd Pique32cbe282018-10-19 13:09:22 -0700104 if (!mCompositionDisplay->isValid()) {
105 ALOGE("Composition Display did not validate!");
106 }
107
Lloyd Pique31cb2942018-10-19 17:23:03 -0700108 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700109
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400110 setPowerMode(args.initialPowerMode);
Alec Mouriba013fa2018-10-16 12:43:11 -0700111
Jesse Hallffe1f192013-03-22 15:13:48 -0700112 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800113 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114}
115
Lloyd Pique09594832018-01-22 17:48:03 -0800116DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700117
Lloyd Pique45a165a2018-10-19 11:54:47 -0700118void DisplayDevice::disconnect() {
119 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700120}
121
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700122int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000123 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700124}
125
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700126int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000127 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700128}
129
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700130void DisplayDevice::setDisplayName(const std::string& displayName) {
131 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700132 // never override the name with an empty name
133 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700134 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700135 }
136}
137
Vishnu Nairaf6d2972022-11-18 06:26:38 +0000138auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700139 gui::DisplayInfo info;
140 info.displayId = getLayerStack().id;
141
142 // The physical orientation is set when the orientation of the display panel is
143 // different than the default orientation of the device. Other services like
144 // InputFlinger do not know about this, so we do not need to expose the physical
145 // orientation of the panel outside of SurfaceFlinger.
146 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
147 auto width = getWidth();
148 auto height = getHeight();
149 if (inversePhysicalOrientation == ui::ROTATION_90 ||
150 inversePhysicalOrientation == ui::ROTATION_270) {
151 std::swap(width, height);
152 }
153 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
154 inversePhysicalOrientation),
155 width, height);
156 const auto& displayTransform = undoPhysicalOrientation * getTransform();
157 // Send the inverse display transform to input so it can convert display coordinates to
158 // logical display.
159 info.transform = displayTransform.inverse();
160
161 info.logicalWidth = getLayerStackSpaceRect().width();
162 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800163
164 return {.info = info,
165 .transform = displayTransform,
166 .receivesInput = receivesInput(),
Vishnu Nair67b431c2022-11-16 01:54:05 +0000167 .isSecure = isSecure(),
168 .isPrimary = isPrimary(),
Vishnu Nair81750622023-03-08 15:02:06 -0800169 .isVirtual = isVirtual(),
Vishnu Naircfb2d252023-01-19 04:44:02 +0000170 .rotationFlags = ui::Transform::toRotationFlags(mOrientation),
171 .transformHint = getTransformHint()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700172}
173
Peiyong Lin65248e02020-04-18 21:15:07 -0700174void DisplayDevice::setPowerMode(hal::PowerMode mode) {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400175 // TODO(b/241285876): Skip this for virtual displays.
joenchene72ba5e2022-08-24 13:08:58 +0000176 if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
linpeterdcfd4e62022-12-29 09:39:14 +0800177 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchene72ba5e2022-08-24 13:08:58 +0000178 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
179 mBrightness = *mStagedBrightness;
180 }
181 mStagedBrightness = std::nullopt;
182 getCompositionDisplay()->applyDisplayBrightness(true);
183 }
184
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400185 mPowerMode = mode;
linpeter1c69cd32022-07-13 19:20:48 +0800186
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500187 getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700188}
189
Ady Abraham66452722023-03-14 17:41:47 -0700190void DisplayDevice::tracePowerMode() {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400191 // Assign the same value for tracing.
192 mPowerMode = mPowerMode.get();
Ady Abraham66452722023-03-14 17:41:47 -0700193}
194
Alec Mouri023c1882021-05-08 16:36:33 -0700195void DisplayDevice::enableLayerCaching(bool enable) {
196 getCompositionDisplay()->setLayerCachingEnabled(enable);
197}
198
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400199hal::PowerMode DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700200 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700201}
202
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700203bool DisplayDevice::isPoweredOn() const {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400204 return mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700205}
206
ramindania04b8a52023-08-07 18:49:47 -0700207void DisplayDevice::setActiveMode(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
Dominik Laskowski59746512023-11-19 09:30:24 -0500208 ATRACE_INT(mActiveModeFpsTrace.c_str(), vsyncRate.getIntValue());
209 ATRACE_INT(mRenderRateFpsTrace.c_str(), renderFps.getIntValue());
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700210
Ady Abrahamace3d052022-11-17 16:25:05 -0800211 mRefreshRateSelector->setActiveMode(modeId, renderFps);
ramindania04b8a52023-08-07 18:49:47 -0700212 updateRefreshRateOverlayRate(vsyncRate, renderFps);
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700213}
214
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500215bool DisplayDevice::initiateModeChange(display::DisplayModeRequest&& desiredMode,
Dominik Laskowski4b32e4b2023-11-19 10:24:51 -0500216 const hal::VsyncPeriodChangeConstraints& constraints,
217 hal::VsyncPeriodChangeTimeline& outTimeline) {
Dominik Laskowskifb078ab2023-12-08 15:26:04 -0500218 // TODO(b/255635711): Flow the DisplayModeRequest through the desired/pending/active states. For
219 // now, `desiredMode` and `mDesiredModeOpt` are one and the same, but the latter is not cleared
220 // until the next `SF::initiateDisplayModeChanges`. However, the desired mode has been consumed
221 // at this point, so clear the `force` flag to prevent an endless loop of `initiateModeChange`.
222 if (FlagManager::getInstance().connected_display()) {
223 std::scoped_lock lock(mDesiredModeLock);
224 if (mDesiredModeOpt) {
225 mDesiredModeOpt->force = false;
226 }
227 }
228
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500229 mPendingModeOpt = std::move(desiredMode);
Dominik Laskowski63abc2b2023-12-21 19:33:25 +0000230 mIsModeSetPending = true;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400231
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500232 const auto& mode = *mPendingModeOpt->mode.modePtr;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400233
Dominik Laskowski4b32e4b2023-11-19 10:24:51 -0500234 if (mHwComposer.setActiveModeWithConstraints(getPhysicalId(), mode.getHwcId(), constraints,
235 &outTimeline) != OK) {
236 return false;
237 }
238
239 ATRACE_INT(mPendingModeFpsTrace.c_str(), mode.getVsyncRate().getIntValue());
240 return true;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400241}
242
Dominik Laskowski63abc2b2023-12-21 19:33:25 +0000243void DisplayDevice::finalizeModeChange(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
244 setActiveMode(modeId, vsyncRate, renderFps);
245 mIsModeSetPending = false;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100246}
247
Marin Shalamanov045b7002021-01-07 16:56:24 +0100248nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
249 const auto physicalId = getPhysicalId();
250 if (!mHwComposer.isConnected(physicalId)) {
251 return 0;
252 }
253
254 nsecs_t vsyncPeriod;
255 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
256 if (status == NO_ERROR) {
257 return vsyncPeriod;
258 }
259
ramindania04b8a52023-08-07 18:49:47 -0700260 return refreshRateSelector().getActiveMode().modePtr->getVsyncRate().getPeriodNsecs();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100261}
262
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800263ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700264 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800265}
266
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700267void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
268 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700269 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700270 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700271 }
Sally Qi147581b2023-06-27 11:55:34 -0700272 if (mHdrSdrRatioOverlay) {
273 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
274 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700275}
276
Evan Rosky2239b372021-05-20 13:43:47 -0700277void DisplayDevice::setFlags(uint32_t flags) {
278 mFlags = flags;
279}
280
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800281void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200282 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700283 const auto size = ui::Size(width, height);
284 mCompositionDisplay->setDisplaySize(size);
285 if (mRefreshRateOverlay) {
286 mRefreshRateOverlay->setViewport(size);
287 }
Sally Qi147581b2023-06-27 11:55:34 -0700288 if (mHdrSdrRatioOverlay) {
289 mHdrSdrRatioOverlay->setViewport(size);
290 }
Michael Lentine47e45402014-07-18 15:34:25 -0700291}
292
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200293void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
294 Rect orientedDisplaySpaceRect) {
Sally Qi147581b2023-06-27 11:55:34 -0700295 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700296 mOrientation = orientation;
297
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200298 // We need to take care of display rotation for globalTransform for case if the panel is not
299 // installed aligned with device orientation.
300 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000301
302 const auto& state = getCompositionDisplay()->getState();
303
304 // If the layer stack and destination frames have never been set, then configure them to be the
305 // same as the physical device, taking into account the total transform.
306 if (!orientedDisplaySpaceRect.isValid()) {
307 ui::Size bounds = state.displaySpace.getBounds();
308 bounds.rotate(transformOrientation);
309 orientedDisplaySpaceRect = Rect(bounds);
310 }
311 if (layerStackSpaceRect.isEmpty()) {
312 ui::Size bounds = state.framebufferSpace.getBounds();
313 bounds.rotate(transformOrientation);
314 layerStackSpaceRect = Rect(bounds);
315 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200316 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
317 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700318}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700319
Alec Mouricdf16792021-12-10 13:16:06 -0800320void DisplayDevice::stageBrightness(float brightness) {
321 mStagedBrightness = brightness;
322}
323
324void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800325 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000326 if (needsComposite) {
327 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
328 }
Alec Mouricdf16792021-12-10 13:16:06 -0800329 mBrightness = *mStagedBrightness;
330 }
331 mStagedBrightness = std::nullopt;
332}
333
334std::optional<float> DisplayDevice::getStagedBrightness() const {
335 return mStagedBrightness;
336}
337
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700338void DisplayDevice::dump(utils::Dumper& dumper) const {
339 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100340
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400341 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700342 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700343
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400344 if (mRefreshRateSelector) {
345 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700346 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700347}
Irvelffc9efc2016-07-27 15:16:37 -0700348
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700349bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
350 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700351}
352
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200353DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700354 return mCompositionDisplay->getId();
355}
356
357bool DisplayDevice::isSecure() const {
358 return mCompositionDisplay->isSecure();
359}
360
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700361void DisplayDevice::setSecure(bool secure) {
362 mCompositionDisplay->setSecure(secure);
363}
364
Angel Aguayob108a6d2021-08-06 21:34:57 +0000365const Rect DisplayDevice::getBounds() const {
366 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700367}
368
369const Region& DisplayDevice::getUndefinedRegion() const {
370 return mCompositionDisplay->getState().undefinedRegion;
371}
372
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800373ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700374 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700375}
376
Garfield Tan54edd912020-10-21 16:31:41 -0700377ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
378 return mCompositionDisplay->getTransformHint();
379}
380
Lloyd Pique32cbe282018-10-19 13:09:22 -0700381const ui::Transform& DisplayDevice::getTransform() const {
382 return mCompositionDisplay->getState().transform;
383}
384
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200385const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000386 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700387}
388
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200389const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000390 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700391}
392
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700393bool DisplayDevice::hasWideColorGamut() const {
394 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
395}
396
397bool DisplayDevice::hasHDR10PlusSupport() const {
398 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
399}
400
401bool DisplayDevice::hasHDR10Support() const {
402 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
403}
404
405bool DisplayDevice::hasHLGSupport() const {
406 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
407}
408
409bool DisplayDevice::hasDolbyVisionSupport() const {
410 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
411}
412
413int DisplayDevice::getSupportedPerFrameMetadata() const {
414 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
415}
416
Kriti Dang49ad4132021-01-08 11:49:56 +0100417void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
418 mOverrideHdrTypes = hdrTypes;
419}
420
421HdrCapabilities DisplayDevice::getHdrCapabilities() const {
422 const HdrCapabilities& capabilities =
423 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
424 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
425 if (!mOverrideHdrTypes.empty()) {
426 hdrTypes = mOverrideHdrTypes;
427 }
428 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
429 capabilities.getDesiredMaxAverageLuminance(),
430 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700431}
432
Sally Qi147581b2023-06-27 11:55:34 -0700433void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
434 if (!enable) {
435 mHdrSdrRatioOverlay.reset();
436 return;
437 }
438
Ady Abrahamddba9342023-10-02 16:28:03 -0700439 mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
440 if (mHdrSdrRatioOverlay) {
441 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
442 mHdrSdrRatioOverlay->setViewport(getSize());
443 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
444 }
Sally Qi147581b2023-06-27 11:55:34 -0700445}
446
447void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
448 ATRACE_CALL();
449 mHdrSdrRatio = currentHdrSdrRatio;
450 if (mHdrSdrRatioOverlay) {
451 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
452 }
453}
454
Lloyd Pique30db6402023-06-26 18:56:51 +0000455void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, bool showSpinner,
456 bool showRenderRate, bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700457 if (!enable) {
458 mRefreshRateOverlay.reset();
459 return;
460 }
461
Ady Abraham0aa373a2022-11-22 13:56:50 -0800462 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000463 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800464 features |= RefreshRateOverlay::Features::Spinner;
465 }
466
Lloyd Pique30db6402023-06-26 18:56:51 +0000467 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800468 features |= RefreshRateOverlay::Features::RenderRate;
469 }
470
Lloyd Pique30db6402023-06-26 18:56:51 +0000471 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800472 features |= RefreshRateOverlay::Features::ShowInMiddle;
473 }
474
ramindanib2158ee2023-02-13 20:29:59 -0800475 if (setByHwc) {
476 features |= RefreshRateOverlay::Features::SetByHwc;
477 }
478
ramindania04b8a52023-08-07 18:49:47 -0700479 // TODO(b/296636258) Update to use the render rate range in VRR mode.
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400480 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abrahamddba9342023-10-02 16:28:03 -0700481 mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
482 if (mRefreshRateOverlay) {
483 mRefreshRateOverlay->setLayerStack(getLayerStack());
484 mRefreshRateOverlay->setViewport(getSize());
485 updateRefreshRateOverlayRate(getActiveMode().modePtr->getVsyncRate(), getActiveMode().fps,
486 setByHwc);
487 }
ramindanib2158ee2023-02-13 20:29:59 -0800488}
489
ramindania04b8a52023-08-07 18:49:47 -0700490void DisplayDevice::updateRefreshRateOverlayRate(Fps vsyncRate, Fps renderFps, bool setByHwc) {
ramindanib2158ee2023-02-13 20:29:59 -0800491 ATRACE_CALL();
ramindani3faf5742023-09-21 13:45:12 -0700492 if (mRefreshRateOverlay) {
493 if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
494 mRefreshRateOverlay->changeRefreshRate(vsyncRate, renderFps);
495 } else {
496 mRefreshRateOverlay->changeRenderRate(renderFps);
497 }
ramindanib2158ee2023-02-13 20:29:59 -0800498 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700499}
500
501bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
502 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400503 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800504 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400505 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800506 if (newMode) {
ramindania04b8a52023-08-07 18:49:47 -0700507 updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700508 return true;
509 }
510 }
511
512 return false;
513}
514
Sally Qi147581b2023-06-27 11:55:34 -0700515void DisplayDevice::animateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700516 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700517 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700518 }
Sally Qi147581b2023-06-27 11:55:34 -0700519 if (mHdrSdrRatioOverlay) {
520 // hdr sdr ratio is designed to be on the top right of the screen,
521 // therefore, we need to re-calculate the display's width and height
522 if (mIsOrientationChanged) {
523 auto width = getWidth();
524 auto height = getHeight();
525 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
526 std::swap(width, height);
527 }
528 mHdrSdrRatioOverlay->setViewport({width, height});
529 }
530 mHdrSdrRatioOverlay->animate();
531 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700532}
533
Dominik Laskowskifb078ab2023-12-08 15:26:04 -0500534auto DisplayDevice::setDesiredMode(display::DisplayModeRequest&& desiredMode) -> DesiredModeAction {
Ady Abraham690f4612021-07-01 23:24:03 -0700535 ATRACE_CALL();
536
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500537 const auto& desiredModePtr = desiredMode.mode.modePtr;
538
539 LOG_ALWAYS_FATAL_IF(getPhysicalId() != desiredModePtr->getPhysicalDisplayId(),
Ady Abrahamace3d052022-11-17 16:25:05 -0800540 "DisplayId mismatch");
Ady Abraham690f4612021-07-01 23:24:03 -0700541
Dominik Laskowskifb078ab2023-12-08 15:26:04 -0500542 // TODO (b/318533819): Stringize DisplayModeRequest.
543 ALOGD("%s(%s, force=%s)", __func__, to_string(*desiredModePtr).c_str(),
544 desiredMode.force ? "true" : "false");
Ady Abraham690f4612021-07-01 23:24:03 -0700545
Dominik Laskowski59746512023-11-19 09:30:24 -0500546 std::scoped_lock lock(mDesiredModeLock);
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500547 if (mDesiredModeOpt) {
Dominik Laskowski59746512023-11-19 09:30:24 -0500548 // A mode transition was already scheduled, so just override the desired mode.
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500549 const bool emitEvent = mDesiredModeOpt->emitEvent;
Dominik Laskowskifb078ab2023-12-08 15:26:04 -0500550 const bool force = mDesiredModeOpt->force;
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500551 mDesiredModeOpt = std::move(desiredMode);
552 mDesiredModeOpt->emitEvent |= emitEvent;
Dominik Laskowskifb078ab2023-12-08 15:26:04 -0500553 if (FlagManager::getInstance().connected_display()) {
554 mDesiredModeOpt->force |= force;
555 }
Dominik Laskowski59746512023-11-19 09:30:24 -0500556 return DesiredModeAction::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700557 }
558
Dominik Laskowski59746512023-11-19 09:30:24 -0500559 // If the desired mode is already active...
560 const auto activeMode = refreshRateSelector().getActiveMode();
Dominik Laskowskifb078ab2023-12-08 15:26:04 -0500561 if (!desiredMode.force && activeMode.modePtr->getId() == desiredModePtr->getId()) {
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500562 if (activeMode == desiredMode.mode) {
Dominik Laskowski59746512023-11-19 09:30:24 -0500563 return DesiredModeAction::None;
Ady Abrahamace3d052022-11-17 16:25:05 -0800564 }
565
Dominik Laskowski59746512023-11-19 09:30:24 -0500566 // ...but the render rate changed:
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500567 setActiveMode(desiredModePtr->getId(), desiredModePtr->getVsyncRate(),
568 desiredMode.mode.fps);
Dominik Laskowski59746512023-11-19 09:30:24 -0500569 return DesiredModeAction::InitiateRenderRateSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700570 }
571
Dominik Laskowski59746512023-11-19 09:30:24 -0500572 // Set the render frame rate to the active physical refresh rate to schedule the next
Ady Abraham95a37e22023-01-18 22:57:28 -0800573 // frame as soon as possible.
Dominik Laskowski59746512023-11-19 09:30:24 -0500574 setActiveMode(activeMode.modePtr->getId(), activeMode.modePtr->getVsyncRate(),
575 activeMode.modePtr->getVsyncRate());
Ady Abraham95a37e22023-01-18 22:57:28 -0800576
Ady Abraham690f4612021-07-01 23:24:03 -0700577 // Initiate a mode change.
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500578 mDesiredModeOpt = std::move(desiredMode);
579 mHasDesiredModeTrace = true;
Dominik Laskowski59746512023-11-19 09:30:24 -0500580 return DesiredModeAction::InitiateDisplayModeSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700581}
582
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500583auto DisplayDevice::getDesiredMode() const -> DisplayModeRequestOpt {
Dominik Laskowski59746512023-11-19 09:30:24 -0500584 std::scoped_lock lock(mDesiredModeLock);
Dominik Laskowski1ab20c52023-11-27 11:45:57 -0500585 return mDesiredModeOpt;
Ady Abraham690f4612021-07-01 23:24:03 -0700586}
587
Dominik Laskowski63abc2b2023-12-21 19:33:25 +0000588void DisplayDevice::clearDesiredMode() {
589 std::scoped_lock lock(mDesiredModeLock);
590 mDesiredModeOpt.reset();
591 mHasDesiredModeTrace = false;
Ady Abraham690f4612021-07-01 23:24:03 -0700592}
593
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500594void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000595 using fps_approx_ops::operator<=;
596 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700597 return;
598 }
599
600 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500601 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
602 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700603 return;
604 }
605
606 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000607 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
608 if (divisor == 0) {
609 mAdjustedRefreshRate = 0_Hz;
610 return;
611 }
612
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500613 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700614}
615
Dominik Laskowski663bd282018-04-19 15:26:54 -0700616std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700617
618} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800619
620// TODO(b/129481165): remove the #pragma below and fix conversion issues
621#pragma clang diagnostic pop // ignored "-Wconversion"