blob: 32bd890aee5d0c12b6dd7a8f1da7871135dd8639 [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
Dominik Laskowskib363c4c2022-08-02 14:03:41 -070041#include "Display/DisplaySnapshot.h"
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},
Ady Abraham690f4612021-07-01 23:24:03 -070067 mActiveModeFPSTrace("ActiveModeFPS -" + to_string(getId())),
68 mActiveModeFPSHwcTrace("ActiveModeFPS_HWC -" + to_string(getId())),
Ady Abrahamace3d052022-11-17 16:25:05 -080069 mRenderFrameRateFPSTrace("RenderRateFPS -" + to_string(getId())),
Dominik Laskowski718f9602019-11-09 20:01:35 -080070 mPhysicalOrientation(args.physicalOrientation),
Ady Abraham3efa3942021-06-24 19:01:25 -070071 mIsPrimary(args.isPrimary),
Huihong Luo1768cb02022-10-11 11:10:34 -070072 mRequestedRefreshRate(args.requestedRefreshRate),
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040073 mRefreshRateSelector(std::move(args.refreshRateSelector)) {
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
Ady Abrahamace3d052022-11-17 16:25:05 -0800212void DisplayDevice::setActiveMode(DisplayModeId modeId, Fps displayFps, Fps renderFps) {
213 ATRACE_INT(mActiveModeFPSTrace.c_str(), displayFps.getIntValue());
214 ATRACE_INT(mRenderFrameRateFPSTrace.c_str(), renderFps.getIntValue());
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700215
Ady Abrahamace3d052022-11-17 16:25:05 -0800216 mRefreshRateSelector->setActiveMode(modeId, renderFps);
Dominik Laskowski02bb2072022-08-26 15:02:52 -0700217
Ady Abraham1b11bc62021-06-03 19:51:19 -0700218 if (mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800219 mRefreshRateOverlay->changeRefreshRate(displayFps, renderFps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700220 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700221}
222
Ady Abraham690f4612021-07-01 23:24:03 -0700223status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100224 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700225 hal::VsyncPeriodChangeTimeline* outTimeline) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800226 if (!info.modeOpt || info.modeOpt->modePtr->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100227 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abrahamace3d052022-11-17 16:25:05 -0800228 info.modeOpt ? std::to_string(info.modeOpt->modePtr->getId().value()).c_str()
229 : "null",
Ady Abraham690f4612021-07-01 23:24:03 -0700230 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100231 return BAD_VALUE;
232 }
Ady Abraham690f4612021-07-01 23:24:03 -0700233 mUpcomingActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800234 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.modeOpt->modePtr->getFps().getIntValue());
235 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(),
236 info.modeOpt->modePtr->getHwcId(), constraints,
237 outTimeline);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100238}
239
Marin Shalamanov045b7002021-01-07 16:56:24 +0100240nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
241 const auto physicalId = getPhysicalId();
242 if (!mHwComposer.isConnected(physicalId)) {
243 return 0;
244 }
245
246 nsecs_t vsyncPeriod;
247 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
248 if (status == NO_ERROR) {
249 return vsyncPeriod;
250 }
251
Ady Abrahamace3d052022-11-17 16:25:05 -0800252 return refreshRateSelector().getActiveMode().modePtr->getVsyncPeriod();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100253}
254
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800255ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700256 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800257}
258
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700259void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
260 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700261 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700262 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700263 }
Sally Qi147581b2023-06-27 11:55:34 -0700264 if (mHdrSdrRatioOverlay) {
265 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
266 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700267}
268
Evan Rosky2239b372021-05-20 13:43:47 -0700269void DisplayDevice::setFlags(uint32_t flags) {
270 mFlags = flags;
271}
272
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800273void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200274 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700275 const auto size = ui::Size(width, height);
276 mCompositionDisplay->setDisplaySize(size);
277 if (mRefreshRateOverlay) {
278 mRefreshRateOverlay->setViewport(size);
279 }
Sally Qi147581b2023-06-27 11:55:34 -0700280 if (mHdrSdrRatioOverlay) {
281 mHdrSdrRatioOverlay->setViewport(size);
282 }
Michael Lentine47e45402014-07-18 15:34:25 -0700283}
284
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200285void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
286 Rect orientedDisplaySpaceRect) {
Sally Qi147581b2023-06-27 11:55:34 -0700287 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700288 mOrientation = orientation;
289
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200290 // We need to take care of display rotation for globalTransform for case if the panel is not
291 // installed aligned with device orientation.
292 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000293
294 const auto& state = getCompositionDisplay()->getState();
295
296 // If the layer stack and destination frames have never been set, then configure them to be the
297 // same as the physical device, taking into account the total transform.
298 if (!orientedDisplaySpaceRect.isValid()) {
299 ui::Size bounds = state.displaySpace.getBounds();
300 bounds.rotate(transformOrientation);
301 orientedDisplaySpaceRect = Rect(bounds);
302 }
303 if (layerStackSpaceRect.isEmpty()) {
304 ui::Size bounds = state.framebufferSpace.getBounds();
305 bounds.rotate(transformOrientation);
306 layerStackSpaceRect = Rect(bounds);
307 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200308 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
309 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700310}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700311
Alec Mouricdf16792021-12-10 13:16:06 -0800312void DisplayDevice::stageBrightness(float brightness) {
313 mStagedBrightness = brightness;
314}
315
316void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800317 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000318 if (needsComposite) {
319 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
320 }
Alec Mouricdf16792021-12-10 13:16:06 -0800321 mBrightness = *mStagedBrightness;
322 }
323 mStagedBrightness = std::nullopt;
324}
325
326std::optional<float> DisplayDevice::getStagedBrightness() const {
327 return mStagedBrightness;
328}
329
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700330void DisplayDevice::dump(utils::Dumper& dumper) const {
331 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100332
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400333 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700334 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700335
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400336 if (mRefreshRateSelector) {
337 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700338 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700339}
Irvelffc9efc2016-07-27 15:16:37 -0700340
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700341bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
342 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700343}
344
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200345DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700346 return mCompositionDisplay->getId();
347}
348
349bool DisplayDevice::isSecure() const {
350 return mCompositionDisplay->isSecure();
351}
352
Angel Aguayob108a6d2021-08-06 21:34:57 +0000353const Rect DisplayDevice::getBounds() const {
354 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700355}
356
357const Region& DisplayDevice::getUndefinedRegion() const {
358 return mCompositionDisplay->getState().undefinedRegion;
359}
360
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800361ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700362 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700363}
364
Garfield Tan54edd912020-10-21 16:31:41 -0700365ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
366 return mCompositionDisplay->getTransformHint();
367}
368
Lloyd Pique32cbe282018-10-19 13:09:22 -0700369const ui::Transform& DisplayDevice::getTransform() const {
370 return mCompositionDisplay->getState().transform;
371}
372
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200373const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000374 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700375}
376
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200377const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000378 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700379}
380
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700381bool DisplayDevice::hasWideColorGamut() const {
382 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
383}
384
385bool DisplayDevice::hasHDR10PlusSupport() const {
386 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
387}
388
389bool DisplayDevice::hasHDR10Support() const {
390 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
391}
392
393bool DisplayDevice::hasHLGSupport() const {
394 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
395}
396
397bool DisplayDevice::hasDolbyVisionSupport() const {
398 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
399}
400
401int DisplayDevice::getSupportedPerFrameMetadata() const {
402 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
403}
404
Kriti Dang49ad4132021-01-08 11:49:56 +0100405void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
406 mOverrideHdrTypes = hdrTypes;
407}
408
409HdrCapabilities DisplayDevice::getHdrCapabilities() const {
410 const HdrCapabilities& capabilities =
411 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
412 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
413 if (!mOverrideHdrTypes.empty()) {
414 hdrTypes = mOverrideHdrTypes;
415 }
416 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
417 capabilities.getDesiredMaxAverageLuminance(),
418 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700419}
420
Sally Qi147581b2023-06-27 11:55:34 -0700421void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
422 if (!enable) {
423 mHdrSdrRatioOverlay.reset();
424 return;
425 }
426
427 mHdrSdrRatioOverlay = std::make_unique<HdrSdrRatioOverlay>();
428 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
429 mHdrSdrRatioOverlay->setViewport(getSize());
430 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
431}
432
433void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
434 ATRACE_CALL();
435 mHdrSdrRatio = currentHdrSdrRatio;
436 if (mHdrSdrRatioOverlay) {
437 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
438 }
439}
440
Lloyd Pique30db6402023-06-26 18:56:51 +0000441void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, bool showSpinner,
442 bool showRenderRate, bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700443 if (!enable) {
444 mRefreshRateOverlay.reset();
445 return;
446 }
447
Ady Abraham0aa373a2022-11-22 13:56:50 -0800448 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000449 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800450 features |= RefreshRateOverlay::Features::Spinner;
451 }
452
Lloyd Pique30db6402023-06-26 18:56:51 +0000453 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800454 features |= RefreshRateOverlay::Features::RenderRate;
455 }
456
Lloyd Pique30db6402023-06-26 18:56:51 +0000457 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800458 features |= RefreshRateOverlay::Features::ShowInMiddle;
459 }
460
ramindanib2158ee2023-02-13 20:29:59 -0800461 if (setByHwc) {
462 features |= RefreshRateOverlay::Features::SetByHwc;
463 }
464
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400465 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abraham0aa373a2022-11-22 13:56:50 -0800466 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(fpsRange, features);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700467 mRefreshRateOverlay->setLayerStack(getLayerStack());
468 mRefreshRateOverlay->setViewport(getSize());
ramindanib2158ee2023-02-13 20:29:59 -0800469 updateRefreshRateOverlayRate(getActiveMode().modePtr->getFps(), getActiveMode().fps);
470}
471
472void DisplayDevice::updateRefreshRateOverlayRate(Fps displayFps, Fps renderFps, bool setByHwc) {
473 ATRACE_CALL();
474 if (mRefreshRateOverlay && (!mRefreshRateOverlay->isSetByHwc() || setByHwc)) {
475 mRefreshRateOverlay->changeRefreshRate(displayFps, renderFps);
476 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700477}
478
479bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
480 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400481 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800482 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400483 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800484 if (newMode) {
ramindanib2158ee2023-02-13 20:29:59 -0800485 updateRefreshRateOverlayRate(newMode->modePtr->getFps(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700486 return true;
487 }
488 }
489
490 return false;
491}
492
Sally Qi147581b2023-06-27 11:55:34 -0700493void DisplayDevice::animateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700494 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700495 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700496 }
Sally Qi147581b2023-06-27 11:55:34 -0700497 if (mHdrSdrRatioOverlay) {
498 // hdr sdr ratio is designed to be on the top right of the screen,
499 // therefore, we need to re-calculate the display's width and height
500 if (mIsOrientationChanged) {
501 auto width = getWidth();
502 auto height = getHeight();
503 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
504 std::swap(width, height);
505 }
506 mHdrSdrRatioOverlay->setViewport({width, height});
507 }
508 mHdrSdrRatioOverlay->animate();
509 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700510}
511
Dominik Laskowskifc378b02022-12-02 14:56:05 -0500512auto DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info, bool force)
513 -> DesiredActiveModeAction {
Ady Abraham690f4612021-07-01 23:24:03 -0700514 ATRACE_CALL();
515
Ady Abrahamace3d052022-11-17 16:25:05 -0800516 LOG_ALWAYS_FATAL_IF(!info.modeOpt, "desired mode not provided");
517 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.modeOpt->modePtr->getPhysicalDisplayId(),
518 "DisplayId mismatch");
Ady Abraham690f4612021-07-01 23:24:03 -0700519
Ady Abrahamace3d052022-11-17 16:25:05 -0800520 ALOGV("%s(%s)", __func__, to_string(*info.modeOpt->modePtr).c_str());
Ady Abraham690f4612021-07-01 23:24:03 -0700521
522 std::scoped_lock lock(mActiveModeLock);
523 if (mDesiredActiveModeChanged) {
524 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700525 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700526 mDesiredActiveMode = info;
527 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
Ady Abrahamace3d052022-11-17 16:25:05 -0800528 return DesiredActiveModeAction::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700529 }
530
Ady Abrahamace3d052022-11-17 16:25:05 -0800531 const auto& desiredMode = *info.modeOpt->modePtr;
532
Ady Abraham690f4612021-07-01 23:24:03 -0700533 // Check if we are already at the desired mode
Ady Abraham95a37e22023-01-18 22:57:28 -0800534 const auto currentMode = refreshRateSelector().getActiveMode();
535 if (!force && currentMode.modePtr->getId() == desiredMode.getId()) {
536 if (currentMode == info.modeOpt) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800537 return DesiredActiveModeAction::None;
538 }
539
540 setActiveMode(desiredMode.getId(), desiredMode.getFps(), info.modeOpt->fps);
541 return DesiredActiveModeAction::InitiateRenderRateSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700542 }
543
Ady Abraham95a37e22023-01-18 22:57:28 -0800544 // Set the render frame rate to the current physical refresh rate to schedule the next
545 // frame as soon as possible.
546 setActiveMode(currentMode.modePtr->getId(), currentMode.modePtr->getFps(),
547 currentMode.modePtr->getFps());
548
Ady Abraham690f4612021-07-01 23:24:03 -0700549 // Initiate a mode change.
550 mDesiredActiveModeChanged = true;
551 mDesiredActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800552 return DesiredActiveModeAction::InitiateDisplayModeSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700553}
554
555std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
556 std::scoped_lock lock(mActiveModeLock);
557 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
558 return std::nullopt;
559}
560
561void DisplayDevice::clearDesiredActiveModeState() {
562 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700563 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700564 mDesiredActiveModeChanged = false;
565}
566
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500567void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000568 using fps_approx_ops::operator<=;
569 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700570 return;
571 }
572
573 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500574 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
575 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700576 return;
577 }
578
579 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000580 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
581 if (divisor == 0) {
582 mAdjustedRefreshRate = 0_Hz;
583 return;
584 }
585
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500586 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700587}
588
Dominik Laskowski663bd282018-04-19 15:26:54 -0700589std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700590
591} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800592
593// TODO(b/129481165): remove the #pragma below and fix conversion issues
594#pragma clang diagnostic pop // ignored "-Wconversion"