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