blob: c9a516f734aad90e2bbb51c701b4f96167acb87c [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>
30#include <ui/Overlay.h>
31
Mathias Agopian7e27f052010-05-28 14:22:23 -070032#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080033#include <private/surfaceflinger/SharedBufferStack.h>
34#include <private/surfaceflinger/LayerState.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <pixelflinger/pixelflinger.h>
37
38#include "Transform.h"
39
40namespace android {
41
42// ---------------------------------------------------------------------------
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044class DisplayHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class Client;
Mathias Agopian3330b202009-10-05 17:07:12 -070046class GraphicBuffer;
47class GraphicPlane;
48class SurfaceFlinger;
Mathias Agopiand606de62010-05-10 20:06:11 -070049class Texture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
51// ---------------------------------------------------------------------------
52
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070056 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 DisplayID dpy;
59 mutable bool contentDirty;
60 Region visibleRegionScreen;
61 Region transparentRegionScreen;
62 Region coveredRegionScreen;
63
64 struct State {
65 uint32_t w;
66 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070067 uint32_t requested_w;
68 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 uint32_t z;
70 uint8_t alpha;
71 uint8_t flags;
72 uint8_t reserved[2];
73 int32_t sequence; // changes when visible regions can change
74 uint32_t tint;
75 Transform transform;
76 Region transparentRegion;
77 };
78
Mathias Agopiand1296592010-03-09 19:17:47 -080079 void setName(const String8& name);
80 String8 getName() const;
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 // modify current state
83 bool setPosition(int32_t x, int32_t y);
84 bool setLayer(uint32_t z);
85 bool setSize(uint32_t w, uint32_t h);
86 bool setAlpha(uint8_t alpha);
87 bool setMatrix(const layer_state_t::matrix22_t& matrix);
88 bool setTransparentRegionHint(const Region& opaque);
89 bool setFlags(uint8_t flags, uint8_t mask);
90
Mathias Agopianba6be542009-09-29 22:32:36 -070091 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 bool requestTransaction();
93 void forceVisibilityTransaction();
94
95 uint32_t getTransactionFlags(uint32_t flags);
96 uint32_t setTransactionFlags(uint32_t flags);
97
98 Rect visibleBounds() const;
99 void drawRegion(const Region& reg) const;
100
101 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800102
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700103 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 /**
106 * draw - performs some global clipping optimizations
107 * and calls onDraw().
108 * Typically this method is not overridden, instead implement onDraw()
109 * to perform the actual drawing.
110 */
111 virtual void draw(const Region& clip) const;
112
113 /**
114 * onDraw - draws the surface.
115 */
116 virtual void onDraw(const Region& clip) const = 0;
117
118 /**
119 * initStates - called just after construction
120 */
121 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
122
123 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 * doTransaction - process the transaction. This is a good place to figure
125 * out which attributes of the surface have changed.
126 */
127 virtual uint32_t doTransaction(uint32_t transactionFlags);
128
129 /**
130 * setVisibleRegion - called to set the new visible region. This gives
131 * a chance to update the new visible region or record the fact it changed.
132 */
133 virtual void setVisibleRegion(const Region& visibleRegion);
134
135 /**
136 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700137 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 * (transparently or not) by another surface.
139 */
140 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 * validateVisibility - cache a bunch of things
144 */
145 virtual void validateVisibility(const Transform& globalTransform);
146
147 /**
148 * lockPageFlip - called each time the screen is redrawn and returns whether
149 * the visible regions need to be recomputed (this is a fairly heavy
150 * operation, so this should be set only if needed). Typically this is used
151 * to figure out if the content or size of a surface has changed.
152 */
153 virtual void lockPageFlip(bool& recomputeVisibleRegions);
154
155 /**
156 * unlockPageFlip - called each time the screen is redrawn. updates the
157 * final dirty region wrt the planeTransform.
158 * At this point, all visible regions, surface position and size, etc... are
159 * correct.
160 */
161 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
162
163 /**
164 * finishPageFlip - called after all surfaces have drawn.
165 */
166 virtual void finishPageFlip();
167
168 /**
169 * needsBlending - true if this surface needs blending
170 */
171 virtual bool needsBlending() const { return false; }
172
173 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700174 * needsDithering - true if this surface needs dithering
175 */
176 virtual bool needsDithering() const { return false; }
177
178 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700179 * needsLinearFiltering - true if this surface needs filtering
180 */
181 virtual bool needsFiltering() const { return mNeedsFiltering; }
182
183 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700185 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 */
187 virtual bool isSecure() const { return false; }
188
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700189 /** Called from the main thread, when the surface is removed from the
190 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700191 virtual status_t ditch() { return NO_ERROR; }
192
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700193 /** called with the state lock when the surface is removed from the
194 * current list */
195 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700196
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700197 /** always call base class first */
198 virtual void dump(String8& result, char* scratch, size_t size) const;
199
200
Mathias Agopian9a112062009-04-17 19:36:26 -0700201 enum { // flags for doTransaction()
202 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700203 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204
205
206 inline const State& drawingState() const { return mDrawingState; }
207 inline const State& currentState() const { return mCurrentState; }
208 inline State& currentState() { return mCurrentState; }
209
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210 static int compareCurrentStateZ(
211 sp<LayerBase> const * layerA,
212 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 return layerA[0]->currentState().z - layerB[0]->currentState().z;
214 }
215
216 int32_t getOrientation() const { return mOrientation; }
217 int tx() const { return mLeft; }
218 int ty() const { return mTop; }
219
220protected:
221 const GraphicPlane& graphicPlane(int dpy) const;
222 GraphicPlane& graphicPlane(int dpy);
223
Mathias Agopian010fccb2010-05-26 22:26:12 -0700224 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
225 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700227 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700228
Mathias Agopian9a112062009-04-17 19:36:26 -0700229 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 uint32_t mFlags;
231
232 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700233 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700235 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 int mLeft;
238 int mTop;
239
240 // these are protected by an external lock
241 State mCurrentState;
242 State mDrawingState;
243 volatile int32_t mTransactionFlags;
244
245 // don't change, don't need a lock
246 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800247 String8 mName;
248 mutable bool mDebug;
249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 // atomic
252 volatile int32_t mInvalidate;
253
254
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700255protected:
256 virtual ~LayerBase();
257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700259 LayerBase(const LayerBase& rhs);
260 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261};
262
263
264// ---------------------------------------------------------------------------
265
266class LayerBaseClient : public LayerBase
267{
268public:
269 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270
Mathias Agopian96f08192010-06-02 23:28:45 -0700271 const wp<Client> client;
272
273 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
274 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 virtual ~LayerBaseClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277 sp<Surface> getSurface();
278 virtual sp<Surface> createSurface() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700279 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800280
Mathias Agopian96f08192010-06-02 23:28:45 -0700281 uint32_t getIdentity() const { return mIdentity; }
282
283 class Surface : public BnSurface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700285 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700287 protected:
Mathias Agopian96f08192010-06-02 23:28:45 -0700288 Surface(const sp<SurfaceFlinger>& flinger, int identity,
Mathias Agopian9a112062009-04-17 19:36:26 -0700289 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 virtual ~Surface();
291 virtual status_t onTransact(uint32_t code, const Parcel& data,
292 Parcel* reply, uint32_t flags);
293 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
295 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700296 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
297 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700298 virtual status_t setBufferCount(int bufferCount);
299
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700300 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
301 virtual void postBuffer(ssize_t offset);
302 virtual void unregisterBuffers();
303 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800304 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700305
Mathias Agopian9a112062009-04-17 19:36:26 -0700306 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700308 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700309 int32_t mIdentity;
310 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 };
312
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700315protected:
316 virtual void dump(String8& result, char* scratch, size_t size) const;
317
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318private:
Mathias Agopian96f08192010-06-02 23:28:45 -0700319 mutable Mutex mLock;
320 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700321 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700322 const uint32_t mIdentity;
323 static int32_t sIdentity;
324
325 // TODO: get rid of this
326public:
327 virtual void setToken(int32_t token) { }
328 virtual int32_t getToken() const { return -1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329};
330
331// ---------------------------------------------------------------------------
332
333}; // namespace android
334
335#endif // ANDROID_LAYER_BASE_H