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