blob: b9c93371e1a993fa59ed97dba3551385faac0c3a [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;
37struct hwc_layer_list_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;
Mathias Agopian83727852010-09-23 18:13:21 -070045class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070046class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070047
Mathias Agopiana350ff92010-08-10 17:14:02 -070048class HWComposer
49{
50public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070051 class EventHandler {
52 friend class HWComposer;
53 virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0;
54 protected:
55 virtual ~EventHandler() {}
56 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070057
Mathias Agopian888c8222012-08-04 21:10:38 -070058 HWComposer(const sp<SurfaceFlinger>& flinger, EventHandler& handler);
Mathias Agopiana350ff92010-08-10 17:14:02 -070059 ~HWComposer();
60
61 status_t initCheck() const;
62
Mathias Agopiana350ff92010-08-10 17:14:02 -070063 // Asks the HAL what it can do
64 status_t prepare() const;
65
Mathias Agopian7ee4cd52011-09-02 12:22:39 -070066 // disable hwc until next createWorkList
67 status_t disable();
68
Mathias Agopiana350ff92010-08-10 17:14:02 -070069 // commits the list
Jesse Hall34a09ba2012-07-29 22:35:34 -070070 status_t commit(void* fbDisplay, void* fbSurface) const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070071
Colin Cross10fbdb62012-07-12 17:56:34 -070072 // release hardware resources and blank screen
Antti Hatalaf5f27122010-09-09 02:33:05 -070073 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070074
Colin Cross10fbdb62012-07-12 17:56:34 -070075 // acquire hardware resources and unblank screen
76 status_t acquire() const;
77
Mathias Agopian3e8b8532012-05-13 20:42:01 -070078 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
79 status_t createWorkList(size_t numLayers);
80
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070081 // get the layer array created by createWorkList()
Mathias Agopian45721772010-08-12 15:03:26 -070082 size_t getNumLayers() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070083
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070084 // get number of layers of the given type as updated in prepare().
85 // type is HWC_OVERLAY or HWC_FRAMEBUFFER
Mathias Agopian9c6e2972011-09-20 17:21:56 -070086 size_t getLayerCount(int type) const;
87
Mathias Agopian3e8b8532012-05-13 20:42:01 -070088 // needed forward declarations
89 class LayerListIterator;
90
91 /*
92 * Interface to hardware composer's layers functionality.
93 * This abstracts the HAL interface to layers which can evolve in
94 * incompatible ways from one release to another.
95 * The idea is that we could extend this interface as we add
96 * features to h/w composer.
97 */
98 class HWCLayerInterface {
99 protected:
100 virtual ~HWCLayerInterface() { }
101 public:
102 virtual int32_t getCompositionType() const = 0;
103 virtual uint32_t getHints() const = 0;
Jesse Hallef194142012-06-14 14:45:17 -0700104 virtual int getAndResetReleaseFenceFd() = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700105 virtual void setDefaultState() = 0;
106 virtual void setSkip(bool skip) = 0;
107 virtual void setBlending(uint32_t blending) = 0;
108 virtual void setTransform(uint32_t transform) = 0;
109 virtual void setFrame(const Rect& frame) = 0;
110 virtual void setCrop(const Rect& crop) = 0;
111 virtual void setVisibleRegionScreen(const Region& reg) = 0;
112 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
Jesse Halldc5b4852012-06-29 15:21:18 -0700113 virtual void setAcquireFenceFd(int fenceFd) = 0;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700114 };
115
116 /*
117 * Interface used to implement an iterator to a list
118 * of HWCLayer.
119 */
120 class HWCLayer : public HWCLayerInterface {
121 friend class LayerListIterator;
122 // select the layer at the given index
123 virtual status_t setLayer(size_t index) = 0;
124 virtual HWCLayer* dup() = 0;
125 static HWCLayer* copy(HWCLayer *rhs) {
126 return rhs ? rhs->dup() : NULL;
127 }
128 protected:
129 virtual ~HWCLayer() { }
130 };
131
132 /*
133 * Iterator through a HWCLayer list.
134 * This behaves more or less like a forward iterator.
135 */
136 class LayerListIterator {
137 friend struct HWComposer;
138 HWCLayer* const mLayerList;
139 size_t mIndex;
140
141 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
142
143 LayerListIterator(HWCLayer* layer, size_t index)
144 : mLayerList(layer), mIndex(index) { }
145
146 // we don't allow assignment, because we don't need it for now
147 LayerListIterator& operator = (const LayerListIterator& rhs);
148
149 public:
150 // copy operators
151 LayerListIterator(const LayerListIterator& rhs)
152 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
153 }
154
155 ~LayerListIterator() { delete mLayerList; }
156
157 // pre-increment
158 LayerListIterator& operator++() {
159 mLayerList->setLayer(++mIndex);
160 return *this;
161 }
162
163 // dereference
164 HWCLayerInterface& operator * () { return *mLayerList; }
165 HWCLayerInterface* operator -> () { return mLayerList; }
166
167 // comparison
168 bool operator == (const LayerListIterator& rhs) const {
169 return mIndex == rhs.mIndex;
170 }
171 bool operator != (const LayerListIterator& rhs) const {
172 return !operator==(rhs);
173 }
174 };
175
176 // Returns an iterator to the beginning of the layer list
177 LayerListIterator begin();
178
179 // Returns an iterator to the end of the layer list
180 LayerListIterator end();
181
182
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700183 // Events handling ---------------------------------------------------------
184
185 enum {
186 EVENT_VSYNC = HWC_EVENT_VSYNC
187 };
188
Mathias Agopian03e40722012-04-26 16:11:59 -0700189 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700190
Mathias Agopian888c8222012-08-04 21:10:38 -0700191 nsecs_t getRefreshPeriod() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700192 nsecs_t getRefreshTimestamp() const;
193
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700194 // this class is only used to fake the VSync event on systems that don't
195 // have it.
196 class VSyncThread : public Thread {
197 HWComposer& mHwc;
198 mutable Mutex mLock;
199 Condition mCondition;
200 bool mEnabled;
201 mutable nsecs_t mNextFakeVSync;
202 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700203 virtual void onFirstRef();
204 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700205 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700206 VSyncThread(HWComposer& hwc);
207 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700208 };
209
210 friend class VSyncThread;
211
212 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700213 void dump(String8& out, char* scratch, size_t SIZE,
214 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700215
Mathias Agopiana350ff92010-08-10 17:14:02 -0700216private:
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700217 LayerListIterator getLayerIterator(size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700218
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700219 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700220
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700221 static void hook_invalidate(struct hwc_procs* procs);
Mathias Agopian31d28432012-04-03 16:31:39 -0700222 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp);
223
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700224 inline void invalidate();
225 inline void vsync(int dpy, int64_t timestamp);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700226
Jesse Hall5880cc52012-06-05 23:40:32 -0700227 sp<SurfaceFlinger> mFlinger;
228 hw_module_t const* mModule;
229 struct hwc_composer_device_1* mHwc;
230 struct hwc_layer_list_1* mList;
231 size_t mCapacity;
232 mutable size_t mNumOVLayers;
233 mutable size_t mNumFBLayers;
Jesse Hall5880cc52012-06-05 23:40:32 -0700234 cb_context* mCBContext;
235 EventHandler& mEventHandler;
236 nsecs_t mRefreshPeriod;
237 size_t mVSyncCount;
238 sp<VSyncThread> mVSyncThread;
239 bool mDebugForceFakeVSync;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700240
241 // protected by mLock
242 mutable Mutex mLock;
243 mutable nsecs_t mLastHwVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700244};
245
Mathias Agopiana350ff92010-08-10 17:14:02 -0700246// ---------------------------------------------------------------------------
247}; // namespace android
248
249#endif // ANDROID_SF_HWCOMPOSER_H