blob: 7162e47275c13cc4d5443329e8606e9916715344 [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_BASE_H
18#define ANDROID_LAYER_BASE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian1fed11c2009-06-23 18:08:22 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
Mathias Agopianeda65402010-02-22 03:15:57 -080025#include <GLES/gl.h>
Mathias Agopian1fed11c2009-06-23 18:08:22 -070026
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <utils/RefBase.h>
28
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/Region.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Mathias Agopian7e27f052010-05-28 14:22:23 -070031#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080032#include <private/surfaceflinger/SharedBufferStack.h>
33#include <private/surfaceflinger/LayerState.h>
34
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <pixelflinger/pixelflinger.h>
36
Mathias Agopiana350ff92010-08-10 17:14:02 -070037#include <hardware/hwcomposer.h>
38
Mathias Agopian5ec99b52010-12-07 23:41:55 -080039#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include "Transform.h"
41
42namespace android {
43
44// ---------------------------------------------------------------------------
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046class DisplayHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047class Client;
Mathias Agopian3330b202009-10-05 17:07:12 -070048class GraphicBuffer;
49class GraphicPlane;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070050class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070051class SurfaceFlinger;
Mathias Agopiand606de62010-05-10 20:06:11 -070052class Texture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54// ---------------------------------------------------------------------------
55
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070058 static int32_t sSequence;
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070061 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 DisplayID dpy;
64 mutable bool contentDirty;
65 Region visibleRegionScreen;
66 Region transparentRegionScreen;
67 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070068 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
70 struct State {
71 uint32_t w;
72 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070073 uint32_t requested_w;
74 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 uint32_t z;
76 uint8_t alpha;
77 uint8_t flags;
78 uint8_t reserved[2];
79 int32_t sequence; // changes when visible regions can change
80 uint32_t tint;
81 Transform transform;
82 Region transparentRegion;
83 };
84
Mathias Agopiand1296592010-03-09 19:17:47 -080085 void setName(const String8& name);
86 String8 getName() const;
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 // modify current state
89 bool setPosition(int32_t x, int32_t y);
90 bool setLayer(uint32_t z);
91 bool setSize(uint32_t w, uint32_t h);
92 bool setAlpha(uint8_t alpha);
93 bool setMatrix(const layer_state_t::matrix22_t& matrix);
94 bool setTransparentRegionHint(const Region& opaque);
95 bool setFlags(uint8_t flags, uint8_t mask);
96
Mathias Agopianba6be542009-09-29 22:32:36 -070097 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 bool requestTransaction();
99 void forceVisibilityTransaction();
100
101 uint32_t getTransactionFlags(uint32_t flags);
102 uint32_t setTransactionFlags(uint32_t flags);
103
104 Rect visibleBounds() const;
105 void drawRegion(const Region& reg) const;
106
107 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800108
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700109 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
110
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700111 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700112
Mathias Agopiana350ff92010-08-10 17:14:02 -0700113 virtual void setGeometry(hwc_layer_t* hwcl);
114
115 virtual void setPerFrameData(hwc_layer_t* hwcl);
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 /**
118 * draw - performs some global clipping optimizations
119 * and calls onDraw().
120 * Typically this method is not overridden, instead implement onDraw()
121 * to perform the actual drawing.
122 */
123 virtual void draw(const Region& clip) const;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700124 virtual void drawForSreenShot() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125
126 /**
127 * onDraw - draws the surface.
128 */
129 virtual void onDraw(const Region& clip) const = 0;
130
131 /**
132 * initStates - called just after construction
133 */
134 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
135
136 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 * doTransaction - process the transaction. This is a good place to figure
138 * out which attributes of the surface have changed.
139 */
140 virtual uint32_t doTransaction(uint32_t transactionFlags);
141
142 /**
143 * setVisibleRegion - called to set the new visible region. This gives
144 * a chance to update the new visible region or record the fact it changed.
145 */
146 virtual void setVisibleRegion(const Region& visibleRegion);
147
148 /**
149 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700150 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 * (transparently or not) by another surface.
152 */
153 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 * validateVisibility - cache a bunch of things
157 */
158 virtual void validateVisibility(const Transform& globalTransform);
159
160 /**
161 * lockPageFlip - called each time the screen is redrawn and returns whether
162 * the visible regions need to be recomputed (this is a fairly heavy
163 * operation, so this should be set only if needed). Typically this is used
164 * to figure out if the content or size of a surface has changed.
165 */
166 virtual void lockPageFlip(bool& recomputeVisibleRegions);
167
168 /**
169 * unlockPageFlip - called each time the screen is redrawn. updates the
170 * final dirty region wrt the planeTransform.
171 * At this point, all visible regions, surface position and size, etc... are
172 * correct.
173 */
174 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
175
176 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 * needsBlending - true if this surface needs blending
178 */
179 virtual bool needsBlending() const { return false; }
180
181 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700182 * needsDithering - true if this surface needs dithering
183 */
184 virtual bool needsDithering() const { return false; }
185
186 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700187 * needsLinearFiltering - true if this surface needs filtering
188 */
Mathias Agopian733189d2010-12-02 21:32:29 -0800189 virtual bool needsFiltering() const {
190 return (!(mFlags & DisplayHardware::SLOW_CONFIG)) && mNeedsFiltering;
191 }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700192
193 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700195 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 */
197 virtual bool isSecure() const { return false; }
198
Glenn Kasten16f04532011-01-19 15:27:27 -0800199 /**
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800200 * isProtected - true if the layer may contain protected content in the
201 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kasten16f04532011-01-19 15:27:27 -0800202 */
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800203 virtual bool isProtected() const { return false; }
Glenn Kasten16f04532011-01-19 15:27:27 -0800204
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700205 /** Called from the main thread, when the surface is removed from the
206 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700207 virtual status_t ditch() { return NO_ERROR; }
208
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700209 /** called with the state lock when the surface is removed from the
210 * current list */
211 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700212
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700213 /** always call base class first */
214 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800215 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700216
217
Mathias Agopian9a112062009-04-17 19:36:26 -0700218 enum { // flags for doTransaction()
219 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700220 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221
222
223 inline const State& drawingState() const { return mDrawingState; }
224 inline const State& currentState() const { return mCurrentState; }
225 inline State& currentState() { return mCurrentState; }
226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 int32_t getOrientation() const { return mOrientation; }
228 int tx() const { return mLeft; }
229 int ty() const { return mTop; }
230
231protected:
232 const GraphicPlane& graphicPlane(int dpy) const;
233 GraphicPlane& graphicPlane(int dpy);
234
Mathias Agopian010fccb2010-05-26 22:26:12 -0700235 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
236 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700238 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700239
Mathias Agopianb661d662010-08-19 17:01:19 -0700240 // these must be called from the post/drawing thread
241 void setBufferCrop(const Rect& crop);
242 void setBufferTransform(uint32_t transform);
243
Mathias Agopian9a112062009-04-17 19:36:26 -0700244 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 uint32_t mFlags;
246
Mathias Agopianb661d662010-08-19 17:01:19 -0700247 // post/drawing thread
248 Rect mBufferCrop;
249 uint32_t mBufferTransform;
250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700252 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700254 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 int mLeft;
257 int mTop;
258
259 // these are protected by an external lock
260 State mCurrentState;
261 State mDrawingState;
262 volatile int32_t mTransactionFlags;
263
264 // don't change, don't need a lock
265 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800266 String8 mName;
267 mutable bool mDebug;
268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 // atomic
271 volatile int32_t mInvalidate;
272
273
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700274protected:
275 virtual ~LayerBase();
276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279};
280
281
282// ---------------------------------------------------------------------------
283
284class LayerBaseClient : public LayerBase
285{
286public:
287 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
Mathias Agopian96f08192010-06-02 23:28:45 -0700289 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
290 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 virtual ~LayerBaseClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 sp<Surface> getSurface();
Mathias Agopian0d156122011-01-25 20:17:45 -0800294 wp<IBinder> getSurfaceBinder() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700295 virtual sp<Surface> createSurface() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700296 virtual sp<LayerBaseClient> getLayerBaseClient() const {
297 return const_cast<LayerBaseClient*>(this); }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700298 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800299
Mathias Agopian96f08192010-06-02 23:28:45 -0700300 uint32_t getIdentity() const { return mIdentity; }
301
302 class Surface : public BnSurface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700304 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700305
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700306 protected:
Mathias Agopian96f08192010-06-02 23:28:45 -0700307 Surface(const sp<SurfaceFlinger>& flinger, int identity,
Mathias Agopian9a112062009-04-17 19:36:26 -0700308 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700309 virtual ~Surface();
310 virtual status_t onTransact(uint32_t code, const Parcel& data,
311 Parcel* reply, uint32_t flags);
312 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313
314 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700315 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
316 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700317 virtual status_t setBufferCount(int bufferCount);
318
Mathias Agopian9a112062009-04-17 19:36:26 -0700319 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700320 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700321 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 int32_t mIdentity;
323 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 };
325
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700328protected:
329 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian48b888a2011-01-19 16:15:53 -0800330 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700331
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332private:
Mathias Agopian96f08192010-06-02 23:28:45 -0700333 mutable Mutex mLock;
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800334 mutable bool mHasSurface;
Mathias Agopian0d156122011-01-25 20:17:45 -0800335 wp<IBinder> mClientSurfaceBinder;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700336 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700337 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700338 const uint32_t mIdentity;
339 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340};
341
342// ---------------------------------------------------------------------------
343
344}; // namespace android
345
346#endif // ANDROID_LAYER_BASE_H