blob: d227b2d39dd27438080aae3306c614b615f43111 [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 Agopian1b031492012-06-20 17:51:20 -070035#include "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;
Mathias Agopian670a8992011-09-20 15:13:14 -070045class Layer;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070046class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070047class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
49// ---------------------------------------------------------------------------
50
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070053 static int32_t sSequence;
54
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;
Mathias Agopian4fec8732012-06-29 14:12:52 -070060 // regions below are in window-manager space
61 Region visibleRegion;
62 Region coveredRegion;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070063 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopian93ffb862012-05-16 17:07:49 -070065 struct Geometry {
Mathias Agopianb30c4152012-05-16 18:21:32 -070066 uint32_t w;
67 uint32_t h;
68 Rect crop;
69 inline bool operator == (const Geometry& rhs) const {
70 return (w==rhs.w && h==rhs.h && crop==rhs.crop);
71 }
72 inline bool operator != (const Geometry& rhs) const {
73 return !operator == (rhs);
74 }
Mathias Agopian93ffb862012-05-16 17:07:49 -070075 };
76
77 struct State {
78 Geometry active;
79 Geometry requested;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 uint32_t z;
81 uint8_t alpha;
82 uint8_t flags;
83 uint8_t reserved[2];
84 int32_t sequence; // changes when visible regions can change
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 Transform transform;
86 Region transparentRegion;
87 };
88
Mathias Agopian4fec8732012-06-29 14:12:52 -070089 class LayerMesh {
90 friend class LayerBase;
91 GLfloat mVertices[4][2];
92 size_t mNumVertices;
93 public:
94 LayerMesh() : mNumVertices(4) { }
95 GLfloat const* getVertices() const {
96 return &mVertices[0][0];
97 }
98 size_t getVertexCount() const {
99 return mNumVertices;
100 }
101 };
102
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700103 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -0800104 String8 getName() const;
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700107 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 bool setLayer(uint32_t z);
109 bool setSize(uint32_t w, uint32_t h);
110 bool setAlpha(uint8_t alpha);
111 bool setMatrix(const layer_state_t::matrix22_t& matrix);
112 bool setTransparentRegionHint(const Region& opaque);
113 bool setFlags(uint8_t flags, uint8_t mask);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700114 bool setCrop(const Rect& crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115
Mathias Agopianba6be542009-09-29 22:32:36 -0700116 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 bool requestTransaction();
118 void forceVisibilityTransaction();
119
120 uint32_t getTransactionFlags(uint32_t flags);
121 uint32_t setTransactionFlags(uint32_t flags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700122
123 void computeGeometry(const DisplayHardware& hw, LayerMesh* mesh) const;
124 Rect computeBounds() const;
125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700127 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
Mathias Agopian670a8992011-09-20 15:13:14 -0700128 virtual sp<Layer> getLayer() const { return 0; }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700129
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700130 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700131
Mathias Agopian4fec8732012-06-29 14:12:52 -0700132 virtual void setGeometry(const DisplayHardware& hw,
133 HWComposer::HWCLayerInterface& layer);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700134 virtual void setPerFrameData(HWComposer::HWCLayerInterface& layer);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700135 virtual void setAcquireFence(HWComposer::HWCLayerInterface& layer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 /**
138 * draw - performs some global clipping optimizations
139 * and calls onDraw().
140 * Typically this method is not overridden, instead implement onDraw()
141 * to perform the actual drawing.
142 */
Mathias Agopian1b031492012-06-20 17:51:20 -0700143 virtual void draw(const DisplayHardware& hw, const Region& clip) const;
144 virtual void drawForSreenShot(const DisplayHardware& hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
146 /**
147 * onDraw - draws the surface.
148 */
Mathias Agopian1b031492012-06-20 17:51:20 -0700149 virtual void onDraw(const DisplayHardware& hw, const Region& clip) const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150
151 /**
152 * initStates - called just after construction
153 */
154 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
155
156 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 * doTransaction - process the transaction. This is a good place to figure
158 * out which attributes of the surface have changed.
159 */
160 virtual uint32_t doTransaction(uint32_t transactionFlags);
161
162 /**
163 * setVisibleRegion - called to set the new visible region. This gives
164 * a chance to update the new visible region or record the fact it changed.
165 */
166 virtual void setVisibleRegion(const Region& visibleRegion);
167
168 /**
169 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700170 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 * (transparently or not) by another surface.
172 */
173 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 /**
Mathias Agopian4fec8732012-06-29 14:12:52 -0700176 * latchBuffer - called each time the screen is redrawn and returns whether
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 * the visible regions need to be recomputed (this is a fairly heavy
178 * operation, so this should be set only if needed). Typically this is used
179 * to figure out if the content or size of a surface has changed.
180 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700181 virtual Region latchBuffer(bool& recomputeVisibleRegions);
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700184 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700186 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187
188 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700189 * needsDithering - true if this surface needs dithering
190 */
191 virtual bool needsDithering() const { return false; }
192
193 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700194 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700195 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700196 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700197
198 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700200 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 */
202 virtual bool isSecure() const { return false; }
203
Glenn Kasten16f04532011-01-19 15:27:27 -0800204 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800205 * isProtected - true if the layer may contain protected content in the
206 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800207 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800208 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800209
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700210 /** called with the state lock when the surface is removed from the
211 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800212 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800213
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800214 /** called after page-flip
215 */
Jesse Hallef194142012-06-14 14:45:17 -0700216 virtual void onLayerDisplayed(HWComposer::HWCLayerInterface* layer) { }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800217
218 /** called before composition.
219 * returns true if the layer has pending updates.
220 */
221 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800222
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700223 /** always call base class first */
224 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800225 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800226 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800227 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700228
229
Mathias Agopian9a112062009-04-17 19:36:26 -0700230 enum { // flags for doTransaction()
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700231 eDontUpdateGeometryState = 0x00000001,
232 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700233 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
235
236 inline const State& drawingState() const { return mDrawingState; }
237 inline const State& currentState() const { return mCurrentState; }
238 inline State& currentState() { return mCurrentState; }
239
Mathias Agopian1b031492012-06-20 17:51:20 -0700240 void clearWithOpenGL(const DisplayHardware& hw, const Region& clip) const;
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242protected:
Mathias Agopian1b031492012-06-20 17:51:20 -0700243 void clearWithOpenGL(const DisplayHardware& hw, const Region& clip,
244 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
245 void drawWithOpenGL(const DisplayHardware& hw, const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700246
247 void setFiltering(bool filtering);
248 bool getFiltering() const;
Mathias Agopianb661d662010-08-19 17:01:19 -0700249
Mathias Agopian9a112062009-04-17 19:36:26 -0700250 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
Mathias Agopiana67932f2011-04-20 14:20:59 -0700252private:
253 // accessed only in the main thread
254 // Whether filtering is forced on or not
255 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700256
Mathias Agopiana67932f2011-04-20 14:20:59 -0700257 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700258 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700259
260protected:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 // these are protected by an external lock
262 State mCurrentState;
263 State mDrawingState;
264 volatile int32_t mTransactionFlags;
265
266 // don't change, don't need a lock
267 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800268 String8 mName;
269 mutable bool mDebug;
270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271
Mathias Agopianca4d3602011-05-19 15:38:14 -0700272public:
273 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700274 virtual ~LayerBase();
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278};
279
280
281// ---------------------------------------------------------------------------
282
283class LayerBaseClient : public LayerBase
284{
285public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700286 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
287 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
Mathias Agopiana67932f2011-04-20 14:20:59 -0700289 virtual ~LayerBaseClient();
290
291 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800292 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700293 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700294
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700295 virtual sp<LayerBaseClient> getLayerBaseClient() const {
296 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700297
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700298 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800299
Mathias Agopian96f08192010-06-02 23:28:45 -0700300 uint32_t getIdentity() const { return mIdentity; }
301
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700302protected:
303 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800304 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700305
Mathias Agopiana67932f2011-04-20 14:20:59 -0700306 class LayerCleaner {
307 sp<SurfaceFlinger> mFlinger;
308 wp<LayerBaseClient> mLayer;
309 protected:
310 ~LayerCleaner();
311 public:
312 LayerCleaner(const sp<SurfaceFlinger>& flinger,
313 const sp<LayerBaseClient>& layer);
314 };
315
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700317 virtual sp<ISurface> createSurface();
318
Mathias Agopian96f08192010-06-02 23:28:45 -0700319 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800320 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800321 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700322 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700323 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700324 const uint32_t mIdentity;
325 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326};
327
328// ---------------------------------------------------------------------------
329
330}; // namespace android
331
332#endif // ANDROID_LAYER_BASE_H