blob: 5e14dc85c7acc033eb3e5fcfe3961f3cfedf9765 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 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_LAYER_BASE_H
18#define ANDROID_LAYER_BASE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <private/ui/LayerState.h>
24
25#include <ui/Region.h>
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080026#include <ui/Overlay.h>
27
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070028#include <pixelflinger/pixelflinger.h>
29
30#include "Transform.h"
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
37class DisplayHardware;
38class GraphicPlane;
39class Client;
40
41// ---------------------------------------------------------------------------
42
43class LayerBase
44{
45 // poor man's dynamic_cast below
46 template<typename T>
47 struct getTypeInfoOfAnyType {
48 static uint32_t get() { return T::typeInfo; }
49 };
50
51 template<typename T>
52 struct getTypeInfoOfAnyType<T*> {
53 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
54 };
55
56public:
57 static const uint32_t typeInfo;
58 static const char* const typeID;
59 virtual char const* getTypeID() const { return typeID; }
60 virtual uint32_t getTypeInfo() const { return typeInfo; }
61
62 template<typename T>
63 static T dynamicCast(LayerBase* base) {
64 uint32_t mostDerivedInfo = base->getTypeInfo();
65 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
66 if ((mostDerivedInfo & castToInfo) == castToInfo)
67 return static_cast<T>(base);
68 return 0;
69 }
70
71
72 static Vector<GLuint> deletedTextures;
73
74 LayerBase(SurfaceFlinger* flinger, DisplayID display);
75 virtual ~LayerBase();
76
77 DisplayID dpy;
The Android Open Source Project27629322009-01-09 17:51:23 -080078 mutable bool contentDirty;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079 Region visibleRegionScreen;
80 Region transparentRegionScreen;
81 Region coveredRegionScreen;
82
83 struct State {
84 uint32_t w;
85 uint32_t h;
86 uint32_t z;
87 uint8_t alpha;
88 uint8_t flags;
89 uint8_t sequence; // changes when visible regions can change
90 uint8_t reserved;
91 uint32_t tint;
92 Transform transform;
93 Region transparentRegion;
94 };
95
96 // modify current state
97 bool setPosition(int32_t x, int32_t y);
98 bool setLayer(uint32_t z);
99 bool setSize(uint32_t w, uint32_t h);
100 bool setAlpha(uint8_t alpha);
101 bool setMatrix(const layer_state_t::matrix22_t& matrix);
102 bool setTransparentRegionHint(const Region& opaque);
103 bool setFlags(uint8_t flags, uint8_t mask);
104
105 void commitTransaction(bool skipSize);
106 bool requestTransaction();
107
108 uint32_t getTransactionFlags(uint32_t flags);
109 uint32_t setTransactionFlags(uint32_t flags);
110
111 void validateVisibility(const Transform& globalTransform);
112 Rect visibleBounds() const;
113 void drawRegion(const Region& reg) const;
114
The Android Open Source Project27629322009-01-09 17:51:23 -0800115 void invalidate();
116
117 /**
118 * draw - performs some global clipping optimizations
119 * and calls onDraw().
120 * Typically this method is not overridden, instead implement onDraw()
121 * to perform the actual drawing.
122 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700123 virtual void draw(const Region& clip) const;
The Android Open Source Project27629322009-01-09 17:51:23 -0800124
125 /**
126 * onDraw - draws the surface.
127 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700128 virtual void onDraw(const Region& clip) const = 0;
The Android Open Source Project27629322009-01-09 17:51:23 -0800129
130 /**
131 * initStates - called just after construction
132 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
The Android Open Source Project27629322009-01-09 17:51:23 -0800134
135 /**
136 * setSizeChanged - called when the *current* state's size is changed.
137 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 virtual void setSizeChanged(uint32_t w, uint32_t h);
The Android Open Source Project27629322009-01-09 17:51:23 -0800139
140 /**
141 * doTransaction - process the transaction. This is a good place to figure
142 * out which attributes of the surface have changed.
143 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144 virtual uint32_t doTransaction(uint32_t transactionFlags);
The Android Open Source Project27629322009-01-09 17:51:23 -0800145
146 /**
147 * setVisibleRegion - called to set the new visible region. This gives
148 * a chance to update the new visible region or record the fact it changed.
149 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150 virtual void setVisibleRegion(const Region& visibleRegion);
The Android Open Source Project27629322009-01-09 17:51:23 -0800151
152 /**
153 * setCoveredRegion - called when the covered region changes. The covered
154 * region correspond to any area of the surface that is covered
155 * (transparently or not) by another surface.
156 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700157 virtual void setCoveredRegion(const Region& coveredRegion);
The Android Open Source Project27629322009-01-09 17:51:23 -0800158
159 /**
160 * getPhysicalSize - returns the physical size of the drawing state of
161 * the surface. If the surface is backed by a bitmap, this is the size of
162 * the bitmap (as opposed to the size of the drawing state).
163 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 virtual Point getPhysicalSize() const;
The Android Open Source Project27629322009-01-09 17:51:23 -0800165
166 /**
167 * lockPageFlip - called each time the screen is redrawn and returns whether
168 * the visible regions need to be recomputed (this is a fairly heavy
169 * operation, so this should be set only if needed). Typically this is used
170 * to figure out if the content or size of a surface has changed.
171 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172 virtual void lockPageFlip(bool& recomputeVisibleRegions);
The Android Open Source Project27629322009-01-09 17:51:23 -0800173
174 /**
175 * unlockPageFlip - called each time the screen is redrawn. updates the
176 * final dirty region wrt the planeTransform.
177 * At this point, all visible regions, surface position and size, etc... are
178 * correct.
179 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
The Android Open Source Project27629322009-01-09 17:51:23 -0800181
182 /**
183 * finishPageFlip - called after all surfaces have drawn.
184 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 virtual void finishPageFlip();
The Android Open Source Project27629322009-01-09 17:51:23 -0800186
187 /**
188 * needsBlending - true if this surface needs blending
189 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 virtual bool needsBlending() const { return false; }
The Android Open Source Project27629322009-01-09 17:51:23 -0800191
192 /**
193 * isSecure - true if this surface is secure, that is if it prevents a
194 * screenshot to be taken,
195 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196 virtual bool isSecure() const { return false; }
197
198 enum { // flags for doTransaction()
199 eVisibleRegion = 0x00000002,
200 eRestartTransaction = 0x00000008
201 };
202
203
204 inline const State& drawingState() const { return mDrawingState; }
205 inline const State& currentState() const { return mCurrentState; }
206 inline State& currentState() { return mCurrentState; }
207
208 static int compareCurrentStateZ(LayerBase*const* layerA, LayerBase*const* layerB) {
209 return layerA[0]->currentState().z - layerB[0]->currentState().z;
210 }
211
212 int32_t getOrientation() const { return mOrientation; }
213 bool transformed() const { return mTransformed; }
214 int tx() const { return mLeft; }
215 int ty() const { return mTop; }
216
217protected:
218 const GraphicPlane& graphicPlane(int dpy) const;
219 GraphicPlane& graphicPlane(int dpy);
220
221 GLuint createTexture() const;
222
223 void drawWithOpenGL(const Region& clip,
224 GLint textureName, const GGLSurface& surface) const;
225
226 void clearWithOpenGL(const Region& clip) const;
227
228 void loadTexture(const Region& dirty,
229 GLint textureName, const GGLSurface& t,
230 GLuint& textureWidth, GLuint& textureHeight) const;
231
232 bool canUseCopybit() const;
233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 SurfaceFlinger* mFlinger;
235 uint32_t mFlags;
236
237 // cached during validateVisibility()
238 bool mTransformed;
239 int32_t mOrientation;
240 GLfixed mVertices[4][2];
241 Rect mTransformedBounds;
242 bool mCanUseCopyBit;
243 int mLeft;
244 int mTop;
245
246 // these are protected by an external lock
247 State mCurrentState;
248 State mDrawingState;
249 volatile int32_t mTransactionFlags;
250
251 // don't change, don't need a lock
252 bool mPremultipliedAlpha;
253
254 // only read
The Android Open Source Project27629322009-01-09 17:51:23 -0800255 const uint32_t mIdentity;
256
257 // atomic
258 volatile int32_t mInvalidate;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259
260
261private:
262 void validateTexture(GLint textureName) const;
263 static int32_t sIdentity;
264};
265
266
267// ---------------------------------------------------------------------------
268
269class LayerBaseClient : public LayerBase
270{
271public:
272 class Surface;
273 static const uint32_t typeInfo;
274 static const char* const typeID;
275 virtual char const* getTypeID() const { return typeID; }
276 virtual uint32_t getTypeInfo() const { return typeInfo; }
277
278 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
279 Client* client, int32_t i);
280 virtual ~LayerBaseClient();
281
282
283 Client* const client;
284 layer_cblk_t* const lcblk;
285
286 inline int32_t clientIndex() const { return mIndex; }
287 int32_t serverIndex() const;
288
289 virtual sp<Surface> getSurface() const;
290
291 uint32_t getIdentity() const { return mIdentity; }
292
293 class Surface : public BnSurface
294 {
295 public:
296 Surface(SurfaceID id, int identity) {
297 mParams.token = id;
298 mParams.identity = identity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 }
300 Surface(SurfaceID id,
301 const sp<IMemoryHeap>& heap0,
302 const sp<IMemoryHeap>& heap1,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800303 int identity)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 {
305 mParams.token = id;
306 mParams.identity = identity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 mParams.heap[0] = heap0;
308 mParams.heap[1] = heap1;
309 }
310 virtual ~Surface() {
311 // TODO: We now have a point here were we can clean-up the
312 // client's mess.
313 // This is also where surface id should be recycled.
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800314 //LOGD("Surface %d, heaps={%p, %p} destroyed",
315 // mId, mHeap[0].get(), mHeap[1].get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 }
317
318 virtual void getSurfaceData(
319 ISurfaceFlingerClient::surface_data_t* params) const {
320 *params = mParams;
321 }
322
323 virtual status_t registerBuffers(int w, int h, int hstride, int vstride,
324 PixelFormat format, const sp<IMemoryHeap>& heap)
325 { return INVALID_OPERATION; }
326 virtual void postBuffer(ssize_t offset) { }
327 virtual void unregisterBuffers() { };
The Android Open Source Project27629322009-01-09 17:51:23 -0800328 virtual sp<OverlayRef> createOverlay(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800329 uint32_t w, uint32_t h, int32_t format) {
330 return NULL;
331 };
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332
333 private:
334 ISurfaceFlingerClient::surface_data_t mParams;
335 };
336
337private:
338 int32_t mIndex;
339
340};
341
342// ---------------------------------------------------------------------------
343
344}; // namespace android
345
346#endif // ANDROID_LAYER_BASE_H