HWC2.h revision b7432cc57cd957fb18f68d7976c5829b3a3a7751
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#ifndef ANDROID_SF_HWC2_H
18#define ANDROID_SF_HWC2_H
19
20#define HWC2_INCLUDE_STRINGIFICATION
21#define HWC2_USE_CPP11
22#include <hardware/hwcomposer2.h>
23#undef HWC2_INCLUDE_STRINGIFICATION
24#undef HWC2_USE_CPP11
25
26#include <ui/HdrCapabilities.h>
27#include <ui/mat4.h>
28
29#include <utils/Log.h>
30#include <utils/StrongPointer.h>
31#include <utils/Timers.h>
32
33#include <functional>
34#include <string>
35#include <unordered_map>
36#include <unordered_set>
37#include <vector>
38
39namespace android {
40    class Fence;
41    class FloatRect;
42    class GraphicBuffer;
43    class Rect;
44    class Region;
45    namespace Hwc2 {
46        class Composer;
47    };
48}
49
50namespace HWC2 {
51
52class Display;
53class Layer;
54
55typedef std::function<void(std::shared_ptr<Display>, Connection)>
56        HotplugCallback;
57typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
58typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
59
60class Device
61{
62public:
63#ifdef BYPASS_IHWC
64    explicit Device(hwc2_device_t* device);
65#else
66    Device();
67#endif
68    ~Device();
69
70    friend class HWC2::Display;
71    friend class HWC2::Layer;
72
73    // Required by HWC2
74
75    std::string dump() const;
76
77    const std::unordered_set<Capability>& getCapabilities() const {
78        return mCapabilities;
79    };
80
81    uint32_t getMaxVirtualDisplayCount() const;
82    Error createVirtualDisplay(uint32_t width, uint32_t height,
83            android_pixel_format_t* format,
84            std::shared_ptr<Display>* outDisplay);
85
86    void registerHotplugCallback(HotplugCallback hotplug);
87    void registerRefreshCallback(RefreshCallback refresh);
88    void registerVsyncCallback(VsyncCallback vsync);
89
90    // For use by callbacks
91
92    void callHotplug(std::shared_ptr<Display> display, Connection connected);
93    void callRefresh(std::shared_ptr<Display> display);
94    void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
95
96    // Other Device methods
97
98    // This will create a Display if one is not found, but it will not be marked
99    // as connected. This Display may be null if the display has been torn down
100    // but has not been removed from the map yet.
101    std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
102
103    bool hasCapability(HWC2::Capability capability) const;
104
105private:
106    // Initialization methods
107
108#ifdef BYPASS_IHWC
109    template <typename PFN>
110    [[clang::warn_unused_result]] bool loadFunctionPointer(
111            FunctionDescriptor desc, PFN& outPFN) {
112        auto intDesc = static_cast<int32_t>(desc);
113        auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
114        if (pfn != nullptr) {
115            outPFN = reinterpret_cast<PFN>(pfn);
116            return true;
117        } else {
118            ALOGE("Failed to load function %s", to_string(desc).c_str());
119            return false;
120        }
121    }
122
123    template <typename PFN, typename HOOK>
124    void registerCallback(Callback callback, HOOK hook) {
125        static_assert(std::is_same<PFN, HOOK>::value,
126                "Incompatible function pointer");
127        auto intCallback = static_cast<int32_t>(callback);
128        auto callbackData = static_cast<hwc2_callback_data_t>(this);
129        auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
130        mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
131    }
132#endif
133
134    void loadCapabilities();
135    void loadFunctionPointers();
136    void registerCallbacks();
137
138    // For use by Display
139
140    void destroyVirtualDisplay(hwc2_display_t display);
141
142    // Member variables
143
144#ifdef BYPASS_IHWC
145    hwc2_device_t* mHwcDevice;
146
147    // Device function pointers
148    HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
149    HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
150    HWC2_PFN_DUMP mDump;
151    HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
152    HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
153
154    // Display function pointers
155    HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
156    HWC2_PFN_CREATE_LAYER mCreateLayer;
157    HWC2_PFN_DESTROY_LAYER mDestroyLayer;
158    HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
159    HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
160    HWC2_PFN_GET_COLOR_MODES mGetColorModes;
161    HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
162    HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
163    HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
164    HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
165    HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
166    HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
167    HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
168    HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
169    HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
170    HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
171    HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
172    HWC2_PFN_SET_COLOR_MODE mSetColorMode;
173    HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
174    HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
175    HWC2_PFN_SET_POWER_MODE mSetPowerMode;
176    HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
177    HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
178
179    // Layer function pointers
180    HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
181    HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
182    HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
183    HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
184    HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
185    HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
186    HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
187    HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
188    HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
189    HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
190    HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
191    HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
192    HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
193    HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
194#else
195    std::unique_ptr<android::Hwc2::Composer> mComposer;
196#endif // BYPASS_IHWC
197
198    std::unordered_set<Capability> mCapabilities;
199    std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
200
201    HotplugCallback mHotplug;
202    std::vector<std::pair<std::shared_ptr<Display>, Connection>>
203            mPendingHotplugs;
204    RefreshCallback mRefresh;
205    std::vector<std::shared_ptr<Display>> mPendingRefreshes;
206    VsyncCallback mVsync;
207    std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
208};
209
210class Display : public std::enable_shared_from_this<Display>
211{
212public:
213    Display(Device& device, hwc2_display_t id);
214    ~Display();
215
216    friend class HWC2::Device;
217    friend class HWC2::Layer;
218
219    class Config
220    {
221    public:
222        class Builder
223        {
224        public:
225            Builder(Display& display, hwc2_config_t id);
226
227            std::shared_ptr<const Config> build() {
228                return std::const_pointer_cast<const Config>(
229                        std::move(mConfig));
230            }
231
232            Builder& setWidth(int32_t width) {
233                mConfig->mWidth = width;
234                return *this;
235            }
236            Builder& setHeight(int32_t height) {
237                mConfig->mHeight = height;
238                return *this;
239            }
240            Builder& setVsyncPeriod(int32_t vsyncPeriod) {
241                mConfig->mVsyncPeriod = vsyncPeriod;
242                return *this;
243            }
244            Builder& setDpiX(int32_t dpiX) {
245                if (dpiX == -1) {
246                    mConfig->mDpiX = getDefaultDensity();
247                } else {
248                    mConfig->mDpiX = dpiX / 1000.0f;
249                }
250                return *this;
251            }
252            Builder& setDpiY(int32_t dpiY) {
253                if (dpiY == -1) {
254                    mConfig->mDpiY = getDefaultDensity();
255                } else {
256                    mConfig->mDpiY = dpiY / 1000.0f;
257                }
258                return *this;
259            }
260
261        private:
262            float getDefaultDensity();
263            std::shared_ptr<Config> mConfig;
264        };
265
266        hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
267        hwc2_config_t getId() const { return mId; }
268
269        int32_t getWidth() const { return mWidth; }
270        int32_t getHeight() const { return mHeight; }
271        nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
272        float getDpiX() const { return mDpiX; }
273        float getDpiY() const { return mDpiY; }
274
275    private:
276        Config(Display& display, hwc2_config_t id);
277
278        Display& mDisplay;
279        hwc2_config_t mId;
280
281        int32_t mWidth;
282        int32_t mHeight;
283        nsecs_t mVsyncPeriod;
284        float mDpiX;
285        float mDpiY;
286    };
287
288    // Required by HWC2
289
290    [[clang::warn_unused_result]] Error acceptChanges();
291    [[clang::warn_unused_result]] Error createLayer(
292            std::shared_ptr<Layer>* outLayer);
293    [[clang::warn_unused_result]] Error getActiveConfig(
294            std::shared_ptr<const Config>* outConfig) const;
295    [[clang::warn_unused_result]] Error getChangedCompositionTypes(
296            std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
297    [[clang::warn_unused_result]] Error getColorModes(
298            std::vector<android_color_mode_t>* outModes) const;
299
300    // Doesn't call into the HWC2 device, so no errors are possible
301    std::vector<std::shared_ptr<const Config>> getConfigs() const;
302
303    [[clang::warn_unused_result]] Error getName(std::string* outName) const;
304    [[clang::warn_unused_result]] Error getRequests(
305            DisplayRequest* outDisplayRequests,
306            std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
307                    outLayerRequests);
308    [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
309    [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
310    [[clang::warn_unused_result]] Error getHdrCapabilities(
311            std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
312    [[clang::warn_unused_result]] Error getReleaseFences(
313            std::unordered_map<std::shared_ptr<Layer>,
314                    android::sp<android::Fence>>* outFences) const;
315    [[clang::warn_unused_result]] Error present(
316            android::sp<android::Fence>* outRetireFence);
317    [[clang::warn_unused_result]] Error setActiveConfig(
318            const std::shared_ptr<const Config>& config);
319    [[clang::warn_unused_result]] Error setClientTarget(
320            buffer_handle_t target,
321            const android::sp<android::Fence>& acquireFence,
322            android_dataspace_t dataspace);
323    [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
324    [[clang::warn_unused_result]] Error setColorTransform(
325            const android::mat4& matrix, android_color_transform_t hint);
326    [[clang::warn_unused_result]] Error setOutputBuffer(
327            const android::sp<android::GraphicBuffer>& buffer,
328            const android::sp<android::Fence>& releaseFence);
329    [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
330    [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
331    [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
332            uint32_t* outNumRequests);
333
334    // Other Display methods
335
336    Device& getDevice() const { return mDevice; }
337    hwc2_display_t getId() const { return mId; }
338    bool isConnected() const { return mIsConnected; }
339
340private:
341    // For use by Device
342
343    // Virtual displays are always connected
344    void setVirtual() {
345        mIsVirtual = true;
346        mIsConnected = true;
347    }
348
349    void setConnected(bool connected) { mIsConnected = connected; }
350    int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
351    void loadConfig(hwc2_config_t configId);
352    void loadConfigs();
353
354    // For use by Layer
355    void destroyLayer(hwc2_layer_t layerId);
356
357    // This may fail (and return a null pointer) if no layer with this ID exists
358    // on this display
359    std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
360
361    // Member variables
362
363    Device& mDevice;
364    hwc2_display_t mId;
365    bool mIsConnected;
366    bool mIsVirtual;
367    std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
368    std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
369};
370
371class Layer
372{
373public:
374    Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
375    ~Layer();
376
377    bool isAbandoned() const { return mDisplay.expired(); }
378    hwc2_layer_t getId() const { return mId; }
379
380    [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
381    [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
382            const android::sp<android::Fence>& acquireFence);
383    [[clang::warn_unused_result]] Error setSurfaceDamage(
384            const android::Region& damage);
385
386    [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
387    [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
388    [[clang::warn_unused_result]] Error setCompositionType(Composition type);
389    [[clang::warn_unused_result]] Error setDataspace(
390            android_dataspace_t dataspace);
391    [[clang::warn_unused_result]] Error setDisplayFrame(
392            const android::Rect& frame);
393    [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
394    [[clang::warn_unused_result]] Error setSidebandStream(
395            const native_handle_t* stream);
396    [[clang::warn_unused_result]] Error setSourceCrop(
397            const android::FloatRect& crop);
398    [[clang::warn_unused_result]] Error setTransform(Transform transform);
399    [[clang::warn_unused_result]] Error setVisibleRegion(
400            const android::Region& region);
401    [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
402
403private:
404    std::weak_ptr<Display> mDisplay;
405    hwc2_display_t mDisplayId;
406    Device& mDevice;
407    hwc2_layer_t mId;
408};
409
410} // namespace HWC2
411
412#endif // ANDROID_SF_HWC2_H
413