HWComposer.h revision bbd164a3c790a0649dffd2f015e6f47692c72e1c
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
32extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
33                           const struct timespec *request,
34                           struct timespec *remain);
35
36struct hwc_composer_device_1;
37struct hwc_display_contents_1;
38struct hwc_procs;
39struct framebuffer_device_t;
40
41namespace android {
42// ---------------------------------------------------------------------------
43
44class GraphicBuffer;
45class LayerBase;
46class Region;
47class String8;
48class SurfaceFlinger;
49
50class HWComposer
51{
52public:
53    class EventHandler {
54        friend class HWComposer;
55        virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0;
56    protected:
57        virtual ~EventHandler() {}
58    };
59
60    enum {
61        MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
62    };
63
64    HWComposer(
65            const sp<SurfaceFlinger>& flinger,
66            EventHandler& handler,
67            framebuffer_device_t const* fbDev);
68
69    ~HWComposer();
70
71    status_t initCheck() const;
72
73    // Asks the HAL what it can do
74    status_t prepare() const;
75
76    // disable hwc until next createWorkList
77    status_t disable();
78
79    // commits the list
80    status_t commit(void* fbDisplay, void* fbSurface) const;
81
82    // release hardware resources and blank screen
83    status_t release() const;
84
85    // acquire hardware resources and unblank screen
86    status_t acquire() const;
87
88    // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
89    status_t createWorkList(int32_t id, size_t numLayers);
90
91    // get number of layers of the given type as updated in prepare().
92    // type is HWC_OVERLAY or HWC_FRAMEBUFFER
93    size_t getLayerCount(int32_t id, int type) const;
94
95    // needed forward declarations
96    class LayerListIterator;
97
98    /*
99     * Interface to hardware composer's layers functionality.
100     * This abstracts the HAL interface to layers which can evolve in
101     * incompatible ways from one release to another.
102     * The idea is that we could extend this interface as we add
103     * features to h/w composer.
104     */
105    class HWCLayerInterface {
106    protected:
107        virtual ~HWCLayerInterface() { }
108    public:
109        virtual int32_t getCompositionType() const = 0;
110        virtual uint32_t getHints() const = 0;
111        virtual int getAndResetReleaseFenceFd() = 0;
112        virtual void setDefaultState() = 0;
113        virtual void setSkip(bool skip) = 0;
114        virtual void setBlending(uint32_t blending) = 0;
115        virtual void setTransform(uint32_t transform) = 0;
116        virtual void setFrame(const Rect& frame) = 0;
117        virtual void setCrop(const Rect& crop) = 0;
118        virtual void setVisibleRegionScreen(const Region& reg) = 0;
119        virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
120        virtual void setAcquireFenceFd(int fenceFd) = 0;
121    };
122
123    /*
124     * Interface used to implement an iterator to a list
125     * of HWCLayer.
126     */
127    class HWCLayer : public HWCLayerInterface {
128        friend class LayerListIterator;
129        // select the layer at the given index
130        virtual status_t setLayer(size_t index) = 0;
131        virtual HWCLayer* dup() = 0;
132        static HWCLayer* copy(HWCLayer *rhs) {
133            return rhs ? rhs->dup() : NULL;
134        }
135    protected:
136        virtual ~HWCLayer() { }
137    };
138
139    /*
140     * Iterator through a HWCLayer list.
141     * This behaves more or less like a forward iterator.
142     */
143    class LayerListIterator {
144        friend struct HWComposer;
145        HWCLayer* const mLayerList;
146        size_t mIndex;
147
148        LayerListIterator() : mLayerList(NULL), mIndex(0) { }
149
150        LayerListIterator(HWCLayer* layer, size_t index)
151            : mLayerList(layer), mIndex(index) { }
152
153        // we don't allow assignment, because we don't need it for now
154        LayerListIterator& operator = (const LayerListIterator& rhs);
155
156    public:
157        // copy operators
158        LayerListIterator(const LayerListIterator& rhs)
159            : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
160        }
161
162        ~LayerListIterator() { delete mLayerList; }
163
164        // pre-increment
165        LayerListIterator& operator++() {
166            mLayerList->setLayer(++mIndex);
167            return *this;
168        }
169
170        // dereference
171        HWCLayerInterface& operator * () { return *mLayerList; }
172        HWCLayerInterface* operator -> () { return mLayerList; }
173
174        // comparison
175        bool operator == (const LayerListIterator& rhs) const {
176            return mIndex == rhs.mIndex;
177        }
178        bool operator != (const LayerListIterator& rhs) const {
179            return !operator==(rhs);
180        }
181    };
182
183    // Returns an iterator to the beginning of the layer list
184    LayerListIterator begin(int32_t id);
185
186    // Returns an iterator to the end of the layer list
187    LayerListIterator end(int32_t id);
188
189
190    // Events handling ---------------------------------------------------------
191
192    enum {
193        EVENT_VSYNC = HWC_EVENT_VSYNC
194    };
195
196    void eventControl(int event, int enabled);
197
198    nsecs_t getRefreshPeriod() const;
199    nsecs_t getRefreshTimestamp() const;
200    float getDpiX() const;
201    float getDpiY() const;
202
203    // this class is only used to fake the VSync event on systems that don't
204    // have it.
205    class VSyncThread : public Thread {
206        HWComposer& mHwc;
207        mutable Mutex mLock;
208        Condition mCondition;
209        bool mEnabled;
210        mutable nsecs_t mNextFakeVSync;
211        nsecs_t mRefreshPeriod;
212        virtual void onFirstRef();
213        virtual bool threadLoop();
214    public:
215        VSyncThread(HWComposer& hwc);
216        void setEnabled(bool enabled);
217    };
218
219    friend class VSyncThread;
220
221    // for debugging ----------------------------------------------------------
222    void dump(String8& out, char* scratch, size_t SIZE,
223            const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
224
225private:
226
227    LayerListIterator getLayerIterator(int32_t id, size_t index);
228    size_t getNumLayers(int32_t id) const;
229
230    struct cb_context;
231
232    static void hook_invalidate(const struct hwc_procs* procs);
233    static void hook_vsync(const struct hwc_procs* procs, int dpy,
234            int64_t timestamp);
235
236    inline void invalidate();
237    inline void vsync(int dpy, int64_t timestamp);
238
239
240    sp<SurfaceFlinger>              mFlinger;
241    hw_module_t const*              mModule;
242    struct hwc_composer_device_1*   mHwc;
243    // invariant: mLists[0] != NULL iff mHwc != NULL
244    // TODO: decide whether mLists[i>0] should be non-NULL when display i is
245    //       not attached/enabled.
246    struct hwc_display_contents_1*  mLists[MAX_DISPLAYS];
247    size_t                          mNumDisplays;
248
249    size_t                          mCapacity;
250    mutable size_t                  mNumOVLayers;
251    mutable size_t                  mNumFBLayers;
252    cb_context*                     mCBContext;
253    EventHandler&                   mEventHandler;
254    nsecs_t                         mRefreshPeriod;
255    float                           mDpiX;
256    float                           mDpiY;
257    size_t                          mVSyncCount;
258    sp<VSyncThread>                 mVSyncThread;
259    bool                            mDebugForceFakeVSync;
260
261    // protected by mLock
262    mutable Mutex mLock;
263    mutable nsecs_t mLastHwVSync;
264};
265
266// ---------------------------------------------------------------------------
267}; // namespace android
268
269#endif // ANDROID_SF_HWCOMPOSER_H
270