blob: 2ab6f6721bf35f8498463f35ad69b485dd9c9335 [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
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025#include <utils/RefBase.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/Region.h>
28#include <ui/Overlay.h>
29
30#include <pixelflinger/pixelflinger.h>
31
32#include "Transform.h"
33
34namespace android {
35
36// ---------------------------------------------------------------------------
37
38class SurfaceFlinger;
39class DisplayHardware;
40class GraphicPlane;
41class Client;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042class SurfaceBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44// ---------------------------------------------------------------------------
45
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047{
48 // poor man's dynamic_cast below
49 template<typename T>
50 struct getTypeInfoOfAnyType {
51 static uint32_t get() { return T::typeInfo; }
52 };
53
54 template<typename T>
55 struct getTypeInfoOfAnyType<T*> {
56 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
57 };
58
59public:
60 static const uint32_t typeInfo;
61 static const char* const typeID;
62 virtual char const* getTypeID() const { return typeID; }
63 virtual uint32_t getTypeInfo() const { return typeInfo; }
64
65 template<typename T>
66 static T dynamicCast(LayerBase* base) {
67 uint32_t mostDerivedInfo = base->getTypeInfo();
68 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
69 if ((mostDerivedInfo & castToInfo) == castToInfo)
70 return static_cast<T>(base);
71 return 0;
72 }
73
74
75 static Vector<GLuint> deletedTextures;
76
77 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078
79 DisplayID dpy;
80 mutable bool contentDirty;
81 Region visibleRegionScreen;
82 Region transparentRegionScreen;
83 Region coveredRegionScreen;
84
85 struct State {
86 uint32_t w;
87 uint32_t h;
88 uint32_t z;
89 uint8_t alpha;
90 uint8_t flags;
91 uint8_t reserved[2];
92 int32_t sequence; // changes when visible regions can change
93 uint32_t tint;
94 Transform transform;
95 Region transparentRegion;
96 };
97
98 // modify current state
99 bool setPosition(int32_t x, int32_t y);
100 bool setLayer(uint32_t z);
101 bool setSize(uint32_t w, uint32_t h);
102 bool setAlpha(uint8_t alpha);
103 bool setMatrix(const layer_state_t::matrix22_t& matrix);
104 bool setTransparentRegionHint(const Region& opaque);
105 bool setFlags(uint8_t flags, uint8_t mask);
106
107 void commitTransaction(bool skipSize);
108 bool requestTransaction();
109 void forceVisibilityTransaction();
110
111 uint32_t getTransactionFlags(uint32_t flags);
112 uint32_t setTransactionFlags(uint32_t flags);
113
114 Rect visibleBounds() const;
115 void drawRegion(const Region& reg) const;
116
117 void invalidate();
118
119 /**
120 * draw - performs some global clipping optimizations
121 * and calls onDraw().
122 * Typically this method is not overridden, instead implement onDraw()
123 * to perform the actual drawing.
124 */
125 virtual void draw(const Region& clip) const;
126
127 /**
128 * onDraw - draws the surface.
129 */
130 virtual void onDraw(const Region& clip) const = 0;
131
132 /**
133 * initStates - called just after construction
134 */
135 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
136
137 /**
138 * setSizeChanged - called when the *current* state's size is changed.
139 */
140 virtual void setSizeChanged(uint32_t w, uint32_t h);
141
142 /**
143 * doTransaction - process the transaction. This is a good place to figure
144 * out which attributes of the surface have changed.
145 */
146 virtual uint32_t doTransaction(uint32_t transactionFlags);
147
148 /**
149 * setVisibleRegion - called to set the new visible region. This gives
150 * a chance to update the new visible region or record the fact it changed.
151 */
152 virtual void setVisibleRegion(const Region& visibleRegion);
153
154 /**
155 * setCoveredRegion - called when the covered region changes. The covered
156 * region correspond to any area of the surface that is covered
157 * (transparently or not) by another surface.
158 */
159 virtual void setCoveredRegion(const Region& coveredRegion);
160
161 /**
162 * getPhysicalSize - returns the physical size of the drawing state of
163 * the surface. If the surface is backed by a bitmap, this is the size of
164 * the bitmap (as opposed to the size of the drawing state).
165 */
166 virtual Point getPhysicalSize() const;
167
168 /**
169 * validateVisibility - cache a bunch of things
170 */
171 virtual void validateVisibility(const Transform& globalTransform);
172
173 /**
174 * lockPageFlip - called each time the screen is redrawn and returns whether
175 * the visible regions need to be recomputed (this is a fairly heavy
176 * operation, so this should be set only if needed). Typically this is used
177 * to figure out if the content or size of a surface has changed.
178 */
179 virtual void lockPageFlip(bool& recomputeVisibleRegions);
180
181 /**
182 * unlockPageFlip - called each time the screen is redrawn. updates the
183 * final dirty region wrt the planeTransform.
184 * At this point, all visible regions, surface position and size, etc... are
185 * correct.
186 */
187 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
188
189 /**
190 * finishPageFlip - called after all surfaces have drawn.
191 */
192 virtual void finishPageFlip();
193
194 /**
195 * needsBlending - true if this surface needs blending
196 */
197 virtual bool needsBlending() const { return false; }
198
199 /**
200 * transformed -- true is this surface needs a to be transformed
201 */
202 virtual bool transformed() const { return mTransformed; }
203
204 /**
205 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700206 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 */
208 virtual bool isSecure() const { return false; }
209
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700210 /** signal this layer that it's not needed any longer. called from the
211 * main thread */
Mathias Agopian9a112062009-04-17 19:36:26 -0700212 virtual status_t ditch() { return NO_ERROR; }
213
214
215
216 enum { // flags for doTransaction()
217 eVisibleRegion = 0x00000002,
218 eRestartTransaction = 0x00000008
219 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220
221
222 inline const State& drawingState() const { return mDrawingState; }
223 inline const State& currentState() const { return mCurrentState; }
224 inline State& currentState() { return mCurrentState; }
225
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700226 static int compareCurrentStateZ(
227 sp<LayerBase> const * layerA,
228 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 return layerA[0]->currentState().z - layerB[0]->currentState().z;
230 }
231
232 int32_t getOrientation() const { return mOrientation; }
233 int tx() const { return mLeft; }
234 int ty() const { return mTop; }
235
236protected:
237 const GraphicPlane& graphicPlane(int dpy) const;
238 GraphicPlane& graphicPlane(int dpy);
239
240 GLuint createTexture() const;
241
242 void drawWithOpenGL(const Region& clip,
243 GLint textureName,
244 const GGLSurface& surface,
245 int transform = 0) const;
246
247 void clearWithOpenGL(const Region& clip) const;
248
249 void loadTexture(const Region& dirty,
250 GLint textureName, const GGLSurface& t,
251 GLuint& textureWidth, GLuint& textureHeight) const;
252
Mathias Agopian9a112062009-04-17 19:36:26 -0700253 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 uint32_t mFlags;
255
256 // cached during validateVisibility()
257 bool mTransformed;
258 int32_t mOrientation;
259 GLfixed mVertices[4][2];
260 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 int mLeft;
262 int mTop;
263
264 // these are protected by an external lock
265 State mCurrentState;
266 State mDrawingState;
267 volatile int32_t mTransactionFlags;
268
269 // don't change, don't need a lock
270 bool mPremultipliedAlpha;
271
272 // only read
273 const uint32_t mIdentity;
274
275 // atomic
276 volatile int32_t mInvalidate;
277
278
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279protected:
280 virtual ~LayerBase();
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700283 LayerBase(const LayerBase& rhs);
284 void validateTexture(GLint textureName) const;
285 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286};
287
288
289// ---------------------------------------------------------------------------
290
291class LayerBaseClient : public LayerBase
292{
293public:
294 class Surface;
295 static const uint32_t typeInfo;
296 static const char* const typeID;
297 virtual char const* getTypeID() const { return typeID; }
298 virtual uint32_t getTypeInfo() const { return typeInfo; }
299
300 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
301 Client* client, int32_t i);
302 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304
305 Client* const client;
306 layer_cblk_t* const lcblk;
307
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 inline int32_t clientIndex() const { return mIndex; }
310 int32_t serverIndex() const;
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 sp<Surface> getSurface();
314 virtual sp<Surface> createSurface() const;
315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316
317 class Surface : public BnSurface
318 {
319 public:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 virtual void getSurfaceData(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 ISurfaceFlingerClient::surface_data_t* params) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700325 Surface(const sp<SurfaceFlinger>& flinger,
326 SurfaceID id, int identity,
327 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 virtual ~Surface();
329 virtual status_t onTransact(uint32_t code, const Parcel& data,
330 Parcel* reply, uint32_t flags);
331 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332
333 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700334 virtual sp<SurfaceBuffer> getBuffer();
335 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
336 virtual void postBuffer(ssize_t offset);
337 virtual void unregisterBuffers();
338 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
339 int32_t format);
340
Mathias Agopian9a112062009-04-17 19:36:26 -0700341 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700342 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700343 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700344 int32_t mToken;
345 int32_t mIdentity;
346 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 };
348
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700349 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700351private:
352 int32_t mIndex;
353 mutable Mutex mLock;
354 mutable wp<Surface> mClientSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355};
356
357// ---------------------------------------------------------------------------
358
359}; // namespace android
360
361#endif // ANDROID_LAYER_BASE_H