blob: cd6efdd1fc8bc45a0c630e98323777a04c2f3467 [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 Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080032
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
Mathias Agopiana350ff92010-08-10 17:14:02 -070035#include <hardware/hwcomposer.h>
36
Mathias Agopian5ec99b52010-12-07 23:41:55 -080037#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include "Transform.h"
39
40namespace android {
41
42// ---------------------------------------------------------------------------
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044class Client;
Mathias Agopiana67932f2011-04-20 14:20:59 -070045class DisplayHardware;
Mathias Agopian3330b202009-10-05 17:07:12 -070046class GraphicBuffer;
47class GraphicPlane;
Mathias Agopian670a8992011-09-20 15:13:14 -070048class Layer;
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
Jamie Gennisa249f2d2011-09-16 17:31:54 -070083 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -080084 String8 getName() const;
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -070087 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 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
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700105 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
Mathias Agopian670a8992011-09-20 15:13:14 -0700106 virtual sp<Layer> getLayer() const { return 0; }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700107
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700108 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700109
Mathias Agopiana350ff92010-08-10 17:14:02 -0700110 virtual void setGeometry(hwc_layer_t* hwcl);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700111 virtual void setPerFrameData(hwc_layer_t* hwcl);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700112 void setOverlay(bool inOverlay);
113 bool isOverlay() const;
114
Mathias Agopiana350ff92010-08-10 17:14:02 -0700115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 /**
117 * draw - performs some global clipping optimizations
118 * and calls onDraw().
119 * Typically this method is not overridden, instead implement onDraw()
120 * to perform the actual drawing.
121 */
122 virtual void draw(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700123 virtual void drawForSreenShot();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124
125 /**
126 * onDraw - draws the surface.
127 */
128 virtual void onDraw(const Region& clip) const = 0;
129
130 /**
131 * initStates - called just after construction
132 */
133 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
134
135 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 * doTransaction - process the transaction. This is a good place to figure
137 * out which attributes of the surface have changed.
138 */
139 virtual uint32_t doTransaction(uint32_t transactionFlags);
140
141 /**
142 * setVisibleRegion - called to set the new visible region. This gives
143 * a chance to update the new visible region or record the fact it changed.
144 */
145 virtual void setVisibleRegion(const Region& visibleRegion);
146
147 /**
148 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700149 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 * (transparently or not) by another surface.
151 */
152 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 * validateVisibility - cache a bunch of things
156 */
157 virtual void validateVisibility(const Transform& globalTransform);
158
159 /**
160 * lockPageFlip - called each time the screen is redrawn and returns whether
161 * the visible regions need to be recomputed (this is a fairly heavy
162 * operation, so this should be set only if needed). Typically this is used
163 * to figure out if the content or size of a surface has changed.
164 */
165 virtual void lockPageFlip(bool& recomputeVisibleRegions);
166
167 /**
168 * unlockPageFlip - called each time the screen is redrawn. updates the
169 * final dirty region wrt the planeTransform.
170 * At this point, all visible regions, surface position and size, etc... are
171 * correct.
172 */
173 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
174
175 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700176 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700178 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179
180 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700181 * needsDithering - true if this surface needs dithering
182 */
183 virtual bool needsDithering() const { return false; }
184
185 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700186 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700187 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700188 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700189
190 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700192 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 */
194 virtual bool isSecure() const { return false; }
195
Glenn Kasten16f04532011-01-19 15:27:27 -0800196 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800197 * isProtected - true if the layer may contain protected content in the
198 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800199 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800200 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800201
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700202 /** called with the state lock when the surface is removed from the
203 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800204 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800205
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800206 /** called after page-flip
207 */
208 virtual void onLayerDisplayed() { }
209
210 /** called before composition.
211 * returns true if the layer has pending updates.
212 */
213 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800214
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700215 /** always call base class first */
216 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800217 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800218 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800219 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700220
221
Mathias Agopian9a112062009-04-17 19:36:26 -0700222 enum { // flags for doTransaction()
223 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700224 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225
226
227 inline const State& drawingState() const { return mDrawingState; }
228 inline const State& currentState() const { return mCurrentState; }
229 inline State& currentState() { return mCurrentState; }
230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 int32_t getOrientation() const { return mOrientation; }
Jamie Gennis8d91b422011-09-23 15:54:34 -0700232 int32_t getPlaneOrientation() const { return mPlaneOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
234protected:
235 const GraphicPlane& graphicPlane(int dpy) const;
236 GraphicPlane& graphicPlane(int dpy);
237
Mathias Agopian010fccb2010-05-26 22:26:12 -0700238 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
239 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 void clearWithOpenGL(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700241 void drawWithOpenGL(const Region& clip) const;
242
243 void setFiltering(bool filtering);
244 bool getFiltering() const;
Mathias Agopianb661d662010-08-19 17:01:19 -0700245
Mathias Agopian9a112062009-04-17 19:36:26 -0700246 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 uint32_t mFlags;
248
Mathias Agopiana67932f2011-04-20 14:20:59 -0700249private:
250 // accessed only in the main thread
251 // Whether filtering is forced on or not
252 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 // cached during validateVisibility()
Mathias Agopiana67932f2011-04-20 14:20:59 -0700255 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700256 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700257
Mathias Agopianf384cc32011-09-08 18:31:55 -0700258 // this layer is currently handled by the hwc. this is
259 // updated at composition time, always frmo the composition
260 // thread.
261 bool mInOverlay;
262
Mathias Agopiana67932f2011-04-20 14:20:59 -0700263protected:
264 // cached during validateVisibility()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 int32_t mOrientation;
Jamie Gennis8d91b422011-09-23 15:54:34 -0700266 int32_t mPlaneOrientation;
Mathias Agopiand992db32011-08-18 18:31:00 -0700267 Transform mTransform;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700268 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270
271 // these are protected by an external lock
272 State mCurrentState;
273 State mDrawingState;
274 volatile int32_t mTransactionFlags;
275
276 // don't change, don't need a lock
277 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800278 String8 mName;
279 mutable bool mDebug;
280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
Mathias Agopianca4d3602011-05-19 15:38:14 -0700282public:
283 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700284 virtual ~LayerBase();
285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700287 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288};
289
290
291// ---------------------------------------------------------------------------
292
293class LayerBaseClient : public LayerBase
294{
295public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700296 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
297 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
Mathias Agopiana67932f2011-04-20 14:20:59 -0700299 virtual ~LayerBaseClient();
300
301 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800302 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700303 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700304
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700305 virtual sp<LayerBaseClient> getLayerBaseClient() const {
306 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700307
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700308 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800309
Mathias Agopian96f08192010-06-02 23:28:45 -0700310 uint32_t getIdentity() const { return mIdentity; }
311
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700312protected:
313 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800314 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700315
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316 class LayerCleaner {
317 sp<SurfaceFlinger> mFlinger;
318 wp<LayerBaseClient> mLayer;
319 protected:
320 ~LayerCleaner();
321 public:
322 LayerCleaner(const sp<SurfaceFlinger>& flinger,
323 const sp<LayerBaseClient>& layer);
324 };
325
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700327 virtual sp<ISurface> createSurface();
328
Mathias Agopian96f08192010-06-02 23:28:45 -0700329 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800330 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800331 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700332 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700333 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700334 const uint32_t mIdentity;
335 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336};
337
338// ---------------------------------------------------------------------------
339
340}; // namespace android
341
342#endif // ANDROID_LAYER_BASE_H