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