blob: 62970c3048c54f140a8c95a180ec6ed2e0d4c9e7 [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;
79 int32_t sequence;
80
81 enum { // flags for doTransaction()
82 eDontUpdateGeometryState = 0x00000001,
83 eVisibleRegion = 0x00000002,
84 };
85
86 struct Geometry {
87 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 }
96 };
97
98 struct State {
99 Geometry active;
100 Geometry requested;
101 uint32_t z;
102 uint32_t layerStack;
103 uint8_t alpha;
104 uint8_t flags;
105 uint8_t reserved[2];
106 int32_t sequence; // changes when visible regions can change
107 Transform transform;
Mathias Agopian2ca79392013-04-02 18:30:32 -0700108 // the transparentRegion hint is a bit special, it's latched only
109 // when we receive a buffer -- this is because it's "content"
110 // dependent.
111 Region activeTransparentRegion;
112 Region requestedTransparentRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800113 };
114
Mathias Agopian13127d82013-03-05 17:47:11 -0800115 // -----------------------------------------------------------------------
116
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
118 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700119
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700122 // the this layer's size and format
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124
Mathias Agopian13127d82013-03-05 17:47:11 -0800125 // modify current state
126 bool setPosition(float x, float y);
127 bool setLayer(uint32_t z);
128 bool setSize(uint32_t w, uint32_t h);
129 bool setAlpha(uint8_t alpha);
130 bool setMatrix(const layer_state_t::matrix22_t& matrix);
131 bool setTransparentRegionHint(const Region& transparent);
132 bool setFlags(uint8_t flags, uint8_t mask);
133 bool setCrop(const Rect& crop);
134 bool setLayerStack(uint32_t layerStack);
135
Mathias Agopian13127d82013-03-05 17:47:11 -0800136 uint32_t getTransactionFlags(uint32_t flags);
137 uint32_t setTransactionFlags(uint32_t flags);
138
Dan Stozac7014012014-02-14 15:03:43 -0800139 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
140 bool useIdentityTransform) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800141 Rect computeBounds() const;
142
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700143 sp<IBinder> getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -0700144 sp<IGraphicBufferProducer> getProducer() const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700145 const String8& getName() const;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700146
Mathias Agopian13127d82013-03-05 17:47:11 -0800147 // -----------------------------------------------------------------------
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700148 // Virtuals
Mathias Agopian13127d82013-03-05 17:47:11 -0800149
Mathias Agopian13127d82013-03-05 17:47:11 -0800150 virtual const char* getTypeId() const { return "Layer"; }
151
Mathias Agopian13127d82013-03-05 17:47:11 -0800152 /*
153 * isOpaque - true if this surface is opaque
Andy McFadden4125a4f2014-01-29 17:17:11 -0800154 *
155 * This takes into account the buffer format (i.e. whether or not the
156 * pixel format includes an alpha channel) and the "opaque" flag set
157 * on the layer. It does not examine the current plane alpha value.
Mathias Agopian13127d82013-03-05 17:47:11 -0800158 */
Andy McFadden4125a4f2014-01-29 17:17:11 -0800159 virtual bool isOpaque(const Layer::State& s) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800160
161 /*
162 * isSecure - true if this surface is secure, that is if it prevents
163 * screenshots or VNC servers.
164 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800166
167 /*
168 * isProtected - true if the layer may contain protected content in the
169 * GRALLOC_USAGE_PROTECTED sense.
170 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800171 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800172
173 /*
174 * isVisible - true if this layer is visible, false otherwise
175 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700176 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177
Mathias Agopian13127d82013-03-05 17:47:11 -0800178 /*
179 * isFixedSize - true if content has a fixed size
180 */
181 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700182
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700183protected:
184 /*
185 * onDraw - draws the surface.
186 */
Dan Stozac7014012014-02-14 15:03:43 -0800187 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
188 bool useIdentityTransform) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700189
190public:
191 // -----------------------------------------------------------------------
192
193 void setGeometry(const sp<const DisplayDevice>& hw,
194 HWComposer::HWCLayerInterface& layer);
195 void setPerFrameData(const sp<const DisplayDevice>& hw,
196 HWComposer::HWCLayerInterface& layer);
197 void setAcquireFence(const sp<const DisplayDevice>& hw,
198 HWComposer::HWCLayerInterface& layer);
199
200 /*
201 * called after page-flip
202 */
203 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
204 HWComposer::HWCLayerInterface* layer);
205
206 /*
207 * called before composition.
208 * returns true if the layer has pending updates.
209 */
210 bool onPreComposition();
211
212 /*
213 * called after composition.
214 */
215 void onPostComposition();
216
217 /*
218 * draw - performs some global clipping optimizations
219 * and calls onDraw().
220 */
221 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
Dan Stozac7014012014-02-14 15:03:43 -0800222 void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const;
223 void draw(const sp<const DisplayDevice>& hw) const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700224
225 /*
226 * doTransaction - process the transaction. This is a good place to figure
227 * out which attributes of the surface have changed.
228 */
229 uint32_t doTransaction(uint32_t transactionFlags);
230
231 /*
232 * setVisibleRegion - called to set the new visible region. This gives
233 * a chance to update the new visible region or record the fact it changed.
234 */
235 void setVisibleRegion(const Region& visibleRegion);
236
237 /*
238 * setCoveredRegion - called when the covered region changes. The covered
239 * region corresponds to any area of the surface that is covered
240 * (transparently or not) by another surface.
241 */
242 void setCoveredRegion(const Region& coveredRegion);
243
244 /*
245 * setVisibleNonTransparentRegion - called when the visible and
246 * non-transparent region changes.
247 */
248 void setVisibleNonTransparentRegion(const Region&
249 visibleNonTransparentRegion);
250
251 /*
252 * latchBuffer - called each time the screen is redrawn and returns whether
253 * the visible regions need to be recomputed (this is a fairly heavy
254 * operation, so this should be set only if needed). Typically this is used
255 * to figure out if the content or size of a surface has changed.
256 */
257 Region latchBuffer(bool& recomputeVisibleRegions);
258
Mathias Agopian13127d82013-03-05 17:47:11 -0800259 /*
260 * called with the state lock when the surface is removed from the
261 * current list
262 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700263 void onRemoved();
Mathias Agopian13127d82013-03-05 17:47:11 -0800264
265
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800266 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800267 // the current orientation of the display device.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700268 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700269
Mathias Agopian13127d82013-03-05 17:47:11 -0800270 /*
271 * returns the rectangle that crops the content of the layer and scales it
272 * to the layer's size.
273 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700274 Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800275
Mathias Agopian13127d82013-03-05 17:47:11 -0800276 // -----------------------------------------------------------------------
277
278 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
279 void setFiltering(bool filtering);
280 bool getFiltering() const;
281
282 // only for debugging
283 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
284
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700285 inline const State& getDrawingState() const { return mDrawingState; }
286 inline const State& getCurrentState() const { return mCurrentState; }
287 inline State& getCurrentState() { return mCurrentState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800288
289
290 /* always call base class first */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700291 void dump(String8& result, Colorizer& colorizer) const;
Svetoslavd85084b2014-03-20 10:28:31 -0700292 void dumpFrameStats(String8& result) const;
293 void clearFrameStats();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700294 void logFrameStats();
Svetoslavd85084b2014-03-20 10:28:31 -0700295 void getFrameStats(FrameStats* outStats) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700296
Mathias Agopian13127d82013-03-05 17:47:11 -0800297protected:
298 // constant
299 sp<SurfaceFlinger> mFlinger;
300
301 virtual void onFirstRef();
302
303 /*
304 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
305 * is called.
306 */
307 class LayerCleaner {
308 sp<SurfaceFlinger> mFlinger;
309 wp<Layer> mLayer;
310 protected:
311 ~LayerCleaner();
312 public:
313 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
314 };
315
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317private:
Jesse Hall399184a2014-03-03 15:42:54 -0800318 // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
Mathias Agopian13127d82013-03-05 17:47:11 -0800319 virtual void onFrameAvailable();
Jesse Hall399184a2014-03-03 15:42:54 -0800320 virtual void onSidebandStreamChanged();
Mathias Agopian13127d82013-03-05 17:47:11 -0800321
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700322 void commitTransaction();
323
324 // needsLinearFiltering - true if this surface's state requires filtering
325 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800326
Mathias Agopian3330b202009-10-05 17:07:12 -0700327 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian6b442672013-07-09 21:24:52 -0700328 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700329 bool isCropped() const;
330 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700331
Mathias Agopian13127d82013-03-05 17:47:11 -0800332 // drawing
333 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian3f844832013-08-07 21:24:32 -0700334 float r, float g, float b, float alpha) const;
Dan Stozac7014012014-02-14 15:03:43 -0800335 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
336 bool useIdentityTransform) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800337
Igor Murashkina4a31492012-10-29 13:36:11 -0700338
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700339 // -----------------------------------------------------------------------
340
Mathias Agopiana67932f2011-04-20 14:20:59 -0700341 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800342 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Dan Stozab9b08832014-03-13 11:55:57 -0700343 sp<IGraphicBufferProducer> mProducer;
Mathias Agopian3f844832013-08-07 21:24:32 -0700344 uint32_t mTextureName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800345 bool mPremultipliedAlpha;
346 String8 mName;
347 mutable bool mDebug;
348 PixelFormat mFormat;
Mathias Agopian13127d82013-03-05 17:47:11 -0800349
350 // these are protected by an external lock
351 State mCurrentState;
352 State mDrawingState;
353 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700354
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700355 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700356 volatile int32_t mQueuedFrames;
Jesse Hall399184a2014-03-03 15:42:54 -0800357 volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800358 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700359
360 // main thread
361 sp<GraphicBuffer> mActiveBuffer;
Jesse Hall399184a2014-03-03 15:42:54 -0800362 sp<NativeHandle> mSidebandStream;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700363 Rect mCurrentCrop;
364 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700365 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700366 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800367 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800368 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800369 // Whether filtering is forced on or not
370 bool mFiltering;
371 // Whether filtering is needed b/c of the drawingstate
372 bool mNeedsFiltering;
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700373 // The mesh used to draw the layer in GLES composition mode
374 mutable Mesh mMesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700375 // The mesh used to draw the layer in GLES composition mode
376 mutable Texture mTexture;
Mathias Agopiand606de62010-05-10 20:06:11 -0700377
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700378 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800379 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800380 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800381
382 // protected by mLock
383 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700384 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800385 mutable bool mHasSurface;
386 const wp<Client> mClientRef;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387};
388
389// ---------------------------------------------------------------------------
390
391}; // namespace android
392
393#endif // ANDROID_LAYER_H