blob: 27ea4a9865d2d7c6a26b66ebeffcfa3679c1658b [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>
Dominik Laskowski5c989f52024-04-11 13:57:14 -040038#include <ftl/concat.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070039#include <log/log.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080040#include <system/window.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070042#include "DisplayDevice.h"
Vishnu Nairaf6d2972022-11-18 06:26:38 +000043#include "FrontEnd/DisplayInfo.h"
Sally Qi147581b2023-06-27 11:55:34 -070044#include "HdrSdrRatioOverlay.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080045#include "Layer.h"
Ady Abraham1b11bc62021-06-03 19:51:19 -070046#include "RefreshRateOverlay.h"
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070047#include "SurfaceFlinger.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048
Peiyong Linfd997e02018-03-28 15:29:00 -070049namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Peiyong Lin65248e02020-04-18 21:15:07 -070051namespace hal = hardware::graphics::composer::hal;
52
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070053DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010054 const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070055 std::shared_ptr<compositionengine::Display> compositionDisplay)
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010056 : flinger(flinger),
57 hwComposer(hwComposer),
58 displayToken(displayToken),
59 compositionDisplay(compositionDisplay) {}
Chia-I Wube02ec02018-05-18 10:59:36 -070060
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070061DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070062 : mFlinger(args.flinger),
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010063 mHwComposer(args.hwComposer),
Lloyd Pique2eef1d22018-09-18 21:30:04 -070064 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -080065 mSequenceId(args.sequenceId),
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070066 mCompositionDisplay{args.compositionDisplay},
Dominik Laskowski718f9602019-11-09 20:01:35 -080067 mPhysicalOrientation(args.physicalOrientation),
Dominik Laskowskia42d5392022-09-29 14:26:53 -040068 mPowerMode(ftl::Concat("PowerMode ", getId().value).c_str(), args.initialPowerMode),
Ady Abraham3efa3942021-06-24 19:01:25 -070069 mIsPrimary(args.isPrimary),
Huihong Luo1768cb02022-10-11 11:10:34 -070070 mRequestedRefreshRate(args.requestedRefreshRate),
Dominik Laskowski5c989f52024-04-11 13:57:14 -040071 mRefreshRateSelector(std::move(args.refreshRateSelector)) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070072 mCompositionDisplay->editState().isSecure = args.isSecure;
Chavi Weingarten18fa7c62023-11-28 21:16:03 +000073 mCompositionDisplay->editState().isProtected = args.isProtected;
Lloyd Pique31cb2942018-10-19 17:23:03 -070074 mCompositionDisplay->createRenderSurface(
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070075 compositionengine::RenderSurfaceCreationArgsBuilder()
76 .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
77 .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
78 .setNativeWindow(std::move(args.nativeWindow))
79 .setDisplaySurface(std::move(args.displaySurface))
Lloyd Pique30db6402023-06-26 18:56:51 +000080 .setMaxTextureCacheSize(
81 static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
Dominik Laskowskib9ac3e12021-04-23 13:01:16 -070082 .build());
Lloyd Pique31cb2942018-10-19 17:23:03 -070083
Lloyd Pique30db6402023-06-26 18:56:51 +000084 if (!mFlinger->mDisableClientCompositionCache &&
85 SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
Vishnu Nair9b079a22020-01-21 14:36:08 -080086 mCompositionDisplay->createClientCompositionCache(
Lloyd Pique30db6402023-06-26 18:56:51 +000087 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
Vishnu Nair9b079a22020-01-21 14:36:08 -080088 }
89
Lloyd Pique30db6402023-06-26 18:56:51 +000090 mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
91 mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070092 mCompositionDisplay->createDisplayColorProfile(
Kriti Dang646f8ec2022-01-18 14:35:02 +010093 compositionengine::DisplayColorProfileCreationArgsBuilder()
94 .setHasWideColorGamut(args.hasWideColorGamut)
95 .setHdrCapabilities(std::move(args.hdrCapabilities))
96 .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
97 .setHwcColorModes(std::move(args.hwcColorModes))
98 .Build());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070099
Lloyd Pique32cbe282018-10-19 13:09:22 -0700100 if (!mCompositionDisplay->isValid()) {
101 ALOGE("Composition Display did not validate!");
102 }
103
Lloyd Pique31cb2942018-10-19 17:23:03 -0700104 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700105
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400106 setPowerMode(args.initialPowerMode);
Alec Mouriba013fa2018-10-16 12:43:11 -0700107
Jesse Hallffe1f192013-03-22 15:13:48 -0700108 // initialize the display orientation transform.
Dominik Laskowski718f9602019-11-09 20:01:35 -0800109 setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110}
111
Lloyd Pique09594832018-01-22 17:48:03 -0800112DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700113
Lloyd Pique45a165a2018-10-19 11:54:47 -0700114void DisplayDevice::disconnect() {
115 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700116}
117
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700118int DisplayDevice::getWidth() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000119 return mCompositionDisplay->getState().displaySpace.getBounds().width;
Mathias Agopiana4912602012-07-12 14:25:33 -0700120}
121
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700122int DisplayDevice::getHeight() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000123 return mCompositionDisplay->getState().displaySpace.getBounds().height;
Mathias Agopiana4912602012-07-12 14:25:33 -0700124}
125
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700126void DisplayDevice::setDisplayName(const std::string& displayName) {
127 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700128 // never override the name with an empty name
129 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700131 }
132}
133
Vishnu Nairaf6d2972022-11-18 06:26:38 +0000134auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700135 gui::DisplayInfo info;
Linnan Li13bf76a2024-05-05 19:18:02 +0800136 info.displayId = ui::LogicalDisplayId{static_cast<int32_t>(getLayerStack().id)};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700137
138 // The physical orientation is set when the orientation of the display panel is
139 // different than the default orientation of the device. Other services like
140 // InputFlinger do not know about this, so we do not need to expose the physical
141 // orientation of the panel outside of SurfaceFlinger.
142 const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
143 auto width = getWidth();
144 auto height = getHeight();
145 if (inversePhysicalOrientation == ui::ROTATION_90 ||
146 inversePhysicalOrientation == ui::ROTATION_270) {
147 std::swap(width, height);
148 }
149 const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
150 inversePhysicalOrientation),
151 width, height);
152 const auto& displayTransform = undoPhysicalOrientation * getTransform();
153 // Send the inverse display transform to input so it can convert display coordinates to
154 // logical display.
155 info.transform = displayTransform.inverse();
156
157 info.logicalWidth = getLayerStackSpaceRect().width();
158 info.logicalHeight = getLayerStackSpaceRect().height();
Dominik Laskowski9f410f02022-01-08 16:22:46 -0800159
160 return {.info = info,
161 .transform = displayTransform,
162 .receivesInput = receivesInput(),
Vishnu Nair67b431c2022-11-16 01:54:05 +0000163 .isSecure = isSecure(),
164 .isPrimary = isPrimary(),
Vishnu Nair81750622023-03-08 15:02:06 -0800165 .isVirtual = isVirtual(),
Vishnu Naircfb2d252023-01-19 04:44:02 +0000166 .rotationFlags = ui::Transform::toRotationFlags(mOrientation),
167 .transformHint = getTransformHint()};
Prabir Pradhan48f8cb92021-08-26 14:05:36 -0700168}
169
Peiyong Lin65248e02020-04-18 21:15:07 -0700170void DisplayDevice::setPowerMode(hal::PowerMode mode) {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400171 // TODO(b/241285876): Skip this for virtual displays.
joenchene72ba5e2022-08-24 13:08:58 +0000172 if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
linpeterdcfd4e62022-12-29 09:39:14 +0800173 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchene72ba5e2022-08-24 13:08:58 +0000174 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
175 mBrightness = *mStagedBrightness;
176 }
177 mStagedBrightness = std::nullopt;
178 getCompositionDisplay()->applyDisplayBrightness(true);
179 }
180
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400181 mPowerMode = mode;
linpeter1c69cd32022-07-13 19:20:48 +0800182
Dominik Laskowskib4ff7d92022-11-22 13:59:53 -0500183 getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700184}
185
Ady Abraham66452722023-03-14 17:41:47 -0700186void DisplayDevice::tracePowerMode() {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400187 // Assign the same value for tracing.
188 mPowerMode = mPowerMode.get();
Ady Abraham66452722023-03-14 17:41:47 -0700189}
190
Alec Mouri023c1882021-05-08 16:36:33 -0700191void DisplayDevice::enableLayerCaching(bool enable) {
192 getCompositionDisplay()->setLayerCachingEnabled(enable);
193}
194
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400195hal::PowerMode DisplayDevice::getPowerMode() const {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700196 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700197}
198
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700199bool DisplayDevice::isPoweredOn() const {
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400200 return mPowerMode != hal::PowerMode::OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700201}
202
Marin Shalamanov045b7002021-01-07 16:56:24 +0100203nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
204 const auto physicalId = getPhysicalId();
205 if (!mHwComposer.isConnected(physicalId)) {
206 return 0;
207 }
208
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500209 if (const auto vsyncPeriodOpt = mHwComposer.getDisplayVsyncPeriod(physicalId).value_opt()) {
210 return *vsyncPeriodOpt;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100211 }
212
ramindania04b8a52023-08-07 18:49:47 -0700213 return refreshRateSelector().getActiveMode().modePtr->getVsyncRate().getPeriodNsecs();
Marin Shalamanov045b7002021-01-07 16:56:24 +0100214}
215
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800216ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700217 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800218}
219
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700220void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
221 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700222 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700223 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700224 }
Sally Qi147581b2023-06-27 11:55:34 -0700225 if (mHdrSdrRatioOverlay) {
226 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
227 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700228}
229
Evan Rosky2239b372021-05-20 13:43:47 -0700230void DisplayDevice::setFlags(uint32_t flags) {
231 mFlags = flags;
232}
233
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800234void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200235 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700236 const auto size = ui::Size(width, height);
237 mCompositionDisplay->setDisplaySize(size);
238 if (mRefreshRateOverlay) {
239 mRefreshRateOverlay->setViewport(size);
240 }
Sally Qi147581b2023-06-27 11:55:34 -0700241 if (mHdrSdrRatioOverlay) {
242 mHdrSdrRatioOverlay->setViewport(size);
243 }
Michael Lentine47e45402014-07-18 15:34:25 -0700244}
245
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200246void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
247 Rect orientedDisplaySpaceRect) {
Sally Qi147581b2023-06-27 11:55:34 -0700248 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249 mOrientation = orientation;
250
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200251 // We need to take care of display rotation for globalTransform for case if the panel is not
252 // installed aligned with device orientation.
253 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000254
255 const auto& state = getCompositionDisplay()->getState();
256
257 // If the layer stack and destination frames have never been set, then configure them to be the
258 // same as the physical device, taking into account the total transform.
259 if (!orientedDisplaySpaceRect.isValid()) {
260 ui::Size bounds = state.displaySpace.getBounds();
261 bounds.rotate(transformOrientation);
262 orientedDisplaySpaceRect = Rect(bounds);
263 }
264 if (layerStackSpaceRect.isEmpty()) {
265 ui::Size bounds = state.framebufferSpace.getBounds();
266 bounds.rotate(transformOrientation);
267 layerStackSpaceRect = Rect(bounds);
268 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200269 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
270 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700271}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700272
Alec Mouricdf16792021-12-10 13:16:06 -0800273void DisplayDevice::stageBrightness(float brightness) {
274 mStagedBrightness = brightness;
275}
276
277void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800278 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000279 if (needsComposite) {
280 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
281 }
Alec Mouricdf16792021-12-10 13:16:06 -0800282 mBrightness = *mStagedBrightness;
283 }
284 mStagedBrightness = std::nullopt;
285}
286
287std::optional<float> DisplayDevice::getStagedBrightness() const {
288 return mStagedBrightness;
289}
290
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700291void DisplayDevice::dump(utils::Dumper& dumper) const {
292 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100293
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400294 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700295 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700296
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400297 if (mRefreshRateSelector) {
298 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700299 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700300}
Irvelffc9efc2016-07-27 15:16:37 -0700301
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700302bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
303 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700304}
305
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200306DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700307 return mCompositionDisplay->getId();
308}
309
310bool DisplayDevice::isSecure() const {
311 return mCompositionDisplay->isSecure();
312}
313
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700314void DisplayDevice::setSecure(bool secure) {
315 mCompositionDisplay->setSecure(secure);
316}
317
Angel Aguayob108a6d2021-08-06 21:34:57 +0000318const Rect DisplayDevice::getBounds() const {
319 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700320}
321
322const Region& DisplayDevice::getUndefinedRegion() const {
323 return mCompositionDisplay->getState().undefinedRegion;
324}
325
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800326ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700327 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700328}
329
Garfield Tan54edd912020-10-21 16:31:41 -0700330ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
331 return mCompositionDisplay->getTransformHint();
332}
333
Lloyd Pique32cbe282018-10-19 13:09:22 -0700334const ui::Transform& DisplayDevice::getTransform() const {
335 return mCompositionDisplay->getState().transform;
336}
337
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200338const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000339 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700340}
341
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200342const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000343 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700344}
345
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700346bool DisplayDevice::hasWideColorGamut() const {
347 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
348}
349
350bool DisplayDevice::hasHDR10PlusSupport() const {
351 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
352}
353
354bool DisplayDevice::hasHDR10Support() const {
355 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
356}
357
358bool DisplayDevice::hasHLGSupport() const {
359 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
360}
361
362bool DisplayDevice::hasDolbyVisionSupport() const {
363 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
364}
365
366int DisplayDevice::getSupportedPerFrameMetadata() const {
367 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
368}
369
Kriti Dang49ad4132021-01-08 11:49:56 +0100370void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
371 mOverrideHdrTypes = hdrTypes;
372}
373
374HdrCapabilities DisplayDevice::getHdrCapabilities() const {
375 const HdrCapabilities& capabilities =
376 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
377 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
378 if (!mOverrideHdrTypes.empty()) {
379 hdrTypes = mOverrideHdrTypes;
380 }
381 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
382 capabilities.getDesiredMaxAverageLuminance(),
383 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700384}
385
Sally Qi147581b2023-06-27 11:55:34 -0700386void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
387 if (!enable) {
388 mHdrSdrRatioOverlay.reset();
389 return;
390 }
391
Ady Abrahamddba9342023-10-02 16:28:03 -0700392 mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
393 if (mHdrSdrRatioOverlay) {
394 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
395 mHdrSdrRatioOverlay->setViewport(getSize());
396 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
397 }
Sally Qi147581b2023-06-27 11:55:34 -0700398}
399
400void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
401 ATRACE_CALL();
402 mHdrSdrRatio = currentHdrSdrRatio;
403 if (mHdrSdrRatioOverlay) {
404 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
405 }
406}
407
Dominik Laskowski5c989f52024-04-11 13:57:14 -0400408void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, Fps refreshRate,
409 Fps renderFps, bool showSpinner, bool showRenderRate,
410 bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700411 if (!enable) {
412 mRefreshRateOverlay.reset();
413 return;
414 }
415
Ady Abraham0aa373a2022-11-22 13:56:50 -0800416 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000417 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800418 features |= RefreshRateOverlay::Features::Spinner;
419 }
420
Lloyd Pique30db6402023-06-26 18:56:51 +0000421 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800422 features |= RefreshRateOverlay::Features::RenderRate;
423 }
424
Lloyd Pique30db6402023-06-26 18:56:51 +0000425 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800426 features |= RefreshRateOverlay::Features::ShowInMiddle;
427 }
428
ramindanib2158ee2023-02-13 20:29:59 -0800429 if (setByHwc) {
430 features |= RefreshRateOverlay::Features::SetByHwc;
431 }
432
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400433 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abrahamddba9342023-10-02 16:28:03 -0700434 mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
435 if (mRefreshRateOverlay) {
436 mRefreshRateOverlay->setLayerStack(getLayerStack());
437 mRefreshRateOverlay->setViewport(getSize());
Dominik Laskowski5c989f52024-04-11 13:57:14 -0400438 updateRefreshRateOverlayRate(refreshRate, renderFps, setByHwc);
Ady Abrahamddba9342023-10-02 16:28:03 -0700439 }
ramindanib2158ee2023-02-13 20:29:59 -0800440}
441
Ying Wei22d59cc2024-05-11 02:41:08 +0000442void DisplayDevice::updateRefreshRateOverlayRate(Fps refreshRate, Fps renderFps, bool setByHwc) {
ramindanib2158ee2023-02-13 20:29:59 -0800443 ATRACE_CALL();
ramindani3faf5742023-09-21 13:45:12 -0700444 if (mRefreshRateOverlay) {
445 if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
ramindanic6e522e2024-05-07 16:42:51 -0700446 if (mRefreshRateSelector->isVrrDevice() && !mRefreshRateOverlay->isSetByHwc()) {
447 refreshRate = renderFps;
448 }
Ying Wei22d59cc2024-05-11 02:41:08 +0000449 mRefreshRateOverlay->changeRefreshRate(refreshRate, renderFps);
ramindani3faf5742023-09-21 13:45:12 -0700450 } else {
451 mRefreshRateOverlay->changeRenderRate(renderFps);
452 }
ramindanib2158ee2023-02-13 20:29:59 -0800453 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700454}
455
456bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
457 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400458 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800459 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400460 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800461 if (newMode) {
ramindania04b8a52023-08-07 18:49:47 -0700462 updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700463 return true;
464 }
465 }
466
467 return false;
468}
469
Sally Qi147581b2023-06-27 11:55:34 -0700470void DisplayDevice::animateOverlay() {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700471 if (mRefreshRateOverlay) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700472 mRefreshRateOverlay->animate();
Ady Abraham1b11bc62021-06-03 19:51:19 -0700473 }
Sally Qi147581b2023-06-27 11:55:34 -0700474 if (mHdrSdrRatioOverlay) {
475 // hdr sdr ratio is designed to be on the top right of the screen,
476 // therefore, we need to re-calculate the display's width and height
477 if (mIsOrientationChanged) {
478 auto width = getWidth();
479 auto height = getHeight();
480 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
481 std::swap(width, height);
482 }
483 mHdrSdrRatioOverlay->setViewport({width, height});
484 }
485 mHdrSdrRatioOverlay->animate();
486 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700487}
488
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500489void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000490 using fps_approx_ops::operator<=;
491 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700492 return;
493 }
494
495 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500496 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
497 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700498 return;
499 }
500
501 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000502 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
503 if (divisor == 0) {
504 mAdjustedRefreshRate = 0_Hz;
505 return;
506 }
507
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500508 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700509}
510
Dominik Laskowski663bd282018-04-19 15:26:54 -0700511std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700512
513} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800514
515// TODO(b/129481165): remove the #pragma below and fix conversion issues
516#pragma clang diagnostic pop // ignored "-Wconversion"