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