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