blob: 8f8989e933dc8d95e354b6de9606c1cc1356eb20 [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"
Dan Stozab9b08832014-03-13 11:55:57 -070040#include "MonitoredProducer.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080041#include "SurfaceFlinger.h"
42#include "SurfaceFlingerConsumer.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 Agopian3f844832013-08-07 21:24:32 -070047#include "RenderEngine/Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070048#include "RenderEngine/Texture.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050namespace android {
51
52// ---------------------------------------------------------------------------
53
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020055class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080056class DisplayDevice;
57class GraphicBuffer;
58class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60// ---------------------------------------------------------------------------
61
Andy McFadden882e3a32013-01-08 16:06:15 -080062/*
Andy McFadden882e3a32013-01-08 16:06:15 -080063 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
64 * Layer is first referenced.
65 *
66 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
67 * that new data has arrived.
68 */
Jesse Hall399184a2014-03-03 15:42:54 -080069class Layer : public SurfaceFlingerConsumer::ContentsChangedListener {
Mathias Agopian13127d82013-03-05 17:47:11 -080070 static int32_t sSequence;
71
Mathias Agopiand606de62010-05-10 20:06:11 -070072public:
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mutable bool contentDirty;
74 // regions below are in window-manager space
75 Region visibleRegion;
76 Region coveredRegion;
77 Region visibleNonTransparentRegion;
78 int32_t sequence;
79
80 enum { // flags for doTransaction()
81 eDontUpdateGeometryState = 0x00000001,
82 eVisibleRegion = 0x00000002,
83 };
84
85 struct Geometry {
86 uint32_t w;
87 uint32_t h;
88 Rect crop;
89 inline bool operator ==(const Geometry& rhs) const {
90 return (w == rhs.w && h == rhs.h && crop == rhs.crop);
91 }
92 inline bool operator !=(const Geometry& rhs) const {
93 return !operator ==(rhs);
94 }
95 };
96
97 struct State {
98 Geometry active;
99 Geometry requested;
100 uint32_t z;
101 uint32_t layerStack;
102 uint8_t alpha;
103 uint8_t flags;
104 uint8_t reserved[2];
105 int32_t sequence; // changes when visible regions can change
106 Transform transform;
Mathias Agopian2ca79392013-04-02 18:30:32 -0700107 // the transparentRegion hint is a bit special, it's latched only
108 // when we receive a buffer -- this is because it's "content"
109 // dependent.
110 Region activeTransparentRegion;
111 Region requestedTransparentRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800112 };
113
Mathias Agopian13127d82013-03-05 17:47:11 -0800114 // -----------------------------------------------------------------------
115
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
117 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700118
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700121 // the this layer's size and format
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
Mathias Agopian13127d82013-03-05 17:47:11 -0800124 // modify current state
125 bool setPosition(float x, float y);
126 bool setLayer(uint32_t z);
127 bool setSize(uint32_t w, uint32_t h);
128 bool setAlpha(uint8_t alpha);
129 bool setMatrix(const layer_state_t::matrix22_t& matrix);
130 bool setTransparentRegionHint(const Region& transparent);
131 bool setFlags(uint8_t flags, uint8_t mask);
132 bool setCrop(const Rect& crop);
133 bool setLayerStack(uint32_t layerStack);
134
Mathias Agopian13127d82013-03-05 17:47:11 -0800135 uint32_t getTransactionFlags(uint32_t flags);
136 uint32_t setTransactionFlags(uint32_t flags);
137
Dan Stozac7014012014-02-14 15:03:43 -0800138 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
139 bool useIdentityTransform) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800140 Rect computeBounds() const;
141
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700142 sp<IBinder> getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -0700143 sp<IGraphicBufferProducer> getProducer() const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700144 const String8& getName() const;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145
Mathias Agopian13127d82013-03-05 17:47:11 -0800146 // -----------------------------------------------------------------------
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700147 // Virtuals
Mathias Agopian13127d82013-03-05 17:47:11 -0800148
Mathias Agopian13127d82013-03-05 17:47:11 -0800149 virtual const char* getTypeId() const { return "Layer"; }
150
Mathias Agopian13127d82013-03-05 17:47:11 -0800151 /*
152 * isOpaque - true if this surface is opaque
Andy McFadden4125a4f2014-01-29 17:17:11 -0800153 *
154 * This takes into account the buffer format (i.e. whether or not the
155 * pixel format includes an alpha channel) and the "opaque" flag set
156 * on the layer. It does not examine the current plane alpha value.
Mathias Agopian13127d82013-03-05 17:47:11 -0800157 */
Andy McFadden4125a4f2014-01-29 17:17:11 -0800158 virtual bool isOpaque(const Layer::State& s) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800159
160 /*
161 * isSecure - true if this surface is secure, that is if it prevents
162 * screenshots or VNC servers.
163 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800165
166 /*
167 * isProtected - true if the layer may contain protected content in the
168 * GRALLOC_USAGE_PROTECTED sense.
169 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800170 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800171
172 /*
173 * isVisible - true if this layer is visible, false otherwise
174 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700175 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
Mathias Agopian13127d82013-03-05 17:47:11 -0800177 /*
178 * isFixedSize - true if content has a fixed size
179 */
180 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700181
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700182protected:
183 /*
184 * onDraw - draws the surface.
185 */
Dan Stozac7014012014-02-14 15:03:43 -0800186 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
187 bool useIdentityTransform) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700188
189public:
190 // -----------------------------------------------------------------------
191
192 void setGeometry(const sp<const DisplayDevice>& hw,
193 HWComposer::HWCLayerInterface& layer);
194 void setPerFrameData(const sp<const DisplayDevice>& hw,
195 HWComposer::HWCLayerInterface& layer);
196 void setAcquireFence(const sp<const DisplayDevice>& hw,
197 HWComposer::HWCLayerInterface& layer);
198
199 /*
200 * called after page-flip
201 */
202 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
203 HWComposer::HWCLayerInterface* layer);
204
205 /*
206 * called before composition.
207 * returns true if the layer has pending updates.
208 */
209 bool onPreComposition();
210
211 /*
212 * called after composition.
213 */
214 void onPostComposition();
215
216 /*
217 * draw - performs some global clipping optimizations
218 * and calls onDraw().
219 */
220 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
Dan Stozac7014012014-02-14 15:03:43 -0800221 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const;
222 void draw(const sp<const DisplayDevice>& hw) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700223
224 /*
225 * doTransaction - process the transaction. This is a good place to figure
226 * out which attributes of the surface have changed.
227 */
228 uint32_t doTransaction(uint32_t transactionFlags);
229
230 /*
231 * setVisibleRegion - called to set the new visible region. This gives
232 * a chance to update the new visible region or record the fact it changed.
233 */
234 void setVisibleRegion(const Region& visibleRegion);
235
236 /*
237 * setCoveredRegion - called when the covered region changes. The covered
238 * region corresponds to any area of the surface that is covered
239 * (transparently or not) by another surface.
240 */
241 void setCoveredRegion(const Region& coveredRegion);
242
243 /*
244 * setVisibleNonTransparentRegion - called when the visible and
245 * non-transparent region changes.
246 */
247 void setVisibleNonTransparentRegion(const Region&
248 visibleNonTransparentRegion);
249
250 /*
251 * latchBuffer - called each time the screen is redrawn and returns whether
252 * the visible regions need to be recomputed (this is a fairly heavy
253 * operation, so this should be set only if needed). Typically this is used
254 * to figure out if the content or size of a surface has changed.
255 */
256 Region latchBuffer(bool& recomputeVisibleRegions);
257
Mathias Agopian13127d82013-03-05 17:47:11 -0800258 /*
259 * called with the state lock when the surface is removed from the
260 * current list
261 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700262 void onRemoved();
Mathias Agopian13127d82013-03-05 17:47:11 -0800263
264
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800265 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800266 // the current orientation of the display device.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700267 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700268
Mathias Agopian13127d82013-03-05 17:47:11 -0800269 /*
270 * returns the rectangle that crops the content of the layer and scales it
271 * to the layer's size.
272 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700273 Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800274
Mathias Agopian13127d82013-03-05 17:47:11 -0800275 // -----------------------------------------------------------------------
276
277 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
278 void setFiltering(bool filtering);
279 bool getFiltering() const;
280
281 // only for debugging
282 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
283
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700284 inline const State& getDrawingState() const { return mDrawingState; }
285 inline const State& getCurrentState() const { return mCurrentState; }
286 inline State& getCurrentState() { return mCurrentState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800287
288
289 /* always call base class first */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700290 void dump(String8& result, Colorizer& colorizer) const;
291 void dumpStats(String8& result) const;
292 void clearStats();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700293 void logFrameStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700294
Mathias Agopian13127d82013-03-05 17:47:11 -0800295protected:
296 // constant
297 sp<SurfaceFlinger> mFlinger;
298
299 virtual void onFirstRef();
300
301 /*
302 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
303 * is called.
304 */
305 class LayerCleaner {
306 sp<SurfaceFlinger> mFlinger;
307 wp<Layer> mLayer;
308 protected:
309 ~LayerCleaner();
310 public:
311 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
312 };
313
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315private:
Jesse Hall399184a2014-03-03 15:42:54 -0800316 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
Mathias Agopian13127d82013-03-05 17:47:11 -0800317 virtual void onFrameAvailable();
Jesse Hall399184a2014-03-03 15:42:54 -0800318 virtual void onSidebandStreamChanged();
Mathias Agopian13127d82013-03-05 17:47:11 -0800319
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700320 void commitTransaction();
321
322 // needsLinearFiltering - true if this surface's state requires filtering
323 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800324
Mathias Agopian3330b202009-10-05 17:07:12 -0700325 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian6b442672013-07-09 21:24:52 -0700326 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700327 bool isCropped() const;
328 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700329
Mathias Agopian13127d82013-03-05 17:47:11 -0800330 // drawing
331 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian3f844832013-08-07 21:24:32 -0700332 float r, float g, float b, float alpha) const;
Dan Stozac7014012014-02-14 15:03:43 -0800333 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
334 bool useIdentityTransform) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800335
Igor Murashkina4a31492012-10-29 13:36:11 -0700336
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700337 // -----------------------------------------------------------------------
338
Mathias Agopiana67932f2011-04-20 14:20:59 -0700339 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800340 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700341 sp<IGraphicBufferProducer> mProducer;
Mathias Agopian3f844832013-08-07 21:24:32 -0700342 uint32_t mTextureName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800343 bool mPremultipliedAlpha;
344 String8 mName;
345 mutable bool mDebug;
346 PixelFormat mFormat;
Mathias Agopian13127d82013-03-05 17:47:11 -0800347
348 // these are protected by an external lock
349 State mCurrentState;
350 State mDrawingState;
351 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700352
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700353 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700354 volatile int32_t mQueuedFrames;
Jesse Hall399184a2014-03-03 15:42:54 -0800355 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800356 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700357
358 // main thread
359 sp<GraphicBuffer> mActiveBuffer;
Jesse Hall399184a2014-03-03 15:42:54 -0800360 sp<NativeHandle> mSidebandStream;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700361 Rect mCurrentCrop;
362 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700363 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700364 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800365 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800366 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800367 // Whether filtering is forced on or not
368 bool mFiltering;
369 // Whether filtering is needed b/c of the drawingstate
370 bool mNeedsFiltering;
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700371 // The mesh used to draw the layer in GLES composition mode
372 mutable Mesh mMesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700373 // The mesh used to draw the layer in GLES composition mode
374 mutable Texture mTexture;
Mathias Agopiand606de62010-05-10 20:06:11 -0700375
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700376 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800377 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800378 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800379
380 // protected by mLock
381 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700382 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800383 mutable bool mHasSurface;
384 const wp<Client> mClientRef;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385};
386
387// ---------------------------------------------------------------------------
388
389}; // namespace android
390
391#endif // ANDROID_LAYER_H