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