blob: b8f7680b2b2531ba514deeb2073d202ce037c85c [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 Agopian670a8992011-09-20 15:13:14 -070049class Layer;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070050class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070051class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53// ---------------------------------------------------------------------------
54
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070057 static int32_t sSequence;
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070060 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062 DisplayID dpy;
63 mutable bool contentDirty;
64 Region visibleRegionScreen;
65 Region transparentRegionScreen;
66 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070067 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
69 struct State {
70 uint32_t w;
71 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070072 uint32_t requested_w;
73 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 uint32_t z;
75 uint8_t alpha;
76 uint8_t flags;
77 uint8_t reserved[2];
78 int32_t sequence; // changes when visible regions can change
79 uint32_t tint;
80 Transform transform;
81 Region transparentRegion;
82 };
83
Jamie Gennisa249f2d2011-09-16 17:31:54 -070084 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -080085 String8 getName() const;
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -070088 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 bool setLayer(uint32_t z);
90 bool setSize(uint32_t w, uint32_t h);
91 bool setAlpha(uint8_t alpha);
92 bool setMatrix(const layer_state_t::matrix22_t& matrix);
93 bool setTransparentRegionHint(const Region& opaque);
94 bool setFlags(uint8_t flags, uint8_t mask);
95
Mathias Agopianba6be542009-09-29 22:32:36 -070096 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 bool requestTransaction();
98 void forceVisibilityTransaction();
99
100 uint32_t getTransactionFlags(uint32_t flags);
101 uint32_t setTransactionFlags(uint32_t flags);
102
103 Rect visibleBounds() const;
104 void drawRegion(const Region& reg) const;
105
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700106 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
Mathias Agopian670a8992011-09-20 15:13:14 -0700107 virtual sp<Layer> getLayer() const { return 0; }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700108
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);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700112 virtual void setPerFrameData(hwc_layer_t* hwcl);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700113 void setOverlay(bool inOverlay);
114 bool isOverlay() const;
115
Mathias Agopiana350ff92010-08-10 17:14:02 -0700116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 /**
118 * draw - performs some global clipping optimizations
119 * and calls onDraw().
120 * Typically this method is not overridden, instead implement onDraw()
121 * to perform the actual drawing.
122 */
123 virtual void draw(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700124 virtual void drawForSreenShot();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125
126 /**
127 * onDraw - draws the surface.
128 */
129 virtual void onDraw(const Region& clip) const = 0;
130
131 /**
132 * initStates - called just after construction
133 */
134 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
135
136 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 * doTransaction - process the transaction. This is a good place to figure
138 * out which attributes of the surface have changed.
139 */
140 virtual uint32_t doTransaction(uint32_t transactionFlags);
141
142 /**
143 * setVisibleRegion - called to set the new visible region. This gives
144 * a chance to update the new visible region or record the fact it changed.
145 */
146 virtual void setVisibleRegion(const Region& visibleRegion);
147
148 /**
149 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700150 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 * (transparently or not) by another surface.
152 */
153 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 * validateVisibility - cache a bunch of things
157 */
158 virtual void validateVisibility(const Transform& globalTransform);
159
160 /**
161 * lockPageFlip - called each time the screen is redrawn and returns whether
162 * the visible regions need to be recomputed (this is a fairly heavy
163 * operation, so this should be set only if needed). Typically this is used
164 * to figure out if the content or size of a surface has changed.
165 */
166 virtual void lockPageFlip(bool& recomputeVisibleRegions);
167
168 /**
169 * unlockPageFlip - called each time the screen is redrawn. updates the
170 * final dirty region wrt the planeTransform.
171 * At this point, all visible regions, surface position and size, etc... are
172 * correct.
173 */
174 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
175
176 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700177 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700179 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180
181 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700182 * needsDithering - true if this surface needs dithering
183 */
184 virtual bool needsDithering() const { return false; }
185
186 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700187 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700188 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700189 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700190
191 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700193 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 */
195 virtual bool isSecure() const { return false; }
196
Glenn Kasten16f04532011-01-19 15:27:27 -0800197 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800198 * isProtected - true if the layer may contain protected content in the
199 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800200 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800201 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800202
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700203 /** called with the state lock when the surface is removed from the
204 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800205 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800206
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800207 /** called after page-flip
208 */
209 virtual void onLayerDisplayed() { }
210
211 /** called before composition.
212 * returns true if the layer has pending updates.
213 */
214 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800215
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700216 /** always call base class first */
217 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800218 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800219 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800220 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700221
222
Mathias Agopian9a112062009-04-17 19:36:26 -0700223 enum { // flags for doTransaction()
224 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700225 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226
227
228 inline const State& drawingState() const { return mDrawingState; }
229 inline const State& currentState() const { return mCurrentState; }
230 inline State& currentState() { return mCurrentState; }
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 int32_t getOrientation() const { return mOrientation; }
Jamie Gennis8d91b422011-09-23 15:54:34 -0700233 int32_t getPlaneOrientation() const { return mPlaneOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
235protected:
236 const GraphicPlane& graphicPlane(int dpy) const;
237 GraphicPlane& graphicPlane(int dpy);
238
Mathias Agopian010fccb2010-05-26 22:26:12 -0700239 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
240 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 void clearWithOpenGL(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700242 void drawWithOpenGL(const Region& clip) const;
243
244 void setFiltering(bool filtering);
245 bool getFiltering() const;
Mathias Agopianb661d662010-08-19 17:01:19 -0700246
Mathias Agopian9a112062009-04-17 19:36:26 -0700247 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 uint32_t mFlags;
249
Mathias Agopiana67932f2011-04-20 14:20:59 -0700250private:
251 // accessed only in the main thread
252 // Whether filtering is forced on or not
253 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 // cached during validateVisibility()
Mathias Agopiana67932f2011-04-20 14:20:59 -0700256 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700257 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700258
Mathias Agopianf384cc32011-09-08 18:31:55 -0700259 // this layer is currently handled by the hwc. this is
260 // updated at composition time, always frmo the composition
261 // thread.
262 bool mInOverlay;
263
Mathias Agopiana67932f2011-04-20 14:20:59 -0700264protected:
265 // cached during validateVisibility()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 int32_t mOrientation;
Jamie Gennis8d91b422011-09-23 15:54:34 -0700267 int32_t mPlaneOrientation;
Mathias Agopiand992db32011-08-18 18:31:00 -0700268 Transform mTransform;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700269 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271
272 // these are protected by an external lock
273 State mCurrentState;
274 State mDrawingState;
275 volatile int32_t mTransactionFlags;
276
277 // don't change, don't need a lock
278 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800279 String8 mName;
280 mutable bool mDebug;
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
Mathias Agopianca4d3602011-05-19 15:38:14 -0700283public:
284 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 virtual ~LayerBase();
286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700288 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289};
290
291
292// ---------------------------------------------------------------------------
293
294class LayerBaseClient : public LayerBase
295{
296public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700297 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
298 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopiana67932f2011-04-20 14:20:59 -0700300 virtual ~LayerBaseClient();
301
302 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800303 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700304 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700305
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700306 virtual sp<LayerBaseClient> getLayerBaseClient() const {
307 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700308
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700309 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800310
Mathias Agopian96f08192010-06-02 23:28:45 -0700311 uint32_t getIdentity() const { return mIdentity; }
312
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700313protected:
314 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800315 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700316
Mathias Agopiana67932f2011-04-20 14:20:59 -0700317 class LayerCleaner {
318 sp<SurfaceFlinger> mFlinger;
319 wp<LayerBaseClient> mLayer;
320 protected:
321 ~LayerCleaner();
322 public:
323 LayerCleaner(const sp<SurfaceFlinger>& flinger,
324 const sp<LayerBaseClient>& layer);
325 };
326
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700328 virtual sp<ISurface> createSurface();
329
Mathias Agopian96f08192010-06-02 23:28:45 -0700330 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800331 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800332 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700333 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700334 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700335 const uint32_t mIdentity;
336 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337};
338
339// ---------------------------------------------------------------------------
340
341}; // namespace android
342
343#endif // ANDROID_LAYER_BASE_H