blob: 9f961139b7efa83fb80aa030f8ac75d3b791a9bf [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;
Mathias Agopian6b442672013-07-09 21:24:52 -070050class FloatRect;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070051class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070052class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070053class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070054
Mathias Agopiana350ff92010-08-10 17:14:02 -070055class HWComposer
56{
57public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070058 class EventHandler {
59 friend class HWComposer;
Jesse Hall1bd20e02012-08-29 10:47:52 -070060 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
Mathias Agopian148994e2012-09-19 17:31:36 -070061 virtual void onHotplugReceived(int disp, bool connected) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070062 protected:
63 virtual ~EventHandler() {}
64 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070065
Mathias Agopian1e260872012-08-08 18:35:12 -070066 enum {
Jesse Hall9e663de2013-08-16 14:28:37 -070067 NUM_BUILTIN_DISPLAYS = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
68 MAX_HWC_DISPLAYS = HWC_NUM_DISPLAY_TYPES,
69 VIRTUAL_DISPLAY_ID_BASE = HWC_DISPLAY_VIRTUAL,
Mathias Agopian1e260872012-08-08 18:35:12 -070070 };
71
Mathias Agopian8b736f12012-08-13 17:54:26 -070072 HWComposer(
73 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070074 EventHandler& handler);
Mathias Agopian8b736f12012-08-13 17:54:26 -070075
Mathias Agopiana350ff92010-08-10 17:14:02 -070076 ~HWComposer();
77
78 status_t initCheck() const;
79
Jesse Hall9e663de2013-08-16 14:28:37 -070080 // Returns a display ID starting at VIRTUAL_DISPLAY_ID_BASE, this ID is to
81 // be used with createWorkList (and all other methods requiring an ID
82 // below).
83 // IDs below NUM_BUILTIN_DISPLAYS are pre-defined and therefore are
84 // always valid.
85 // Returns -1 if an ID cannot be allocated
Mathias Agopiane60b0682012-08-21 23:34:09 -070086 int32_t allocateDisplayId();
87
Jesse Hall9e663de2013-08-16 14:28:37 -070088 // Recycles the given virtual display ID and frees the associated worklist.
89 // IDs below NUM_BUILTIN_DISPLAYS are not recycled.
Mathias Agopiane60b0682012-08-21 23:34:09 -070090 status_t freeDisplayId(int32_t id);
91
92
Mathias Agopiana350ff92010-08-10 17:14:02 -070093 // Asks the HAL what it can do
Mathias Agopiane60b0682012-08-21 23:34:09 -070094 status_t prepare();
Mathias Agopiana350ff92010-08-10 17:14:02 -070095
96 // commits the list
Mathias Agopian30bcc612012-08-22 15:39:48 -070097 status_t commit();
Mathias Agopiana350ff92010-08-10 17:14:02 -070098
Colin Cross10fbdb62012-07-12 17:56:34 -070099 // release hardware resources and blank screen
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700100 status_t release(int disp);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700101
Colin Cross10fbdb62012-07-12 17:56:34 -0700102 // acquire hardware resources and unblank screen
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700103 status_t acquire(int disp);
Colin Cross10fbdb62012-07-12 17:56:34 -0700104
Andy McFadden27ec5732012-10-02 19:04:45 -0700105 // reset state when an external, non-virtual display is disconnected
106 void disconnectDisplay(int disp);
107
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700108 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -0700109 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700110
Mathias Agopianda27af92012-09-13 18:17:13 -0700111 bool supportsFramebufferTarget() const;
112
Mathias Agopiane60b0682012-08-21 23:34:09 -0700113 // does this display have layers handled by HWC
114 bool hasHwcComposition(int32_t id) const;
115
116 // does this display have layers handled by GLES
117 bool hasGlesComposition(int32_t id) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700118
Jesse Hall80e0a392013-03-15 12:32:10 -0700119 // get the releaseFence file descriptor for a display's framebuffer layer.
Mathias Agopianda27af92012-09-13 18:17:13 -0700120 // the release fence is only valid after commit()
Jesse Hall13f01cb2013-03-20 11:37:21 -0700121 sp<Fence> getAndResetReleaseFence(int32_t id);
Mathias Agopianda27af92012-09-13 18:17:13 -0700122
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700123 // needed forward declarations
124 class LayerListIterator;
125
Mathias Agopiancde87a32012-09-13 14:09:01 -0700126 // return the visual id to be used to find a suitable EGLConfig for
127 // *ALL* displays.
128 int getVisualID() const;
129
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700130 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
Mathias Agopianda27af92012-09-13 18:17:13 -0700131 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700132 int fbCompositionComplete();
133 void fbDump(String8& result);
134
Jesse Hall851cfe82013-03-20 13:44:00 -0700135 // Set the output buffer and acquire fence for a virtual display.
136 // Returns INVALID_OPERATION if id is not a virtual display.
137 status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
138 const sp<GraphicBuffer>& buf);
139
140 // Get the retire fence for the last committed frame. This fence will
141 // signal when the h/w composer is completely finished with the frame.
142 // For physical displays, it is no longer being displayed. For virtual
143 // displays, writes to the output buffer are complete.
144 sp<Fence> getLastRetireFence(int32_t id);
145
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700146 /*
147 * Interface to hardware composer's layers functionality.
148 * This abstracts the HAL interface to layers which can evolve in
149 * incompatible ways from one release to another.
150 * The idea is that we could extend this interface as we add
151 * features to h/w composer.
152 */
153 class HWCLayerInterface {
154 protected:
155 virtual ~HWCLayerInterface() { }
156 public:
157 virtual int32_t getCompositionType() const = 0;
158 virtual uint32_t getHints() const = 0;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700159 virtual sp<Fence> getAndResetReleaseFence() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700160 virtual void setDefaultState() = 0;
161 virtual void setSkip(bool skip) = 0;
162 virtual void setBlending(uint32_t blending) = 0;
163 virtual void setTransform(uint32_t transform) = 0;
164 virtual void setFrame(const Rect& frame) = 0;
Mathias Agopian6b442672013-07-09 21:24:52 -0700165 virtual void setCrop(const FloatRect& crop) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700166 virtual void setVisibleRegionScreen(const Region& reg) = 0;
167 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700168 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800169 virtual void setPlaneAlpha(uint8_t alpha) = 0;
Mathias Agopianc3973602012-08-31 17:51:25 -0700170 virtual void onDisplayed() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700171 };
172
173 /*
174 * Interface used to implement an iterator to a list
175 * of HWCLayer.
176 */
177 class HWCLayer : public HWCLayerInterface {
178 friend class LayerListIterator;
179 // select the layer at the given index
180 virtual status_t setLayer(size_t index) = 0;
181 virtual HWCLayer* dup() = 0;
182 static HWCLayer* copy(HWCLayer *rhs) {
183 return rhs ? rhs->dup() : NULL;
184 }
185 protected:
186 virtual ~HWCLayer() { }
187 };
188
189 /*
190 * Iterator through a HWCLayer list.
191 * This behaves more or less like a forward iterator.
192 */
193 class LayerListIterator {
194 friend struct HWComposer;
195 HWCLayer* const mLayerList;
196 size_t mIndex;
197
198 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
199
200 LayerListIterator(HWCLayer* layer, size_t index)
201 : mLayerList(layer), mIndex(index) { }
202
203 // we don't allow assignment, because we don't need it for now
204 LayerListIterator& operator = (const LayerListIterator& rhs);
205
206 public:
207 // copy operators
208 LayerListIterator(const LayerListIterator& rhs)
209 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
210 }
211
212 ~LayerListIterator() { delete mLayerList; }
213
214 // pre-increment
215 LayerListIterator& operator++() {
216 mLayerList->setLayer(++mIndex);
217 return *this;
218 }
219
220 // dereference
221 HWCLayerInterface& operator * () { return *mLayerList; }
222 HWCLayerInterface* operator -> () { return mLayerList; }
223
224 // comparison
225 bool operator == (const LayerListIterator& rhs) const {
226 return mIndex == rhs.mIndex;
227 }
228 bool operator != (const LayerListIterator& rhs) const {
229 return !operator==(rhs);
230 }
231 };
232
233 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700234 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700235
236 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700237 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700238
239
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700240 // Events handling ---------------------------------------------------------
241
242 enum {
243 EVENT_VSYNC = HWC_EVENT_VSYNC
244 };
245
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700246 void eventControl(int disp, int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700247
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700248 // Query display parameters. Pass in a display index (e.g.
249 // HWC_DISPLAY_PRIMARY).
250 nsecs_t getRefreshPeriod(int disp) const;
251 nsecs_t getRefreshTimestamp(int disp) const;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800252 sp<Fence> getDisplayFence(int disp) const;
Jesse Halldb276212012-09-07 11:20:56 -0700253 uint32_t getWidth(int disp) const;
254 uint32_t getHeight(int disp) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700255 uint32_t getFormat(int disp) const;
256 float getDpiX(int disp) const;
257 float getDpiY(int disp) const;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700258 bool isConnected(int disp) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700259
Jesse Hall1c569c42013-04-05 13:44:52 -0700260 status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
261 uint32_t format);
262
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700263 // this class is only used to fake the VSync event on systems that don't
264 // have it.
265 class VSyncThread : public Thread {
266 HWComposer& mHwc;
267 mutable Mutex mLock;
268 Condition mCondition;
269 bool mEnabled;
270 mutable nsecs_t mNextFakeVSync;
271 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700272 virtual void onFirstRef();
273 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700274 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700275 VSyncThread(HWComposer& hwc);
276 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700277 };
278
279 friend class VSyncThread;
280
281 // for debugging ----------------------------------------------------------
Mathias Agopian74d211a2013-04-22 16:55:35 +0200282 void dump(String8& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700283
Mathias Agopiana350ff92010-08-10 17:14:02 -0700284private:
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700285 void loadHwcModule();
Jesse Hallef64b752013-03-18 11:28:50 -0700286 int loadFbHalModule();
Jesse Hallb685c542012-07-31 14:32:56 -0700287
Mathias Agopian1e260872012-08-08 18:35:12 -0700288 LayerListIterator getLayerIterator(int32_t id, size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700289
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700290 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700291
Jesse Hallbbd164a2012-08-21 12:05:09 -0700292 static void hook_invalidate(const struct hwc_procs* procs);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700293 static void hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700294 int64_t timestamp);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700295 static void hook_hotplug(const struct hwc_procs* procs, int disp,
296 int connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700297
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700298 inline void invalidate();
Jesse Hall1bd20e02012-08-29 10:47:52 -0700299 inline void vsync(int disp, int64_t timestamp);
300 inline void hotplug(int disp, int connected);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700301
Mathias Agopian1604f772012-09-18 21:54:42 -0700302 status_t queryDisplayProperties(int disp);
Mathias Agopian1e260872012-08-08 18:35:12 -0700303
Mathias Agopianda27af92012-09-13 18:17:13 -0700304 status_t setFramebufferTarget(int32_t id,
305 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
306
307
Mathias Agopiane60b0682012-08-21 23:34:09 -0700308 struct DisplayData {
Jesse Halla9a1b002013-02-27 16:39:25 -0800309 DisplayData();
310 ~DisplayData();
Jesse Halldb276212012-09-07 11:20:56 -0700311 uint32_t width;
312 uint32_t height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700313 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
Mathias Agopiane60b0682012-08-21 23:34:09 -0700314 float xdpi;
315 float ydpi;
316 nsecs_t refresh;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700317 bool connected;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700318 bool hasFbComp;
319 bool hasOvComp;
Mathias Agopianf4358632012-08-22 17:16:19 -0700320 size_t capacity;
321 hwc_display_contents_1* list;
Mathias Agopianda27af92012-09-13 18:17:13 -0700322 hwc_layer_1* framebufferTarget;
323 buffer_handle_t fbTargetHandle;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800324 sp<Fence> lastRetireFence; // signals when the last set op retires
325 sp<Fence> lastDisplayFence; // signals when the last set op takes
326 // effect on screen
Jesse Hall851cfe82013-03-20 13:44:00 -0700327 buffer_handle_t outbufHandle;
328 sp<Fence> outbufAcquireFence;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800329
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700330 // protected by mEventControlLock
331 int32_t events;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700332 };
333
Jesse Hall5880cc52012-06-05 23:40:32 -0700334 sp<SurfaceFlinger> mFlinger;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700335 framebuffer_device_t* mFbDev;
Jesse Hall5880cc52012-06-05 23:40:32 -0700336 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700337 // invariant: mLists[0] != NULL iff mHwc != NULL
Mathias Agopiane60b0682012-08-21 23:34:09 -0700338 // mLists[i>0] can be NULL. that display is to be ignored
Jesse Hall9e663de2013-08-16 14:28:37 -0700339 struct hwc_display_contents_1* mLists[MAX_HWC_DISPLAYS];
340 DisplayData mDisplayData[MAX_HWC_DISPLAYS];
Jesse Hall8f971ff2012-08-22 11:50:00 -0700341 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700342
Jesse Hall5880cc52012-06-05 23:40:32 -0700343 cb_context* mCBContext;
344 EventHandler& mEventHandler;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700345 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Jesse Hall5880cc52012-06-05 23:40:32 -0700346 sp<VSyncThread> mVSyncThread;
347 bool mDebugForceFakeVSync;
Mathias Agopianf4358632012-08-22 17:16:19 -0700348 BitSet32 mAllocatedDisplayIDs;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700349
350 // protected by mLock
351 mutable Mutex mLock;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700352 mutable nsecs_t mLastHwVSync[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700353
354 // thread-safe
355 mutable Mutex mEventControlLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700356};
357
Mathias Agopiana350ff92010-08-10 17:14:02 -0700358// ---------------------------------------------------------------------------
359}; // namespace android
360
361#endif // ANDROID_SF_HWCOMPOSER_H