HWComposer.h revision e89987affd9a40f9616a696a3c0bfbb94274603e
1/*
2 * Copyright (C) 2010 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 USE_HWC2
18#include "HWComposer_hwc1.h"
19#else
20
21#ifndef ANDROID_SF_HWCOMPOSER_H
22#define ANDROID_SF_HWCOMPOSER_H
23
24#include "HWC2.h"
25
26#include <stdint.h>
27#include <sys/types.h>
28
29#include <ui/Fence.h>
30
31#include <utils/BitSet.h>
32#include <utils/Condition.h>
33#include <utils/Mutex.h>
34#include <utils/StrongPointer.h>
35#include <utils/Thread.h>
36#include <utils/Timers.h>
37#include <utils/Vector.h>
38
39#include <memory>
40#include <set>
41#include <vector>
42
43extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
44                           const struct timespec *request,
45                           struct timespec *remain);
46
47struct framebuffer_device_t;
48
49namespace HWC2 {
50    class Device;
51    class Display;
52}
53
54namespace android {
55// ---------------------------------------------------------------------------
56
57class DisplayDevice;
58class Fence;
59class FloatRect;
60class GraphicBuffer;
61class HWC2On1Adapter;
62class NativeHandle;
63class Region;
64class String8;
65class SurfaceFlinger;
66
67class HWComposer
68{
69public:
70    class EventHandler {
71        friend class HWComposer;
72        virtual void onVSyncReceived(int32_t disp, nsecs_t timestamp) = 0;
73        virtual void onHotplugReceived(int32_t disp, bool connected) = 0;
74    protected:
75        virtual ~EventHandler() {}
76    };
77
78    HWComposer(const sp<SurfaceFlinger>& flinger);
79
80    ~HWComposer();
81
82    void setEventHandler(EventHandler* handler);
83
84    bool hasCapability(HWC2::Capability capability) const;
85
86    // Attempts to allocate a virtual display. If the virtual display is created
87    // on the HWC device, outId will contain its HWC ID.
88    status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
89            android_pixel_format_t* format, int32_t* outId);
90
91    // Attempts to create a new layer on this display
92    std::shared_ptr<HWC2::Layer> createLayer(int32_t displayId);
93
94    // Asks the HAL what it can do
95    status_t prepare(DisplayDevice& displayDevice);
96
97    status_t setClientTarget(int32_t displayId, const sp<Fence>& acquireFence,
98            const sp<GraphicBuffer>& target, android_dataspace_t dataspace);
99
100    // Finalize the layers and present them
101    status_t commit(int32_t displayId);
102
103    // set power mode
104    status_t setPowerMode(int32_t displayId, int mode);
105
106    // set active config
107    status_t setActiveConfig(int32_t displayId, size_t configId);
108
109    // Sets a color transform to be applied to the result of composition
110    status_t setColorTransform(int32_t displayId, const mat4& transform);
111
112    // reset state when an external, non-virtual display is disconnected
113    void disconnectDisplay(int32_t displayId);
114
115    // does this display have layers handled by HWC
116    bool hasDeviceComposition(int32_t displayId) const;
117
118    // does this display have layers handled by GLES
119    bool hasClientComposition(int32_t displayId) const;
120
121    // get the retire fence for the previous frame (i.e., corresponding to the
122    // last call to presentDisplay
123    sp<Fence> getRetireFence(int32_t displayId) const;
124
125    // Returns true if the retire fence represents the start of the display
126    // controller's scan out. This should be true for all HWC2 implementations,
127    // except for the wrapper around HWC1 implementations.
128    bool retireFenceRepresentsStartOfScanout() const;
129
130    // Get last release fence for the given layer
131    sp<Fence> getLayerReleaseFence(int32_t displayId,
132            const std::shared_ptr<HWC2::Layer>& layer) const;
133
134    // Set the output buffer and acquire fence for a virtual display.
135    // Returns INVALID_OPERATION if displayId is not a virtual display.
136    status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
137            const sp<GraphicBuffer>& buf);
138
139    // After SurfaceFlinger has retrieved the release fences for all the frames,
140    // it can call this to clear the shared pointers in the release fence map
141    void clearReleaseFences(int32_t displayId);
142
143    // Returns the HDR capabilities of the given display
144    std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId);
145
146    // Events handling ---------------------------------------------------------
147
148    void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
149
150    // Query display parameters.  Pass in a display index (e.g.
151    // HWC_DISPLAY_PRIMARY).
152    nsecs_t getRefreshTimestamp(int32_t displayId) const;
153    bool isConnected(int32_t displayId) const;
154
155    // Non-const because it can update configMap inside of mDisplayData
156    std::vector<std::shared_ptr<const HWC2::Display::Config>>
157            getConfigs(int32_t displayId) const;
158
159    std::shared_ptr<const HWC2::Display::Config>
160            getActiveConfig(int32_t displayId) const;
161
162    std::vector<android_color_mode_t> getColorModes(int32_t displayId) const;
163
164    status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode);
165
166    // for debugging ----------------------------------------------------------
167    void dump(String8& out) const;
168
169private:
170    static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2;
171
172    void loadHwcModule();
173
174    bool isValidDisplay(int32_t displayId) const;
175    static void validateChange(HWC2::Composition from, HWC2::Composition to);
176
177    struct cb_context;
178
179    void invalidate(const std::shared_ptr<HWC2::Display>& display);
180    void vsync(const std::shared_ptr<HWC2::Display>& display,
181            int64_t timestamp);
182    void hotplug(const std::shared_ptr<HWC2::Display>& display,
183            HWC2::Connection connected);
184
185    struct DisplayData {
186        DisplayData();
187        ~DisplayData();
188        void reset();
189
190        bool hasClientComposition;
191        bool hasDeviceComposition;
192        std::shared_ptr<HWC2::Display> hwcDisplay;
193        HWC2::DisplayRequest displayRequests;
194        sp<Fence> lastRetireFence;  // signals when the last set op retires
195        std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>>
196                releaseFences;
197        buffer_handle_t outbufHandle;
198        sp<Fence> outbufAcquireFence;
199        mutable std::unordered_map<int32_t,
200                std::shared_ptr<const HWC2::Display::Config>> configMap;
201
202        // protected by mVsyncLock
203        HWC2::Vsync vsyncEnabled;
204    };
205
206    sp<SurfaceFlinger>              mFlinger;
207    std::unique_ptr<HWC2On1Adapter> mAdapter;
208    std::unique_ptr<HWC2::Device>   mHwcDevice;
209    std::vector<DisplayData>        mDisplayData;
210    std::set<size_t>                mFreeDisplaySlots;
211    std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
212    // protect mDisplayData from races between prepare and dump
213    mutable Mutex mDisplayLock;
214
215    cb_context*                     mCBContext;
216    EventHandler*                   mEventHandler;
217    size_t                          mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
218    uint32_t                        mRemainingHwcVirtualDisplays;
219
220    // protected by mLock
221    mutable Mutex mLock;
222    mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync;
223
224    // thread-safe
225    mutable Mutex mVsyncLock;
226};
227
228// ---------------------------------------------------------------------------
229}; // namespace android
230
231#endif // ANDROID_SF_HWCOMPOSER_H
232
233#endif // #ifdef USE_HWC2
234