blob: 8080d3bb4dbe7930fcc41e984c07be29b80f4e5d [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 Agopianc7d14e22011-08-01 16:32:21 -070031
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070032extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
33 const struct timespec *request,
34 struct timespec *remain);
35
Jesse Hall5880cc52012-06-05 23:40:32 -070036struct hwc_composer_device_1;
Jesse Hallb685c542012-07-31 14:32:56 -070037struct hwc_display_contents_1;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070038struct hwc_procs;
Mathias Agopian8b736f12012-08-13 17:54:26 -070039struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070040
Mathias Agopiana350ff92010-08-10 17:14:02 -070041namespace android {
42// ---------------------------------------------------------------------------
43
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044class GraphicBuffer;
45class LayerBase;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070046class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070047class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070048class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070049
Mathias Agopiana350ff92010-08-10 17:14:02 -070050class HWComposer
51{
52public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070053 class EventHandler {
54 friend class HWComposer;
55 virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0;
56 protected:
57 virtual ~EventHandler() {}
58 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070059
Mathias Agopian1e260872012-08-08 18:35:12 -070060 enum {
Jesse Hall8f971ff2012-08-22 11:50:00 -070061 MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
Mathias Agopian1e260872012-08-08 18:35:12 -070062 };
63
Mathias Agopian8b736f12012-08-13 17:54:26 -070064 HWComposer(
65 const sp<SurfaceFlinger>& flinger,
66 EventHandler& handler,
67 framebuffer_device_t const* fbDev);
68
Mathias Agopiana350ff92010-08-10 17:14:02 -070069 ~HWComposer();
70
71 status_t initCheck() const;
72
Mathias Agopiana350ff92010-08-10 17:14:02 -070073 // Asks the HAL what it can do
74 status_t prepare() const;
75
Mathias Agopian7ee4cd52011-09-02 12:22:39 -070076 // disable hwc until next createWorkList
77 status_t disable();
78
Mathias Agopiana350ff92010-08-10 17:14:02 -070079 // commits the list
Jesse Hall34a09ba2012-07-29 22:35:34 -070080 status_t commit(void* fbDisplay, void* fbSurface) const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070081
Colin Cross10fbdb62012-07-12 17:56:34 -070082 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070083 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070084
Colin Cross10fbdb62012-07-12 17:56:34 -070085 // acquire hardware resources and unblank screen
86 status_t acquire() const;
87
Mathias Agopian3e8b8532012-05-13 20:42:01 -070088 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -070089 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -070090
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070091 // get number of layers of the given type as updated in prepare().
92 // type is HWC_OVERLAY or HWC_FRAMEBUFFER
Mathias Agopian1e260872012-08-08 18:35:12 -070093 size_t getLayerCount(int32_t id, int type) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -070094
Mathias Agopian3e8b8532012-05-13 20:42:01 -070095 // needed forward declarations
96 class LayerListIterator;
97
98 /*
99 * Interface to hardware composer's layers functionality.
100 * This abstracts the HAL interface to layers which can evolve in
101 * incompatible ways from one release to another.
102 * The idea is that we could extend this interface as we add
103 * features to h/w composer.
104 */
105 class HWCLayerInterface {
106 protected:
107 virtual ~HWCLayerInterface() { }
108 public:
109 virtual int32_t getCompositionType() const = 0;
110 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700111 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700112 virtual void setDefaultState() = 0;
113 virtual void setSkip(bool skip) = 0;
114 virtual void setBlending(uint32_t blending) = 0;
115 virtual void setTransform(uint32_t transform) = 0;
116 virtual void setFrame(const Rect& frame) = 0;
117 virtual void setCrop(const Rect& crop) = 0;
118 virtual void setVisibleRegionScreen(const Region& reg) = 0;
119 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700120 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700121 };
122
123 /*
124 * Interface used to implement an iterator to a list
125 * of HWCLayer.
126 */
127 class HWCLayer : public HWCLayerInterface {
128 friend class LayerListIterator;
129 // select the layer at the given index
130 virtual status_t setLayer(size_t index) = 0;
131 virtual HWCLayer* dup() = 0;
132 static HWCLayer* copy(HWCLayer *rhs) {
133 return rhs ? rhs->dup() : NULL;
134 }
135 protected:
136 virtual ~HWCLayer() { }
137 };
138
139 /*
140 * Iterator through a HWCLayer list.
141 * This behaves more or less like a forward iterator.
142 */
143 class LayerListIterator {
144 friend struct HWComposer;
145 HWCLayer* const mLayerList;
146 size_t mIndex;
147
148 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
149
150 LayerListIterator(HWCLayer* layer, size_t index)
151 : mLayerList(layer), mIndex(index) { }
152
153 // we don't allow assignment, because we don't need it for now
154 LayerListIterator& operator = (const LayerListIterator& rhs);
155
156 public:
157 // copy operators
158 LayerListIterator(const LayerListIterator& rhs)
159 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
160 }
161
162 ~LayerListIterator() { delete mLayerList; }
163
164 // pre-increment
165 LayerListIterator& operator++() {
166 mLayerList->setLayer(++mIndex);
167 return *this;
168 }
169
170 // dereference
171 HWCLayerInterface& operator * () { return *mLayerList; }
172 HWCLayerInterface* operator -> () { return mLayerList; }
173
174 // comparison
175 bool operator == (const LayerListIterator& rhs) const {
176 return mIndex == rhs.mIndex;
177 }
178 bool operator != (const LayerListIterator& rhs) const {
179 return !operator==(rhs);
180 }
181 };
182
183 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700184 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700185
186 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700187 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700188
189
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700190 // Events handling ---------------------------------------------------------
191
192 enum {
193 EVENT_VSYNC = HWC_EVENT_VSYNC
194 };
195
Mathias Agopian03e40722012-04-26 16:11:59 -0700196 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700197
Mathias Agopian888c8222012-08-04 21:10:38 -0700198 nsecs_t getRefreshPeriod() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700199 nsecs_t getRefreshTimestamp() const;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700200 float getDpiX() const;
201 float getDpiY() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700202
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700203 // this class is only used to fake the VSync event on systems that don't
204 // have it.
205 class VSyncThread : public Thread {
206 HWComposer& mHwc;
207 mutable Mutex mLock;
208 Condition mCondition;
209 bool mEnabled;
210 mutable nsecs_t mNextFakeVSync;
211 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700212 virtual void onFirstRef();
213 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700214 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700215 VSyncThread(HWComposer& hwc);
216 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700217 };
218
219 friend class VSyncThread;
220
221 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700222 void dump(String8& out, char* scratch, size_t SIZE,
223 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700224
Mathias Agopiana350ff92010-08-10 17:14:02 -0700225private:
Jesse Hallb685c542012-07-31 14:32:56 -0700226
Mathias Agopian1e260872012-08-08 18:35:12 -0700227 LayerListIterator getLayerIterator(int32_t id, size_t index);
228 size_t getNumLayers(int32_t id) const;
Mathias Agopian31d28432012-04-03 16:31:39 -0700229
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700230 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700231
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700232 static void hook_invalidate(struct hwc_procs* procs);
Mathias Agopian31d28432012-04-03 16:31:39 -0700233 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp);
234
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700235 inline void invalidate();
236 inline void vsync(int dpy, int64_t timestamp);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700237
Mathias Agopian1e260872012-08-08 18:35:12 -0700238
Jesse Hall5880cc52012-06-05 23:40:32 -0700239 sp<SurfaceFlinger> mFlinger;
240 hw_module_t const* mModule;
241 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700242 // invariant: mLists[0] != NULL iff mHwc != NULL
243 // TODO: decide whether mLists[i>0] should be non-NULL when display i is
244 // not attached/enabled.
245 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Jesse Hall8f971ff2012-08-22 11:50:00 -0700246 size_t mNumDisplays;
Mathias Agopian1e260872012-08-08 18:35:12 -0700247
Jesse Hall5880cc52012-06-05 23:40:32 -0700248 size_t mCapacity;
249 mutable size_t mNumOVLayers;
250 mutable size_t mNumFBLayers;
Jesse Hall5880cc52012-06-05 23:40:32 -0700251 cb_context* mCBContext;
252 EventHandler& mEventHandler;
253 nsecs_t mRefreshPeriod;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700254 float mDpiX;
255 float mDpiY;
Jesse Hall5880cc52012-06-05 23:40:32 -0700256 size_t mVSyncCount;
257 sp<VSyncThread> mVSyncThread;
258 bool mDebugForceFakeVSync;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700259
260 // protected by mLock
261 mutable Mutex mLock;
262 mutable nsecs_t mLastHwVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700263};
264
Mathias Agopiana350ff92010-08-10 17:14:02 -0700265// ---------------------------------------------------------------------------
266}; // namespace android
267
268#endif // ANDROID_SF_HWCOMPOSER_H