blob: 2168de0e79f5e4931368295e4221afb9ad33ca0e [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
Mathias Agopian1fed11c2009-06-23 18:08:22 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <private/ui/LayerState.h>
27
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <utils/RefBase.h>
29
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <ui/Region.h>
31#include <ui/Overlay.h>
32
33#include <pixelflinger/pixelflinger.h>
34
35#include "Transform.h"
36
37namespace android {
38
39// ---------------------------------------------------------------------------
40
41class SurfaceFlinger;
42class DisplayHardware;
43class GraphicPlane;
44class Client;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045class SurfaceBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -070046class Buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
48// ---------------------------------------------------------------------------
49
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051{
52 // poor man's dynamic_cast below
53 template<typename T>
54 struct getTypeInfoOfAnyType {
55 static uint32_t get() { return T::typeInfo; }
56 };
57
58 template<typename T>
59 struct getTypeInfoOfAnyType<T*> {
60 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
61 };
62
63public:
64 static const uint32_t typeInfo;
65 static const char* const typeID;
66 virtual char const* getTypeID() const { return typeID; }
67 virtual uint32_t getTypeInfo() const { return typeInfo; }
68
69 template<typename T>
70 static T dynamicCast(LayerBase* base) {
71 uint32_t mostDerivedInfo = base->getTypeInfo();
72 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
73 if ((mostDerivedInfo & castToInfo) == castToInfo)
74 return static_cast<T>(base);
75 return 0;
76 }
77
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
81 DisplayID dpy;
82 mutable bool contentDirty;
83 Region visibleRegionScreen;
84 Region transparentRegionScreen;
85 Region coveredRegionScreen;
86
87 struct State {
88 uint32_t w;
89 uint32_t h;
90 uint32_t z;
91 uint8_t alpha;
92 uint8_t flags;
93 uint8_t reserved[2];
94 int32_t sequence; // changes when visible regions can change
95 uint32_t tint;
96 Transform transform;
97 Region transparentRegion;
98 };
99
100 // modify current state
101 bool setPosition(int32_t x, int32_t y);
102 bool setLayer(uint32_t z);
103 bool setSize(uint32_t w, uint32_t h);
104 bool setAlpha(uint8_t alpha);
105 bool setMatrix(const layer_state_t::matrix22_t& matrix);
106 bool setTransparentRegionHint(const Region& opaque);
107 bool setFlags(uint8_t flags, uint8_t mask);
108
109 void commitTransaction(bool skipSize);
110 bool requestTransaction();
111 void forceVisibilityTransaction();
112
113 uint32_t getTransactionFlags(uint32_t flags);
114 uint32_t setTransactionFlags(uint32_t flags);
115
116 Rect visibleBounds() const;
117 void drawRegion(const Region& reg) const;
118
119 void invalidate();
120
121 /**
122 * draw - performs some global clipping optimizations
123 * and calls onDraw().
124 * Typically this method is not overridden, instead implement onDraw()
125 * to perform the actual drawing.
126 */
127 virtual void draw(const Region& clip) const;
128
129 /**
130 * onDraw - draws the surface.
131 */
132 virtual void onDraw(const Region& clip) const = 0;
133
134 /**
135 * initStates - called just after construction
136 */
137 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
138
139 /**
140 * setSizeChanged - called when the *current* state's size is changed.
141 */
142 virtual void setSizeChanged(uint32_t w, uint32_t h);
143
144 /**
145 * doTransaction - process the transaction. This is a good place to figure
146 * out which attributes of the surface have changed.
147 */
148 virtual uint32_t doTransaction(uint32_t transactionFlags);
149
150 /**
151 * setVisibleRegion - called to set the new visible region. This gives
152 * a chance to update the new visible region or record the fact it changed.
153 */
154 virtual void setVisibleRegion(const Region& visibleRegion);
155
156 /**
157 * setCoveredRegion - called when the covered region changes. The covered
158 * region correspond to any area of the surface that is covered
159 * (transparently or not) by another surface.
160 */
161 virtual void setCoveredRegion(const Region& coveredRegion);
162
163 /**
164 * getPhysicalSize - returns the physical size of the drawing state of
165 * the surface. If the surface is backed by a bitmap, this is the size of
166 * the bitmap (as opposed to the size of the drawing state).
167 */
168 virtual Point getPhysicalSize() const;
169
170 /**
171 * validateVisibility - cache a bunch of things
172 */
173 virtual void validateVisibility(const Transform& globalTransform);
174
175 /**
176 * lockPageFlip - called each time the screen is redrawn and returns whether
177 * the visible regions need to be recomputed (this is a fairly heavy
178 * operation, so this should be set only if needed). Typically this is used
179 * to figure out if the content or size of a surface has changed.
180 */
181 virtual void lockPageFlip(bool& recomputeVisibleRegions);
182
183 /**
184 * unlockPageFlip - called each time the screen is redrawn. updates the
185 * final dirty region wrt the planeTransform.
186 * At this point, all visible regions, surface position and size, etc... are
187 * correct.
188 */
189 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
190
191 /**
192 * finishPageFlip - called after all surfaces have drawn.
193 */
194 virtual void finishPageFlip();
195
196 /**
197 * needsBlending - true if this surface needs blending
198 */
199 virtual bool needsBlending() const { return false; }
200
201 /**
202 * transformed -- true is this surface needs a to be transformed
203 */
204 virtual bool transformed() const { return mTransformed; }
205
206 /**
207 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700208 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 */
210 virtual bool isSecure() const { return false; }
211
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700212 /** signal this layer that it's not needed any longer. called from the
213 * main thread */
Mathias Agopian9a112062009-04-17 19:36:26 -0700214 virtual status_t ditch() { return NO_ERROR; }
215
216
217
218 enum { // flags for doTransaction()
219 eVisibleRegion = 0x00000002,
220 eRestartTransaction = 0x00000008
221 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222
223
224 inline const State& drawingState() const { return mDrawingState; }
225 inline const State& currentState() const { return mCurrentState; }
226 inline State& currentState() { return mCurrentState; }
227
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228 static int compareCurrentStateZ(
229 sp<LayerBase> const * layerA,
230 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 return layerA[0]->currentState().z - layerB[0]->currentState().z;
232 }
233
234 int32_t getOrientation() const { return mOrientation; }
235 int tx() const { return mLeft; }
236 int ty() const { return mTop; }
237
238protected:
239 const GraphicPlane& graphicPlane(int dpy) const;
240 GraphicPlane& graphicPlane(int dpy);
241
242 GLuint createTexture() const;
243
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700244 struct Texture {
245 Texture() : name(-1U), width(0), height(0),
246 image(EGL_NO_IMAGE_KHR), transform(0), dirty(true) { }
247 GLuint name;
248 GLuint width;
249 GLuint height;
250 EGLImageKHR image;
251 uint32_t transform;
252 bool dirty;
253 };
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700254
255 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
256 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700258 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
259 void loadTexture(Texture* texture, GLint textureName,
260 const Region& dirty, const GGLSurface& t) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700262
Mathias Agopian9a112062009-04-17 19:36:26 -0700263 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 uint32_t mFlags;
265
266 // cached during validateVisibility()
267 bool mTransformed;
268 int32_t mOrientation;
269 GLfixed mVertices[4][2];
270 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 int mLeft;
272 int mTop;
273
274 // these are protected by an external lock
275 State mCurrentState;
276 State mDrawingState;
277 volatile int32_t mTransactionFlags;
278
279 // don't change, don't need a lock
280 bool mPremultipliedAlpha;
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 // atomic
283 volatile int32_t mInvalidate;
284
285
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286protected:
287 virtual ~LayerBase();
288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 LayerBase(const LayerBase& rhs);
291 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292};
293
294
295// ---------------------------------------------------------------------------
296
297class LayerBaseClient : public LayerBase
298{
299public:
300 class Surface;
301 static const uint32_t typeInfo;
302 static const char* const typeID;
303 virtual char const* getTypeID() const { return typeID; }
304 virtual uint32_t getTypeInfo() const { return typeInfo; }
305
306 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700307 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700309 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Mathias Agopianf9d93272009-06-19 17:00:27 -0700311 wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 layer_cblk_t* const lcblk;
313
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 inline int32_t clientIndex() const { return mIndex; }
316 int32_t serverIndex() const;
317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319 sp<Surface> getSurface();
320 virtual sp<Surface> createSurface() const;
321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
323 class Surface : public BnSurface
324 {
325 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700326 int32_t getToken() const { return mToken; }
327 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700330 Surface(const sp<SurfaceFlinger>& flinger,
331 SurfaceID id, int identity,
332 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700333 virtual ~Surface();
334 virtual status_t onTransact(uint32_t code, const Parcel& data,
335 Parcel* reply, uint32_t flags);
336 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337
338 private:
Mathias Agopian52212712009-08-11 22:34:02 -0700339 virtual sp<SurfaceBuffer> getBuffer(int usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
341 virtual void postBuffer(ssize_t offset);
342 virtual void unregisterBuffers();
343 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
344 int32_t format);
345
Mathias Agopian9a112062009-04-17 19:36:26 -0700346 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700348 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700349 int32_t mToken;
350 int32_t mIdentity;
351 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 };
353
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700354 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700356private:
357 int32_t mIndex;
358 mutable Mutex mLock;
359 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700360 // only read
361 const uint32_t mIdentity;
362 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363};
364
365// ---------------------------------------------------------------------------
366
367}; // namespace android
368
369#endif // ANDROID_LAYER_BASE_H