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