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