blob: 7c674078db9a2b50d844823b44c8d19a3287e73f [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 Agopianda27af92012-09-13 18:17:13 -070039struct hwc_layer_1;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070040struct hwc_procs;
Mathias Agopian8b736f12012-08-13 17:54:26 -070041struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070042
Mathias Agopiana350ff92010-08-10 17:14:02 -070043namespace android {
44// ---------------------------------------------------------------------------
45
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046class GraphicBuffer;
Mathias Agopianda27af92012-09-13 18:17:13 -070047class Fence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070048class LayerBase;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070049class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070050class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070051class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070052
Mathias Agopiana350ff92010-08-10 17:14:02 -070053class HWComposer
54{
55public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070056 class EventHandler {
57 friend class HWComposer;
Jesse Hall1bd20e02012-08-29 10:47:52 -070058 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
Mathias Agopian148994e2012-09-19 17:31:36 -070059 virtual void onHotplugReceived(int disp, bool connected) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070060 protected:
61 virtual ~EventHandler() {}
62 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070063
Mathias Agopian1e260872012-08-08 18:35:12 -070064 enum {
Jesse Hall8f971ff2012-08-22 11:50:00 -070065 MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
Mathias Agopian1e260872012-08-08 18:35:12 -070066 };
67
Mathias Agopian8b736f12012-08-13 17:54:26 -070068 HWComposer(
69 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070070 EventHandler& handler);
Mathias Agopian8b736f12012-08-13 17:54:26 -070071
Mathias Agopiana350ff92010-08-10 17:14:02 -070072 ~HWComposer();
73
74 status_t initCheck() const;
75
Mathias Agopiane60b0682012-08-21 23:34:09 -070076 // 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
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 // Asks the HAL what it can do
Mathias Agopiane60b0682012-08-21 23:34:09 -070089 status_t prepare();
Mathias Agopiana350ff92010-08-10 17:14:02 -070090
91 // commits the list
Mathias Agopian30bcc612012-08-22 15:39:48 -070092 status_t commit();
Mathias Agopiana350ff92010-08-10 17:14:02 -070093
Colin Cross10fbdb62012-07-12 17:56:34 -070094 // release hardware resources and blank screen
Mathias Agopian81cd5d32012-10-04 02:34:38 -070095 status_t release(int disp);
Mathias Agopiana350ff92010-08-10 17:14:02 -070096
Colin Cross10fbdb62012-07-12 17:56:34 -070097 // acquire hardware resources and unblank screen
Mathias Agopian81cd5d32012-10-04 02:34:38 -070098 status_t acquire(int disp);
Colin Cross10fbdb62012-07-12 17:56:34 -070099
Andy McFadden27ec5732012-10-02 19:04:45 -0700100 // reset state when an external, non-virtual display is disconnected
101 void disconnectDisplay(int disp);
102
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700103 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -0700104 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105
Mathias Agopianda27af92012-09-13 18:17:13 -0700106 bool supportsFramebufferTarget() const;
107
Mathias Agopiane60b0682012-08-21 23:34:09 -0700108 // does this display have layers handled by HWC
109 bool hasHwcComposition(int32_t id) const;
110
111 // does this display have layers handled by GLES
112 bool hasGlesComposition(int32_t id) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700113
Mathias Agopianda27af92012-09-13 18:17:13 -0700114 // get the releaseFence file descriptor for the given display
115 // the release fence is only valid after commit()
116 int getAndResetReleaseFenceFd(int32_t id);
117
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700118 // needed forward declarations
119 class LayerListIterator;
120
Mathias Agopiancde87a32012-09-13 14:09:01 -0700121 // return the visual id to be used to find a suitable EGLConfig for
122 // *ALL* displays.
123 int getVisualID() const;
124
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700125 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
Mathias Agopianda27af92012-09-13 18:17:13 -0700126 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700127 int fbCompositionComplete();
128 void fbDump(String8& result);
129
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700130 /*
131 * Interface to hardware composer's layers functionality.
132 * This abstracts the HAL interface to layers which can evolve in
133 * incompatible ways from one release to another.
134 * The idea is that we could extend this interface as we add
135 * features to h/w composer.
136 */
137 class HWCLayerInterface {
138 protected:
139 virtual ~HWCLayerInterface() { }
140 public:
141 virtual int32_t getCompositionType() const = 0;
142 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700143 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopianee932d02012-11-14 14:41:42 -0800144 virtual void setPerFrameDefaultState() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700145 virtual void setDefaultState() = 0;
146 virtual void setSkip(bool skip) = 0;
147 virtual void setBlending(uint32_t blending) = 0;
148 virtual void setTransform(uint32_t transform) = 0;
149 virtual void setFrame(const Rect& frame) = 0;
150 virtual void setCrop(const Rect& crop) = 0;
151 virtual void setVisibleRegionScreen(const Region& reg) = 0;
152 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700153 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopianc3973602012-08-31 17:51:25 -0700154 virtual void onDisplayed() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700155 };
156
157 /*
158 * Interface used to implement an iterator to a list
159 * of HWCLayer.
160 */
161 class HWCLayer : public HWCLayerInterface {
162 friend class LayerListIterator;
163 // select the layer at the given index
164 virtual status_t setLayer(size_t index) = 0;
165 virtual HWCLayer* dup() = 0;
166 static HWCLayer* copy(HWCLayer *rhs) {
167 return rhs ? rhs->dup() : NULL;
168 }
169 protected:
170 virtual ~HWCLayer() { }
171 };
172
173 /*
174 * Iterator through a HWCLayer list.
175 * This behaves more or less like a forward iterator.
176 */
177 class LayerListIterator {
178 friend struct HWComposer;
179 HWCLayer* const mLayerList;
180 size_t mIndex;
181
182 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
183
184 LayerListIterator(HWCLayer* layer, size_t index)
185 : mLayerList(layer), mIndex(index) { }
186
187 // we don't allow assignment, because we don't need it for now
188 LayerListIterator& operator = (const LayerListIterator& rhs);
189
190 public:
191 // copy operators
192 LayerListIterator(const LayerListIterator& rhs)
193 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
194 }
195
196 ~LayerListIterator() { delete mLayerList; }
197
198 // pre-increment
199 LayerListIterator& operator++() {
200 mLayerList->setLayer(++mIndex);
201 return *this;
202 }
203
204 // dereference
205 HWCLayerInterface& operator * () { return *mLayerList; }
206 HWCLayerInterface* operator -> () { return mLayerList; }
207
208 // comparison
209 bool operator == (const LayerListIterator& rhs) const {
210 return mIndex == rhs.mIndex;
211 }
212 bool operator != (const LayerListIterator& rhs) const {
213 return !operator==(rhs);
214 }
215 };
216
217 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700218 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700219
220 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700221 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700222
223
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700224 // Events handling ---------------------------------------------------------
225
226 enum {
227 EVENT_VSYNC = HWC_EVENT_VSYNC
228 };
229
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700230 void eventControl(int disp, int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700231
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700232 // Query display parameters. Pass in a display index (e.g.
233 // HWC_DISPLAY_PRIMARY).
234 nsecs_t getRefreshPeriod(int disp) const;
235 nsecs_t getRefreshTimestamp(int disp) const;
Jesse Halldb276212012-09-07 11:20:56 -0700236 uint32_t getWidth(int disp) const;
237 uint32_t getHeight(int disp) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700238 uint32_t getFormat(int disp) const;
239 float getDpiX(int disp) const;
240 float getDpiY(int disp) const;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700241 bool isConnected(int disp) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700242
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700243 // this class is only used to fake the VSync event on systems that don't
244 // have it.
245 class VSyncThread : public Thread {
246 HWComposer& mHwc;
247 mutable Mutex mLock;
248 Condition mCondition;
249 bool mEnabled;
250 mutable nsecs_t mNextFakeVSync;
251 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700252 virtual void onFirstRef();
253 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700254 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700255 VSyncThread(HWComposer& hwc);
256 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700257 };
258
259 friend class VSyncThread;
260
261 // for debugging ----------------------------------------------------------
Mathias Agopiancb558572012-10-04 15:58:54 -0700262 void dump(String8& out, char* scratch, size_t SIZE) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700263
Mathias Agopiana350ff92010-08-10 17:14:02 -0700264private:
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700265 void loadHwcModule();
266 void loadFbHalModule();
Jesse Hallb685c542012-07-31 14:32:56 -0700267
Mathias Agopian1e260872012-08-08 18:35:12 -0700268 LayerListIterator getLayerIterator(int32_t id, size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700269
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700270 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700271
Jesse Hallbbd164a2012-08-21 12:05:09 -0700272 static void hook_invalidate(const struct hwc_procs* procs);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700273 static void hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700274 int64_t timestamp);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700275 static void hook_hotplug(const struct hwc_procs* procs, int disp,
276 int connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700277
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700278 inline void invalidate();
Jesse Hall1bd20e02012-08-29 10:47:52 -0700279 inline void vsync(int disp, int64_t timestamp);
280 inline void hotplug(int disp, int connected);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700281
Mathias Agopian1604f772012-09-18 21:54:42 -0700282 status_t queryDisplayProperties(int disp);
Mathias Agopian1e260872012-08-08 18:35:12 -0700283
Mathias Agopianda27af92012-09-13 18:17:13 -0700284 status_t setFramebufferTarget(int32_t id,
285 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
286
287
Mathias Agopiane60b0682012-08-21 23:34:09 -0700288 struct DisplayData {
289 DisplayData() : xdpi(0), ydpi(0), refresh(0),
Mathias Agopianf5a33922012-09-19 18:16:22 -0700290 connected(false), hasFbComp(false), hasOvComp(false),
Mathias Agopianda27af92012-09-13 18:17:13 -0700291 capacity(0), list(NULL),
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700292 framebufferTarget(NULL), fbTargetHandle(NULL), events(0) { }
Mathias Agopianf4358632012-08-22 17:16:19 -0700293 ~DisplayData() {
294 free(list);
295 }
Jesse Halldb276212012-09-07 11:20:56 -0700296 uint32_t width;
297 uint32_t height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700298 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
Mathias Agopiane60b0682012-08-21 23:34:09 -0700299 float xdpi;
300 float ydpi;
301 nsecs_t refresh;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700302 bool connected;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700303 bool hasFbComp;
304 bool hasOvComp;
Mathias Agopianf4358632012-08-22 17:16:19 -0700305 size_t capacity;
306 hwc_display_contents_1* list;
Mathias Agopianda27af92012-09-13 18:17:13 -0700307 hwc_layer_1* framebufferTarget;
308 buffer_handle_t fbTargetHandle;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700309 // protected by mEventControlLock
310 int32_t events;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700311 };
312
Jesse Hall5880cc52012-06-05 23:40:32 -0700313 sp<SurfaceFlinger> mFlinger;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700314 framebuffer_device_t* mFbDev;
Jesse Hall5880cc52012-06-05 23:40:32 -0700315 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700316 // invariant: mLists[0] != NULL iff mHwc != NULL
Mathias Agopiane60b0682012-08-21 23:34:09 -0700317 // mLists[i>0] can be NULL. that display is to be ignored
Jesse Hallb685c542012-07-31 14:32:56 -0700318 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Mathias Agopiane60b0682012-08-21 23:34:09 -0700319 DisplayData mDisplayData[MAX_DISPLAYS];
Jesse Hall8f971ff2012-08-22 11:50:00 -0700320 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700321
Jesse Hall5880cc52012-06-05 23:40:32 -0700322 cb_context* mCBContext;
323 EventHandler& mEventHandler;
Jesse Hall5880cc52012-06-05 23:40:32 -0700324 size_t mVSyncCount;
325 sp<VSyncThread> mVSyncThread;
326 bool mDebugForceFakeVSync;
Mathias Agopianf4358632012-08-22 17:16:19 -0700327 BitSet32 mAllocatedDisplayIDs;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700328
329 // protected by mLock
330 mutable Mutex mLock;
331 mutable nsecs_t mLastHwVSync;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700332
333 // thread-safe
334 mutable Mutex mEventControlLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700335};
336
Mathias Agopiana350ff92010-08-10 17:14:02 -0700337// ---------------------------------------------------------------------------
338}; // namespace android
339
340#endif // ANDROID_SF_HWCOMPOSER_H