blob: 1391aa7c7eac2c6e02b22a2de4e2f4ae12ff4de7 [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),
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040074 mRefreshRateSelector(std::move(args.refreshRateSelector)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070075 mCompositionDisplay->editState().isSecure = args.isSecure;
Lloyd Pique31cb2942018-10-19 17:23:03 -070076 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070077 compositionengine::RenderSurfaceCreationArgsBuilder()
78 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
79 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
80 .setNativeWindow(std::move(args.nativeWindow))
81 .setDisplaySurface(std::move(args.displaySurface))
82 .setMaxTextureCacheSize(
83 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
84 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070085
Vishnu Nair9b079a22020-01-21 14:36:08 -080086 if (!mFlinger->mDisableClientCompositionCache &&
87 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
88 mCompositionDisplay->createClientCompositionCache(
89 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
90 }
91
Vishnu Naira3140382022-02-24 14:07:11 -080092 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
Alec Mouridda07d92022-04-25 22:39:25 +000093 mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070094 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010095 compositionengine::DisplayColorProfileCreationArgsBuilder()
96 .setHasWideColorGamut(args.hasWideColorGamut)
97 .setHdrCapabilities(std::move(args.hdrCapabilities))
98 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
99 .setHwcColorModes(std::move(args.hwcColorModes))
100 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700101
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102 if (!mCompositionDisplay->isValid()) {
103 ALOGE("Composition Display did not validate!");
104 }
105
Lloyd Pique31cb2942018-10-19 17:23:03 -0700106 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700107
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500108 if (const auto powerModeOpt = args.initialPowerMode) {
109 setPowerMode(*powerModeOpt);
110 }
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(),
169 .rotationFlags = ui::Transform::toRotationFlags(mOrientation)};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700170}
171
Peiyong Lin65248e02020-04-18 21:15:07 -0700172void DisplayDevice::setPowerMode(hal::PowerMode mode) {
joenchene72ba5e2022-08-24 13:08:58 +0000173 if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
linpeterdcfd4e62022-12-29 09:39:14 +0800174 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchene72ba5e2022-08-24 13:08:58 +0000175 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
176 mBrightness = *mStagedBrightness;
177 }
178 mStagedBrightness = std::nullopt;
179 getCompositionDisplay()->applyDisplayBrightness(true);
180 }
181
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700182 mPowerMode = mode;
linpeter1c69cd32022-07-13 19:20:48 +0800183
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500184 getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700185}
186
Alec Mouri023c1882021-05-08 16:36:33 -0700187void DisplayDevice::enableLayerCaching(bool enable) {
188 getCompositionDisplay()->setLayerCachingEnabled(enable);
189}
190
linpeter1c69cd32022-07-13 19:20:48 +0800191std::optional<hal::PowerMode> DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700192 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700193}
194
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700195bool DisplayDevice::isPoweredOn() const {
linpeter1c69cd32022-07-13 19:20:48 +0800196 return mPowerMode && *mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700197}
198
Ady Abrahamace3d052022-11-17 16:25:05 -0800199void DisplayDevice::setActiveMode(DisplayModeId modeId, Fps displayFps, Fps renderFps) {
200 ATRACE_INT(mActiveModeFPSTrace.c_str(), displayFps.getIntValue());
201 ATRACE_INT(mRenderFrameRateFPSTrace.c_str(), renderFps.getIntValue());
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700202
Ady Abrahamace3d052022-11-17 16:25:05 -0800203 mRefreshRateSelector->setActiveMode(modeId, renderFps);
Dominik Laskowski02bb2072022-08-26 15:02:52 -0700204
Ady Abraham1b11bc62021-06-03 19:51:19 -0700205 if (mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800206 mRefreshRateOverlay->changeRefreshRate(displayFps, renderFps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700207 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700208}
209
Ady Abraham690f4612021-07-01 23:24:03 -0700210status_t DisplayDevice::initiateModeChange(const ActiveModeInfo& info,
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100211 const hal::VsyncPeriodChangeConstraints& constraints,
Ady Abraham690f4612021-07-01 23:24:03 -0700212 hal::VsyncPeriodChangeTimeline* outTimeline) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800213 if (!info.modeOpt || info.modeOpt->modePtr->getPhysicalDisplayId() != getPhysicalId()) {
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100214 ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
Ady Abrahamace3d052022-11-17 16:25:05 -0800215 info.modeOpt ? std::to_string(info.modeOpt->modePtr->getId().value()).c_str()
216 : "null",
Ady Abraham690f4612021-07-01 23:24:03 -0700217 to_string(getId()).c_str());
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100218 return BAD_VALUE;
219 }
Ady Abraham690f4612021-07-01 23:24:03 -0700220 mUpcomingActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800221 ATRACE_INT(mActiveModeFPSHwcTrace.c_str(), info.modeOpt->modePtr->getFps().getIntValue());
222 return mHwComposer.setActiveModeWithConstraints(getPhysicalId(),
223 info.modeOpt->modePtr->getHwcId(), constraints,
224 outTimeline);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100225}
226
Marin Shalamanov045b7002021-01-07 16:56:24 +0100227nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
228 const auto physicalId = getPhysicalId();
229 if (!mHwComposer.isConnected(physicalId)) {
230 return 0;
231 }
232
233 nsecs_t vsyncPeriod;
234 const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
235 if (status == NO_ERROR) {
236 return vsyncPeriod;
237 }
238
Ady Abrahamace3d052022-11-17 16:25:05 -0800239 return refreshRateSelector().getActiveMode().modePtr->getVsyncPeriod();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100240}
241
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800242ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700243 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800244}
245
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700246void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
247 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700248 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700249 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700250 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700251}
252
Evan Rosky2239b372021-05-20 13:43:47 -0700253void DisplayDevice::setFlags(uint32_t flags) {
254 mFlags = flags;
255}
256
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800257void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200258 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700259 const auto size = ui::Size(width, height);
260 mCompositionDisplay->setDisplaySize(size);
261 if (mRefreshRateOverlay) {
262 mRefreshRateOverlay->setViewport(size);
263 }
Michael Lentine47e45402014-07-18 15:34:25 -0700264}
265
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200266void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
267 Rect orientedDisplaySpaceRect) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700268 mOrientation = orientation;
269
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200270 if (isPrimary()) {
271 sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
272 }
273
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200274 // We need to take care of display rotation for globalTransform for case if the panel is not
275 // installed aligned with device orientation.
276 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000277
278 const auto& state = getCompositionDisplay()->getState();
279
280 // If the layer stack and destination frames have never been set, then configure them to be the
281 // same as the physical device, taking into account the total transform.
282 if (!orientedDisplaySpaceRect.isValid()) {
283 ui::Size bounds = state.displaySpace.getBounds();
284 bounds.rotate(transformOrientation);
285 orientedDisplaySpaceRect = Rect(bounds);
286 }
287 if (layerStackSpaceRect.isEmpty()) {
288 ui::Size bounds = state.framebufferSpace.getBounds();
289 bounds.rotate(transformOrientation);
290 layerStackSpaceRect = Rect(bounds);
291 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200292 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
293 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700294}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700295
Alec Mouricdf16792021-12-10 13:16:06 -0800296void DisplayDevice::stageBrightness(float brightness) {
297 mStagedBrightness = brightness;
298}
299
300void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800301 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000302 if (needsComposite) {
303 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
304 }
Alec Mouricdf16792021-12-10 13:16:06 -0800305 mBrightness = *mStagedBrightness;
306 }
307 mStagedBrightness = std::nullopt;
308}
309
310std::optional<float> DisplayDevice::getStagedBrightness() const {
311 return mStagedBrightness;
312}
313
Dominik Laskowski718f9602019-11-09 20:01:35 -0800314ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
315 return sPrimaryDisplayRotationFlags;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700316}
317
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700318void DisplayDevice::dump(utils::Dumper& dumper) const {
319 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100320
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400321 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700322 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700323
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400324 if (mRefreshRateSelector) {
325 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700326 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700327}
Irvelffc9efc2016-07-27 15:16:37 -0700328
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700329bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
330 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700331}
332
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200333DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700334 return mCompositionDisplay->getId();
335}
336
337bool DisplayDevice::isSecure() const {
338 return mCompositionDisplay->isSecure();
339}
340
Angel Aguayob108a6d2021-08-06 21:34:57 +0000341const Rect DisplayDevice::getBounds() const {
342 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700343}
344
345const Region& DisplayDevice::getUndefinedRegion() const {
346 return mCompositionDisplay->getState().undefinedRegion;
347}
348
349bool DisplayDevice::needsFiltering() const {
350 return mCompositionDisplay->getState().needsFiltering;
351}
352
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800353ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700354 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700355}
356
Garfield Tan54edd912020-10-21 16:31:41 -0700357ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
358 return mCompositionDisplay->getTransformHint();
359}
360
Lloyd Pique32cbe282018-10-19 13:09:22 -0700361const ui::Transform& DisplayDevice::getTransform() const {
362 return mCompositionDisplay->getState().transform;
363}
364
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200365const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000366 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700367}
368
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200369const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000370 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700371}
372
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700373bool DisplayDevice::hasWideColorGamut() const {
374 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
375}
376
377bool DisplayDevice::hasHDR10PlusSupport() const {
378 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
379}
380
381bool DisplayDevice::hasHDR10Support() const {
382 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
383}
384
385bool DisplayDevice::hasHLGSupport() const {
386 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
387}
388
389bool DisplayDevice::hasDolbyVisionSupport() const {
390 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
391}
392
393int DisplayDevice::getSupportedPerFrameMetadata() const {
394 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
395}
396
Kriti Dang49ad4132021-01-08 11:49:56 +0100397void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
398 mOverrideHdrTypes = hdrTypes;
399}
400
401HdrCapabilities DisplayDevice::getHdrCapabilities() const {
402 const HdrCapabilities& capabilities =
403 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
404 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
405 if (!mOverrideHdrTypes.empty()) {
406 hdrTypes = mOverrideHdrTypes;
407 }
408 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
409 capabilities.getDesiredMaxAverageLuminance(),
410 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700411}
412
Ady Abraham0aa373a2022-11-22 13:56:50 -0800413void DisplayDevice::enableRefreshRateOverlay(bool enable, bool showSpinner, bool showRenderRate) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700414 if (!enable) {
415 mRefreshRateOverlay.reset();
416 return;
417 }
418
Ady Abraham0aa373a2022-11-22 13:56:50 -0800419 ftl::Flags<RefreshRateOverlay::Features> features;
420 if (showSpinner) {
421 features |= RefreshRateOverlay::Features::Spinner;
422 }
423
424 if (showRenderRate) {
425 features |= RefreshRateOverlay::Features::RenderRate;
426 }
427
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400428 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abraham0aa373a2022-11-22 13:56:50 -0800429 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(fpsRange, features);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700430 mRefreshRateOverlay->setLayerStack(getLayerStack());
431 mRefreshRateOverlay->setViewport(getSize());
Ady Abraham0aa373a2022-11-22 13:56:50 -0800432 mRefreshRateOverlay->changeRefreshRate(getActiveMode().modePtr->getFps(), getActiveMode().fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700433}
434
435bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
436 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400437 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800438 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400439 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800440 if (newMode) {
441 mRefreshRateOverlay->changeRefreshRate(newMode->modePtr->getFps(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700442 return true;
443 }
444 }
445
446 return false;
447}
448
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700449void DisplayDevice::animateRefreshRateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700450 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700451 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700452 }
453}
454
Dominik Laskowskifc378b02022-12-02 14:56:05 -0500455auto DisplayDevice::setDesiredActiveMode(const ActiveModeInfo& info, bool force)
456 -> DesiredActiveModeAction {
Ady Abraham690f4612021-07-01 23:24:03 -0700457 ATRACE_CALL();
458
Ady Abrahamace3d052022-11-17 16:25:05 -0800459 LOG_ALWAYS_FATAL_IF(!info.modeOpt, "desired mode not provided");
460 LOG_ALWAYS_FATAL_IF(getPhysicalId() != info.modeOpt->modePtr->getPhysicalDisplayId(),
461 "DisplayId mismatch");
Ady Abraham690f4612021-07-01 23:24:03 -0700462
Ady Abrahamace3d052022-11-17 16:25:05 -0800463 ALOGV("%s(%s)", __func__, to_string(*info.modeOpt->modePtr).c_str());
Ady Abraham690f4612021-07-01 23:24:03 -0700464
465 std::scoped_lock lock(mActiveModeLock);
466 if (mDesiredActiveModeChanged) {
467 // If a mode change is pending, just cache the latest request in mDesiredActiveMode
Dominik Laskowski068173d2021-08-11 17:22:59 -0700468 const auto prevConfig = mDesiredActiveMode.event;
Ady Abraham690f4612021-07-01 23:24:03 -0700469 mDesiredActiveMode = info;
470 mDesiredActiveMode.event = mDesiredActiveMode.event | prevConfig;
Ady Abrahamace3d052022-11-17 16:25:05 -0800471 return DesiredActiveModeAction::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700472 }
473
Ady Abrahamace3d052022-11-17 16:25:05 -0800474 const auto& desiredMode = *info.modeOpt->modePtr;
475
Ady Abraham690f4612021-07-01 23:24:03 -0700476 // Check if we are already at the desired mode
Dominik Laskowskifc378b02022-12-02 14:56:05 -0500477 if (!force && refreshRateSelector().getActiveMode().modePtr->getId() == desiredMode.getId()) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800478 if (refreshRateSelector().getActiveMode() == info.modeOpt) {
479 return DesiredActiveModeAction::None;
480 }
481
482 setActiveMode(desiredMode.getId(), desiredMode.getFps(), info.modeOpt->fps);
483 return DesiredActiveModeAction::InitiateRenderRateSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700484 }
485
486 // Initiate a mode change.
487 mDesiredActiveModeChanged = true;
488 mDesiredActiveMode = info;
Ady Abrahamace3d052022-11-17 16:25:05 -0800489 return DesiredActiveModeAction::InitiateDisplayModeSwitch;
Ady Abraham690f4612021-07-01 23:24:03 -0700490}
491
492std::optional<DisplayDevice::ActiveModeInfo> DisplayDevice::getDesiredActiveMode() const {
493 std::scoped_lock lock(mActiveModeLock);
494 if (mDesiredActiveModeChanged) return mDesiredActiveMode;
495 return std::nullopt;
496}
497
498void DisplayDevice::clearDesiredActiveModeState() {
499 std::scoped_lock lock(mActiveModeLock);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700500 mDesiredActiveMode.event = scheduler::DisplayModeEvent::None;
Ady Abraham690f4612021-07-01 23:24:03 -0700501 mDesiredActiveModeChanged = false;
502}
503
Dominik Laskowski663bd282018-04-19 15:26:54 -0700504std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700505
506} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800507
508// TODO(b/129481165): remove the #pragma below and fix conversion issues
509#pragma clang diagnostic pop // ignored "-Wconversion"