HWComposer.h revision c01a79d77b829e7de86ef137694e8ad708470ca1
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#include <utils/BitSet.h>
32
33extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
34                           const struct timespec *request,
35                           struct timespec *remain);
36
37struct hwc_composer_device_1;
38struct hwc_display_contents_1;
39struct hwc_layer_1;
40struct hwc_procs;
41struct framebuffer_device_t;
42
43namespace android {
44// ---------------------------------------------------------------------------
45
46class GraphicBuffer;
47class Fence;
48class LayerBase;
49class Region;
50class String8;
51class SurfaceFlinger;
52
53class HWComposer
54{
55public:
56    class EventHandler {
57        friend class HWComposer;
58        virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
59        virtual void onHotplugReceived(int disp, bool connected) = 0;
60    protected:
61        virtual ~EventHandler() {}
62    };
63
64    enum {
65        MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
66    };
67
68    HWComposer(
69            const sp<SurfaceFlinger>& flinger,
70            EventHandler& handler);
71
72    ~HWComposer();
73
74    status_t initCheck() const;
75
76    // returns a display ID starting at MAX_DISPLAYS, this ID
77    // is to be used with createWorkList (and all other
78    // methods requiring an ID below).
79    // IDs below MAX_DISPLAY are pre-defined and therefore are always valid.
80    // returns a negative error code if an ID cannot be allocated
81    int32_t allocateDisplayId();
82
83    // recycles the given ID and frees the associated worklist.
84    // IDs below MAX_DISPLAYS are not recycled
85    status_t freeDisplayId(int32_t id);
86
87
88    // Asks the HAL what it can do
89    status_t prepare();
90
91    // commits the list
92    status_t commit();
93
94    // release hardware resources and blank screen
95    status_t release(int disp) const;
96
97    // acquire hardware resources and unblank screen
98    status_t acquire(int disp) const;
99
100    // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
101    status_t createWorkList(int32_t id, size_t numLayers);
102
103    bool supportsFramebufferTarget() const;
104
105    // does this display have layers handled by HWC
106    bool hasHwcComposition(int32_t id) const;
107
108    // does this display have layers handled by GLES
109    bool hasGlesComposition(int32_t id) const;
110
111    // get the releaseFence file descriptor for the given display
112    // the release fence is only valid after commit()
113    int getAndResetReleaseFenceFd(int32_t id);
114
115    // needed forward declarations
116    class LayerListIterator;
117
118    // return the visual id to be used to find a suitable EGLConfig for
119    // *ALL* displays.
120    int getVisualID() const;
121
122    // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
123    int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
124    int fbCompositionComplete();
125    void fbDump(String8& result);
126
127    /*
128     * Interface to hardware composer's layers functionality.
129     * This abstracts the HAL interface to layers which can evolve in
130     * incompatible ways from one release to another.
131     * The idea is that we could extend this interface as we add
132     * features to h/w composer.
133     */
134    class HWCLayerInterface {
135    protected:
136        virtual ~HWCLayerInterface() { }
137    public:
138        virtual int32_t getCompositionType() const = 0;
139        virtual uint32_t getHints() const = 0;
140        virtual int getAndResetReleaseFenceFd() = 0;
141        virtual void setDefaultState() = 0;
142        virtual void setSkip(bool skip) = 0;
143        virtual void setBlending(uint32_t blending) = 0;
144        virtual void setTransform(uint32_t transform) = 0;
145        virtual void setFrame(const Rect& frame) = 0;
146        virtual void setCrop(const Rect& crop) = 0;
147        virtual void setVisibleRegionScreen(const Region& reg) = 0;
148        virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
149        virtual void setAcquireFenceFd(int fenceFd) = 0;
150        virtual void onDisplayed() = 0;
151    };
152
153    /*
154     * Interface used to implement an iterator to a list
155     * of HWCLayer.
156     */
157    class HWCLayer : public HWCLayerInterface {
158        friend class LayerListIterator;
159        // select the layer at the given index
160        virtual status_t setLayer(size_t index) = 0;
161        virtual HWCLayer* dup() = 0;
162        static HWCLayer* copy(HWCLayer *rhs) {
163            return rhs ? rhs->dup() : NULL;
164        }
165    protected:
166        virtual ~HWCLayer() { }
167    };
168
169    /*
170     * Iterator through a HWCLayer list.
171     * This behaves more or less like a forward iterator.
172     */
173    class LayerListIterator {
174        friend struct HWComposer;
175        HWCLayer* const mLayerList;
176        size_t mIndex;
177
178        LayerListIterator() : mLayerList(NULL), mIndex(0) { }
179
180        LayerListIterator(HWCLayer* layer, size_t index)
181            : mLayerList(layer), mIndex(index) { }
182
183        // we don't allow assignment, because we don't need it for now
184        LayerListIterator& operator = (const LayerListIterator& rhs);
185
186    public:
187        // copy operators
188        LayerListIterator(const LayerListIterator& rhs)
189            : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
190        }
191
192        ~LayerListIterator() { delete mLayerList; }
193
194        // pre-increment
195        LayerListIterator& operator++() {
196            mLayerList->setLayer(++mIndex);
197            return *this;
198        }
199
200        // dereference
201        HWCLayerInterface& operator * () { return *mLayerList; }
202        HWCLayerInterface* operator -> () { return mLayerList; }
203
204        // comparison
205        bool operator == (const LayerListIterator& rhs) const {
206            return mIndex == rhs.mIndex;
207        }
208        bool operator != (const LayerListIterator& rhs) const {
209            return !operator==(rhs);
210        }
211    };
212
213    // Returns an iterator to the beginning of the layer list
214    LayerListIterator begin(int32_t id);
215
216    // Returns an iterator to the end of the layer list
217    LayerListIterator end(int32_t id);
218
219
220    // Events handling ---------------------------------------------------------
221
222    enum {
223        EVENT_VSYNC = HWC_EVENT_VSYNC
224    };
225
226    void eventControl(int event, int enabled);
227
228    // Query display parameters.  Pass in a display index (e.g.
229    // HWC_DISPLAY_PRIMARY).
230    nsecs_t getRefreshPeriod(int disp) const;
231    nsecs_t getRefreshTimestamp(int disp) const;
232    uint32_t getWidth(int disp) const;
233    uint32_t getHeight(int disp) const;
234    uint32_t getFormat(int disp) const;
235    float getDpiX(int disp) const;
236    float getDpiY(int disp) const;
237    bool isConnected(int disp) const;
238
239    // this class is only used to fake the VSync event on systems that don't
240    // have it.
241    class VSyncThread : public Thread {
242        HWComposer& mHwc;
243        mutable Mutex mLock;
244        Condition mCondition;
245        bool mEnabled;
246        mutable nsecs_t mNextFakeVSync;
247        nsecs_t mRefreshPeriod;
248        virtual void onFirstRef();
249        virtual bool threadLoop();
250    public:
251        VSyncThread(HWComposer& hwc);
252        void setEnabled(bool enabled);
253    };
254
255    friend class VSyncThread;
256
257    // for debugging ----------------------------------------------------------
258    void dump(String8& out, char* scratch, size_t SIZE,
259            const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
260
261private:
262    void loadHwcModule();
263    void loadFbHalModule();
264
265    LayerListIterator getLayerIterator(int32_t id, size_t index);
266
267    struct cb_context;
268
269    static void hook_invalidate(const struct hwc_procs* procs);
270    static void hook_vsync(const struct hwc_procs* procs, int disp,
271            int64_t timestamp);
272    static void hook_hotplug(const struct hwc_procs* procs, int disp,
273            int connected);
274
275    inline void invalidate();
276    inline void vsync(int disp, int64_t timestamp);
277    inline void hotplug(int disp, int connected);
278
279    status_t queryDisplayProperties(int disp);
280
281    status_t setFramebufferTarget(int32_t id,
282            const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
283
284
285    struct DisplayData {
286        DisplayData() : xdpi(0), ydpi(0), refresh(0),
287            connected(false), hasFbComp(false), hasOvComp(false),
288            capacity(0), list(NULL),
289            framebufferTarget(NULL), fbTargetHandle(NULL) { }
290        ~DisplayData() {
291            free(list);
292        }
293        uint32_t width;
294        uint32_t height;
295        uint32_t format;    // pixel format from FB hal, for pre-hwc-1.1
296        float xdpi;
297        float ydpi;
298        nsecs_t refresh;
299        bool connected;
300        bool hasFbComp;
301        bool hasOvComp;
302        size_t capacity;
303        hwc_display_contents_1* list;
304        hwc_layer_1* framebufferTarget;
305        buffer_handle_t fbTargetHandle;
306    };
307
308    sp<SurfaceFlinger>              mFlinger;
309    framebuffer_device_t*           mFbDev;
310    struct hwc_composer_device_1*   mHwc;
311    // invariant: mLists[0] != NULL iff mHwc != NULL
312    // mLists[i>0] can be NULL. that display is to be ignored
313    struct hwc_display_contents_1*  mLists[MAX_DISPLAYS];
314    DisplayData                     mDisplayData[MAX_DISPLAYS];
315    size_t                          mNumDisplays;
316
317    cb_context*                     mCBContext;
318    EventHandler&                   mEventHandler;
319    size_t                          mVSyncCount;
320    sp<VSyncThread>                 mVSyncThread;
321    bool                            mDebugForceFakeVSync;
322    BitSet32                        mAllocatedDisplayIDs;
323
324    // protected by mLock
325    mutable Mutex mLock;
326    mutable nsecs_t mLastHwVSync;
327};
328
329// ---------------------------------------------------------------------------
330}; // namespace android
331
332#endif // ANDROID_SF_HWCOMPOSER_H
333