HWC2.h revision 0c6ce4608fc022608537162dd919e7f729b9cb77
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 <math/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#include <map>
39
40namespace android {
41    class Fence;
42    class FloatRect;
43    class GraphicBuffer;
44    class Rect;
45    class Region;
46    namespace Hwc2 {
47        class Composer;
48    }
49}
50
51namespace HWC2 {
52
53class Display;
54class Layer;
55
56typedef std::function<void(std::shared_ptr<Display>, Connection)>
57        HotplugCallback;
58typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
59typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
60
61// C++ Wrapper around hwc2_device_t. Load all functions pointers
62// and handle callback registration.
63class Device
64{
65public:
66    // useVrComposer is passed to the composer HAL. When true, the composer HAL
67    // will use the vr composer service, otherwise it uses the real hardware
68    // composer.
69    Device(bool useVrComposer);
70    ~Device();
71
72    friend class HWC2::Display;
73    friend class HWC2::Layer;
74
75    // Required by HWC2
76
77    std::string dump() const;
78
79    const std::unordered_set<Capability>& getCapabilities() const {
80        return mCapabilities;
81    };
82
83    uint32_t getMaxVirtualDisplayCount() const;
84    Error createVirtualDisplay(uint32_t width, uint32_t height,
85            android_pixel_format_t* format,
86            std::shared_ptr<Display>* outDisplay);
87
88    void registerHotplugCallback(HotplugCallback hotplug);
89    void registerRefreshCallback(RefreshCallback refresh);
90    void registerVsyncCallback(VsyncCallback vsync);
91
92    // For use by callbacks
93
94    void callHotplug(std::shared_ptr<Display> display, Connection connected);
95    void callRefresh(std::shared_ptr<Display> display);
96    void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
97
98    // Other Device methods
99
100    // This will create a Display if one is not found, but it will not be marked
101    // as connected. This Display may be null if the display has been torn down
102    // but has not been removed from the map yet.
103    std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
104
105    bool hasCapability(HWC2::Capability capability) const;
106
107    android::Hwc2::Composer* getComposer() { return mComposer.get(); }
108
109private:
110    // Initialization methods
111
112    void loadCapabilities();
113    void registerCallbacks();
114
115    // For use by Display
116
117    void destroyVirtualDisplay(hwc2_display_t display);
118
119    // Member variables
120    std::unique_ptr<android::Hwc2::Composer> mComposer;
121
122    std::unordered_set<Capability> mCapabilities;
123    std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
124
125    HotplugCallback mHotplug;
126    std::vector<std::pair<std::shared_ptr<Display>, Connection>>
127            mPendingHotplugs;
128    RefreshCallback mRefresh;
129    std::vector<std::shared_ptr<Display>> mPendingRefreshes;
130    VsyncCallback mVsync;
131    std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
132};
133
134// Convenience C++ class to access hwc2_device_t Display functions directly.
135class Display : public std::enable_shared_from_this<Display>
136{
137public:
138    Display(Device& device, hwc2_display_t id);
139    ~Display();
140
141    friend class HWC2::Device;
142    friend class HWC2::Layer;
143
144    class Config
145    {
146    public:
147        class Builder
148        {
149        public:
150            Builder(Display& display, hwc2_config_t id);
151
152            std::shared_ptr<const Config> build() {
153                return std::const_pointer_cast<const Config>(
154                        std::move(mConfig));
155            }
156
157            Builder& setWidth(int32_t width) {
158                mConfig->mWidth = width;
159                return *this;
160            }
161            Builder& setHeight(int32_t height) {
162                mConfig->mHeight = height;
163                return *this;
164            }
165            Builder& setVsyncPeriod(int32_t vsyncPeriod) {
166                mConfig->mVsyncPeriod = vsyncPeriod;
167                return *this;
168            }
169            Builder& setDpiX(int32_t dpiX) {
170                if (dpiX == -1) {
171                    mConfig->mDpiX = getDefaultDensity();
172                } else {
173                    mConfig->mDpiX = dpiX / 1000.0f;
174                }
175                return *this;
176            }
177            Builder& setDpiY(int32_t dpiY) {
178                if (dpiY == -1) {
179                    mConfig->mDpiY = getDefaultDensity();
180                } else {
181                    mConfig->mDpiY = dpiY / 1000.0f;
182                }
183                return *this;
184            }
185
186        private:
187            float getDefaultDensity();
188            std::shared_ptr<Config> mConfig;
189        };
190
191        hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
192        hwc2_config_t getId() const { return mId; }
193
194        int32_t getWidth() const { return mWidth; }
195        int32_t getHeight() const { return mHeight; }
196        nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
197        float getDpiX() const { return mDpiX; }
198        float getDpiY() const { return mDpiY; }
199
200    private:
201        Config(Display& display, hwc2_config_t id);
202
203        Display& mDisplay;
204        hwc2_config_t mId;
205
206        int32_t mWidth;
207        int32_t mHeight;
208        nsecs_t mVsyncPeriod;
209        float mDpiX;
210        float mDpiY;
211    };
212
213    // Required by HWC2
214
215    [[clang::warn_unused_result]] Error acceptChanges();
216    [[clang::warn_unused_result]] Error createLayer(
217            std::shared_ptr<Layer>* outLayer);
218    [[clang::warn_unused_result]] Error getActiveConfig(
219            std::shared_ptr<const Config>* outConfig) const;
220    [[clang::warn_unused_result]] Error getChangedCompositionTypes(
221            std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
222    [[clang::warn_unused_result]] Error getColorModes(
223            std::vector<android_color_mode_t>* outModes) const;
224
225    // Doesn't call into the HWC2 device, so no errors are possible
226    std::vector<std::shared_ptr<const Config>> getConfigs() const;
227
228    [[clang::warn_unused_result]] Error getName(std::string* outName) const;
229    [[clang::warn_unused_result]] Error getRequests(
230            DisplayRequest* outDisplayRequests,
231            std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
232                    outLayerRequests);
233    [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
234    [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
235    [[clang::warn_unused_result]] Error getHdrCapabilities(
236            std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
237    [[clang::warn_unused_result]] Error getReleaseFences(
238            std::unordered_map<std::shared_ptr<Layer>,
239                    android::sp<android::Fence>>* outFences) const;
240    [[clang::warn_unused_result]] Error present(
241            android::sp<android::Fence>* outPresentFence);
242    [[clang::warn_unused_result]] Error setActiveConfig(
243            const std::shared_ptr<const Config>& config);
244    [[clang::warn_unused_result]] Error setClientTarget(
245            uint32_t slot, const android::sp<android::GraphicBuffer>& target,
246            const android::sp<android::Fence>& acquireFence,
247            android_dataspace_t dataspace);
248    [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
249    [[clang::warn_unused_result]] Error setColorTransform(
250            const android::mat4& matrix, android_color_transform_t hint);
251    [[clang::warn_unused_result]] Error setOutputBuffer(
252            const android::sp<android::GraphicBuffer>& buffer,
253            const android::sp<android::Fence>& releaseFence);
254    [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
255    [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
256    [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
257            uint32_t* outNumRequests);
258    [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
259                                                 uint32_t* outNumRequests,
260                                                          android::sp<android::Fence>* outPresentFence, uint32_t* state);
261
262    // Most methods in this class write a command to a command buffer.  The
263    // command buffer is implicitly submitted in validate, present, and
264    // presentOrValidate.  This method provides a way to discard the commands,
265    // which can be used to discard stale commands.
266    void discardCommands();
267
268    // Other Display methods
269
270    Device& getDevice() const { return mDevice; }
271    hwc2_display_t getId() const { return mId; }
272    bool isConnected() const { return mIsConnected; }
273
274private:
275    // For use by Device
276
277    void setConnected(bool connected) { mIsConnected = connected; }
278    int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
279    void loadConfig(hwc2_config_t configId);
280    void loadConfigs();
281
282    // For use by Layer
283    void destroyLayer(hwc2_layer_t layerId);
284
285    // This may fail (and return a null pointer) if no layer with this ID exists
286    // on this display
287    std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
288
289    // Member variables
290
291    Device& mDevice;
292    hwc2_display_t mId;
293    bool mIsConnected;
294    DisplayType mType;
295    std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
296    // The ordering in this map matters, for getConfigs(), when it is
297    // converted to a vector
298    std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
299};
300
301// Convenience C++ class to access hwc2_device_t Layer functions directly.
302class Layer
303{
304public:
305    Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
306    ~Layer();
307
308    bool isAbandoned() const { return mDisplay.expired(); }
309    hwc2_layer_t getId() const { return mId; }
310
311    [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
312    [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
313            const android::sp<android::GraphicBuffer>& buffer,
314            const android::sp<android::Fence>& acquireFence);
315    [[clang::warn_unused_result]] Error setSurfaceDamage(
316            const android::Region& damage);
317
318    [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
319    [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
320    [[clang::warn_unused_result]] Error setCompositionType(Composition type);
321    [[clang::warn_unused_result]] Error setDataspace(
322            android_dataspace_t dataspace);
323    [[clang::warn_unused_result]] Error setDisplayFrame(
324            const android::Rect& frame);
325    [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
326    [[clang::warn_unused_result]] Error setSidebandStream(
327            const native_handle_t* stream);
328    [[clang::warn_unused_result]] Error setSourceCrop(
329            const android::FloatRect& crop);
330    [[clang::warn_unused_result]] Error setTransform(Transform transform);
331    [[clang::warn_unused_result]] Error setVisibleRegion(
332            const android::Region& region);
333    [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
334    [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
335
336private:
337    std::weak_ptr<Display> mDisplay;
338    hwc2_display_t mDisplayId;
339    Device& mDevice;
340    hwc2_layer_t mId;
341};
342
343} // namespace HWC2
344
345#endif // ANDROID_SF_HWC2_H
346