blob: 46713faaeb83eef2afc87954650f37ed64fec292 [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
Igor Murashkina4a31492012-10-29 13:36:11 -070052class LayerBase : virtual 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 Agopian3ee454a2012-08-27 16:28:24 -070057 LayerBase(SurfaceFlinger* flinger);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 mutable bool contentDirty;
Mathias Agopian4fec8732012-06-29 14:12:52 -070060 // regions below are in window-manager space
61 Region visibleRegion;
62 Region coveredRegion;
Jesse Halla8026d22012-09-25 13:25:04 -070063 Region visibleNonTransparentRegion;
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;
Mathias Agopian87855782012-07-24 21:41:09 -070082 uint32_t layerStack;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 uint8_t alpha;
84 uint8_t flags;
85 uint8_t reserved[2];
86 int32_t sequence; // changes when visible regions can change
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 Transform transform;
88 Region transparentRegion;
89 };
90
Mathias Agopian4fec8732012-06-29 14:12:52 -070091 class LayerMesh {
92 friend class LayerBase;
93 GLfloat mVertices[4][2];
94 size_t mNumVertices;
95 public:
96 LayerMesh() : mNumVertices(4) { }
97 GLfloat const* getVertices() const {
98 return &mVertices[0][0];
99 }
100 size_t getVertexCount() const {
101 return mNumVertices;
102 }
103 };
104
Jamie Gennisa249f2d2011-09-16 17:31:54 -0700105 virtual void setName(const String8& name);
Mathias Agopiand1296592010-03-09 19:17:47 -0800106 String8 getName() const;
107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 // modify current state
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700109 bool setPosition(float x, float y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 bool setLayer(uint32_t z);
111 bool setSize(uint32_t w, uint32_t h);
112 bool setAlpha(uint8_t alpha);
113 bool setMatrix(const layer_state_t::matrix22_t& matrix);
Jesse Halla8026d22012-09-25 13:25:04 -0700114 bool setTransparentRegionHint(const Region& transparent);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 bool setFlags(uint8_t flags, uint8_t mask);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700116 bool setCrop(const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700117 bool setLayerStack(uint32_t layerStack);
118
Mathias Agopianba6be542009-09-29 22:32:36 -0700119 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 bool requestTransaction();
121 void forceVisibilityTransaction();
122
123 uint32_t getTransactionFlags(uint32_t flags);
124 uint32_t setTransactionFlags(uint32_t flags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700125
Mathias Agopian42977342012-08-05 00:40:46 -0700126 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700127 Rect computeBounds() const;
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700130 virtual sp<LayerBaseClient> getLayerBaseClient() const;
131 virtual sp<Layer> getLayer() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700132
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700133 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700134
Mathias Agopian42977342012-08-05 00:40:46 -0700135 virtual void setGeometry(const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700136 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700137 virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700138 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700139 virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700140 HWComposer::HWCLayerInterface& layer);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 /**
143 * draw - performs some global clipping optimizations
144 * and calls onDraw().
145 * Typically this method is not overridden, instead implement onDraw()
146 * to perform the actual drawing.
147 */
Mathias Agopian42977342012-08-05 00:40:46 -0700148 virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
149 virtual void draw(const sp<const DisplayDevice>& hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150
151 /**
152 * onDraw - draws the surface.
153 */
Mathias Agopian42977342012-08-05 00:40:46 -0700154 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155
156 /**
157 * initStates - called just after construction
158 */
159 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
160
161 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 * doTransaction - process the transaction. This is a good place to figure
163 * out which attributes of the surface have changed.
164 */
165 virtual uint32_t doTransaction(uint32_t transactionFlags);
166
167 /**
168 * setVisibleRegion - called to set the new visible region. This gives
169 * a chance to update the new visible region or record the fact it changed.
170 */
171 virtual void setVisibleRegion(const Region& visibleRegion);
172
173 /**
174 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700175 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 * (transparently or not) by another surface.
177 */
178 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 /**
Jesse Halla8026d22012-09-25 13:25:04 -0700181 * setVisibleNonTransparentRegion - called when the visible and
182 * non-transparent region changes.
183 */
184 virtual void setVisibleNonTransparentRegion(const Region&
185 visibleNonTransparentRegion);
186
187 /**
Mathias Agopian4fec8732012-06-29 14:12:52 -0700188 * latchBuffer - called each time the screen is redrawn and returns whether
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 * the visible regions need to be recomputed (this is a fairly heavy
190 * operation, so this should be set only if needed). Typically this is used
191 * to figure out if the content or size of a surface has changed.
192 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700193 virtual Region latchBuffer(bool& recomputeVisibleRegions);
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700196 * isOpaque - true if this surface is opaque
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700198 virtual bool isOpaque() const { return true; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199
200 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700201 * needsDithering - true if this surface needs dithering
202 */
203 virtual bool needsDithering() const { return false; }
204
205 /**
Mathias Agopiana67932f2011-04-20 14:20:59 -0700206 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopiana7f66922010-05-26 22:08:52 -0700207 */
Mathias Agopianeba8c682012-09-19 23:14:45 -0700208 virtual bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana7f66922010-05-26 22:08:52 -0700209
210 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700212 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 */
214 virtual bool isSecure() const { return false; }
215
Glenn Kasten16f04532011-01-19 15:27:27 -0800216 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800217 * isProtected - true if the layer may contain protected content in the
218 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800219 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800220 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800221
Mathias Agopianda27af92012-09-13 18:17:13 -0700222 /*
223 * isVisible - true if this layer is visibile, false otherwise
224 */
225 virtual bool isVisible() const;
226
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700227 /** called with the state lock when the surface is removed from the
228 * current list */
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800229 virtual void onRemoved() { }
Jamie Gennise8696a42012-01-15 18:54:57 -0800230
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800231 /** called after page-flip
232 */
Mathias Agopian42977342012-08-05 00:40:46 -0700233 virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
Mathias Agopianc3973602012-08-31 17:51:25 -0700234 HWComposer::HWCLayerInterface* layer);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800235
236 /** called before composition.
237 * returns true if the layer has pending updates.
238 */
239 virtual bool onPreComposition() { return false; }
Jamie Gennise8696a42012-01-15 18:54:57 -0800240
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700241 /** called before composition.
242 */
243 virtual void onPostComposition() { }
244
Andy McFadden69052052012-09-14 16:10:11 -0700245 /**
246 * Updates the SurfaceTexture's transform hint, for layers that have
247 * a SurfaceTexture.
248 */
Mathias Agopian84300952012-11-21 16:02:13 -0800249 virtual void updateTransformHint(const sp<const DisplayDevice>& hw) const { }
Andy McFadden69052052012-09-14 16:10:11 -0700250
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700251 /** always call base class first */
252 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800253 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800254 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800255 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700256
257
Mathias Agopian9a112062009-04-17 19:36:26 -0700258 enum { // flags for doTransaction()
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700259 eDontUpdateGeometryState = 0x00000001,
260 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700261 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262
263
264 inline const State& drawingState() const { return mDrawingState; }
265 inline const State& currentState() const { return mCurrentState; }
266 inline State& currentState() { return mCurrentState; }
267
Mathias Agopian42977342012-08-05 00:40:46 -0700268 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700269
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700270 void setFiltering(bool filtering);
271 bool getFiltering() const;
272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273protected:
Mathias Agopian42977342012-08-05 00:40:46 -0700274 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian1b031492012-06-20 17:51:20 -0700275 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
Mathias Agopian42977342012-08-05 00:40:46 -0700276 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700277
Mathias Agopian9a112062009-04-17 19:36:26 -0700278 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
Mathias Agopiana67932f2011-04-20 14:20:59 -0700280private:
281 // accessed only in the main thread
282 // Whether filtering is forced on or not
283 bool mFiltering;
Mathias Agopianb661d662010-08-19 17:01:19 -0700284
Mathias Agopiana67932f2011-04-20 14:20:59 -0700285 // Whether filtering is needed b/c of the drawingstate
Mathias Agopiana7f66922010-05-26 22:08:52 -0700286 bool mNeedsFiltering;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700287
288protected:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 // these are protected by an external lock
290 State mCurrentState;
291 State mDrawingState;
292 volatile int32_t mTransactionFlags;
293
294 // don't change, don't need a lock
295 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800296 String8 mName;
297 mutable bool mDebug;
298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopianca4d3602011-05-19 15:38:14 -0700300public:
301 // called from class SurfaceFlinger
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700302 virtual ~LayerBase();
303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700305 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306};
307
308
309// ---------------------------------------------------------------------------
310
311class LayerBaseClient : public LayerBase
312{
313public:
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700314 LayerBaseClient(SurfaceFlinger* flinger, const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316 virtual ~LayerBaseClient();
317
318 sp<ISurface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800319 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700320 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700321
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700322 virtual sp<LayerBaseClient> getLayerBaseClient() const {
323 return const_cast<LayerBaseClient*>(this); }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700324
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700325 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800326
Mathias Agopian96f08192010-06-02 23:28:45 -0700327 uint32_t getIdentity() const { return mIdentity; }
328
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700329protected:
330 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800331 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700332
Mathias Agopiana67932f2011-04-20 14:20:59 -0700333 class LayerCleaner {
334 sp<SurfaceFlinger> mFlinger;
335 wp<LayerBaseClient> mLayer;
336 protected:
337 ~LayerCleaner();
338 public:
339 LayerCleaner(const sp<SurfaceFlinger>& flinger,
340 const sp<LayerBaseClient>& layer);
341 };
342
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700343private:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700344 virtual sp<ISurface> createSurface();
345
Mathias Agopian96f08192010-06-02 23:28:45 -0700346 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800347 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800348 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700349 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700350 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700351 const uint32_t mIdentity;
352 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353};
354
355// ---------------------------------------------------------------------------
356
357}; // namespace android
358
359#endif // ANDROID_LAYER_BASE_H