blob: a78424ea5441a792a0727becdb28eb1ddf31d69d [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;
Mathias Agopiand606de62010-05-10 20:06:11 -070049class Texture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
51// ---------------------------------------------------------------------------
52
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070056 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 DisplayID dpy;
59 mutable bool contentDirty;
60 Region visibleRegionScreen;
61 Region transparentRegionScreen;
62 Region coveredRegionScreen;
63
64 struct State {
65 uint32_t w;
66 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070067 uint32_t requested_w;
68 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 uint32_t z;
70 uint8_t alpha;
71 uint8_t flags;
72 uint8_t reserved[2];
73 int32_t sequence; // changes when visible regions can change
74 uint32_t tint;
75 Transform transform;
76 Region transparentRegion;
77 };
78
Mathias Agopiand1296592010-03-09 19:17:47 -080079 void setName(const String8& name);
80 String8 getName() const;
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 // modify current state
83 bool setPosition(int32_t x, int32_t y);
84 bool setLayer(uint32_t z);
85 bool setSize(uint32_t w, uint32_t h);
86 bool setAlpha(uint8_t alpha);
87 bool setMatrix(const layer_state_t::matrix22_t& matrix);
88 bool setTransparentRegionHint(const Region& opaque);
89 bool setFlags(uint8_t flags, uint8_t mask);
90
Mathias Agopianba6be542009-09-29 22:32:36 -070091 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 bool requestTransaction();
93 void forceVisibilityTransaction();
94
95 uint32_t getTransactionFlags(uint32_t flags);
96 uint32_t setTransactionFlags(uint32_t flags);
97
98 Rect visibleBounds() const;
99 void drawRegion(const Region& reg) const;
100
101 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800102
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700103 virtual const char* getTypeId() const { return "LayerBase"; }
104 virtual ssize_t serverIndex() const { return -1; }
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 /**
107 * draw - performs some global clipping optimizations
108 * and calls onDraw().
109 * Typically this method is not overridden, instead implement onDraw()
110 * to perform the actual drawing.
111 */
112 virtual void draw(const Region& clip) const;
113
114 /**
115 * onDraw - draws the surface.
116 */
117 virtual void onDraw(const Region& clip) const = 0;
118
119 /**
120 * initStates - called just after construction
121 */
122 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
123
124 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 * doTransaction - process the transaction. This is a good place to figure
126 * out which attributes of the surface have changed.
127 */
128 virtual uint32_t doTransaction(uint32_t transactionFlags);
129
130 /**
131 * setVisibleRegion - called to set the new visible region. This gives
132 * a chance to update the new visible region or record the fact it changed.
133 */
134 virtual void setVisibleRegion(const Region& visibleRegion);
135
136 /**
137 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700138 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 * (transparently or not) by another surface.
140 */
141 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 * validateVisibility - cache a bunch of things
145 */
146 virtual void validateVisibility(const Transform& globalTransform);
147
148 /**
149 * lockPageFlip - called each time the screen is redrawn and returns whether
150 * the visible regions need to be recomputed (this is a fairly heavy
151 * operation, so this should be set only if needed). Typically this is used
152 * to figure out if the content or size of a surface has changed.
153 */
154 virtual void lockPageFlip(bool& recomputeVisibleRegions);
155
156 /**
157 * unlockPageFlip - called each time the screen is redrawn. updates the
158 * final dirty region wrt the planeTransform.
159 * At this point, all visible regions, surface position and size, etc... are
160 * correct.
161 */
162 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
163
164 /**
165 * finishPageFlip - called after all surfaces have drawn.
166 */
167 virtual void finishPageFlip();
168
169 /**
170 * needsBlending - true if this surface needs blending
171 */
172 virtual bool needsBlending() const { return false; }
173
174 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700175 * needsDithering - true if this surface needs dithering
176 */
177 virtual bool needsDithering() const { return false; }
178
179 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700180 * needsLinearFiltering - true if this surface needs filtering
181 */
182 virtual bool needsFiltering() const { return mNeedsFiltering; }
183
184 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700186 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 */
188 virtual bool isSecure() const { return false; }
189
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700190 /** Called from the main thread, when the surface is removed from the
191 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700192 virtual status_t ditch() { return NO_ERROR; }
193
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700194 /** called with the state lock when the surface is removed from the
195 * current list */
196 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700197
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700198 /** always call base class first */
199 virtual void dump(String8& result, char* scratch, size_t size) const;
200
201
Mathias Agopian9a112062009-04-17 19:36:26 -0700202 enum { // flags for doTransaction()
203 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700204 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205
206
207 inline const State& drawingState() const { return mDrawingState; }
208 inline const State& currentState() const { return mCurrentState; }
209 inline State& currentState() { return mCurrentState; }
210
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 static int compareCurrentStateZ(
212 sp<LayerBase> const * layerA,
213 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 return layerA[0]->currentState().z - layerB[0]->currentState().z;
215 }
216
217 int32_t getOrientation() const { return mOrientation; }
218 int tx() const { return mLeft; }
219 int ty() const { return mTop; }
220
221protected:
222 const GraphicPlane& graphicPlane(int dpy) const;
223 GraphicPlane& graphicPlane(int dpy);
224
Mathias Agopian010fccb2010-05-26 22:26:12 -0700225 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
226 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700228 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700229
Mathias Agopian9a112062009-04-17 19:36:26 -0700230 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 uint32_t mFlags;
232
233 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700234 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700236 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 int mLeft;
239 int mTop;
240
241 // these are protected by an external lock
242 State mCurrentState;
243 State mDrawingState;
244 volatile int32_t mTransactionFlags;
245
246 // don't change, don't need a lock
247 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800248 String8 mName;
249 mutable bool mDebug;
250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 // atomic
253 volatile int32_t mInvalidate;
254
255
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700256protected:
257 virtual ~LayerBase();
258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260 LayerBase(const LayerBase& rhs);
261 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262};
263
264
265// ---------------------------------------------------------------------------
266
267class LayerBaseClient : public LayerBase
268{
269public:
270 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271
272 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700273 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
Mathias Agopian48d819a2009-09-10 19:41:18 -0700277 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 inline int32_t clientIndex() const { return mIndex; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700282 sp<Surface> getSurface();
283 virtual sp<Surface> createSurface() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700284 virtual ssize_t serverIndex() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700285 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 class Surface : public BnSurface
288 {
289 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700290 int32_t getToken() const { return mToken; }
291 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700292
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700294 Surface(const sp<SurfaceFlinger>& flinger,
295 SurfaceID id, int identity,
296 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 virtual ~Surface();
298 virtual status_t onTransact(uint32_t code, const Parcel& data,
299 Parcel* reply, uint32_t flags);
300 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301
302 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700303 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
304 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700305 virtual status_t setBufferCount(int bufferCount);
306
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
308 virtual void postBuffer(ssize_t offset);
309 virtual void unregisterBuffers();
310 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800311 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312
Mathias Agopian9a112062009-04-17 19:36:26 -0700313 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700315 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316 int32_t mToken;
317 int32_t mIdentity;
318 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 };
320
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700321 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700323protected:
324 virtual void dump(String8& result, char* scratch, size_t size) const;
325
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326private:
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700327 int32_t mIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 mutable Mutex mLock;
329 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700330 // only read
331 const uint32_t mIdentity;
332 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333};
334
335// ---------------------------------------------------------------------------
336
337}; // namespace android
338
339#endif // ANDROID_LAYER_BASE_H