blob: 6d7859ac2123626d3446e4910cc98359e8a478cf [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 * transformed -- true is this surface needs a to be transformed
186 */
187 virtual bool transformed() const { return mTransformed; }
188
189 /**
190 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700191 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 */
193 virtual bool isSecure() const { return false; }
194
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700195 /** Called from the main thread, when the surface is removed from the
196 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700197 virtual status_t ditch() { return NO_ERROR; }
198
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700199 /** called with the state lock when the surface is removed from the
200 * current list */
201 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700202
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700203 /** always call base class first */
204 virtual void dump(String8& result, char* scratch, size_t size) const;
205
206
Mathias Agopian9a112062009-04-17 19:36:26 -0700207 enum { // flags for doTransaction()
208 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700209 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210
211
212 inline const State& drawingState() const { return mDrawingState; }
213 inline const State& currentState() const { return mCurrentState; }
214 inline State& currentState() { return mCurrentState; }
215
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216 static int compareCurrentStateZ(
217 sp<LayerBase> const * layerA,
218 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 return layerA[0]->currentState().z - layerB[0]->currentState().z;
220 }
221
222 int32_t getOrientation() const { return mOrientation; }
223 int tx() const { return mLeft; }
224 int ty() const { return mTop; }
225
226protected:
227 const GraphicPlane& graphicPlane(int dpy) const;
228 GraphicPlane& graphicPlane(int dpy);
229
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700230 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
231 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700233 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700234
Mathias Agopian9a112062009-04-17 19:36:26 -0700235 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 uint32_t mFlags;
237
238 // cached during validateVisibility()
239 bool mTransformed;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700240 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700242 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 int mLeft;
245 int mTop;
246
247 // these are protected by an external lock
248 State mCurrentState;
249 State mDrawingState;
250 volatile int32_t mTransactionFlags;
251
252 // don't change, don't need a lock
253 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800254 String8 mName;
255 mutable bool mDebug;
256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 // atomic
259 volatile int32_t mInvalidate;
260
261
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700262protected:
263 virtual ~LayerBase();
264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700266 LayerBase(const LayerBase& rhs);
267 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268};
269
270
271// ---------------------------------------------------------------------------
272
273class LayerBaseClient : public LayerBase
274{
275public:
276 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277
278 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700279 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
Mathias Agopian48d819a2009-09-10 19:41:18 -0700283 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286 inline int32_t clientIndex() const { return mIndex; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700288 sp<Surface> getSurface();
289 virtual sp<Surface> createSurface() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700290 virtual ssize_t serverIndex() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700291 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800292
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 class Surface : public BnSurface
294 {
295 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700296 int32_t getToken() const { return mToken; }
297 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700299 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700300 Surface(const sp<SurfaceFlinger>& flinger,
301 SurfaceID id, int identity,
302 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 virtual ~Surface();
304 virtual status_t onTransact(uint32_t code, const Parcel& data,
305 Parcel* reply, uint32_t flags);
306 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
308 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700309 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
310 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700311 virtual status_t setBufferCount(int bufferCount);
312
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
314 virtual void postBuffer(ssize_t offset);
315 virtual void unregisterBuffers();
316 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800317 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318
Mathias Agopian9a112062009-04-17 19:36:26 -0700319 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700320 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700321 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 int32_t mToken;
323 int32_t mIdentity;
324 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 };
326
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700329protected:
330 virtual void dump(String8& result, char* scratch, size_t size) const;
331
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332private:
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700333 int32_t mIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700334 mutable Mutex mLock;
335 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700336 // only read
337 const uint32_t mIdentity;
338 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339};
340
341// ---------------------------------------------------------------------------
342
343}; // namespace android
344
345#endif // ANDROID_LAYER_BASE_H