blob: 3a5224082439593716b32047fc8efccc2f067b18 [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
Mathias Agopiancbb288b2009-09-07 16:32:45 -070026#include <private/ui/SharedBufferStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <private/ui/LayerState.h>
28
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <utils/RefBase.h>
30
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <ui/Region.h>
32#include <ui/Overlay.h>
33
34#include <pixelflinger/pixelflinger.h>
35
36#include "Transform.h"
37
38namespace android {
39
40// ---------------------------------------------------------------------------
41
42class SurfaceFlinger;
43class DisplayHardware;
44class GraphicPlane;
45class Client;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046class SurfaceBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -070047class Buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
49// ---------------------------------------------------------------------------
50
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052{
53 // poor man's dynamic_cast below
54 template<typename T>
55 struct getTypeInfoOfAnyType {
56 static uint32_t get() { return T::typeInfo; }
57 };
58
59 template<typename T>
60 struct getTypeInfoOfAnyType<T*> {
61 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
62 };
63
64public:
65 static const uint32_t typeInfo;
66 static const char* const typeID;
67 virtual char const* getTypeID() const { return typeID; }
68 virtual uint32_t getTypeInfo() const { return typeInfo; }
69
70 template<typename T>
71 static T dynamicCast(LayerBase* base) {
72 uint32_t mostDerivedInfo = base->getTypeInfo();
73 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
74 if ((mostDerivedInfo & castToInfo) == castToInfo)
75 return static_cast<T>(base);
76 return 0;
77 }
78
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
82 DisplayID dpy;
83 mutable bool contentDirty;
84 Region visibleRegionScreen;
85 Region transparentRegionScreen;
86 Region coveredRegionScreen;
87
88 struct State {
89 uint32_t w;
90 uint32_t h;
91 uint32_t z;
92 uint8_t alpha;
93 uint8_t flags;
94 uint8_t reserved[2];
95 int32_t sequence; // changes when visible regions can change
96 uint32_t tint;
97 Transform transform;
98 Region transparentRegion;
99 };
100
101 // modify current state
102 bool setPosition(int32_t x, int32_t y);
103 bool setLayer(uint32_t z);
104 bool setSize(uint32_t w, uint32_t h);
105 bool setAlpha(uint8_t alpha);
106 bool setMatrix(const layer_state_t::matrix22_t& matrix);
107 bool setTransparentRegionHint(const Region& opaque);
108 bool setFlags(uint8_t flags, uint8_t mask);
109
110 void commitTransaction(bool skipSize);
111 bool requestTransaction();
112 void forceVisibilityTransaction();
113
114 uint32_t getTransactionFlags(uint32_t flags);
115 uint32_t setTransactionFlags(uint32_t flags);
116
117 Rect visibleBounds() const;
118 void drawRegion(const Region& reg) const;
119
120 void invalidate();
121
122 /**
123 * draw - performs some global clipping optimizations
124 * and calls onDraw().
125 * Typically this method is not overridden, instead implement onDraw()
126 * to perform the actual drawing.
127 */
128 virtual void draw(const Region& clip) const;
129
130 /**
131 * onDraw - draws the surface.
132 */
133 virtual void onDraw(const Region& clip) const = 0;
134
135 /**
136 * initStates - called just after construction
137 */
138 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
139
140 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 * 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 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 * validateVisibility - cache a bunch of things
161 */
162 virtual void validateVisibility(const Transform& globalTransform);
163
164 /**
165 * lockPageFlip - called each time the screen is redrawn and returns whether
166 * the visible regions need to be recomputed (this is a fairly heavy
167 * operation, so this should be set only if needed). Typically this is used
168 * to figure out if the content or size of a surface has changed.
169 */
170 virtual void lockPageFlip(bool& recomputeVisibleRegions);
171
172 /**
173 * unlockPageFlip - called each time the screen is redrawn. updates the
174 * final dirty region wrt the planeTransform.
175 * At this point, all visible regions, surface position and size, etc... are
176 * correct.
177 */
178 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
179
180 /**
181 * finishPageFlip - called after all surfaces have drawn.
182 */
183 virtual void finishPageFlip();
184
185 /**
186 * needsBlending - true if this surface needs blending
187 */
188 virtual bool needsBlending() const { return false; }
189
190 /**
191 * transformed -- true is this surface needs a to be transformed
192 */
193 virtual bool transformed() const { return mTransformed; }
194
195 /**
196 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700197 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 */
199 virtual bool isSecure() const { return false; }
200
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700201 /** signal this layer that it's not needed any longer. called from the
202 * main thread */
Mathias Agopian9a112062009-04-17 19:36:26 -0700203 virtual status_t ditch() { return NO_ERROR; }
204
205
206
207 enum { // flags for doTransaction()
208 eVisibleRegion = 0x00000002,
209 eRestartTransaction = 0x00000008
210 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211
212
213 inline const State& drawingState() const { return mDrawingState; }
214 inline const State& currentState() const { return mCurrentState; }
215 inline State& currentState() { return mCurrentState; }
216
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 static int compareCurrentStateZ(
218 sp<LayerBase> const * layerA,
219 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 return layerA[0]->currentState().z - layerB[0]->currentState().z;
221 }
222
223 int32_t getOrientation() const { return mOrientation; }
224 int tx() const { return mLeft; }
225 int ty() const { return mTop; }
226
227protected:
228 const GraphicPlane& graphicPlane(int dpy) const;
229 GraphicPlane& graphicPlane(int dpy);
230
231 GLuint createTexture() const;
232
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700233 struct Texture {
234 Texture() : name(-1U), width(0), height(0),
235 image(EGL_NO_IMAGE_KHR), transform(0), dirty(true) { }
236 GLuint name;
237 GLuint width;
238 GLuint height;
239 EGLImageKHR image;
240 uint32_t transform;
241 bool dirty;
242 };
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700243
244 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
245 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700247 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
248 void loadTexture(Texture* texture, GLint textureName,
249 const Region& dirty, const GGLSurface& t) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700251
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 // atomic
272 volatile int32_t mInvalidate;
273
274
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275protected:
276 virtual ~LayerBase();
277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 LayerBase(const LayerBase& rhs);
280 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281};
282
283
284// ---------------------------------------------------------------------------
285
286class LayerBaseClient : public LayerBase
287{
288public:
289 class Surface;
290 static const uint32_t typeInfo;
291 static const char* const typeID;
292 virtual char const* getTypeID() const { return typeID; }
293 virtual uint32_t getTypeInfo() const { return typeInfo; }
294
Mathias Agopian48d819a2009-09-10 19:41:18 -0700295 // lcblk is (almost) only accessed from the main SF thread, in the places
296 // where it's not, a reference to Client must be held
297 SharedBufferServer* lcblk;
298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 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 Agopian48d819a2009-09-10 19:41:18 -0700304 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
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
Mathias Agopian48d819a2009-09-10 19:41:18 -0700314 virtual void onRemoved() { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316 class Surface : public BnSurface
317 {
318 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700319 int32_t getToken() const { return mToken; }
320 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700321
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 Agopiancbb288b2009-09-07 16:32:45 -0700332 virtual sp<SurfaceBuffer> requestBuffer(int index, int usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700333 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;
Mathias Agopian2e123242009-06-23 20:06:46 -0700353 // only read
354 const uint32_t mIdentity;
355 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356};
357
358// ---------------------------------------------------------------------------
359
360}; // namespace android
361
362#endif // ANDROID_LAYER_BASE_H