blob: 8852ab6f83c51b838b1d0126eae23f680887d00e [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
Mathias Agopian3e8b8532012-05-13 20:42:01 -070023#include <hardware/hwcomposer_defs.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070024
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025#include <utils/Condition.h>
26#include <utils/Mutex.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070027#include <utils/StrongPointer.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070028#include <utils/Thread.h>
29#include <utils/Timers.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070030#include <utils/Vector.h>
Mathias Agopiane60b0682012-08-21 23:34:09 -070031#include <utils/BitSet.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070032
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070033extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
34 const struct timespec *request,
35 struct timespec *remain);
36
Jesse Hall5880cc52012-06-05 23:40:32 -070037struct hwc_composer_device_1;
Jesse Hallb685c542012-07-31 14:32:56 -070038struct hwc_display_contents_1;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070039struct hwc_procs;
Mathias Agopian8b736f12012-08-13 17:54:26 -070040struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070041
Mathias Agopiana350ff92010-08-10 17:14:02 -070042namespace android {
43// ---------------------------------------------------------------------------
44
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045class GraphicBuffer;
46class LayerBase;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070047class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070048class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070049class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070050
Mathias Agopiana350ff92010-08-10 17:14:02 -070051class HWComposer
52{
53public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070054 class EventHandler {
55 friend class HWComposer;
Jesse Hall1bd20e02012-08-29 10:47:52 -070056 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070057 protected:
58 virtual ~EventHandler() {}
59 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070060
Mathias Agopian1e260872012-08-08 18:35:12 -070061 enum {
Jesse Hall8f971ff2012-08-22 11:50:00 -070062 MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
Mathias Agopian1e260872012-08-08 18:35:12 -070063 };
64
Mathias Agopian8b736f12012-08-13 17:54:26 -070065 HWComposer(
66 const sp<SurfaceFlinger>& flinger,
67 EventHandler& handler,
68 framebuffer_device_t const* fbDev);
69
Mathias Agopiana350ff92010-08-10 17:14:02 -070070 ~HWComposer();
71
72 status_t initCheck() const;
73
Mathias Agopiane60b0682012-08-21 23:34:09 -070074 // returns a display ID starting at MAX_DISPLAYS, this ID
75 // is to be used with createWorkList (and all other
76 // methods requiring an ID below).
77 // IDs below MAX_DISPLAY are pre-defined and therefore are always valid.
78 // returns a negative error code if an ID cannot be allocated
79 int32_t allocateDisplayId();
80
81 // recycles the given ID and frees the associated worklist.
82 // IDs below MAX_DISPLAYS are not recycled
83 status_t freeDisplayId(int32_t id);
84
85
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 // Asks the HAL what it can do
Mathias Agopiane60b0682012-08-21 23:34:09 -070087 status_t prepare();
Mathias Agopiana350ff92010-08-10 17:14:02 -070088
89 // commits the list
Mathias Agopian30bcc612012-08-22 15:39:48 -070090 status_t commit();
Mathias Agopiana350ff92010-08-10 17:14:02 -070091
Colin Cross10fbdb62012-07-12 17:56:34 -070092 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070093 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070094
Colin Cross10fbdb62012-07-12 17:56:34 -070095 // acquire hardware resources and unblank screen
96 status_t acquire() const;
97
Mathias Agopian3e8b8532012-05-13 20:42:01 -070098 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -070099 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700100
Mathias Agopiane60b0682012-08-21 23:34:09 -0700101 // does this display have layers handled by HWC
102 bool hasHwcComposition(int32_t id) const;
103
104 // does this display have layers handled by GLES
105 bool hasGlesComposition(int32_t id) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700106
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700107 // needed forward declarations
108 class LayerListIterator;
109
110 /*
111 * Interface to hardware composer's layers functionality.
112 * This abstracts the HAL interface to layers which can evolve in
113 * incompatible ways from one release to another.
114 * The idea is that we could extend this interface as we add
115 * features to h/w composer.
116 */
117 class HWCLayerInterface {
118 protected:
119 virtual ~HWCLayerInterface() { }
120 public:
121 virtual int32_t getCompositionType() const = 0;
122 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700123 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700124 virtual void setDefaultState() = 0;
125 virtual void setSkip(bool skip) = 0;
126 virtual void setBlending(uint32_t blending) = 0;
127 virtual void setTransform(uint32_t transform) = 0;
128 virtual void setFrame(const Rect& frame) = 0;
129 virtual void setCrop(const Rect& crop) = 0;
130 virtual void setVisibleRegionScreen(const Region& reg) = 0;
131 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700132 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700133 };
134
135 /*
136 * Interface used to implement an iterator to a list
137 * of HWCLayer.
138 */
139 class HWCLayer : public HWCLayerInterface {
140 friend class LayerListIterator;
141 // select the layer at the given index
142 virtual status_t setLayer(size_t index) = 0;
143 virtual HWCLayer* dup() = 0;
144 static HWCLayer* copy(HWCLayer *rhs) {
145 return rhs ? rhs->dup() : NULL;
146 }
147 protected:
148 virtual ~HWCLayer() { }
149 };
150
151 /*
152 * Iterator through a HWCLayer list.
153 * This behaves more or less like a forward iterator.
154 */
155 class LayerListIterator {
156 friend struct HWComposer;
157 HWCLayer* const mLayerList;
158 size_t mIndex;
159
160 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
161
162 LayerListIterator(HWCLayer* layer, size_t index)
163 : mLayerList(layer), mIndex(index) { }
164
165 // we don't allow assignment, because we don't need it for now
166 LayerListIterator& operator = (const LayerListIterator& rhs);
167
168 public:
169 // copy operators
170 LayerListIterator(const LayerListIterator& rhs)
171 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
172 }
173
174 ~LayerListIterator() { delete mLayerList; }
175
176 // pre-increment
177 LayerListIterator& operator++() {
178 mLayerList->setLayer(++mIndex);
179 return *this;
180 }
181
182 // dereference
183 HWCLayerInterface& operator * () { return *mLayerList; }
184 HWCLayerInterface* operator -> () { return mLayerList; }
185
186 // comparison
187 bool operator == (const LayerListIterator& rhs) const {
188 return mIndex == rhs.mIndex;
189 }
190 bool operator != (const LayerListIterator& rhs) const {
191 return !operator==(rhs);
192 }
193 };
194
195 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700196 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700197
198 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700199 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700200
201
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700202 // Events handling ---------------------------------------------------------
203
204 enum {
205 EVENT_VSYNC = HWC_EVENT_VSYNC
206 };
207
Mathias Agopian03e40722012-04-26 16:11:59 -0700208 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700209
Mathias Agopian888c8222012-08-04 21:10:38 -0700210 nsecs_t getRefreshPeriod() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700211 nsecs_t getRefreshTimestamp() const;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700212 float getDpiX() const;
213 float getDpiY() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700214
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700215 // this class is only used to fake the VSync event on systems that don't
216 // have it.
217 class VSyncThread : public Thread {
218 HWComposer& mHwc;
219 mutable Mutex mLock;
220 Condition mCondition;
221 bool mEnabled;
222 mutable nsecs_t mNextFakeVSync;
223 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700224 virtual void onFirstRef();
225 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700226 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700227 VSyncThread(HWComposer& hwc);
228 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700229 };
230
231 friend class VSyncThread;
232
233 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700234 void dump(String8& out, char* scratch, size_t SIZE,
235 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700236
Mathias Agopiana350ff92010-08-10 17:14:02 -0700237private:
Jesse Hallb685c542012-07-31 14:32:56 -0700238
Mathias Agopian1e260872012-08-08 18:35:12 -0700239 LayerListIterator getLayerIterator(int32_t id, size_t index);
240 size_t getNumLayers(int32_t id) const;
Mathias Agopian31d28432012-04-03 16:31:39 -0700241
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700242 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700243
Jesse Hallbbd164a2012-08-21 12:05:09 -0700244 static void hook_invalidate(const struct hwc_procs* procs);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700245 static void hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700246 int64_t timestamp);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700247 static void hook_hotplug(const struct hwc_procs* procs, int disp,
248 int connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700249
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700250 inline void invalidate();
Jesse Hall1bd20e02012-08-29 10:47:52 -0700251 inline void vsync(int disp, int64_t timestamp);
252 inline void hotplug(int disp, int connected);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700253
Jesse Hall1bd20e02012-08-29 10:47:52 -0700254 void queryDisplayProperties(int disp);
Mathias Agopian1e260872012-08-08 18:35:12 -0700255
Mathias Agopiane60b0682012-08-21 23:34:09 -0700256 struct DisplayData {
257 DisplayData() : xdpi(0), ydpi(0), refresh(0),
Mathias Agopianf4358632012-08-22 17:16:19 -0700258 hasFbComp(false), hasOvComp(false),
259 capacity(0), list(NULL) { }
260 ~DisplayData() {
261 free(list);
262 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700263 float xdpi;
264 float ydpi;
265 nsecs_t refresh;
266 bool hasFbComp;
267 bool hasOvComp;
Mathias Agopianf4358632012-08-22 17:16:19 -0700268 size_t capacity;
269 hwc_display_contents_1* list;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700270 };
271
Jesse Hall5880cc52012-06-05 23:40:32 -0700272 sp<SurfaceFlinger> mFlinger;
273 hw_module_t const* mModule;
274 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700275 // invariant: mLists[0] != NULL iff mHwc != NULL
Mathias Agopiane60b0682012-08-21 23:34:09 -0700276 // mLists[i>0] can be NULL. that display is to be ignored
Jesse Hallb685c542012-07-31 14:32:56 -0700277 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Mathias Agopiane60b0682012-08-21 23:34:09 -0700278 DisplayData mDisplayData[MAX_DISPLAYS];
Jesse Hall8f971ff2012-08-22 11:50:00 -0700279 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700280
Jesse Hall5880cc52012-06-05 23:40:32 -0700281 cb_context* mCBContext;
282 EventHandler& mEventHandler;
Jesse Hall5880cc52012-06-05 23:40:32 -0700283 size_t mVSyncCount;
284 sp<VSyncThread> mVSyncThread;
285 bool mDebugForceFakeVSync;
Mathias Agopianf4358632012-08-22 17:16:19 -0700286 BitSet32 mAllocatedDisplayIDs;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700287
288 // protected by mLock
289 mutable Mutex mLock;
290 mutable nsecs_t mLastHwVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700291};
292
Mathias Agopiana350ff92010-08-10 17:14:02 -0700293// ---------------------------------------------------------------------------
294}; // namespace android
295
296#endif // ANDROID_SF_HWCOMPOSER_H