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