blob: 05c6f6ddd2a1dd974ad754906fa7e4d8ac99f559 [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
23#include <EGL/egl.h>
24
Mathias Agopian3e8b8532012-05-13 20:42:01 -070025#include <hardware/hwcomposer_defs.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070026
Mathias Agopianc7d14e22011-08-01 16:32:21 -070027#include <utils/StrongPointer.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070028#include <utils/Vector.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070029
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070030extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
31 const struct timespec *request,
32 struct timespec *remain);
33
Jesse Hall5880cc52012-06-05 23:40:32 -070034struct hwc_composer_device_1;
35struct hwc_layer_list_1;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070036struct hwc_procs;
37
Mathias Agopiana350ff92010-08-10 17:14:02 -070038namespace android {
39// ---------------------------------------------------------------------------
40
Mathias Agopian83727852010-09-23 18:13:21 -070041class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070042class SurfaceFlinger;
Mathias Agopian22da60c2011-09-09 00:49:11 -070043class LayerBase;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070044class GraphicBuffer;
Mathias Agopian83727852010-09-23 18:13:21 -070045
Mathias Agopiana350ff92010-08-10 17:14:02 -070046class HWComposer
47{
48public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070049 class EventHandler {
50 friend class HWComposer;
51 virtual void onVSyncReceived(int dpy, nsecs_t timestamp) = 0;
52 protected:
53 virtual ~EventHandler() {}
54 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070055
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070056 HWComposer(const sp<SurfaceFlinger>& flinger,
57 EventHandler& handler, nsecs_t refreshPeriod);
Mathias Agopiana350ff92010-08-10 17:14:02 -070058 ~HWComposer();
59
60 status_t initCheck() const;
61
62 // tells the HAL what the framebuffer is
63 void setFrameBuffer(EGLDisplay dpy, EGLSurface sur);
64
Mathias Agopiana350ff92010-08-10 17:14:02 -070065 // Asks the HAL what it can do
66 status_t prepare() const;
67
Mathias Agopian7ee4cd52011-09-02 12:22:39 -070068 // disable hwc until next createWorkList
69 status_t disable();
70
Mathias Agopiana350ff92010-08-10 17:14:02 -070071 // commits the list
72 status_t commit() const;
73
Antti Hatalaf5f27122010-09-09 02:33:05 -070074 // release hardware resources
75 status_t release() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070076
Mathias Agopian3e8b8532012-05-13 20:42:01 -070077 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
78 status_t createWorkList(size_t numLayers);
79
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070080 // get the layer array created by createWorkList()
Mathias Agopian45721772010-08-12 15:03:26 -070081 size_t getNumLayers() const;
Mathias Agopiana350ff92010-08-10 17:14:02 -070082
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070083 // get number of layers of the given type as updated in prepare().
84 // type is HWC_OVERLAY or HWC_FRAMEBUFFER
Mathias Agopian9c6e2972011-09-20 17:21:56 -070085 size_t getLayerCount(int type) const;
86
Mathias Agopian3e8b8532012-05-13 20:42:01 -070087 // needed forward declarations
88 class LayerListIterator;
89
90 /*
91 * Interface to hardware composer's layers functionality.
92 * This abstracts the HAL interface to layers which can evolve in
93 * incompatible ways from one release to another.
94 * The idea is that we could extend this interface as we add
95 * features to h/w composer.
96 */
97 class HWCLayerInterface {
98 protected:
99 virtual ~HWCLayerInterface() { }
100 public:
101 virtual int32_t getCompositionType() const = 0;
102 virtual uint32_t getHints() const = 0;
103 virtual void setDefaultState() = 0;
104 virtual void setSkip(bool skip) = 0;
105 virtual void setBlending(uint32_t blending) = 0;
106 virtual void setTransform(uint32_t transform) = 0;
107 virtual void setFrame(const Rect& frame) = 0;
108 virtual void setCrop(const Rect& crop) = 0;
109 virtual void setVisibleRegionScreen(const Region& reg) = 0;
110 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
111 };
112
113 /*
114 * Interface used to implement an iterator to a list
115 * of HWCLayer.
116 */
117 class HWCLayer : public HWCLayerInterface {
118 friend class LayerListIterator;
119 // select the layer at the given index
120 virtual status_t setLayer(size_t index) = 0;
121 virtual HWCLayer* dup() = 0;
122 static HWCLayer* copy(HWCLayer *rhs) {
123 return rhs ? rhs->dup() : NULL;
124 }
125 protected:
126 virtual ~HWCLayer() { }
127 };
128
129 /*
130 * Iterator through a HWCLayer list.
131 * This behaves more or less like a forward iterator.
132 */
133 class LayerListIterator {
134 friend struct HWComposer;
135 HWCLayer* const mLayerList;
136 size_t mIndex;
137
138 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
139
140 LayerListIterator(HWCLayer* layer, size_t index)
141 : mLayerList(layer), mIndex(index) { }
142
143 // we don't allow assignment, because we don't need it for now
144 LayerListIterator& operator = (const LayerListIterator& rhs);
145
146 public:
147 // copy operators
148 LayerListIterator(const LayerListIterator& rhs)
149 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
150 }
151
152 ~LayerListIterator() { delete mLayerList; }
153
154 // pre-increment
155 LayerListIterator& operator++() {
156 mLayerList->setLayer(++mIndex);
157 return *this;
158 }
159
160 // dereference
161 HWCLayerInterface& operator * () { return *mLayerList; }
162 HWCLayerInterface* operator -> () { return mLayerList; }
163
164 // comparison
165 bool operator == (const LayerListIterator& rhs) const {
166 return mIndex == rhs.mIndex;
167 }
168 bool operator != (const LayerListIterator& rhs) const {
169 return !operator==(rhs);
170 }
171 };
172
173 // Returns an iterator to the beginning of the layer list
174 LayerListIterator begin();
175
176 // Returns an iterator to the end of the layer list
177 LayerListIterator end();
178
179
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700180 // Events handling ---------------------------------------------------------
181
182 enum {
183 EVENT_VSYNC = HWC_EVENT_VSYNC
184 };
185
Mathias Agopian03e40722012-04-26 16:11:59 -0700186 void eventControl(int event, int enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700187
188 // this class is only used to fake the VSync event on systems that don't
189 // have it.
190 class VSyncThread : public Thread {
191 HWComposer& mHwc;
192 mutable Mutex mLock;
193 Condition mCondition;
194 bool mEnabled;
195 mutable nsecs_t mNextFakeVSync;
196 nsecs_t mRefreshPeriod;
Mathias Agopian2965b262012-04-08 15:13:32 -0700197 virtual void onFirstRef();
198 virtual bool threadLoop();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700199 public:
Mathias Agopian2965b262012-04-08 15:13:32 -0700200 VSyncThread(HWComposer& hwc);
201 void setEnabled(bool enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700202 };
203
204 friend class VSyncThread;
205
206 // for debugging ----------------------------------------------------------
Mathias Agopian22da60c2011-09-09 00:49:11 -0700207 void dump(String8& out, char* scratch, size_t SIZE,
208 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700209
Mathias Agopiana350ff92010-08-10 17:14:02 -0700210private:
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700211 LayerListIterator getLayerIterator(size_t index);
Mathias Agopian31d28432012-04-03 16:31:39 -0700212
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700213 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700214
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700215 static void hook_invalidate(struct hwc_procs* procs);
Mathias Agopian31d28432012-04-03 16:31:39 -0700216 static void hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp);
217
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700218 inline void invalidate();
219 inline void vsync(int dpy, int64_t timestamp);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700220
Jesse Hall5880cc52012-06-05 23:40:32 -0700221 sp<SurfaceFlinger> mFlinger;
222 hw_module_t const* mModule;
223 struct hwc_composer_device_1* mHwc;
224 struct hwc_layer_list_1* mList;
225 size_t mCapacity;
226 mutable size_t mNumOVLayers;
227 mutable size_t mNumFBLayers;
228 EGLDisplay mDpy;
229 EGLSurface mSur;
230 cb_context* mCBContext;
231 EventHandler& mEventHandler;
232 nsecs_t mRefreshPeriod;
233 size_t mVSyncCount;
234 sp<VSyncThread> mVSyncThread;
235 bool mDebugForceFakeVSync;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700236};
237
Mathias Agopiana350ff92010-08-10 17:14:02 -0700238// ---------------------------------------------------------------------------
239}; // namespace android
240
241#endif // ANDROID_SF_HWCOMPOSER_H