blob: 219a53c2d77e6cfc66f2220d06b9eacf89664ee1 [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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50// ---------------------------------------------------------------------------
51
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070055 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 DisplayID dpy;
58 mutable bool contentDirty;
59 Region visibleRegionScreen;
60 Region transparentRegionScreen;
61 Region coveredRegionScreen;
62
63 struct State {
64 uint32_t w;
65 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070066 uint32_t requested_w;
67 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 uint32_t z;
69 uint8_t alpha;
70 uint8_t flags;
71 uint8_t reserved[2];
72 int32_t sequence; // changes when visible regions can change
73 uint32_t tint;
74 Transform transform;
75 Region transparentRegion;
76 };
77
Mathias Agopiand1296592010-03-09 19:17:47 -080078 void setName(const String8& name);
79 String8 getName() const;
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 // modify current state
82 bool setPosition(int32_t x, int32_t y);
83 bool setLayer(uint32_t z);
84 bool setSize(uint32_t w, uint32_t h);
85 bool setAlpha(uint8_t alpha);
86 bool setMatrix(const layer_state_t::matrix22_t& matrix);
87 bool setTransparentRegionHint(const Region& opaque);
88 bool setFlags(uint8_t flags, uint8_t mask);
89
Mathias Agopianba6be542009-09-29 22:32:36 -070090 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 bool requestTransaction();
92 void forceVisibilityTransaction();
93
94 uint32_t getTransactionFlags(uint32_t flags);
95 uint32_t setTransactionFlags(uint32_t flags);
96
97 Rect visibleBounds() const;
98 void drawRegion(const Region& reg) const;
99
100 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800101
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700102 virtual const char* getTypeId() const { return "LayerBase"; }
103 virtual ssize_t serverIndex() const { return -1; }
104
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 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 * transformed -- true is this surface needs a to be transformed
180 */
181 virtual bool transformed() const { return mTransformed; }
182
183 /**
184 * 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
224 GLuint createTexture() const;
225
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700226 struct Texture {
227 Texture() : name(-1U), width(0), height(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700228 image(EGL_NO_IMAGE_KHR), transform(0),
229 NPOTAdjust(false), dirty(true) { }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700230 GLuint name;
231 GLuint width;
232 GLuint height;
Mathias Agopian3330b202009-10-05 17:07:12 -0700233 GLuint potWidth;
234 GLuint potHeight;
235 GLfloat wScale;
236 GLfloat hScale;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700237 EGLImageKHR image;
238 uint32_t transform;
Mathias Agopian3330b202009-10-05 17:07:12 -0700239 bool NPOTAdjust;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700240 bool dirty;
241 };
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700242
243 void clearWithOpenGL(const Region& clip, GLclampx r, GLclampx g,
244 GLclampx b, GLclampx alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700246 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700247 void loadTexture(Texture* texture,
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700248 const Region& dirty, const GGLSurface& t) const;
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700249 status_t initializeEglImage(
250 const sp<GraphicBuffer>& buffer, Texture* texture);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800252 bool isSupportedYuvFormat(int format) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700253
Mathias Agopian9a112062009-04-17 19:36:26 -0700254 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 uint32_t mFlags;
256
257 // cached during validateVisibility()
258 bool mTransformed;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700259 bool mUseLinearFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700261 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 int mLeft;
264 int mTop;
265
266 // these are protected by an external lock
267 State mCurrentState;
268 State mDrawingState;
269 volatile int32_t mTransactionFlags;
270
271 // don't change, don't need a lock
272 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800273 String8 mName;
274 mutable bool mDebug;
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 // atomic
278 volatile int32_t mInvalidate;
279
280
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281protected:
282 virtual ~LayerBase();
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 LayerBase(const LayerBase& rhs);
286 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287};
288
289
290// ---------------------------------------------------------------------------
291
292class LayerBaseClient : public LayerBase
293{
294public:
295 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
Mathias Agopian48d819a2009-09-10 19:41:18 -0700297 // lcblk is (almost) only accessed from the main SF thread, in the places
298 // where it's not, a reference to Client must be held
299 SharedBufferServer* lcblk;
300
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -0700302 const sp<Client>& client, int32_t i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 virtual ~LayerBaseClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700304 virtual void onFirstRef();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
Mathias Agopian48d819a2009-09-10 19:41:18 -0700306 const wp<Client> client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308 inline uint32_t getIdentity() const { return mIdentity; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 inline int32_t clientIndex() const { return mIndex; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700311 sp<Surface> getSurface();
312 virtual sp<Surface> createSurface() const;
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700313 virtual ssize_t serverIndex() const;
314 virtual void onRemoved();
315 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 class Surface : public BnSurface
318 {
319 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700320 int32_t getToken() const { return mToken; }
321 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700323 protected:
Mathias Agopian9a112062009-04-17 19:36:26 -0700324 Surface(const sp<SurfaceFlinger>& flinger,
325 SurfaceID id, int identity,
326 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327 virtual ~Surface();
328 virtual status_t onTransact(uint32_t code, const Parcel& data,
329 Parcel* reply, uint32_t flags);
330 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331
332 private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700333 virtual sp<GraphicBuffer> requestBuffer(int index, int usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700334 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
335 virtual void postBuffer(ssize_t offset);
336 virtual void unregisterBuffers();
337 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800338 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339
Mathias Agopian9a112062009-04-17 19:36:26 -0700340 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700341 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700342 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700343 int32_t mToken;
344 int32_t mIdentity;
345 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 };
347
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700348 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700350protected:
351 virtual void dump(String8& result, char* scratch, size_t size) const;
352
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700353private:
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700354 int32_t mIndex;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700355 mutable Mutex mLock;
356 mutable wp<Surface> mClientSurface;
Mathias Agopian2e123242009-06-23 20:06:46 -0700357 // only read
358 const uint32_t mIdentity;
359 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360};
361
362// ---------------------------------------------------------------------------
363
364}; // namespace android
365
366#endif // ANDROID_LAYER_BASE_H