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