blob: a020f44e8746a9b966cdc60a4c095903030e0edb [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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>
26#include <ui/Overlay.h>
27
28#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;
78 mutable bool contentDirty;
79 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 reserved[2];
90 int32_t sequence; // changes when visible regions can change
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 void forceVisibilityTransaction();
108
109 uint32_t getTransactionFlags(uint32_t flags);
110 uint32_t setTransactionFlags(uint32_t flags);
111
112 Rect visibleBounds() const;
113 void drawRegion(const Region& reg) const;
114
115 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 */
123 virtual void draw(const Region& clip) const;
124
125 /**
126 * onDraw - draws the surface.
127 */
128 virtual void onDraw(const Region& clip) const = 0;
129
130 /**
131 * initStates - called just after construction
132 */
133 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
134
135 /**
136 * setSizeChanged - called when the *current* state's size is changed.
137 */
138 virtual void setSizeChanged(uint32_t w, uint32_t h);
139
140 /**
141 * doTransaction - process the transaction. This is a good place to figure
142 * out which attributes of the surface have changed.
143 */
144 virtual uint32_t doTransaction(uint32_t transactionFlags);
145
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 */
150 virtual void setVisibleRegion(const Region& visibleRegion);
151
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 */
157 virtual void setCoveredRegion(const Region& coveredRegion);
158
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 */
164 virtual Point getPhysicalSize() const;
165
166 /**
167 * validateVisibility - cache a bunch of things
168 */
169 virtual void validateVisibility(const Transform& globalTransform);
170
171 /**
172 * lockPageFlip - called each time the screen is redrawn and returns whether
173 * the visible regions need to be recomputed (this is a fairly heavy
174 * operation, so this should be set only if needed). Typically this is used
175 * to figure out if the content or size of a surface has changed.
176 */
177 virtual void lockPageFlip(bool& recomputeVisibleRegions);
178
179 /**
180 * unlockPageFlip - called each time the screen is redrawn. updates the
181 * final dirty region wrt the planeTransform.
182 * At this point, all visible regions, surface position and size, etc... are
183 * correct.
184 */
185 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
186
187 /**
188 * finishPageFlip - called after all surfaces have drawn.
189 */
190 virtual void finishPageFlip();
191
192 /**
193 * needsBlending - true if this surface needs blending
194 */
195 virtual bool needsBlending() const { return false; }
196
197 /**
198 * transformed -- true is this surface needs a to be transformed
199 */
200 virtual bool transformed() const { return mTransformed; }
201
202 /**
203 * isSecure - true if this surface is secure, that is if it prevents
204 * screenshots or vns servers.
205 */
206 virtual bool isSecure() const { return false; }
207
208 enum { // flags for doTransaction()
209 eVisibleRegion = 0x00000002,
210 eRestartTransaction = 0x00000008
211 };
212
213
214 inline const State& drawingState() const { return mDrawingState; }
215 inline const State& currentState() const { return mCurrentState; }
216 inline State& currentState() { return mCurrentState; }
217
218 static int compareCurrentStateZ(LayerBase*const* layerA, LayerBase*const* layerB) {
219 return layerA[0]->currentState().z - layerB[0]->currentState().z;
220 }
221
222 int32_t getOrientation() const { return mOrientation; }
223 int tx() const { return mLeft; }
224 int ty() const { return mTop; }
225
226protected:
227 const GraphicPlane& graphicPlane(int dpy) const;
228 GraphicPlane& graphicPlane(int dpy);
229
230 GLuint createTexture() const;
231
232 void drawWithOpenGL(const Region& clip,
233 GLint textureName,
234 const GGLSurface& surface,
235 int transform = 0) const;
236
237 void clearWithOpenGL(const Region& clip) const;
238
239 void loadTexture(const Region& dirty,
240 GLint textureName, const GGLSurface& t,
241 GLuint& textureWidth, GLuint& textureHeight) const;
242
243 bool canUseCopybit() const;
244
245 SurfaceFlinger* mFlinger;
246 uint32_t mFlags;
247
248 // cached during validateVisibility()
249 bool mTransformed;
250 int32_t mOrientation;
251 GLfixed mVertices[4][2];
252 Rect mTransformedBounds;
253 bool mCanUseCopyBit;
254 int mLeft;
255 int mTop;
256
257 // these are protected by an external lock
258 State mCurrentState;
259 State mDrawingState;
260 volatile int32_t mTransactionFlags;
261
262 // don't change, don't need a lock
263 bool mPremultipliedAlpha;
264
265 // only read
266 const uint32_t mIdentity;
267
268 // atomic
269 volatile int32_t mInvalidate;
270
271
272private:
273 void validateTexture(GLint textureName) const;
274 static int32_t sIdentity;
275};
276
277
278// ---------------------------------------------------------------------------
279
280class LayerBaseClient : public LayerBase
281{
282public:
283 class Surface;
284 static const uint32_t typeInfo;
285 static const char* const typeID;
286 virtual char const* getTypeID() const { return typeID; }
287 virtual uint32_t getTypeInfo() const { return typeInfo; }
288
289 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
290 Client* client, int32_t i);
291 virtual ~LayerBaseClient();
292
293
294 Client* const client;
295 layer_cblk_t* const lcblk;
296
297 inline int32_t clientIndex() const { return mIndex; }
298 int32_t serverIndex() const;
299
300 virtual sp<Surface> getSurface() const;
301
302 uint32_t getIdentity() const { return mIdentity; }
303
304 class Surface : public BnSurface
305 {
306 public:
307 Surface(SurfaceID id, int identity) {
308 mParams.token = id;
309 mParams.identity = identity;
310 }
311 Surface(SurfaceID id,
312 const sp<IMemoryHeap>& heap0,
313 const sp<IMemoryHeap>& heap1,
314 int identity)
315 {
316 mParams.token = id;
317 mParams.identity = identity;
318 mParams.heap[0] = heap0;
319 mParams.heap[1] = heap1;
320 }
321 virtual ~Surface() {
322 // TODO: We now have a point here were we can clean-up the
323 // client's mess.
324 // This is also where surface id should be recycled.
325 //LOGD("Surface %d, heaps={%p, %p} destroyed",
326 // mId, mHeap[0].get(), mHeap[1].get());
327 }
328
329 virtual void getSurfaceData(
330 ISurfaceFlingerClient::surface_data_t* params) const {
331 *params = mParams;
332 }
333
334 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers)
335 { return INVALID_OPERATION; }
336 virtual void postBuffer(ssize_t offset) { }
337 virtual void unregisterBuffers() { };
338 virtual sp<OverlayRef> createOverlay(
339 uint32_t w, uint32_t h, int32_t format) {
340 return NULL;
341 };
342
343 private:
344 ISurfaceFlingerClient::surface_data_t mParams;
345 };
346
347private:
348 int32_t mIndex;
349
350};
351
352// ---------------------------------------------------------------------------
353
354}; // namespace android
355
356#endif // ANDROID_LAYER_BASE_H