blob: 6fb1d1c50cabf52f9510de31d6cb7bdf758fd115 [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
Mathias Agopian1fed11c2009-06-23 18:08:22 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <private/ui/LayerState.h>
27
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <utils/RefBase.h>
29
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <ui/Region.h>
31#include <ui/Overlay.h>
32
33#include <pixelflinger/pixelflinger.h>
34
35#include "Transform.h"
36
37namespace android {
38
39// ---------------------------------------------------------------------------
40
41class SurfaceFlinger;
42class DisplayHardware;
43class GraphicPlane;
44class Client;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045class SurfaceBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -070046class Buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
48// ---------------------------------------------------------------------------
49
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051{
52 // poor man's dynamic_cast below
53 template<typename T>
54 struct getTypeInfoOfAnyType {
55 static uint32_t get() { return T::typeInfo; }
56 };
57
58 template<typename T>
59 struct getTypeInfoOfAnyType<T*> {
60 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
61 };
62
63public:
64 static const uint32_t typeInfo;
65 static const char* const typeID;
66 virtual char const* getTypeID() const { return typeID; }
67 virtual uint32_t getTypeInfo() const { return typeInfo; }
68
69 template<typename T>
70 static T dynamicCast(LayerBase* base) {
71 uint32_t mostDerivedInfo = base->getTypeInfo();
72 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
73 if ((mostDerivedInfo & castToInfo) == castToInfo)
74 return static_cast<T>(base);
75 return 0;
76 }
77
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
81 DisplayID dpy;
82 mutable bool contentDirty;
83 Region visibleRegionScreen;
84 Region transparentRegionScreen;
85 Region coveredRegionScreen;
86
87 struct State {
88 uint32_t w;
89 uint32_t h;
90 uint32_t z;
91 uint8_t alpha;
92 uint8_t flags;
93 uint8_t reserved[2];
94 int32_t sequence; // changes when visible regions can change
95 uint32_t tint;
96 Transform transform;
97 Region transparentRegion;
98 };
99
100 // modify current state
101 bool setPosition(int32_t x, int32_t y);
102 bool setLayer(uint32_t z);
103 bool setSize(uint32_t w, uint32_t h);
104 bool setAlpha(uint8_t alpha);
105 bool setMatrix(const layer_state_t::matrix22_t& matrix);
106 bool setTransparentRegionHint(const Region& opaque);
107 bool setFlags(uint8_t flags, uint8_t mask);
108
109 void commitTransaction(bool skipSize);
110 bool requestTransaction();
111 void forceVisibilityTransaction();
112
113 uint32_t getTransactionFlags(uint32_t flags);
114 uint32_t setTransactionFlags(uint32_t flags);
115
116 Rect visibleBounds() const;
117 void drawRegion(const Region& reg) const;
118
119 void invalidate();
120
121 /**
122 * draw - performs some global clipping optimizations
123 * and calls onDraw().
124 * Typically this method is not overridden, instead implement onDraw()
125 * to perform the actual drawing.
126 */
127 virtual void draw(const Region& clip) const;
128
129 /**
130 * onDraw - draws the surface.
131 */
132 virtual void onDraw(const Region& clip) const = 0;
133
134 /**
135 * initStates - called just after construction
136 */
137 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
138
139 /**
140 * setSizeChanged - called when the *current* state's size is changed.
141 */
142 virtual void setSizeChanged(uint32_t w, uint32_t h);
143
144 /**
145 * doTransaction - process the transaction. This is a good place to figure
146 * out which attributes of the surface have changed.
147 */
148 virtual uint32_t doTransaction(uint32_t transactionFlags);
149
150 /**
151 * setVisibleRegion - called to set the new visible region. This gives
152 * a chance to update the new visible region or record the fact it changed.
153 */
154 virtual void setVisibleRegion(const Region& visibleRegion);
155
156 /**
157 * setCoveredRegion - called when the covered region changes. The covered
158 * region correspond to any area of the surface that is covered
159 * (transparently or not) by another surface.
160 */
161 virtual void setCoveredRegion(const Region& coveredRegion);
162
163 /**
164 * getPhysicalSize - returns the physical size of the drawing state of
165 * the surface. If the surface is backed by a bitmap, this is the size of
166 * the bitmap (as opposed to the size of the drawing state).
167 */
168 virtual Point getPhysicalSize() const;
169
170 /**
171 * validateVisibility - cache a bunch of things
172 */
173 virtual void validateVisibility(const Transform& globalTransform);
174
175 /**
176 * lockPageFlip - called each time the screen is redrawn and returns whether
177 * the visible regions need to be recomputed (this is a fairly heavy
178 * operation, so this should be set only if needed). Typically this is used
179 * to figure out if the content or size of a surface has changed.
180 */
181 virtual void lockPageFlip(bool& recomputeVisibleRegions);
182
183 /**
184 * unlockPageFlip - called each time the screen is redrawn. updates the
185 * final dirty region wrt the planeTransform.
186 * At this point, all visible regions, surface position and size, etc... are
187 * correct.
188 */
189 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
190
191 /**
192 * finishPageFlip - called after all surfaces have drawn.
193 */
194 virtual void finishPageFlip();
195
196 /**
197 * needsBlending - true if this surface needs blending
198 */
199 virtual bool needsBlending() const { return false; }
200
201 /**
202 * transformed -- true is this surface needs a to be transformed
203 */
204 virtual bool transformed() const { return mTransformed; }
205
206 /**
207 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700208 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 */
210 virtual bool isSecure() const { return false; }
211
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700212 /** signal this layer that it's not needed any longer. called from the
213 * main thread */
Mathias Agopian9a112062009-04-17 19:36:26 -0700214 virtual status_t ditch() { return NO_ERROR; }
215
216
217
218 enum { // flags for doTransaction()
219 eVisibleRegion = 0x00000002,
220 eRestartTransaction = 0x00000008
221 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222
223
224 inline const State& drawingState() const { return mDrawingState; }
225 inline const State& currentState() const { return mCurrentState; }
226 inline State& currentState() { return mCurrentState; }
227
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228 static int compareCurrentStateZ(
229 sp<LayerBase> const * layerA,
230 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 return layerA[0]->currentState().z - layerB[0]->currentState().z;
232 }
233
234 int32_t getOrientation() const { return mOrientation; }
235 int tx() const { return mLeft; }
236 int ty() const { return mTop; }
237
238protected:
239 const GraphicPlane& graphicPlane(int dpy) const;
240 GraphicPlane& graphicPlane(int dpy);
241
242 GLuint createTexture() const;
243
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700244 struct Texture {
245 Texture() : name(-1U), width(0), height(0),
246 image(EGL_NO_IMAGE_KHR), transform(0), dirty(true) { }
247 GLuint name;
248 GLuint width;
249 GLuint height;
250 EGLImageKHR image;
251 uint32_t transform;
252 bool dirty;
253 };
254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700256 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
257 void loadTexture(Texture* texture, GLint textureName,
258 const Region& dirty, const GGLSurface& t) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700260
Mathias Agopian9a112062009-04-17 19:36:26 -0700261 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 uint32_t mFlags;
263
264 // cached during validateVisibility()
265 bool mTransformed;
266 int32_t mOrientation;
267 GLfixed mVertices[4][2];
268 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 int mLeft;
270 int mTop;
271
272 // these are protected by an external lock
273 State mCurrentState;
274 State mDrawingState;
275 volatile int32_t mTransactionFlags;
276
277 // don't change, don't need a lock
278 bool mPremultipliedAlpha;
279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 // atomic
281 volatile int32_t mInvalidate;
282
283
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700284protected:
285 virtual ~LayerBase();
286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700288 LayerBase(const LayerBase& rhs);
289 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290};
291
292
293// ---------------------------------------------------------------------------
294
295class LayerBaseClient : public LayerBase
296{
297public:
298 class Surface;
299 static const uint32_t typeInfo;
300 static const char* const typeID;
301 virtual char const* getTypeID() const { return typeID; }
302 virtual uint32_t getTypeInfo() const { return typeInfo; }
303
304 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700305 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
Mathias Agopianf9d93272009-06-19 17:00:27 -0700309 wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 layer_cblk_t* const lcblk;
311
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 inline int32_t clientIndex() const { return mIndex; }
314 int32_t serverIndex() const;
315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317 sp<Surface> getSurface();
318 virtual sp<Surface> createSurface() const;
319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320
321 class Surface : public BnSurface
322 {
323 public:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 virtual void getSurfaceData(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 ISurfaceFlingerClient::surface_data_t* params) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700329 Surface(const sp<SurfaceFlinger>& flinger,
330 SurfaceID id, int identity,
331 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332 virtual ~Surface();
333 virtual status_t onTransact(uint32_t code, const Parcel& data,
334 Parcel* reply, uint32_t flags);
335 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336
337 private:
Fred Quintanab2fd4662009-08-11 20:49:35 -0700338 virtual sp<SurfaceBuffer> getBuffer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
340 virtual void postBuffer(ssize_t offset);
341 virtual void unregisterBuffers();
342 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
343 int32_t format);
344
Mathias Agopian9a112062009-04-17 19:36:26 -0700345 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700347 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700348 int32_t mToken;
349 int32_t mIdentity;
350 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 };
352
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700353 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700355private:
356 int32_t mIndex;
357 mutable Mutex mLock;
358 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700359 // only read
360 const uint32_t mIdentity;
361 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362};
363
364// ---------------------------------------------------------------------------
365
366}; // namespace android
367
368#endif // ANDROID_LAYER_BASE_H