blob: a3d36444bc16bcf22fc7a465d0e32bc22456f452 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Mathias Agopian7e27f052010-05-28 14:22:23 -070031#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080032#include <private/surfaceflinger/LayerState.h>
33
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <pixelflinger/pixelflinger.h>
35
Mathias Agopiana350ff92010-08-10 17:14:02 -070036#include <hardware/hwcomposer.h>
37
Mathias Agopian5ec99b52010-12-07 23:41:55 -080038#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include "Transform.h"
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class Client;
Mathias Agopiana67932f2011-04-20 14:20:59 -070046class DisplayHardware;
Mathias Agopian3330b202009-10-05 17:07:12 -070047class GraphicBuffer;
48class GraphicPlane;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070049class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070050class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52// ---------------------------------------------------------------------------
53
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070056 static int32_t sSequence;
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070059 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 DisplayID dpy;
62 mutable bool contentDirty;
63 Region visibleRegionScreen;
64 Region transparentRegionScreen;
65 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070066 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
68 struct State {
69 uint32_t w;
70 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070071 uint32_t requested_w;
72 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 uint32_t z;
74 uint8_t alpha;
75 uint8_t flags;
76 uint8_t reserved[2];
77 int32_t sequence; // changes when visible regions can change
78 uint32_t tint;
79 Transform transform;
80 Region transparentRegion;
81 };
82
Mathias Agopiand1296592010-03-09 19:17:47 -080083 void setName(const String8& name);
84 String8 getName() const;
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 // modify current state
87 bool setPosition(int32_t x, int32_t y);
88 bool setLayer(uint32_t z);
89 bool setSize(uint32_t w, uint32_t h);
90 bool setAlpha(uint8_t alpha);
91 bool setMatrix(const layer_state_t::matrix22_t& matrix);
92 bool setTransparentRegionHint(const Region& opaque);
93 bool setFlags(uint8_t flags, uint8_t mask);
94
Mathias Agopianba6be542009-09-29 22:32:36 -070095 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 bool requestTransaction();
97 void forceVisibilityTransaction();
98
99 uint32_t getTransactionFlags(uint32_t flags);
100 uint32_t setTransactionFlags(uint32_t flags);
101
102 Rect visibleBounds() const;
103 void drawRegion(const Region& reg) const;
104
105 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800106
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700107 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
108
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700109 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700110
Mathias Agopiana350ff92010-08-10 17:14:02 -0700111 virtual void setGeometry(hwc_layer_t* hwcl);
112
113 virtual void setPerFrameData(hwc_layer_t* hwcl);
114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 /**
116 * draw - performs some global clipping optimizations
117 * and calls onDraw().
118 * Typically this method is not overridden, instead implement onDraw()
119 * to perform the actual drawing.
120 */
121 virtual void draw(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700122 virtual void drawForSreenShot();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
124 /**
125 * onDraw - draws the surface.
126 */
127 virtual void onDraw(const Region& clip) const = 0;
128
129 /**
130 * initStates - called just after construction
131 */
132 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
133
134 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 * doTransaction - process the transaction. This is a good place to figure
136 * out which attributes of the surface have changed.
137 */
138 virtual uint32_t doTransaction(uint32_t transactionFlags);
139
140 /**
141 * setVisibleRegion - called to set the new visible region. This gives
142 * a chance to update the new visible region or record the fact it changed.
143 */
144 virtual void setVisibleRegion(const Region& visibleRegion);
145
146 /**
147 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700148 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 * (transparently or not) by another surface.
150 */
151 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 * validateVisibility - cache a bunch of things
155 */
156 virtual void validateVisibility(const Transform& globalTransform);
157
158 /**
159 * lockPageFlip - called each time the screen is redrawn and returns whether
160 * the visible regions need to be recomputed (this is a fairly heavy
161 * operation, so this should be set only if needed). Typically this is used
162 * to figure out if the content or size of a surface has changed.
163 */
164 virtual void lockPageFlip(bool& recomputeVisibleRegions);
165
166 /**
167 * unlockPageFlip - called each time the screen is redrawn. updates the
168 * final dirty region wrt the planeTransform.
169 * At this point, all visible regions, surface position and size, etc... are
170 * correct.
171 */
172 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
173
174 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700175 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700177 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178
179 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700180 * needsDithering - true if this surface needs dithering
181 */
182 virtual bool needsDithering() const { return false; }
183
184 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700185 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700186 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700187 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700188
189 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 * 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
Glenn Kasten16f04532011-01-19 15:27:27 -0800195 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800196 * isProtected - true if the layer may contain protected content in the
197 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800198 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800199 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800200
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700201 /** called with the state lock when the surface is removed from the
202 * current list */
203 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700204
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700205 /** always call base class first */
206 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800207 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700208
209
Mathias Agopian9a112062009-04-17 19:36:26 -0700210 enum { // flags for doTransaction()
211 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700212 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
214
215 inline const State& drawingState() const { return mDrawingState; }
216 inline const State& currentState() const { return mCurrentState; }
217 inline State& currentState() { return mCurrentState; }
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 int32_t getOrientation() const { return mOrientation; }
220 int tx() const { return mLeft; }
221 int ty() const { return mTop; }
222
223protected:
224 const GraphicPlane& graphicPlane(int dpy) const;
225 GraphicPlane& graphicPlane(int dpy);
226
Mathias Agopian010fccb2010-05-26 22:26:12 -0700227 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
228 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 void clearWithOpenGL(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700230 void drawWithOpenGL(const Region& clip) const;
231
232 void setFiltering(bool filtering);
233 bool getFiltering() const;
Mathias Agopianb661d662010-08-19 17:01:19 -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
Mathias Agopiana67932f2011-04-20 14:20:59 -0700238private:
239 // accessed only in the main thread
240 // Whether filtering is forced on or not
241 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 // cached during validateVisibility()
Mathias Agopiana67932f2011-04-20 14:20:59 -0700244 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700245 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700246
247protected:
248 // cached during validateVisibility()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700250 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 int mLeft;
253 int mTop;
254
255 // these are protected by an external lock
256 State mCurrentState;
257 State mDrawingState;
258 volatile int32_t mTransactionFlags;
259
260 // don't change, don't need a lock
261 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800262 String8 mName;
263 mutable bool mDebug;
264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 // atomic
267 volatile int32_t mInvalidate;
268
269
Mathias Agopianca4d3602011-05-19 15:38:14 -0700270public:
271 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 virtual ~LayerBase();
273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276};
277
278
279// ---------------------------------------------------------------------------
280
281class LayerBaseClient : public LayerBase
282{
283public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700284 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
285 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
Mathias Agopiana67932f2011-04-20 14:20:59 -0700287 virtual ~LayerBaseClient();
288
289 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800290 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700291 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700292
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700293 virtual sp<LayerBaseClient> getLayerBaseClient() const {
294 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700295
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700296 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800297
Mathias Agopian96f08192010-06-02 23:28:45 -0700298 uint32_t getIdentity() const { return mIdentity; }
299
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700300protected:
301 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800302 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700303
Mathias Agopiana67932f2011-04-20 14:20:59 -0700304 class LayerCleaner {
305 sp<SurfaceFlinger> mFlinger;
306 wp<LayerBaseClient> mLayer;
307 protected:
308 ~LayerCleaner();
309 public:
310 LayerCleaner(const sp<SurfaceFlinger>& flinger,
311 const sp<LayerBaseClient>& layer);
312 };
313
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700315 virtual sp<ISurface> createSurface();
316
Mathias Agopian96f08192010-06-02 23:28:45 -0700317 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800318 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800319 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700320 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700321 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700322 const uint32_t mIdentity;
323 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324};
325
326// ---------------------------------------------------------------------------
327
328}; // namespace android
329
330#endif // ANDROID_LAYER_BASE_H