• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include <android-base/thread_annotations.h>
24 #include <android/native_window.h>
25 #include <binder/IBinder.h>
26 #include <gui/LayerState.h>
27 #include <math/mat4.h>
28 #include <renderengine/RenderEngine.h>
29 #include <system/window.h>
30 #include <ui/DisplayId.h>
31 #include <ui/DisplayIdentification.h>
32 #include <ui/DisplayState.h>
33 #include <ui/GraphicTypes.h>
34 #include <ui/HdrCapabilities.h>
35 #include <ui/Region.h>
36 #include <ui/StaticDisplayInfo.h>
37 #include <ui/Transform.h>
38 #include <utils/Errors.h>
39 #include <utils/Mutex.h>
40 #include <utils/RefBase.h>
41 #include <utils/Timers.h>
42 
43 #include "DisplayHardware/DisplayMode.h"
44 #include "DisplayHardware/Hal.h"
45 #include "DisplayHardware/PowerAdvisor.h"
46 #include "FrontEnd/DisplayInfo.h"
47 #include "Scheduler/RefreshRateSelector.h"
48 #include "ThreadContext.h"
49 #include "TracedOrdinal.h"
50 #include "Utils/Dumper.h"
51 
52 namespace android {
53 
54 class Fence;
55 class HWComposer;
56 class HdrSdrRatioOverlay;
57 class IGraphicBufferProducer;
58 class Layer;
59 class RefreshRateOverlay;
60 class SurfaceFlinger;
61 
62 struct CompositionInfo;
63 struct DisplayDeviceCreationArgs;
64 
65 namespace compositionengine {
66 class Display;
67 class DisplaySurface;
68 } // namespace compositionengine
69 
70 namespace display {
71 class DisplaySnapshot;
72 } // namespace display
73 
74 class DisplayDevice : public RefBase {
75 public:
76     constexpr static float sDefaultMinLumiance = 0.0;
77     constexpr static float sDefaultMaxLumiance = 500.0;
78     enum { eReceivesInput = 0x01 };
79 
80     explicit DisplayDevice(DisplayDeviceCreationArgs& args);
81 
82     // Must be destroyed on the main thread because it may call into HWComposer.
83     virtual ~DisplayDevice();
84 
getCompositionDisplay()85     std::shared_ptr<compositionengine::Display> getCompositionDisplay() const {
86         return mCompositionDisplay;
87     }
88 
isVirtual()89     bool isVirtual() const { return getId().isVirtual(); }
isPrimary()90     bool isPrimary() const { return mIsPrimary; }
91 
92     // isSecure indicates whether this display can be trusted to display
93     // secure surfaces.
94     bool isSecure() const;
95     void setSecure(bool secure);
96 
97     int getWidth() const;
98     int getHeight() const;
getSize()99     ui::Size getSize() const { return {getWidth(), getHeight()}; }
100 
101     void setLayerFilter(ui::LayerFilter);
102     void setDisplaySize(int width, int height);
103     void setProjection(ui::Rotation orientation, Rect viewport, Rect frame);
104     void stageBrightness(float brightness) REQUIRES(kMainThreadContext);
105     void persistBrightness(bool needsComposite) REQUIRES(kMainThreadContext);
106     bool isBrightnessStale() const REQUIRES(kMainThreadContext);
107     void setFlags(uint32_t flags);
108 
getPhysicalOrientation()109     ui::Rotation getPhysicalOrientation() const { return mPhysicalOrientation; }
getOrientation()110     ui::Rotation getOrientation() const { return mOrientation; }
111 
112     std::optional<float> getStagedBrightness() const REQUIRES(kMainThreadContext);
113     ui::Transform::RotationFlags getTransformHint() const;
114     const ui::Transform& getTransform() const;
115     const Rect& getLayerStackSpaceRect() const;
116     const Rect& getOrientedDisplaySpaceRect() const;
117     ui::LayerStack getLayerStack() const;
receivesInput()118     bool receivesInput() const { return mFlags & eReceivesInput; }
119 
120     DisplayId getId() const;
121 
122     // Shorthand to upcast the ID of a display whose type is known as a precondition.
getPhysicalId()123     PhysicalDisplayId getPhysicalId() const {
124         const auto id = PhysicalDisplayId::tryCast(getId());
125         LOG_FATAL_IF(!id);
126         return *id;
127     }
128 
getVirtualId()129     VirtualDisplayId getVirtualId() const {
130         const auto id = VirtualDisplayId::tryCast(getId());
131         LOG_FATAL_IF(!id);
132         return *id;
133     }
134 
getDisplayToken()135     const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
getSequenceId()136     int32_t getSequenceId() const { return mSequenceId; }
137 
138     const Region& getUndefinedRegion() const;
139 
140     int32_t getSupportedPerFrameMetadata() const;
141 
142     bool hasWideColorGamut() const;
143     // Whether h/w composer has native support for specific HDR type.
144     bool hasHDR10PlusSupport() const;
145     bool hasHDR10Support() const;
146     bool hasHLGSupport() const;
147     bool hasDolbyVisionSupport() const;
148 
149     void overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes);
150 
151     // The returned HdrCapabilities is the combination of HDR capabilities from
152     // hardware composer and RenderEngine. When the DisplayDevice supports wide
153     // color gamut, RenderEngine is able to simulate HDR support in Display P3
154     // color space for both PQ and HLG HDR contents. The minimum and maximum
155     // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance
156     // respectively if hardware composer doesn't return meaningful values.
157     HdrCapabilities getHdrCapabilities() const;
158 
159     // Return true if intent is supported by the display.
160     bool hasRenderIntent(ui::RenderIntent intent) const;
161 
162     const Rect getBounds() const;
bounds()163     const Rect bounds() const { return getBounds(); }
164 
165     void setDisplayName(const std::string& displayName);
getDisplayName()166     const std::string& getDisplayName() const { return mDisplayName; }
167 
168     surfaceflinger::frontend::DisplayInfo getFrontEndInfo() const;
169 
170     /* ------------------------------------------------------------------------
171      * Display power mode management.
172      */
173     hardware::graphics::composer::hal::PowerMode getPowerMode() const;
174     void setPowerMode(hardware::graphics::composer::hal::PowerMode);
175     bool isPoweredOn() const;
176     void tracePowerMode();
177 
178     // Enables layer caching on this DisplayDevice
179     void enableLayerCaching(bool enable);
180 
181     ui::Dataspace getCompositionDataSpace() const;
182 
refreshRateSelector()183     scheduler::RefreshRateSelector& refreshRateSelector() const { return *mRefreshRateSelector; }
184 
185     // Extends the lifetime of the RefreshRateSelector, so it can outlive this DisplayDevice.
holdRefreshRateSelector()186     std::shared_ptr<scheduler::RefreshRateSelector> holdRefreshRateSelector() const {
187         return mRefreshRateSelector;
188     }
189 
190     // Enables an overlay to be displayed with the current refresh rate
191     // TODO(b/241285876): Move overlay to DisplayModeController.
192     void enableRefreshRateOverlay(bool enable, bool setByHwc, Fps refreshRate, Fps renderFps,
193                                   bool showSpinner, bool showRenderRate, bool showInMiddle)
194             REQUIRES(kMainThreadContext);
195     void updateRefreshRateOverlayRate(Fps refreshRate, Fps renderFps, bool setByHwc = false);
isRefreshRateOverlayEnabled()196     bool isRefreshRateOverlayEnabled() const { return mRefreshRateOverlay != nullptr; }
197     void animateOverlay();
198     bool onKernelTimerChanged(std::optional<DisplayModeId>, bool timerExpired);
199     void onVrrIdle(bool idle);
200 
201     // Enables an overlay to be display with the hdr/sdr ratio
202     void enableHdrSdrRatioOverlay(bool enable) REQUIRES(kMainThreadContext);
203     void updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio);
isHdrSdrRatioOverlayEnabled()204     bool isHdrSdrRatioOverlayEnabled() const { return mHdrSdrRatioOverlay != nullptr; }
205 
206     nsecs_t getVsyncPeriodFromHWC() const;
207 
getAdjustedRefreshRate()208     Fps getAdjustedRefreshRate() const { return mAdjustedRefreshRate; }
209 
210     // Round the requested refresh rate to match a divisor of the pacesetter
211     // display's refresh rate. Only supported for virtual displays.
212     void adjustRefreshRate(Fps pacesetterDisplayRefreshRate);
213 
214     // release HWC resources (if any) for removable displays
215     void disconnect();
216 
217     void dump(utils::Dumper&) const;
218 
219 private:
220     const sp<SurfaceFlinger> mFlinger;
221     HWComposer& mHwComposer;
222     const wp<IBinder> mDisplayToken;
223     const int32_t mSequenceId;
224 
225     const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
226 
227     std::string mDisplayName;
228 
229     const ui::Rotation mPhysicalOrientation;
230     ui::Rotation mOrientation = ui::ROTATION_0;
231     bool mIsOrientationChanged = false;
232 
233     TracedOrdinal<hardware::graphics::composer::hal::PowerMode> mPowerMode;
234 
235     std::optional<float> mStagedBrightness;
236     std::optional<float> mBrightness;
237 
238     // TODO(b/182939859): Remove special cases for primary display.
239     const bool mIsPrimary;
240 
241     uint32_t mFlags = 0;
242 
243     // Requested refresh rate in fps, supported only for virtual displays.
244     // when this value is non zero, SurfaceFlinger will try to drop frames
245     // for virtual displays to match this requested refresh rate.
246     const Fps mRequestedRefreshRate;
247 
248     // Adjusted refresh rate, rounded to match a divisor of the pacesetter
249     // display's refresh rate. Only supported for virtual displays.
250     Fps mAdjustedRefreshRate = 0_Hz;
251 
252     std::vector<ui::Hdr> mOverrideHdrTypes;
253 
254     std::shared_ptr<scheduler::RefreshRateSelector> mRefreshRateSelector;
255     std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay;
256     std::unique_ptr<HdrSdrRatioOverlay> mHdrSdrRatioOverlay;
257     // This parameter is only used for hdr/sdr ratio overlay
258     float mHdrSdrRatio = 1.0f;
259 };
260 
261 struct DisplayDeviceState {
262     struct Physical {
263         PhysicalDisplayId id;
264         hardware::graphics::composer::hal::HWDisplayId hwcDisplayId;
265         DisplayModePtr activeMode;
266 
267         bool operator==(const Physical& other) const {
268             return id == other.id && hwcDisplayId == other.hwcDisplayId;
269         }
270     };
271 
isVirtualDisplayDeviceState272     bool isVirtual() const { return !physical; }
273 
274     int32_t sequenceId = sNextSequenceId++;
275     std::optional<Physical> physical;
276     sp<IGraphicBufferProducer> surface;
277     ui::LayerStack layerStack;
278     uint32_t flags = 0;
279     Rect layerStackSpaceRect;
280     Rect orientedDisplaySpaceRect;
281     ui::Rotation orientation = ui::ROTATION_0;
282     uint32_t width = 0;
283     uint32_t height = 0;
284     std::string displayName;
285     std::string uniqueId;
286     bool isSecure = false;
287     bool isProtected = false;
288     // Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only
289     Fps requestedRefreshRate;
290 
291 private:
292     static std::atomic<int32_t> sNextSequenceId;
293 };
294 
295 struct DisplayDeviceCreationArgs {
296     // We use a constructor to ensure some of the values are set, without
297     // assuming a default value.
298     DisplayDeviceCreationArgs(const sp<SurfaceFlinger>&, HWComposer& hwComposer,
299                               const wp<IBinder>& displayToken,
300                               std::shared_ptr<compositionengine::Display>);
301     const sp<SurfaceFlinger> flinger;
302     HWComposer& hwComposer;
303     const wp<IBinder> displayToken;
304     const std::shared_ptr<compositionengine::Display> compositionDisplay;
305     std::shared_ptr<scheduler::RefreshRateSelector> refreshRateSelector;
306 
307     int32_t sequenceId{0};
308     bool isSecure{false};
309     bool isProtected{false};
310     sp<ANativeWindow> nativeWindow;
311     sp<compositionengine::DisplaySurface> displaySurface;
312     ui::Rotation physicalOrientation{ui::ROTATION_0};
313     bool hasWideColorGamut{false};
314     HdrCapabilities hdrCapabilities;
315     int32_t supportedPerFrameMetadata{0};
316     std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
317     hardware::graphics::composer::hal::PowerMode initialPowerMode{
318             hardware::graphics::composer::hal::PowerMode::OFF};
319     bool isPrimary{false};
320     // Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only
321     Fps requestedRefreshRate;
322 };
323 
324 } // namespace android
325