1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2007 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
18*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
19*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wconversion"
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker // #define LOG_NDEBUG 0
22*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
23*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "DisplayDevice"
24*38e8c45fSAndroid Build Coastguard Worker
25*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
26*38e8c45fSAndroid Build Coastguard Worker
27*38e8c45fSAndroid Build Coastguard Worker #include <common/trace.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/CompositionEngine.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/Display.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/DisplayColorProfile.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/DisplayColorProfileCreationArgs.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/DisplayCreationArgs.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/DisplaySurface.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/ProjectionSpace.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/RenderSurface.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/RenderSurfaceCreationArgs.h>
37*38e8c45fSAndroid Build Coastguard Worker #include <compositionengine/impl/OutputCompositionState.h>
38*38e8c45fSAndroid Build Coastguard Worker #include <configstore/Utils.h>
39*38e8c45fSAndroid Build Coastguard Worker #include <ftl/concat.h>
40*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
41*38e8c45fSAndroid Build Coastguard Worker #include <system/window.h>
42*38e8c45fSAndroid Build Coastguard Worker
43*38e8c45fSAndroid Build Coastguard Worker #include "DisplayDevice.h"
44*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/DisplayInfo.h"
45*38e8c45fSAndroid Build Coastguard Worker #include "HdrSdrRatioOverlay.h"
46*38e8c45fSAndroid Build Coastguard Worker #include "Layer.h"
47*38e8c45fSAndroid Build Coastguard Worker #include "RefreshRateOverlay.h"
48*38e8c45fSAndroid Build Coastguard Worker #include "SurfaceFlinger.h"
49*38e8c45fSAndroid Build Coastguard Worker
50*38e8c45fSAndroid Build Coastguard Worker namespace android {
51*38e8c45fSAndroid Build Coastguard Worker
52*38e8c45fSAndroid Build Coastguard Worker namespace hal = hardware::graphics::composer::hal;
53*38e8c45fSAndroid Build Coastguard Worker
DisplayDeviceCreationArgs(const sp<SurfaceFlinger> & flinger,HWComposer & hwComposer,const wp<IBinder> & displayToken,std::shared_ptr<compositionengine::Display> compositionDisplay)54*38e8c45fSAndroid Build Coastguard Worker DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
55*38e8c45fSAndroid Build Coastguard Worker const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
56*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<compositionengine::Display> compositionDisplay)
57*38e8c45fSAndroid Build Coastguard Worker : flinger(flinger),
58*38e8c45fSAndroid Build Coastguard Worker hwComposer(hwComposer),
59*38e8c45fSAndroid Build Coastguard Worker displayToken(displayToken),
60*38e8c45fSAndroid Build Coastguard Worker compositionDisplay(compositionDisplay) {}
61*38e8c45fSAndroid Build Coastguard Worker
DisplayDevice(DisplayDeviceCreationArgs & args)62*38e8c45fSAndroid Build Coastguard Worker DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
63*38e8c45fSAndroid Build Coastguard Worker : mFlinger(args.flinger),
64*38e8c45fSAndroid Build Coastguard Worker mHwComposer(args.hwComposer),
65*38e8c45fSAndroid Build Coastguard Worker mDisplayToken(args.displayToken),
66*38e8c45fSAndroid Build Coastguard Worker mSequenceId(args.sequenceId),
67*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay{args.compositionDisplay},
68*38e8c45fSAndroid Build Coastguard Worker mPhysicalOrientation(args.physicalOrientation),
69*38e8c45fSAndroid Build Coastguard Worker mPowerMode(ftl::Concat("PowerMode ", getId().value).c_str(), args.initialPowerMode),
70*38e8c45fSAndroid Build Coastguard Worker mIsPrimary(args.isPrimary),
71*38e8c45fSAndroid Build Coastguard Worker mRequestedRefreshRate(args.requestedRefreshRate),
72*38e8c45fSAndroid Build Coastguard Worker mRefreshRateSelector(std::move(args.refreshRateSelector)) {
73*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->editState().isSecure = args.isSecure;
74*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->editState().isProtected = args.isProtected;
75*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->createRenderSurface(
76*38e8c45fSAndroid Build Coastguard Worker compositionengine::RenderSurfaceCreationArgsBuilder()
77*38e8c45fSAndroid Build Coastguard Worker .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
78*38e8c45fSAndroid Build Coastguard Worker .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
79*38e8c45fSAndroid Build Coastguard Worker .setNativeWindow(std::move(args.nativeWindow))
80*38e8c45fSAndroid Build Coastguard Worker .setDisplaySurface(std::move(args.displaySurface))
81*38e8c45fSAndroid Build Coastguard Worker .setMaxTextureCacheSize(
82*38e8c45fSAndroid Build Coastguard Worker static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
83*38e8c45fSAndroid Build Coastguard Worker .build());
84*38e8c45fSAndroid Build Coastguard Worker
85*38e8c45fSAndroid Build Coastguard Worker if (!mFlinger->mDisableClientCompositionCache &&
86*38e8c45fSAndroid Build Coastguard Worker SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
87*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->createClientCompositionCache(
88*38e8c45fSAndroid Build Coastguard Worker static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
89*38e8c45fSAndroid Build Coastguard Worker }
90*38e8c45fSAndroid Build Coastguard Worker
91*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
92*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
93*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->createDisplayColorProfile(
94*38e8c45fSAndroid Build Coastguard Worker compositionengine::DisplayColorProfileCreationArgsBuilder()
95*38e8c45fSAndroid Build Coastguard Worker .setHasWideColorGamut(args.hasWideColorGamut)
96*38e8c45fSAndroid Build Coastguard Worker .setHdrCapabilities(std::move(args.hdrCapabilities))
97*38e8c45fSAndroid Build Coastguard Worker .setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
98*38e8c45fSAndroid Build Coastguard Worker .setHwcColorModes(std::move(args.hwcColorModes))
99*38e8c45fSAndroid Build Coastguard Worker .Build());
100*38e8c45fSAndroid Build Coastguard Worker
101*38e8c45fSAndroid Build Coastguard Worker if (!mCompositionDisplay->isValid()) {
102*38e8c45fSAndroid Build Coastguard Worker ALOGE("Composition Display did not validate!");
103*38e8c45fSAndroid Build Coastguard Worker }
104*38e8c45fSAndroid Build Coastguard Worker
105*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->getRenderSurface()->initialize();
106*38e8c45fSAndroid Build Coastguard Worker
107*38e8c45fSAndroid Build Coastguard Worker setPowerMode(args.initialPowerMode);
108*38e8c45fSAndroid Build Coastguard Worker
109*38e8c45fSAndroid Build Coastguard Worker // initialize the display orientation transform.
110*38e8c45fSAndroid Build Coastguard Worker setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
111*38e8c45fSAndroid Build Coastguard Worker }
112*38e8c45fSAndroid Build Coastguard Worker
113*38e8c45fSAndroid Build Coastguard Worker DisplayDevice::~DisplayDevice() = default;
114*38e8c45fSAndroid Build Coastguard Worker
disconnect()115*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::disconnect() {
116*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->disconnect();
117*38e8c45fSAndroid Build Coastguard Worker }
118*38e8c45fSAndroid Build Coastguard Worker
getWidth() const119*38e8c45fSAndroid Build Coastguard Worker int DisplayDevice::getWidth() const {
120*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().displaySpace.getBounds().width;
121*38e8c45fSAndroid Build Coastguard Worker }
122*38e8c45fSAndroid Build Coastguard Worker
getHeight() const123*38e8c45fSAndroid Build Coastguard Worker int DisplayDevice::getHeight() const {
124*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().displaySpace.getBounds().height;
125*38e8c45fSAndroid Build Coastguard Worker }
126*38e8c45fSAndroid Build Coastguard Worker
setDisplayName(const std::string & displayName)127*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setDisplayName(const std::string& displayName) {
128*38e8c45fSAndroid Build Coastguard Worker if (!displayName.empty()) {
129*38e8c45fSAndroid Build Coastguard Worker // never override the name with an empty name
130*38e8c45fSAndroid Build Coastguard Worker mDisplayName = displayName;
131*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->setName(displayName);
132*38e8c45fSAndroid Build Coastguard Worker }
133*38e8c45fSAndroid Build Coastguard Worker }
134*38e8c45fSAndroid Build Coastguard Worker
getFrontEndInfo() const135*38e8c45fSAndroid Build Coastguard Worker auto DisplayDevice::getFrontEndInfo() const -> frontend::DisplayInfo {
136*38e8c45fSAndroid Build Coastguard Worker gui::DisplayInfo info;
137*38e8c45fSAndroid Build Coastguard Worker info.displayId = ui::LogicalDisplayId{static_cast<int32_t>(getLayerStack().id)};
138*38e8c45fSAndroid Build Coastguard Worker
139*38e8c45fSAndroid Build Coastguard Worker // The physical orientation is set when the orientation of the display panel is
140*38e8c45fSAndroid Build Coastguard Worker // different than the default orientation of the device. Other services like
141*38e8c45fSAndroid Build Coastguard Worker // InputFlinger do not know about this, so we do not need to expose the physical
142*38e8c45fSAndroid Build Coastguard Worker // orientation of the panel outside of SurfaceFlinger.
143*38e8c45fSAndroid Build Coastguard Worker const ui::Rotation inversePhysicalOrientation = ui::ROTATION_0 - mPhysicalOrientation;
144*38e8c45fSAndroid Build Coastguard Worker auto width = getWidth();
145*38e8c45fSAndroid Build Coastguard Worker auto height = getHeight();
146*38e8c45fSAndroid Build Coastguard Worker if (inversePhysicalOrientation == ui::ROTATION_90 ||
147*38e8c45fSAndroid Build Coastguard Worker inversePhysicalOrientation == ui::ROTATION_270) {
148*38e8c45fSAndroid Build Coastguard Worker std::swap(width, height);
149*38e8c45fSAndroid Build Coastguard Worker }
150*38e8c45fSAndroid Build Coastguard Worker const ui::Transform undoPhysicalOrientation(ui::Transform::toRotationFlags(
151*38e8c45fSAndroid Build Coastguard Worker inversePhysicalOrientation),
152*38e8c45fSAndroid Build Coastguard Worker width, height);
153*38e8c45fSAndroid Build Coastguard Worker const auto& displayTransform = undoPhysicalOrientation * getTransform();
154*38e8c45fSAndroid Build Coastguard Worker // Send the inverse display transform to input so it can convert display coordinates to
155*38e8c45fSAndroid Build Coastguard Worker // logical display.
156*38e8c45fSAndroid Build Coastguard Worker info.transform = displayTransform.inverse();
157*38e8c45fSAndroid Build Coastguard Worker
158*38e8c45fSAndroid Build Coastguard Worker info.logicalWidth = getLayerStackSpaceRect().width();
159*38e8c45fSAndroid Build Coastguard Worker info.logicalHeight = getLayerStackSpaceRect().height();
160*38e8c45fSAndroid Build Coastguard Worker
161*38e8c45fSAndroid Build Coastguard Worker return {.info = info,
162*38e8c45fSAndroid Build Coastguard Worker .transform = displayTransform,
163*38e8c45fSAndroid Build Coastguard Worker .receivesInput = receivesInput(),
164*38e8c45fSAndroid Build Coastguard Worker .isSecure = isSecure(),
165*38e8c45fSAndroid Build Coastguard Worker .isPrimary = isPrimary(),
166*38e8c45fSAndroid Build Coastguard Worker .isVirtual = isVirtual(),
167*38e8c45fSAndroid Build Coastguard Worker .rotationFlags = ui::Transform::toRotationFlags(mOrientation),
168*38e8c45fSAndroid Build Coastguard Worker .transformHint = getTransformHint()};
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker
setPowerMode(hal::PowerMode mode)171*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setPowerMode(hal::PowerMode mode) {
172*38e8c45fSAndroid Build Coastguard Worker // TODO(b/241285876): Skip this for virtual displays.
173*38e8c45fSAndroid Build Coastguard Worker if (mode == hal::PowerMode::OFF || mode == hal::PowerMode::ON) {
174*38e8c45fSAndroid Build Coastguard Worker if (mStagedBrightness && mBrightness != mStagedBrightness) {
175*38e8c45fSAndroid Build Coastguard Worker getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
176*38e8c45fSAndroid Build Coastguard Worker mBrightness = *mStagedBrightness;
177*38e8c45fSAndroid Build Coastguard Worker }
178*38e8c45fSAndroid Build Coastguard Worker mStagedBrightness = std::nullopt;
179*38e8c45fSAndroid Build Coastguard Worker getCompositionDisplay()->applyDisplayBrightness(true);
180*38e8c45fSAndroid Build Coastguard Worker }
181*38e8c45fSAndroid Build Coastguard Worker
182*38e8c45fSAndroid Build Coastguard Worker mPowerMode = mode;
183*38e8c45fSAndroid Build Coastguard Worker
184*38e8c45fSAndroid Build Coastguard Worker getCompositionDisplay()->setCompositionEnabled(isPoweredOn());
185*38e8c45fSAndroid Build Coastguard Worker }
186*38e8c45fSAndroid Build Coastguard Worker
tracePowerMode()187*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::tracePowerMode() {
188*38e8c45fSAndroid Build Coastguard Worker // Assign the same value for tracing.
189*38e8c45fSAndroid Build Coastguard Worker mPowerMode = mPowerMode.get();
190*38e8c45fSAndroid Build Coastguard Worker }
191*38e8c45fSAndroid Build Coastguard Worker
enableLayerCaching(bool enable)192*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::enableLayerCaching(bool enable) {
193*38e8c45fSAndroid Build Coastguard Worker getCompositionDisplay()->setLayerCachingEnabled(enable);
194*38e8c45fSAndroid Build Coastguard Worker }
195*38e8c45fSAndroid Build Coastguard Worker
getPowerMode() const196*38e8c45fSAndroid Build Coastguard Worker hal::PowerMode DisplayDevice::getPowerMode() const {
197*38e8c45fSAndroid Build Coastguard Worker return mPowerMode;
198*38e8c45fSAndroid Build Coastguard Worker }
199*38e8c45fSAndroid Build Coastguard Worker
isPoweredOn() const200*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::isPoweredOn() const {
201*38e8c45fSAndroid Build Coastguard Worker return mPowerMode != hal::PowerMode::OFF;
202*38e8c45fSAndroid Build Coastguard Worker }
203*38e8c45fSAndroid Build Coastguard Worker
isRefreshable() const204*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::isRefreshable() const {
205*38e8c45fSAndroid Build Coastguard Worker return mPowerMode == hal::PowerMode::DOZE || mPowerMode == hal::PowerMode::ON;
206*38e8c45fSAndroid Build Coastguard Worker }
207*38e8c45fSAndroid Build Coastguard Worker
getCompositionDataSpace() const208*38e8c45fSAndroid Build Coastguard Worker ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
209*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().dataspace;
210*38e8c45fSAndroid Build Coastguard Worker }
211*38e8c45fSAndroid Build Coastguard Worker
setLayerFilter(ui::LayerFilter filter)212*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setLayerFilter(ui::LayerFilter filter) {
213*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->setLayerFilter(filter);
214*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateOverlay) {
215*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->setLayerStack(filter.layerStack);
216*38e8c45fSAndroid Build Coastguard Worker }
217*38e8c45fSAndroid Build Coastguard Worker if (mHdrSdrRatioOverlay) {
218*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->setLayerStack(filter.layerStack);
219*38e8c45fSAndroid Build Coastguard Worker }
220*38e8c45fSAndroid Build Coastguard Worker }
221*38e8c45fSAndroid Build Coastguard Worker
setFlags(uint32_t flags)222*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setFlags(uint32_t flags) {
223*38e8c45fSAndroid Build Coastguard Worker mFlags = flags;
224*38e8c45fSAndroid Build Coastguard Worker }
225*38e8c45fSAndroid Build Coastguard Worker
setDisplaySize(int width,int height)226*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setDisplaySize(int width, int height) {
227*38e8c45fSAndroid Build Coastguard Worker LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
228*38e8c45fSAndroid Build Coastguard Worker const auto size = ui::Size(width, height);
229*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->setDisplaySize(size);
230*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateOverlay) {
231*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->setViewport(size);
232*38e8c45fSAndroid Build Coastguard Worker }
233*38e8c45fSAndroid Build Coastguard Worker if (mHdrSdrRatioOverlay) {
234*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->setViewport(size);
235*38e8c45fSAndroid Build Coastguard Worker }
236*38e8c45fSAndroid Build Coastguard Worker }
237*38e8c45fSAndroid Build Coastguard Worker
setProjection(ui::Rotation orientation,Rect layerStackSpaceRect,Rect orientedDisplaySpaceRect)238*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
239*38e8c45fSAndroid Build Coastguard Worker Rect orientedDisplaySpaceRect) {
240*38e8c45fSAndroid Build Coastguard Worker mIsOrientationChanged = mOrientation != orientation;
241*38e8c45fSAndroid Build Coastguard Worker mOrientation = orientation;
242*38e8c45fSAndroid Build Coastguard Worker
243*38e8c45fSAndroid Build Coastguard Worker // We need to take care of display rotation for globalTransform for case if the panel is not
244*38e8c45fSAndroid Build Coastguard Worker // installed aligned with device orientation.
245*38e8c45fSAndroid Build Coastguard Worker const auto transformOrientation = orientation + mPhysicalOrientation;
246*38e8c45fSAndroid Build Coastguard Worker
247*38e8c45fSAndroid Build Coastguard Worker const auto& state = getCompositionDisplay()->getState();
248*38e8c45fSAndroid Build Coastguard Worker
249*38e8c45fSAndroid Build Coastguard Worker // If the layer stack and destination frames have never been set, then configure them to be the
250*38e8c45fSAndroid Build Coastguard Worker // same as the physical device, taking into account the total transform.
251*38e8c45fSAndroid Build Coastguard Worker if (!orientedDisplaySpaceRect.isValid()) {
252*38e8c45fSAndroid Build Coastguard Worker ui::Size bounds = state.displaySpace.getBounds();
253*38e8c45fSAndroid Build Coastguard Worker bounds.rotate(transformOrientation);
254*38e8c45fSAndroid Build Coastguard Worker orientedDisplaySpaceRect = Rect(bounds);
255*38e8c45fSAndroid Build Coastguard Worker }
256*38e8c45fSAndroid Build Coastguard Worker if (layerStackSpaceRect.isEmpty()) {
257*38e8c45fSAndroid Build Coastguard Worker ui::Size bounds = state.framebufferSpace.getBounds();
258*38e8c45fSAndroid Build Coastguard Worker bounds.rotate(transformOrientation);
259*38e8c45fSAndroid Build Coastguard Worker layerStackSpaceRect = Rect(bounds);
260*38e8c45fSAndroid Build Coastguard Worker }
261*38e8c45fSAndroid Build Coastguard Worker getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
262*38e8c45fSAndroid Build Coastguard Worker orientedDisplaySpaceRect);
263*38e8c45fSAndroid Build Coastguard Worker }
264*38e8c45fSAndroid Build Coastguard Worker
stageBrightness(float brightness)265*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::stageBrightness(float brightness) {
266*38e8c45fSAndroid Build Coastguard Worker mStagedBrightness = brightness;
267*38e8c45fSAndroid Build Coastguard Worker }
268*38e8c45fSAndroid Build Coastguard Worker
persistBrightness(bool needsComposite)269*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::persistBrightness(bool needsComposite) {
270*38e8c45fSAndroid Build Coastguard Worker if (mStagedBrightness && mBrightness != mStagedBrightness) {
271*38e8c45fSAndroid Build Coastguard Worker if (needsComposite) {
272*38e8c45fSAndroid Build Coastguard Worker getCompositionDisplay()->setNextBrightness(*mStagedBrightness);
273*38e8c45fSAndroid Build Coastguard Worker }
274*38e8c45fSAndroid Build Coastguard Worker mBrightness = *mStagedBrightness;
275*38e8c45fSAndroid Build Coastguard Worker }
276*38e8c45fSAndroid Build Coastguard Worker mStagedBrightness = std::nullopt;
277*38e8c45fSAndroid Build Coastguard Worker }
278*38e8c45fSAndroid Build Coastguard Worker
getStagedBrightness() const279*38e8c45fSAndroid Build Coastguard Worker std::optional<float> DisplayDevice::getStagedBrightness() const {
280*38e8c45fSAndroid Build Coastguard Worker return mStagedBrightness;
281*38e8c45fSAndroid Build Coastguard Worker }
282*38e8c45fSAndroid Build Coastguard Worker
dump(utils::Dumper & dumper) const283*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::dump(utils::Dumper& dumper) const {
284*38e8c45fSAndroid Build Coastguard Worker using namespace std::string_view_literals;
285*38e8c45fSAndroid Build Coastguard Worker
286*38e8c45fSAndroid Build Coastguard Worker dumper.dump("name"sv, '"' + mDisplayName + '"');
287*38e8c45fSAndroid Build Coastguard Worker dumper.dump("powerMode"sv, mPowerMode);
288*38e8c45fSAndroid Build Coastguard Worker
289*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateSelector) {
290*38e8c45fSAndroid Build Coastguard Worker mRefreshRateSelector->dump(dumper);
291*38e8c45fSAndroid Build Coastguard Worker }
292*38e8c45fSAndroid Build Coastguard Worker }
293*38e8c45fSAndroid Build Coastguard Worker
hasRenderIntent(ui::RenderIntent intent) const294*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
295*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
296*38e8c45fSAndroid Build Coastguard Worker }
297*38e8c45fSAndroid Build Coastguard Worker
getId() const298*38e8c45fSAndroid Build Coastguard Worker DisplayId DisplayDevice::getId() const {
299*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getId();
300*38e8c45fSAndroid Build Coastguard Worker }
301*38e8c45fSAndroid Build Coastguard Worker
isSecure() const302*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::isSecure() const {
303*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->isSecure();
304*38e8c45fSAndroid Build Coastguard Worker }
305*38e8c45fSAndroid Build Coastguard Worker
setSecure(bool secure)306*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::setSecure(bool secure) {
307*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->setSecure(secure);
308*38e8c45fSAndroid Build Coastguard Worker }
309*38e8c45fSAndroid Build Coastguard Worker
getBounds() const310*38e8c45fSAndroid Build Coastguard Worker const Rect DisplayDevice::getBounds() const {
311*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().displaySpace.getBoundsAsRect();
312*38e8c45fSAndroid Build Coastguard Worker }
313*38e8c45fSAndroid Build Coastguard Worker
getUndefinedRegion() const314*38e8c45fSAndroid Build Coastguard Worker const Region& DisplayDevice::getUndefinedRegion() const {
315*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().undefinedRegion;
316*38e8c45fSAndroid Build Coastguard Worker }
317*38e8c45fSAndroid Build Coastguard Worker
getLayerStack() const318*38e8c45fSAndroid Build Coastguard Worker ui::LayerStack DisplayDevice::getLayerStack() const {
319*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().layerFilter.layerStack;
320*38e8c45fSAndroid Build Coastguard Worker }
321*38e8c45fSAndroid Build Coastguard Worker
getTransformHint() const322*38e8c45fSAndroid Build Coastguard Worker ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
323*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getTransformHint();
324*38e8c45fSAndroid Build Coastguard Worker }
325*38e8c45fSAndroid Build Coastguard Worker
getTransform() const326*38e8c45fSAndroid Build Coastguard Worker const ui::Transform& DisplayDevice::getTransform() const {
327*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().transform;
328*38e8c45fSAndroid Build Coastguard Worker }
329*38e8c45fSAndroid Build Coastguard Worker
getLayerStackSpaceRect() const330*38e8c45fSAndroid Build Coastguard Worker const Rect& DisplayDevice::getLayerStackSpaceRect() const {
331*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().layerStackSpace.getContent();
332*38e8c45fSAndroid Build Coastguard Worker }
333*38e8c45fSAndroid Build Coastguard Worker
getOrientedDisplaySpaceRect() const334*38e8c45fSAndroid Build Coastguard Worker const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
335*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getState().orientedDisplaySpace.getContent();
336*38e8c45fSAndroid Build Coastguard Worker }
337*38e8c45fSAndroid Build Coastguard Worker
hasWideColorGamut() const338*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::hasWideColorGamut() const {
339*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
340*38e8c45fSAndroid Build Coastguard Worker }
341*38e8c45fSAndroid Build Coastguard Worker
hasHDR10PlusSupport() const342*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::hasHDR10PlusSupport() const {
343*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
344*38e8c45fSAndroid Build Coastguard Worker }
345*38e8c45fSAndroid Build Coastguard Worker
hasHDR10Support() const346*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::hasHDR10Support() const {
347*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
348*38e8c45fSAndroid Build Coastguard Worker }
349*38e8c45fSAndroid Build Coastguard Worker
hasHLGSupport() const350*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::hasHLGSupport() const {
351*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
352*38e8c45fSAndroid Build Coastguard Worker }
353*38e8c45fSAndroid Build Coastguard Worker
hasDolbyVisionSupport() const354*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::hasDolbyVisionSupport() const {
355*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
356*38e8c45fSAndroid Build Coastguard Worker }
357*38e8c45fSAndroid Build Coastguard Worker
getSupportedPerFrameMetadata() const358*38e8c45fSAndroid Build Coastguard Worker int DisplayDevice::getSupportedPerFrameMetadata() const {
359*38e8c45fSAndroid Build Coastguard Worker return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
360*38e8c45fSAndroid Build Coastguard Worker }
361*38e8c45fSAndroid Build Coastguard Worker
overrideHdrTypes(const std::vector<ui::Hdr> & hdrTypes)362*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
363*38e8c45fSAndroid Build Coastguard Worker mOverrideHdrTypes = hdrTypes;
364*38e8c45fSAndroid Build Coastguard Worker }
365*38e8c45fSAndroid Build Coastguard Worker
getHdrCapabilities() const366*38e8c45fSAndroid Build Coastguard Worker HdrCapabilities DisplayDevice::getHdrCapabilities() const {
367*38e8c45fSAndroid Build Coastguard Worker const HdrCapabilities& capabilities =
368*38e8c45fSAndroid Build Coastguard Worker mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
369*38e8c45fSAndroid Build Coastguard Worker std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
370*38e8c45fSAndroid Build Coastguard Worker if (!mOverrideHdrTypes.empty()) {
371*38e8c45fSAndroid Build Coastguard Worker hdrTypes = mOverrideHdrTypes;
372*38e8c45fSAndroid Build Coastguard Worker }
373*38e8c45fSAndroid Build Coastguard Worker return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
374*38e8c45fSAndroid Build Coastguard Worker capabilities.getDesiredMaxAverageLuminance(),
375*38e8c45fSAndroid Build Coastguard Worker capabilities.getDesiredMinLuminance());
376*38e8c45fSAndroid Build Coastguard Worker }
377*38e8c45fSAndroid Build Coastguard Worker
enableHdrSdrRatioOverlay(bool enable)378*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::enableHdrSdrRatioOverlay(bool enable) {
379*38e8c45fSAndroid Build Coastguard Worker if (!enable) {
380*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay.reset();
381*38e8c45fSAndroid Build Coastguard Worker return;
382*38e8c45fSAndroid Build Coastguard Worker }
383*38e8c45fSAndroid Build Coastguard Worker
384*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay = HdrSdrRatioOverlay::create();
385*38e8c45fSAndroid Build Coastguard Worker if (mHdrSdrRatioOverlay) {
386*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->setLayerStack(getLayerStack());
387*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->setViewport(getSize());
388*38e8c45fSAndroid Build Coastguard Worker updateHdrSdrRatioOverlayRatio(mHdrSdrRatio);
389*38e8c45fSAndroid Build Coastguard Worker }
390*38e8c45fSAndroid Build Coastguard Worker }
391*38e8c45fSAndroid Build Coastguard Worker
updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio)392*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio) {
393*38e8c45fSAndroid Build Coastguard Worker SFTRACE_CALL();
394*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatio = currentHdrSdrRatio;
395*38e8c45fSAndroid Build Coastguard Worker if (mHdrSdrRatioOverlay) {
396*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->changeHdrSdrRatio(currentHdrSdrRatio);
397*38e8c45fSAndroid Build Coastguard Worker }
398*38e8c45fSAndroid Build Coastguard Worker }
399*38e8c45fSAndroid Build Coastguard Worker
enableRefreshRateOverlay(bool enable,bool setByHwc,Fps refreshRate,Fps renderFps,bool showSpinner,bool showRenderRate,bool showInMiddle)400*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::enableRefreshRateOverlay(bool enable, bool setByHwc, Fps refreshRate,
401*38e8c45fSAndroid Build Coastguard Worker Fps renderFps, bool showSpinner, bool showRenderRate,
402*38e8c45fSAndroid Build Coastguard Worker bool showInMiddle) {
403*38e8c45fSAndroid Build Coastguard Worker if (!enable) {
404*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay.reset();
405*38e8c45fSAndroid Build Coastguard Worker return;
406*38e8c45fSAndroid Build Coastguard Worker }
407*38e8c45fSAndroid Build Coastguard Worker
408*38e8c45fSAndroid Build Coastguard Worker ftl::Flags<RefreshRateOverlay::Features> features;
409*38e8c45fSAndroid Build Coastguard Worker if (showSpinner) {
410*38e8c45fSAndroid Build Coastguard Worker features |= RefreshRateOverlay::Features::Spinner;
411*38e8c45fSAndroid Build Coastguard Worker }
412*38e8c45fSAndroid Build Coastguard Worker
413*38e8c45fSAndroid Build Coastguard Worker if (showRenderRate) {
414*38e8c45fSAndroid Build Coastguard Worker features |= RefreshRateOverlay::Features::RenderRate;
415*38e8c45fSAndroid Build Coastguard Worker }
416*38e8c45fSAndroid Build Coastguard Worker
417*38e8c45fSAndroid Build Coastguard Worker if (showInMiddle) {
418*38e8c45fSAndroid Build Coastguard Worker features |= RefreshRateOverlay::Features::ShowInMiddle;
419*38e8c45fSAndroid Build Coastguard Worker }
420*38e8c45fSAndroid Build Coastguard Worker
421*38e8c45fSAndroid Build Coastguard Worker if (setByHwc) {
422*38e8c45fSAndroid Build Coastguard Worker features |= RefreshRateOverlay::Features::SetByHwc;
423*38e8c45fSAndroid Build Coastguard Worker }
424*38e8c45fSAndroid Build Coastguard Worker
425*38e8c45fSAndroid Build Coastguard Worker const auto fpsRange = mRefreshRateSelector->getSupportedRefreshRateRange();
426*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay = RefreshRateOverlay::create(fpsRange, features);
427*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateOverlay) {
428*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->setLayerStack(getLayerStack());
429*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->setViewport(getSize());
430*38e8c45fSAndroid Build Coastguard Worker updateRefreshRateOverlayRate(refreshRate, renderFps, setByHwc);
431*38e8c45fSAndroid Build Coastguard Worker }
432*38e8c45fSAndroid Build Coastguard Worker }
433*38e8c45fSAndroid Build Coastguard Worker
updateRefreshRateOverlayRate(Fps refreshRate,Fps renderFps,bool setByHwc)434*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::updateRefreshRateOverlayRate(Fps refreshRate, Fps renderFps, bool setByHwc) {
435*38e8c45fSAndroid Build Coastguard Worker SFTRACE_CALL();
436*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateOverlay) {
437*38e8c45fSAndroid Build Coastguard Worker if (!mRefreshRateOverlay->isSetByHwc() || setByHwc) {
438*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateSelector->isVrrDevice() && !mRefreshRateOverlay->isSetByHwc()) {
439*38e8c45fSAndroid Build Coastguard Worker refreshRate = renderFps;
440*38e8c45fSAndroid Build Coastguard Worker }
441*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->changeRefreshRate(refreshRate, renderFps);
442*38e8c45fSAndroid Build Coastguard Worker } else {
443*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->changeRenderRate(renderFps);
444*38e8c45fSAndroid Build Coastguard Worker }
445*38e8c45fSAndroid Build Coastguard Worker }
446*38e8c45fSAndroid Build Coastguard Worker }
447*38e8c45fSAndroid Build Coastguard Worker
onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,bool timerExpired)448*38e8c45fSAndroid Build Coastguard Worker bool DisplayDevice::onKernelTimerChanged(std::optional<DisplayModeId> desiredModeId,
449*38e8c45fSAndroid Build Coastguard Worker bool timerExpired) {
450*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateSelector && mRefreshRateOverlay) {
451*38e8c45fSAndroid Build Coastguard Worker const auto newMode =
452*38e8c45fSAndroid Build Coastguard Worker mRefreshRateSelector->onKernelTimerChanged(desiredModeId, timerExpired);
453*38e8c45fSAndroid Build Coastguard Worker if (newMode) {
454*38e8c45fSAndroid Build Coastguard Worker updateRefreshRateOverlayRate(newMode->modePtr->getVsyncRate(), newMode->fps);
455*38e8c45fSAndroid Build Coastguard Worker return true;
456*38e8c45fSAndroid Build Coastguard Worker }
457*38e8c45fSAndroid Build Coastguard Worker }
458*38e8c45fSAndroid Build Coastguard Worker
459*38e8c45fSAndroid Build Coastguard Worker return false;
460*38e8c45fSAndroid Build Coastguard Worker }
461*38e8c45fSAndroid Build Coastguard Worker
onVrrIdle(bool idle)462*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::onVrrIdle(bool idle) {
463*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateOverlay) {
464*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->onVrrIdle(idle);
465*38e8c45fSAndroid Build Coastguard Worker }
466*38e8c45fSAndroid Build Coastguard Worker }
467*38e8c45fSAndroid Build Coastguard Worker
animateOverlay()468*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::animateOverlay() {
469*38e8c45fSAndroid Build Coastguard Worker if (mRefreshRateOverlay) {
470*38e8c45fSAndroid Build Coastguard Worker mRefreshRateOverlay->animate();
471*38e8c45fSAndroid Build Coastguard Worker }
472*38e8c45fSAndroid Build Coastguard Worker if (mHdrSdrRatioOverlay) {
473*38e8c45fSAndroid Build Coastguard Worker // hdr sdr ratio is designed to be on the top right of the screen,
474*38e8c45fSAndroid Build Coastguard Worker // therefore, we need to re-calculate the display's width and height
475*38e8c45fSAndroid Build Coastguard Worker if (mIsOrientationChanged) {
476*38e8c45fSAndroid Build Coastguard Worker auto width = getWidth();
477*38e8c45fSAndroid Build Coastguard Worker auto height = getHeight();
478*38e8c45fSAndroid Build Coastguard Worker if (mOrientation == ui::ROTATION_90 || mOrientation == ui::ROTATION_270) {
479*38e8c45fSAndroid Build Coastguard Worker std::swap(width, height);
480*38e8c45fSAndroid Build Coastguard Worker }
481*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->setViewport({width, height});
482*38e8c45fSAndroid Build Coastguard Worker }
483*38e8c45fSAndroid Build Coastguard Worker mHdrSdrRatioOverlay->animate();
484*38e8c45fSAndroid Build Coastguard Worker }
485*38e8c45fSAndroid Build Coastguard Worker }
486*38e8c45fSAndroid Build Coastguard Worker
adjustRefreshRate(Fps pacesetterDisplayRefreshRate)487*38e8c45fSAndroid Build Coastguard Worker void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
488*38e8c45fSAndroid Build Coastguard Worker using fps_approx_ops::operator<=;
489*38e8c45fSAndroid Build Coastguard Worker if (mRequestedRefreshRate <= 0_Hz) {
490*38e8c45fSAndroid Build Coastguard Worker return;
491*38e8c45fSAndroid Build Coastguard Worker }
492*38e8c45fSAndroid Build Coastguard Worker
493*38e8c45fSAndroid Build Coastguard Worker using fps_approx_ops::operator>;
494*38e8c45fSAndroid Build Coastguard Worker if (mRequestedRefreshRate > pacesetterDisplayRefreshRate) {
495*38e8c45fSAndroid Build Coastguard Worker mAdjustedRefreshRate = pacesetterDisplayRefreshRate;
496*38e8c45fSAndroid Build Coastguard Worker return;
497*38e8c45fSAndroid Build Coastguard Worker }
498*38e8c45fSAndroid Build Coastguard Worker
499*38e8c45fSAndroid Build Coastguard Worker unsigned divisor = static_cast<unsigned>(
500*38e8c45fSAndroid Build Coastguard Worker std::floor(pacesetterDisplayRefreshRate.getValue() / mRequestedRefreshRate.getValue()));
501*38e8c45fSAndroid Build Coastguard Worker if (divisor == 0) {
502*38e8c45fSAndroid Build Coastguard Worker mAdjustedRefreshRate = 0_Hz;
503*38e8c45fSAndroid Build Coastguard Worker return;
504*38e8c45fSAndroid Build Coastguard Worker }
505*38e8c45fSAndroid Build Coastguard Worker
506*38e8c45fSAndroid Build Coastguard Worker mAdjustedRefreshRate = pacesetterDisplayRefreshRate / divisor;
507*38e8c45fSAndroid Build Coastguard Worker }
508*38e8c45fSAndroid Build Coastguard Worker
509*38e8c45fSAndroid Build Coastguard Worker std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
510*38e8c45fSAndroid Build Coastguard Worker
511*38e8c45fSAndroid Build Coastguard Worker } // namespace android
512*38e8c45fSAndroid Build Coastguard Worker
513*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
514*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop // ignored "-Wconversion"
515