blob: 1a07f32cb5b5b40ed9ec45d8bb9e1896e7fe4d2d [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;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070048class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070049class SurfaceFlinger;
Mathias Agopiand606de62010-05-10 20:06:11 -070050class Texture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52// ---------------------------------------------------------------------------
53
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070057 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 DisplayID dpy;
60 mutable bool contentDirty;
61 Region visibleRegionScreen;
62 Region transparentRegionScreen;
63 Region coveredRegionScreen;
64
65 struct State {
66 uint32_t w;
67 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070068 uint32_t requested_w;
69 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 uint32_t z;
71 uint8_t alpha;
72 uint8_t flags;
73 uint8_t reserved[2];
74 int32_t sequence; // changes when visible regions can change
75 uint32_t tint;
76 Transform transform;
77 Region transparentRegion;
78 };
79
Mathias Agopiand1296592010-03-09 19:17:47 -080080 void setName(const String8& name);
81 String8 getName() const;
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 // modify current state
84 bool setPosition(int32_t x, int32_t y);
85 bool setLayer(uint32_t z);
86 bool setSize(uint32_t w, uint32_t h);
87 bool setAlpha(uint8_t alpha);
88 bool setMatrix(const layer_state_t::matrix22_t& matrix);
89 bool setTransparentRegionHint(const Region& opaque);
90 bool setFlags(uint8_t flags, uint8_t mask);
91
Mathias Agopianba6be542009-09-29 22:32:36 -070092 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 bool requestTransaction();
94 void forceVisibilityTransaction();
95
96 uint32_t getTransactionFlags(uint32_t flags);
97 uint32_t setTransactionFlags(uint32_t flags);
98
99 Rect visibleBounds() const;
100 void drawRegion(const Region& reg) const;
101
102 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800103
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700104 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
105
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700106 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 /**
109 * draw - performs some global clipping optimizations
110 * and calls onDraw().
111 * Typically this method is not overridden, instead implement onDraw()
112 * to perform the actual drawing.
113 */
114 virtual void draw(const Region& clip) const;
115
116 /**
117 * onDraw - draws the surface.
118 */
119 virtual void onDraw(const Region& clip) const = 0;
120
121 /**
122 * initStates - called just after construction
123 */
124 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
125
126 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 * doTransaction - process the transaction. This is a good place to figure
128 * out which attributes of the surface have changed.
129 */
130 virtual uint32_t doTransaction(uint32_t transactionFlags);
131
132 /**
133 * setVisibleRegion - called to set the new visible region. This gives
134 * a chance to update the new visible region or record the fact it changed.
135 */
136 virtual void setVisibleRegion(const Region& visibleRegion);
137
138 /**
139 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700140 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 * (transparently or not) by another surface.
142 */
143 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 * validateVisibility - cache a bunch of things
147 */
148 virtual void validateVisibility(const Transform& globalTransform);
149
150 /**
151 * lockPageFlip - called each time the screen is redrawn and returns whether
152 * the visible regions need to be recomputed (this is a fairly heavy
153 * operation, so this should be set only if needed). Typically this is used
154 * to figure out if the content or size of a surface has changed.
155 */
156 virtual void lockPageFlip(bool& recomputeVisibleRegions);
157
158 /**
159 * unlockPageFlip - called each time the screen is redrawn. updates the
160 * final dirty region wrt the planeTransform.
161 * At this point, all visible regions, surface position and size, etc... are
162 * correct.
163 */
164 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
165
166 /**
167 * finishPageFlip - called after all surfaces have drawn.
168 */
169 virtual void finishPageFlip();
170
171 /**
172 * needsBlending - true if this surface needs blending
173 */
174 virtual bool needsBlending() const { return false; }
175
176 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700177 * needsDithering - true if this surface needs dithering
178 */
179 virtual bool needsDithering() const { return false; }
180
181 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700182 * needsLinearFiltering - true if this surface needs filtering
183 */
184 virtual bool needsFiltering() const { return mNeedsFiltering; }
185
186 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700188 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 */
190 virtual bool isSecure() const { return false; }
191
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700192 /** Called from the main thread, when the surface is removed from the
193 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700194 virtual status_t ditch() { return NO_ERROR; }
195
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700196 /** called with the state lock when the surface is removed from the
197 * current list */
198 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700199
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700200 /** always call base class first */
201 virtual void dump(String8& result, char* scratch, size_t size) const;
202
203
Mathias Agopian9a112062009-04-17 19:36:26 -0700204 enum { // flags for doTransaction()
205 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700206 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
208
209 inline const State& drawingState() const { return mDrawingState; }
210 inline const State& currentState() const { return mCurrentState; }
211 inline State& currentState() { return mCurrentState; }
212
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 static int compareCurrentStateZ(
214 sp<LayerBase> const * layerA,
215 sp<LayerBase> const * layerB) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 return layerA[0]->currentState().z - layerB[0]->currentState().z;
217 }
218
219 int32_t getOrientation() const { return mOrientation; }
220 int tx() const { return mLeft; }
221 int ty() const { return mTop; }
222
223protected:
224 const GraphicPlane& graphicPlane(int dpy) const;
225 GraphicPlane& graphicPlane(int dpy);
226
Mathias Agopian010fccb2010-05-26 22:26:12 -0700227 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
228 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700230 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700231
Mathias Agopian9a112062009-04-17 19:36:26 -0700232 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 uint32_t mFlags;
234
235 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700236 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700238 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 int mLeft;
241 int mTop;
242
243 // these are protected by an external lock
244 State mCurrentState;
245 State mDrawingState;
246 volatile int32_t mTransactionFlags;
247
248 // don't change, don't need a lock
249 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800250 String8 mName;
251 mutable bool mDebug;
252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 // atomic
255 volatile int32_t mInvalidate;
256
257
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258protected:
259 virtual ~LayerBase();
260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700262 LayerBase(const LayerBase& rhs);
263 void validateTexture(GLint textureName) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264};
265
266
267// ---------------------------------------------------------------------------
268
269class LayerBaseClient : public LayerBase
270{
271public:
272 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
Mathias Agopian96f08192010-06-02 23:28:45 -0700274 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
275 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 virtual ~LayerBaseClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278 sp<Surface> getSurface();
279 virtual sp<Surface> createSurface() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700280 virtual sp<LayerBaseClient> getLayerBaseClient() const {
281 return const_cast<LayerBaseClient*>(this); }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700282 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800283
Mathias Agopian96f08192010-06-02 23:28:45 -0700284 uint32_t getIdentity() const { return mIdentity; }
285
286 class Surface : public BnSurface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700288 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 protected:
Mathias Agopian96f08192010-06-02 23:28:45 -0700291 Surface(const sp<SurfaceFlinger>& flinger, int identity,
Mathias Agopian9a112062009-04-17 19:36:26 -0700292 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 virtual ~Surface();
294 virtual status_t onTransact(uint32_t code, const Parcel& data,
295 Parcel* reply, uint32_t flags);
296 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
298 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700299 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
300 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700301 virtual status_t setBufferCount(int bufferCount);
302
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
304 virtual void postBuffer(ssize_t offset);
305 virtual void unregisterBuffers();
306 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800307 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308
Mathias Agopian9a112062009-04-17 19:36:26 -0700309 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700310 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700311 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312 int32_t mIdentity;
313 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 };
315
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700318protected:
319 virtual void dump(String8& result, char* scratch, size_t size) const;
320
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700321private:
Mathias Agopian96f08192010-06-02 23:28:45 -0700322 mutable Mutex mLock;
323 mutable wp<Surface> mClientSurface;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700324 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700325 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700326 const uint32_t mIdentity;
327 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328};
329
330// ---------------------------------------------------------------------------
331
332}; // namespace android
333
334#endif // ANDROID_LAYER_BASE_H