blob: 2a4e13391c25dbc3664859ce6f838a0fb57c672b [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
206 * screenshots or vns servers.
207 */
208 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
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 static int compareCurrentStateZ(
221 sp<LayerBase> const * layerA,
222 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 return layerA[0]->currentState().z - layerB[0]->currentState().z;
224 }
225
226 int32_t getOrientation() const { return mOrientation; }
227 int tx() const { return mLeft; }
228 int ty() const { return mTop; }
229
230protected:
231 const GraphicPlane& graphicPlane(int dpy) const;
232 GraphicPlane& graphicPlane(int dpy);
233
234 GLuint createTexture() const;
235
236 void drawWithOpenGL(const Region& clip,
237 GLint textureName,
238 const GGLSurface& surface,
239 int transform = 0) const;
240
241 void clearWithOpenGL(const Region& clip) const;
242
243 void loadTexture(const Region& dirty,
244 GLint textureName, const GGLSurface& t,
245 GLuint& textureWidth, GLuint& textureHeight) const;
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 SurfaceFlinger* mFlinger;
248 uint32_t mFlags;
249
250 // cached during validateVisibility()
251 bool mTransformed;
252 int32_t mOrientation;
253 GLfixed mVertices[4][2];
254 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 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
267 const uint32_t mIdentity;
268
269 // atomic
270 volatile int32_t mInvalidate;
271
272
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700273protected:
274 virtual ~LayerBase();
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277 LayerBase(const LayerBase& rhs);
278 void validateTexture(GLint textureName) const;
279 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280};
281
282
283// ---------------------------------------------------------------------------
284
285class LayerBaseClient : public LayerBase
286{
287public:
288 class Surface;
289 static const uint32_t typeInfo;
290 static const char* const typeID;
291 virtual char const* getTypeID() const { return typeID; }
292 virtual uint32_t getTypeInfo() const { return typeInfo; }
293
294 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
295 Client* client, int32_t i);
296 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
299 Client* const client;
300 layer_cblk_t* const lcblk;
301
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700302 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 inline int32_t clientIndex() const { return mIndex; }
304 int32_t serverIndex() const;
305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307 sp<Surface> getSurface();
308 virtual sp<Surface> createSurface() const;
309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
311 class Surface : public BnSurface
312 {
313 public:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 virtual void getSurfaceData(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316 ISurfaceFlingerClient::surface_data_t* params) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318 protected:
319 Surface(SurfaceID id, int identity, const sp<LayerBaseClient>& owner);
320 virtual ~Surface();
321 virtual status_t onTransact(uint32_t code, const Parcel& data,
322 Parcel* reply, uint32_t flags);
323 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324
325 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 virtual sp<SurfaceBuffer> getBuffer();
327 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
328 virtual void postBuffer(ssize_t offset);
329 virtual void unregisterBuffers();
330 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
331 int32_t format);
332
333 private:
334 friend class LayerBaseClient;
335 int32_t mToken;
336 int32_t mIdentity;
337 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 };
339
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700342private:
343 int32_t mIndex;
344 mutable Mutex mLock;
345 mutable wp<Surface> mClientSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346};
347
348// ---------------------------------------------------------------------------
349
350}; // namespace android
351
352#endif // ANDROID_LAYER_BASE_H