• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 <android-base/expected.h>
20 #include <android-base/thread_annotations.h>
21 #include <ftl/expected.h>
22 #include <ftl/future.h>
23 #include <gui/HdrMetadata.h>
24 #include <math/mat4.h>
25 #include <ui/HdrCapabilities.h>
26 #include <ui/Region.h>
27 #include <ui/StaticDisplayInfo.h>
28 #include <utils/Log.h>
29 #include <utils/StrongPointer.h>
30 #include <utils/Timers.h>
31 
32 #include <functional>
33 #include <string>
34 #include <unordered_map>
35 #include <unordered_set>
36 #include <vector>
37 
38 #include "ComposerHal.h"
39 #include "Hal.h"
40 
41 #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
42 #include <aidl/android/hardware/graphics/common/DisplayHotplugEvent.h>
43 #include <aidl/android/hardware/graphics/composer3/Capability.h>
44 #include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
45 #include <aidl/android/hardware/graphics/composer3/Color.h>
46 #include <aidl/android/hardware/graphics/composer3/Composition.h>
47 #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
48 #include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
49 #include <aidl/android/hardware/graphics/composer3/RefreshRateChangedDebugData.h>
50 
51 namespace android {
52 
53 class Fence;
54 class FloatRect;
55 class GraphicBuffer;
56 class TestableSurfaceFlinger;
57 struct DisplayedFrameStats;
58 
59 namespace Hwc2 {
60 class Composer;
61 } // namespace Hwc2
62 
63 namespace HWC2 {
64 
65 class Layer;
66 
67 namespace hal = android::hardware::graphics::composer::hal;
68 
69 using aidl::android::hardware::graphics::common::DisplayHotplugEvent;
70 using aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
71 
72 // Implement this interface to receive hardware composer events.
73 //
74 // These callback functions will generally be called on a hwbinder thread, but
75 // when first registering the callback the onComposerHalHotplugEvent() function
76 // will immediately be called on the thread calling registerCallback().
77 struct ComposerCallback {
78     virtual void onComposerHalHotplugEvent(hal::HWDisplayId, DisplayHotplugEvent) = 0;
79     virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
80     virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
81                                     std::optional<hal::VsyncPeriodNanos>) = 0;
82     virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
83                                                        const hal::VsyncPeriodChangeTimeline&) = 0;
84     virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
85     virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
86     virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
87 
88 protected:
89     ~ComposerCallback() = default;
90 };
91 
92 // Convenience C++ class to access per display functions directly.
93 class Display {
94 public:
95     virtual ~Display();
96 
97     virtual hal::HWDisplayId getId() const = 0;
98     virtual bool isConnected() const = 0;
99     virtual void setConnected(bool connected) = 0; // For use by HWComposer only
100     virtual bool hasCapability(
101             aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
102     virtual bool isVsyncPeriodSwitchSupported() const = 0;
103     virtual bool hasDisplayIdleTimerCapability() const = 0;
104     virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
105 
106     [[nodiscard]] virtual hal::Error acceptChanges() = 0;
107     [[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
108     createLayer() = 0;
109     [[nodiscard]] virtual hal::Error getChangedCompositionTypes(
110             std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
111                     outTypes) = 0;
112     [[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
113     // Returns a bitmask which contains HdrMetadata::Type::*.
114     [[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
115     [[nodiscard]] virtual hal::Error getRenderIntents(
116             hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
117     [[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
118                                                                   android::mat4* outMatrix) = 0;
119 
120     [[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
121     [[nodiscard]] virtual hal::Error getRequests(
122             hal::DisplayRequest* outDisplayRequests,
123             std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
124     [[nodiscard]] virtual ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType()
125             const = 0;
126     [[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
127     [[nodiscard]] virtual hal::Error getHdrCapabilities(
128             android::HdrCapabilities* outCapabilities) const = 0;
129     [[nodiscard]] virtual hal::Error getOverlaySupport(
130             aidl::android::hardware::graphics::composer3::OverlayProperties* outProperties)
131             const = 0;
132     [[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
133             hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
134             uint8_t* outComponentMask) const = 0;
135     [[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
136                                                                       uint8_t componentMask,
137                                                                       uint64_t maxFrames) const = 0;
138     [[nodiscard]] virtual hal::Error getDisplayedContentSample(
139             uint64_t maxFrames, uint64_t timestamp,
140             android::DisplayedFrameStats* outStats) const = 0;
141     [[nodiscard]] virtual hal::Error getReleaseFences(
142             std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
143     [[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
144     [[nodiscard]] virtual hal::Error setClientTarget(
145             uint32_t slot, const android::sp<android::GraphicBuffer>& target,
146             const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace,
147             float hdrSdrRatio) = 0;
148     [[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
149                                                   hal::RenderIntent renderIntent) = 0;
150     [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
151     [[nodiscard]] virtual hal::Error setOutputBuffer(
152             const android::sp<android::GraphicBuffer>& buffer,
153             const android::sp<android::Fence>& releaseFence) = 0;
154     [[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
155     [[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
156     [[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
157                                               uint32_t* outNumTypes, uint32_t* outNumRequests) = 0;
158     [[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
159                                                        int32_t frameIntervalNs,
160                                                        uint32_t* outNumTypes,
161                                                        uint32_t* outNumRequests,
162                                                        android::sp<android::Fence>* outPresentFence,
163                                                        uint32_t* state) = 0;
164     [[nodiscard]] virtual ftl::Future<hal::Error> setDisplayBrightness(
165             float brightness, float brightnessNits,
166             const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
167     [[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
168             hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
169             hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
170     [[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
171     [[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
172     [[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
173             hal::HWConfigId* configId) const = 0;
174     [[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
175     [[nodiscard]] virtual hal::Error getSupportedContentTypes(
176             std::vector<hal::ContentType>*) const = 0;
177     [[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
178     [[nodiscard]] virtual hal::Error getClientTargetProperty(
179             aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
180                     outClientTargetProperty) = 0;
181     [[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
182             std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
183                     support) = 0;
184     [[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
185     [[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
186             Hwc2::AidlTransform* outTransform) const = 0;
187 };
188 
189 namespace impl {
190 
191 class Layer;
192 
193 class Display : public HWC2::Display {
194 public:
195     Display(android::Hwc2::Composer&,
196             const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
197             hal::HWDisplayId, hal::DisplayType);
198     ~Display() override;
199 
200     // Required by HWC2
201     hal::Error acceptChanges() override;
202     base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
203     hal::Error getChangedCompositionTypes(
204             std::unordered_map<HWC2::Layer*,
205                                aidl::android::hardware::graphics::composer3::Composition>* outTypes)
206             override;
207     hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
208     // Returns a bitmask which contains HdrMetadata::Type::*.
209     int32_t getSupportedPerFrameMetadata() const override;
210     hal::Error getRenderIntents(hal::ColorMode colorMode,
211                                 std::vector<hal::RenderIntent>* outRenderIntents) const override;
212     hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
213 
214     hal::Error getName(std::string* outName) const override;
215     hal::Error getRequests(
216             hal::DisplayRequest* outDisplayRequests,
217             std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
218     ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType() const override;
219     hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
220     hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
221     hal::Error getOverlaySupport(aidl::android::hardware::graphics::composer3::OverlayProperties*
222                                          outProperties) const override;
223     hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
224                                                      hal::Dataspace* outDataspace,
225                                                      uint8_t* outComponentMask) const override;
226     hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
227                                                 uint64_t maxFrames) const override;
228     hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
229                                          android::DisplayedFrameStats* outStats) const override;
230     hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
231                                         outFences) const override;
232     hal::Error present(android::sp<android::Fence>* outPresentFence) override;
233     hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
234                                const android::sp<android::Fence>& acquireFence,
235                                hal::Dataspace dataspace, float hdrSdrRatio) override;
236     hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
237     hal::Error setColorTransform(const android::mat4& matrix) override;
238     hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
239                                const android::sp<android::Fence>& releaseFence) override;
240     hal::Error setPowerMode(hal::PowerMode) override;
241     hal::Error setVsyncEnabled(hal::Vsync enabled) override;
242     hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs, uint32_t* outNumTypes,
243                         uint32_t* outNumRequests) override;
244     hal::Error presentOrValidate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
245                                  uint32_t* outNumTypes, uint32_t* outNumRequests,
246                                  android::sp<android::Fence>* outPresentFence,
247                                  uint32_t* state) override;
248     ftl::Future<hal::Error> setDisplayBrightness(
249             float brightness, float brightnessNits,
250             const Hwc2::Composer::DisplayBrightnessOptions& options) override;
251     hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
252                                               const hal::VsyncPeriodChangeConstraints& constraints,
253                                               hal::VsyncPeriodChangeTimeline* outTimeline) override;
254     hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
255     hal::Error clearBootDisplayConfig() override;
256     hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
257     hal::Error setAutoLowLatencyMode(bool on) override;
258     hal::Error getSupportedContentTypes(
259             std::vector<hal::ContentType>* outSupportedContentTypes) const override;
260     hal::Error setContentType(hal::ContentType) override;
261     hal::Error getClientTargetProperty(
262             aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
263                     outClientTargetProperty) override;
264     hal::Error getDisplayDecorationSupport(
265             std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
266                     support) override;
267     hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
268 
269     // Other Display methods
getId()270     hal::HWDisplayId getId() const override { return mId; }
isConnected()271     bool isConnected() const override { return mIsConnected; }
272     void setConnected(bool connected) override;
273     bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
274             const override EXCLUDES(mDisplayCapabilitiesMutex);
275     bool isVsyncPeriodSwitchSupported() const override;
276     bool hasDisplayIdleTimerCapability() const override;
277     void onLayerDestroyed(hal::HWLayerId layerId) override;
278     hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
279 
280 private:
281     void loadDisplayCapabilities();
282 
283     // This may fail (and return a null pointer) if no layer with this ID exists
284     // on this display
285     std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
286 
287     friend android::TestableSurfaceFlinger;
288 
289     // Member variables
290 
291     // These are references to data owned by HWComposer, which will outlive
292     // this HWC2::Display, so these references are guaranteed to be valid for
293     // the lifetime of this object.
294     android::Hwc2::Composer& mComposer;
295     const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
296             mCapabilities;
297 
298     const hal::HWDisplayId mId;
299     hal::DisplayType mType;
300     // Cached on first call to getConnectionType.
301     mutable std::optional<ftl::Expected<ui::DisplayConnectionType, hal::Error>> mConnectionType;
302     bool mIsConnected = false;
303 
304     using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
305     Layers mLayers;
306 
307     mutable std::mutex mDisplayCapabilitiesMutex;
308     std::once_flag mDisplayCapabilityQueryFlag;
309     std::optional<
310             std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
311             mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
312 };
313 
314 } // namespace impl
315 
316 class Layer {
317 public:
318     virtual ~Layer();
319 
320     virtual hal::HWLayerId getId() const = 0;
321 
322     [[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
323     [[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
324                                                const android::sp<android::GraphicBuffer>& buffer,
325                                                const android::sp<android::Fence>& acquireFence) = 0;
326     [[nodiscard]] virtual hal::Error setBufferSlotsToClear(
327             const std::vector<uint32_t>& slotsToClear, uint32_t activeBufferSlot) = 0;
328     [[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
329 
330     [[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
331     [[nodiscard]] virtual hal::Error setColor(
332             aidl::android::hardware::graphics::composer3::Color color) = 0;
333     [[nodiscard]] virtual hal::Error setCompositionType(
334             aidl::android::hardware::graphics::composer3::Composition type) = 0;
335     [[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
336     [[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
337                                                          const android::HdrMetadata& metadata) = 0;
338     [[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
339     [[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
340     [[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
341     [[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
342     [[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
343     [[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
344     [[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
345 
346     // Composer HAL 2.3
347     [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
348 
349     // Composer HAL 2.4
350     [[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
351                                                              bool mandatory,
352                                                              const std::vector<uint8_t>& value) = 0;
353 
354     // AIDL HAL
355     [[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
356     [[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
357 };
358 
359 namespace impl {
360 
361 // Convenience C++ class to access per layer functions directly.
362 
363 class Layer : public HWC2::Layer {
364 public:
365     Layer(android::Hwc2::Composer& composer,
366           const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
367                   capabilities,
368           HWC2::Display& display, hal::HWLayerId layerId);
369     ~Layer() override;
370 
371     void onOwningDisplayDestroyed();
372 
getId()373     hal::HWLayerId getId() const override { return mId; }
374 
375     hal::Error setCursorPosition(int32_t x, int32_t y) override;
376     hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
377                          const android::sp<android::Fence>& acquireFence) override;
378     hal::Error setBufferSlotsToClear(const std::vector<uint32_t>& slotsToClear,
379                                      uint32_t activeBufferSlot) override;
380     hal::Error setSurfaceDamage(const android::Region& damage) override;
381 
382     hal::Error setBlendMode(hal::BlendMode mode) override;
383     hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
384     hal::Error setCompositionType(
385             aidl::android::hardware::graphics::composer3::Composition type) override;
386     hal::Error setDataspace(hal::Dataspace dataspace) override;
387     hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
388                                    const android::HdrMetadata& metadata) override;
389     hal::Error setDisplayFrame(const android::Rect& frame) override;
390     hal::Error setPlaneAlpha(float alpha) override;
391     hal::Error setSidebandStream(const native_handle_t* stream) override;
392     hal::Error setSourceCrop(const android::FloatRect& crop) override;
393     hal::Error setTransform(hal::Transform transform) override;
394     hal::Error setVisibleRegion(const android::Region& region) override;
395     hal::Error setZOrder(uint32_t z) override;
396 
397     // Composer HAL 2.3
398     hal::Error setColorTransform(const android::mat4& matrix) override;
399 
400     // Composer HAL 2.4
401     hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
402                                        const std::vector<uint8_t>& value) override;
403 
404     // AIDL HAL
405     hal::Error setBrightness(float brightness) override;
406     hal::Error setBlockingRegion(const android::Region& region) override;
407 
408 private:
409     // These are references to data owned by HWComposer, which will outlive
410     // this HWC2::Layer, so these references are guaranteed to be valid for
411     // the lifetime of this object.
412     android::Hwc2::Composer& mComposer;
413     const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
414             mCapabilities;
415 
416     HWC2::Display* mDisplay;
417     hal::HWLayerId mId;
418 
419     // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
420     // multiple times.
421     android::Region mVisibleRegion = android::Region::INVALID_REGION;
422     android::Region mDamageRegion = android::Region::INVALID_REGION;
423     android::Region mBlockingRegion = android::Region::INVALID_REGION;
424     hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
425     android::HdrMetadata mHdrMetadata;
426     android::mat4 mColorMatrix;
427     uint32_t mBufferSlot;
428 };
429 
430 } // namespace impl
431 } // namespace HWC2
432 } // namespace android
433