blob: 5fb6d8b682f77801840bbf2e5f3bc1af426f724c [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 Agopian13127d82013-03-05 17:47:11 -080054class DisplayDevice;
55class GraphicBuffer;
56class SurfaceFlinger;
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057class GLExtensions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
59// ---------------------------------------------------------------------------
60
Andy McFadden882e3a32013-01-08 16:06:15 -080061/*
Andy McFadden882e3a32013-01-08 16:06:15 -080062 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
63 * Layer is first referenced.
64 *
65 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
66 * that new data has arrived.
67 */
Mathias Agopian13127d82013-03-05 17:47:11 -080068class Layer : public SurfaceFlingerConsumer::FrameAvailableListener {
69 static int32_t sSequence;
70
Mathias Agopiand606de62010-05-10 20:06:11 -070071public:
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mutable bool contentDirty;
73 // regions below are in window-manager space
74 Region visibleRegion;
75 Region coveredRegion;
76 Region visibleNonTransparentRegion;
77 int32_t sequence;
78
79 enum { // flags for doTransaction()
80 eDontUpdateGeometryState = 0x00000001,
81 eVisibleRegion = 0x00000002,
82 };
83
84 struct Geometry {
85 uint32_t w;
86 uint32_t h;
87 Rect crop;
88 inline bool operator ==(const Geometry& rhs) const {
89 return (w == rhs.w && h == rhs.h && crop == rhs.crop);
90 }
91 inline bool operator !=(const Geometry& rhs) const {
92 return !operator ==(rhs);
93 }
94 };
95
96 struct State {
97 Geometry active;
98 Geometry requested;
99 uint32_t z;
100 uint32_t layerStack;
101 uint8_t alpha;
102 uint8_t flags;
103 uint8_t reserved[2];
104 int32_t sequence; // changes when visible regions can change
105 Transform transform;
106 Region transparentRegion;
107 };
108
109 class LayerMesh {
110 friend class Layer;
111 GLfloat mVertices[4][2];
112 size_t mNumVertices;
113 public:
114 LayerMesh() :
115 mNumVertices(4) {
116 }
117 GLfloat const* getVertices() const {
118 return &mVertices[0][0];
119 }
120 size_t getVertexCount() const {
121 return mNumVertices;
122 }
123 };
124
125 // -----------------------------------------------------------------------
126
Andy McFadden882e3a32013-01-08 16:06:15 -0800127 Layer(SurfaceFlinger* flinger, const sp<Client>& client);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700128 virtual ~Layer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700130 // the this layer's size and format
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700131 status_t setBuffers(uint32_t w, uint32_t h,
132 PixelFormat format, uint32_t flags=0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133
Mathias Agopian13127d82013-03-05 17:47:11 -0800134 // Creates an ISurface associated with this object. This may only be
135 // called once. to provide your own ISurface, override createSurface().
136 sp<ISurface> getSurface();
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
149 void commitTransaction();
150
151 uint32_t getTransactionFlags(uint32_t flags);
152 uint32_t setTransactionFlags(uint32_t flags);
153
154 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
155 Rect computeBounds() const;
156
157 // -----------------------------------------------------------------------
158
159 /*
160 * initStates - called just after construction
161 */
162 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
163
164 virtual const char* getTypeId() const { return "Layer"; }
165
166 virtual void setName(const String8& name);
167 String8 getName() const;
168
Mathias Agopian42977342012-08-05 00:40:46 -0700169 virtual void setGeometry(const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700170 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700171 virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700172 HWComposer::HWCLayerInterface& layer);
Mathias Agopian42977342012-08-05 00:40:46 -0700173 virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700174 HWComposer::HWCLayerInterface& layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800175
176 /*
177 * called after page-flip
178 */
Mathias Agopian42977342012-08-05 00:40:46 -0700179 virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700180 HWComposer::HWCLayerInterface* layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800181
182 /*
183 * called before composition.
184 * returns true if the layer has pending updates.
185 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700186 virtual bool onPreComposition();
Mathias Agopian13127d82013-03-05 17:47:11 -0800187
188 /*
189 * called after composition.
190 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700191 virtual void onPostComposition();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700192
Mathias Agopian13127d82013-03-05 17:47:11 -0800193 /*
194 * draw - performs some global clipping optimizations
195 * and calls onDraw().
196 * Typically this method is not overridden, instead implement onDraw()
197 * to perform the actual drawing.
198 */
199 virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
200 virtual void draw(const sp<const DisplayDevice>& hw);
201
202 /*
203 * onDraw - draws the surface.
204 */
Mathias Agopian42977342012-08-05 00:40:46 -0700205 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800206
207 /*
208 * needsLinearFiltering - true if this surface's state requires filtering
209 */
210 virtual bool needsFiltering(const sp<const DisplayDevice>& hw) const;
211
212 /*
213 * doTransaction - process the transaction. This is a good place to figure
214 * out which attributes of the surface have changed.
215 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 virtual uint32_t doTransaction(uint32_t transactionFlags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800217
218 /*
219 * setVisibleRegion - called to set the new visible region. This gives
220 * a chance to update the new visible region or record the fact it changed.
221 */
222 virtual void setVisibleRegion(const Region& visibleRegion);
223
224 /*
225 * setCoveredRegion - called when the covered region changes. The covered
226 * region corresponds to any area of the surface that is covered
227 * (transparently or not) by another surface.
228 */
229 virtual void setCoveredRegion(const Region& coveredRegion);
230
231 /*
232 * setVisibleNonTransparentRegion - called when the visible and
233 * non-transparent region changes.
234 */
235 virtual void setVisibleNonTransparentRegion(const Region&
236 visibleNonTransparentRegion);
237
238 /*
239 * latchBuffer - called each time the screen is redrawn and returns whether
240 * the visible regions need to be recomputed (this is a fairly heavy
241 * operation, so this should be set only if needed). Typically this is used
242 * to figure out if the content or size of a surface has changed.
243 */
Mathias Agopian4fec8732012-06-29 14:12:52 -0700244 virtual Region latchBuffer(bool& recomputeVisibleRegions);
Mathias Agopian13127d82013-03-05 17:47:11 -0800245
246 /*
247 * isOpaque - true if this surface is opaque
248 */
Mathias Agopiana67932f2011-04-20 14:20:59 -0700249 virtual bool isOpaque() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800250
251 /*
252 * isSecure - true if this surface is secure, that is if it prevents
253 * screenshots or VNC servers.
254 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 virtual bool isSecure() const { return mSecure; }
Mathias Agopian13127d82013-03-05 17:47:11 -0800256
257 /*
258 * isProtected - true if the layer may contain protected content in the
259 * GRALLOC_USAGE_PROTECTED sense.
260 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800261 virtual bool isProtected() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800262
263 /*
264 * isVisible - true if this layer is visible, false otherwise
265 */
Mathias Agopianda27af92012-09-13 18:17:13 -0700266 virtual bool isVisible() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267
Mathias Agopian13127d82013-03-05 17:47:11 -0800268 /*
269 * isFixedSize - true if content has a fixed size
270 */
271 virtual bool isFixedSize() const;
Jamie Gennis582270d2011-08-17 18:19:00 -0700272
Mathias Agopian13127d82013-03-05 17:47:11 -0800273 /*
274 * called with the state lock when the surface is removed from the
275 * current list
276 */
277 virtual void onRemoved();
278
279
280 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700281
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800282 // Updates the transform hint in our SurfaceFlingerConsumer to match
Mathias Agopian84300952012-11-21 16:02:13 -0800283 // the current orientation of the display device.
284 virtual void updateTransformHint(const sp<const DisplayDevice>& hw) const;
Andy McFadden69052052012-09-14 16:10:11 -0700285
Mathias Agopian13127d82013-03-05 17:47:11 -0800286 /*
287 * returns the rectangle that crops the content of the layer and scales it
288 * to the layer's size.
289 */
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800290 virtual Rect getContentCrop() const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800291
292 /*
293 * returns the transform bits (90 rotation / h-flip / v-flip) of the
294 * layer's content
295 */
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800296 virtual uint32_t getContentTransform() const;
297
Mathias Agopian13127d82013-03-05 17:47:11 -0800298 // -----------------------------------------------------------------------
299
300 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
301 void setFiltering(bool filtering);
302 bool getFiltering() const;
303
304 // only for debugging
305 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
306
307 inline const State& drawingState() const { return mDrawingState; }
308 inline const State& currentState() const { return mCurrentState; }
309 inline State& currentState() { return mCurrentState; }
310
311
312 /* always call base class first */
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700313 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800314 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800315 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800316 virtual void clearStats();
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700317
Mathias Agopian13127d82013-03-05 17:47:11 -0800318protected:
319 // constant
320 sp<SurfaceFlinger> mFlinger;
321
322 virtual void onFirstRef();
323
324 /*
325 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
326 * is called.
327 */
328 class LayerCleaner {
329 sp<SurfaceFlinger> mFlinger;
330 wp<Layer> mLayer;
331 protected:
332 ~LayerCleaner();
333 public:
334 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
335 };
336
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338private:
Andy McFadden882e3a32013-01-08 16:06:15 -0800339 // Creates an instance of ISurface for this Layer.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700340 virtual sp<ISurface> createSurface();
Andy McFadden882e3a32013-01-08 16:06:15 -0800341
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
343 virtual void onFrameAvailable();
344
345
Mathias Agopian3330b202009-10-05 17:07:12 -0700346 uint32_t getEffectiveUsage(uint32_t usage) const;
Mathias Agopian13127d82013-03-05 17:47:11 -0800347 Rect computeCrop(const sp<const DisplayDevice>& hw) const;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 bool isCropped() const;
349 static bool getOpacityForFormat(uint32_t format);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700350
Mathias Agopian13127d82013-03-05 17:47:11 -0800351 // drawing
352 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
353 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
354 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
355
Igor Murashkina4a31492012-10-29 13:36:11 -0700356
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700357 // -----------------------------------------------------------------------
358
Mathias Agopiana67932f2011-04-20 14:20:59 -0700359 // constants
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800360 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700361 GLuint mTextureName;
Mathias Agopian13127d82013-03-05 17:47:11 -0800362 bool mPremultipliedAlpha;
363 String8 mName;
364 mutable bool mDebug;
365 PixelFormat mFormat;
366 const GLExtensions& mGLExtensions;
367 bool mOpaqueLayer;
368
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;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800376 FrameTracker mFrameTracker;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700377
378 // main thread
379 sp<GraphicBuffer> mActiveBuffer;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700380 Rect mCurrentCrop;
381 uint32_t mCurrentTransform;
Mathias Agopian933389f2011-07-18 16:15:08 -0700382 uint32_t mCurrentScalingMode;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700383 bool mCurrentOpacity;
Mathias Agopian4d143ee2012-02-23 20:05:39 -0800384 bool mRefreshPending;
Jamie Gennise8696a42012-01-15 18:54:57 -0800385 bool mFrameLatencyNeeded;
Mathias Agopian13127d82013-03-05 17:47:11 -0800386 // Whether filtering is forced on or not
387 bool mFiltering;
388 // Whether filtering is needed b/c of the drawingstate
389 bool mNeedsFiltering;
Mathias Agopiand606de62010-05-10 20:06:11 -0700390
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700391 // page-flip thread (currently main thread)
Mathias Agopian13127d82013-03-05 17:47:11 -0800392 bool mSecure; // no screenshots
Glenn Kasten16f04532011-01-19 15:27:27 -0800393 bool mProtectedByApp; // application requires protected path to external sink
Mathias Agopian13127d82013-03-05 17:47:11 -0800394
395 // protected by mLock
396 mutable Mutex mLock;
397 // Set to true if an ISurface has been associated with this object.
398 mutable bool mHasSurface;
399 const wp<Client> mClientRef;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400};
401
402// ---------------------------------------------------------------------------
403
404}; // namespace android
405
406#endif // ANDROID_LAYER_H