• 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 <optional>
21 #include <string>
22 #include <unordered_map>
23 
24 #include <android-base/thread_annotations.h>
25 #include <android/native_window.h>
26 #include <binder/IBinder.h>
27 #include <gui/LayerState.h>
28 #include <math/mat4.h>
29 #include <renderengine/RenderEngine.h>
30 #include <system/window.h>
31 #include <ui/DisplayId.h>
32 #include <ui/DisplayIdentification.h>
33 #include <ui/DisplayState.h>
34 #include <ui/GraphicTypes.h>
35 #include <ui/HdrCapabilities.h>
36 #include <ui/Region.h>
37 #include <ui/StaticDisplayInfo.h>
38 #include <ui/Transform.h>
39 #include <utils/Errors.h>
40 #include <utils/Mutex.h>
41 #include <utils/RefBase.h>
42 #include <utils/Timers.h>
43 
44 #include "DisplayHardware/DisplayMode.h"
45 #include "DisplayHardware/Hal.h"
46 #include "DisplayHardware/PowerAdvisor.h"
47 #include "Scheduler/RefreshRateConfigs.h"
48 #include "ThreadContext.h"
49 #include "TracedOrdinal.h"
50 
51 namespace android {
52 
53 class Fence;
54 class HWComposer;
55 class IGraphicBufferProducer;
56 class Layer;
57 class RefreshRateOverlay;
58 class SurfaceFlinger;
59 
60 struct CompositionInfo;
61 struct DisplayDeviceCreationArgs;
62 
63 namespace compositionengine {
64 class Display;
65 class DisplaySurface;
66 } // namespace compositionengine
67 
68 class DisplayDevice : public RefBase {
69 public:
70     constexpr static float sDefaultMinLumiance = 0.0;
71     constexpr static float sDefaultMaxLumiance = 500.0;
72     enum { eReceivesInput = 0x01 };
73 
74     explicit DisplayDevice(DisplayDeviceCreationArgs& args);
75 
76     // Must be destroyed on the main thread because it may call into HWComposer.
77     virtual ~DisplayDevice();
78 
getCompositionDisplay()79     std::shared_ptr<compositionengine::Display> getCompositionDisplay() const {
80         return mCompositionDisplay;
81     }
82 
getConnectionType()83     std::optional<ui::DisplayConnectionType> getConnectionType() const { return mConnectionType; }
84 
isVirtual()85     bool isVirtual() const { return !mConnectionType; }
isPrimary()86     bool isPrimary() const { return mIsPrimary; }
isInternal()87     bool isInternal() const { return mConnectionType == ui::DisplayConnectionType::Internal; }
88 
89     // isSecure indicates whether this display can be trusted to display
90     // secure surfaces.
91     bool isSecure() const;
92 
93     int getWidth() const;
94     int getHeight() const;
getSize()95     ui::Size getSize() const { return {getWidth(), getHeight()}; }
96 
97     void setLayerStack(ui::LayerStack);
98     void setDisplaySize(int width, int height);
99     void setProjection(ui::Rotation orientation, Rect viewport, Rect frame);
100     void stageBrightness(float brightness) REQUIRES(kMainThreadContext);
101     void persistBrightness(bool needsComposite) REQUIRES(kMainThreadContext);
102     bool isBrightnessStale() const REQUIRES(kMainThreadContext);
103     void setFlags(uint32_t flags);
104 
getPhysicalOrientation()105     ui::Rotation getPhysicalOrientation() const { return mPhysicalOrientation; }
getOrientation()106     ui::Rotation getOrientation() const { return mOrientation; }
107 
108     static ui::Transform::RotationFlags getPrimaryDisplayRotationFlags();
109 
110     std::optional<float> getStagedBrightness() const REQUIRES(kMainThreadContext);
111     ui::Transform::RotationFlags getTransformHint() const;
112     const ui::Transform& getTransform() const;
113     const Rect& getLayerStackSpaceRect() const;
114     const Rect& getOrientedDisplaySpaceRect() const;
115     bool needsFiltering() const;
116     ui::LayerStack getLayerStack() const;
receivesInput()117     bool receivesInput() const { return mFlags & eReceivesInput; }
118 
119     DisplayId getId() const;
120 
121     // Shorthand to upcast the ID of a display whose type is known as a precondition.
getPhysicalId()122     PhysicalDisplayId getPhysicalId() const {
123         const auto id = PhysicalDisplayId::tryCast(getId());
124         LOG_FATAL_IF(!id);
125         return *id;
126     }
127 
getVirtualId()128     VirtualDisplayId getVirtualId() const {
129         const auto id = VirtualDisplayId::tryCast(getId());
130         LOG_FATAL_IF(!id);
131         return *id;
132     }
133 
getDisplayToken()134     const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
getSequenceId()135     int32_t getSequenceId() const { return mSequenceId; }
136 
137     const Region& getUndefinedRegion() const;
138 
139     int32_t getSupportedPerFrameMetadata() const;
140 
141     bool hasWideColorGamut() const;
142     // Whether h/w composer has native support for specific HDR type.
143     bool hasHDR10PlusSupport() const;
144     bool hasHDR10Support() const;
145     bool hasHLGSupport() const;
146     bool hasDolbyVisionSupport() const;
147 
148     void overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes);
149 
150     // The returned HdrCapabilities is the combination of HDR capabilities from
151     // hardware composer and RenderEngine. When the DisplayDevice supports wide
152     // color gamut, RenderEngine is able to simulate HDR support in Display P3
153     // color space for both PQ and HLG HDR contents. The minimum and maximum
154     // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance
155     // respectively if hardware composer doesn't return meaningful values.
156     HdrCapabilities getHdrCapabilities() const;
157 
158     // Return true if intent is supported by the display.
159     bool hasRenderIntent(ui::RenderIntent intent) const;
160 
161     const Rect getBounds() const;
bounds()162     const Rect bounds() const { return getBounds(); }
163 
164     void setDisplayName(const std::string& displayName);
getDisplayName()165     const std::string& getDisplayName() const { return mDisplayName; }
166 
167     void setDeviceProductInfo(std::optional<DeviceProductInfo> info);
getDeviceProductInfo()168     const std::optional<DeviceProductInfo>& getDeviceProductInfo() const {
169         return mDeviceProductInfo;
170     }
171 
172     struct InputInfo {
173         gui::DisplayInfo info;
174         ui::Transform transform;
175         bool receivesInput;
176         bool isSecure;
177     };
178 
179     InputInfo getInputInfo() const;
180 
181     /* ------------------------------------------------------------------------
182      * Display power mode management.
183      */
184     std::optional<hardware::graphics::composer::hal::PowerMode> getPowerMode() const;
185     void setPowerMode(hardware::graphics::composer::hal::PowerMode mode);
186     bool isPoweredOn() const;
187 
188     // Enables layer caching on this DisplayDevice
189     void enableLayerCaching(bool enable);
190 
191     ui::Dataspace getCompositionDataSpace() const;
192 
193     /* ------------------------------------------------------------------------
194      * Display mode management.
195      */
196     const DisplayModePtr& getActiveMode() const;
197 
198     struct ActiveModeInfo {
199         DisplayModePtr mode;
200         scheduler::DisplayModeEvent event = scheduler::DisplayModeEvent::None;
201 
202         bool operator!=(const ActiveModeInfo& other) const {
203             return mode != other.mode || event != other.event;
204         }
205     };
206 
207     bool setDesiredActiveMode(const ActiveModeInfo&, bool force = false) EXCLUDES(mActiveModeLock);
208     std::optional<ActiveModeInfo> getDesiredActiveMode() const EXCLUDES(mActiveModeLock);
209     void clearDesiredActiveModeState() EXCLUDES(mActiveModeLock);
getUpcomingActiveMode()210     ActiveModeInfo getUpcomingActiveMode() const REQUIRES(kMainThreadContext) {
211         return mUpcomingActiveMode;
212     }
213 
214     void setActiveMode(DisplayModeId) REQUIRES(kMainThreadContext);
215     status_t initiateModeChange(const ActiveModeInfo&,
216                                 const hal::VsyncPeriodChangeConstraints& constraints,
217                                 hal::VsyncPeriodChangeTimeline* outTimeline)
218             REQUIRES(kMainThreadContext);
219 
220     // Return the immutable list of supported display modes. The HWC may report different modes
221     // after a hotplug reconnect event, in which case the DisplayDevice object will be recreated.
222     // Hotplug reconnects are common for external displays.
223     const DisplayModes& getSupportedModes() const;
224 
225     // Returns nullptr if the given mode ID is not supported. A previously
226     // supported mode may be no longer supported for some devices like TVs and
227     // set-top boxes after a hotplug reconnect.
228     DisplayModePtr getMode(DisplayModeId) const;
229 
230     std::optional<DisplayModeId> translateModeId(hal::HWConfigId) const;
231 
232     // Returns the refresh rate configs for this display.
refreshRateConfigs()233     scheduler::RefreshRateConfigs& refreshRateConfigs() const { return *mRefreshRateConfigs; }
234 
235     // Returns a shared pointer to the refresh rate configs for this display.
236     // Clients can store this refresh rate configs and use it even if the DisplayDevice
237     // is destroyed.
holdRefreshRateConfigs()238     std::shared_ptr<scheduler::RefreshRateConfigs> holdRefreshRateConfigs() const {
239         return mRefreshRateConfigs;
240     }
241 
242     // Enables an overlay to be displayed with the current refresh rate
243     void enableRefreshRateOverlay(bool enable, bool showSpinner);
isRefreshRateOverlayEnabled()244     bool isRefreshRateOverlayEnabled() const { return mRefreshRateOverlay != nullptr; }
245     bool onKernelTimerChanged(std::optional<DisplayModeId>, bool timerExpired);
246     void animateRefreshRateOverlay();
247 
248     void onVsync(nsecs_t timestamp);
249     nsecs_t getVsyncPeriodFromHWC() const;
250     nsecs_t getRefreshTimestamp() const;
251 
252     status_t setRefreshRatePolicy(
253             const std::optional<scheduler::RefreshRateConfigs::Policy>& policy,
254             bool overridePolicy);
255 
256     // release HWC resources (if any) for removable displays
257     void disconnect();
258 
259     /* ------------------------------------------------------------------------
260      * Debugging
261      */
262     uint32_t getPageFlipCount() const;
263     std::string getDebugName() const;
264     void dump(std::string& result) const;
265 
266 private:
267     const sp<SurfaceFlinger> mFlinger;
268     HWComposer& mHwComposer;
269     const wp<IBinder> mDisplayToken;
270     const int32_t mSequenceId;
271     const std::optional<ui::DisplayConnectionType> mConnectionType;
272 
273     const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
274 
275     std::string mDisplayName;
276     std::string mActiveModeFPSTrace;
277     std::string mActiveModeFPSHwcTrace;
278 
279     const ui::Rotation mPhysicalOrientation;
280     ui::Rotation mOrientation = ui::ROTATION_0;
281 
282     static ui::Transform::RotationFlags sPrimaryDisplayRotationFlags;
283 
284      // allow initial power mode as null.
285     std::optional<hardware::graphics::composer::hal::PowerMode> mPowerMode;
286     DisplayModePtr mActiveMode;
287     std::optional<float> mStagedBrightness;
288     std::optional<float> mBrightness;
289     const DisplayModes mSupportedModes;
290 
291     std::atomic<nsecs_t> mLastHwVsync = 0;
292 
293     // TODO(b/182939859): Remove special cases for primary display.
294     const bool mIsPrimary;
295 
296     uint32_t mFlags = 0;
297 
298     std::optional<DeviceProductInfo> mDeviceProductInfo;
299 
300     std::vector<ui::Hdr> mOverrideHdrTypes;
301 
302     std::shared_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs;
303     std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay;
304 
305     mutable std::mutex mActiveModeLock;
306     ActiveModeInfo mDesiredActiveMode GUARDED_BY(mActiveModeLock);
307     TracedOrdinal<bool> mDesiredActiveModeChanged
308             GUARDED_BY(mActiveModeLock) = {"DesiredActiveModeChanged", false};
309     ActiveModeInfo mUpcomingActiveMode GUARDED_BY(kMainThreadContext);
310 
311     std::atomic_int mNumModeSwitchesInPolicy = 0;
312 };
313 
314 struct DisplayDeviceState {
315     struct Physical {
316         PhysicalDisplayId id;
317         ui::DisplayConnectionType type;
318         hardware::graphics::composer::hal::HWDisplayId hwcDisplayId;
319         std::optional<DeviceProductInfo> deviceProductInfo;
320         DisplayModes supportedModes;
321         DisplayModePtr activeMode;
322 
323         bool operator==(const Physical& other) const {
324             return id == other.id && type == other.type && hwcDisplayId == other.hwcDisplayId;
325         }
326     };
327 
isVirtualDisplayDeviceState328     bool isVirtual() const { return !physical; }
329 
330     int32_t sequenceId = sNextSequenceId++;
331     std::optional<Physical> physical;
332     sp<IGraphicBufferProducer> surface;
333     ui::LayerStack layerStack;
334     uint32_t flags = 0;
335     Rect layerStackSpaceRect;
336     Rect orientedDisplaySpaceRect;
337     ui::Rotation orientation = ui::ROTATION_0;
338     uint32_t width = 0;
339     uint32_t height = 0;
340     std::string displayName;
341     bool isSecure = false;
342 
343 private:
344     static std::atomic<int32_t> sNextSequenceId;
345 };
346 
347 struct DisplayDeviceCreationArgs {
348     // We use a constructor to ensure some of the values are set, without
349     // assuming a default value.
350     DisplayDeviceCreationArgs(const sp<SurfaceFlinger>&, HWComposer& hwComposer,
351                               const wp<IBinder>& displayToken,
352                               std::shared_ptr<compositionengine::Display>);
353     const sp<SurfaceFlinger> flinger;
354     HWComposer& hwComposer;
355     const wp<IBinder> displayToken;
356     const std::shared_ptr<compositionengine::Display> compositionDisplay;
357     std::shared_ptr<scheduler::RefreshRateConfigs> refreshRateConfigs;
358 
359     int32_t sequenceId{0};
360     std::optional<ui::DisplayConnectionType> connectionType;
361     bool isSecure{false};
362     sp<ANativeWindow> nativeWindow;
363     sp<compositionengine::DisplaySurface> displaySurface;
364     ui::Rotation physicalOrientation{ui::ROTATION_0};
365     bool hasWideColorGamut{false};
366     HdrCapabilities hdrCapabilities;
367     int32_t supportedPerFrameMetadata{0};
368     std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
369     std::optional<hardware::graphics::composer::hal::PowerMode> initialPowerMode;
370     bool isPrimary{false};
371     DisplayModes supportedModes;
372     DisplayModeId activeModeId;
373 };
374 
375 } // namespace android
376