blob: 4d5d1e40203ec120596e0474f4dbfebd50857c51 [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 Agopian5ec99b52010-12-07 23:41:55 -080035#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "Transform.h"
37
38namespace android {
39
40// ---------------------------------------------------------------------------
41
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042class Client;
Mathias Agopiana67932f2011-04-20 14:20:59 -070043class DisplayHardware;
Mathias Agopian3330b202009-10-05 17:07:12 -070044class GraphicBuffer;
45class GraphicPlane;
Mathias Agopian670a8992011-09-20 15:13:14 -070046class Layer;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070047class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070048class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50// ---------------------------------------------------------------------------
51
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070054 static int32_t sSequence;
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070057 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 DisplayID dpy;
60 mutable bool contentDirty;
61 Region visibleRegionScreen;
62 Region transparentRegionScreen;
63 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070064 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopian93ffb862012-05-16 17:07:49 -070066 struct Geometry {
Mathias Agopianb30c4152012-05-16 18:21:32 -070067 uint32_t w;
68 uint32_t h;
69 Rect crop;
70 inline bool operator == (const Geometry& rhs) const {
71 return (w==rhs.w && h==rhs.h && crop==rhs.crop);
72 }
73 inline bool operator != (const Geometry& rhs) const {
74 return !operator == (rhs);
75 }
Mathias Agopian93ffb862012-05-16 17:07:49 -070076 };
77
78 struct State {
79 Geometry active;
80 Geometry requested;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 uint32_t z;
82 uint8_t alpha;
83 uint8_t flags;
84 uint8_t reserved[2];
85 int32_t sequence; // changes when visible regions can change
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 Transform transform;
87 Region transparentRegion;
88 };
89
Jamie Gennisa249f2d2011-09-16 17:31:54 -070090 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -080091 String8 getName() const;
92
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -070094 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 bool setLayer(uint32_t z);
96 bool setSize(uint32_t w, uint32_t h);
97 bool setAlpha(uint8_t alpha);
98 bool setMatrix(const layer_state_t::matrix22_t& matrix);
99 bool setTransparentRegionHint(const Region& opaque);
100 bool setFlags(uint8_t flags, uint8_t mask);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700101 bool setCrop(const Rect& crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
Mathias Agopianba6be542009-09-29 22:32:36 -0700103 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 bool requestTransaction();
105 void forceVisibilityTransaction();
106
107 uint32_t getTransactionFlags(uint32_t flags);
108 uint32_t setTransactionFlags(uint32_t flags);
109
110 Rect visibleBounds() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700112 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
Mathias Agopian670a8992011-09-20 15:13:14 -0700113 virtual sp<Layer> getLayer() const { return 0; }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700114
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700115 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700116
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700117 virtual void setGeometry(HWComposer::HWCLayerInterface& layer);
118 virtual void setPerFrameData(HWComposer::HWCLayerInterface& layer);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700119
Mathias Agopiana350ff92010-08-10 17:14:02 -0700120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 /**
122 * draw - performs some global clipping optimizations
123 * and calls onDraw().
124 * Typically this method is not overridden, instead implement onDraw()
125 * to perform the actual drawing.
126 */
127 virtual void draw(const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700128 virtual void drawForSreenShot();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129
130 /**
131 * onDraw - draws the surface.
132 */
133 virtual void onDraw(const Region& clip) const = 0;
134
135 /**
136 * initStates - called just after construction
137 */
138 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
139
140 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 * doTransaction - process the transaction. This is a good place to figure
142 * out which attributes of the surface have changed.
143 */
144 virtual uint32_t doTransaction(uint32_t transactionFlags);
145
146 /**
147 * setVisibleRegion - called to set the new visible region. This gives
148 * a chance to update the new visible region or record the fact it changed.
149 */
150 virtual void setVisibleRegion(const Region& visibleRegion);
151
152 /**
153 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700154 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 * (transparently or not) by another surface.
156 */
157 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 * validateVisibility - cache a bunch of things
161 */
162 virtual void validateVisibility(const Transform& globalTransform);
163
164 /**
165 * lockPageFlip - called each time the screen is redrawn and returns whether
166 * the visible regions need to be recomputed (this is a fairly heavy
167 * operation, so this should be set only if needed). Typically this is used
168 * to figure out if the content or size of a surface has changed.
169 */
170 virtual void lockPageFlip(bool& recomputeVisibleRegions);
171
172 /**
173 * unlockPageFlip - called each time the screen is redrawn. updates the
174 * final dirty region wrt the planeTransform.
175 * At this point, all visible regions, surface position and size, etc... are
176 * correct.
177 */
178 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
179
180 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700181 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700183 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
185 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700186 * needsDithering - true if this surface needs dithering
187 */
188 virtual bool needsDithering() const { return false; }
189
190 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700191 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700192 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700193 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700194
195 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700197 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 */
199 virtual bool isSecure() const { return false; }
200
Glenn Kasten16f04532011-01-19 15:27:27 -0800201 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800202 * isProtected - true if the layer may contain protected content in the
203 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800204 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800205 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800206
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700207 /** called with the state lock when the surface is removed from the
208 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800209 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800210
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800211 /** called after page-flip
212 */
213 virtual void onLayerDisplayed() { }
214
215 /** called before composition.
216 * returns true if the layer has pending updates.
217 */
218 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800219
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700220 /** always call base class first */
221 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800222 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800223 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800224 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700225
226
Mathias Agopian9a112062009-04-17 19:36:26 -0700227 enum { // flags for doTransaction()
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700228 eDontUpdateGeometryState = 0x00000001,
229 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700230 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231
232
233 inline const State& drawingState() const { return mDrawingState; }
234 inline const State& currentState() const { return mCurrentState; }
235 inline State& currentState() { return mCurrentState; }
236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 int32_t getOrientation() const { return mOrientation; }
Jamie Gennis8d91b422011-09-23 15:54:34 -0700238 int32_t getPlaneOrientation() const { return mPlaneOrientation; }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700239
240 void clearWithOpenGL(const Region& clip) const;
241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242protected:
243 const GraphicPlane& graphicPlane(int dpy) const;
244 GraphicPlane& graphicPlane(int dpy);
245
Mathias Agopian010fccb2010-05-26 22:26:12 -0700246 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
247 GLclampf b, GLclampf alpha) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700248 void drawWithOpenGL(const Region& clip) const;
249
250 void setFiltering(bool filtering);
251 bool getFiltering() const;
Mathias Agopianb661d662010-08-19 17:01:19 -0700252
Mathias Agopian9a112062009-04-17 19:36:26 -0700253 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 uint32_t mFlags;
255
Mathias Agopiana67932f2011-04-20 14:20:59 -0700256private:
257 // accessed only in the main thread
258 // Whether filtering is forced on or not
259 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 // cached during validateVisibility()
Mathias Agopiana67932f2011-04-20 14:20:59 -0700262 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700263 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700264
265protected:
266 // cached during validateVisibility()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 int32_t mOrientation;
Jamie Gennis8d91b422011-09-23 15:54:34 -0700268 int32_t mPlaneOrientation;
Mathias Agopiand992db32011-08-18 18:31:00 -0700269 Transform mTransform;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700270 GLfloat mVertices[4][2];
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700271 size_t mNumVertices;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
274 // these are protected by an external lock
275 State mCurrentState;
276 State mDrawingState;
277 volatile int32_t mTransactionFlags;
278
279 // don't change, don't need a lock
280 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800281 String8 mName;
282 mutable bool mDebug;
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
Mathias Agopianca4d3602011-05-19 15:38:14 -0700285public:
286 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700287 virtual ~LayerBase();
288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291};
292
293
294// ---------------------------------------------------------------------------
295
296class LayerBaseClient : public LayerBase
297{
298public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700299 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
300 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301
Mathias Agopiana67932f2011-04-20 14:20:59 -0700302 virtual ~LayerBaseClient();
303
304 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800305 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700306 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700307
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700308 virtual sp<LayerBaseClient> getLayerBaseClient() const {
309 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700310
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700311 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800312
Mathias Agopian96f08192010-06-02 23:28:45 -0700313 uint32_t getIdentity() const { return mIdentity; }
314
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700315protected:
316 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800317 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700318
Mathias Agopiana67932f2011-04-20 14:20:59 -0700319 class LayerCleaner {
320 sp<SurfaceFlinger> mFlinger;
321 wp<LayerBaseClient> mLayer;
322 protected:
323 ~LayerCleaner();
324 public:
325 LayerCleaner(const sp<SurfaceFlinger>& flinger,
326 const sp<LayerBaseClient>& layer);
327 };
328
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700330 virtual sp<ISurface> createSurface();
331
Mathias Agopian96f08192010-06-02 23:28:45 -0700332 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800333 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800334 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700335 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700336 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700337 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