blob: 62ec8399ad28ddf3f8fa690ece3cac4b08b0a8fe [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
Mathias Agopiand1296592010-03-09 19:17:47 -0800104 void setName(const String8& name);
105 String8 getName() const;
106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 // modify current state
108 bool setPosition(int32_t x, int32_t y);
109 bool setLayer(uint32_t z);
110 bool setSize(uint32_t w, uint32_t h);
111 bool setAlpha(uint8_t alpha);
112 bool setMatrix(const layer_state_t::matrix22_t& matrix);
113 bool setTransparentRegionHint(const Region& opaque);
114 bool setFlags(uint8_t flags, uint8_t mask);
115
Mathias Agopianba6be542009-09-29 22:32:36 -0700116 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 bool requestTransaction();
118 void forceVisibilityTransaction();
119
120 uint32_t getTransactionFlags(uint32_t flags);
121 uint32_t setTransactionFlags(uint32_t flags);
122
123 Rect visibleBounds() const;
124 void drawRegion(const Region& reg) const;
125
126 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 /**
129 * draw - performs some global clipping optimizations
130 * and calls onDraw().
131 * Typically this method is not overridden, instead implement onDraw()
132 * to perform the actual drawing.
133 */
134 virtual void draw(const Region& clip) const;
135
136 /**
137 * onDraw - draws the surface.
138 */
139 virtual void onDraw(const Region& clip) const = 0;
140
141 /**
142 * initStates - called just after construction
143 */
144 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
145
146 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 * doTransaction - process the transaction. This is a good place to figure
148 * out which attributes of the surface have changed.
149 */
150 virtual uint32_t doTransaction(uint32_t transactionFlags);
151
152 /**
153 * setVisibleRegion - called to set the new visible region. This gives
154 * a chance to update the new visible region or record the fact it changed.
155 */
156 virtual void setVisibleRegion(const Region& visibleRegion);
157
158 /**
159 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700160 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 * (transparently or not) by another surface.
162 */
163 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 * validateVisibility - cache a bunch of things
167 */
168 virtual void validateVisibility(const Transform& globalTransform);
169
170 /**
171 * lockPageFlip - called each time the screen is redrawn and returns whether
172 * the visible regions need to be recomputed (this is a fairly heavy
173 * operation, so this should be set only if needed). Typically this is used
174 * to figure out if the content or size of a surface has changed.
175 */
176 virtual void lockPageFlip(bool& recomputeVisibleRegions);
177
178 /**
179 * unlockPageFlip - called each time the screen is redrawn. updates the
180 * final dirty region wrt the planeTransform.
181 * At this point, all visible regions, surface position and size, etc... are
182 * correct.
183 */
184 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
185
186 /**
187 * finishPageFlip - called after all surfaces have drawn.
188 */
189 virtual void finishPageFlip();
190
191 /**
192 * needsBlending - true if this surface needs blending
193 */
194 virtual bool needsBlending() const { return false; }
195
196 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700197 * needsDithering - true if this surface needs dithering
198 */
199 virtual bool needsDithering() const { return false; }
200
201 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 * 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 Agopian0b3ad462009-10-02 18:12:30 -0700212 /** Called from the main thread, when the surface is removed from the
213 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700214 virtual status_t ditch() { return NO_ERROR; }
215
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700216 /** called with the state lock when the surface is removed from the
217 * current list */
218 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700219
220
221 enum { // flags for doTransaction()
222 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700223 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224
225
226 inline const State& drawingState() const { return mDrawingState; }
227 inline const State& currentState() const { return mCurrentState; }
228 inline State& currentState() { return mCurrentState; }
229
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230 static int compareCurrentStateZ(
231 sp<LayerBase> const * layerA,
232 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 return layerA[0]->currentState().z - layerB[0]->currentState().z;
234 }
235
236 int32_t getOrientation() const { return mOrientation; }
237 int tx() const { return mLeft; }
238 int ty() const { return mTop; }
239
240protected:
241 const GraphicPlane& graphicPlane(int dpy) const;
242 GraphicPlane& graphicPlane(int dpy);
243
244 GLuint createTexture() const;
245
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700246 struct Texture {
247 Texture() : name(-1U), width(0), height(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700248 image(EGL_NO_IMAGE_KHR), transform(0),
249 NPOTAdjust(false), dirty(true) { }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700250 GLuint name;
251 GLuint width;
252 GLuint height;
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 GLuint potWidth;
254 GLuint potHeight;
255 GLfloat wScale;
256 GLfloat hScale;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700257 EGLImageKHR image;
258 uint32_t transform;
Mathias Agopian3330b202009-10-05 17:07:12 -0700259 bool NPOTAdjust;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700260 bool dirty;
261 };
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700262
263 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
264 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700266 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700267 void loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700268 const Region& dirty, const GGLSurface& t) const;
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700269 status_t initializeEglImage(
270 const sp<GraphicBuffer>& buffer, Texture* texture);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800272 bool isSupportedYuvFormat(int format) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700273
Mathias Agopian9a112062009-04-17 19:36:26 -0700274 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 uint32_t mFlags;
276
277 // cached during validateVisibility()
278 bool mTransformed;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700279 bool mUseLinearFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 int32_t mOrientation;
281 GLfixed mVertices[4][2];
282 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 int mLeft;
284 int mTop;
285
286 // these are protected by an external lock
287 State mCurrentState;
288 State mDrawingState;
289 volatile int32_t mTransactionFlags;
290
291 // don't change, don't need a lock
292 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800293 String8 mName;
294 mutable bool mDebug;
295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 // atomic
298 volatile int32_t mInvalidate;
299
300
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700301protected:
302 virtual ~LayerBase();
303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700305 LayerBase(const LayerBase& rhs);
306 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307};
308
309
310// ---------------------------------------------------------------------------
311
312class LayerBaseClient : public LayerBase
313{
314public:
315 class Surface;
316 static const uint32_t typeInfo;
317 static const char* const typeID;
318 virtual char const* getTypeID() const { return typeID; }
319 virtual uint32_t getTypeInfo() const { return typeInfo; }
320
Mathias Agopian48d819a2009-09-10 19:41:18 -0700321 // lcblk is (almost) only accessed from the main SF thread, in the places
322 // where it's not, a reference to Client must be held
323 SharedBufferServer* lcblk;
324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700326 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329
Mathias Agopian48d819a2009-09-10 19:41:18 -0700330 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 inline int32_t clientIndex() const { return mIndex; }
334 int32_t serverIndex() const;
335
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700337 sp<Surface> getSurface();
338 virtual sp<Surface> createSurface() const;
339
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700340 virtual void onRemoved();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341
Mathias Agopian285dbde2010-03-01 16:09:43 -0800342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 class Surface : public BnSurface
344 {
345 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700346 int32_t getToken() const { return mToken; }
347 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700348
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700349 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700350 Surface(const sp<SurfaceFlinger>& flinger,
351 SurfaceID id, int identity,
352 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700353 virtual ~Surface();
354 virtual status_t onTransact(uint32_t code, const Parcel& data,
355 Parcel* reply, uint32_t flags);
356 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357
358 private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700359 virtual sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700360 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
361 virtual void postBuffer(ssize_t offset);
362 virtual void unregisterBuffers();
363 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800364 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700365
Mathias Agopian9a112062009-04-17 19:36:26 -0700366 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700367 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700368 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700369 int32_t mToken;
370 int32_t mIdentity;
371 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 };
373
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700374 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700376private:
377 int32_t mIndex;
378 mutable Mutex mLock;
379 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700380 // only read
381 const uint32_t mIdentity;
382 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383};
384
385// ---------------------------------------------------------------------------
386
387}; // namespace android
388
389#endif // ANDROID_LAYER_BASE_H