blob: 6aacd8251dcbbb7dfc20f667d164e9c3b7407722 [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>
Mathias Agopianeda65402010-02-22 03:15:57 -080025#include <GLES/gl.h>
Mathias Agopian1fed11c2009-06-23 18:08:22 -070026
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <utils/RefBase.h>
28
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/Region.h>
30#include <ui/Overlay.h>
31
Mathias Agopian9cce3252010-02-09 17:46:37 -080032#include <surfaceflinger/ISurfaceFlingerClient.h>
33#include <private/surfaceflinger/SharedBufferStack.h>
34#include <private/surfaceflinger/LayerState.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <pixelflinger/pixelflinger.h>
37
38#include "Transform.h"
39
40namespace android {
41
42// ---------------------------------------------------------------------------
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044class DisplayHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class Client;
Mathias Agopian3330b202009-10-05 17:07:12 -070046class GraphicBuffer;
47class GraphicPlane;
48class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50// ---------------------------------------------------------------------------
51
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053{
54 // poor man's dynamic_cast below
55 template<typename T>
56 struct getTypeInfoOfAnyType {
57 static uint32_t get() { return T::typeInfo; }
58 };
59
60 template<typename T>
61 struct getTypeInfoOfAnyType<T*> {
62 static uint32_t get() { return getTypeInfoOfAnyType<T>::get(); }
63 };
64
65public:
66 static const uint32_t typeInfo;
67 static const char* const typeID;
68 virtual char const* getTypeID() const { return typeID; }
69 virtual uint32_t getTypeInfo() const { return typeInfo; }
70
71 template<typename T>
72 static T dynamicCast(LayerBase* base) {
73 uint32_t mostDerivedInfo = base->getTypeInfo();
74 uint32_t castToInfo = getTypeInfoOfAnyType<T>::get();
75 if ((mostDerivedInfo & castToInfo) == castToInfo)
76 return static_cast<T>(base);
77 return 0;
78 }
79
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
83 DisplayID dpy;
84 mutable bool contentDirty;
85 Region visibleRegionScreen;
86 Region transparentRegionScreen;
87 Region coveredRegionScreen;
88
89 struct State {
90 uint32_t w;
91 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070092 uint32_t requested_w;
93 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 uint32_t z;
95 uint8_t alpha;
96 uint8_t flags;
97 uint8_t reserved[2];
98 int32_t sequence; // changes when visible regions can change
99 uint32_t tint;
100 Transform transform;
101 Region transparentRegion;
102 };
103
104 // modify current state
105 bool setPosition(int32_t x, int32_t y);
106 bool setLayer(uint32_t z);
107 bool setSize(uint32_t w, uint32_t h);
108 bool setAlpha(uint8_t alpha);
109 bool setMatrix(const layer_state_t::matrix22_t& matrix);
110 bool setTransparentRegionHint(const Region& opaque);
111 bool setFlags(uint8_t flags, uint8_t mask);
112
Mathias Agopianba6be542009-09-29 22:32:36 -0700113 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 bool requestTransaction();
115 void forceVisibilityTransaction();
116
117 uint32_t getTransactionFlags(uint32_t flags);
118 uint32_t setTransactionFlags(uint32_t flags);
119
120 Rect visibleBounds() const;
121 void drawRegion(const Region& reg) const;
122
123 void invalidate();
124
125 /**
126 * draw - performs some global clipping optimizations
127 * and calls onDraw().
128 * Typically this method is not overridden, instead implement onDraw()
129 * to perform the actual drawing.
130 */
131 virtual void draw(const Region& clip) const;
132
133 /**
134 * onDraw - draws the surface.
135 */
136 virtual void onDraw(const Region& clip) const = 0;
137
138 /**
139 * initStates - called just after construction
140 */
141 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
142
143 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 * doTransaction - process the transaction. This is a good place to figure
145 * out which attributes of the surface have changed.
146 */
147 virtual uint32_t doTransaction(uint32_t transactionFlags);
148
149 /**
150 * setVisibleRegion - called to set the new visible region. This gives
151 * a chance to update the new visible region or record the fact it changed.
152 */
153 virtual void setVisibleRegion(const Region& visibleRegion);
154
155 /**
156 * setCoveredRegion - called when the covered region changes. The covered
157 * region correspond to any area of the surface that is covered
158 * (transparently or not) by another surface.
159 */
160 virtual void setCoveredRegion(const Region& coveredRegion);
161
162 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 * validateVisibility - cache a bunch of things
164 */
165 virtual void validateVisibility(const Transform& globalTransform);
166
167 /**
168 * lockPageFlip - called each time the screen is redrawn and returns whether
169 * the visible regions need to be recomputed (this is a fairly heavy
170 * operation, so this should be set only if needed). Typically this is used
171 * to figure out if the content or size of a surface has changed.
172 */
173 virtual void lockPageFlip(bool& recomputeVisibleRegions);
174
175 /**
176 * unlockPageFlip - called each time the screen is redrawn. updates the
177 * final dirty region wrt the planeTransform.
178 * At this point, all visible regions, surface position and size, etc... are
179 * correct.
180 */
181 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
182
183 /**
184 * finishPageFlip - called after all surfaces have drawn.
185 */
186 virtual void finishPageFlip();
187
188 /**
189 * needsBlending - true if this surface needs blending
190 */
191 virtual bool needsBlending() const { return false; }
192
193 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700194 * needsDithering - true if this surface needs dithering
195 */
196 virtual bool needsDithering() const { return false; }
197
198 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 * 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 Agopian0b3ad462009-10-02 18:12:30 -0700209 /** Called from the main thread, when the surface is removed from the
210 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700211 virtual status_t ditch() { return NO_ERROR; }
212
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700213 /** called with the state lock when the surface is removed from the
214 * current list */
215 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700216
217
218 enum { // flags for doTransaction()
219 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700220 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221
222
223 inline const State& drawingState() const { return mDrawingState; }
224 inline const State& currentState() const { return mCurrentState; }
225 inline State& currentState() { return mCurrentState; }
226
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227 static int compareCurrentStateZ(
228 sp<LayerBase> const * layerA,
229 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 return layerA[0]->currentState().z - layerB[0]->currentState().z;
231 }
232
233 int32_t getOrientation() const { return mOrientation; }
234 int tx() const { return mLeft; }
235 int ty() const { return mTop; }
236
237protected:
238 const GraphicPlane& graphicPlane(int dpy) const;
239 GraphicPlane& graphicPlane(int dpy);
240
241 GLuint createTexture() const;
242
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700243 struct Texture {
244 Texture() : name(-1U), width(0), height(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 image(EGL_NO_IMAGE_KHR), transform(0),
246 NPOTAdjust(false), dirty(true) { }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700247 GLuint name;
248 GLuint width;
249 GLuint height;
Mathias Agopian3330b202009-10-05 17:07:12 -0700250 GLuint potWidth;
251 GLuint potHeight;
252 GLfloat wScale;
253 GLfloat hScale;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700254 EGLImageKHR image;
255 uint32_t transform;
Mathias Agopian3330b202009-10-05 17:07:12 -0700256 bool NPOTAdjust;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700257 bool dirty;
258 };
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700259
260 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
261 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700263 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700264 void loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700265 const Region& dirty, const GGLSurface& t) const;
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700266 status_t initializeEglImage(
267 const sp<GraphicBuffer>& buffer, Texture* texture);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800269 bool isSupportedYuvFormat(int format) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700270
Mathias Agopian9a112062009-04-17 19:36:26 -0700271 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 uint32_t mFlags;
273
274 // cached during validateVisibility()
275 bool mTransformed;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700276 bool mUseLinearFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 int32_t mOrientation;
278 GLfixed mVertices[4][2];
279 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 int mLeft;
281 int mTop;
282
283 // these are protected by an external lock
284 State mCurrentState;
285 State mDrawingState;
286 volatile int32_t mTransactionFlags;
287
288 // don't change, don't need a lock
289 bool mPremultipliedAlpha;
290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 // atomic
292 volatile int32_t mInvalidate;
293
294
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700295protected:
296 virtual ~LayerBase();
297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700299 LayerBase(const LayerBase& rhs);
300 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301};
302
303
304// ---------------------------------------------------------------------------
305
306class LayerBaseClient : public LayerBase
307{
308public:
309 class Surface;
310 static const uint32_t typeInfo;
311 static const char* const typeID;
312 virtual char const* getTypeID() const { return typeID; }
313 virtual uint32_t getTypeInfo() const { return typeInfo; }
314
Mathias Agopian48d819a2009-09-10 19:41:18 -0700315 // lcblk is (almost) only accessed from the main SF thread, in the places
316 // where it's not, a reference to Client must be held
317 SharedBufferServer* lcblk;
318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700320 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323
Mathias Agopian48d819a2009-09-10 19:41:18 -0700324 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 inline int32_t clientIndex() const { return mIndex; }
328 int32_t serverIndex() const;
329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700331 sp<Surface> getSurface();
332 virtual sp<Surface> createSurface() const;
333
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700334 virtual void onRemoved();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335
336 class Surface : public BnSurface
337 {
338 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700339 int32_t getToken() const { return mToken; }
340 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700341
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700342 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700343 Surface(const sp<SurfaceFlinger>& flinger,
344 SurfaceID id, int identity,
345 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 virtual ~Surface();
347 virtual status_t onTransact(uint32_t code, const Parcel& data,
348 Parcel* reply, uint32_t flags);
349 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
351 private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700352 virtual sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700353 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
354 virtual void postBuffer(ssize_t offset);
355 virtual void unregisterBuffers();
356 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800357 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700358
Mathias Agopian9a112062009-04-17 19:36:26 -0700359 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700360 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700361 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700362 int32_t mToken;
363 int32_t mIdentity;
364 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365 };
366
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700367 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700369private:
370 int32_t mIndex;
371 mutable Mutex mLock;
372 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700373 // only read
374 const uint32_t mIdentity;
375 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376};
377
378// ---------------------------------------------------------------------------
379
380}; // namespace android
381
382#endif // ANDROID_LAYER_BASE_H