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