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