blob: 47473e79072a76f587fa8ad4062f9df742deca90 [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
36#include "Transform.h"
Mathias Agopian86303202012-07-24 22:46:10 -070037#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
39namespace android {
40
41// ---------------------------------------------------------------------------
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043class Client;
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070044class DisplayDevice;
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
Andy McFadden882e3a32013-01-08 16:06:15 -080052/*
53 * Layers are rectangular graphic entities, internal to SurfaceFlinger.
54 * They have properties including width, height, Z-depth, and 2D
55 * transformations (chiefly translation and 90-degree rotations).
56 *
57 * Layers are organized into "layer stacks". Each layer is a member of
58 * exactly one layer stack, identified by an integer in Layer::State. A
59 * given layer stack may appear on more than one display.
60 *
61 * Notable subclasses (below LayerBaseClient) include Layer, LayerDim, and
62 * LayerScreenshot.
63 */
Igor Murashkina4a31492012-10-29 13:36:11 -070064class LayerBase : virtual public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070066 static int32_t sSequence;
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068public:
Mathias Agopian3ee454a2012-08-27 16:28:24 -070069 LayerBase(SurfaceFlinger* flinger);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 mutable bool contentDirty;
Mathias Agopian4fec8732012-06-29 14:12:52 -070072 // regions below are in window-manager space
73 Region visibleRegion;
74 Region coveredRegion;
Jesse Halla8026d22012-09-25 13:25:04 -070075 Region visibleNonTransparentRegion;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070076 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
Mathias Agopian93ffb862012-05-16 17:07:49 -070078 struct Geometry {
Mathias Agopianb30c4152012-05-16 18:21:32 -070079 uint32_t w;
80 uint32_t h;
81 Rect crop;
82 inline bool operator == (const Geometry& rhs) const {
83 return (w==rhs.w && h==rhs.h && crop==rhs.crop);
84 }
85 inline bool operator != (const Geometry& rhs) const {
86 return !operator == (rhs);
87 }
Mathias Agopian93ffb862012-05-16 17:07:49 -070088 };
89
90 struct State {
91 Geometry active;
92 Geometry requested;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 uint32_t z;
Mathias Agopian87855782012-07-24 21:41:09 -070094 uint32_t layerStack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 uint8_t alpha;
96 uint8_t flags;
97 uint8_t reserved[2];
98 int32_t sequence; // changes when visible regions can change
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 Transform transform;
100 Region transparentRegion;
101 };
102
Mathias Agopian4fec8732012-06-29 14:12:52 -0700103 class LayerMesh {
104 friend class LayerBase;
105 GLfloat mVertices[4][2];
106 size_t mNumVertices;
107 public:
108 LayerMesh() : mNumVertices(4) { }
109 GLfloat const* getVertices() const {
110 return &mVertices[0][0];
111 }
112 size_t getVertexCount() const {
113 return mNumVertices;
114 }
115 };
116
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700117 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -0800118 String8 getName() const;
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700121 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 bool setLayer(uint32_t z);
123 bool setSize(uint32_t w, uint32_t h);
124 bool setAlpha(uint8_t alpha);
125 bool setMatrix(const layer_state_t::matrix22_t& matrix);
Jesse Halla8026d22012-09-25 13:25:04 -0700126 bool setTransparentRegionHint(const Region& transparent);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 bool setFlags(uint8_t flags, uint8_t mask);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700128 bool setCrop(const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700129 bool setLayerStack(uint32_t layerStack);
130
Mathias Agopianba6be542009-09-29 22:32:36 -0700131 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 bool requestTransaction();
133 void forceVisibilityTransaction();
134
135 uint32_t getTransactionFlags(uint32_t flags);
136 uint32_t setTransactionFlags(uint32_t flags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700137
Mathias Agopian42977342012-08-05 00:40:46 -0700138 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700139 Rect computeBounds() const;
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700142 virtual sp<LayerBaseClient> getLayerBaseClient() const;
143 virtual sp<Layer> getLayer() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700145 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700146
Mathias Agopian42977342012-08-05 00:40:46 -0700147 virtual void setGeometry(const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700148 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700149 virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700150 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700151 virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700152 HWComposer::HWCLayerInterface& layer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 /**
155 * draw - performs some global clipping optimizations
156 * and calls onDraw().
157 * Typically this method is not overridden, instead implement onDraw()
158 * to perform the actual drawing.
159 */
Mathias Agopian42977342012-08-05 00:40:46 -0700160 virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
161 virtual void draw(const sp<const DisplayDevice>& hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
163 /**
164 * onDraw - draws the surface.
165 */
Mathias Agopian42977342012-08-05 00:40:46 -0700166 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
168 /**
169 * initStates - called just after construction
170 */
171 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
172
173 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 * doTransaction - process the transaction. This is a good place to figure
175 * out which attributes of the surface have changed.
176 */
177 virtual uint32_t doTransaction(uint32_t transactionFlags);
178
179 /**
180 * setVisibleRegion - called to set the new visible region. This gives
181 * a chance to update the new visible region or record the fact it changed.
182 */
183 virtual void setVisibleRegion(const Region& visibleRegion);
184
185 /**
186 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700187 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 * (transparently or not) by another surface.
189 */
190 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 /**
Jesse Halla8026d22012-09-25 13:25:04 -0700193 * setVisibleNonTransparentRegion - called when the visible and
194 * non-transparent region changes.
195 */
196 virtual void setVisibleNonTransparentRegion(const Region&
197 visibleNonTransparentRegion);
198
199 /**
Mathias Agopian4fec8732012-06-29 14:12:52 -0700200 * latchBuffer - called each time the screen is redrawn and returns whether
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 * the visible regions need to be recomputed (this is a fairly heavy
202 * operation, so this should be set only if needed). Typically this is used
203 * to figure out if the content or size of a surface has changed.
204 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700205 virtual Region latchBuffer(bool& recomputeVisibleRegions);
206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700208 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700210 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211
212 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700213 * needsDithering - true if this surface needs dithering
214 */
215 virtual bool needsDithering() const { return false; }
216
217 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700218 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700219 */
Mathias Agopianeba8c682012-09-19 23:14:45 -0700220 virtual bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700221
222 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700224 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 */
226 virtual bool isSecure() const { return false; }
227
Glenn Kasten16f04532011-01-19 15:27:27 -0800228 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800229 * isProtected - true if the layer may contain protected content in the
230 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800231 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800232 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800233
Mathias Agopianda27af92012-09-13 18:17:13 -0700234 /*
235 * isVisible - true if this layer is visibile, false otherwise
236 */
237 virtual bool isVisible() const;
238
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700239 /** called with the state lock when the surface is removed from the
240 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800241 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800242
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800243 /** called after page-flip
244 */
Mathias Agopian42977342012-08-05 00:40:46 -0700245 virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
Mathias Agopianc3973602012-08-31 17:51:25 -0700246 HWComposer::HWCLayerInterface* layer);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800247
248 /** called before composition.
249 * returns true if the layer has pending updates.
250 */
251 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800252
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700253 /** called before composition.
254 */
255 virtual void onPostComposition() { }
256
Andy McFadden69052052012-09-14 16:10:11 -0700257 /**
Andy McFadden2adaf042012-12-18 09:49:45 -0800258 * Updates the GLConsumer's transform hint, for layers that have
259 * a GLConsumer.
Andy McFadden69052052012-09-14 16:10:11 -0700260 */
Mathias Agopian84300952012-11-21 16:02:13 -0800261 virtual void updateTransformHint(const sp<const DisplayDevice>& hw) const { }
Andy McFadden69052052012-09-14 16:10:11 -0700262
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700263 /** always call base class first */
264 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800265 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800266 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800267 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700268
269
Mathias Agopian9a112062009-04-17 19:36:26 -0700270 enum { // flags for doTransaction()
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700271 eDontUpdateGeometryState = 0x00000001,
272 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700273 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274
275
276 inline const State& drawingState() const { return mDrawingState; }
277 inline const State& currentState() const { return mCurrentState; }
278 inline State& currentState() { return mCurrentState; }
279
Mathias Agopian42977342012-08-05 00:40:46 -0700280 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700281
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700282 void setFiltering(bool filtering);
283 bool getFiltering() const;
284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285protected:
Mathias Agopian42977342012-08-05 00:40:46 -0700286 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian1b031492012-06-20 17:51:20 -0700287 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
Mathias Agopian42977342012-08-05 00:40:46 -0700288 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700289
Mathias Agopian9a112062009-04-17 19:36:26 -0700290 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291
Mathias Agopiana67932f2011-04-20 14:20:59 -0700292private:
293 // accessed only in the main thread
294 // Whether filtering is forced on or not
295 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700296
Mathias Agopiana67932f2011-04-20 14:20:59 -0700297 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700298 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700299
300protected:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 // these are protected by an external lock
302 State mCurrentState;
303 State mDrawingState;
304 volatile int32_t mTransactionFlags;
305
306 // don't change, don't need a lock
307 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800308 String8 mName;
309 mutable bool mDebug;
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311
Mathias Agopianca4d3602011-05-19 15:38:14 -0700312public:
313 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314 virtual ~LayerBase();
315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318};
319
320
321// ---------------------------------------------------------------------------
322
Andy McFadden882e3a32013-01-08 16:06:15 -0800323/*
324 * This adds some additional fields and methods to support some Binder IPC
325 * interactions. In particular, the LayerBaseClient's lifetime can be
326 * managed by references to an ISurface object in another process.
327 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328class LayerBaseClient : public LayerBase
329{
330public:
Andy McFadden882e3a32013-01-08 16:06:15 -0800331 LayerBaseClient(SurfaceFlinger* flinger, const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332
Andy McFadden882e3a32013-01-08 16:06:15 -0800333 virtual ~LayerBaseClient();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700334
Andy McFadden882e3a32013-01-08 16:06:15 -0800335 // Creates an ISurface associated with this object. This may only be
336 // called once (see also getSurfaceBinder()).
337 sp<ISurface> getSurface();
338
339 // Returns the Binder object for the ISurface associated with
340 // this object.
341 wp<IBinder> getSurfaceBinder() const;
342
343 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700345 virtual sp<LayerBaseClient> getLayerBaseClient() const {
346 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700347
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700348 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800349
Mathias Agopian96f08192010-06-02 23:28:45 -0700350 uint32_t getIdentity() const { return mIdentity; }
351
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700352protected:
353 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800354 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700355
Andy McFadden882e3a32013-01-08 16:06:15 -0800356 /*
357 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
358 * is called.
359 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700360 class LayerCleaner {
361 sp<SurfaceFlinger> mFlinger;
362 wp<LayerBaseClient> mLayer;
363 protected:
364 ~LayerCleaner();
365 public:
366 LayerCleaner(const sp<SurfaceFlinger>& flinger,
367 const sp<LayerBaseClient>& layer);
368 };
369
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700370private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700371 virtual sp<ISurface> createSurface();
372
Mathias Agopian96f08192010-06-02 23:28:45 -0700373 mutable Mutex mLock;
Andy McFadden882e3a32013-01-08 16:06:15 -0800374
375 // Set to true if an ISurface has been associated with this object.
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800376 mutable bool mHasSurface;
Andy McFadden882e3a32013-01-08 16:06:15 -0800377
378 // The ISurface's Binder object, set by getSurface().
Mathias Agopian0d156122011-01-25 20:17:45 -0800379 wp<IBinder> mClientSurfaceBinder;
Andy McFadden882e3a32013-01-08 16:06:15 -0800380
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700381 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700382 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700383 const uint32_t mIdentity;
384 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385};
386
387// ---------------------------------------------------------------------------
388
389}; // namespace android
390
391#endif // ANDROID_LAYER_BASE_H