blob: 5e0b3d85ea06a1ccca6ec80a6df7d7d222c14402 [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 Agopianda27af92012-09-13 18:17:13 -070048class Fence;
Mathias Agopian6b442672013-07-09 21:24:52 -070049class FloatRect;
Jesse Hall399184a2014-03-03 15:42:54 -080050class GraphicBuffer;
51class NativeHandle;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070052class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070053class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070054class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070055
Mathias Agopiana350ff92010-08-10 17:14:02 -070056class HWComposer
57{
58public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070059 class EventHandler {
60 friend class HWComposer;
Jesse Hall1bd20e02012-08-29 10:47:52 -070061 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
Mathias Agopian148994e2012-09-19 17:31:36 -070062 virtual void onHotplugReceived(int disp, bool connected) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070063 protected:
64 virtual ~EventHandler() {}
65 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070066
Mathias Agopian1e260872012-08-08 18:35:12 -070067 enum {
Jesse Hall9e663de2013-08-16 14:28:37 -070068 NUM_BUILTIN_DISPLAYS = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
69 MAX_HWC_DISPLAYS = HWC_NUM_DISPLAY_TYPES,
70 VIRTUAL_DISPLAY_ID_BASE = HWC_DISPLAY_VIRTUAL,
Mathias Agopian1e260872012-08-08 18:35:12 -070071 };
72
Mathias Agopian8b736f12012-08-13 17:54:26 -070073 HWComposer(
74 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070075 EventHandler& handler);
Mathias Agopian8b736f12012-08-13 17:54:26 -070076
Mathias Agopiana350ff92010-08-10 17:14:02 -070077 ~HWComposer();
78
79 status_t initCheck() const;
80
Jesse Hall9e663de2013-08-16 14:28:37 -070081 // Returns a display ID starting at VIRTUAL_DISPLAY_ID_BASE, this ID is to
82 // be used with createWorkList (and all other methods requiring an ID
83 // below).
84 // IDs below NUM_BUILTIN_DISPLAYS are pre-defined and therefore are
85 // always valid.
86 // Returns -1 if an ID cannot be allocated
Mathias Agopiane60b0682012-08-21 23:34:09 -070087 int32_t allocateDisplayId();
88
Jesse Hall9e663de2013-08-16 14:28:37 -070089 // Recycles the given virtual display ID and frees the associated worklist.
90 // IDs below NUM_BUILTIN_DISPLAYS are not recycled.
Mathias Agopiane60b0682012-08-21 23:34:09 -070091 status_t freeDisplayId(int32_t id);
92
93
Mathias Agopiana350ff92010-08-10 17:14:02 -070094 // Asks the HAL what it can do
Mathias Agopiane60b0682012-08-21 23:34:09 -070095 status_t prepare();
Mathias Agopiana350ff92010-08-10 17:14:02 -070096
97 // commits the list
Mathias Agopian30bcc612012-08-22 15:39:48 -070098 status_t commit();
Mathias Agopiana350ff92010-08-10 17:14:02 -070099
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700100 // set power mode
101 status_t setPowerMode(int disp, int mode);
Colin Cross10fbdb62012-07-12 17:56:34 -0700102
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700103 // set active config
104 status_t setActiveConfig(int disp, int mode);
105
Andy McFadden27ec5732012-10-02 19:04:45 -0700106 // reset state when an external, non-virtual display is disconnected
107 void disconnectDisplay(int disp);
108
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700109 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -0700110 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700111
Mathias Agopianda27af92012-09-13 18:17:13 -0700112 bool supportsFramebufferTarget() const;
113
Mathias Agopiane60b0682012-08-21 23:34:09 -0700114 // does this display have layers handled by HWC
115 bool hasHwcComposition(int32_t id) const;
116
117 // does this display have layers handled by GLES
118 bool hasGlesComposition(int32_t id) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700119
Jesse Hall80e0a392013-03-15 12:32:10 -0700120 // get the releaseFence file descriptor for a display's framebuffer layer.
Mathias Agopianda27af92012-09-13 18:17:13 -0700121 // the release fence is only valid after commit()
Jesse Hall13f01cb2013-03-20 11:37:21 -0700122 sp<Fence> getAndResetReleaseFence(int32_t id);
Mathias Agopianda27af92012-09-13 18:17:13 -0700123
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700124 // needed forward declarations
125 class LayerListIterator;
126
Mathias Agopiancde87a32012-09-13 14:09:01 -0700127 // return the visual id to be used to find a suitable EGLConfig for
128 // *ALL* displays.
129 int getVisualID() const;
130
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700131 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
Mathias Agopianda27af92012-09-13 18:17:13 -0700132 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700133 int fbCompositionComplete();
134 void fbDump(String8& result);
135
Jesse Hall851cfe82013-03-20 13:44:00 -0700136 // Set the output buffer and acquire fence for a virtual display.
137 // Returns INVALID_OPERATION if id is not a virtual display.
138 status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
139 const sp<GraphicBuffer>& buf);
140
141 // Get the retire fence for the last committed frame. This fence will
142 // signal when the h/w composer is completely finished with the frame.
143 // For physical displays, it is no longer being displayed. For virtual
144 // displays, writes to the output buffer are complete.
Andy McFadden41d67d72014-04-25 16:58:34 -0700145 sp<Fence> getLastRetireFence(int32_t id) const;
Jesse Hall851cfe82013-03-20 13:44:00 -0700146
Riley Andrews03414a12014-07-01 14:22:59 -0700147 status_t setCursorPositionAsync(int32_t id, const Rect &pos);
148
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700149 /*
150 * Interface to hardware composer's layers functionality.
151 * This abstracts the HAL interface to layers which can evolve in
152 * incompatible ways from one release to another.
153 * The idea is that we could extend this interface as we add
154 * features to h/w composer.
155 */
156 class HWCLayerInterface {
157 protected:
158 virtual ~HWCLayerInterface() { }
159 public:
160 virtual int32_t getCompositionType() const = 0;
161 virtual uint32_t getHints() const = 0;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700162 virtual sp<Fence> getAndResetReleaseFence() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700163 virtual void setDefaultState() = 0;
164 virtual void setSkip(bool skip) = 0;
Riley Andrews03414a12014-07-01 14:22:59 -0700165 virtual void setIsCursorLayerHint(bool isCursor = true) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700166 virtual void setBlending(uint32_t blending) = 0;
167 virtual void setTransform(uint32_t transform) = 0;
168 virtual void setFrame(const Rect& frame) = 0;
Mathias Agopian6b442672013-07-09 21:24:52 -0700169 virtual void setCrop(const FloatRect& crop) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700170 virtual void setVisibleRegionScreen(const Region& reg) = 0;
Dan Stozaee44edd2015-03-23 15:50:23 -0700171 virtual void setSurfaceDamage(const Region& reg) = 0;
Jesse Hall399184a2014-03-03 15:42:54 -0800172 virtual void setSidebandStream(const sp<NativeHandle>& stream) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700173 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700174 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800175 virtual void setPlaneAlpha(uint8_t alpha) = 0;
Mathias Agopianc3973602012-08-31 17:51:25 -0700176 virtual void onDisplayed() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700177 };
178
179 /*
180 * Interface used to implement an iterator to a list
181 * of HWCLayer.
182 */
183 class HWCLayer : public HWCLayerInterface {
184 friend class LayerListIterator;
185 // select the layer at the given index
186 virtual status_t setLayer(size_t index) = 0;
187 virtual HWCLayer* dup() = 0;
188 static HWCLayer* copy(HWCLayer *rhs) {
189 return rhs ? rhs->dup() : NULL;
190 }
191 protected:
192 virtual ~HWCLayer() { }
193 };
194
195 /*
196 * Iterator through a HWCLayer list.
197 * This behaves more or less like a forward iterator.
198 */
199 class LayerListIterator {
Dan Albertaeb8ab82014-09-09 15:07:07 -0700200 friend class HWComposer;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700201 HWCLayer* const mLayerList;
202 size_t mIndex;
203
204 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
205
206 LayerListIterator(HWCLayer* layer, size_t index)
207 : mLayerList(layer), mIndex(index) { }
208
209 // we don't allow assignment, because we don't need it for now
210 LayerListIterator& operator = (const LayerListIterator& rhs);
211
212 public:
213 // copy operators
214 LayerListIterator(const LayerListIterator& rhs)
215 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
216 }
217
218 ~LayerListIterator() { delete mLayerList; }
219
220 // pre-increment
221 LayerListIterator& operator++() {
222 mLayerList->setLayer(++mIndex);
223 return *this;
224 }
225
226 // dereference
227 HWCLayerInterface& operator * () { return *mLayerList; }
228 HWCLayerInterface* operator -> () { return mLayerList; }
229
230 // comparison
231 bool operator == (const LayerListIterator& rhs) const {
232 return mIndex == rhs.mIndex;
233 }
234 bool operator != (const LayerListIterator& rhs) const {
235 return !operator==(rhs);
236 }
237 };
238
239 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700240 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700241
242 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700243 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700244
245
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700246 // Events handling ---------------------------------------------------------
247
248 enum {
249 EVENT_VSYNC = HWC_EVENT_VSYNC
250 };
251
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700252 void eventControl(int disp, int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700253
Dan Stoza7f7da322014-05-02 15:26:25 -0700254 struct DisplayConfig {
255 uint32_t width;
256 uint32_t height;
257 float xdpi;
258 float ydpi;
259 nsecs_t refresh;
Dan Stozaf2699fc2015-08-31 12:06:48 -0700260 int colorTransform;
Dan Stoza7f7da322014-05-02 15:26:25 -0700261 };
262
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700263 // Query display parameters. Pass in a display index (e.g.
264 // HWC_DISPLAY_PRIMARY).
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700265 nsecs_t getRefreshTimestamp(int disp) const;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800266 sp<Fence> getDisplayFence(int disp) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700267 uint32_t getFormat(int disp) const;
268 bool isConnected(int disp) const;
269
270 // These return the values for the current config of a given display index.
271 // To get the values for all configs, use getConfigs below.
Jesse Halldb276212012-09-07 11:20:56 -0700272 uint32_t getWidth(int disp) const;
273 uint32_t getHeight(int disp) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700274 float getDpiX(int disp) const;
275 float getDpiY(int disp) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700276 nsecs_t getRefreshPeriod(int disp) const;
277
278 const Vector<DisplayConfig>& getConfigs(int disp) const;
279 size_t getCurrentConfig(int disp) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700280
Jesse Hall1c569c42013-04-05 13:44:52 -0700281 status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
282 uint32_t format);
283
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700284 // this class is only used to fake the VSync event on systems that don't
285 // have it.
286 class VSyncThread : public Thread {
287 HWComposer& mHwc;
288 mutable Mutex mLock;
289 Condition mCondition;
290 bool mEnabled;
291 mutable nsecs_t mNextFakeVSync;
292 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700293 virtual void onFirstRef();
294 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700295 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700296 VSyncThread(HWComposer& hwc);
297 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700298 };
299
300 friend class VSyncThread;
301
302 // for debugging ----------------------------------------------------------
Mathias Agopian74d211a2013-04-22 16:55:35 +0200303 void dump(String8& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700304
Mathias Agopiana350ff92010-08-10 17:14:02 -0700305private:
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700306 void loadHwcModule();
Jesse Hallef64b752013-03-18 11:28:50 -0700307 int loadFbHalModule();
Jesse Hallb685c542012-07-31 14:32:56 -0700308
Mathias Agopian1e260872012-08-08 18:35:12 -0700309 LayerListIterator getLayerIterator(int32_t id, size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700310
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700311 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700312
Jesse Hallbbd164a2012-08-21 12:05:09 -0700313 static void hook_invalidate(const struct hwc_procs* procs);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700314 static void hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700315 int64_t timestamp);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700316 static void hook_hotplug(const struct hwc_procs* procs, int disp,
317 int connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700318
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700319 inline void invalidate();
Jesse Hall1bd20e02012-08-29 10:47:52 -0700320 inline void vsync(int disp, int64_t timestamp);
321 inline void hotplug(int disp, int connected);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700322
Mathias Agopian1604f772012-09-18 21:54:42 -0700323 status_t queryDisplayProperties(int disp);
Mathias Agopian1e260872012-08-08 18:35:12 -0700324
Mathias Agopianda27af92012-09-13 18:17:13 -0700325 status_t setFramebufferTarget(int32_t id,
326 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
327
Mathias Agopiane60b0682012-08-21 23:34:09 -0700328 struct DisplayData {
Jesse Halla9a1b002013-02-27 16:39:25 -0800329 DisplayData();
330 ~DisplayData();
Dan Stoza7f7da322014-05-02 15:26:25 -0700331 Vector<DisplayConfig> configs;
332 size_t currentConfig;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700333 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
Mathias Agopianf5a33922012-09-19 18:16:22 -0700334 bool connected;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700335 bool hasFbComp;
336 bool hasOvComp;
Mathias Agopianf4358632012-08-22 17:16:19 -0700337 size_t capacity;
338 hwc_display_contents_1* list;
Mathias Agopianda27af92012-09-13 18:17:13 -0700339 hwc_layer_1* framebufferTarget;
340 buffer_handle_t fbTargetHandle;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800341 sp<Fence> lastRetireFence; // signals when the last set op retires
342 sp<Fence> lastDisplayFence; // signals when the last set op takes
343 // effect on screen
Jesse Hall851cfe82013-03-20 13:44:00 -0700344 buffer_handle_t outbufHandle;
345 sp<Fence> outbufAcquireFence;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800346
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700347 // protected by mEventControlLock
348 int32_t events;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700349 };
350
Jesse Hall5880cc52012-06-05 23:40:32 -0700351 sp<SurfaceFlinger> mFlinger;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700352 framebuffer_device_t* mFbDev;
Jesse Hall5880cc52012-06-05 23:40:32 -0700353 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700354 // invariant: mLists[0] != NULL iff mHwc != NULL
Mathias Agopiane60b0682012-08-21 23:34:09 -0700355 // mLists[i>0] can be NULL. that display is to be ignored
Jesse Hall9e663de2013-08-16 14:28:37 -0700356 struct hwc_display_contents_1* mLists[MAX_HWC_DISPLAYS];
357 DisplayData mDisplayData[MAX_HWC_DISPLAYS];
Manoj Kumar AVMe04e4ed2015-06-11 14:18:14 -0700358 // protect mDisplayData from races between prepare and dump
359 mutable Mutex mDisplayLock;
Jesse Hall8f971ff2012-08-22 11:50:00 -0700360 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700361
Jesse Hall5880cc52012-06-05 23:40:32 -0700362 cb_context* mCBContext;
363 EventHandler& mEventHandler;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700364 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Jesse Hall5880cc52012-06-05 23:40:32 -0700365 sp<VSyncThread> mVSyncThread;
366 bool mDebugForceFakeVSync;
Mathias Agopianf4358632012-08-22 17:16:19 -0700367 BitSet32 mAllocatedDisplayIDs;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700368
369 // protected by mLock
370 mutable Mutex mLock;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700371 mutable nsecs_t mLastHwVSync[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700372
373 // thread-safe
374 mutable Mutex mEventControlLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700375};
376
Mathias Agopiana350ff92010-08-10 17:14:02 -0700377// ---------------------------------------------------------------------------
378}; // namespace android
379
380#endif // ANDROID_SF_HWCOMPOSER_H