blob: 402a3d2e2f0b55dd768a5ed5e0fb5c700d7e2660 [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
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800204ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700205 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800206}
207
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700208void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
209 mCompositionDisplay->setLayerFilter(filter);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700210 if (mRefreshRateOverlay) {
Dominik Laskowskib363c4c2022-08-02 14:03:41 -0700211 mRefreshRateOverlay->setLayerStack(filter.layerStack);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700212 }
Sally Qi147581b2023-06-27 11:55:34 -0700213 if (mHdrSdrRatioOverlay) {
214 mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
215 }
Mathias Agopian28947d72012-08-08 18:51:15 -0700216}
217
Evan Rosky2239b372021-05-20 13:43:47 -0700218void DisplayDevice::setFlags(uint32_t flags) {
219 mFlags = flags;
220}
221
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800222void DisplayDevice::setDisplaySize(int width, int height) {
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200223 LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
Ady Abraham1b11bc62021-06-03 19:51:19 -0700224 const auto size = ui::Size(width, height);
225 mCompositionDisplay->setDisplaySize(size);
226 if (mRefreshRateOverlay) {
227 mRefreshRateOverlay->setViewport(size);
228 }
Sally Qi147581b2023-06-27 11:55:34 -0700229 if (mHdrSdrRatioOverlay) {
230 mHdrSdrRatioOverlay->setViewport(size);
231 }
Michael Lentine47e45402014-07-18 15:34:25 -0700232}
233
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200234void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
235 Rect orientedDisplaySpaceRect) {
Sally Qi147581b2023-06-27 11:55:34 -0700236 mIsOrientationChanged = mOrientation != orientation;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700237 mOrientation = orientation;
238
Marin Shalamanov8c23c4e2020-08-19 15:26:31 +0200239 // We need to take care of display rotation for globalTransform for case if the panel is not
240 // installed aligned with device orientation.
241 const auto transformOrientation = orientation + mPhysicalOrientation;
Alec Mouri168f6cc2022-04-07 20:58:00 +0000242
243 const auto& state = getCompositionDisplay()->getState();
244
245 // If the layer stack and destination frames have never been set, then configure them to be the
246 // same as the physical device, taking into account the total transform.
247 if (!orientedDisplaySpaceRect.isValid()) {
248 ui::Size bounds = state.displaySpace.getBounds();
249 bounds.rotate(transformOrientation);
250 orientedDisplaySpaceRect = Rect(bounds);
251 }
252 if (layerStackSpaceRect.isEmpty()) {
253 ui::Size bounds = state.framebufferSpace.getBounds();
254 bounds.rotate(transformOrientation);
255 layerStackSpaceRect = Rect(bounds);
256 }
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200257 getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
258 orientedDisplaySpaceRect);
Mathias Agopian1b031492012-06-20 17:51:20 -0700259}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700260
Alec Mouricdf16792021-12-10 13:16:06 -0800261void DisplayDevice::stageBrightness(float brightness) {
262 mStagedBrightness = brightness;
263}
264
265void DisplayDevice::persistBrightness(bool needsComposite) {
linpeterdcfd4e62022-12-29 09:39:14 +0800266 if (mStagedBrightness && mBrightness != mStagedBrightness) {
joenchen42928102022-09-06 18:03:57 +0000267 if (needsComposite) {
268 getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
269 }
Alec Mouricdf16792021-12-10 13:16:06 -0800270 mBrightness = *mStagedBrightness;
271 }
272 mStagedBrightness = std::nullopt;
273}
274
275std::optional<float> DisplayDevice::getStagedBrightness() const {
276 return mStagedBrightness;
277}
278
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700279void DisplayDevice::dump(utils::Dumper& dumper) const {
280 using namespace std::string_view_literals;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100281
Dominik Laskowski5d7de5f2022-11-03 12:38:32 -0400282 dumper.dump("name"sv, '"' + mDisplayName + '"');
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700283 dumper.dump("powerMode"sv, mPowerMode);
Ady Abraham3efa3942021-06-24 19:01:25 -0700284
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400285 if (mRefreshRateSelector) {
286 mRefreshRateSelector->dump(dumper);
Ady Abraham3efa3942021-06-24 19:01:25 -0700287 }
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700288}
Irvelffc9efc2016-07-27 15:16:37 -0700289
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700290bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
291 return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700292}
293
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200294DisplayId DisplayDevice::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700295 return mCompositionDisplay->getId();
296}
297
298bool DisplayDevice::isSecure() const {
299 return mCompositionDisplay->isSecure();
300}
301
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700302void DisplayDevice::setSecure(bool secure) {
303 mCompositionDisplay->setSecure(secure);
304}
305
Angel Aguayob108a6d2021-08-06 21:34:57 +0000306const Rect DisplayDevice::getBounds() const {
307 return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700308}
309
310const Region& DisplayDevice::getUndefinedRegion() const {
311 return mCompositionDisplay->getState().undefinedRegion;
312}
313
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800314ui::LayerStack DisplayDevice::getLayerStack() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700315 return mCompositionDisplay->getState().layerFilter.layerStack;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700316}
317
Garfield Tan54edd912020-10-21 16:31:41 -0700318ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
319 return mCompositionDisplay->getTransformHint();
320}
321
Lloyd Pique32cbe282018-10-19 13:09:22 -0700322const ui::Transform& DisplayDevice::getTransform() const {
323 return mCompositionDisplay->getState().transform;
324}
325
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200326const Rect& DisplayDevice::getLayerStackSpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000327 return mCompositionDisplay->getState().layerStackSpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700328}
329
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200330const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
Angel Aguayob108a6d2021-08-06 21:34:57 +0000331 return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700332}
333
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700334bool DisplayDevice::hasWideColorGamut() const {
335 return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
336}
337
338bool DisplayDevice::hasHDR10PlusSupport() const {
339 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
340}
341
342bool DisplayDevice::hasHDR10Support() const {
343 return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
344}
345
346bool DisplayDevice::hasHLGSupport() const {
347 return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
348}
349
350bool DisplayDevice::hasDolbyVisionSupport() const {
351 return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
352}
353
354int DisplayDevice::getSupportedPerFrameMetadata() const {
355 return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
356}
357
Kriti Dang49ad4132021-01-08 11:49:56 +0100358void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
359 mOverrideHdrTypes = hdrTypes;
360}
361
362HdrCapabilities DisplayDevice::getHdrCapabilities() const {
363 const HdrCapabilities& capabilities =
364 mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
365 std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
366 if (!mOverrideHdrTypes.empty()) {
367 hdrTypes = mOverrideHdrTypes;
368 }
369 return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
370 capabilities.getDesiredMaxAverageLuminance(),
371 capabilities.getDesiredMinLuminance());
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700372}
373
Sally Qi147581b2023-06-27 11:55:34 -0700374void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
375 if (!enable) {
376 mHdrSdrRatioOverlay.reset();
377 return;
378 }
379
Ady Abrahamddba9342023-10-02 16:28:03 -0700380 mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
381 if (mHdrSdrRatioOverlay) {
382 mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
383 mHdrSdrRatioOverlay->setViewport(getSize());
384 updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
385 }
Sally Qi147581b2023-06-27 11:55:34 -0700386}
387
388void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000389 SFTRACE_CALL();
Sally Qi147581b2023-06-27 11:55:34 -0700390 mHdrSdrRatio = currentHdrSdrRatio;
391 if (mHdrSdrRatioOverlay) {
392 mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
393 }
394}
395
Dominik Laskowski5c989f52024-04-11 13:57:14 -0400396void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, Fps refreshRate,
397 Fps renderFps, bool showSpinner, bool showRenderRate,
398 bool showInMiddle) {
Ady Abraham1b11bc62021-06-03 19:51:19 -0700399 if (!enable) {
400 mRefreshRateOverlay.reset();
401 return;
402 }
403
Ady Abraham0aa373a2022-11-22 13:56:50 -0800404 ftl::Flags<RefreshRateOverlay::Features> features;
Lloyd Pique30db6402023-06-26 18:56:51 +0000405 if (showSpinner) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800406 features |= RefreshRateOverlay::Features::Spinner;
407 }
408
Lloyd Pique30db6402023-06-26 18:56:51 +0000409 if (showRenderRate) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800410 features |= RefreshRateOverlay::Features::RenderRate;
411 }
412
Lloyd Pique30db6402023-06-26 18:56:51 +0000413 if (showInMiddle) {
Yifei Zhangcfb7bb32023-01-12 16:17:14 -0800414 features |= RefreshRateOverlay::Features::ShowInMiddle;
415 }
416
ramindanib2158ee2023-02-13 20:29:59 -0800417 if (setByHwc) {
418 features |= RefreshRateOverlay::Features::SetByHwc;
419 }
420
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400421 const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
Ady Abrahamddba9342023-10-02 16:28:03 -0700422 mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
423 if (mRefreshRateOverlay) {
424 mRefreshRateOverlay->setLayerStack(getLayerStack());
425 mRefreshRateOverlay->setViewport(getSize());
Dominik Laskowski5c989f52024-04-11 13:57:14 -0400426 updateRefreshRateOverlayRate(refreshRate, renderFps, setByHwc);
Ady Abrahamddba9342023-10-02 16:28:03 -0700427 }
ramindanib2158ee2023-02-13 20:29:59 -0800428}
429
Ying Wei22d59cc2024-05-11 02:41:08 +0000430void DisplayDevice::updateRefreshRateOverlayRate(Fps refreshRate, Fps renderFps, bool setByHwc) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000431 SFTRACE_CALL();
ramindani3faf5742023-09-21 13:45:12 -0700432 if (mRefreshRateOverlay) {
433 if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
ramindanic6e522e2024-05-07 16:42:51 -0700434 if (mRefreshRateSelector->isVrrDevice() && !mRefreshRateOverlay->isSetByHwc()) {
435 refreshRate = renderFps;
436 }
Ying Wei22d59cc2024-05-11 02:41:08 +0000437 mRefreshRateOverlay->changeRefreshRate(refreshRate, renderFps);
ramindani3faf5742023-09-21 13:45:12 -0700438 } else {
439 mRefreshRateOverlay->changeRenderRate(renderFps);
440 }
ramindanib2158ee2023-02-13 20:29:59 -0800441 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700442}
443
444bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
445 bool timerExpired) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400446 if (mRefreshRateSelector && mRefreshRateOverlay) {
Ady Abraham0aa373a2022-11-22 13:56:50 -0800447 const auto newMode =
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400448 mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
Ady Abraham0aa373a2022-11-22 13:56:50 -0800449 if (newMode) {
ramindania04b8a52023-08-07 18:49:47 -0700450 updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
Ady Abraham1b11bc62021-06-03 19:51:19 -0700451 return true;
452 }
453 }
454
455 return false;
456}
457
Ady Abrahameba039c2024-07-10 17:45:29 -0700458void DisplayDevice::onVrrIdle(bool idle) {
459 if (mRefreshRateOverlay) {
460 mRefreshRateOverlay->onVrrIdle(idle);
461 }
462}
463
Sally Qi147581b2023-06-27 11:55:34 -0700464void DisplayDevice::animateOverlay() {
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 }
Sally Qi147581b2023-06-27 11:55:34 -0700468 if (mHdrSdrRatioOverlay) {
469 // hdr sdr ratio is designed to be on the top right of the screen,
470 // therefore, we need to re-calculate the display's width and height
471 if (mIsOrientationChanged) {
472 auto width = getWidth();
473 auto height = getHeight();
474 if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
475 std::swap(width, height);
476 }
477 mHdrSdrRatioOverlay->setViewport({width, height});
478 }
479 mHdrSdrRatioOverlay->animate();
480 }
Ady Abraham1b11bc62021-06-03 19:51:19 -0700481}
482
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500483void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
Huihong Luo59a60b72023-03-08 21:44:59 +0000484 using fps_approx_ops::operator<=;
485 if (mRequestedRefreshRate <= 0_Hz) {
Huihong Luo1768cb02022-10-11 11:10:34 -0700486 return;
487 }
488
489 using fps_approx_ops::operator>;
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500490 if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
491 mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
Huihong Luo1768cb02022-10-11 11:10:34 -0700492 return;
493 }
494
495 unsigned divisor = static_cast<unsigned>(
Huihong Luo59a60b72023-03-08 21:44:59 +0000496 std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
497 if (divisor == 0) {
498 mAdjustedRefreshRate = 0_Hz;
499 return;
500 }
501
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500502 mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
Huihong Luo1768cb02022-10-11 11:10:34 -0700503}
504
Dominik Laskowski663bd282018-04-19 15:26:54 -0700505std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700506
507} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800508
509// TODO(b/129481165): remove the #pragma below and fix conversion issues
510#pragma clang diagnostic pop // ignored "-Wconversion"