blob: 346526aae4443487d0a34f307dfed1692893c8a4 [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
Jamie Gennis2ec3e072012-11-11 16:24:33 -080025#include <ui/Fence.h>
26
27#include <utils/BitSet.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070028#include <utils/Condition.h>
29#include <utils/Mutex.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070030#include <utils/StrongPointer.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070031#include <utils/Thread.h>
32#include <utils/Timers.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070033#include <utils/Vector.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070034
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070035extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
36 const struct timespec *request,
37 struct timespec *remain);
38
Jesse Hall5880cc52012-06-05 23:40:32 -070039struct hwc_composer_device_1;
Jesse Hallb685c542012-07-31 14:32:56 -070040struct hwc_display_contents_1;
Mathias Agopianda27af92012-09-13 18:17:13 -070041struct hwc_layer_1;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070042struct hwc_procs;
Mathias Agopian8b736f12012-08-13 17:54:26 -070043struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070044
Mathias Agopiana350ff92010-08-10 17:14:02 -070045namespace android {
46// ---------------------------------------------------------------------------
47
Mathias Agopian921e6ac2012-07-23 23:11:29 -070048class GraphicBuffer;
Mathias Agopianda27af92012-09-13 18:17:13 -070049class Fence;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070050class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070051class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070052class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070053
Mathias Agopiana350ff92010-08-10 17:14:02 -070054class HWComposer
55{
56public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070057 class EventHandler {
58 friend class HWComposer;
Jesse Hall1bd20e02012-08-29 10:47:52 -070059 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
Mathias Agopian148994e2012-09-19 17:31:36 -070060 virtual void onHotplugReceived(int disp, bool connected) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070061 protected:
62 virtual ~EventHandler() {}
63 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070064
Mathias Agopian1e260872012-08-08 18:35:12 -070065 enum {
Jesse Hall8f971ff2012-08-22 11:50:00 -070066 MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
Mathias Agopian1e260872012-08-08 18:35:12 -070067 };
68
Mathias Agopian8b736f12012-08-13 17:54:26 -070069 HWComposer(
70 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070071 EventHandler& handler);
Mathias Agopian8b736f12012-08-13 17:54:26 -070072
Mathias Agopiana350ff92010-08-10 17:14:02 -070073 ~HWComposer();
74
75 status_t initCheck() const;
76
Mathias Agopiane60b0682012-08-21 23:34:09 -070077 // returns a display ID starting at MAX_DISPLAYS, this ID
78 // is to be used with createWorkList (and all other
79 // methods requiring an ID below).
80 // IDs below MAX_DISPLAY are pre-defined and therefore are always valid.
81 // returns a negative error code if an ID cannot be allocated
82 int32_t allocateDisplayId();
83
84 // recycles the given ID and frees the associated worklist.
85 // IDs below MAX_DISPLAYS are not recycled
86 status_t freeDisplayId(int32_t id);
87
88
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 // Asks the HAL what it can do
Mathias Agopiane60b0682012-08-21 23:34:09 -070090 status_t prepare();
Mathias Agopiana350ff92010-08-10 17:14:02 -070091
92 // commits the list
Mathias Agopian30bcc612012-08-22 15:39:48 -070093 status_t commit();
Mathias Agopiana350ff92010-08-10 17:14:02 -070094
Colin Cross10fbdb62012-07-12 17:56:34 -070095 // release hardware resources and blank screen
Mathias Agopian81cd5d32012-10-04 02:34:38 -070096 status_t release(int disp);
Mathias Agopiana350ff92010-08-10 17:14:02 -070097
Colin Cross10fbdb62012-07-12 17:56:34 -070098 // acquire hardware resources and unblank screen
Mathias Agopian81cd5d32012-10-04 02:34:38 -070099 status_t acquire(int disp);
Colin Cross10fbdb62012-07-12 17:56:34 -0700100
Andy McFadden27ec5732012-10-02 19:04:45 -0700101 // reset state when an external, non-virtual display is disconnected
102 void disconnectDisplay(int disp);
103
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700104 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -0700105 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700106
Mathias Agopianda27af92012-09-13 18:17:13 -0700107 bool supportsFramebufferTarget() const;
108
Mathias Agopiane60b0682012-08-21 23:34:09 -0700109 // does this display have layers handled by HWC
110 bool hasHwcComposition(int32_t id) const;
111
112 // does this display have layers handled by GLES
113 bool hasGlesComposition(int32_t id) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700114
Jesse Hall80e0a392013-03-15 12:32:10 -0700115 // get the releaseFence file descriptor for a display's framebuffer layer.
Mathias Agopianda27af92012-09-13 18:17:13 -0700116 // the release fence is only valid after commit()
Jesse Hall13f01cb2013-03-20 11:37:21 -0700117 sp<Fence> getAndResetReleaseFence(int32_t id);
Mathias Agopianda27af92012-09-13 18:17:13 -0700118
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700119 // needed forward declarations
120 class LayerListIterator;
121
Mathias Agopiancde87a32012-09-13 14:09:01 -0700122 // return the visual id to be used to find a suitable EGLConfig for
123 // *ALL* displays.
124 int getVisualID() const;
125
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700126 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
Mathias Agopianda27af92012-09-13 18:17:13 -0700127 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700128 int fbCompositionComplete();
129 void fbDump(String8& result);
130
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700131 /*
132 * Interface to hardware composer's layers functionality.
133 * This abstracts the HAL interface to layers which can evolve in
134 * incompatible ways from one release to another.
135 * The idea is that we could extend this interface as we add
136 * features to h/w composer.
137 */
138 class HWCLayerInterface {
139 protected:
140 virtual ~HWCLayerInterface() { }
141 public:
142 virtual int32_t getCompositionType() const = 0;
143 virtual uint32_t getHints() const = 0;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700144 virtual sp<Fence> getAndResetReleaseFence() = 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 Agopian9f8386e2013-01-29 18:56:42 -0800154 virtual void setPlaneAlpha(uint8_t alpha) = 0;
Mathias Agopianc3973602012-08-31 17:51:25 -0700155 virtual void onDisplayed() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700156 };
157
158 /*
159 * Interface used to implement an iterator to a list
160 * of HWCLayer.
161 */
162 class HWCLayer : public HWCLayerInterface {
163 friend class LayerListIterator;
164 // select the layer at the given index
165 virtual status_t setLayer(size_t index) = 0;
166 virtual HWCLayer* dup() = 0;
167 static HWCLayer* copy(HWCLayer *rhs) {
168 return rhs ? rhs->dup() : NULL;
169 }
170 protected:
171 virtual ~HWCLayer() { }
172 };
173
174 /*
175 * Iterator through a HWCLayer list.
176 * This behaves more or less like a forward iterator.
177 */
178 class LayerListIterator {
179 friend struct HWComposer;
180 HWCLayer* const mLayerList;
181 size_t mIndex;
182
183 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
184
185 LayerListIterator(HWCLayer* layer, size_t index)
186 : mLayerList(layer), mIndex(index) { }
187
188 // we don't allow assignment, because we don't need it for now
189 LayerListIterator& operator = (const LayerListIterator& rhs);
190
191 public:
192 // copy operators
193 LayerListIterator(const LayerListIterator& rhs)
194 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
195 }
196
197 ~LayerListIterator() { delete mLayerList; }
198
199 // pre-increment
200 LayerListIterator& operator++() {
201 mLayerList->setLayer(++mIndex);
202 return *this;
203 }
204
205 // dereference
206 HWCLayerInterface& operator * () { return *mLayerList; }
207 HWCLayerInterface* operator -> () { return mLayerList; }
208
209 // comparison
210 bool operator == (const LayerListIterator& rhs) const {
211 return mIndex == rhs.mIndex;
212 }
213 bool operator != (const LayerListIterator& rhs) const {
214 return !operator==(rhs);
215 }
216 };
217
218 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700219 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700220
221 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700222 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700223
224
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700225 // Events handling ---------------------------------------------------------
226
227 enum {
228 EVENT_VSYNC = HWC_EVENT_VSYNC
229 };
230
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700231 void eventControl(int disp, int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700232
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700233 // Query display parameters. Pass in a display index (e.g.
234 // HWC_DISPLAY_PRIMARY).
235 nsecs_t getRefreshPeriod(int disp) const;
236 nsecs_t getRefreshTimestamp(int disp) const;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800237 sp<Fence> getDisplayFence(int disp) const;
Jesse Halldb276212012-09-07 11:20:56 -0700238 uint32_t getWidth(int disp) const;
239 uint32_t getHeight(int disp) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700240 uint32_t getFormat(int disp) const;
241 float getDpiX(int disp) const;
242 float getDpiY(int disp) const;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700243 bool isConnected(int disp) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700244
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700245 // this class is only used to fake the VSync event on systems that don't
246 // have it.
247 class VSyncThread : public Thread {
248 HWComposer& mHwc;
249 mutable Mutex mLock;
250 Condition mCondition;
251 bool mEnabled;
252 mutable nsecs_t mNextFakeVSync;
253 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700254 virtual void onFirstRef();
255 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700256 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700257 VSyncThread(HWComposer& hwc);
258 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700259 };
260
261 friend class VSyncThread;
262
263 // for debugging ----------------------------------------------------------
Mathias Agopiancb558572012-10-04 15:58:54 -0700264 void dump(String8& out, char* scratch, size_t SIZE) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700265
Mathias Agopiana350ff92010-08-10 17:14:02 -0700266private:
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700267 void loadHwcModule();
Jesse Hallef64b752013-03-18 11:28:50 -0700268 int loadFbHalModule();
Jesse Hallb685c542012-07-31 14:32:56 -0700269
Mathias Agopian1e260872012-08-08 18:35:12 -0700270 LayerListIterator getLayerIterator(int32_t id, size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700271
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700272 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700273
Jesse Hallbbd164a2012-08-21 12:05:09 -0700274 static void hook_invalidate(const struct hwc_procs* procs);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700275 static void hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700276 int64_t timestamp);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700277 static void hook_hotplug(const struct hwc_procs* procs, int disp,
278 int connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700279
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700280 inline void invalidate();
Jesse Hall1bd20e02012-08-29 10:47:52 -0700281 inline void vsync(int disp, int64_t timestamp);
282 inline void hotplug(int disp, int connected);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700283
Mathias Agopian1604f772012-09-18 21:54:42 -0700284 status_t queryDisplayProperties(int disp);
Mathias Agopian1e260872012-08-08 18:35:12 -0700285
Mathias Agopianda27af92012-09-13 18:17:13 -0700286 status_t setFramebufferTarget(int32_t id,
287 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
288
289
Mathias Agopiane60b0682012-08-21 23:34:09 -0700290 struct DisplayData {
Jesse Halla9a1b002013-02-27 16:39:25 -0800291 DisplayData();
292 ~DisplayData();
Jesse Halldb276212012-09-07 11:20:56 -0700293 uint32_t width;
294 uint32_t height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700295 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
Mathias Agopiane60b0682012-08-21 23:34:09 -0700296 float xdpi;
297 float ydpi;
298 nsecs_t refresh;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700299 bool connected;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700300 bool hasFbComp;
301 bool hasOvComp;
Mathias Agopianf4358632012-08-22 17:16:19 -0700302 size_t capacity;
303 hwc_display_contents_1* list;
Mathias Agopianda27af92012-09-13 18:17:13 -0700304 hwc_layer_1* framebufferTarget;
305 buffer_handle_t fbTargetHandle;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800306 sp<Fence> lastRetireFence; // signals when the last set op retires
307 sp<Fence> lastDisplayFence; // signals when the last set op takes
308 // effect on screen
309
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700310 // protected by mEventControlLock
311 int32_t events;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700312 };
313
Jesse Hall5880cc52012-06-05 23:40:32 -0700314 sp<SurfaceFlinger> mFlinger;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700315 framebuffer_device_t* mFbDev;
Jesse Hall5880cc52012-06-05 23:40:32 -0700316 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700317 // invariant: mLists[0] != NULL iff mHwc != NULL
Mathias Agopiane60b0682012-08-21 23:34:09 -0700318 // mLists[i>0] can be NULL. that display is to be ignored
Jesse Hallb685c542012-07-31 14:32:56 -0700319 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Mathias Agopiane60b0682012-08-21 23:34:09 -0700320 DisplayData mDisplayData[MAX_DISPLAYS];
Jesse Hall8f971ff2012-08-22 11:50:00 -0700321 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700322
Jesse Hall5880cc52012-06-05 23:40:32 -0700323 cb_context* mCBContext;
324 EventHandler& mEventHandler;
Jesse Hall5880cc52012-06-05 23:40:32 -0700325 size_t mVSyncCount;
326 sp<VSyncThread> mVSyncThread;
327 bool mDebugForceFakeVSync;
Mathias Agopianf4358632012-08-22 17:16:19 -0700328 BitSet32 mAllocatedDisplayIDs;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700329
330 // protected by mLock
331 mutable Mutex mLock;
332 mutable nsecs_t mLastHwVSync;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700333
334 // thread-safe
335 mutable Mutex mEventControlLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700336};
337
Mathias Agopiana350ff92010-08-10 17:14:02 -0700338// ---------------------------------------------------------------------------
339}; // namespace android
340
341#endif // ANDROID_SF_HWCOMPOSER_H