blob: 8332a5af1efbfb5552c8f61d5e51db7fe1461b91 [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_H
18#define ANDROID_LAYER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian076b1cc2009-04-10 14:24:30 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025
Mathias Agopian13127d82013-03-05 17:47:11 -080026#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/Timers.h>
29
30#include <ui/GraphicBuffer.h>
31#include <ui/PixelFormat.h>
32#include <ui/Region.h>
33
34#include <gui/ISurfaceComposerClient.h>
35
36#include <private/gui/LayerState.h>
37
Jamie Gennis82dbc742012-11-08 19:23:28 -080038#include "FrameTracker.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080039#include "Client.h"
40#include "SurfaceFlinger.h"
41#include "SurfaceFlingerConsumer.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070042#include "SurfaceTextureLayer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include "Transform.h"
44
Mathias Agopian13127d82013-03-05 17:47:11 -080045#include "DisplayHardware/HWComposer.h"
Mathias Agopian6b442672013-07-09 21:24:52 -070046#include "DisplayHardware/FloatRect.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080047
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048namespace android {
49
50// ---------------------------------------------------------------------------
51
Mathias Agopian1f7bec62010-06-25 18:02:21 -070052class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020053class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080054class DisplayDevice;
55class GraphicBuffer;
56class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58// ---------------------------------------------------------------------------
59
Andy McFadden882e3a32013-01-08 16:06:15 -080060/*
Andy McFadden882e3a32013-01-08 16:06:15 -080061 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
62 * Layer is first referenced.
63 *
64 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
65 * that new data has arrived.
66 */
Mathias Agopian13127d82013-03-05 17:47:11 -080067class Layer : public SurfaceFlingerConsumer::FrameAvailableListener {
68 static int32_t sSequence;
69
Mathias Agopiand606de62010-05-10 20:06:11 -070070public:
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mutable bool contentDirty;
72 // regions below are in window-manager space
73 Region visibleRegion;
74 Region coveredRegion;
75 Region visibleNonTransparentRegion;
76 int32_t sequence;
77
78 enum { // flags for doTransaction()
79 eDontUpdateGeometryState = 0x00000001,
80 eVisibleRegion = 0x00000002,
81 };
82
83 struct Geometry {
84 uint32_t w;
85 uint32_t h;
86 Rect crop;
87 inline bool operator ==(const Geometry& rhs) const {
88 return (w == rhs.w && h == rhs.h && crop == rhs.crop);
89 }
90 inline bool operator !=(const Geometry& rhs) const {
91 return !operator ==(rhs);
92 }
93 };
94
95 struct State {
96 Geometry active;
97 Geometry requested;
98 uint32_t z;
99 uint32_t layerStack;
100 uint8_t alpha;
101 uint8_t flags;
102 uint8_t reserved[2];
103 int32_t sequence; // changes when visible regions can change
104 Transform transform;
Mathias Agopian2ca79392013-04-02 18:30:32 -0700105 // the transparentRegion hint is a bit special, it's latched only
106 // when we receive a buffer -- this is because it's "content"
107 // dependent.
108 Region activeTransparentRegion;
109 Region requestedTransparentRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800110 };
111
112 class LayerMesh {
113 friend class Layer;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700114 typedef GLfloat float2[2];
115 float2 mVertices[4];
Mathias Agopian13127d82013-03-05 17:47:11 -0800116 size_t mNumVertices;
117 public:
118 LayerMesh() :
119 mNumVertices(4) {
120 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700121 float2 const* getVertices() const {
122 return mVertices;
Mathias Agopian13127d82013-03-05 17:47:11 -0800123 }
124 size_t getVertexCount() const {
125 return mNumVertices;
126 }
127 };
128
129 // -----------------------------------------------------------------------
130
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
132 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700133
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700134 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700136 // the this layer's size and format
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700137 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
Mathias Agopian13127d82013-03-05 17:47:11 -0800139 // modify current state
140 bool setPosition(float x, float y);
141 bool setLayer(uint32_t z);
142 bool setSize(uint32_t w, uint32_t h);
143 bool setAlpha(uint8_t alpha);
144 bool setMatrix(const layer_state_t::matrix22_t& matrix);
145 bool setTransparentRegionHint(const Region& transparent);
146 bool setFlags(uint8_t flags, uint8_t mask);
147 bool setCrop(const Rect& crop);
148 bool setLayerStack(uint32_t layerStack);
149
Mathias Agopian13127d82013-03-05 17:47:11 -0800150 uint32_t getTransactionFlags(uint32_t flags);
151 uint32_t setTransactionFlags(uint32_t flags);
152
153 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
154 Rect computeBounds() const;
155
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700156 sp<IBinder> getHandle();
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700157 sp<IGraphicBufferProducer> getBufferQueue() const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700158 const String8& getName() const;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700159
Mathias Agopian13127d82013-03-05 17:47:11 -0800160 // -----------------------------------------------------------------------
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700161 // Virtuals
Mathias Agopian13127d82013-03-05 17:47:11 -0800162
Mathias Agopian13127d82013-03-05 17:47:11 -0800163 virtual const char* getTypeId() const { return "Layer"; }
164
Mathias Agopian13127d82013-03-05 17:47:11 -0800165 /*
166 * isOpaque - true if this surface is opaque
167 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700168 virtual bool isOpaque() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800169
170 /*
171 * isSecure - true if this surface is secure, that is if it prevents
172 * screenshots or VNC servers.
173 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800175
176 /*
177 * isProtected - true if the layer may contain protected content in the
178 * GRALLOC_USAGE_PROTECTED sense.
179 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800180 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800181
182 /*
183 * isVisible - true if this layer is visible, false otherwise
184 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700185 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186
Mathias Agopian13127d82013-03-05 17:47:11 -0800187 /*
188 * isFixedSize - true if content has a fixed size
189 */
190 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700191
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700192protected:
193 /*
194 * onDraw - draws the surface.
195 */
196 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
197
198public:
199 // -----------------------------------------------------------------------
200
201 void setGeometry(const sp<const DisplayDevice>& hw,
202 HWComposer::HWCLayerInterface& layer);
203 void setPerFrameData(const sp<const DisplayDevice>& hw,
204 HWComposer::HWCLayerInterface& layer);
205 void setAcquireFence(const sp<const DisplayDevice>& hw,
206 HWComposer::HWCLayerInterface& layer);
207
208 /*
209 * called after page-flip
210 */
211 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
212 HWComposer::HWCLayerInterface* layer);
213
214 /*
215 * called before composition.
216 * returns true if the layer has pending updates.
217 */
218 bool onPreComposition();
219
220 /*
221 * called after composition.
222 */
223 void onPostComposition();
224
225 /*
226 * draw - performs some global clipping optimizations
227 * and calls onDraw().
228 */
229 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
230 void draw(const sp<const DisplayDevice>& hw);
231
232 /*
233 * doTransaction - process the transaction. This is a good place to figure
234 * out which attributes of the surface have changed.
235 */
236 uint32_t doTransaction(uint32_t transactionFlags);
237
238 /*
239 * setVisibleRegion - called to set the new visible region. This gives
240 * a chance to update the new visible region or record the fact it changed.
241 */
242 void setVisibleRegion(const Region& visibleRegion);
243
244 /*
245 * setCoveredRegion - called when the covered region changes. The covered
246 * region corresponds to any area of the surface that is covered
247 * (transparently or not) by another surface.
248 */
249 void setCoveredRegion(const Region& coveredRegion);
250
251 /*
252 * setVisibleNonTransparentRegion - called when the visible and
253 * non-transparent region changes.
254 */
255 void setVisibleNonTransparentRegion(const Region&
256 visibleNonTransparentRegion);
257
258 /*
259 * latchBuffer - called each time the screen is redrawn and returns whether
260 * the visible regions need to be recomputed (this is a fairly heavy
261 * operation, so this should be set only if needed). Typically this is used
262 * to figure out if the content or size of a surface has changed.
263 */
264 Region latchBuffer(bool& recomputeVisibleRegions);
265
Mathias Agopian13127d82013-03-05 17:47:11 -0800266 /*
267 * called with the state lock when the surface is removed from the
268 * current list
269 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700270 void onRemoved();
Mathias Agopian13127d82013-03-05 17:47:11 -0800271
272
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800273 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800274 // the current orientation of the display device.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700275 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700276
Mathias Agopian13127d82013-03-05 17:47:11 -0800277 /*
278 * returns the rectangle that crops the content of the layer and scales it
279 * to the layer's size.
280 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700281 Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800282
Mathias Agopian13127d82013-03-05 17:47:11 -0800283 // -----------------------------------------------------------------------
284
285 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
286 void setFiltering(bool filtering);
287 bool getFiltering() const;
288
289 // only for debugging
290 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
291
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700292 inline const State& getDrawingState() const { return mDrawingState; }
293 inline const State& getCurrentState() const { return mCurrentState; }
294 inline State& getCurrentState() { return mCurrentState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800295
296
297 /* always call base class first */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700298 void dump(String8& result, Colorizer& colorizer) const;
299 void dumpStats(String8& result) const;
300 void clearStats();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700301 void logFrameStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700302
Mathias Agopian13127d82013-03-05 17:47:11 -0800303protected:
304 // constant
305 sp<SurfaceFlinger> mFlinger;
306
307 virtual void onFirstRef();
308
309 /*
310 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
311 * is called.
312 */
313 class LayerCleaner {
314 sp<SurfaceFlinger> mFlinger;
315 wp<Layer> mLayer;
316 protected:
317 ~LayerCleaner();
318 public:
319 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
320 };
321
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323private:
Mathias Agopian13127d82013-03-05 17:47:11 -0800324 // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
325 virtual void onFrameAvailable();
326
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700327 void commitTransaction();
328
329 // needsLinearFiltering - true if this surface's state requires filtering
330 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800331
Mathias Agopian3330b202009-10-05 17:07:12 -0700332 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian6b442672013-07-09 21:24:52 -0700333 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700334 bool isCropped() const;
335 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700336
Mathias Agopian13127d82013-03-05 17:47:11 -0800337 // drawing
338 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
339 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
340 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
341
Igor Murashkina4a31492012-10-29 13:36:11 -0700342
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700343 // -----------------------------------------------------------------------
344
Mathias Agopiana67932f2011-04-20 14:20:59 -0700345 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800346 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700347 sp<BufferQueue> mBufferQueue;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 GLuint mTextureName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800349 bool mPremultipliedAlpha;
350 String8 mName;
351 mutable bool mDebug;
352 PixelFormat mFormat;
Mathias Agopian13127d82013-03-05 17:47:11 -0800353 bool mOpaqueLayer;
354
355 // these are protected by an external lock
356 State mCurrentState;
357 State mDrawingState;
358 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700359
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700360 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700361 volatile int32_t mQueuedFrames;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800362 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700363
364 // main thread
365 sp<GraphicBuffer> mActiveBuffer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700366 Rect mCurrentCrop;
367 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700368 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700369 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800370 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800371 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800372 // Whether filtering is forced on or not
373 bool mFiltering;
374 // Whether filtering is needed b/c of the drawingstate
375 bool mNeedsFiltering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700376
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700377 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800378 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800379 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800380
381 // protected by mLock
382 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700383 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800384 mutable bool mHasSurface;
385 const wp<Client> mClientRef;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386};
387
388// ---------------------------------------------------------------------------
389
390}; // namespace android
391
392#endif // ANDROID_LAYER_H