blob: b5265d2b2b5649f5a19000acbf6dd25923a270aa [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;
Mathias Agopian0926f502009-05-04 14:17:04 -070043class Buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45// ---------------------------------------------------------------------------
46
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048{
49 // poor man's dynamic_cast below
50 template<typename T>
51 struct getTypeInfoOfAnyType {
52 static uint32_t get() { return T::typeInfo; }
53 };
54
55 template<typename T>
56 struct getTypeInfoOfAnyType<T*> {
57 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
58 };
59
60public:
61 static const uint32_t typeInfo;
62 static const char* const typeID;
63 virtual char const* getTypeID() const { return typeID; }
64 virtual uint32_t getTypeInfo() const { return typeInfo; }
65
66 template<typename T>
67 static T dynamicCast(LayerBase* base) {
68 uint32_t mostDerivedInfo = base->getTypeInfo();
69 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
70 if ((mostDerivedInfo & castToInfo) == castToInfo)
71 return static_cast<T>(base);
72 return 0;
73 }
74
75
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
78 DisplayID dpy;
79 mutable bool contentDirty;
80 Region visibleRegionScreen;
81 Region transparentRegionScreen;
82 Region coveredRegionScreen;
83
84 struct State {
85 uint32_t w;
86 uint32_t h;
87 uint32_t z;
88 uint8_t alpha;
89 uint8_t flags;
90 uint8_t reserved[2];
91 int32_t sequence; // changes when visible regions can change
92 uint32_t tint;
93 Transform transform;
94 Region transparentRegion;
95 };
96
97 // modify current state
98 bool setPosition(int32_t x, int32_t y);
99 bool setLayer(uint32_t z);
100 bool setSize(uint32_t w, uint32_t h);
101 bool setAlpha(uint8_t alpha);
102 bool setMatrix(const layer_state_t::matrix22_t& matrix);
103 bool setTransparentRegionHint(const Region& opaque);
104 bool setFlags(uint8_t flags, uint8_t mask);
105
106 void commitTransaction(bool skipSize);
107 bool requestTransaction();
108 void forceVisibilityTransaction();
109
110 uint32_t getTransactionFlags(uint32_t flags);
111 uint32_t setTransactionFlags(uint32_t flags);
112
113 Rect visibleBounds() const;
114 void drawRegion(const Region& reg) const;
115
116 void invalidate();
117
118 /**
119 * draw - performs some global clipping optimizations
120 * and calls onDraw().
121 * Typically this method is not overridden, instead implement onDraw()
122 * to perform the actual drawing.
123 */
124 virtual void draw(const Region& clip) const;
125
126 /**
127 * onDraw - draws the surface.
128 */
129 virtual void onDraw(const Region& clip) const = 0;
130
131 /**
132 * initStates - called just after construction
133 */
134 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
135
136 /**
137 * setSizeChanged - called when the *current* state's size is changed.
138 */
139 virtual void setSizeChanged(uint32_t w, uint32_t h);
140
141 /**
142 * doTransaction - process the transaction. This is a good place to figure
143 * out which attributes of the surface have changed.
144 */
145 virtual uint32_t doTransaction(uint32_t transactionFlags);
146
147 /**
148 * setVisibleRegion - called to set the new visible region. This gives
149 * a chance to update the new visible region or record the fact it changed.
150 */
151 virtual void setVisibleRegion(const Region& visibleRegion);
152
153 /**
154 * setCoveredRegion - called when the covered region changes. The covered
155 * region correspond to any area of the surface that is covered
156 * (transparently or not) by another surface.
157 */
158 virtual void setCoveredRegion(const Region& coveredRegion);
159
160 /**
161 * getPhysicalSize - returns the physical size of the drawing state of
162 * the surface. If the surface is backed by a bitmap, this is the size of
163 * the bitmap (as opposed to the size of the drawing state).
164 */
165 virtual Point getPhysicalSize() const;
166
167 /**
168 * validateVisibility - cache a bunch of things
169 */
170 virtual void validateVisibility(const Transform& globalTransform);
171
172 /**
173 * lockPageFlip - called each time the screen is redrawn and returns whether
174 * the visible regions need to be recomputed (this is a fairly heavy
175 * operation, so this should be set only if needed). Typically this is used
176 * to figure out if the content or size of a surface has changed.
177 */
178 virtual void lockPageFlip(bool& recomputeVisibleRegions);
179
180 /**
181 * unlockPageFlip - called each time the screen is redrawn. updates the
182 * final dirty region wrt the planeTransform.
183 * At this point, all visible regions, surface position and size, etc... are
184 * correct.
185 */
186 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
187
188 /**
189 * finishPageFlip - called after all surfaces have drawn.
190 */
191 virtual void finishPageFlip();
192
193 /**
194 * needsBlending - true if this surface needs blending
195 */
196 virtual bool needsBlending() const { return false; }
197
198 /**
199 * transformed -- true is this surface needs a to be transformed
200 */
201 virtual bool transformed() const { return mTransformed; }
202
203 /**
204 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700205 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 */
207 virtual bool isSecure() const { return false; }
208
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700209 /** signal this layer that it's not needed any longer. called from the
210 * main thread */
Mathias Agopian9a112062009-04-17 19:36:26 -0700211 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,
Mathias Agopian0926f502009-05-04 14:17:04 -0700243 const sp<const Buffer>& buffer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 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,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700300 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700302 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
Mathias Agopianf9d93272009-06-19 17:00:27 -0700304 wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 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