blob: 80bc4c3e8db68014c0859b8b6444ebcab10c45a4 [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>
Mathias Agopiana4912602012-07-12 14:25:33 -070028#include <utils/String8.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <ui/Region.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Mathias Agopian90ac7992012-02-25 18:48:35 -080032#include <gui/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080033
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Mathias Agopian1b031492012-06-20 17:51:20 -070036#include "DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include "Transform.h"
38
39namespace android {
40
41// ---------------------------------------------------------------------------
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043class Client;
Mathias Agopiana67932f2011-04-20 14:20:59 -070044class DisplayHardware;
Mathias Agopian3330b202009-10-05 17:07:12 -070045class GraphicBuffer;
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;
Mathias Agopian4fec8732012-06-29 14:12:52 -070061 // regions below are in window-manager space
62 Region visibleRegion;
63 Region coveredRegion;
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
Mathias Agopian4fec8732012-06-29 14:12:52 -070090 class LayerMesh {
91 friend class LayerBase;
92 GLfloat mVertices[4][2];
93 size_t mNumVertices;
94 public:
95 LayerMesh() : mNumVertices(4) { }
96 GLfloat const* getVertices() const {
97 return &mVertices[0][0];
98 }
99 size_t getVertexCount() const {
100 return mNumVertices;
101 }
102 };
103
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700104 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -0800105 String8 getName() const;
106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700108 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 bool setLayer(uint32_t z);
110 bool setSize(uint32_t w, uint32_t h);
111 bool setAlpha(uint8_t alpha);
112 bool setMatrix(const layer_state_t::matrix22_t& matrix);
113 bool setTransparentRegionHint(const Region& opaque);
114 bool setFlags(uint8_t flags, uint8_t mask);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700115 bool setCrop(const Rect& crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Mathias Agopianba6be542009-09-29 22:32:36 -0700117 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 bool requestTransaction();
119 void forceVisibilityTransaction();
120
121 uint32_t getTransactionFlags(uint32_t flags);
122 uint32_t setTransactionFlags(uint32_t flags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700123
124 void computeGeometry(const DisplayHardware& hw, LayerMesh* mesh) const;
125 Rect computeBounds() const;
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700128 virtual sp<LayerBaseClient> getLayerBaseClient() const;
129 virtual sp<Layer> getLayer() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700130
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700131 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700132
Mathias Agopian4fec8732012-06-29 14:12:52 -0700133 virtual void setGeometry(const DisplayHardware& hw,
134 HWComposer::HWCLayerInterface& layer);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700135 virtual void setPerFrameData(HWComposer::HWCLayerInterface& layer);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700136 virtual void setAcquireFence(HWComposer::HWCLayerInterface& layer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 /**
139 * draw - performs some global clipping optimizations
140 * and calls onDraw().
141 * Typically this method is not overridden, instead implement onDraw()
142 * to perform the actual drawing.
143 */
Mathias Agopian1b031492012-06-20 17:51:20 -0700144 virtual void draw(const DisplayHardware& hw, const Region& clip) const;
145 virtual void drawForSreenShot(const DisplayHardware& hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146
147 /**
148 * onDraw - draws the surface.
149 */
Mathias Agopian1b031492012-06-20 17:51:20 -0700150 virtual void onDraw(const DisplayHardware& hw, const Region& clip) const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151
152 /**
153 * initStates - called just after construction
154 */
155 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
156
157 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 * doTransaction - process the transaction. This is a good place to figure
159 * out which attributes of the surface have changed.
160 */
161 virtual uint32_t doTransaction(uint32_t transactionFlags);
162
163 /**
164 * setVisibleRegion - called to set the new visible region. This gives
165 * a chance to update the new visible region or record the fact it changed.
166 */
167 virtual void setVisibleRegion(const Region& visibleRegion);
168
169 /**
170 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700171 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 * (transparently or not) by another surface.
173 */
174 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 /**
Mathias Agopian4fec8732012-06-29 14:12:52 -0700177 * latchBuffer - called each time the screen is redrawn and returns whether
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 * the visible regions need to be recomputed (this is a fairly heavy
179 * operation, so this should be set only if needed). Typically this is used
180 * to figure out if the content or size of a surface has changed.
181 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700182 virtual Region latchBuffer(bool& recomputeVisibleRegions);
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700185 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700187 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
189 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700190 * needsDithering - true if this surface needs dithering
191 */
192 virtual bool needsDithering() const { return false; }
193
194 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700195 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700196 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700197 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700198
199 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700201 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 */
203 virtual bool isSecure() const { return false; }
204
Glenn Kasten16f04532011-01-19 15:27:27 -0800205 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800206 * isProtected - true if the layer may contain protected content in the
207 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800208 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800209 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800210
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700211 /** called with the state lock when the surface is removed from the
212 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800213 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800214
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800215 /** called after page-flip
216 */
Jesse Hallef194142012-06-14 14:45:17 -0700217 virtual void onLayerDisplayed(HWComposer::HWCLayerInterface* layer) { }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800218
219 /** called before composition.
220 * returns true if the layer has pending updates.
221 */
222 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800223
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700224 /** always call base class first */
225 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800226 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800227 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800228 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700229
230
Mathias Agopian9a112062009-04-17 19:36:26 -0700231 enum { // flags for doTransaction()
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700232 eDontUpdateGeometryState = 0x00000001,
233 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700234 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236
237 inline const State& drawingState() const { return mDrawingState; }
238 inline const State& currentState() const { return mCurrentState; }
239 inline State& currentState() { return mCurrentState; }
240
Mathias Agopian1b031492012-06-20 17:51:20 -0700241 void clearWithOpenGL(const DisplayHardware& hw, const Region& clip) const;
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243protected:
Mathias Agopian1b031492012-06-20 17:51:20 -0700244 void clearWithOpenGL(const DisplayHardware& hw, const Region& clip,
245 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
246 void drawWithOpenGL(const DisplayHardware& hw, const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700247
248 void setFiltering(bool filtering);
249 bool getFiltering() const;
Mathias Agopianb661d662010-08-19 17:01:19 -0700250
Mathias Agopian9a112062009-04-17 19:36:26 -0700251 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
Mathias Agopiana67932f2011-04-20 14:20:59 -0700253private:
254 // accessed only in the main thread
255 // Whether filtering is forced on or not
256 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700257
Mathias Agopiana67932f2011-04-20 14:20:59 -0700258 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700259 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700260
261protected:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 // these are protected by an external lock
263 State mCurrentState;
264 State mDrawingState;
265 volatile int32_t mTransactionFlags;
266
267 // don't change, don't need a lock
268 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800269 String8 mName;
270 mutable bool mDebug;
271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272
Mathias Agopianca4d3602011-05-19 15:38:14 -0700273public:
274 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 virtual ~LayerBase();
276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279};
280
281
282// ---------------------------------------------------------------------------
283
284class LayerBaseClient : public LayerBase
285{
286public:
Mathias Agopian96f08192010-06-02 23:28:45 -0700287 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
288 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
Mathias Agopiana67932f2011-04-20 14:20:59 -0700290 virtual ~LayerBaseClient();
291
292 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800293 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700294 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700295
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700296 virtual sp<LayerBaseClient> getLayerBaseClient() const {
297 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700298
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700299 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800300
Mathias Agopian96f08192010-06-02 23:28:45 -0700301 uint32_t getIdentity() const { return mIdentity; }
302
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700303protected:
304 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800305 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700306
Mathias Agopiana67932f2011-04-20 14:20:59 -0700307 class LayerCleaner {
308 sp<SurfaceFlinger> mFlinger;
309 wp<LayerBaseClient> mLayer;
310 protected:
311 ~LayerCleaner();
312 public:
313 LayerCleaner(const sp<SurfaceFlinger>& flinger,
314 const sp<LayerBaseClient>& layer);
315 };
316
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700318 virtual sp<ISurface> createSurface();
319
Mathias Agopian96f08192010-06-02 23:28:45 -0700320 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800321 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800322 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700323 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700324 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700325 const uint32_t mIdentity;
326 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327};
328
329// ---------------------------------------------------------------------------
330
331}; // namespace android
332
333#endif // ANDROID_LAYER_BASE_H