blob: e7e9585499a8e790feebc14868db48c6b5109cef [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"
46
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047namespace android {
48
49// ---------------------------------------------------------------------------
50
Mathias Agopian1f7bec62010-06-25 18:02:21 -070051class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020052class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080053class DisplayDevice;
54class GraphicBuffer;
55class SurfaceFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
57// ---------------------------------------------------------------------------
58
Andy McFadden882e3a32013-01-08 16:06:15 -080059/*
Andy McFadden882e3a32013-01-08 16:06:15 -080060 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
61 * Layer is first referenced.
62 *
63 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
64 * that new data has arrived.
65 */
Mathias Agopian13127d82013-03-05 17:47:11 -080066class Layer : public SurfaceFlingerConsumer::FrameAvailableListener {
67 static int32_t sSequence;
68
Mathias Agopiand606de62010-05-10 20:06:11 -070069public:
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mutable bool contentDirty;
71 // regions below are in window-manager space
72 Region visibleRegion;
73 Region coveredRegion;
74 Region visibleNonTransparentRegion;
75 int32_t sequence;
76
77 enum { // flags for doTransaction()
78 eDontUpdateGeometryState = 0x00000001,
79 eVisibleRegion = 0x00000002,
80 };
81
82 struct Geometry {
83 uint32_t w;
84 uint32_t h;
85 Rect crop;
86 inline bool operator ==(const Geometry& rhs) const {
87 return (w == rhs.w && h == rhs.h && crop == rhs.crop);
88 }
89 inline bool operator !=(const Geometry& rhs) const {
90 return !operator ==(rhs);
91 }
92 };
93
94 struct State {
95 Geometry active;
96 Geometry requested;
97 uint32_t z;
98 uint32_t layerStack;
99 uint8_t alpha;
100 uint8_t flags;
101 uint8_t reserved[2];
102 int32_t sequence; // changes when visible regions can change
103 Transform transform;
Mathias Agopian2ca79392013-04-02 18:30:32 -0700104 // the transparentRegion hint is a bit special, it's latched only
105 // when we receive a buffer -- this is because it's "content"
106 // dependent.
107 Region activeTransparentRegion;
108 Region requestedTransparentRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800109 };
110
111 class LayerMesh {
112 friend class Layer;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700113 typedef GLfloat float2[2];
114 float2 mVertices[4];
Mathias Agopian13127d82013-03-05 17:47:11 -0800115 size_t mNumVertices;
116 public:
117 LayerMesh() :
118 mNumVertices(4) {
119 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700120 float2 const* getVertices() const {
121 return mVertices;
Mathias Agopian13127d82013-03-05 17:47:11 -0800122 }
123 size_t getVertexCount() const {
124 return mNumVertices;
125 }
126 };
127
128 // -----------------------------------------------------------------------
129
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
131 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700132
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700133 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700135 // the this layer's size and format
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137
Mathias Agopian13127d82013-03-05 17:47:11 -0800138 // modify current state
139 bool setPosition(float x, float y);
140 bool setLayer(uint32_t z);
141 bool setSize(uint32_t w, uint32_t h);
142 bool setAlpha(uint8_t alpha);
143 bool setMatrix(const layer_state_t::matrix22_t& matrix);
144 bool setTransparentRegionHint(const Region& transparent);
145 bool setFlags(uint8_t flags, uint8_t mask);
146 bool setCrop(const Rect& crop);
147 bool setLayerStack(uint32_t layerStack);
148
Mathias Agopian13127d82013-03-05 17:47:11 -0800149 uint32_t getTransactionFlags(uint32_t flags);
150 uint32_t setTransactionFlags(uint32_t flags);
151
152 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
153 Rect computeBounds() const;
154
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700155 sp<IBinder> getHandle();
156 sp<BufferQueue> getBufferQueue() const;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700157 const String8& getName() const;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700158
Mathias Agopian13127d82013-03-05 17:47:11 -0800159 // -----------------------------------------------------------------------
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700160 // Virtuals
Mathias Agopian13127d82013-03-05 17:47:11 -0800161
Mathias Agopian13127d82013-03-05 17:47:11 -0800162 virtual const char* getTypeId() const { return "Layer"; }
163
Mathias Agopian13127d82013-03-05 17:47:11 -0800164 /*
165 * isOpaque - true if this surface is opaque
166 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700167 virtual bool isOpaque() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800168
169 /*
170 * isSecure - true if this surface is secure, that is if it prevents
171 * screenshots or VNC servers.
172 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800174
175 /*
176 * isProtected - true if the layer may contain protected content in the
177 * GRALLOC_USAGE_PROTECTED sense.
178 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800179 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800180
181 /*
182 * isVisible - true if this layer is visible, false otherwise
183 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700184 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
Mathias Agopian13127d82013-03-05 17:47:11 -0800186 /*
187 * isFixedSize - true if content has a fixed size
188 */
189 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700190
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700191protected:
192 /*
193 * onDraw - draws the surface.
194 */
195 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
196
197public:
198 // -----------------------------------------------------------------------
199
200 void setGeometry(const sp<const DisplayDevice>& hw,
201 HWComposer::HWCLayerInterface& layer);
202 void setPerFrameData(const sp<const DisplayDevice>& hw,
203 HWComposer::HWCLayerInterface& layer);
204 void setAcquireFence(const sp<const DisplayDevice>& hw,
205 HWComposer::HWCLayerInterface& layer);
206
207 /*
208 * called after page-flip
209 */
210 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
211 HWComposer::HWCLayerInterface* layer);
212
213 /*
214 * called before composition.
215 * returns true if the layer has pending updates.
216 */
217 bool onPreComposition();
218
219 /*
220 * called after composition.
221 */
222 void onPostComposition();
223
224 /*
225 * draw - performs some global clipping optimizations
226 * and calls onDraw().
227 */
228 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
229 void draw(const sp<const DisplayDevice>& hw);
230
231 /*
232 * doTransaction - process the transaction. This is a good place to figure
233 * out which attributes of the surface have changed.
234 */
235 uint32_t doTransaction(uint32_t transactionFlags);
236
237 /*
238 * setVisibleRegion - called to set the new visible region. This gives
239 * a chance to update the new visible region or record the fact it changed.
240 */
241 void setVisibleRegion(const Region& visibleRegion);
242
243 /*
244 * setCoveredRegion - called when the covered region changes. The covered
245 * region corresponds to any area of the surface that is covered
246 * (transparently or not) by another surface.
247 */
248 void setCoveredRegion(const Region& coveredRegion);
249
250 /*
251 * setVisibleNonTransparentRegion - called when the visible and
252 * non-transparent region changes.
253 */
254 void setVisibleNonTransparentRegion(const Region&
255 visibleNonTransparentRegion);
256
257 /*
258 * latchBuffer - called each time the screen is redrawn and returns whether
259 * the visible regions need to be recomputed (this is a fairly heavy
260 * operation, so this should be set only if needed). Typically this is used
261 * to figure out if the content or size of a surface has changed.
262 */
263 Region latchBuffer(bool& recomputeVisibleRegions);
264
Mathias Agopian13127d82013-03-05 17:47:11 -0800265 /*
266 * called with the state lock when the surface is removed from the
267 * current list
268 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700269 void onRemoved();
Mathias Agopian13127d82013-03-05 17:47:11 -0800270
271
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800272 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800273 // the current orientation of the display device.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700274 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700275
Mathias Agopian13127d82013-03-05 17:47:11 -0800276 /*
277 * returns the rectangle that crops the content of the layer and scales it
278 * to the layer's size.
279 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700280 Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800281
282 /*
283 * returns the transform bits (90 rotation / h-flip / v-flip) of the
284 * layer's content
285 */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700286 uint32_t getContentTransform() const;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800287
Mathias Agopian13127d82013-03-05 17:47:11 -0800288 // -----------------------------------------------------------------------
289
290 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
291 void setFiltering(bool filtering);
292 bool getFiltering() const;
293
294 // only for debugging
295 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
296
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700297 inline const State& getDrawingState() const { return mDrawingState; }
298 inline const State& getCurrentState() const { return mCurrentState; }
299 inline State& getCurrentState() { return mCurrentState; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800300
301
302 /* always call base class first */
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700303 void dump(String8& result, Colorizer& colorizer) const;
304 void dumpStats(String8& result) const;
305 void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700306
Mathias Agopian13127d82013-03-05 17:47:11 -0800307protected:
308 // constant
309 sp<SurfaceFlinger> mFlinger;
310
311 virtual void onFirstRef();
312
313 /*
314 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
315 * is called.
316 */
317 class LayerCleaner {
318 sp<SurfaceFlinger> mFlinger;
319 wp<Layer> mLayer;
320 protected:
321 ~LayerCleaner();
322 public:
323 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
324 };
325
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327private:
Mathias Agopian13127d82013-03-05 17:47:11 -0800328 // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
329 virtual void onFrameAvailable();
330
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700331 void commitTransaction();
332
333 // needsLinearFiltering - true if this surface's state requires filtering
334 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800335
Mathias Agopian3330b202009-10-05 17:07:12 -0700336 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800337 Rect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700338 bool isCropped() const;
339 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700340
Mathias Agopian13127d82013-03-05 17:47:11 -0800341 // drawing
342 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
343 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
344 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
345
Igor Murashkina4a31492012-10-29 13:36:11 -0700346
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700347 // -----------------------------------------------------------------------
348
Mathias Agopiana67932f2011-04-20 14:20:59 -0700349 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800350 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700351 GLuint mTextureName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800352 bool mPremultipliedAlpha;
353 String8 mName;
354 mutable bool mDebug;
355 PixelFormat mFormat;
Mathias Agopian13127d82013-03-05 17:47:11 -0800356 bool mOpaqueLayer;
357
358 // these are protected by an external lock
359 State mCurrentState;
360 State mDrawingState;
361 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700362
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700363 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700364 volatile int32_t mQueuedFrames;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800365 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700366
367 // main thread
368 sp<GraphicBuffer> mActiveBuffer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700369 Rect mCurrentCrop;
370 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700371 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700372 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800373 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800374 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800375 // Whether filtering is forced on or not
376 bool mFiltering;
377 // Whether filtering is needed b/c of the drawingstate
378 bool mNeedsFiltering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700379
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700380 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800381 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800382 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800383
384 // protected by mLock
385 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700386 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800387 mutable bool mHasSurface;
388 const wp<Client> mClientRef;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389};
390
391// ---------------------------------------------------------------------------
392
393}; // namespace android
394
395#endif // ANDROID_LAYER_H