HWComposer.h revision a9a1b006e48320f5c501473e51e6c4a5f7a17b88
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 LayerBase;
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 the given display
117    // the release fence is only valid after commit()
118    int getAndResetReleaseFenceFd(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    /*
133     * Interface to hardware composer's layers functionality.
134     * This abstracts the HAL interface to layers which can evolve in
135     * incompatible ways from one release to another.
136     * The idea is that we could extend this interface as we add
137     * features to h/w composer.
138     */
139    class HWCLayerInterface {
140    protected:
141        virtual ~HWCLayerInterface() { }
142    public:
143        virtual int32_t getCompositionType() const = 0;
144        virtual uint32_t getHints() const = 0;
145        virtual int getAndResetReleaseFenceFd() = 0;
146        virtual void setDefaultState() = 0;
147        virtual void setSkip(bool skip) = 0;
148        virtual void setBlending(uint32_t blending) = 0;
149        virtual void setTransform(uint32_t transform) = 0;
150        virtual void setFrame(const Rect& frame) = 0;
151        virtual void setCrop(const Rect& crop) = 0;
152        virtual void setVisibleRegionScreen(const Region& reg) = 0;
153        virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
154        virtual void setAcquireFenceFd(int fenceFd) = 0;
155        virtual void setPlaneAlpha(uint8_t alpha) = 0;
156        virtual void onDisplayed() = 0;
157    };
158
159    /*
160     * Interface used to implement an iterator to a list
161     * of HWCLayer.
162     */
163    class HWCLayer : public HWCLayerInterface {
164        friend class LayerListIterator;
165        // select the layer at the given index
166        virtual status_t setLayer(size_t index) = 0;
167        virtual HWCLayer* dup() = 0;
168        static HWCLayer* copy(HWCLayer *rhs) {
169            return rhs ? rhs->dup() : NULL;
170        }
171    protected:
172        virtual ~HWCLayer() { }
173    };
174
175    /*
176     * Iterator through a HWCLayer list.
177     * This behaves more or less like a forward iterator.
178     */
179    class LayerListIterator {
180        friend struct HWComposer;
181        HWCLayer* const mLayerList;
182        size_t mIndex;
183
184        LayerListIterator() : mLayerList(NULL), mIndex(0) { }
185
186        LayerListIterator(HWCLayer* layer, size_t index)
187            : mLayerList(layer), mIndex(index) { }
188
189        // we don't allow assignment, because we don't need it for now
190        LayerListIterator& operator = (const LayerListIterator& rhs);
191
192    public:
193        // copy operators
194        LayerListIterator(const LayerListIterator& rhs)
195            : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
196        }
197
198        ~LayerListIterator() { delete mLayerList; }
199
200        // pre-increment
201        LayerListIterator& operator++() {
202            mLayerList->setLayer(++mIndex);
203            return *this;
204        }
205
206        // dereference
207        HWCLayerInterface& operator * () { return *mLayerList; }
208        HWCLayerInterface* operator -> () { return mLayerList; }
209
210        // comparison
211        bool operator == (const LayerListIterator& rhs) const {
212            return mIndex == rhs.mIndex;
213        }
214        bool operator != (const LayerListIterator& rhs) const {
215            return !operator==(rhs);
216        }
217    };
218
219    // Returns an iterator to the beginning of the layer list
220    LayerListIterator begin(int32_t id);
221
222    // Returns an iterator to the end of the layer list
223    LayerListIterator end(int32_t id);
224
225
226    // Events handling ---------------------------------------------------------
227
228    enum {
229        EVENT_VSYNC = HWC_EVENT_VSYNC
230    };
231
232    void eventControl(int disp, int event, int enabled);
233
234    // Query display parameters.  Pass in a display index (e.g.
235    // HWC_DISPLAY_PRIMARY).
236    nsecs_t getRefreshPeriod(int disp) const;
237    nsecs_t getRefreshTimestamp(int disp) const;
238    sp<Fence> getDisplayFence(int disp) const;
239    uint32_t getWidth(int disp) const;
240    uint32_t getHeight(int disp) const;
241    uint32_t getFormat(int disp) const;
242    float getDpiX(int disp) const;
243    float getDpiY(int disp) const;
244    bool isConnected(int disp) const;
245
246    // this class is only used to fake the VSync event on systems that don't
247    // have it.
248    class VSyncThread : public Thread {
249        HWComposer& mHwc;
250        mutable Mutex mLock;
251        Condition mCondition;
252        bool mEnabled;
253        mutable nsecs_t mNextFakeVSync;
254        nsecs_t mRefreshPeriod;
255        virtual void onFirstRef();
256        virtual bool threadLoop();
257    public:
258        VSyncThread(HWComposer& hwc);
259        void setEnabled(bool enabled);
260    };
261
262    friend class VSyncThread;
263
264    // for debugging ----------------------------------------------------------
265    void dump(String8& out, char* scratch, size_t SIZE) const;
266
267private:
268    void loadHwcModule();
269    void loadFbHalModule();
270
271    LayerListIterator getLayerIterator(int32_t id, size_t index);
272
273    struct cb_context;
274
275    static void hook_invalidate(const struct hwc_procs* procs);
276    static void hook_vsync(const struct hwc_procs* procs, int disp,
277            int64_t timestamp);
278    static void hook_hotplug(const struct hwc_procs* procs, int disp,
279            int connected);
280
281    inline void invalidate();
282    inline void vsync(int disp, int64_t timestamp);
283    inline void hotplug(int disp, int connected);
284
285    status_t queryDisplayProperties(int disp);
286
287    status_t setFramebufferTarget(int32_t id,
288            const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
289
290
291    struct DisplayData {
292        DisplayData();
293        ~DisplayData();
294        uint32_t width;
295        uint32_t height;
296        uint32_t format;    // pixel format from FB hal, for pre-hwc-1.1
297        float xdpi;
298        float ydpi;
299        nsecs_t refresh;
300        bool connected;
301        bool hasFbComp;
302        bool hasOvComp;
303        size_t capacity;
304        hwc_display_contents_1* list;
305        hwc_layer_1* framebufferTarget;
306        buffer_handle_t fbTargetHandle;
307        sp<Fence> lastRetireFence;  // signals when the last set op retires
308        sp<Fence> lastDisplayFence; // signals when the last set op takes
309                                    // effect on screen
310
311        // protected by mEventControlLock
312        int32_t events;
313    };
314
315    sp<SurfaceFlinger>              mFlinger;
316    framebuffer_device_t*           mFbDev;
317    struct hwc_composer_device_1*   mHwc;
318    // invariant: mLists[0] != NULL iff mHwc != NULL
319    // mLists[i>0] can be NULL. that display is to be ignored
320    struct hwc_display_contents_1*  mLists[MAX_DISPLAYS];
321    DisplayData                     mDisplayData[MAX_DISPLAYS];
322    size_t                          mNumDisplays;
323
324    cb_context*                     mCBContext;
325    EventHandler&                   mEventHandler;
326    size_t                          mVSyncCount;
327    sp<VSyncThread>                 mVSyncThread;
328    bool                            mDebugForceFakeVSync;
329    BitSet32                        mAllocatedDisplayIDs;
330
331    // protected by mLock
332    mutable Mutex mLock;
333    mutable nsecs_t mLastHwVSync;
334
335    // thread-safe
336    mutable Mutex mEventControlLock;
337};
338
339// ---------------------------------------------------------------------------
340}; // namespace android
341
342#endif // ANDROID_SF_HWCOMPOSER_H
343