blob: 2377a148650e7779cb77f3ef6c571723564fa963 [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;
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -080089 uint8_t reserved[2];
90 int32_t sequence; // changes when visible regions can change
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070091 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();
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800107 void forceVisibilityTransaction();
108
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 uint32_t getTransactionFlags(uint32_t flags);
110 uint32_t setTransactionFlags(uint32_t flags);
111
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 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 Projecta6938ba2009-02-10 15:44:00 -0800165
166 /**
167 * validateVisibility - cache a bunch of things
168 */
169 virtual void validateVisibility(const Transform& globalTransform);
170
171 /**
172 * getDrawingStateTransform - returns the drawing state's transform.
173 * This is used in validateVisibility() and can be use to override or
174 * modify the transform (if so make sure to trigger a transaction).
175 */
176 virtual Transform getDrawingStateTransform() const;
177
The Android Open Source Project27629322009-01-09 17:51:23 -0800178 /**
179 * lockPageFlip - called each time the screen is redrawn and returns whether
180 * the visible regions need to be recomputed (this is a fairly heavy
181 * operation, so this should be set only if needed). Typically this is used
182 * to figure out if the content or size of a surface has changed.
183 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 virtual void lockPageFlip(bool& recomputeVisibleRegions);
The Android Open Source Project27629322009-01-09 17:51:23 -0800185
186 /**
187 * unlockPageFlip - called each time the screen is redrawn. updates the
188 * final dirty region wrt the planeTransform.
189 * At this point, all visible regions, surface position and size, etc... are
190 * correct.
191 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
The Android Open Source Project27629322009-01-09 17:51:23 -0800193
194 /**
195 * finishPageFlip - called after all surfaces have drawn.
196 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 virtual void finishPageFlip();
The Android Open Source Project27629322009-01-09 17:51:23 -0800198
199 /**
200 * needsBlending - true if this surface needs blending
201 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 virtual bool needsBlending() const { return false; }
The Android Open Source Project27629322009-01-09 17:51:23 -0800203
204 /**
205 * isSecure - true if this surface is secure, that is if it prevents a
206 * screenshot to be taken,
207 */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 virtual bool isSecure() const { return false; }
209
210 enum { // flags for doTransaction()
211 eVisibleRegion = 0x00000002,
212 eRestartTransaction = 0x00000008
213 };
214
215
216 inline const State& drawingState() const { return mDrawingState; }
217 inline const State& currentState() const { return mCurrentState; }
218 inline State& currentState() { return mCurrentState; }
219
220 static int compareCurrentStateZ(LayerBase*const* layerA, LayerBase*const* layerB) {
221 return layerA[0]->currentState().z - layerB[0]->currentState().z;
222 }
223
224 int32_t getOrientation() const { return mOrientation; }
225 bool transformed() const { return mTransformed; }
226 int tx() const { return mLeft; }
227 int ty() const { return mTop; }
228
229protected:
230 const GraphicPlane& graphicPlane(int dpy) const;
231 GraphicPlane& graphicPlane(int dpy);
232
233 GLuint createTexture() const;
234
235 void drawWithOpenGL(const Region& clip,
236 GLint textureName, const GGLSurface& surface) const;
237
238 void clearWithOpenGL(const Region& clip) const;
239
240 void loadTexture(const Region& dirty,
241 GLint textureName, const GGLSurface& t,
242 GLuint& textureWidth, GLuint& textureHeight) const;
243
244 bool canUseCopybit() const;
245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 SurfaceFlinger* mFlinger;
247 uint32_t mFlags;
248
249 // cached during validateVisibility()
250 bool mTransformed;
251 int32_t mOrientation;
252 GLfixed mVertices[4][2];
253 Rect mTransformedBounds;
254 bool mCanUseCopyBit;
255 int mLeft;
256 int mTop;
257
258 // these are protected by an external lock
259 State mCurrentState;
260 State mDrawingState;
261 volatile int32_t mTransactionFlags;
262
263 // don't change, don't need a lock
264 bool mPremultipliedAlpha;
265
266 // only read
The Android Open Source Project27629322009-01-09 17:51:23 -0800267 const uint32_t mIdentity;
268
269 // atomic
270 volatile int32_t mInvalidate;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271
272
273private:
274 void validateTexture(GLint textureName) const;
275 static int32_t sIdentity;
276};
277
278
279// ---------------------------------------------------------------------------
280
281class LayerBaseClient : public LayerBase
282{
283public:
284 class Surface;
285 static const uint32_t typeInfo;
286 static const char* const typeID;
287 virtual char const* getTypeID() const { return typeID; }
288 virtual uint32_t getTypeInfo() const { return typeInfo; }
289
290 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
291 Client* client, int32_t i);
292 virtual ~LayerBaseClient();
293
294
295 Client* const client;
296 layer_cblk_t* const lcblk;
297
298 inline int32_t clientIndex() const { return mIndex; }
299 int32_t serverIndex() const;
300
301 virtual sp<Surface> getSurface() const;
302
303 uint32_t getIdentity() const { return mIdentity; }
304
305 class Surface : public BnSurface
306 {
307 public:
308 Surface(SurfaceID id, int identity) {
309 mParams.token = id;
310 mParams.identity = identity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 }
312 Surface(SurfaceID id,
313 const sp<IMemoryHeap>& heap0,
314 const sp<IMemoryHeap>& heap1,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800315 int identity)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 {
317 mParams.token = id;
318 mParams.identity = identity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 mParams.heap[0] = heap0;
320 mParams.heap[1] = heap1;
321 }
322 virtual ~Surface() {
323 // TODO: We now have a point here were we can clean-up the
324 // client's mess.
325 // This is also where surface id should be recycled.
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800326 //LOGD("Surface %d, heaps={%p, %p} destroyed",
327 // mId, mHeap[0].get(), mHeap[1].get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 }
329
330 virtual void getSurfaceData(
331 ISurfaceFlingerClient::surface_data_t* params) const {
332 *params = mParams;
333 }
334
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800335 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 { return INVALID_OPERATION; }
337 virtual void postBuffer(ssize_t offset) { }
338 virtual void unregisterBuffers() { };
The Android Open Source Project27629322009-01-09 17:51:23 -0800339 virtual sp<OverlayRef> createOverlay(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800340 uint32_t w, uint32_t h, int32_t format) {
341 return NULL;
342 };
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343
344 private:
345 ISurfaceFlingerClient::surface_data_t mParams;
346 };
347
348private:
349 int32_t mIndex;
350
351};
352
353// ---------------------------------------------------------------------------
354
355}; // namespace android
356
357#endif // ANDROID_LAYER_BASE_H