blob: a89b23cc28bb7502f00564ac932cfa27a81fb76c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021// #define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "DisplayDevice"
24
Ady Abraham690f4612021-07-01 23:24:03 -070025#define ATRACE_TAG ATRACE_TAG_GRAPHICS
26
Lloyd Pique45a165a2018-10-19 11:54:47 -070027#include <compositionengine/CompositionEngine.h>
28#include <compositionengine/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070029#include <compositionengine/DisplayColorProfile.h>
30#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070031#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070032#include <compositionengine/DisplaySurface.h>
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020033#include <compositionengine/ProjectionSpace.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070034#include <compositionengine/RenderSurface.h>
35#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070036#include <compositionengine/impl/OutputCompositionState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070037#include <configstore/Utils.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070038#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080039#include <system/window.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070040#include <ui/GraphicTypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Dominik Laskowskib363c4c2022-08-02 14:03:41 -070042#include "Display/DisplaySnapshot.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070043#include "DisplayDevice.h"
Vishnu Nairaf6d2972022-11-18 06:26:38 +000044#include "FrontEnd/DisplayInfo.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
Dominik Laskowski718f9602019-11-09 20:01:35 -080053ui::Transform::RotationFlags DisplayDevice::sPrimaryDisplayRotationFlags = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -070054
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070055DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010056 const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070057 std::shared_ptr<compositionengine::Display> compositionDisplay)
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010058 : flinger(flinger),
59 hwComposer(hwComposer),
60 displayToken(displayToken),
61 compositionDisplay(compositionDisplay) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070062
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070063DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070064 : mFlinger(args.flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010065 mHwComposer(args.hwComposer),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070066 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080067 mSequenceId(args.sequenceId),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070068 mCompositionDisplay{args.compositionDisplay},
Ady Abraham690f4612021-07-01 23:24:03 -070069 mActiveModeFPSTrace("ActiveModeFPS -" + to_string(getId())),
70 mActiveModeFPSHwcTrace("ActiveModeFPS_HWC -" + to_string(getId())),
Ady Abrahamace3d052022-11-17 16:25:05 -080071 mRenderFrameRateFPSTrace("RenderRateFPS -" + to_string(getId())),
Dominik Laskowski718f9602019-11-09 20:01:35 -080072 mPhysicalOrientation(args.physicalOrientation),
Ady Abraham3efa3942021-06-24 19:01:25 -070073 mIsPrimary(args.isPrimary),
Huihong Luo1768cb02022-10-11 11:10:34 -070074 mRequestedRefreshRate(args.requestedRefreshRate),
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040075 mRefreshRateSelector(std::move(args.refreshRateSelector)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070076 mCompositionDisplay->editState().isSecure = args.isSecure;
Lloyd Pique31cb2942018-10-19 17:23:03 -070077 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070078 compositionengine::RenderSurfaceCreationArgsBuilder()
79 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
80 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
81 .setNativeWindow(std::move(args.nativeWindow))
82 .setDisplaySurface(std::move(args.displaySurface))
83 .setMaxTextureCacheSize(
84 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
85 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070086
Vishnu Nair9b079a22020-01-21 14:36:08 -080087 if (!mFlinger->mDisableClientCompositionCache &&
88 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
89 mCompositionDisplay->createClientCompositionCache(
90 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
91 }
92
Vishnu Naira3140382022-02-24 14:07:11 -080093 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
Alec Mouridda07d92022-04-25 22:39:25 +000094 mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070095 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010096 compositionengine::DisplayColorProfileCreationArgsBuilder()
97 .setHasWideColorGamut(args.hasWideColorGamut)
98 .setHdrCapabilities(std::move(args.hdrCapabilities))
99 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
100 .setHwcColorModes(std::move(args.hwcColorModes))
101 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700102
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103 if (!mCompositionDisplay->isValid()) {
104 ALOGE("Composition Display did not validate!");
105 }
106
Lloyd Pique31cb2942018-10-19 17:23:03 -0700107 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700108
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500109 if (const auto powerModeOpt = args.initialPowerMode) {
110 setPowerMode(*powerModeOpt);
111 }
Alec Mouriba013fa2018-10-16 12:43:11 -0700112
Jesse Hallffe1f192013-03-22 15:13:48 -0700113 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800114 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115}
116
Lloyd Pique09594832018-01-22 17:48:03 -0800117DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700118
Lloyd Pique45a165a2018-10-19 11:54:47 -0700119void DisplayDevice::disconnect() {
120 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700121}
122
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700123int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000124 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700125}
126
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700127int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000128 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700129}
130
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700131void DisplayDevice::setDisplayName(const std::string& displayName) {
132 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700133 // never override the name with an empty name
134 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700135 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700136 }
137}
138
Vishnu Nairaf6d2972022-11-18 06:26:38 +0000139auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700140 gui::DisplayInfo info;
141 info.displayId = getLayerStack().id;
142
143 // The physical orientation is set when the orientation of the display panel is
144 // different than the default orientation of the device. Other services like
145 // InputFlinger do not know about this, so we do not need to expose the physical
146 // orientation of the panel outside of SurfaceFlinger.
147 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
148 auto width = getWidth();
149 auto height = getHeight();
150 if (inversePhysicalOrientation == ui::ROTATION_90 ||
151 inversePhysicalOrientation == ui::ROTATION_270) {
152 std::swap(width, height);
153 }
154 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
155 inversePhysicalOrientation),
156 width, height);
157 const auto& displayTransform = undoPhysicalOrientation * getTransform();
158 // Send the inverse display transform to input so it can convert display coordinates to
159 // logical display.
160 info.transform = displayTransform.inverse();
161
162 info.logicalWidth = getLayerStackSpaceRect().width();
163 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800164
165 return {.info = info,
166 .transform = displayTransform,
167 .receivesInput = receivesInput(),
Vishnu Nair67b431c2022-11-16 01:54:05 +0000168 .isSecure = isSecure(),
169 .isPrimary = isPrimary(),
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) {
joenchene72ba5e2022-08-24 13:08:58 +0000175 if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
linpeterdcfd4e62022-12-29 09:39:14 +0800176 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchene72ba5e2022-08-24 13:08:58 +0000177 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
178 mBrightness = *mStagedBrightness;
179 }
180 mStagedBrightness = std::nullopt;
181 getCompositionDisplay()->applyDisplayBrightness(true);
182 }
183
Ady Abraham66452722023-03-14 17:41:47 -0700184 if (mPowerMode) {
185 *mPowerMode = mode;
186 } else {
187 mPowerMode.emplace("PowerMode -" + to_string(getId()), mode);
188 }
linpeter1c69cd32022-07-13 19:20:48 +0800189
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500190 getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700191}
192
Ady Abraham66452722023-03-14 17:41:47 -0700193void DisplayDevice::tracePowerMode() {
194 // assign the same value for tracing
195 if (mPowerMode) {
196 const hal::PowerMode powerMode = *mPowerMode;
197 *mPowerMode = powerMode;
198 }
199}
200
Alec Mouri023c1882021-05-08 16:36:33 -0700201void DisplayDevice::enableLayerCaching(bool enable) {
202 getCompositionDisplay()->setLayerCachingEnabled(enable);
203}
204
linpeter1c69cd32022-07-13 19:20:48 +0800205std::optional<hal::PowerMode> DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700206 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700207}
208
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700209bool DisplayDevice::isPoweredOn() const {
linpeter1c69cd32022-07-13 19:20:48 +0800210 return mPowerMode && *mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700211}
212
Ady Abrahamace3d052022-11-17 16:25:05 -0800213void DisplayDevice::setActiveMode(DisplayModeId modeId, Fps displayFps, Fps renderFps) {
214 ATRACE_INT(mActiveModeFPSTrace.c_str(), displayFps.getIntValue());
215 ATRACE_INT(mRenderFrameRateFPSTrace.c_str(), renderFps.getIntValue());
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700216
Ady Abrahamace3d052022-11-17 16:25:05 -0800217 mRefreshRateSelector->setActiveMode(modeId, renderFps);
Dominik Laskowski02bb2072022-08-26 15:02:52 -0700218
Ady Abraham1b11bc62021-06-03 19:51:19 -0700219 if (mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800220 mRefreshRateOverlay->changeRefreshRate(displayFps, renderFps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700221 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700222}
223
Ady Abraham690f4612021-07-01 23:24:03 -0700224status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100225 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700226 hal::VsyncPeriodChangeTimeline* outTimeline) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800227 if (!info.modeOpt || info.modeOpt->modePtr->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100228 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abrahamace3d052022-11-17 16:25:05 -0800229 info.modeOpt ? std::to_string(info.modeOpt->modePtr->getId().value()).c_str()
230 : "null",
Ady Abraham690f4612021-07-01 23:24:03 -0700231 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100232 return BAD_VALUE;
233 }
Ady Abraham690f4612021-07-01 23:24:03 -0700234 mUpcomingActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800235 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.modeOpt->modePtr->getFps().getIntValue());
236 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(),
237 info.modeOpt->modePtr->getHwcId(), constraints,
238 outTimeline);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100239}
240
Marin Shalamanov045b7002021-01-07 16:56:24 +0100241nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
242 const auto physicalId = getPhysicalId();
243 if (!mHwComposer.isConnected(physicalId)) {
244 return 0;
245 }
246
247 nsecs_t vsyncPeriod;
248 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
249 if (status == NO_ERROR) {
250 return vsyncPeriod;
251 }
252
Ady Abrahamace3d052022-11-17 16:25:05 -0800253 return refreshRateSelector().getActiveMode().modePtr->getVsyncPeriod();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100254}
255
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800256ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700257 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800258}
259
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700260void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
261 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700262 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700263 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700264 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700265}
266
Evan Rosky2239b372021-05-20 13:43:47 -0700267void DisplayDevice::setFlags(uint32_t flags) {
268 mFlags = flags;
269}
270
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800271void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200272 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700273 const auto size = ui::Size(width, height);
274 mCompositionDisplay->setDisplaySize(size);
275 if (mRefreshRateOverlay) {
276 mRefreshRateOverlay->setViewport(size);
277 }
Michael Lentine47e45402014-07-18 15:34:25 -0700278}
279
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200280void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
281 Rect orientedDisplaySpaceRect) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700282 mOrientation = orientation;
283
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200284 if (isPrimary()) {
285 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
286 }
287
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200288 // We need to take care of display rotation for globalTransform for case if the panel is not
289 // installed aligned with device orientation.
290 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000291
292 const auto& state = getCompositionDisplay()->getState();
293
294 // If the layer stack and destination frames have never been set, then configure them to be the
295 // same as the physical device, taking into account the total transform.
296 if (!orientedDisplaySpaceRect.isValid()) {
297 ui::Size bounds = state.displaySpace.getBounds();
298 bounds.rotate(transformOrientation);
299 orientedDisplaySpaceRect = Rect(bounds);
300 }
301 if (layerStackSpaceRect.isEmpty()) {
302 ui::Size bounds = state.framebufferSpace.getBounds();
303 bounds.rotate(transformOrientation);
304 layerStackSpaceRect = Rect(bounds);
305 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200306 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
307 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700308}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700309
Alec Mouricdf16792021-12-10 13:16:06 -0800310void DisplayDevice::stageBrightness(float brightness) {
311 mStagedBrightness = brightness;
312}
313
314void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800315 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000316 if (needsComposite) {
317 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
318 }
Alec Mouricdf16792021-12-10 13:16:06 -0800319 mBrightness = *mStagedBrightness;
320 }
321 mStagedBrightness = std::nullopt;
322}
323
324std::optional<float> DisplayDevice::getStagedBrightness() const {
325 return mStagedBrightness;
326}
327
Dominik Laskowski718f9602019-11-09 20:01:35 -0800328ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
329 return sPrimaryDisplayRotationFlags;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700330}
331
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700332void DisplayDevice::dump(utils::Dumper& dumper) const {
333 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100334
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400335 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700336 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700337
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400338 if (mRefreshRateSelector) {
339 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700340 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700341}
Irvelffc9efc2016-07-27 15:16:37 -0700342
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700343bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
344 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700345}
346
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200347DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700348 return mCompositionDisplay->getId();
349}
350
351bool DisplayDevice::isSecure() const {
352 return mCompositionDisplay->isSecure();
353}
354
Angel Aguayob108a6d2021-08-06 21:34:57 +0000355const Rect DisplayDevice::getBounds() const {
356 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700357}
358
359const Region& DisplayDevice::getUndefinedRegion() const {
360 return mCompositionDisplay->getState().undefinedRegion;
361}
362
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800363ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700364 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700365}
366
Garfield Tan54edd912020-10-21 16:31:41 -0700367ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
368 return mCompositionDisplay->getTransformHint();
369}
370
Lloyd Pique32cbe282018-10-19 13:09:22 -0700371const ui::Transform& DisplayDevice::getTransform() const {
372 return mCompositionDisplay->getState().transform;
373}
374
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200375const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000376 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700377}
378
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200379const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000380 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700381}
382
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700383bool DisplayDevice::hasWideColorGamut() const {
384 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
385}
386
387bool DisplayDevice::hasHDR10PlusSupport() const {
388 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
389}
390
391bool DisplayDevice::hasHDR10Support() const {
392 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
393}
394
395bool DisplayDevice::hasHLGSupport() const {
396 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
397}
398
399bool DisplayDevice::hasDolbyVisionSupport() const {
400 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
401}
402
403int DisplayDevice::getSupportedPerFrameMetadata() const {
404 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
405}
406
Kriti Dang49ad4132021-01-08 11:49:56 +0100407void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
408 mOverrideHdrTypes = hdrTypes;
409}
410
411HdrCapabilities DisplayDevice::getHdrCapabilities() const {
412 const HdrCapabilities& capabilities =
413 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
414 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
415 if (!mOverrideHdrTypes.empty()) {
416 hdrTypes = mOverrideHdrTypes;
417 }
418 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
419 capabilities.getDesiredMaxAverageLuminance(),
420 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700421}
422
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800423void DisplayDevice::enableRefreshRateOverlay(bool enable, bool showSpinner, bool showRenderRate,
424 bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700425 if (!enable) {
426 mRefreshRateOverlay.reset();
427 return;
428 }
429
Ady Abraham0aa373a2022-11-22 13:56:50 -0800430 ftl::Flags<RefreshRateOverlay::Features> features;
431 if (showSpinner) {
432 features |= RefreshRateOverlay::Features::Spinner;
433 }
434
435 if (showRenderRate) {
436 features |= RefreshRateOverlay::Features::RenderRate;
437 }
438
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800439 if (showInMiddle) {
440 features |= RefreshRateOverlay::Features::ShowInMiddle;
441 }
442
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400443 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abraham0aa373a2022-11-22 13:56:50 -0800444 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(fpsRange, features);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700445 mRefreshRateOverlay->setLayerStack(getLayerStack());
446 mRefreshRateOverlay->setViewport(getSize());
Ady Abraham0aa373a2022-11-22 13:56:50 -0800447 mRefreshRateOverlay->changeRefreshRate(getActiveMode().modePtr->getFps(), getActiveMode().fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700448}
449
450bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
451 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400452 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800453 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400454 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800455 if (newMode) {
456 mRefreshRateOverlay->changeRefreshRate(newMode->modePtr->getFps(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700457 return true;
458 }
459 }
460
461 return false;
462}
463
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700464void DisplayDevice::animateRefreshRateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700465 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700466 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700467 }
468}
469
Dominik Laskowskifc378b02022-12-02 14:56:05 -0500470auto DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info, bool force)
471 -> DesiredActiveModeAction {
Ady Abraham690f4612021-07-01 23:24:03 -0700472 ATRACE_CALL();
473
Ady Abrahamace3d052022-11-17 16:25:05 -0800474 LOG_ALWAYS_FATAL_IF(!info.modeOpt, "desired mode not provided");
475 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.modeOpt->modePtr->getPhysicalDisplayId(),
476 "DisplayId mismatch");
Ady Abraham690f4612021-07-01 23:24:03 -0700477
Ady Abrahamace3d052022-11-17 16:25:05 -0800478 ALOGV("%s(%s)", __func__, to_string(*info.modeOpt->modePtr).c_str());
Ady Abraham690f4612021-07-01 23:24:03 -0700479
480 std::scoped_lock lock(mActiveModeLock);
481 if (mDesiredActiveModeChanged) {
482 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700483 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700484 mDesiredActiveMode = info;
485 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
Ady Abrahamace3d052022-11-17 16:25:05 -0800486 return DesiredActiveModeAction::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700487 }
488
Ady Abrahamace3d052022-11-17 16:25:05 -0800489 const auto& desiredMode = *info.modeOpt->modePtr;
490
Ady Abraham690f4612021-07-01 23:24:03 -0700491 // Check if we are already at the desired mode
Ady Abraham95a37e22023-01-18 22:57:28 -0800492 const auto currentMode = refreshRateSelector().getActiveMode();
493 if (!force && currentMode.modePtr->getId() == desiredMode.getId()) {
494 if (currentMode == info.modeOpt) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800495 return DesiredActiveModeAction::None;
496 }
497
498 setActiveMode(desiredMode.getId(), desiredMode.getFps(), info.modeOpt->fps);
499 return DesiredActiveModeAction::InitiateRenderRateSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700500 }
501
Ady Abraham95a37e22023-01-18 22:57:28 -0800502 // Set the render frame rate to the current physical refresh rate to schedule the next
503 // frame as soon as possible.
504 setActiveMode(currentMode.modePtr->getId(), currentMode.modePtr->getFps(),
505 currentMode.modePtr->getFps());
506
Ady Abraham690f4612021-07-01 23:24:03 -0700507 // Initiate a mode change.
508 mDesiredActiveModeChanged = true;
509 mDesiredActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800510 return DesiredActiveModeAction::InitiateDisplayModeSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700511}
512
513std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
514 std::scoped_lock lock(mActiveModeLock);
515 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
516 return std::nullopt;
517}
518
519void DisplayDevice::clearDesiredActiveModeState() {
520 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700521 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700522 mDesiredActiveModeChanged = false;
523}
524
Huihong Luo1768cb02022-10-11 11:10:34 -0700525void DisplayDevice::adjustRefreshRate(Fps leaderDisplayRefreshRate) {
526 using fps_approx_ops::operator==;
527 if (mRequestedRefreshRate == 0_Hz) {
528 return;
529 }
530
531 using fps_approx_ops::operator>;
532 if (mRequestedRefreshRate > leaderDisplayRefreshRate) {
533 mAdjustedRefreshRate = leaderDisplayRefreshRate;
534 return;
535 }
536
537 unsigned divisor = static_cast<unsigned>(
538 std::round(leaderDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
539 mAdjustedRefreshRate = leaderDisplayRefreshRate / divisor;
540}
541
Dominik Laskowski663bd282018-04-19 15:26:54 -0700542std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700543
544} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800545
546// TODO(b/129481165): remove the #pragma below and fix conversion issues
547#pragma clang diagnostic pop // ignored "-Wconversion"