xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2010 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 <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <optional>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 
27 #include <android-base/thread_annotations.h>
28 #include <ftl/expected.h>
29 #include <ftl/future.h>
30 #include <ui/DisplayIdentification.h>
31 #include <ui/FenceTime.h>
32 #include <ui/PictureProfileHandle.h>
33 
34 // TODO(b/129481165): remove the #pragma below and fix conversion issues
35 #pragma clang diagnostic push
36 #pragma clang diagnostic ignored "-Wconversion"
37 #pragma clang diagnostic ignored "-Wextra"
38 #include <ui/GraphicTypes.h>
39 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
40 
41 #include <utils/StrongPointer.h>
42 #include <utils/Timers.h>
43 
44 #include "DisplayMode.h"
45 #include "HWC2.h"
46 #include "Hal.h"
47 
48 #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
49 #include <aidl/android/hardware/graphics/common/Hdr.h>
50 #include <aidl/android/hardware/graphics/common/HdrConversionCapability.h>
51 #include <aidl/android/hardware/graphics/common/HdrConversionStrategy.h>
52 #include <aidl/android/hardware/graphics/composer3/Capability.h>
53 #include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
54 #include <aidl/android/hardware/graphics/composer3/Composition.h>
55 #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
56 #include <aidl/android/hardware/graphics/composer3/DisplayLuts.h>
57 #include <aidl/android/hardware/graphics/composer3/LutProperties.h>
58 #include <aidl/android/hardware/graphics/composer3/OutputType.h>
59 #include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
60 
61 namespace android {
62 
63 namespace hal = hardware::graphics::composer::hal;
64 
65 struct DisplayedFrameStats;
66 class GraphicBuffer;
67 class TestableSurfaceFlinger;
68 struct HWComposerTest;
69 struct CompositionInfo;
70 class PictureProfileHandle;
71 
72 namespace Hwc2 {
73 class Composer;
74 } // namespace Hwc2
75 
76 namespace compositionengine {
77 class Output;
78 } // namespace compositionengine
79 
80 struct KnownHWCGenericLayerMetadata {
81     const char* name;
82     const uint32_t id;
83 };
84 
85 // See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing
86 // this class.
87 class HWComposer {
88 public:
89     struct DeviceRequestedChanges {
90         using ChangedTypes =
91                 std::unordered_map<HWC2::Layer*,
92                                    aidl::android::hardware::graphics::composer3::Composition>;
93         using ClientTargetProperty =
94                 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
95         using DisplayRequests = hal::DisplayRequest;
96         using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
97         using LutProperties = aidl::android::hardware::graphics::composer3::LutProperties;
98         using LayerLuts = HWC2::Display::LayerLuts;
99 
100         ChangedTypes changedTypes;
101         DisplayRequests displayRequests;
102         LayerRequests layerRequests;
103         ClientTargetProperty clientTargetProperty;
104         LayerLuts layerLuts;
105     };
106 
107     struct HWCDisplayMode {
108         hal::HWConfigId hwcId;
109         int32_t width = -1;
110         int32_t height = -1;
111         nsecs_t vsyncPeriod = -1;
112         float dpiX = -1.f;
113         float dpiY = -1.f;
114         int32_t configGroup = -1;
115         std::optional<hal::VrrConfig> vrrConfig;
116         OutputType hdrOutputType;
117 
118         friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) {
119             return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height
120                       << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x"
121                       << mode.dpiY << " group=" << mode.configGroup
122                       << " vrrConfig=" << to_string(mode.vrrConfig).c_str()
123                       << " hdrOutputType=" << toString(mode.hdrOutputType);
124         }
125     };
126 
127     virtual ~HWComposer();
128 
129     virtual void setCallback(HWC2::ComposerCallback&) = 0;
130 
131     virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
132                                               DisplayIdentificationData* outData) const = 0;
133 
134     virtual bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const = 0;
135     virtual bool hasDisplayCapability(
136             HalDisplayId,
137             aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
138 
139     virtual size_t getMaxVirtualDisplayCount() const = 0;
140     virtual size_t getMaxVirtualDisplayDimension() const = 0;
141 
142     // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays
143     // supported by the HWC can be queried in advance, but allocation may fail for other reasons.
144     virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0;
145 
146     virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId,
147                                          std::optional<ui::Size> physicalSize) = 0;
148 
149     // Attempts to create a new layer on this display
150     virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0;
151 
152     // Gets any required composition change requests from the HWC device.
153     //
154     // Note that frameUsesClientComposition must be set correctly based on
155     // whether the current frame appears to use client composition. If it is
156     // false some internal optimizations are allowed to present the display
157     // with fewer handshakes, but this does not work if client composition is
158     // expected.
159     virtual status_t getDeviceCompositionChanges(
160             HalDisplayId, bool frameUsesClientComposition,
161             std::optional<std::chrono::steady_clock::time_point> earliestPresentTime,
162             nsecs_t expectedPresentTime, Fps frameInterval,
163             std::optional<DeviceRequestedChanges>* outChanges) = 0;
164 
165     virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
166                                      const sp<GraphicBuffer>& target, ui::Dataspace,
167                                      float hdrSdrRatio) = 0;
168 
169     // Present layers to the display and read releaseFences.
170     virtual status_t presentAndGetReleaseFences(
171             HalDisplayId,
172             std::optional<std::chrono::steady_clock::time_point> earliestPresentTime) = 0;
173 
174     virtual status_t executeCommands(HalDisplayId) = 0;
175 
176     // set power mode
177     virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
178 
179     // Sets a color transform to be applied to the result of composition
180     virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
181 
182     // reset state when a display is disconnected
183     virtual void disconnectDisplay(HalDisplayId) = 0;
184 
185     // Get the present fence/timestamp received from the last call to present.
186     virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
187     virtual nsecs_t getPresentTimestamp(PhysicalDisplayId) const = 0;
188 
189     // Get last release fence for the given layer
190     virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
191 
192     // Set the output buffer and acquire fence for a virtual display.
193     virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
194                                      const sp<GraphicBuffer>& buffer) = 0;
195 
196     // After SurfaceFlinger has retrieved the release fences for all the frames,
197     // it can call this to clear the shared pointers in the release fence map
198     virtual void clearReleaseFences(HalDisplayId) = 0;
199 
200     // Fetches the HDR capabilities of the given display
201     virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
202 
203     virtual const aidl::android::hardware::graphics::composer3::OverlayProperties&
204     getOverlaySupport() const = 0;
205 
206     virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
207 
208     // Returns the available RenderIntent of the given display.
209     virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
210 
211     virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
212 
213     // Returns the attributes of the color sampling engine.
214     virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
215                                                            ui::Dataspace* outDataspace,
216                                                            uint8_t* outComponentMask) = 0;
217     virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
218                                                       uint8_t componentMask,
219                                                       uint64_t maxFrames) = 0;
220     virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
221                                                DisplayedFrameStats* outStats) = 0;
222 
223     // Sets the brightness of a display.
224     virtual ftl::Future<status_t> setDisplayBrightness(
225             PhysicalDisplayId, float brightness, float brightnessNits,
226             const Hwc2::Composer::DisplayBrightnessOptions&) = 0;
227 
228     // Get whether the display skipped validation on the latest present
229     virtual bool getValidateSkipped(HalDisplayId displayId) const = 0;
230 
231     // Events handling ---------------------------------------------------------
232 
233     // Returns stable display ID (and display name on connection of new or previously disconnected
234     // display), or std::nullopt if hotplug event was ignored.
235     // This function is called from SurfaceFlinger.
236     virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
237                                                                hal::Connection) = 0;
238 
239     // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
240     // TODO(b/157555476): Remove when the framework has proper support for headless mode
241     virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
242 
243     // Called when a vsync happens. If the vsync is valid, returns the
244     // corresponding PhysicalDisplayId. Otherwise returns nullopt.
245     virtual std::optional<PhysicalDisplayId> onVsync(hal::HWDisplayId, nsecs_t timestamp) = 0;
246 
247     virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
248 
249     virtual bool isConnected(PhysicalDisplayId) const = 0;
250 
251     virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId,
252                                                  int32_t maxFrameIntervalNs) const = 0;
253 
254     virtual ftl::Expected<hal::HWConfigId, status_t> getActiveMode(PhysicalDisplayId) const = 0;
255 
256     virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
257 
258     virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
259                                         ui::RenderIntent) = 0;
260 
261     // Composer 2.4
262     virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
263     virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
264     virtual ftl::Expected<nsecs_t, status_t> getDisplayVsyncPeriod(PhysicalDisplayId) const = 0;
265     virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
266                                                   const hal::VsyncPeriodChangeConstraints&,
267                                                   hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
268     virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
269     virtual status_t getSupportedContentTypes(
270             PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) const = 0;
271 
supportsContentType(PhysicalDisplayId displayId,hal::ContentType type)272     bool supportsContentType(PhysicalDisplayId displayId, hal::ContentType type) const {
273         std::vector<hal::ContentType> types;
274         return getSupportedContentTypes(displayId, &types) == NO_ERROR &&
275                 std::find(types.begin(), types.end(), type) != types.end();
276     }
277 
278     virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
279 
280     virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
281             const = 0;
282 
283     virtual void dump(std::string& out) const = 0;
284 
285     virtual void dumpOverlayProperties(std::string& out) const = 0;
286 
287     virtual Hwc2::Composer* getComposer() const = 0;
288 
289     // Returns the first display connected at boot. Its connection via HWComposer::onHotplug,
290     // which in practice is immediately after HWComposer construction, must occur before any
291     // call to this function.
292     // The primary display can be temporarily disconnected from the perspective
293     // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId()
294     // if isHeadless().
295     //
296     // TODO(b/182939859): Remove special cases for primary display.
297     virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0;
298     virtual PhysicalDisplayId getPrimaryDisplayId() const = 0;
299     virtual bool isHeadless() const = 0;
300 
301     virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
302     virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
303 
304     // AIDL Composer
305     virtual status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) = 0;
306     virtual status_t clearBootDisplayMode(PhysicalDisplayId) = 0;
307     virtual std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) = 0;
308     virtual status_t getDisplayDecorationSupport(
309             PhysicalDisplayId,
310             std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
311                     support) = 0;
312     virtual status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) = 0;
313     virtual bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const = 0;
314     virtual Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const = 0;
315     virtual std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
316     getHdrConversionCapabilities() const = 0;
317     virtual status_t setHdrConversionStrategy(
318             aidl::android::hardware::graphics::common::HdrConversionStrategy,
319             aidl::android::hardware::graphics::common::Hdr*) = 0;
320     virtual status_t setRefreshRateChangedCallbackDebugEnabled(PhysicalDisplayId, bool enabled) = 0;
321     virtual status_t notifyExpectedPresent(PhysicalDisplayId, TimePoint expectedPresentTime,
322                                            Fps frameInterval) = 0;
323     virtual HWC2::Display::LutFileDescriptorMapper& getLutFileDescriptorMapper() = 0;
324     virtual int32_t getMaxLayerPictureProfiles(PhysicalDisplayId) = 0;
325     virtual status_t setDisplayPictureProfileHandle(PhysicalDisplayId,
326                                                     const PictureProfileHandle& handle) = 0;
327 };
328 
329 static inline bool operator==(const android::HWComposer::DeviceRequestedChanges& lhs,
330                               const android::HWComposer::DeviceRequestedChanges& rhs) {
331     return lhs.changedTypes == rhs.changedTypes && lhs.displayRequests == rhs.displayRequests &&
332             lhs.layerRequests == rhs.layerRequests &&
333             lhs.clientTargetProperty == rhs.clientTargetProperty && lhs.layerLuts == rhs.layerLuts;
334 }
335 
336 namespace impl {
337 
338 class HWComposer final : public android::HWComposer {
339 public:
340     explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
341     explicit HWComposer(const std::string& composerServiceName);
342 
343     ~HWComposer() override;
344 
345     void setCallback(HWC2::ComposerCallback&) override;
346 
347     bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
348                                       DisplayIdentificationData* outData) const override;
349 
350     bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const override;
351     bool hasDisplayCapability(
352             HalDisplayId,
353             aidl::android::hardware::graphics::composer3::DisplayCapability) const override;
354 
355     size_t getMaxVirtualDisplayCount() const override;
356     size_t getMaxVirtualDisplayDimension() const override;
357 
358     bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override;
359 
360     // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
361     void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId,
362                                  std::optional<ui::Size> physicalSize) override;
363 
364     // Attempts to create a new layer on this display
365     std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override;
366 
367     status_t getDeviceCompositionChanges(
368             HalDisplayId, bool frameUsesClientComposition,
369             std::optional<std::chrono::steady_clock::time_point> earliestPresentTime,
370             nsecs_t expectedPresentTime, Fps frameInterval,
371             std::optional<DeviceRequestedChanges>* outChanges) override;
372 
373     status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
374                              const sp<GraphicBuffer>& target, ui::Dataspace,
375                              float hdrSdrRatio) override;
376 
377     // Present layers to the display and read releaseFences.
378     status_t presentAndGetReleaseFences(
379             HalDisplayId,
380             std::optional<std::chrono::steady_clock::time_point> earliestPresentTime) override;
381 
382     status_t executeCommands(HalDisplayId) override;
383 
384     // set power mode
385     status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
386 
387     // Sets a color transform to be applied to the result of composition
388     status_t setColorTransform(HalDisplayId, const mat4& transform) override;
389 
390     // reset state when a display is disconnected
391     void disconnectDisplay(HalDisplayId) override;
392 
393     // Get the present fence/timestamp received from the last call to present.
394     sp<Fence> getPresentFence(HalDisplayId) const override;
395     nsecs_t getPresentTimestamp(PhysicalDisplayId) const override;
396 
397     // Get last release fence for the given layer
398     sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
399 
400     // Set the output buffer and acquire fence for a virtual display.
401     status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
402                              const sp<GraphicBuffer>& buffer) override;
403 
404     // After SurfaceFlinger has retrieved the release fences for all the frames,
405     // it can call this to clear the shared pointers in the release fence map
406     void clearReleaseFences(HalDisplayId) override;
407 
408     // Fetches the HDR capabilities of the given display
409     status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
410 
411     const aidl::android::hardware::graphics::composer3::OverlayProperties& getOverlaySupport()
412             const override;
413 
414     int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
415 
416     // Returns the available RenderIntent of the given display.
417     std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
418 
419     mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
420 
421     // Returns the attributes of the color sampling engine.
422     status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
423                                                    ui::Dataspace* outDataspace,
424                                                    uint8_t* outComponentMask) override;
425     status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
426                                               uint64_t maxFrames) override;
427     status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
428                                        DisplayedFrameStats* outStats) override;
429     ftl::Future<status_t> setDisplayBrightness(
430             PhysicalDisplayId, float brightness, float brightnessNits,
431             const Hwc2::Composer::DisplayBrightnessOptions&) override;
432 
433     // Events handling ---------------------------------------------------------
434 
435     // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
436     // display), or std::nullopt if hotplug event was ignored.
437     std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
438 
439     bool updatesDeviceProductInfoOnHotplugReconnect() const override;
440 
441     std::optional<PhysicalDisplayId> onVsync(hal::HWDisplayId, nsecs_t timestamp) override;
442     void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
443 
444     bool isConnected(PhysicalDisplayId) const override;
445 
446     std::vector<HWCDisplayMode> getModes(PhysicalDisplayId,
447                                          int32_t maxFrameIntervalNs) const override;
448 
449     ftl::Expected<hal::HWConfigId, status_t> getActiveMode(PhysicalDisplayId) const override;
450 
451     std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
452 
453     status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
454 
455     bool getValidateSkipped(HalDisplayId displayId) const override;
456 
457     // Composer 2.4
458     ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
459     bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
460     ftl::Expected<nsecs_t, status_t> getDisplayVsyncPeriod(PhysicalDisplayId) const override;
461     status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
462                                           const hal::VsyncPeriodChangeConstraints&,
463                                           hal::VsyncPeriodChangeTimeline* outTimeline) override;
464     status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
465     status_t getSupportedContentTypes(PhysicalDisplayId,
466                                       std::vector<hal::ContentType>*) const override;
467     status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
468 
469     const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
470 
471     // Composer 3.0
472     status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) override;
473     status_t clearBootDisplayMode(PhysicalDisplayId) override;
474     std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) override;
475     status_t getDisplayDecorationSupport(
476             PhysicalDisplayId,
477             std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
478                     support) override;
479     status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) override;
480     bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const override;
481     Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const override;
482     std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
483     getHdrConversionCapabilities() const override;
484     status_t setHdrConversionStrategy(
485             aidl::android::hardware::graphics::common::HdrConversionStrategy,
486             aidl::android::hardware::graphics::common::Hdr*) override;
487     status_t setRefreshRateChangedCallbackDebugEnabled(PhysicalDisplayId, bool enabled) override;
488     status_t notifyExpectedPresent(PhysicalDisplayId, TimePoint expectedPresentTime,
489                                    Fps frameInterval) override;
490     HWC2::Display::LutFileDescriptorMapper& getLutFileDescriptorMapper() override;
491     int32_t getMaxLayerPictureProfiles(PhysicalDisplayId) override;
492     status_t setDisplayPictureProfileHandle(PhysicalDisplayId,
493                                             const android::PictureProfileHandle& profile) override;
494 
495     // for debugging ----------------------------------------------------------
496     void dump(std::string& out) const override;
497     void dumpOverlayProperties(std::string& out) const override;
498 
getComposer()499     Hwc2::Composer* getComposer() const override { return mComposer.get(); }
500 
getPrimaryHwcDisplayId()501     hal::HWDisplayId getPrimaryHwcDisplayId() const override {
502         LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display");
503         return *mPrimaryHwcDisplayId;
504     }
505 
getPrimaryDisplayId()506     PhysicalDisplayId getPrimaryDisplayId() const override {
507         const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId());
508         LOG_ALWAYS_FATAL_IF(!id, "Missing primary display");
509         return *id;
510     }
511 
isHeadless()512     virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; }
513 
514     std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
515     std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
516 
517 private:
518     // For unit tests
519     friend TestableSurfaceFlinger;
520     friend HWComposerTest;
521 
522     struct DisplayData {
523         std::unique_ptr<HWC2::Display> hwcDisplay;
524 
525         sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
526         nsecs_t lastPresentTimestamp = 0;
527 
528         std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
529 
530         bool validateWasSkipped;
531         hal::Error presentError;
532 
533         bool vsyncTraceToggle = false;
534 
535         std::mutex vsyncEnabledLock;
536         hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
537     };
538 
539     std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
540     std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
541     bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
542 
543     aidl::android::hardware::graphics::composer3::DisplayConfiguration::Dpi
544     getEstimatedDotsPerInchFromSize(uint64_t hwcDisplayId, const HWCDisplayMode& hwcMode) const;
545 
546     aidl::android::hardware::graphics::composer3::DisplayConfiguration::Dpi correctedDpiIfneeded(
547             aidl::android::hardware::graphics::composer3::DisplayConfiguration::Dpi dpi,
548             aidl::android::hardware::graphics::composer3::DisplayConfiguration::Dpi estimatedDpi)
549             const;
550     std::vector<HWCDisplayMode> getModesFromDisplayConfigurations(uint64_t hwcDisplayId,
551                                                                   int32_t maxFrameIntervalNs) const;
552     std::vector<HWCDisplayMode> getModesFromLegacyDisplayConfigs(uint64_t hwcDisplayId) const;
553 
554     int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
555                          hal::Attribute attribute) const;
556 
557     void loadCapabilities();
558     void loadLayerMetadataSupport();
559     void loadOverlayProperties();
560     void loadHdrConversionCapabilities();
561 
562     std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
563 
564     std::unique_ptr<android::Hwc2::Composer> mComposer;
565     std::unordered_set<aidl::android::hardware::graphics::composer3::Capability> mCapabilities;
566     aidl::android::hardware::graphics::composer3::OverlayProperties mOverlayProperties;
567     std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
568             mHdrConversionCapabilities = {};
569 
570     std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
571     bool mRegisteredCallback = false;
572 
573     std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
574     std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
575     bool mHasMultiDisplaySupport = false;
576 
577     const size_t mMaxVirtualDisplayDimension;
578     const bool mUpdateDeviceProductInfoOnHotplugReconnect;
579     bool mEnableVrrTimeout;
580 
581     HWC2::Display::LutFileDescriptorMapper mLutFileDescriptorMapper;
582 };
583 
584 } // namespace impl
585 } // namespace android
586