blob: 8288b99864bfb505534b3586773e90fdcadc5231 [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
Vishnu Nairbe0ad902024-06-27 23:38:43 +000027#include <common/trace.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>
Dominik Laskowski5c989f52024-04-11 13:57:14 -040039#include <ftl/concat.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070040#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080041#include <system/window.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070043#include "DisplayDevice.h"
Vishnu Nairaf6d2972022-11-18 06:26:38 +000044#include "FrontEnd/DisplayInfo.h"
Sally Qi147581b2023-06-27 11:55:34 -070045#include "HdrSdrRatioOverlay.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080046#include "Layer.h"
Ady Abraham1b11bc62021-06-03 19:51:19 -070047#include "RefreshRateOverlay.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070048#include "SurfaceFlinger.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049
Peiyong Linfd997e02018-03-28 15:29:00 -070050namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Peiyong Lin65248e02020-04-18 21:15:07 -070052namespace hal = hardware::graphics::composer::hal;
53
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070054DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010055 const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070056 std::shared_ptr<compositionengine::Display> compositionDisplay)
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010057 : flinger(flinger),
58 hwComposer(hwComposer),
59 displayToken(displayToken),
60 compositionDisplay(compositionDisplay) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070061
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070062DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070063 : mFlinger(args.flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010064 mHwComposer(args.hwComposer),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070065 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080066 mSequenceId(args.sequenceId),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070067 mCompositionDisplay{args.compositionDisplay},
Dominik Laskowski718f9602019-11-09 20:01:35 -080068 mPhysicalOrientation(args.physicalOrientation),
Dominik Laskowskia42d5392022-09-29 14:26:53 -040069 mPowerMode(ftl::Concat("PowerMode ", getId().value).c_str(), args.initialPowerMode),
Ady Abraham3efa3942021-06-24 19:01:25 -070070 mIsPrimary(args.isPrimary),
Huihong Luo1768cb02022-10-11 11:10:34 -070071 mRequestedRefreshRate(args.requestedRefreshRate),
Dominik Laskowski5c989f52024-04-11 13:57:14 -040072 mRefreshRateSelector(std::move(args.refreshRateSelector)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070073 mCompositionDisplay->editState().isSecure = args.isSecure;
Chavi Weingarten18fa7c62023-11-28 21:16:03 +000074 mCompositionDisplay->editState().isProtected = args.isProtected;
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 Laskowskia42d5392022-09-29 14:26:53 -0400107 setPowerMode(args.initialPowerMode);
Alec Mouriba013fa2018-10-16 12:43:11 -0700108
Jesse Hallffe1f192013-03-22 15:13:48 -0700109 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800110 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111}
112
Lloyd Pique09594832018-01-22 17:48:03 -0800113DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700114
Lloyd Pique45a165a2018-10-19 11:54:47 -0700115void DisplayDevice::disconnect() {
116 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700117}
118
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700119int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000120 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700121}
122
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700123int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000124 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700125}
126
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700127void DisplayDevice::setDisplayName(const std::string& displayName) {
128 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700129 // never override the name with an empty name
130 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700131 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700132 }
133}
134
Vishnu Nairaf6d2972022-11-18 06:26:38 +0000135auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700136 gui::DisplayInfo info;
Linnan Li13bf76a2024-05-05 19:18:02 +0800137 info.displayId = ui::LogicalDisplayId{static_cast<int32_t>(getLayerStack().id)};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700138
139 // The physical orientation is set when the orientation of the display panel is
140 // different than the default orientation of the device. Other services like
141 // InputFlinger do not know about this, so we do not need to expose the physical
142 // orientation of the panel outside of SurfaceFlinger.
143 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
144 auto width = getWidth();
145 auto height = getHeight();
146 if (inversePhysicalOrientation == ui::ROTATION_90 ||
147 inversePhysicalOrientation == ui::ROTATION_270) {
148 std::swap(width, height);
149 }
150 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
151 inversePhysicalOrientation),
152 width, height);
153 const auto& displayTransform = undoPhysicalOrientation * getTransform();
154 // Send the inverse display transform to input so it can convert display coordinates to
155 // logical display.
156 info.transform = displayTransform.inverse();
157
158 info.logicalWidth = getLayerStackSpaceRect().width();
159 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800160
161 return {.info = info,
162 .transform = displayTransform,
163 .receivesInput = receivesInput(),
Vishnu Nair67b431c2022-11-16 01:54:05 +0000164 .isSecure = isSecure(),
165 .isPrimary = isPrimary(),
Vishnu Nair81750622023-03-08 15:02:06 -0800166 .isVirtual = isVirtual(),
Vishnu Naircfb2d252023-01-19 04:44:02 +0000167 .rotationFlags = ui::Transform::toRotationFlags(mOrientation),
168 .transformHint = getTransformHint()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700169}
170
Peiyong Lin65248e02020-04-18 21:15:07 -0700171void DisplayDevice::setPowerMode(hal::PowerMode mode) {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400172 // TODO(b/241285876): Skip this for virtual displays.
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
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400182 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
Ady Abraham66452722023-03-14 17:41:47 -0700187void DisplayDevice::tracePowerMode() {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400188 // Assign the same value for tracing.
189 mPowerMode = mPowerMode.get();
Ady Abraham66452722023-03-14 17:41:47 -0700190}
191
Alec Mouri023c1882021-05-08 16:36:33 -0700192void DisplayDevice::enableLayerCaching(bool enable) {
193 getCompositionDisplay()->setLayerCachingEnabled(enable);
194}
195
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400196hal::PowerMode DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700197 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700198}
199
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700200bool DisplayDevice::isPoweredOn() const {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400201 return mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700202}
203
Marin Shalamanov045b7002021-01-07 16:56:24 +0100204nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
205 const auto physicalId = getPhysicalId();
206 if (!mHwComposer.isConnected(physicalId)) {
207 return 0;
208 }
209
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500210 if (const auto vsyncPeriodOpt = mHwComposer.getDisplayVsyncPeriod(physicalId).value_opt()) {
211 return *vsyncPeriodOpt;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100212 }
213
ramindania04b8a52023-08-07 18:49:47 -0700214 return refreshRateSelector().getActiveMode().modePtr->getVsyncRate().getPeriodNsecs();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100215}
216
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800217ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700218 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800219}
220
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700221void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
222 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700223 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700224 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700225 }
Sally Qi147581b2023-06-27 11:55:34 -0700226 if (mHdrSdrRatioOverlay) {
227 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
228 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700229}
230
Evan Rosky2239b372021-05-20 13:43:47 -0700231void DisplayDevice::setFlags(uint32_t flags) {
232 mFlags = flags;
233}
234
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800235void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200236 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700237 const auto size = ui::Size(width, height);
238 mCompositionDisplay->setDisplaySize(size);
239 if (mRefreshRateOverlay) {
240 mRefreshRateOverlay->setViewport(size);
241 }
Sally Qi147581b2023-06-27 11:55:34 -0700242 if (mHdrSdrRatioOverlay) {
243 mHdrSdrRatioOverlay->setViewport(size);
244 }
Michael Lentine47e45402014-07-18 15:34:25 -0700245}
246
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200247void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
248 Rect orientedDisplaySpaceRect) {
Sally Qi147581b2023-06-27 11:55:34 -0700249 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700250 mOrientation = orientation;
251
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200252 // We need to take care of display rotation for globalTransform for case if the panel is not
253 // installed aligned with device orientation.
254 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000255
256 const auto& state = getCompositionDisplay()->getState();
257
258 // If the layer stack and destination frames have never been set, then configure them to be the
259 // same as the physical device, taking into account the total transform.
260 if (!orientedDisplaySpaceRect.isValid()) {
261 ui::Size bounds = state.displaySpace.getBounds();
262 bounds.rotate(transformOrientation);
263 orientedDisplaySpaceRect = Rect(bounds);
264 }
265 if (layerStackSpaceRect.isEmpty()) {
266 ui::Size bounds = state.framebufferSpace.getBounds();
267 bounds.rotate(transformOrientation);
268 layerStackSpaceRect = Rect(bounds);
269 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200270 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
271 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700272}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700273
Alec Mouricdf16792021-12-10 13:16:06 -0800274void DisplayDevice::stageBrightness(float brightness) {
275 mStagedBrightness = brightness;
276}
277
278void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800279 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000280 if (needsComposite) {
281 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
282 }
Alec Mouricdf16792021-12-10 13:16:06 -0800283 mBrightness = *mStagedBrightness;
284 }
285 mStagedBrightness = std::nullopt;
286}
287
288std::optional<float> DisplayDevice::getStagedBrightness() const {
289 return mStagedBrightness;
290}
291
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700292void DisplayDevice::dump(utils::Dumper& dumper) const {
293 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100294
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400295 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700296 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700297
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400298 if (mRefreshRateSelector) {
299 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700300 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700301}
Irvelffc9efc2016-07-27 15:16:37 -0700302
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700303bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
304 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700305}
306
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200307DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700308 return mCompositionDisplay->getId();
309}
310
311bool DisplayDevice::isSecure() const {
312 return mCompositionDisplay->isSecure();
313}
314
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700315void DisplayDevice::setSecure(bool secure) {
316 mCompositionDisplay->setSecure(secure);
317}
318
Angel Aguayob108a6d2021-08-06 21:34:57 +0000319const Rect DisplayDevice::getBounds() const {
320 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700321}
322
323const Region& DisplayDevice::getUndefinedRegion() const {
324 return mCompositionDisplay->getState().undefinedRegion;
325}
326
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800327ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700328 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700329}
330
Garfield Tan54edd912020-10-21 16:31:41 -0700331ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
332 return mCompositionDisplay->getTransformHint();
333}
334
Lloyd Pique32cbe282018-10-19 13:09:22 -0700335const ui::Transform& DisplayDevice::getTransform() const {
336 return mCompositionDisplay->getState().transform;
337}
338
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200339const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000340 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700341}
342
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200343const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000344 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700345}
346
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700347bool DisplayDevice::hasWideColorGamut() const {
348 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
349}
350
351bool DisplayDevice::hasHDR10PlusSupport() const {
352 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
353}
354
355bool DisplayDevice::hasHDR10Support() const {
356 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
357}
358
359bool DisplayDevice::hasHLGSupport() const {
360 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
361}
362
363bool DisplayDevice::hasDolbyVisionSupport() const {
364 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
365}
366
367int DisplayDevice::getSupportedPerFrameMetadata() const {
368 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
369}
370
Kriti Dang49ad4132021-01-08 11:49:56 +0100371void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
372 mOverrideHdrTypes = hdrTypes;
373}
374
375HdrCapabilities DisplayDevice::getHdrCapabilities() const {
376 const HdrCapabilities& capabilities =
377 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
378 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
379 if (!mOverrideHdrTypes.empty()) {
380 hdrTypes = mOverrideHdrTypes;
381 }
382 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
383 capabilities.getDesiredMaxAverageLuminance(),
384 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700385}
386
Sally Qi147581b2023-06-27 11:55:34 -0700387void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
388 if (!enable) {
389 mHdrSdrRatioOverlay.reset();
390 return;
391 }
392
Ady Abrahamddba9342023-10-02 16:28:03 -0700393 mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
394 if (mHdrSdrRatioOverlay) {
395 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
396 mHdrSdrRatioOverlay->setViewport(getSize());
397 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
398 }
Sally Qi147581b2023-06-27 11:55:34 -0700399}
400
401void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000402 SFTRACE_CALL();
Sally Qi147581b2023-06-27 11:55:34 -0700403 mHdrSdrRatio = currentHdrSdrRatio;
404 if (mHdrSdrRatioOverlay) {
405 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
406 }
407}
408
Dominik Laskowski5c989f52024-04-11 13:57:14 -0400409void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, Fps refreshRate,
410 Fps renderFps, bool showSpinner, bool showRenderRate,
411 bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700412 if (!enable) {
413 mRefreshRateOverlay.reset();
414 return;
415 }
416
Ady Abraham0aa373a2022-11-22 13:56:50 -0800417 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000418 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800419 features |= RefreshRateOverlay::Features::Spinner;
420 }
421
Lloyd Pique30db6402023-06-26 18:56:51 +0000422 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800423 features |= RefreshRateOverlay::Features::RenderRate;
424 }
425
Lloyd Pique30db6402023-06-26 18:56:51 +0000426 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800427 features |= RefreshRateOverlay::Features::ShowInMiddle;
428 }
429
ramindanib2158ee2023-02-13 20:29:59 -0800430 if (setByHwc) {
431 features |= RefreshRateOverlay::Features::SetByHwc;
432 }
433
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400434 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abrahamddba9342023-10-02 16:28:03 -0700435 mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
436 if (mRefreshRateOverlay) {
437 mRefreshRateOverlay->setLayerStack(getLayerStack());
438 mRefreshRateOverlay->setViewport(getSize());
Dominik Laskowski5c989f52024-04-11 13:57:14 -0400439 updateRefreshRateOverlayRate(refreshRate, renderFps, setByHwc);
Ady Abrahamddba9342023-10-02 16:28:03 -0700440 }
ramindanib2158ee2023-02-13 20:29:59 -0800441}
442
Ying Wei22d59cc2024-05-11 02:41:08 +0000443void DisplayDevice::updateRefreshRateOverlayRate(Fps refreshRate, Fps renderFps, bool setByHwc) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000444 SFTRACE_CALL();
ramindani3faf5742023-09-21 13:45:12 -0700445 if (mRefreshRateOverlay) {
446 if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
ramindanic6e522e2024-05-07 16:42:51 -0700447 if (mRefreshRateSelector->isVrrDevice() && !mRefreshRateOverlay->isSetByHwc()) {
448 refreshRate = renderFps;
449 }
Ying Wei22d59cc2024-05-11 02:41:08 +0000450 mRefreshRateOverlay->changeRefreshRate(refreshRate, renderFps);
ramindani3faf5742023-09-21 13:45:12 -0700451 } else {
452 mRefreshRateOverlay->changeRenderRate(renderFps);
453 }
ramindanib2158ee2023-02-13 20:29:59 -0800454 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700455}
456
457bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
458 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400459 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800460 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400461 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800462 if (newMode) {
ramindania04b8a52023-08-07 18:49:47 -0700463 updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700464 return true;
465 }
466 }
467
468 return false;
469}
470
Sally Qi147581b2023-06-27 11:55:34 -0700471void DisplayDevice::animateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700472 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700473 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700474 }
Sally Qi147581b2023-06-27 11:55:34 -0700475 if (mHdrSdrRatioOverlay) {
476 // hdr sdr ratio is designed to be on the top right of the screen,
477 // therefore, we need to re-calculate the display's width and height
478 if (mIsOrientationChanged) {
479 auto width = getWidth();
480 auto height = getHeight();
481 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
482 std::swap(width, height);
483 }
484 mHdrSdrRatioOverlay->setViewport({width, height});
485 }
486 mHdrSdrRatioOverlay->animate();
487 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700488}
489
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500490void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000491 using fps_approx_ops::operator<=;
492 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700493 return;
494 }
495
496 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500497 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
498 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700499 return;
500 }
501
502 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000503 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
504 if (divisor == 0) {
505 mAdjustedRefreshRate = 0_Hz;
506 return;
507 }
508
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500509 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700510}
511
Dominik Laskowski663bd282018-04-19 15:26:54 -0700512std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700513
514} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800515
516// TODO(b/129481165): remove the #pragma below and fix conversion issues
517#pragma clang diagnostic pop // ignored "-Wconversion"