blob: 4c520b1b40e9600c01c220e7d7164e41d4eb2cd5 [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,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070067 EventHandler& handler);
Mathias Agopian8b736f12012-08-13 17:54:26 -070068
Mathias Agopiana350ff92010-08-10 17:14:02 -070069 ~HWComposer();
70
71 status_t initCheck() const;
72
Mathias Agopiane60b0682012-08-21 23:34:09 -070073 // returns a display ID starting at MAX_DISPLAYS, this ID
74 // is to be used with createWorkList (and all other
75 // methods requiring an ID below).
76 // IDs below MAX_DISPLAY are pre-defined and therefore are always valid.
77 // returns a negative error code if an ID cannot be allocated
78 int32_t allocateDisplayId();
79
80 // recycles the given ID and frees the associated worklist.
81 // IDs below MAX_DISPLAYS are not recycled
82 status_t freeDisplayId(int32_t id);
83
84
Mathias Agopiana350ff92010-08-10 17:14:02 -070085 // Asks the HAL what it can do
Mathias Agopiane60b0682012-08-21 23:34:09 -070086 status_t prepare();
Mathias Agopiana350ff92010-08-10 17:14:02 -070087
88 // commits the list
Mathias Agopian30bcc612012-08-22 15:39:48 -070089 status_t commit();
Mathias Agopiana350ff92010-08-10 17:14:02 -070090
Colin Cross10fbdb62012-07-12 17:56:34 -070091 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070092 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070093
Colin Cross10fbdb62012-07-12 17:56:34 -070094 // acquire hardware resources and unblank screen
95 status_t acquire() const;
96
Mathias Agopian3e8b8532012-05-13 20:42:01 -070097 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -070098 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -070099
Mathias Agopiane60b0682012-08-21 23:34:09 -0700100 // does this display have layers handled by HWC
101 bool hasHwcComposition(int32_t id) const;
102
103 // does this display have layers handled by GLES
104 bool hasGlesComposition(int32_t id) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700105
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700106 // needed forward declarations
107 class LayerListIterator;
108
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700109 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
110 int fbPost(buffer_handle_t buffer);
111 int fbCompositionComplete();
112 void fbDump(String8& result);
113
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700114 /*
115 * Interface to hardware composer's layers functionality.
116 * This abstracts the HAL interface to layers which can evolve in
117 * incompatible ways from one release to another.
118 * The idea is that we could extend this interface as we add
119 * features to h/w composer.
120 */
121 class HWCLayerInterface {
122 protected:
123 virtual ~HWCLayerInterface() { }
124 public:
125 virtual int32_t getCompositionType() const = 0;
126 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700127 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700128 virtual void setDefaultState() = 0;
129 virtual void setSkip(bool skip) = 0;
130 virtual void setBlending(uint32_t blending) = 0;
131 virtual void setTransform(uint32_t transform) = 0;
132 virtual void setFrame(const Rect& frame) = 0;
133 virtual void setCrop(const Rect& crop) = 0;
134 virtual void setVisibleRegionScreen(const Region& reg) = 0;
135 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700136 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopianc3973602012-08-31 17:51:25 -0700137 virtual void onDisplayed() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700138 };
139
140 /*
141 * Interface used to implement an iterator to a list
142 * of HWCLayer.
143 */
144 class HWCLayer : public HWCLayerInterface {
145 friend class LayerListIterator;
146 // select the layer at the given index
147 virtual status_t setLayer(size_t index) = 0;
148 virtual HWCLayer* dup() = 0;
149 static HWCLayer* copy(HWCLayer *rhs) {
150 return rhs ? rhs->dup() : NULL;
151 }
152 protected:
153 virtual ~HWCLayer() { }
154 };
155
156 /*
157 * Iterator through a HWCLayer list.
158 * This behaves more or less like a forward iterator.
159 */
160 class LayerListIterator {
161 friend struct HWComposer;
162 HWCLayer* const mLayerList;
163 size_t mIndex;
164
165 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
166
167 LayerListIterator(HWCLayer* layer, size_t index)
168 : mLayerList(layer), mIndex(index) { }
169
170 // we don't allow assignment, because we don't need it for now
171 LayerListIterator& operator = (const LayerListIterator& rhs);
172
173 public:
174 // copy operators
175 LayerListIterator(const LayerListIterator& rhs)
176 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
177 }
178
179 ~LayerListIterator() { delete mLayerList; }
180
181 // pre-increment
182 LayerListIterator& operator++() {
183 mLayerList->setLayer(++mIndex);
184 return *this;
185 }
186
187 // dereference
188 HWCLayerInterface& operator * () { return *mLayerList; }
189 HWCLayerInterface* operator -> () { return mLayerList; }
190
191 // comparison
192 bool operator == (const LayerListIterator& rhs) const {
193 return mIndex == rhs.mIndex;
194 }
195 bool operator != (const LayerListIterator& rhs) const {
196 return !operator==(rhs);
197 }
198 };
199
200 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700201 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700202
203 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700204 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700205
206
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700207 // Events handling ---------------------------------------------------------
208
209 enum {
210 EVENT_VSYNC = HWC_EVENT_VSYNC
211 };
212
Mathias Agopian03e40722012-04-26 16:11:59 -0700213 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700214
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700215 // Query display parameters. Pass in a display index (e.g.
216 // HWC_DISPLAY_PRIMARY).
217 nsecs_t getRefreshPeriod(int disp) const;
218 nsecs_t getRefreshTimestamp(int disp) const;
219 uint32_t getResolutionX(int disp) const;
220 uint32_t getResolutionY(int disp) const;
221 uint32_t getFormat(int disp) const;
222 float getDpiX(int disp) const;
223 float getDpiY(int disp) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700224
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700225 // this class is only used to fake the VSync event on systems that don't
226 // have it.
227 class VSyncThread : public Thread {
228 HWComposer& mHwc;
229 mutable Mutex mLock;
230 Condition mCondition;
231 bool mEnabled;
232 mutable nsecs_t mNextFakeVSync;
233 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700234 virtual void onFirstRef();
235 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700236 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700237 VSyncThread(HWComposer& hwc);
238 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700239 };
240
241 friend class VSyncThread;
242
243 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700244 void dump(String8& out, char* scratch, size_t SIZE,
245 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700246
Mathias Agopiana350ff92010-08-10 17:14:02 -0700247private:
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700248 void loadHwcModule();
249 void loadFbHalModule();
Jesse Hallb685c542012-07-31 14:32:56 -0700250
Mathias Agopian1e260872012-08-08 18:35:12 -0700251 LayerListIterator getLayerIterator(int32_t id, size_t index);
252 size_t getNumLayers(int32_t id) const;
Mathias Agopian31d28432012-04-03 16:31:39 -0700253
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700254 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700255
Jesse Hallbbd164a2012-08-21 12:05:09 -0700256 static void hook_invalidate(const struct hwc_procs* procs);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700257 static void hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700258 int64_t timestamp);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700259 static void hook_hotplug(const struct hwc_procs* procs, int disp,
260 int connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700261
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700262 inline void invalidate();
Jesse Hall1bd20e02012-08-29 10:47:52 -0700263 inline void vsync(int disp, int64_t timestamp);
264 inline void hotplug(int disp, int connected);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700265
Jesse Hall1bd20e02012-08-29 10:47:52 -0700266 void queryDisplayProperties(int disp);
Mathias Agopian1e260872012-08-08 18:35:12 -0700267
Mathias Agopiane60b0682012-08-21 23:34:09 -0700268 struct DisplayData {
269 DisplayData() : xdpi(0), ydpi(0), refresh(0),
Mathias Agopianf4358632012-08-22 17:16:19 -0700270 hasFbComp(false), hasOvComp(false),
271 capacity(0), list(NULL) { }
272 ~DisplayData() {
273 free(list);
274 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700275 uint32_t xres;
276 uint32_t yres;
277 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
Mathias Agopiane60b0682012-08-21 23:34:09 -0700278 float xdpi;
279 float ydpi;
280 nsecs_t refresh;
281 bool hasFbComp;
282 bool hasOvComp;
Mathias Agopianf4358632012-08-22 17:16:19 -0700283 size_t capacity;
284 hwc_display_contents_1* list;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700285 };
286
Jesse Hall5880cc52012-06-05 23:40:32 -0700287 sp<SurfaceFlinger> mFlinger;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700288 framebuffer_device_t* mFbDev;
Jesse Hall5880cc52012-06-05 23:40:32 -0700289 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700290 // invariant: mLists[0] != NULL iff mHwc != NULL
Mathias Agopiane60b0682012-08-21 23:34:09 -0700291 // mLists[i>0] can be NULL. that display is to be ignored
Jesse Hallb685c542012-07-31 14:32:56 -0700292 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Mathias Agopiane60b0682012-08-21 23:34:09 -0700293 DisplayData mDisplayData[MAX_DISPLAYS];
Jesse Hall8f971ff2012-08-22 11:50:00 -0700294 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700295
Jesse Hall5880cc52012-06-05 23:40:32 -0700296 cb_context* mCBContext;
297 EventHandler& mEventHandler;
Jesse Hall5880cc52012-06-05 23:40:32 -0700298 size_t mVSyncCount;
299 sp<VSyncThread> mVSyncThread;
300 bool mDebugForceFakeVSync;
Mathias Agopianf4358632012-08-22 17:16:19 -0700301 BitSet32 mAllocatedDisplayIDs;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700302
303 // protected by mLock
304 mutable Mutex mLock;
305 mutable nsecs_t mLastHwVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700306};
307
Mathias Agopiana350ff92010-08-10 17:14:02 -0700308// ---------------------------------------------------------------------------
309}; // namespace android
310
311#endif // ANDROID_SF_HWCOMPOSER_H