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