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 #ifndef ANDROID_SF_HWCOMPOSER_H
18 #define ANDROID_SF_HWCOMPOSER_H
19 
20 #include <cstdint>
21 #include <future>
22 #include <memory>
23 #include <mutex>
24 #include <optional>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <vector>
28 
29 #include <android-base/thread_annotations.h>
30 #include <ui/Fence.h>
31 
32 // TODO(b/129481165): remove the #pragma below and fix conversion issues
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wconversion"
35 #include <ui/GraphicTypes.h>
36 #pragma clang diagnostic pop
37 
38 #include <utils/StrongPointer.h>
39 #include <utils/Timers.h>
40 
41 #include "DisplayIdentification.h"
42 #include "HWC2.h"
43 #include "Hal.h"
44 
45 namespace android {
46 
47 namespace hal = hardware::graphics::composer::hal;
48 
49 struct DisplayedFrameStats;
50 class GraphicBuffer;
51 class TestableSurfaceFlinger;
52 struct CompositionInfo;
53 
54 namespace Hwc2 {
55 class Composer;
56 } // namespace Hwc2
57 
58 namespace compositionengine {
59 class Output;
60 } // namespace compositionengine
61 
62 struct KnownHWCGenericLayerMetadata {
63     const char* name;
64     const uint32_t id;
65 };
66 
67 class HWComposer {
68 public:
69     struct DeviceRequestedChanges {
70         using ChangedTypes = std::unordered_map<HWC2::Layer*, hal::Composition>;
71         using ClientTargetProperty = hal::ClientTargetProperty;
72         using DisplayRequests = hal::DisplayRequest;
73         using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
74 
75         ChangedTypes changedTypes;
76         DisplayRequests displayRequests;
77         LayerRequests layerRequests;
78         ClientTargetProperty clientTargetProperty;
79     };
80 
81     virtual ~HWComposer();
82 
83     virtual void setConfiguration(HWC2::ComposerCallback* callback, int32_t sequenceId) = 0;
84 
85     virtual bool getDisplayIdentificationData(hal::HWDisplayId hwcDisplayId, uint8_t* outPort,
86                                               DisplayIdentificationData* outData) const = 0;
87 
88     virtual bool hasCapability(hal::Capability capability) const = 0;
89     virtual bool hasDisplayCapability(DisplayId displayId,
90                                       hal::DisplayCapability capability) const = 0;
91 
92     // Attempts to allocate a virtual display and returns its ID if created on the HWC device.
93     virtual std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
94                                                             ui::PixelFormat* format) = 0;
95 
96     virtual void allocatePhysicalDisplay(hal::HWDisplayId hwcDisplayId, DisplayId displayId) = 0;
97 
98     // Attempts to create a new layer on this display
99     virtual HWC2::Layer* createLayer(DisplayId displayId) = 0;
100     // Destroy a previously created layer
101     virtual void destroyLayer(DisplayId displayId, HWC2::Layer* layer) = 0;
102 
103     // Gets any required composition change requests from the HWC device.
104     //
105     // Note that frameUsesClientComposition must be set correctly based on
106     // whether the current frame appears to use client composition. If it is
107     // false some internal optimizations are allowed to present the display
108     // with fewer handshakes, but this does not work if client composition is
109     // expected.
110     virtual status_t getDeviceCompositionChanges(
111             DisplayId, bool frameUsesClientComposition,
112             std::optional<DeviceRequestedChanges>* outChanges) = 0;
113 
114     virtual status_t setClientTarget(DisplayId displayId, uint32_t slot,
115                                      const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
116                                      ui::Dataspace dataspace) = 0;
117 
118     // Present layers to the display and read releaseFences.
119     virtual status_t presentAndGetReleaseFences(DisplayId displayId) = 0;
120 
121     // set power mode
122     virtual status_t setPowerMode(DisplayId displayId, hal::PowerMode mode) = 0;
123 
124     // Sets a color transform to be applied to the result of composition
125     virtual status_t setColorTransform(DisplayId displayId, const mat4& transform) = 0;
126 
127     // reset state when an external, non-virtual display is disconnected
128     virtual void disconnectDisplay(DisplayId displayId) = 0;
129 
130     // get the present fence received from the last call to present.
131     virtual sp<Fence> getPresentFence(DisplayId displayId) const = 0;
132 
133     // Get last release fence for the given layer
134     virtual sp<Fence> getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const = 0;
135 
136     // Set the output buffer and acquire fence for a virtual display.
137     // Returns INVALID_OPERATION if displayId is not a virtual display.
138     virtual status_t setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
139                                      const sp<GraphicBuffer>& buffer) = 0;
140 
141     // After SurfaceFlinger has retrieved the release fences for all the frames,
142     // it can call this to clear the shared pointers in the release fence map
143     virtual void clearReleaseFences(DisplayId displayId) = 0;
144 
145     // Fetches the HDR capabilities of the given display
146     virtual status_t getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) = 0;
147 
148     virtual int32_t getSupportedPerFrameMetadata(DisplayId displayId) const = 0;
149 
150     // Returns the available RenderIntent of the given display.
151     virtual std::vector<ui::RenderIntent> getRenderIntents(DisplayId displayId,
152                                                            ui::ColorMode colorMode) const = 0;
153 
154     virtual mat4 getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) = 0;
155 
156     // Returns the attributes of the color sampling engine.
157     virtual status_t getDisplayedContentSamplingAttributes(DisplayId displayId,
158                                                            ui::PixelFormat* outFormat,
159                                                            ui::Dataspace* outDataspace,
160                                                            uint8_t* outComponentMask) = 0;
161     virtual status_t setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
162                                                       uint8_t componentMask,
163                                                       uint64_t maxFrames) = 0;
164     virtual status_t getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
165                                                uint64_t timestamp,
166                                                DisplayedFrameStats* outStats) = 0;
167 
168     // Sets the brightness of a display.
169     virtual std::future<status_t> setDisplayBrightness(DisplayId displayId, float brightness) = 0;
170 
171     // Events handling ---------------------------------------------------------
172 
173     // Returns stable display ID (and display name on connection of new or previously disconnected
174     // display), or std::nullopt if hotplug event was ignored.
175     // This function is called from SurfaceFlinger.
176     virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId hwcDisplayId,
177                                                                hal::Connection connection) = 0;
178 
179     virtual bool onVsync(hal::HWDisplayId hwcDisplayId, int64_t timestamp) = 0;
180     virtual void setVsyncEnabled(DisplayId displayId, hal::Vsync enabled) = 0;
181 
182     virtual nsecs_t getRefreshTimestamp(DisplayId displayId) const = 0;
183     virtual bool isConnected(DisplayId displayId) const = 0;
184 
185     // Non-const because it can update configMap inside of mDisplayData
186     virtual std::vector<std::shared_ptr<const HWC2::Display::Config>> getConfigs(
187             DisplayId displayId) const = 0;
188 
189     virtual std::shared_ptr<const HWC2::Display::Config> getActiveConfig(
190             DisplayId displayId) const = 0;
191     virtual int getActiveConfigIndex(DisplayId displayId) const = 0;
192 
193     virtual std::vector<ui::ColorMode> getColorModes(DisplayId displayId) const = 0;
194 
195     virtual status_t setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
196                                         ui::RenderIntent renderIntent) = 0;
197 
198     virtual bool isUsingVrComposer() const = 0;
199 
200     // Composer 2.4
201     virtual DisplayConnectionType getDisplayConnectionType(DisplayId) const = 0;
202     virtual bool isVsyncPeriodSwitchSupported(DisplayId displayId) const = 0;
203     virtual nsecs_t getDisplayVsyncPeriod(DisplayId displayId) const = 0;
204     virtual status_t setActiveConfigWithConstraints(
205             DisplayId displayId, size_t configId,
206             const hal::VsyncPeriodChangeConstraints& constraints,
207             hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
208     virtual status_t setAutoLowLatencyMode(DisplayId displayId, bool on) = 0;
209     virtual status_t getSupportedContentTypes(
210             DisplayId displayId, std::vector<hal::ContentType>* outSupportedContentTypes) = 0;
211     virtual status_t setContentType(DisplayId displayId, hal::ContentType contentType) = 0;
212     virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
213             const = 0;
214 
215     // for debugging ----------------------------------------------------------
216     virtual void dump(std::string& out) const = 0;
217 
218     virtual Hwc2::Composer* getComposer() const = 0;
219 
220     // TODO(b/74619554): Remove special cases for internal/external display.
221     virtual std::optional<hal::HWDisplayId> getInternalHwcDisplayId() const = 0;
222     virtual std::optional<hal::HWDisplayId> getExternalHwcDisplayId() const = 0;
223 
224     virtual std::optional<DisplayId> toPhysicalDisplayId(hal::HWDisplayId hwcDisplayId) const = 0;
225     virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(DisplayId displayId) const = 0;
226 };
227 
228 namespace impl {
229 
230 class HWComposer final : public android::HWComposer {
231 public:
232     explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
233     explicit HWComposer(const std::string& composerServiceName);
234 
235     ~HWComposer() override;
236 
237     void setConfiguration(HWC2::ComposerCallback* callback, int32_t sequenceId) override;
238 
239     bool getDisplayIdentificationData(hal::HWDisplayId hwcDisplayId, uint8_t* outPort,
240                                       DisplayIdentificationData* outData) const override;
241 
242     bool hasCapability(hal::Capability capability) const override;
243     bool hasDisplayCapability(DisplayId displayId,
244                               hal::DisplayCapability capability) const override;
245 
246     // Attempts to allocate a virtual display and returns its ID if created on the HWC device.
247     std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
248                                                     ui::PixelFormat* format) override;
249 
250     // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
251     void allocatePhysicalDisplay(hal::HWDisplayId hwcDisplayId, DisplayId displayId) override;
252 
253     // Attempts to create a new layer on this display
254     HWC2::Layer* createLayer(DisplayId displayId) override;
255     // Destroy a previously created layer
256     void destroyLayer(DisplayId displayId, HWC2::Layer* layer) override;
257 
258     status_t getDeviceCompositionChanges(
259             DisplayId, bool frameUsesClientComposition,
260             std::optional<DeviceRequestedChanges>* outChanges) override;
261 
262     status_t setClientTarget(DisplayId displayId, uint32_t slot, const sp<Fence>& acquireFence,
263                              const sp<GraphicBuffer>& target, ui::Dataspace dataspace) override;
264 
265     // Present layers to the display and read releaseFences.
266     status_t presentAndGetReleaseFences(DisplayId displayId) override;
267 
268     // set power mode
269     status_t setPowerMode(DisplayId displayId, hal::PowerMode mode) override;
270 
271     // Sets a color transform to be applied to the result of composition
272     status_t setColorTransform(DisplayId displayId, const mat4& transform) override;
273 
274     // reset state when an external, non-virtual display is disconnected
275     void disconnectDisplay(DisplayId displayId) override;
276 
277     // get the present fence received from the last call to present.
278     sp<Fence> getPresentFence(DisplayId displayId) const override;
279 
280     // Get last release fence for the given layer
281     sp<Fence> getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const override;
282 
283     // Set the output buffer and acquire fence for a virtual display.
284     // Returns INVALID_OPERATION if displayId is not a virtual display.
285     status_t setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
286                              const sp<GraphicBuffer>& buffer) override;
287 
288     // After SurfaceFlinger has retrieved the release fences for all the frames,
289     // it can call this to clear the shared pointers in the release fence map
290     void clearReleaseFences(DisplayId displayId) override;
291 
292     // Fetches the HDR capabilities of the given display
293     status_t getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) override;
294 
295     int32_t getSupportedPerFrameMetadata(DisplayId displayId) const override;
296 
297     // Returns the available RenderIntent of the given display.
298     std::vector<ui::RenderIntent> getRenderIntents(DisplayId displayId,
299                                                    ui::ColorMode colorMode) const override;
300 
301     mat4 getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) override;
302 
303     // Returns the attributes of the color sampling engine.
304     status_t getDisplayedContentSamplingAttributes(DisplayId displayId, ui::PixelFormat* outFormat,
305                                                    ui::Dataspace* outDataspace,
306                                                    uint8_t* outComponentMask) override;
307     status_t setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
308                                               uint8_t componentMask, uint64_t maxFrames) override;
309     status_t getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames, uint64_t timestamp,
310                                        DisplayedFrameStats* outStats) override;
311     std::future<status_t> setDisplayBrightness(DisplayId displayId, float brightness) override;
312 
313     // Events handling ---------------------------------------------------------
314 
315     // Returns stable display ID (and display name on connection of new or previously disconnected
316     // display), or std::nullopt if hotplug event was ignored.
317     std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId hwcDisplayId,
318                                                        hal::Connection connection) override;
319 
320     bool onVsync(hal::HWDisplayId hwcDisplayId, int64_t timestamp) override;
321     void setVsyncEnabled(DisplayId displayId, hal::Vsync enabled) override;
322 
323     nsecs_t getRefreshTimestamp(DisplayId displayId) const override;
324     bool isConnected(DisplayId displayId) const override;
325 
326     // Non-const because it can update configMap inside of mDisplayData
327     std::vector<std::shared_ptr<const HWC2::Display::Config>> getConfigs(
328             DisplayId displayId) const override;
329 
330     std::shared_ptr<const HWC2::Display::Config> getActiveConfig(
331             DisplayId displayId) const override;
332     int getActiveConfigIndex(DisplayId displayId) const override;
333 
334     std::vector<ui::ColorMode> getColorModes(DisplayId displayId) const override;
335 
336     status_t setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
337                                 ui::RenderIntent renderIntent) override;
338 
339     bool isUsingVrComposer() const override;
340 
341     // Composer 2.4
342     DisplayConnectionType getDisplayConnectionType(DisplayId) const override;
343     bool isVsyncPeriodSwitchSupported(DisplayId displayId) const override;
344     nsecs_t getDisplayVsyncPeriod(DisplayId displayId) const override;
345     status_t setActiveConfigWithConstraints(DisplayId displayId, size_t configId,
346                                             const hal::VsyncPeriodChangeConstraints& constraints,
347                                             hal::VsyncPeriodChangeTimeline* outTimeline) override;
348     status_t setAutoLowLatencyMode(DisplayId displayId, bool) override;
349     status_t getSupportedContentTypes(DisplayId displayId, std::vector<hal::ContentType>*) override;
350     status_t setContentType(DisplayId displayId, hal::ContentType) override;
351 
352     const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
353 
354     // for debugging ----------------------------------------------------------
355     void dump(std::string& out) const override;
356 
getComposer()357     Hwc2::Composer* getComposer() const override { return mComposer.get(); }
358 
359     // TODO(b/74619554): Remove special cases for internal/external display.
getInternalHwcDisplayId()360     std::optional<hal::HWDisplayId> getInternalHwcDisplayId() const override {
361         return mInternalHwcDisplayId;
362     }
getExternalHwcDisplayId()363     std::optional<hal::HWDisplayId> getExternalHwcDisplayId() const override {
364         return mExternalHwcDisplayId;
365     }
366 
367     std::optional<DisplayId> toPhysicalDisplayId(hal::HWDisplayId hwcDisplayId) const override;
368     std::optional<hal::HWDisplayId> fromPhysicalDisplayId(DisplayId displayId) const override;
369 
370 private:
371     // For unit tests
372     friend TestableSurfaceFlinger;
373 
374     std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId hwcDisplayId);
375     std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId hwcDisplayId);
376     bool shouldIgnoreHotplugConnect(hal::HWDisplayId hwcDisplayId,
377                                     bool hasDisplayIdentificationData) const;
378 
379     void loadCapabilities();
380     void loadLayerMetadataSupport();
381     uint32_t getMaxVirtualDisplayCount() const;
382 
383     struct DisplayData {
384         bool isVirtual = false;
385         std::unique_ptr<HWC2::Display> hwcDisplay;
386         sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
387         std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
388         buffer_handle_t outbufHandle = nullptr;
389         sp<Fence> outbufAcquireFence = Fence::NO_FENCE;
390         mutable std::unordered_map<int32_t,
391                 std::shared_ptr<const HWC2::Display::Config>> configMap;
392 
393         bool validateWasSkipped;
394         hal::Error presentError;
395 
396         bool vsyncTraceToggle = false;
397 
398         std::mutex vsyncEnabledLock;
399         hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
400 
401         mutable std::mutex lastHwVsyncLock;
402         nsecs_t lastHwVsync GUARDED_BY(lastHwVsyncLock) = 0;
403     };
404 
405     std::unordered_map<DisplayId, DisplayData> mDisplayData;
406 
407     std::unique_ptr<android::Hwc2::Composer> mComposer;
408     std::unordered_set<hal::Capability> mCapabilities;
409     std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
410     bool mRegisteredCallback = false;
411 
412     std::unordered_map<hal::HWDisplayId, DisplayId> mPhysicalDisplayIdMap;
413     std::optional<hal::HWDisplayId> mInternalHwcDisplayId;
414     std::optional<hal::HWDisplayId> mExternalHwcDisplayId;
415     bool mHasMultiDisplaySupport = false;
416 
417     std::unordered_set<DisplayId> mFreeVirtualDisplayIds;
418     uint32_t mNextVirtualDisplayId = 0;
419     uint32_t mRemainingHwcVirtualDisplays{getMaxVirtualDisplayCount()};
420 };
421 
422 } // namespace impl
423 } // namespace android
424 
425 #endif // ANDROID_SF_HWCOMPOSER_H
426