blob: 112e12067efbc2c8377bc0940f326ecc37279ca0 [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 Agopian888c8222012-08-04 21:10:38 -070059 HWComposer(const sp<SurfaceFlinger>& flinger, EventHandler& handler);
Mathias Agopiana350ff92010-08-10 17:14:02 -070060 ~HWComposer();
61
62 status_t initCheck() const;
63
Mathias Agopiana350ff92010-08-10 17:14:02 -070064 // Asks the HAL what it can do
65 status_t prepare() const;
66
Mathias Agopian7ee4cd52011-09-02 12:22:39 -070067 // disable hwc until next createWorkList
68 status_t disable();
69
Mathias Agopiana350ff92010-08-10 17:14:02 -070070 // commits the list
Jesse Hall34a09ba2012-07-29 22:35:34 -070071 status_t commit(void* fbDisplay, void* fbSurface) const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070072
Colin Cross10fbdb62012-07-12 17:56:34 -070073 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070074 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070075
Colin Cross10fbdb62012-07-12 17:56:34 -070076 // acquire hardware resources and unblank screen
77 status_t acquire() const;
78
Mathias Agopian3e8b8532012-05-13 20:42:01 -070079 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
80 status_t createWorkList(size_t numLayers);
81
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070082 // get the layer array created by createWorkList()
Mathias Agopian45721772010-08-12 15:03:26 -070083 size_t getNumLayers() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070084
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070085 // get number of layers of the given type as updated in prepare().
86 // type is HWC_OVERLAY or HWC_FRAMEBUFFER
Mathias Agopian9c6e2972011-09-20 17:21:56 -070087 size_t getLayerCount(int type) const;
88
Mathias Agopian3e8b8532012-05-13 20:42:01 -070089 // needed forward declarations
90 class LayerListIterator;
91
92 /*
93 * Interface to hardware composer's layers functionality.
94 * This abstracts the HAL interface to layers which can evolve in
95 * incompatible ways from one release to another.
96 * The idea is that we could extend this interface as we add
97 * features to h/w composer.
98 */
99 class HWCLayerInterface {
100 protected:
101 virtual ~HWCLayerInterface() { }
102 public:
103 virtual int32_t getCompositionType() const = 0;
104 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700105 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700106 virtual void setDefaultState() = 0;
107 virtual void setSkip(bool skip) = 0;
108 virtual void setBlending(uint32_t blending) = 0;
109 virtual void setTransform(uint32_t transform) = 0;
110 virtual void setFrame(const Rect& frame) = 0;
111 virtual void setCrop(const Rect& crop) = 0;
112 virtual void setVisibleRegionScreen(const Region& reg) = 0;
113 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700114 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700115 };
116
117 /*
118 * Interface used to implement an iterator to a list
119 * of HWCLayer.
120 */
121 class HWCLayer : public HWCLayerInterface {
122 friend class LayerListIterator;
123 // select the layer at the given index
124 virtual status_t setLayer(size_t index) = 0;
125 virtual HWCLayer* dup() = 0;
126 static HWCLayer* copy(HWCLayer *rhs) {
127 return rhs ? rhs->dup() : NULL;
128 }
129 protected:
130 virtual ~HWCLayer() { }
131 };
132
133 /*
134 * Iterator through a HWCLayer list.
135 * This behaves more or less like a forward iterator.
136 */
137 class LayerListIterator {
138 friend struct HWComposer;
139 HWCLayer* const mLayerList;
140 size_t mIndex;
141
142 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
143
144 LayerListIterator(HWCLayer* layer, size_t index)
145 : mLayerList(layer), mIndex(index) { }
146
147 // we don't allow assignment, because we don't need it for now
148 LayerListIterator& operator = (const LayerListIterator& rhs);
149
150 public:
151 // copy operators
152 LayerListIterator(const LayerListIterator& rhs)
153 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
154 }
155
156 ~LayerListIterator() { delete mLayerList; }
157
158 // pre-increment
159 LayerListIterator& operator++() {
160 mLayerList->setLayer(++mIndex);
161 return *this;
162 }
163
164 // dereference
165 HWCLayerInterface& operator * () { return *mLayerList; }
166 HWCLayerInterface* operator -> () { return mLayerList; }
167
168 // comparison
169 bool operator == (const LayerListIterator& rhs) const {
170 return mIndex == rhs.mIndex;
171 }
172 bool operator != (const LayerListIterator& rhs) const {
173 return !operator==(rhs);
174 }
175 };
176
177 // Returns an iterator to the beginning of the layer list
178 LayerListIterator begin();
179
180 // Returns an iterator to the end of the layer list
181 LayerListIterator end();
182
183
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700184 // Events handling ---------------------------------------------------------
185
186 enum {
187 EVENT_VSYNC = HWC_EVENT_VSYNC
188 };
189
Mathias Agopian03e40722012-04-26 16:11:59 -0700190 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700191
Mathias Agopian888c8222012-08-04 21:10:38 -0700192 nsecs_t getRefreshPeriod() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700193 nsecs_t getRefreshTimestamp() const;
194
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700195 // this class is only used to fake the VSync event on systems that don't
196 // have it.
197 class VSyncThread : public Thread {
198 HWComposer& mHwc;
199 mutable Mutex mLock;
200 Condition mCondition;
201 bool mEnabled;
202 mutable nsecs_t mNextFakeVSync;
203 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700204 virtual void onFirstRef();
205 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700206 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700207 VSyncThread(HWComposer& hwc);
208 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700209 };
210
211 friend class VSyncThread;
212
213 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700214 void dump(String8& out, char* scratch, size_t SIZE,
215 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700216
Mathias Agopiana350ff92010-08-10 17:14:02 -0700217private:
Jesse Hallb685c542012-07-31 14:32:56 -0700218 enum { MAX_DISPLAYS = 1 };
219
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700220 LayerListIterator getLayerIterator(size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700221
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700222 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700223
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700224 static void hook_invalidate(struct hwc_procs* procs);
Mathias Agopian31d28432012-04-03 16:31:39 -0700225 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp);
226
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700227 inline void invalidate();
228 inline void vsync(int dpy, int64_t timestamp);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700229
Jesse Hall5880cc52012-06-05 23:40:32 -0700230 sp<SurfaceFlinger> mFlinger;
231 hw_module_t const* mModule;
232 struct hwc_composer_device_1* mHwc;
Jesse Hallb685c542012-07-31 14:32:56 -0700233 // invariant: mLists[0] != NULL iff mHwc != NULL
234 // TODO: decide whether mLists[i>0] should be non-NULL when display i is
235 // not attached/enabled.
236 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
Jesse Hall5880cc52012-06-05 23:40:32 -0700237 size_t mCapacity;
238 mutable size_t mNumOVLayers;
239 mutable size_t mNumFBLayers;
Jesse Hall5880cc52012-06-05 23:40:32 -0700240 cb_context* mCBContext;
241 EventHandler& mEventHandler;
242 nsecs_t mRefreshPeriod;
243 size_t mVSyncCount;
244 sp<VSyncThread> mVSyncThread;
245 bool mDebugForceFakeVSync;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700246
247 // protected by mLock
248 mutable Mutex mLock;
249 mutable nsecs_t mLastHwVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700250};
251
Mathias Agopiana350ff92010-08-10 17:14:02 -0700252// ---------------------------------------------------------------------------
253}; // namespace android
254
255#endif // ANDROID_SF_HWCOMPOSER_H