blob: efa4f8c2109a08054f45cbf274c49cf1607ceb2b [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 Agopian9779b2212009-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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264
Mathias Agopian999543b2009-06-23 18:08:22 -0700265
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700266 sp<SurfaceFlinger> mFlinger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 uint32_t mFlags;
268
269 // cached during validateVisibility()
270 bool mTransformed;
Mathias Agopian44cac132009-09-23 18:34:53 -0700271 bool mUseLinearFiltering;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 int32_t mOrientation;
273 GLfixed mVertices[4][2];
274 Rect mTransformedBounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 int mLeft;
276 int mTop;
277
278 // these are protected by an external lock
279 State mCurrentState;
280 State mDrawingState;
281 volatile int32_t mTransactionFlags;
282
283 // don't change, don't need a lock
284 bool mPremultipliedAlpha;
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 // atomic
287 volatile int32_t mInvalidate;
288
289
Mathias Agopian1473f462009-04-10 14:24:30 -0700290protected:
291 virtual ~LayerBase();
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293private:
Mathias Agopian1473f462009-04-10 14:24:30 -0700294 LayerBase(const LayerBase& rhs);
295 void validateTexture(GLint textureName) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296};
297
298
299// ---------------------------------------------------------------------------
300
301class LayerBaseClient : public LayerBase
302{
303public:
304 class Surface;
305 static const uint32_t typeInfo;
306 static const char* const typeID;
307 virtual char const* getTypeID() const { return typeID; }
308 virtual uint32_t getTypeInfo() const { return typeInfo; }
309
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700310 // lcblk is (almost) only accessed from the main SF thread, in the places
311 // where it's not, a reference to Client must be held
312 SharedBufferServer* lcblk;
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700315 const sp<Client>& client, int32_t i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 virtual ~LayerBaseClient();
Mathias Agopian1473f462009-04-10 14:24:30 -0700317 virtual void onFirstRef();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700319 const wp<Client> client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
Mathias Agopian1473f462009-04-10 14:24:30 -0700321 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 inline int32_t clientIndex() const { return mIndex; }
323 int32_t serverIndex() const;
324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
Mathias Agopian1473f462009-04-10 14:24:30 -0700326 sp<Surface> getSurface();
327 virtual sp<Surface> createSurface() const;
328
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700329 virtual void onRemoved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
331 class Surface : public BnSurface
332 {
333 public:
Mathias Agopian18b6b492009-08-19 17:46:26 -0700334 int32_t getToken() const { return mToken; }
335 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian1473f462009-04-10 14:24:30 -0700336
Mathias Agopian1473f462009-04-10 14:24:30 -0700337 protected:
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700338 Surface(const sp<SurfaceFlinger>& flinger,
339 SurfaceID id, int identity,
340 const sp<LayerBaseClient>& owner);
Mathias Agopian1473f462009-04-10 14:24:30 -0700341 virtual ~Surface();
342 virtual status_t onTransact(uint32_t code, const Parcel& data,
343 Parcel* reply, uint32_t flags);
344 sp<LayerBaseClient> getOwner() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345
346 private:
Mathias Agopian6950e422009-10-05 17:07:12 -0700347 virtual sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700348 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
349 virtual void postBuffer(ssize_t offset);
350 virtual void unregisterBuffers();
351 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
352 int32_t format);
353
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700354 protected:
Mathias Agopian1473f462009-04-10 14:24:30 -0700355 friend class LayerBaseClient;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700356 sp<SurfaceFlinger> mFlinger;
Mathias Agopian1473f462009-04-10 14:24:30 -0700357 int32_t mToken;
358 int32_t mIdentity;
359 wp<LayerBaseClient> mOwner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 };
361
Mathias Agopian1473f462009-04-10 14:24:30 -0700362 friend class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
Mathias Agopian1473f462009-04-10 14:24:30 -0700364private:
365 int32_t mIndex;
366 mutable Mutex mLock;
367 mutable wp<Surface> mClientSurface;
Mathias Agopianc6603952009-06-23 20:06:46 -0700368 // only read
369 const uint32_t mIdentity;
370 static int32_t sIdentity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371};
372
373// ---------------------------------------------------------------------------
374
375}; // namespace android
376
377#endif // ANDROID_LAYER_BASE_H