blob: ccff36d36cf1b06e058eac6653cbb3268cadf42b [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 Agopian9a112062009-04-17 19:36:26 -0700210 /** signal this layer that it's not needed any longer */
211 virtual status_t ditch() { return NO_ERROR; }
212
213
214
215 enum { // flags for doTransaction()
216 eVisibleRegion = 0x00000002,
217 eRestartTransaction = 0x00000008
218 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219
220
221 inline const State& drawingState() const { return mDrawingState; }
222 inline const State& currentState() const { return mCurrentState; }
223 inline State& currentState() { return mCurrentState; }
224
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700225 static int compareCurrentStateZ(
226 sp<LayerBase> const * layerA,
227 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 return layerA[0]->currentState().z - layerB[0]->currentState().z;
229 }
230
231 int32_t getOrientation() const { return mOrientation; }
232 int tx() const { return mLeft; }
233 int ty() const { return mTop; }
234
235protected:
236 const GraphicPlane& graphicPlane(int dpy) const;
237 GraphicPlane& graphicPlane(int dpy);
238
239 GLuint createTexture() const;
240
241 void drawWithOpenGL(const Region& clip,
242 GLint textureName,
243 const GGLSurface& surface,
244 int transform = 0) const;
245
246 void clearWithOpenGL(const Region& clip) const;
247
248 void loadTexture(const Region& dirty,
249 GLint textureName, const GGLSurface& t,
250 GLuint& textureWidth, GLuint& textureHeight) const;
251
Mathias Agopian9a112062009-04-17 19:36:26 -0700252 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 uint32_t mFlags;
254
255 // cached during validateVisibility()
256 bool mTransformed;
257 int32_t mOrientation;
258 GLfixed mVertices[4][2];
259 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 int mLeft;
261 int mTop;
262
263 // these are protected by an external lock
264 State mCurrentState;
265 State mDrawingState;
266 volatile int32_t mTransactionFlags;
267
268 // don't change, don't need a lock
269 bool mPremultipliedAlpha;
270
271 // only read
272 const uint32_t mIdentity;
273
274 // atomic
275 volatile int32_t mInvalidate;
276
277
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278protected:
279 virtual ~LayerBase();
280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700282 LayerBase(const LayerBase& rhs);
283 void validateTexture(GLint textureName) const;
284 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285};
286
287
288// ---------------------------------------------------------------------------
289
290class LayerBaseClient : public LayerBase
291{
292public:
293 class Surface;
294 static const uint32_t typeInfo;
295 static const char* const typeID;
296 virtual char const* getTypeID() const { return typeID; }
297 virtual uint32_t getTypeInfo() const { return typeInfo; }
298
299 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
300 Client* client, int32_t i);
301 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700302 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
304 Client* const client;
305 layer_cblk_t* const lcblk;
306
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 inline int32_t clientIndex() const { return mIndex; }
309 int32_t serverIndex() const;
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312 sp<Surface> getSurface();
313 virtual sp<Surface> createSurface() const;
314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316 class Surface : public BnSurface
317 {
318 public:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 virtual void getSurfaceData(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700321 ISurfaceFlingerClient::surface_data_t* params) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700323 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700324 Surface(const sp<SurfaceFlinger>& flinger,
325 SurfaceID id, int identity,
326 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327 virtual ~Surface();
328 virtual status_t onTransact(uint32_t code, const Parcel& data,
329 Parcel* reply, uint32_t flags);
330 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331
332 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700333 virtual sp<SurfaceBuffer> getBuffer();
334 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
335 virtual void postBuffer(ssize_t offset);
336 virtual void unregisterBuffers();
337 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
338 int32_t format);
339
Mathias Agopian9a112062009-04-17 19:36:26 -0700340 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700341 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700342 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700343 int32_t mToken;
344 int32_t mIdentity;
345 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 };
347
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700348 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700350private:
351 int32_t mIndex;
352 mutable Mutex mLock;
353 mutable wp<Surface> mClientSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354};
355
356// ---------------------------------------------------------------------------
357
358}; // namespace android
359
360#endif // ANDROID_LAYER_BASE_H