blob: 01d82d430ad21e504718f3a061cf6af975b82a15 [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 Agopian3330b202009-10-05 17:07:12 -070047class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
49// ---------------------------------------------------------------------------
50
Andy McFadden882e3a32013-01-08 16:06:15 -080051/*
52 * Layers are rectangular graphic entities, internal to SurfaceFlinger.
53 * They have properties including width, height, Z-depth, and 2D
54 * transformations (chiefly translation and 90-degree rotations).
55 *
56 * Layers are organized into "layer stacks". Each layer is a member of
57 * exactly one layer stack, identified by an integer in Layer::State. A
58 * given layer stack may appear on more than one display.
Andy McFadden882e3a32013-01-08 16:06:15 -080059 */
Igor Murashkina4a31492012-10-29 13:36:11 -070060class LayerBase : virtual public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070062 static int32_t sSequence;
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064public:
Mathias Agopianb79f61d2013-03-05 15:14:58 -080065 LayerBase(SurfaceFlinger* flinger, const sp<Client>& client);
66
67
68 // Creates an ISurface associated with this object. This may only be
69 // called once (see also getSurfaceBinder()).
70 sp<ISurface> getSurface();
71
72 // Returns the Binder object for the ISurface associated with
73 // this object.
74 wp<IBinder> getSurfaceBinder() const;
75
76 virtual wp<IBinder> getSurfaceTextureBinder() const;
77
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 mutable bool contentDirty;
Mathias Agopian4fec8732012-06-29 14:12:52 -070080 // regions below are in window-manager space
81 Region visibleRegion;
82 Region coveredRegion;
Jesse Halla8026d22012-09-25 13:25:04 -070083 Region visibleNonTransparentRegion;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070084 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085
Mathias Agopian93ffb862012-05-16 17:07:49 -070086 struct Geometry {
Mathias Agopianb30c4152012-05-16 18:21:32 -070087 uint32_t w;
88 uint32_t h;
89 Rect crop;
90 inline bool operator == (const Geometry& rhs) const {
91 return (w==rhs.w && h==rhs.h && crop==rhs.crop);
92 }
93 inline bool operator != (const Geometry& rhs) const {
94 return !operator == (rhs);
95 }
Mathias Agopian93ffb862012-05-16 17:07:49 -070096 };
97
98 struct State {
99 Geometry active;
100 Geometry requested;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 uint32_t z;
Mathias Agopian87855782012-07-24 21:41:09 -0700102 uint32_t layerStack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 uint8_t alpha;
104 uint8_t flags;
105 uint8_t reserved[2];
106 int32_t sequence; // changes when visible regions can change
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 Transform transform;
108 Region transparentRegion;
109 };
110
Mathias Agopian4fec8732012-06-29 14:12:52 -0700111 class LayerMesh {
112 friend class LayerBase;
113 GLfloat mVertices[4][2];
114 size_t mNumVertices;
115 public:
116 LayerMesh() : mNumVertices(4) { }
117 GLfloat const* getVertices() const {
118 return &mVertices[0][0];
119 }
120 size_t getVertexCount() const {
121 return mNumVertices;
122 }
123 };
124
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700125 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -0800126 String8 getName() const;
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700129 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 bool setLayer(uint32_t z);
131 bool setSize(uint32_t w, uint32_t h);
132 bool setAlpha(uint8_t alpha);
133 bool setMatrix(const layer_state_t::matrix22_t& matrix);
Jesse Halla8026d22012-09-25 13:25:04 -0700134 bool setTransparentRegionHint(const Region& transparent);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 bool setFlags(uint8_t flags, uint8_t mask);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700136 bool setCrop(const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700137 bool setLayerStack(uint32_t layerStack);
138
Mathias Agopianba6be542009-09-29 22:32:36 -0700139 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 bool requestTransaction();
141 void forceVisibilityTransaction();
142
143 uint32_t getTransactionFlags(uint32_t flags);
144 uint32_t setTransactionFlags(uint32_t flags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700145
Mathias Agopian42977342012-08-05 00:40:46 -0700146 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700147 Rect computeBounds() const;
148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700150 virtual sp<Layer> getLayer() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700151
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700152 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700153
Mathias Agopian42977342012-08-05 00:40:46 -0700154 virtual void setGeometry(const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700155 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700156 virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700157 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700158 virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700159 HWComposer::HWCLayerInterface& layer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 /**
162 * draw - performs some global clipping optimizations
163 * and calls onDraw().
164 * Typically this method is not overridden, instead implement onDraw()
165 * to perform the actual drawing.
166 */
Mathias Agopian42977342012-08-05 00:40:46 -0700167 virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
168 virtual void draw(const sp<const DisplayDevice>& hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
170 /**
171 * onDraw - draws the surface.
172 */
Mathias Agopian42977342012-08-05 00:40:46 -0700173 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174
175 /**
176 * initStates - called just after construction
177 */
178 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
179
180 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 * doTransaction - process the transaction. This is a good place to figure
182 * out which attributes of the surface have changed.
183 */
184 virtual uint32_t doTransaction(uint32_t transactionFlags);
185
186 /**
187 * setVisibleRegion - called to set the new visible region. This gives
188 * a chance to update the new visible region or record the fact it changed.
189 */
190 virtual void setVisibleRegion(const Region& visibleRegion);
191
192 /**
193 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700194 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 * (transparently or not) by another surface.
196 */
197 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 /**
Jesse Halla8026d22012-09-25 13:25:04 -0700200 * setVisibleNonTransparentRegion - called when the visible and
201 * non-transparent region changes.
202 */
203 virtual void setVisibleNonTransparentRegion(const Region&
204 visibleNonTransparentRegion);
205
206 /**
Mathias Agopian4fec8732012-06-29 14:12:52 -0700207 * latchBuffer - called each time the screen is redrawn and returns whether
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 * the visible regions need to be recomputed (this is a fairly heavy
209 * operation, so this should be set only if needed). Typically this is used
210 * to figure out if the content or size of a surface has changed.
211 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700212 virtual Region latchBuffer(bool& recomputeVisibleRegions);
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700215 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700217 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218
219 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700220 * needsDithering - true if this surface needs dithering
221 */
222 virtual bool needsDithering() const { return false; }
223
224 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700225 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700226 */
Mathias Agopianeba8c682012-09-19 23:14:45 -0700227 virtual bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700228
229 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700231 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 */
233 virtual bool isSecure() const { return false; }
234
Glenn Kasten16f04532011-01-19 15:27:27 -0800235 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800236 * isProtected - true if the layer may contain protected content in the
237 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800238 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800239 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800240
Mathias Agopianda27af92012-09-13 18:17:13 -0700241 /*
242 * isVisible - true if this layer is visibile, false otherwise
243 */
244 virtual bool isVisible() const;
245
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700246 /** called with the state lock when the surface is removed from the
247 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800248 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800249
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800250 /** called after page-flip
251 */
Mathias Agopian42977342012-08-05 00:40:46 -0700252 virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
Mathias Agopianc3973602012-08-31 17:51:25 -0700253 HWComposer::HWCLayerInterface* layer);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800254
255 /** called before composition.
256 * returns true if the layer has pending updates.
257 */
258 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800259
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700260 /** called before composition.
261 */
262 virtual void onPostComposition() { }
263
Andy McFadden69052052012-09-14 16:10:11 -0700264 /**
Andy McFadden2adaf042012-12-18 09:49:45 -0800265 * Updates the GLConsumer's transform hint, for layers that have
266 * a GLConsumer.
Andy McFadden69052052012-09-14 16:10:11 -0700267 */
Mathias Agopian84300952012-11-21 16:02:13 -0800268 virtual void updateTransformHint(const sp<const DisplayDevice>& hw) const { }
Andy McFadden69052052012-09-14 16:10:11 -0700269
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 /**
271 * returns the rectangle that crops the content of the layer and scales it
272 * to the layer's size.
273 */
274 virtual Rect getContentCrop() const;
275
276 /*
277 * returns the transform bits (90 rotation / h-flip / v-flip) of the
278 * layer's content
279 */
280 virtual uint32_t getContentTransform() const;
281
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700282 /** always call base class first */
283 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800284 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800285 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800286 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700287
288
Mathias Agopian9a112062009-04-17 19:36:26 -0700289 enum { // flags for doTransaction()
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700290 eDontUpdateGeometryState = 0x00000001,
291 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700292 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293
294
295 inline const State& drawingState() const { return mDrawingState; }
296 inline const State& currentState() const { return mCurrentState; }
297 inline State& currentState() { return mCurrentState; }
298
Mathias Agopian42977342012-08-05 00:40:46 -0700299 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700300
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700301 void setFiltering(bool filtering);
302 bool getFiltering() const;
303
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800304private:
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800305 virtual sp<ISurface> createSurface();
306
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800307 Rect computeCrop(const sp<const DisplayDevice>& hw) const;
308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309protected:
Mathias Agopian42977342012-08-05 00:40:46 -0700310 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian1b031492012-06-20 17:51:20 -0700311 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
Mathias Agopian42977342012-08-05 00:40:46 -0700312 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700313
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800314
315 /*
316 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
317 * is called.
318 */
319 class LayerCleaner {
320 sp<SurfaceFlinger> mFlinger;
321 wp<LayerBase> mLayer;
322 protected:
323 ~LayerCleaner();
324 public:
325 LayerCleaner(const sp<SurfaceFlinger>& flinger,
326 const sp<LayerBase>& layer);
327 };
328
329
330
Mathias Agopian9a112062009-04-17 19:36:26 -0700331 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332
Mathias Agopiana67932f2011-04-20 14:20:59 -0700333private:
334 // accessed only in the main thread
335 // Whether filtering is forced on or not
336 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700337
Mathias Agopiana67932f2011-04-20 14:20:59 -0700338 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700339 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700340
341protected:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 // these are protected by an external lock
343 State mCurrentState;
344 State mDrawingState;
345 volatile int32_t mTransactionFlags;
346
347 // don't change, don't need a lock
348 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800349 String8 mName;
350 mutable bool mDebug;
351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700354
Mathias Agopian96f08192010-06-02 23:28:45 -0700355 mutable Mutex mLock;
Andy McFadden882e3a32013-01-08 16:06:15 -0800356
357 // Set to true if an ISurface has been associated with this object.
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800358 mutable bool mHasSurface;
Andy McFadden882e3a32013-01-08 16:06:15 -0800359
360 // The ISurface's Binder object, set by getSurface().
Mathias Agopian0d156122011-01-25 20:17:45 -0800361 wp<IBinder> mClientSurfaceBinder;
Andy McFadden882e3a32013-01-08 16:06:15 -0800362
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700363 const wp<Client> mClientRef;
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800364
365
366public:
367 // called from class SurfaceFlinger
368 virtual ~LayerBase();
369
370private:
371 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372};
373
374// ---------------------------------------------------------------------------
375
376}; // namespace android
377
378#endif // ANDROID_LAYER_BASE_H