HWComposer.h revision 6b44267a3beb457e220cad0666c039d3a765cdb2
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 ANDROID_SF_HWCOMPOSER_H
18#define ANDROID_SF_HWCOMPOSER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <hardware/hwcomposer_defs.h>
24
25#include <ui/Fence.h>
26
27#include <utils/BitSet.h>
28#include <utils/Condition.h>
29#include <utils/Mutex.h>
30#include <utils/StrongPointer.h>
31#include <utils/Thread.h>
32#include <utils/Timers.h>
33#include <utils/Vector.h>
34
35extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
36                           const struct timespec *request,
37                           struct timespec *remain);
38
39struct hwc_composer_device_1;
40struct hwc_display_contents_1;
41struct hwc_layer_1;
42struct hwc_procs;
43struct framebuffer_device_t;
44
45namespace android {
46// ---------------------------------------------------------------------------
47
48class GraphicBuffer;
49class Fence;
50class FloatRect;
51class Region;
52class String8;
53class SurfaceFlinger;
54
55class HWComposer
56{
57public:
58    class EventHandler {
59        friend class HWComposer;
60        virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
61        virtual void onHotplugReceived(int disp, bool connected) = 0;
62    protected:
63        virtual ~EventHandler() {}
64    };
65
66    enum {
67        MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
68    };
69
70    HWComposer(
71            const sp<SurfaceFlinger>& flinger,
72            EventHandler& handler);
73
74    ~HWComposer();
75
76    status_t initCheck() const;
77
78    // returns a display ID starting at MAX_DISPLAYS, this ID
79    // is to be used with createWorkList (and all other
80    // methods requiring an ID below).
81    // IDs below MAX_DISPLAY are pre-defined and therefore are always valid.
82    // returns a negative error code if an ID cannot be allocated
83    int32_t allocateDisplayId();
84
85    // recycles the given ID and frees the associated worklist.
86    // IDs below MAX_DISPLAYS are not recycled
87    status_t freeDisplayId(int32_t id);
88
89
90    // Asks the HAL what it can do
91    status_t prepare();
92
93    // commits the list
94    status_t commit();
95
96    // release hardware resources and blank screen
97    status_t release(int disp);
98
99    // acquire hardware resources and unblank screen
100    status_t acquire(int disp);
101
102    // reset state when an external, non-virtual display is disconnected
103    void disconnectDisplay(int disp);
104
105    // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
106    status_t createWorkList(int32_t id, size_t numLayers);
107
108    bool supportsFramebufferTarget() const;
109
110    // does this display have layers handled by HWC
111    bool hasHwcComposition(int32_t id) const;
112
113    // does this display have layers handled by GLES
114    bool hasGlesComposition(int32_t id) const;
115
116    // get the releaseFence file descriptor for a display's framebuffer layer.
117    // the release fence is only valid after commit()
118    sp<Fence> getAndResetReleaseFence(int32_t id);
119
120    // needed forward declarations
121    class LayerListIterator;
122
123    // return the visual id to be used to find a suitable EGLConfig for
124    // *ALL* displays.
125    int getVisualID() const;
126
127    // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
128    int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
129    int fbCompositionComplete();
130    void fbDump(String8& result);
131
132    // Set the output buffer and acquire fence for a virtual display.
133    // Returns INVALID_OPERATION if id is not a virtual display.
134    status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
135            const sp<GraphicBuffer>& buf);
136
137    // Get the retire fence for the last committed frame. This fence will
138    // signal when the h/w composer is completely finished with the frame.
139    // For physical displays, it is no longer being displayed. For virtual
140    // displays, writes to the output buffer are complete.
141    sp<Fence> getLastRetireFence(int32_t id);
142
143    /*
144     * Interface to hardware composer's layers functionality.
145     * This abstracts the HAL interface to layers which can evolve in
146     * incompatible ways from one release to another.
147     * The idea is that we could extend this interface as we add
148     * features to h/w composer.
149     */
150    class HWCLayerInterface {
151    protected:
152        virtual ~HWCLayerInterface() { }
153    public:
154        virtual int32_t getCompositionType() const = 0;
155        virtual uint32_t getHints() const = 0;
156        virtual sp<Fence> getAndResetReleaseFence() = 0;
157        virtual void setDefaultState() = 0;
158        virtual void setSkip(bool skip) = 0;
159        virtual void setBlending(uint32_t blending) = 0;
160        virtual void setTransform(uint32_t transform) = 0;
161        virtual void setFrame(const Rect& frame) = 0;
162        virtual void setCrop(const FloatRect& crop) = 0;
163        virtual void setVisibleRegionScreen(const Region& reg) = 0;
164        virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
165        virtual void setAcquireFenceFd(int fenceFd) = 0;
166        virtual void setPlaneAlpha(uint8_t alpha) = 0;
167        virtual void onDisplayed() = 0;
168    };
169
170    /*
171     * Interface used to implement an iterator to a list
172     * of HWCLayer.
173     */
174    class HWCLayer : public HWCLayerInterface {
175        friend class LayerListIterator;
176        // select the layer at the given index
177        virtual status_t setLayer(size_t index) = 0;
178        virtual HWCLayer* dup() = 0;
179        static HWCLayer* copy(HWCLayer *rhs) {
180            return rhs ? rhs->dup() : NULL;
181        }
182    protected:
183        virtual ~HWCLayer() { }
184    };
185
186    /*
187     * Iterator through a HWCLayer list.
188     * This behaves more or less like a forward iterator.
189     */
190    class LayerListIterator {
191        friend struct HWComposer;
192        HWCLayer* const mLayerList;
193        size_t mIndex;
194
195        LayerListIterator() : mLayerList(NULL), mIndex(0) { }
196
197        LayerListIterator(HWCLayer* layer, size_t index)
198            : mLayerList(layer), mIndex(index) { }
199
200        // we don't allow assignment, because we don't need it for now
201        LayerListIterator& operator = (const LayerListIterator& rhs);
202
203    public:
204        // copy operators
205        LayerListIterator(const LayerListIterator& rhs)
206            : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
207        }
208
209        ~LayerListIterator() { delete mLayerList; }
210
211        // pre-increment
212        LayerListIterator& operator++() {
213            mLayerList->setLayer(++mIndex);
214            return *this;
215        }
216
217        // dereference
218        HWCLayerInterface& operator * () { return *mLayerList; }
219        HWCLayerInterface* operator -> () { return mLayerList; }
220
221        // comparison
222        bool operator == (const LayerListIterator& rhs) const {
223            return mIndex == rhs.mIndex;
224        }
225        bool operator != (const LayerListIterator& rhs) const {
226            return !operator==(rhs);
227        }
228    };
229
230    // Returns an iterator to the beginning of the layer list
231    LayerListIterator begin(int32_t id);
232
233    // Returns an iterator to the end of the layer list
234    LayerListIterator end(int32_t id);
235
236
237    // Events handling ---------------------------------------------------------
238
239    enum {
240        EVENT_VSYNC = HWC_EVENT_VSYNC
241    };
242
243    void eventControl(int disp, int event, int enabled);
244
245    // Query display parameters.  Pass in a display index (e.g.
246    // HWC_DISPLAY_PRIMARY).
247    nsecs_t getRefreshPeriod(int disp) const;
248    nsecs_t getRefreshTimestamp(int disp) const;
249    sp<Fence> getDisplayFence(int disp) const;
250    uint32_t getWidth(int disp) const;
251    uint32_t getHeight(int disp) const;
252    uint32_t getFormat(int disp) const;
253    float getDpiX(int disp) const;
254    float getDpiY(int disp) const;
255    bool isConnected(int disp) const;
256
257    status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
258            uint32_t format);
259
260    // this class is only used to fake the VSync event on systems that don't
261    // have it.
262    class VSyncThread : public Thread {
263        HWComposer& mHwc;
264        mutable Mutex mLock;
265        Condition mCondition;
266        bool mEnabled;
267        mutable nsecs_t mNextFakeVSync;
268        nsecs_t mRefreshPeriod;
269        virtual void onFirstRef();
270        virtual bool threadLoop();
271    public:
272        VSyncThread(HWComposer& hwc);
273        void setEnabled(bool enabled);
274    };
275
276    friend class VSyncThread;
277
278    // for debugging ----------------------------------------------------------
279    void dump(String8& out) const;
280
281private:
282    void loadHwcModule();
283    int loadFbHalModule();
284
285    LayerListIterator getLayerIterator(int32_t id, size_t index);
286
287    struct cb_context;
288
289    static void hook_invalidate(const struct hwc_procs* procs);
290    static void hook_vsync(const struct hwc_procs* procs, int disp,
291            int64_t timestamp);
292    static void hook_hotplug(const struct hwc_procs* procs, int disp,
293            int connected);
294
295    inline void invalidate();
296    inline void vsync(int disp, int64_t timestamp);
297    inline void hotplug(int disp, int connected);
298
299    status_t queryDisplayProperties(int disp);
300
301    status_t setFramebufferTarget(int32_t id,
302            const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
303
304
305    struct DisplayData {
306        DisplayData();
307        ~DisplayData();
308        uint32_t width;
309        uint32_t height;
310        uint32_t format;    // pixel format from FB hal, for pre-hwc-1.1
311        float xdpi;
312        float ydpi;
313        nsecs_t refresh;
314        bool connected;
315        bool hasFbComp;
316        bool hasOvComp;
317        size_t capacity;
318        hwc_display_contents_1* list;
319        hwc_layer_1* framebufferTarget;
320        buffer_handle_t fbTargetHandle;
321        sp<Fence> lastRetireFence;  // signals when the last set op retires
322        sp<Fence> lastDisplayFence; // signals when the last set op takes
323                                    // effect on screen
324        buffer_handle_t outbufHandle;
325        sp<Fence> outbufAcquireFence;
326
327        // protected by mEventControlLock
328        int32_t events;
329    };
330
331    sp<SurfaceFlinger>              mFlinger;
332    framebuffer_device_t*           mFbDev;
333    struct hwc_composer_device_1*   mHwc;
334    // invariant: mLists[0] != NULL iff mHwc != NULL
335    // mLists[i>0] can be NULL. that display is to be ignored
336    struct hwc_display_contents_1*  mLists[MAX_DISPLAYS];
337    DisplayData                     mDisplayData[MAX_DISPLAYS];
338    size_t                          mNumDisplays;
339
340    cb_context*                     mCBContext;
341    EventHandler&                   mEventHandler;
342    size_t                          mVSyncCount;
343    sp<VSyncThread>                 mVSyncThread;
344    bool                            mDebugForceFakeVSync;
345    BitSet32                        mAllocatedDisplayIDs;
346
347    // protected by mLock
348    mutable Mutex mLock;
349    mutable nsecs_t mLastHwVSync;
350
351    // thread-safe
352    mutable Mutex mEventControlLock;
353};
354
355// ---------------------------------------------------------------------------
356}; // namespace android
357
358#endif // ANDROID_SF_HWCOMPOSER_H
359