blob: 569b0ff2e58c8b37362012de09fc3cd0f0f76836 [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 Agopian9cce3252010-02-09 17:46:37 -080032#include <surfaceflinger/ISurfaceFlingerClient.h>
33#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"; }
104 virtual ssize_t serverIndex() const { return -1; }
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 /**
107 * draw - performs some global clipping optimizations
108 * and calls onDraw().
109 * Typically this method is not overridden, instead implement onDraw()
110 * to perform the actual drawing.
111 */
112 virtual void draw(const Region& clip) const;
113
114 /**
115 * onDraw - draws the surface.
116 */
117 virtual void onDraw(const Region& clip) const = 0;
118
119 /**
120 * initStates - called just after construction
121 */
122 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
123
124 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 * doTransaction - process the transaction. This is a good place to figure
126 * out which attributes of the surface have changed.
127 */
128 virtual uint32_t doTransaction(uint32_t transactionFlags);
129
130 /**
131 * setVisibleRegion - called to set the new visible region. This gives
132 * a chance to update the new visible region or record the fact it changed.
133 */
134 virtual void setVisibleRegion(const Region& visibleRegion);
135
136 /**
137 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700138 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 * (transparently or not) by another surface.
140 */
141 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 * validateVisibility - cache a bunch of things
145 */
146 virtual void validateVisibility(const Transform& globalTransform);
147
148 /**
149 * lockPageFlip - called each time the screen is redrawn and returns whether
150 * the visible regions need to be recomputed (this is a fairly heavy
151 * operation, so this should be set only if needed). Typically this is used
152 * to figure out if the content or size of a surface has changed.
153 */
154 virtual void lockPageFlip(bool& recomputeVisibleRegions);
155
156 /**
157 * unlockPageFlip - called each time the screen is redrawn. updates the
158 * final dirty region wrt the planeTransform.
159 * At this point, all visible regions, surface position and size, etc... are
160 * correct.
161 */
162 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
163
164 /**
165 * finishPageFlip - called after all surfaces have drawn.
166 */
167 virtual void finishPageFlip();
168
169 /**
170 * needsBlending - true if this surface needs blending
171 */
172 virtual bool needsBlending() const { return false; }
173
174 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700175 * needsDithering - true if this surface needs dithering
176 */
177 virtual bool needsDithering() const { return false; }
178
179 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 * transformed -- true is this surface needs a to be transformed
181 */
182 virtual bool transformed() const { return mTransformed; }
183
184 /**
185 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700186 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 */
188 virtual bool isSecure() const { return false; }
189
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700190 /** Called from the main thread, when the surface is removed from the
191 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700192 virtual status_t ditch() { return NO_ERROR; }
193
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700194 /** called with the state lock when the surface is removed from the
195 * current list */
196 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700197
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700198 /** always call base class first */
199 virtual void dump(String8& result, char* scratch, size_t size) const;
200
201
Mathias Agopian9a112062009-04-17 19:36:26 -0700202 enum { // flags for doTransaction()
203 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700204 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205
206
207 inline const State& drawingState() const { return mDrawingState; }
208 inline const State& currentState() const { return mCurrentState; }
209 inline State& currentState() { return mCurrentState; }
210
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 static int compareCurrentStateZ(
212 sp<LayerBase> const * layerA,
213 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 return layerA[0]->currentState().z - layerB[0]->currentState().z;
215 }
216
217 int32_t getOrientation() const { return mOrientation; }
218 int tx() const { return mLeft; }
219 int ty() const { return mTop; }
220
221protected:
222 const GraphicPlane& graphicPlane(int dpy) const;
223 GraphicPlane& graphicPlane(int dpy);
224
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700225 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
226 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700228 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700229
Mathias Agopian9a112062009-04-17 19:36:26 -0700230 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 uint32_t mFlags;
232
233 // cached during validateVisibility()
234 bool mTransformed;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700235 bool mUseLinearFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700237 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 int mLeft;
240 int mTop;
241
242 // these are protected by an external lock
243 State mCurrentState;
244 State mDrawingState;
245 volatile int32_t mTransactionFlags;
246
247 // don't change, don't need a lock
248 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800249 String8 mName;
250 mutable bool mDebug;
251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 // atomic
254 volatile int32_t mInvalidate;
255
256
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257protected:
258 virtual ~LayerBase();
259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700261 LayerBase(const LayerBase& rhs);
262 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263};
264
265
266// ---------------------------------------------------------------------------
267
268class LayerBaseClient : public LayerBase
269{
270public:
271 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272
273 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700274 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700276 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277
Mathias Agopian48d819a2009-09-10 19:41:18 -0700278 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 inline int32_t clientIndex() const { return mIndex; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700283 sp<Surface> getSurface();
284 virtual sp<Surface> createSurface() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700285 virtual ssize_t serverIndex() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700286 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 class Surface : public BnSurface
289 {
290 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700291 int32_t getToken() const { return mToken; }
292 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700294 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700295 Surface(const sp<SurfaceFlinger>& flinger,
296 SurfaceID id, int identity,
297 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298 virtual ~Surface();
299 virtual status_t onTransact(uint32_t code, const Parcel& data,
300 Parcel* reply, uint32_t flags);
301 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
303 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700304 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
305 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700306 virtual status_t setBufferCount(int bufferCount);
307
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
309 virtual void postBuffer(ssize_t offset);
310 virtual void unregisterBuffers();
311 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800312 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313
Mathias Agopian9a112062009-04-17 19:36:26 -0700314 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700316 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317 int32_t mToken;
318 int32_t mIdentity;
319 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 };
321
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700324protected:
325 virtual void dump(String8& result, char* scratch, size_t size) const;
326
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327private:
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700328 int32_t mIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329 mutable Mutex mLock;
330 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700331 // only read
332 const uint32_t mIdentity;
333 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334};
335
336// ---------------------------------------------------------------------------
337
338}; // namespace android
339
340#endif // ANDROID_LAYER_BASE_H