blob: ed07b3f933603e537adaf1ad191ce35c909181c0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopian999543b2009-06-23 18:08:22 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
Mathias Agopian9779b222009-09-07 16:32:45 -070026#include <private/ui/SharedBufferStack.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <private/ui/LayerState.h>
28
Mathias Agopian1473f462009-04-10 14:24:30 -070029#include <utils/RefBase.h>
30
The Android Open Source Project9066cfe2009-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042class DisplayHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043class Client;
Mathias Agopian6950e422009-10-05 17:07:12 -070044class GraphicBuffer;
45class GraphicPlane;
46class SurfaceFlinger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48// ---------------------------------------------------------------------------
49
Mathias Agopian1473f462009-04-10 14:24:30 -070050class LayerBase : public RefBase
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080079 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Project9066cfe2009-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;
Mathias Agopiane1b6f242009-09-29 22:39:22 -070090 uint32_t requested_w;
91 uint32_t requested_h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 uint32_t z;
93 uint8_t alpha;
94 uint8_t flags;
95 uint8_t reserved[2];
96 int32_t sequence; // changes when visible regions can change
97 uint32_t tint;
98 Transform transform;
99 Region transparentRegion;
100 };
101
102 // modify current state
103 bool setPosition(int32_t x, int32_t y);
104 bool setLayer(uint32_t z);
105 bool setSize(uint32_t w, uint32_t h);
106 bool setAlpha(uint8_t alpha);
107 bool setMatrix(const layer_state_t::matrix22_t& matrix);
108 bool setTransparentRegionHint(const Region& opaque);
109 bool setFlags(uint8_t flags, uint8_t mask);
110
Mathias Agopian88516172009-09-29 22:32:36 -0700111 void commitTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 bool requestTransaction();
113 void forceVisibilityTransaction();
114
115 uint32_t getTransactionFlags(uint32_t flags);
116 uint32_t setTransactionFlags(uint32_t flags);
117
118 Rect visibleBounds() const;
119 void drawRegion(const Region& reg) const;
120
121 void invalidate();
122
123 /**
124 * draw - performs some global clipping optimizations
125 * and calls onDraw().
126 * Typically this method is not overridden, instead implement onDraw()
127 * to perform the actual drawing.
128 */
129 virtual void draw(const Region& clip) const;
130
131 /**
132 * onDraw - draws the surface.
133 */
134 virtual void onDraw(const Region& clip) const = 0;
135
136 /**
137 * initStates - called just after construction
138 */
139 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
140
141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * 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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * validateVisibility - cache a bunch of things
162 */
163 virtual void validateVisibility(const Transform& globalTransform);
164
165 /**
166 * lockPageFlip - called each time the screen is redrawn and returns whether
167 * the visible regions need to be recomputed (this is a fairly heavy
168 * operation, so this should be set only if needed). Typically this is used
169 * to figure out if the content or size of a surface has changed.
170 */
171 virtual void lockPageFlip(bool& recomputeVisibleRegions);
172
173 /**
174 * unlockPageFlip - called each time the screen is redrawn. updates the
175 * final dirty region wrt the planeTransform.
176 * At this point, all visible regions, surface position and size, etc... are
177 * correct.
178 */
179 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
180
181 /**
182 * finishPageFlip - called after all surfaces have drawn.
183 */
184 virtual void finishPageFlip();
185
186 /**
187 * needsBlending - true if this surface needs blending
188 */
189 virtual bool needsBlending() const { return false; }
190
191 /**
Mathias Agopiancc934762009-09-23 19:16:27 -0700192 * needsDithering - true if this surface needs dithering
193 */
194 virtual bool needsDithering() const { return false; }
195
196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * transformed -- true is this surface needs a to be transformed
198 */
199 virtual bool transformed() const { return mTransformed; }
200
201 /**
202 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700203 * screenshots or VNC servers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 */
205 virtual bool isSecure() const { return false; }
206
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700207 /** Called from the main thread, when the surface is removed from the
208 * draw list */
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700209 virtual status_t ditch() { return NO_ERROR; }
210
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700211 /** called with the state lock when the surface is removed from the
212 * current list */
213 virtual void onRemoved() { };
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700214
215
216 enum { // flags for doTransaction()
217 eVisibleRegion = 0x00000002,
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700218 };
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700225 static int compareCurrentStateZ(
226 sp<LayerBase> const * layerA,
227 sp<LayerBase> const * layerB) {
The Android Open Source Project9066cfe2009-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
Mathias Agopian999543b2009-06-23 18:08:22 -0700241 struct Texture {
242 Texture() : name(-1U), width(0), height(0),
Mathias Agopian6950e422009-10-05 17:07:12 -0700243 image(EGL_NO_IMAGE_KHR), transform(0),
244 NPOTAdjust(false), dirty(true) { }
Mathias Agopian999543b2009-06-23 18:08:22 -0700245 GLuint name;
246 GLuint width;
247 GLuint height;
Mathias Agopian6950e422009-10-05 17:07:12 -0700248 GLuint potWidth;
249 GLuint potHeight;
250 GLfloat wScale;
251 GLfloat hScale;
Mathias Agopian999543b2009-06-23 18:08:22 -0700252 EGLImageKHR image;
253 uint32_t transform;
Mathias Agopian6950e422009-10-05 17:07:12 -0700254 bool NPOTAdjust;
Mathias Agopian999543b2009-06-23 18:08:22 -0700255 bool dirty;
256 };
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700257
258 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
259 GLclampx b, GLclampx alpha) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian999543b2009-06-23 18:08:22 -0700261 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian6950e422009-10-05 17:07:12 -0700262 void loadTexture(Texture* texture,
Mathias Agopian999543b2009-06-23 18:08:22 -0700263 const Region& dirty, const GGLSurface& t) const;
Mathias Agopian9042b452009-10-26 20:12:37 -0700264 status_t initializeEglImage(
265 const sp<GraphicBuffer>& buffer, Texture* texture);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Mathias Agopian999543b2009-06-23 18:08:22 -0700267
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700268 sp<SurfaceFlinger> mFlinger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 uint32_t mFlags;
270
271 // cached during validateVisibility()
272 bool mTransformed;
Mathias Agopian44cac132009-09-23 18:34:53 -0700273 bool mUseLinearFiltering;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 int32_t mOrientation;
275 GLfixed mVertices[4][2];
276 Rect mTransformedBounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 int mLeft;
278 int mTop;
279
280 // these are protected by an external lock
281 State mCurrentState;
282 State mDrawingState;
283 volatile int32_t mTransactionFlags;
284
285 // don't change, don't need a lock
286 bool mPremultipliedAlpha;
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 // atomic
289 volatile int32_t mInvalidate;
290
291
Mathias Agopian1473f462009-04-10 14:24:30 -0700292protected:
293 virtual ~LayerBase();
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295private:
Mathias Agopian1473f462009-04-10 14:24:30 -0700296 LayerBase(const LayerBase& rhs);
297 void validateTexture(GLint textureName) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298};
299
300
301// ---------------------------------------------------------------------------
302
303class LayerBaseClient : public LayerBase
304{
305public:
306 class Surface;
307 static const uint32_t typeInfo;
308 static const char* const typeID;
309 virtual char const* getTypeID() const { return typeID; }
310 virtual uint32_t getTypeInfo() const { return typeInfo; }
311
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700312 // lcblk is (almost) only accessed from the main SF thread, in the places
313 // where it's not, a reference to Client must be held
314 SharedBufferServer* lcblk;
315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700317 const sp<Client>& client, int32_t i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 virtual ~LayerBaseClient();
Mathias Agopian1473f462009-04-10 14:24:30 -0700319 virtual void onFirstRef();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700321 const wp<Client> client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
Mathias Agopian1473f462009-04-10 14:24:30 -0700323 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 inline int32_t clientIndex() const { return mIndex; }
325 int32_t serverIndex() const;
326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
Mathias Agopian1473f462009-04-10 14:24:30 -0700328 sp<Surface> getSurface();
329 virtual sp<Surface> createSurface() const;
330
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700331 virtual void onRemoved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
333 class Surface : public BnSurface
334 {
335 public:
Mathias Agopian18b6b492009-08-19 17:46:26 -0700336 int32_t getToken() const { return mToken; }
337 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian1473f462009-04-10 14:24:30 -0700338
Mathias Agopian1473f462009-04-10 14:24:30 -0700339 protected:
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700340 Surface(const sp<SurfaceFlinger>& flinger,
341 SurfaceID id, int identity,
342 const sp<LayerBaseClient>& owner);
Mathias Agopian1473f462009-04-10 14:24:30 -0700343 virtual ~Surface();
344 virtual status_t onTransact(uint32_t code, const Parcel& data,
345 Parcel* reply, uint32_t flags);
346 sp<LayerBaseClient> getOwner() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
348 private:
Mathias Agopian6950e422009-10-05 17:07:12 -0700349 virtual sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700350 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
351 virtual void postBuffer(ssize_t offset);
352 virtual void unregisterBuffers();
353 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
354 int32_t format);
355
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700356 protected:
Mathias Agopian1473f462009-04-10 14:24:30 -0700357 friend class LayerBaseClient;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700358 sp<SurfaceFlinger> mFlinger;
Mathias Agopian1473f462009-04-10 14:24:30 -0700359 int32_t mToken;
360 int32_t mIdentity;
361 wp<LayerBaseClient> mOwner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 };
363
Mathias Agopian1473f462009-04-10 14:24:30 -0700364 friend class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
Mathias Agopian1473f462009-04-10 14:24:30 -0700366private:
367 int32_t mIndex;
368 mutable Mutex mLock;
369 mutable wp<Surface> mClientSurface;
Mathias Agopianc6603952009-06-23 20:06:46 -0700370 // only read
371 const uint32_t mIdentity;
372 static int32_t sIdentity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373};
374
375// ---------------------------------------------------------------------------
376
377}; // namespace android
378
379#endif // ANDROID_LAYER_BASE_H