blob: c2fff4fd980ba21c35d155c229f4b6a00b2c0e11 [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
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 <EGL/egl.h>
24
Mathias Agopian3e8b8532012-05-13 20:42:01 -070025#include <hardware/hwcomposer_defs.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070026
Mathias Agopian921e6ac2012-07-23 23:11:29 -070027#include <utils/Condition.h>
28#include <utils/Mutex.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070029#include <utils/StrongPointer.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070030#include <utils/Thread.h>
31#include <utils/Timers.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070032#include <utils/Vector.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070033
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070034extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
35 const struct timespec *request,
36 struct timespec *remain);
37
Jesse Hall5880cc52012-06-05 23:40:32 -070038struct hwc_composer_device_1;
39struct hwc_layer_list_1;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070040struct hwc_procs;
41
Mathias Agopiana350ff92010-08-10 17:14:02 -070042namespace android {
43// ---------------------------------------------------------------------------
44
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045class GraphicBuffer;
46class LayerBase;
Mathias Agopian83727852010-09-23 18:13:21 -070047class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070048class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070049
Mathias Agopiana350ff92010-08-10 17:14:02 -070050class HWComposer
51{
52public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070053 class EventHandler {
54 friend class HWComposer;
55 virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0;
56 protected:
57 virtual ~EventHandler() {}
58 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070059
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070060 HWComposer(const sp<SurfaceFlinger>& flinger,
61 EventHandler& handler, nsecs_t refreshPeriod);
Mathias Agopiana350ff92010-08-10 17:14:02 -070062 ~HWComposer();
63
64 status_t initCheck() const;
65
66 // tells the HAL what the framebuffer is
67 void setFrameBuffer(EGLDisplay dpy, EGLSurface sur);
68
Mathias Agopiana350ff92010-08-10 17:14:02 -070069 // Asks the HAL what it can do
70 status_t prepare() const;
71
Mathias Agopian7ee4cd52011-09-02 12:22:39 -070072 // disable hwc until next createWorkList
73 status_t disable();
74
Mathias Agopiana350ff92010-08-10 17:14:02 -070075 // commits the list
76 status_t commit() const;
77
Colin Cross10fbdb62012-07-12 17:56:34 -070078 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070079 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070080
Colin Cross10fbdb62012-07-12 17:56:34 -070081 // acquire hardware resources and unblank screen
82 status_t acquire() const;
83
Mathias Agopian3e8b8532012-05-13 20:42:01 -070084 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
85 status_t createWorkList(size_t numLayers);
86
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070087 // get the layer array created by createWorkList()
Mathias Agopian45721772010-08-12 15:03:26 -070088 size_t getNumLayers() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070089
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070090 // get number of layers of the given type as updated in prepare().
91 // type is HWC_OVERLAY or HWC_FRAMEBUFFER
Mathias Agopian9c6e2972011-09-20 17:21:56 -070092 size_t getLayerCount(int type) const;
93
Mathias Agopian3e8b8532012-05-13 20:42:01 -070094 // needed forward declarations
95 class LayerListIterator;
96
97 /*
98 * Interface to hardware composer's layers functionality.
99 * This abstracts the HAL interface to layers which can evolve in
100 * incompatible ways from one release to another.
101 * The idea is that we could extend this interface as we add
102 * features to h/w composer.
103 */
104 class HWCLayerInterface {
105 protected:
106 virtual ~HWCLayerInterface() { }
107 public:
108 virtual int32_t getCompositionType() const = 0;
109 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700110 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700111 virtual void setDefaultState() = 0;
112 virtual void setSkip(bool skip) = 0;
113 virtual void setBlending(uint32_t blending) = 0;
114 virtual void setTransform(uint32_t transform) = 0;
115 virtual void setFrame(const Rect& frame) = 0;
116 virtual void setCrop(const Rect& crop) = 0;
117 virtual void setVisibleRegionScreen(const Region& reg) = 0;
118 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700119 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700120 };
121
122 /*
123 * Interface used to implement an iterator to a list
124 * of HWCLayer.
125 */
126 class HWCLayer : public HWCLayerInterface {
127 friend class LayerListIterator;
128 // select the layer at the given index
129 virtual status_t setLayer(size_t index) = 0;
130 virtual HWCLayer* dup() = 0;
131 static HWCLayer* copy(HWCLayer *rhs) {
132 return rhs ? rhs->dup() : NULL;
133 }
134 protected:
135 virtual ~HWCLayer() { }
136 };
137
138 /*
139 * Iterator through a HWCLayer list.
140 * This behaves more or less like a forward iterator.
141 */
142 class LayerListIterator {
143 friend struct HWComposer;
144 HWCLayer* const mLayerList;
145 size_t mIndex;
146
147 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
148
149 LayerListIterator(HWCLayer* layer, size_t index)
150 : mLayerList(layer), mIndex(index) { }
151
152 // we don't allow assignment, because we don't need it for now
153 LayerListIterator& operator = (const LayerListIterator& rhs);
154
155 public:
156 // copy operators
157 LayerListIterator(const LayerListIterator& rhs)
158 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
159 }
160
161 ~LayerListIterator() { delete mLayerList; }
162
163 // pre-increment
164 LayerListIterator& operator++() {
165 mLayerList->setLayer(++mIndex);
166 return *this;
167 }
168
169 // dereference
170 HWCLayerInterface& operator * () { return *mLayerList; }
171 HWCLayerInterface* operator -> () { return mLayerList; }
172
173 // comparison
174 bool operator == (const LayerListIterator& rhs) const {
175 return mIndex == rhs.mIndex;
176 }
177 bool operator != (const LayerListIterator& rhs) const {
178 return !operator==(rhs);
179 }
180 };
181
182 // Returns an iterator to the beginning of the layer list
183 LayerListIterator begin();
184
185 // Returns an iterator to the end of the layer list
186 LayerListIterator end();
187
188
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700189 // Events handling ---------------------------------------------------------
190
191 enum {
192 EVENT_VSYNC = HWC_EVENT_VSYNC
193 };
194
Mathias Agopian03e40722012-04-26 16:11:59 -0700195 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700196
197 // this class is only used to fake the VSync event on systems that don't
198 // have it.
199 class VSyncThread : public Thread {
200 HWComposer& mHwc;
201 mutable Mutex mLock;
202 Condition mCondition;
203 bool mEnabled;
204 mutable nsecs_t mNextFakeVSync;
205 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700206 virtual void onFirstRef();
207 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700208 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700209 VSyncThread(HWComposer& hwc);
210 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700211 };
212
213 friend class VSyncThread;
214
215 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700216 void dump(String8& out, char* scratch, size_t SIZE,
217 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700218
Mathias Agopiana350ff92010-08-10 17:14:02 -0700219private:
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700220 LayerListIterator getLayerIterator(size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700221
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700222 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700223
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700224 static void hook_invalidate(struct hwc_procs* procs);
Mathias Agopian31d28432012-04-03 16:31:39 -0700225 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp);
226
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700227 inline void invalidate();
228 inline void vsync(int dpy, int64_t timestamp);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700229
Jesse Hall5880cc52012-06-05 23:40:32 -0700230 sp<SurfaceFlinger> mFlinger;
231 hw_module_t const* mModule;
232 struct hwc_composer_device_1* mHwc;
233 struct hwc_layer_list_1* mList;
234 size_t mCapacity;
235 mutable size_t mNumOVLayers;
236 mutable size_t mNumFBLayers;
237 EGLDisplay mDpy;
238 EGLSurface mSur;
239 cb_context* mCBContext;
240 EventHandler& mEventHandler;
241 nsecs_t mRefreshPeriod;
242 size_t mVSyncCount;
243 sp<VSyncThread> mVSyncThread;
244 bool mDebugForceFakeVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700245};
246
Mathias Agopiana350ff92010-08-10 17:14:02 -0700247// ---------------------------------------------------------------------------
248}; // namespace android
249
250#endif // ANDROID_SF_HWCOMPOSER_H