blob: 424c03ed38891b03fc6dab2e0b17fae1cdddb242 [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
Svetoslavd85084b2014-03-20 10:28:31 -070030#include <ui/FrameStats.h>
Mathias Agopian13127d82013-03-05 17:47:11 -080031#include <ui/GraphicBuffer.h>
32#include <ui/PixelFormat.h>
33#include <ui/Region.h>
34
35#include <gui/ISurfaceComposerClient.h>
36
37#include <private/gui/LayerState.h>
38
Jamie Gennis82dbc742012-11-08 19:23:28 -080039#include "FrameTracker.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080040#include "Client.h"
Dan Stozab9b08832014-03-13 11:55:57 -070041#include "MonitoredProducer.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080042#include "SurfaceFlinger.h"
43#include "SurfaceFlingerConsumer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include "Transform.h"
45
Mathias Agopian13127d82013-03-05 17:47:11 -080046#include "DisplayHardware/HWComposer.h"
Mathias Agopian6b442672013-07-09 21:24:52 -070047#include "DisplayHardware/FloatRect.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070048#include "RenderEngine/Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070049#include "RenderEngine/Texture.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051namespace android {
52
53// ---------------------------------------------------------------------------
54
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020056class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080057class DisplayDevice;
58class GraphicBuffer;
59class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61// ---------------------------------------------------------------------------
62
Andy McFadden882e3a32013-01-08 16:06:15 -080063/*
Andy McFadden882e3a32013-01-08 16:06:15 -080064 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
65 * Layer is first referenced.
66 *
67 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
68 * that new data has arrived.
69 */
Jesse Hall399184a2014-03-03 15:42:54 -080070class Layer : public SurfaceFlingerConsumer::ContentsChangedListener {
Mathias Agopian13127d82013-03-05 17:47:11 -080071 static int32_t sSequence;
72
Mathias Agopiand606de62010-05-10 20:06:11 -070073public:
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mutable bool contentDirty;
75 // regions below are in window-manager space
76 Region visibleRegion;
77 Region coveredRegion;
78 Region visibleNonTransparentRegion;
Andy McFadden4df87bd2014-04-21 18:08:54 -070079
80 // Layer serial number. This gives layers an explicit ordering, so we
81 // have a stable sort order when their layer stack and Z-order are
82 // the same.
Mathias Agopian13127d82013-03-05 17:47:11 -080083 int32_t sequence;
84
85 enum { // flags for doTransaction()
86 eDontUpdateGeometryState = 0x00000001,
87 eVisibleRegion = 0x00000002,
88 };
89
90 struct Geometry {
91 uint32_t w;
92 uint32_t h;
93 Rect crop;
94 inline bool operator ==(const Geometry& rhs) const {
95 return (w == rhs.w && h == rhs.h && crop == rhs.crop);
96 }
97 inline bool operator !=(const Geometry& rhs) const {
98 return !operator ==(rhs);
99 }
100 };
101
102 struct State {
103 Geometry active;
104 Geometry requested;
105 uint32_t z;
106 uint32_t layerStack;
107 uint8_t alpha;
108 uint8_t flags;
109 uint8_t reserved[2];
110 int32_t sequence; // changes when visible regions can change
111 Transform transform;
Mathias Agopian2ca79392013-04-02 18:30:32 -0700112 // the transparentRegion hint is a bit special, it's latched only
113 // when we receive a buffer -- this is because it's "content"
114 // dependent.
115 Region activeTransparentRegion;
116 Region requestedTransparentRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800117 };
118
Mathias Agopian13127d82013-03-05 17:47:11 -0800119 // -----------------------------------------------------------------------
120
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
122 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700123
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700124 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700126 // the this layer's size and format
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700127 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128
Mathias Agopian13127d82013-03-05 17:47:11 -0800129 // modify current state
130 bool setPosition(float x, float y);
131 bool setLayer(uint32_t z);
132 bool setSize(uint32_t w, uint32_t h);
133 bool setAlpha(uint8_t alpha);
134 bool setMatrix(const layer_state_t::matrix22_t& matrix);
135 bool setTransparentRegionHint(const Region& transparent);
136 bool setFlags(uint8_t flags, uint8_t mask);
137 bool setCrop(const Rect& crop);
138 bool setLayerStack(uint32_t layerStack);
139
Mathias Agopian13127d82013-03-05 17:47:11 -0800140 uint32_t getTransactionFlags(uint32_t flags);
141 uint32_t setTransactionFlags(uint32_t flags);
142
Dan Stozac7014012014-02-14 15:03:43 -0800143 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
144 bool useIdentityTransform) const;
Michael Lentine6c925ed2014-09-26 17:55:01 -0700145 Rect computeBounds(const Region& activeTransparentRegion) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800146 Rect computeBounds() const;
147
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148 sp<IBinder> getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -0700149 sp<IGraphicBufferProducer> getProducer() const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700150 const String8& getName() const;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700151
Mathias Agopian13127d82013-03-05 17:47:11 -0800152 // -----------------------------------------------------------------------
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700153 // Virtuals
Mathias Agopian13127d82013-03-05 17:47:11 -0800154
Mathias Agopian13127d82013-03-05 17:47:11 -0800155 virtual const char* getTypeId() const { return "Layer"; }
156
Mathias Agopian13127d82013-03-05 17:47:11 -0800157 /*
158 * isOpaque - true if this surface is opaque
Andy McFadden4125a4f2014-01-29 17:17:11 -0800159 *
160 * This takes into account the buffer format (i.e. whether or not the
161 * pixel format includes an alpha channel) and the "opaque" flag set
162 * on the layer. It does not examine the current plane alpha value.
Mathias Agopian13127d82013-03-05 17:47:11 -0800163 */
Andy McFadden4125a4f2014-01-29 17:17:11 -0800164 virtual bool isOpaque(const Layer::State& s) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800165
166 /*
167 * isSecure - true if this surface is secure, that is if it prevents
168 * screenshots or VNC servers.
169 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800171
172 /*
173 * isProtected - true if the layer may contain protected content in the
174 * GRALLOC_USAGE_PROTECTED sense.
175 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800176 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800177
178 /*
179 * isVisible - true if this layer is visible, false otherwise
180 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700181 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182
Mathias Agopian13127d82013-03-05 17:47:11 -0800183 /*
184 * isFixedSize - true if content has a fixed size
185 */
186 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700187
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700188protected:
189 /*
190 * onDraw - draws the surface.
191 */
Dan Stozac7014012014-02-14 15:03:43 -0800192 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
193 bool useIdentityTransform) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700194
195public:
196 // -----------------------------------------------------------------------
197
198 void setGeometry(const sp<const DisplayDevice>& hw,
199 HWComposer::HWCLayerInterface& layer);
200 void setPerFrameData(const sp<const DisplayDevice>& hw,
201 HWComposer::HWCLayerInterface& layer);
202 void setAcquireFence(const sp<const DisplayDevice>& hw,
203 HWComposer::HWCLayerInterface& layer);
204
Riley Andrews03414a12014-07-01 14:22:59 -0700205 Rect getPosition(const sp<const DisplayDevice>& hw);
206
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700207 /*
208 * called after page-flip
209 */
210 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
211 HWComposer::HWCLayerInterface* layer);
212
Dan Stoza6b9454d2014-11-07 16:00:59 -0800213 bool shouldPresentNow(const DispSync& dispSync) const;
214
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700215 /*
216 * called before composition.
217 * returns true if the layer has pending updates.
218 */
219 bool onPreComposition();
220
221 /*
222 * called after composition.
223 */
224 void onPostComposition();
225
226 /*
227 * draw - performs some global clipping optimizations
228 * and calls onDraw().
229 */
230 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
Dan Stozac7014012014-02-14 15:03:43 -0800231 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const;
232 void draw(const sp<const DisplayDevice>& hw) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700233
234 /*
235 * doTransaction - process the transaction. This is a good place to figure
236 * out which attributes of the surface have changed.
237 */
238 uint32_t doTransaction(uint32_t transactionFlags);
239
240 /*
241 * setVisibleRegion - called to set the new visible region. This gives
242 * a chance to update the new visible region or record the fact it changed.
243 */
244 void setVisibleRegion(const Region& visibleRegion);
245
246 /*
247 * setCoveredRegion - called when the covered region changes. The covered
248 * region corresponds to any area of the surface that is covered
249 * (transparently or not) by another surface.
250 */
251 void setCoveredRegion(const Region& coveredRegion);
252
253 /*
254 * setVisibleNonTransparentRegion - called when the visible and
255 * non-transparent region changes.
256 */
257 void setVisibleNonTransparentRegion(const Region&
258 visibleNonTransparentRegion);
259
260 /*
261 * latchBuffer - called each time the screen is redrawn and returns whether
262 * the visible regions need to be recomputed (this is a fairly heavy
263 * operation, so this should be set only if needed). Typically this is used
264 * to figure out if the content or size of a surface has changed.
265 */
266 Region latchBuffer(bool& recomputeVisibleRegions);
267
Riley Andrews03414a12014-07-01 14:22:59 -0700268 bool isPotentialCursor() const { return mPotentialCursor;}
269
Mathias Agopian13127d82013-03-05 17:47:11 -0800270 /*
271 * called with the state lock when the surface is removed from the
272 * current list
273 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700274 void onRemoved();
Mathias Agopian13127d82013-03-05 17:47:11 -0800275
276
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800277 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800278 // the current orientation of the display device.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700279 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700280
Mathias Agopian13127d82013-03-05 17:47:11 -0800281 /*
282 * returns the rectangle that crops the content of the layer and scales it
283 * to the layer's size.
284 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700285 Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800286
Eric Penner51c59cd2014-07-28 19:51:58 -0700287 /*
288 * Returns if a frame is queued.
289 */
chenhg0ea1cb92014-09-23 10:52:25 +0800290 bool hasQueuedFrame() const { return mQueuedFrames > 0 || mSidebandStreamChanged; }
Eric Penner51c59cd2014-07-28 19:51:58 -0700291
Mathias Agopian13127d82013-03-05 17:47:11 -0800292 // -----------------------------------------------------------------------
293
294 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
295 void setFiltering(bool filtering);
296 bool getFiltering() const;
297
298 // only for debugging
299 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
300
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700301 inline const State& getDrawingState() const { return mDrawingState; }
302 inline const State& getCurrentState() const { return mCurrentState; }
303 inline State& getCurrentState() { return mCurrentState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800304
305
306 /* always call base class first */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700307 void dump(String8& result, Colorizer& colorizer) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700308 void dumpFrameStats(String8& result) const;
309 void clearFrameStats();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700310 void logFrameStats();
Svetoslavd85084b2014-03-20 10:28:31 -0700311 void getFrameStats(FrameStats* outStats) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700312
Mathias Agopian13127d82013-03-05 17:47:11 -0800313protected:
314 // constant
315 sp<SurfaceFlinger> mFlinger;
316
317 virtual void onFirstRef();
318
319 /*
320 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
321 * is called.
322 */
323 class LayerCleaner {
324 sp<SurfaceFlinger> mFlinger;
325 wp<Layer> mLayer;
326 protected:
327 ~LayerCleaner();
328 public:
329 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
330 };
331
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800332
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333private:
Jesse Hall399184a2014-03-03 15:42:54 -0800334 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
Dan Stoza8dc55392014-11-04 11:37:46 -0800335 virtual void onFrameAvailable(const BufferItem& item);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800336 virtual void onFrameReplaced(const BufferItem& item);
Jesse Hall399184a2014-03-03 15:42:54 -0800337 virtual void onSidebandStreamChanged();
Mathias Agopian13127d82013-03-05 17:47:11 -0800338
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700339 void commitTransaction();
340
341 // needsLinearFiltering - true if this surface's state requires filtering
342 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800343
Mathias Agopian3330b202009-10-05 17:07:12 -0700344 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian6b442672013-07-09 21:24:52 -0700345 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700346 bool isCropped() const;
347 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700348
Mathias Agopian13127d82013-03-05 17:47:11 -0800349 // drawing
350 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian3f844832013-08-07 21:24:32 -0700351 float r, float g, float b, float alpha) const;
Dan Stozac7014012014-02-14 15:03:43 -0800352 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
353 bool useIdentityTransform) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800354
Ruben Brunk1681d952014-06-27 15:51:55 -0700355 // Temporary - Used only for LEGACY camera mode.
356 uint32_t getProducerStickyTransform() const;
357
Igor Murashkina4a31492012-10-29 13:36:11 -0700358
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700359 // -----------------------------------------------------------------------
360
Mathias Agopiana67932f2011-04-20 14:20:59 -0700361 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800362 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700363 sp<IGraphicBufferProducer> mProducer;
Andy McFadden4df87bd2014-04-21 18:08:54 -0700364 uint32_t mTextureName; // from GLES
Mathias Agopian13127d82013-03-05 17:47:11 -0800365 bool mPremultipliedAlpha;
366 String8 mName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800367 PixelFormat mFormat;
Mathias Agopian13127d82013-03-05 17:47:11 -0800368
369 // these are protected by an external lock
370 State mCurrentState;
371 State mDrawingState;
372 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700373
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700374 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700375 volatile int32_t mQueuedFrames;
Jesse Hall399184a2014-03-03 15:42:54 -0800376 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800377 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700378
379 // main thread
380 sp<GraphicBuffer> mActiveBuffer;
Jesse Hall399184a2014-03-03 15:42:54 -0800381 sp<NativeHandle> mSidebandStream;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382 Rect mCurrentCrop;
383 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700384 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700385 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800386 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800387 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800388 // Whether filtering is forced on or not
389 bool mFiltering;
390 // Whether filtering is needed b/c of the drawingstate
391 bool mNeedsFiltering;
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700392 // The mesh used to draw the layer in GLES composition mode
393 mutable Mesh mMesh;
Andy McFadden4df87bd2014-04-21 18:08:54 -0700394 // The texture used to draw the layer in GLES composition mode
Mathias Agopian49457ac2013-08-14 18:20:17 -0700395 mutable Texture mTexture;
Mathias Agopiand606de62010-05-10 20:06:11 -0700396
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700397 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800398 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800399 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800400
401 // protected by mLock
402 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700403 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800404 mutable bool mHasSurface;
405 const wp<Client> mClientRef;
Riley Andrews03414a12014-07-01 14:22:59 -0700406
407 // This layer can be a cursor on some displays.
408 bool mPotentialCursor;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800409
410 // Local copy of the queued contents of the incoming BufferQueue
411 mutable Mutex mQueueItemLock;
412 Vector<BufferItem> mQueueItems;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413};
414
415// ---------------------------------------------------------------------------
416
417}; // namespace android
418
419#endif // ANDROID_LAYER_H