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