• 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 <ftl/concat.h>
28 #include <gui/LayerState.h>
29 #include <math/mat4.h>
30 #include <renderengine/RenderEngine.h>
31 #include <system/window.h>
32 #include <ui/DisplayId.h>
33 #include <ui/DisplayIdentification.h>
34 #include <ui/DisplayState.h>
35 #include <ui/GraphicTypes.h>
36 #include <ui/HdrCapabilities.h>
37 #include <ui/Region.h>
38 #include <ui/StaticDisplayInfo.h>
39 #include <ui/Transform.h>
40 #include <utils/Errors.h>
41 #include <utils/Mutex.h>
42 #include <utils/RefBase.h>
43 #include <utils/Timers.h>
44 
45 #include "Display/DisplayModeRequest.h"
46 #include "DisplayHardware/DisplayMode.h"
47 #include "DisplayHardware/Hal.h"
48 #include "DisplayHardware/PowerAdvisor.h"
49 #include "FrontEnd/DisplayInfo.h"
50 #include "Scheduler/RefreshRateSelector.h"
51 #include "ThreadContext.h"
52 #include "TracedOrdinal.h"
53 #include "Utils/Dumper.h"
54 namespace android {
55 
56 class Fence;
57 class HWComposer;
58 class IGraphicBufferProducer;
59 class Layer;
60 class RefreshRateOverlay;
61 class SurfaceFlinger;
62 
63 struct CompositionInfo;
64 struct DisplayDeviceCreationArgs;
65 
66 namespace compositionengine {
67 class Display;
68 class DisplaySurface;
69 } // namespace compositionengine
70 
71 namespace display {
72 class DisplaySnapshot;
73 } // namespace display
74 
75 class DisplayDevice : public RefBase {
76 public:
77     constexpr static float sDefaultMinLumiance = 0.0;
78     constexpr static float sDefaultMaxLumiance = 500.0;
79     enum { eReceivesInput = 0x01 };
80 
81     explicit DisplayDevice(DisplayDeviceCreationArgs& args);
82 
83     // Must be destroyed on the main thread because it may call into HWComposer.
84     virtual ~DisplayDevice();
85 
getCompositionDisplay()86     std::shared_ptr<compositionengine::Display> getCompositionDisplay() const {
87         return mCompositionDisplay;
88     }
89 
isVirtual()90     bool isVirtual() const { return VirtualDisplayId::tryCast(getId()).has_value(); }
isPrimary()91     bool isPrimary() const { return mIsPrimary; }
92 
93     // isSecure indicates whether this display can be trusted to display
94     // secure surfaces.
95     bool isSecure() const;
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     std::optional<hardware::graphics::composer::hal::PowerMode> getPowerMode() const;
174     void setPowerMode(hardware::graphics::composer::hal::PowerMode mode);
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 
183     /* ------------------------------------------------------------------------
184      * Display mode management.
185      */
186 
187     // TODO(b/241285876): Replace ActiveModeInfo and DisplayModeEvent with DisplayModeRequest.
188     struct ActiveModeInfo {
189         using Event = scheduler::DisplayModeEvent;
190 
191         ActiveModeInfo() = default;
ActiveModeInfoActiveModeInfo192         ActiveModeInfo(scheduler::FrameRateMode mode, Event event)
193               : modeOpt(std::move(mode)), event(event) {}
194 
ActiveModeInfoActiveModeInfo195         explicit ActiveModeInfo(display::DisplayModeRequest&& request)
196               : ActiveModeInfo(std::move(request.mode),
197                                request.emitEvent ? Event::Changed : Event::None) {}
198 
199         ftl::Optional<scheduler::FrameRateMode> modeOpt;
200         Event event = Event::None;
201 
202         bool operator!=(const ActiveModeInfo& other) const {
203             return modeOpt != other.modeOpt || event != other.event;
204         }
205     };
206 
207     enum class DesiredActiveModeAction {
208         None,
209         InitiateDisplayModeSwitch,
210         InitiateRenderRateSwitch
211     };
212     DesiredActiveModeAction setDesiredActiveMode(const ActiveModeInfo&, bool force = false)
213             EXCLUDES(mActiveModeLock);
214     std::optional<ActiveModeInfo> getDesiredActiveMode() const EXCLUDES(mActiveModeLock);
215     void clearDesiredActiveModeState() EXCLUDES(mActiveModeLock);
getUpcomingActiveMode()216     ActiveModeInfo getUpcomingActiveMode() const REQUIRES(kMainThreadContext) {
217         return mUpcomingActiveMode;
218     }
219 
isModeSetPending()220     bool isModeSetPending() const REQUIRES(kMainThreadContext) { return mIsModeSetPending; }
221 
getActiveMode()222     scheduler::FrameRateMode getActiveMode() const REQUIRES(kMainThreadContext) {
223         return mRefreshRateSelector->getActiveMode();
224     }
225 
226     void setActiveMode(DisplayModeId, Fps displayFps, Fps renderFps);
227 
228     status_t initiateModeChange(const ActiveModeInfo&,
229                                 const hal::VsyncPeriodChangeConstraints& constraints,
230                                 hal::VsyncPeriodChangeTimeline* outTimeline)
231             REQUIRES(kMainThreadContext);
232 
233     void finalizeModeChange(DisplayModeId, Fps displayFps, Fps renderFps)
234             REQUIRES(kMainThreadContext);
235 
refreshRateSelector()236     scheduler::RefreshRateSelector& refreshRateSelector() const { return *mRefreshRateSelector; }
237 
238     // Extends the lifetime of the RefreshRateSelector, so it can outlive this DisplayDevice.
holdRefreshRateSelector()239     std::shared_ptr<scheduler::RefreshRateSelector> holdRefreshRateSelector() const {
240         return mRefreshRateSelector;
241     }
242 
243     // Enables an overlay to be displayed with the current refresh rate
244     void enableRefreshRateOverlay(bool enable, bool setByHwc, bool showSpinner, bool showRenderRate,
245                                   bool showInMiddle) REQUIRES(kMainThreadContext);
246     void updateRefreshRateOverlayRate(Fps displayFps, Fps renderFps, bool setByHwc = false);
isRefreshRateOverlayEnabled()247     bool isRefreshRateOverlayEnabled() const { return mRefreshRateOverlay != nullptr; }
248     bool onKernelTimerChanged(std::optional<DisplayModeId>, bool timerExpired);
249     void animateRefreshRateOverlay();
250 
251     nsecs_t getVsyncPeriodFromHWC() const;
252 
getAdjustedRefreshRate()253     Fps getAdjustedRefreshRate() const { return mAdjustedRefreshRate; }
254 
255     // Round the requested refresh rate to match a divisor of the pacesetter
256     // display's refresh rate. Only supported for virtual displays.
257     void adjustRefreshRate(Fps pacesetterDisplayRefreshRate);
258 
259     // release HWC resources (if any) for removable displays
260     void disconnect();
261 
262     void dump(utils::Dumper&) const;
263 
264 private:
265     const sp<SurfaceFlinger> mFlinger;
266     HWComposer& mHwComposer;
267     const wp<IBinder> mDisplayToken;
268     const int32_t mSequenceId;
269 
270     const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
271 
272     std::string mDisplayName;
273     std::string mActiveModeFPSTrace;
274     std::string mActiveModeFPSHwcTrace;
275     std::string mRenderFrameRateFPSTrace;
276 
277     const ui::Rotation mPhysicalOrientation;
278     ui::Rotation mOrientation = ui::ROTATION_0;
279 
280     // Allow nullopt as initial power mode.
281     using TracedPowerMode = TracedOrdinal<hardware::graphics::composer::hal::PowerMode>;
282     std::optional<TracedPowerMode> mPowerMode;
283 
284     std::optional<float> mStagedBrightness;
285     std::optional<float> mBrightness;
286 
287     // TODO(b/182939859): Remove special cases for primary display.
288     const bool mIsPrimary;
289 
290     uint32_t mFlags = 0;
291 
292     // Requested refresh rate in fps, supported only for virtual displays.
293     // when this value is non zero, SurfaceFlinger will try to drop frames
294     // for virtual displays to match this requested refresh rate.
295     const Fps mRequestedRefreshRate;
296 
297     // Adjusted refresh rate, rounded to match a divisor of the pacesetter
298     // display's refresh rate. Only supported for virtual displays.
299     Fps mAdjustedRefreshRate = 0_Hz;
300 
301     std::vector<ui::Hdr> mOverrideHdrTypes;
302 
303     std::shared_ptr<scheduler::RefreshRateSelector> mRefreshRateSelector;
304     std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay;
305 
306     mutable std::mutex mActiveModeLock;
307     ActiveModeInfo mDesiredActiveMode GUARDED_BY(mActiveModeLock);
308     TracedOrdinal<bool> mDesiredActiveModeChanged GUARDED_BY(mActiveModeLock) =
309             {ftl::Concat("DesiredActiveModeChanged-", getId().value).c_str(), false};
310 
311     ActiveModeInfo mUpcomingActiveMode GUARDED_BY(kMainThreadContext);
312     bool mIsModeSetPending GUARDED_BY(kMainThreadContext) = false;
313 };
314 
315 struct DisplayDeviceState {
316     struct Physical {
317         PhysicalDisplayId id;
318         hardware::graphics::composer::hal::HWDisplayId hwcDisplayId;
319         DisplayModePtr activeMode;
320 
321         bool operator==(const Physical& other) const {
322             return id == other.id && hwcDisplayId == other.hwcDisplayId;
323         }
324     };
325 
isVirtualDisplayDeviceState326     bool isVirtual() const { return !physical; }
327 
328     int32_t sequenceId = sNextSequenceId++;
329     std::optional<Physical> physical;
330     sp<IGraphicBufferProducer> surface;
331     ui::LayerStack layerStack;
332     uint32_t flags = 0;
333     Rect layerStackSpaceRect;
334     Rect orientedDisplaySpaceRect;
335     ui::Rotation orientation = ui::ROTATION_0;
336     uint32_t width = 0;
337     uint32_t height = 0;
338     std::string displayName;
339     bool isSecure = false;
340     // Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only
341     Fps requestedRefreshRate;
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::RefreshRateSelector> refreshRateSelector;
358 
359     int32_t sequenceId{0};
360     bool isSecure{false};
361     sp<ANativeWindow> nativeWindow;
362     sp<compositionengine::DisplaySurface> displaySurface;
363     ui::Rotation physicalOrientation{ui::ROTATION_0};
364     bool hasWideColorGamut{false};
365     HdrCapabilities hdrCapabilities;
366     int32_t supportedPerFrameMetadata{0};
367     std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
368     std::optional<hardware::graphics::composer::hal::PowerMode> initialPowerMode;
369     bool isPrimary{false};
370     DisplayModeId activeModeId;
371     // Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only
372     Fps requestedRefreshRate;
373 };
374 
375 } // namespace android
376