blob: 11fdbb571fa78b2a9434d0fbc73b137054d28904 [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>
25#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Mathias Agopian13127d82013-03-05 17:47:11 -080028#include <utils/RefBase.h>
29#include <utils/String8.h>
30#include <utils/Timers.h>
31
32#include <ui/GraphicBuffer.h>
33#include <ui/PixelFormat.h>
34#include <ui/Region.h>
35
36#include <gui/ISurfaceComposerClient.h>
37
38#include <private/gui/LayerState.h>
39
Jamie Gennis82dbc742012-11-08 19:23:28 -080040#include "FrameTracker.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080041#include "Client.h"
42#include "SurfaceFlinger.h"
43#include "SurfaceFlingerConsumer.h"
Mathias Agopiana67932f2011-04-20 14:20:59 -070044#include "SurfaceTextureLayer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include "Transform.h"
46
Mathias Agopian13127d82013-03-05 17:47:11 -080047#include "DisplayHardware/HWComposer.h"
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049namespace android {
50
51// ---------------------------------------------------------------------------
52
Mathias Agopian1f7bec62010-06-25 18:02:21 -070053class Client;
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054class Colorizer;
Mathias Agopian13127d82013-03-05 17:47:11 -080055class DisplayDevice;
56class GraphicBuffer;
57class SurfaceFlinger;
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058class GLExtensions;
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 */
Mathias Agopian13127d82013-03-05 17:47:11 -080069class Layer : public SurfaceFlingerConsumer::FrameAvailableListener {
70 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
114 class LayerMesh {
115 friend class Layer;
116 GLfloat mVertices[4][2];
117 size_t mNumVertices;
118 public:
119 LayerMesh() :
120 mNumVertices(4) {
121 }
122 GLfloat const* getVertices() const {
123 return &mVertices[0][0];
124 }
125 size_t getVertexCount() const {
126 return mNumVertices;
127 }
128 };
129
130 // -----------------------------------------------------------------------
131
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
133 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
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
150 void commitTransaction();
151
152 uint32_t getTransactionFlags(uint32_t flags);
153 uint32_t setTransactionFlags(uint32_t flags);
154
155 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
156 Rect computeBounds() const;
157
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700158 sp<IBinder> getHandle();
159 sp<BufferQueue> getBufferQueue() const;
160 String8 getName() const;
161
Mathias Agopian13127d82013-03-05 17:47:11 -0800162 // -----------------------------------------------------------------------
163
Mathias Agopian13127d82013-03-05 17:47:11 -0800164 virtual const char* getTypeId() const { return "Layer"; }
165
Mathias Agopian42977342012-08-05 00:40:46 -0700166 virtual void setGeometry(const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700167 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700168 virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700169 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700170 virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700171 HWComposer::HWCLayerInterface& layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800172
173 /*
174 * called after page-flip
175 */
Mathias Agopian42977342012-08-05 00:40:46 -0700176 virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700177 HWComposer::HWCLayerInterface* layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800178
179 /*
180 * called before composition.
181 * returns true if the layer has pending updates.
182 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700183 virtual bool onPreComposition();
Mathias Agopian13127d82013-03-05 17:47:11 -0800184
185 /*
186 * called after composition.
187 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700188 virtual void onPostComposition();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700189
Mathias Agopian13127d82013-03-05 17:47:11 -0800190 /*
191 * draw - performs some global clipping optimizations
192 * and calls onDraw().
193 * Typically this method is not overridden, instead implement onDraw()
194 * to perform the actual drawing.
195 */
196 virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
197 virtual void draw(const sp<const DisplayDevice>& hw);
198
199 /*
200 * onDraw - draws the surface.
201 */
Mathias Agopian42977342012-08-05 00:40:46 -0700202 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800203
204 /*
205 * needsLinearFiltering - true if this surface's state requires filtering
206 */
207 virtual bool needsFiltering(const sp<const DisplayDevice>& hw) const;
208
209 /*
210 * doTransaction - process the transaction. This is a good place to figure
211 * out which attributes of the surface have changed.
212 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 virtual uint32_t doTransaction(uint32_t transactionFlags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800214
215 /*
216 * setVisibleRegion - called to set the new visible region. This gives
217 * a chance to update the new visible region or record the fact it changed.
218 */
219 virtual void setVisibleRegion(const Region& visibleRegion);
220
221 /*
222 * setCoveredRegion - called when the covered region changes. The covered
223 * region corresponds to any area of the surface that is covered
224 * (transparently or not) by another surface.
225 */
226 virtual void setCoveredRegion(const Region& coveredRegion);
227
228 /*
229 * setVisibleNonTransparentRegion - called when the visible and
230 * non-transparent region changes.
231 */
232 virtual void setVisibleNonTransparentRegion(const Region&
233 visibleNonTransparentRegion);
234
235 /*
236 * latchBuffer - called each time the screen is redrawn and returns whether
237 * the visible regions need to be recomputed (this is a fairly heavy
238 * operation, so this should be set only if needed). Typically this is used
239 * to figure out if the content or size of a surface has changed.
240 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700241 virtual Region latchBuffer(bool& recomputeVisibleRegions);
Mathias Agopian13127d82013-03-05 17:47:11 -0800242
243 /*
244 * isOpaque - true if this surface is opaque
245 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700246 virtual bool isOpaque() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800247
248 /*
249 * isSecure - true if this surface is secure, that is if it prevents
250 * screenshots or VNC servers.
251 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800253
254 /*
255 * isProtected - true if the layer may contain protected content in the
256 * GRALLOC_USAGE_PROTECTED sense.
257 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800258 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800259
260 /*
261 * isVisible - true if this layer is visible, false otherwise
262 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700263 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264
Mathias Agopian13127d82013-03-05 17:47:11 -0800265 /*
266 * isFixedSize - true if content has a fixed size
267 */
268 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700269
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 */
274 virtual void onRemoved();
275
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.
279 virtual 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 Agopiana8bca8d2013-02-27 22:03:19 -0800285 virtual Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800286
287 /*
288 * returns the transform bits (90 rotation / h-flip / v-flip) of the
289 * layer's content
290 */
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800291 virtual uint32_t getContentTransform() const;
292
Mathias Agopian13127d82013-03-05 17:47:11 -0800293 // -----------------------------------------------------------------------
294
295 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
296 void setFiltering(bool filtering);
297 bool getFiltering() const;
298
299 // only for debugging
300 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
301
302 inline const State& drawingState() const { return mDrawingState; }
303 inline const State& currentState() const { return mCurrentState; }
304 inline State& currentState() { return mCurrentState; }
305
306
307 /* always call base class first */
Mathias Agopian3e25fd82013-04-22 17:52:16 +0200308 virtual void dump(String8& result, Colorizer& colorizer) const;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200309 virtual void dumpStats(String8& result) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800310 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700311
Mathias Agopian13127d82013-03-05 17:47:11 -0800312protected:
313 // constant
314 sp<SurfaceFlinger> mFlinger;
315
316 virtual void onFirstRef();
317
318 /*
319 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
320 * is called.
321 */
322 class LayerCleaner {
323 sp<SurfaceFlinger> mFlinger;
324 wp<Layer> mLayer;
325 protected:
326 ~LayerCleaner();
327 public:
328 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
329 };
330
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332private:
Mathias Agopian13127d82013-03-05 17:47:11 -0800333 // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
334 virtual void onFrameAvailable();
335
336
Mathias Agopian3330b202009-10-05 17:07:12 -0700337 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800338 Rect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700339 bool isCropped() const;
340 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700341
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 // drawing
343 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
344 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
345 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
346
Igor Murashkina4a31492012-10-29 13:36:11 -0700347
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700348 // -----------------------------------------------------------------------
349
Mathias Agopiana67932f2011-04-20 14:20:59 -0700350 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800351 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700352 GLuint mTextureName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800353 bool mPremultipliedAlpha;
354 String8 mName;
355 mutable bool mDebug;
356 PixelFormat mFormat;
357 const GLExtensions& mGLExtensions;
358 bool mOpaqueLayer;
359
360 // these are protected by an external lock
361 State mCurrentState;
362 State mDrawingState;
363 volatile int32_t mTransactionFlags;
Mathias Agopiand606de62010-05-10 20:06:11 -0700364
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700365 // thread-safe
Mathias Agopiana67932f2011-04-20 14:20:59 -0700366 volatile int32_t mQueuedFrames;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800367 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700368
369 // main thread
370 sp<GraphicBuffer> mActiveBuffer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700371 Rect mCurrentCrop;
372 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700373 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700374 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800375 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800376 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800377 // Whether filtering is forced on or not
378 bool mFiltering;
379 // Whether filtering is needed b/c of the drawingstate
380 bool mNeedsFiltering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700381
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700382 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800383 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800384 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800385
386 // protected by mLock
387 mutable Mutex mLock;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700388 // Set to true once we've returned this surface's handle
Mathias Agopian13127d82013-03-05 17:47:11 -0800389 mutable bool mHasSurface;
390 const wp<Client> mClientRef;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391};
392
393// ---------------------------------------------------------------------------
394
395}; // namespace android
396
397#endif // ANDROID_LAYER_H