blob: c177c2a4faf2cf22d732276fae56f21461a98b92 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
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
Mathias Agopian9a112062009-04-17 19:36:26 -0700204 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 */
206 virtual bool isSecure() const { return false; }
207
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700208 /** signal this layer that it's not needed any longer. called from the
209 * main thread */
Mathias Agopian9a112062009-04-17 19:36:26 -0700210 virtual status_t ditch() { return NO_ERROR; }
211
212
213
214 enum { // flags for doTransaction()
215 eVisibleRegion = 0x00000002,
216 eRestartTransaction = 0x00000008
217 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218
219
220 inline const State& drawingState() const { return mDrawingState; }
221 inline const State& currentState() const { return mCurrentState; }
222 inline State& currentState() { return mCurrentState; }
223
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700224 static int compareCurrentStateZ(
225 sp<LayerBase> const * layerA,
226 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 return layerA[0]->currentState().z - layerB[0]->currentState().z;
228 }
229
230 int32_t getOrientation() const { return mOrientation; }
231 int tx() const { return mLeft; }
232 int ty() const { return mTop; }
233
234protected:
235 const GraphicPlane& graphicPlane(int dpy) const;
236 GraphicPlane& graphicPlane(int dpy);
237
238 GLuint createTexture() const;
239
240 void drawWithOpenGL(const Region& clip,
241 GLint textureName,
242 const GGLSurface& surface,
243 int transform = 0) const;
244
245 void clearWithOpenGL(const Region& clip) const;
246
247 void loadTexture(const Region& dirty,
248 GLint textureName, const GGLSurface& t,
249 GLuint& textureWidth, GLuint& textureHeight) const;
250
Mathias Agopian9a112062009-04-17 19:36:26 -0700251 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 uint32_t mFlags;
253
254 // cached during validateVisibility()
255 bool mTransformed;
256 int32_t mOrientation;
257 GLfixed mVertices[4][2];
258 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 int mLeft;
260 int mTop;
261
262 // these are protected by an external lock
263 State mCurrentState;
264 State mDrawingState;
265 volatile int32_t mTransactionFlags;
266
267 // don't change, don't need a lock
268 bool mPremultipliedAlpha;
269
270 // only read
271 const uint32_t mIdentity;
272
273 // atomic
274 volatile int32_t mInvalidate;
275
276
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277protected:
278 virtual ~LayerBase();
279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281 LayerBase(const LayerBase& rhs);
282 void validateTexture(GLint textureName) const;
283 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284};
285
286
287// ---------------------------------------------------------------------------
288
289class LayerBaseClient : public LayerBase
290{
291public:
292 class Surface;
293 static const uint32_t typeInfo;
294 static const char* const typeID;
295 virtual char const* getTypeID() const { return typeID; }
296 virtual uint32_t getTypeInfo() const { return typeInfo; }
297
298 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
299 Client* client, int32_t i);
300 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700301 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
303 Client* const client;
304 layer_cblk_t* const lcblk;
305
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700306 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 inline int32_t clientIndex() const { return mIndex; }
308 int32_t serverIndex() const;
309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700311 sp<Surface> getSurface();
312 virtual sp<Surface> createSurface() const;
313
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314
315 class Surface : public BnSurface
316 {
317 public:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 virtual void getSurfaceData(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700320 ISurfaceFlingerClient::surface_data_t* params) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700323 Surface(const sp<SurfaceFlinger>& flinger,
324 SurfaceID id, int identity,
325 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 virtual ~Surface();
327 virtual status_t onTransact(uint32_t code, const Parcel& data,
328 Parcel* reply, uint32_t flags);
329 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330
331 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332 virtual sp<SurfaceBuffer> getBuffer();
333 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
334 virtual void postBuffer(ssize_t offset);
335 virtual void unregisterBuffers();
336 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
337 int32_t format);
338
Mathias Agopian9a112062009-04-17 19:36:26 -0700339 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700341 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700342 int32_t mToken;
343 int32_t mIdentity;
344 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 };
346
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700349private:
350 int32_t mIndex;
351 mutable Mutex mLock;
352 mutable wp<Surface> mClientSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353};
354
355// ---------------------------------------------------------------------------
356
357}; // namespace android
358
359#endif // ANDROID_LAYER_BASE_H