blob: 52171f34935df4a49b257386a4b08e3af19ca737 [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;
39
Mathias Agopiana350ff92010-08-10 17:14:02 -070040namespace android {
41// ---------------------------------------------------------------------------
42
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043class GraphicBuffer;
44class LayerBase;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070045class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070046class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070047class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070048
Mathias Agopiana350ff92010-08-10 17:14:02 -070049class HWComposer
50{
51public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070052 class EventHandler {
53 friend class HWComposer;
54 virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0;
55 protected:
56 virtual ~EventHandler() {}
57 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070058
Mathias Agopian1e260872012-08-08 18:35:12 -070059 enum {
60 MAIN = 0,
61 HDMI = 1,
62 MAX_DISPLAYS
63 };
64
Mathias Agopian888c8222012-08-04 21:10:38 -070065 HWComposer(const sp<SurfaceFlinger>& flinger, EventHandler& handler);
Mathias Agopiana350ff92010-08-10 17:14:02 -070066 ~HWComposer();
67
68 status_t initCheck() const;
69
Mathias Agopiana350ff92010-08-10 17:14:02 -070070 // Asks the HAL what it can do
71 status_t prepare() const;
72
Mathias Agopian7ee4cd52011-09-02 12:22:39 -070073 // disable hwc until next createWorkList
74 status_t disable();
75
Mathias Agopiana350ff92010-08-10 17:14:02 -070076 // commits the list
Jesse Hall34a09ba2012-07-29 22:35:34 -070077 status_t commit(void* fbDisplay, void* fbSurface) const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070078
Colin Cross10fbdb62012-07-12 17:56:34 -070079 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070080 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070081
Colin Cross10fbdb62012-07-12 17:56:34 -070082 // acquire hardware resources and unblank screen
83 status_t acquire() const;
84
Mathias Agopian3e8b8532012-05-13 20:42:01 -070085 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
Mathias Agopian1e260872012-08-08 18:35:12 -070086 status_t createWorkList(int32_t id, size_t numLayers);
Mathias Agopiana350ff92010-08-10 17:14:02 -070087
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070088 // get number of layers of the given type as updated in prepare().
89 // type is HWC_OVERLAY or HWC_FRAMEBUFFER
Mathias Agopian1e260872012-08-08 18:35:12 -070090 size_t getLayerCount(int32_t id, int type) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -070091
Mathias Agopian3e8b8532012-05-13 20:42:01 -070092 // needed forward declarations
93 class LayerListIterator;
94
95 /*
96 * Interface to hardware composer's layers functionality.
97 * This abstracts the HAL interface to layers which can evolve in
98 * incompatible ways from one release to another.
99 * The idea is that we could extend this interface as we add
100 * features to h/w composer.
101 */
102 class HWCLayerInterface {
103 protected:
104 virtual ~HWCLayerInterface() { }
105 public:
106 virtual int32_t getCompositionType() const = 0;
107 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700108 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700109 virtual void setDefaultState() = 0;
110 virtual void setSkip(bool skip) = 0;
111 virtual void setBlending(uint32_t blending) = 0;
112 virtual void setTransform(uint32_t transform) = 0;
113 virtual void setFrame(const Rect& frame) = 0;
114 virtual void setCrop(const Rect& crop) = 0;
115 virtual void setVisibleRegionScreen(const Region& reg) = 0;
116 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700117 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700118 };
119
120 /*
121 * Interface used to implement an iterator to a list
122 * of HWCLayer.
123 */
124 class HWCLayer : public HWCLayerInterface {
125 friend class LayerListIterator;
126 // select the layer at the given index
127 virtual status_t setLayer(size_t index) = 0;
128 virtual HWCLayer* dup() = 0;
129 static HWCLayer* copy(HWCLayer *rhs) {
130 return rhs ? rhs->dup() : NULL;
131 }
132 protected:
133 virtual ~HWCLayer() { }
134 };
135
136 /*
137 * Iterator through a HWCLayer list.
138 * This behaves more or less like a forward iterator.
139 */
140 class LayerListIterator {
141 friend struct HWComposer;
142 HWCLayer* const mLayerList;
143 size_t mIndex;
144
145 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
146
147 LayerListIterator(HWCLayer* layer, size_t index)
148 : mLayerList(layer), mIndex(index) { }
149
150 // we don't allow assignment, because we don't need it for now
151 LayerListIterator& operator = (const LayerListIterator& rhs);
152
153 public:
154 // copy operators
155 LayerListIterator(const LayerListIterator& rhs)
156 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
157 }
158
159 ~LayerListIterator() { delete mLayerList; }
160
161 // pre-increment
162 LayerListIterator& operator++() {
163 mLayerList->setLayer(++mIndex);
164 return *this;
165 }
166
167 // dereference
168 HWCLayerInterface& operator * () { return *mLayerList; }
169 HWCLayerInterface* operator -> () { return mLayerList; }
170
171 // comparison
172 bool operator == (const LayerListIterator& rhs) const {
173 return mIndex == rhs.mIndex;
174 }
175 bool operator != (const LayerListIterator& rhs) const {
176 return !operator==(rhs);
177 }
178 };
179
180 // Returns an iterator to the beginning of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700181 LayerListIterator begin(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700182
183 // Returns an iterator to the end of the layer list
Mathias Agopian1e260872012-08-08 18:35:12 -0700184 LayerListIterator end(int32_t id);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700185
186
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700187 // Events handling ---------------------------------------------------------
188
189 enum {
190 EVENT_VSYNC = HWC_EVENT_VSYNC
191 };
192
Mathias Agopian03e40722012-04-26 16:11:59 -0700193 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700194
Mathias Agopian888c8222012-08-04 21:10:38 -0700195 nsecs_t getRefreshPeriod() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700196 nsecs_t getRefreshTimestamp() const;
197
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700198 // this class is only used to fake the VSync event on systems that don't
199 // have it.
200 class VSyncThread : public Thread {
201 HWComposer& mHwc;
202 mutable Mutex mLock;
203 Condition mCondition;
204 bool mEnabled;
205 mutable nsecs_t mNextFakeVSync;
206 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700207 virtual void onFirstRef();
208 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700209 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700210 VSyncThread(HWComposer& hwc);
211 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700212 };
213
214 friend class VSyncThread;
215
216 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700217 void dump(String8& out, char* scratch, size_t SIZE,
218 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700219
Mathias Agopiana350ff92010-08-10 17:14:02 -0700220private:
Jesse Hallb685c542012-07-31 14:32:56 -0700221
Mathias Agopian1e260872012-08-08 18:35:12 -0700222 LayerListIterator getLayerIterator(int32_t id, size_t index);
223 size_t getNumLayers(int32_t id) const;
Mathias Agopian31d28432012-04-03 16:31:39 -0700224
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700225 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700226
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700227 static void hook_invalidate(struct hwc_procs* procs);
Mathias Agopian31d28432012-04-03 16:31:39 -0700228 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp);
229
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700230 inline void invalidate();
231 inline void vsync(int dpy, int64_t timestamp);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700232
Mathias Agopian1e260872012-08-08 18:35:12 -0700233
Jesse Hall5880cc52012-06-05 23:40:32 -0700234 sp<SurfaceFlinger> mFlinger;
235 hw_module_t const* mModule;
236 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700237 // invariant: mLists[0] != NULL iff mHwc != NULL
238 // TODO: decide whether mLists[i>0] should be non-NULL when display i is
239 // not attached/enabled.
240 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Mathias Agopian1e260872012-08-08 18:35:12 -0700241
Jesse Hall5880cc52012-06-05 23:40:32 -0700242 size_t mCapacity;
243 mutable size_t mNumOVLayers;
244 mutable size_t mNumFBLayers;
Jesse Hall5880cc52012-06-05 23:40:32 -0700245 cb_context* mCBContext;
246 EventHandler& mEventHandler;
247 nsecs_t mRefreshPeriod;
248 size_t mVSyncCount;
249 sp<VSyncThread> mVSyncThread;
250 bool mDebugForceFakeVSync;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700251
252 // protected by mLock
253 mutable Mutex mLock;
254 mutable nsecs_t mLastHwVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700255};
256
Mathias Agopiana350ff92010-08-10 17:14:02 -0700257// ---------------------------------------------------------------------------
258}; // namespace android
259
260#endif // ANDROID_SF_HWCOMPOSER_H