blob: 633df96542a5f4fa44164d8ada8084a1dc0de465 [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{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070056 static int32_t sSequence;
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070059 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 DisplayID dpy;
62 mutable bool contentDirty;
63 Region visibleRegionScreen;
64 Region transparentRegionScreen;
65 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070066 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
68 struct State {
69 uint32_t w;
70 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070071 uint32_t requested_w;
72 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 uint32_t z;
74 uint8_t alpha;
75 uint8_t flags;
76 uint8_t reserved[2];
77 int32_t sequence; // changes when visible regions can change
78 uint32_t tint;
79 Transform transform;
80 Region transparentRegion;
81 };
82
Mathias Agopiand1296592010-03-09 19:17:47 -080083 void setName(const String8& name);
84 String8 getName() const;
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 // modify current state
87 bool setPosition(int32_t x, int32_t y);
88 bool setLayer(uint32_t z);
89 bool setSize(uint32_t w, uint32_t h);
90 bool setAlpha(uint8_t alpha);
91 bool setMatrix(const layer_state_t::matrix22_t& matrix);
92 bool setTransparentRegionHint(const Region& opaque);
93 bool setFlags(uint8_t flags, uint8_t mask);
94
Mathias Agopianba6be542009-09-29 22:32:36 -070095 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 bool requestTransaction();
97 void forceVisibilityTransaction();
98
99 uint32_t getTransactionFlags(uint32_t flags);
100 uint32_t setTransactionFlags(uint32_t flags);
101
102 Rect visibleBounds() const;
103 void drawRegion(const Region& reg) const;
104
105 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800106
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700107 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
108
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700109 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 /**
112 * draw - performs some global clipping optimizations
113 * and calls onDraw().
114 * Typically this method is not overridden, instead implement onDraw()
115 * to perform the actual drawing.
116 */
117 virtual void draw(const Region& clip) const;
Mathias Agopiandf85c452010-09-29 13:02:36 -0700118 virtual void drawForSreenShot() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119
120 /**
121 * onDraw - draws the surface.
122 */
123 virtual void onDraw(const Region& clip) const = 0;
124
125 /**
126 * initStates - called just after construction
127 */
128 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
129
130 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 * doTransaction - process the transaction. This is a good place to figure
132 * out which attributes of the surface have changed.
133 */
134 virtual uint32_t doTransaction(uint32_t transactionFlags);
135
136 /**
137 * setVisibleRegion - called to set the new visible region. This gives
138 * a chance to update the new visible region or record the fact it changed.
139 */
140 virtual void setVisibleRegion(const Region& visibleRegion);
141
142 /**
143 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700144 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 * (transparently or not) by another surface.
146 */
147 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 * validateVisibility - cache a bunch of things
151 */
152 virtual void validateVisibility(const Transform& globalTransform);
153
154 /**
155 * lockPageFlip - called each time the screen is redrawn and returns whether
156 * the visible regions need to be recomputed (this is a fairly heavy
157 * operation, so this should be set only if needed). Typically this is used
158 * to figure out if the content or size of a surface has changed.
159 */
160 virtual void lockPageFlip(bool& recomputeVisibleRegions);
161
162 /**
163 * unlockPageFlip - called each time the screen is redrawn. updates the
164 * final dirty region wrt the planeTransform.
165 * At this point, all visible regions, surface position and size, etc... are
166 * correct.
167 */
168 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
169
170 /**
171 * finishPageFlip - called after all surfaces have drawn.
172 */
173 virtual void finishPageFlip();
174
175 /**
176 * needsBlending - true if this surface needs blending
177 */
178 virtual bool needsBlending() const { return false; }
179
180 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700181 * needsDithering - true if this surface needs dithering
182 */
183 virtual bool needsDithering() const { return false; }
184
185 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700186 * needsLinearFiltering - true if this surface needs filtering
187 */
Mathias Agopian733189d2010-12-02 21:32:29 -0800188 virtual bool needsFiltering() const {
189 return (!(mFlags & DisplayHardware::SLOW_CONFIG)) && mNeedsFiltering;
190 }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700191
192 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700194 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 */
196 virtual bool isSecure() const { return false; }
197
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700198 /** Called from the main thread, when the surface is removed from the
199 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700200 virtual status_t ditch() { return NO_ERROR; }
201
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700202 /** called with the state lock when the surface is removed from the
203 * current list */
204 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700205
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700206 /** always call base class first */
207 virtual void dump(String8& result, char* scratch, size_t size) const;
208
209
Mathias Agopian9a112062009-04-17 19:36:26 -0700210 enum { // flags for doTransaction()
211 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700212 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
214
215 inline const State& drawingState() const { return mDrawingState; }
216 inline const State& currentState() const { return mCurrentState; }
217 inline State& currentState() { return mCurrentState; }
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 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 Agopianb661d662010-08-19 17:01:19 -0700232 // these must be called from the post/drawing thread
233 void setBufferCrop(const Rect& crop);
234 void setBufferTransform(uint32_t transform);
235
Mathias Agopian9a112062009-04-17 19:36:26 -0700236 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 uint32_t mFlags;
238
Mathias Agopianb661d662010-08-19 17:01:19 -0700239 // post/drawing thread
240 Rect mBufferCrop;
241 uint32_t mBufferTransform;
242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700244 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700246 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 int mLeft;
249 int mTop;
250
251 // these are protected by an external lock
252 State mCurrentState;
253 State mDrawingState;
254 volatile int32_t mTransactionFlags;
255
256 // don't change, don't need a lock
257 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800258 String8 mName;
259 mutable bool mDebug;
260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 // atomic
263 volatile int32_t mInvalidate;
264
265
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700266protected:
267 virtual ~LayerBase();
268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700270 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271};
272
273
274// ---------------------------------------------------------------------------
275
276class LayerBaseClient : public LayerBase
277{
278public:
279 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280
Mathias Agopian96f08192010-06-02 23:28:45 -0700281 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
282 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 virtual ~LayerBaseClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 sp<Surface> getSurface();
286 virtual sp<Surface> createSurface() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700287 virtual sp<LayerBaseClient> getLayerBaseClient() const {
288 return const_cast<LayerBaseClient*>(this); }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700289 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800290
Mathias Agopian96f08192010-06-02 23:28:45 -0700291 uint32_t getIdentity() const { return mIdentity; }
292
293 class Surface : public BnSurface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700295 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700296
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 protected:
Mathias Agopian96f08192010-06-02 23:28:45 -0700298 Surface(const sp<SurfaceFlinger>& flinger, int identity,
Mathias Agopian9a112062009-04-17 19:36:26 -0700299 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700300 virtual ~Surface();
301 virtual status_t onTransact(uint32_t code, const Parcel& data,
302 Parcel* reply, uint32_t flags);
303 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304
305 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700306 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
307 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700308 virtual status_t setBufferCount(int bufferCount);
309
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700310 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
311 virtual void postBuffer(ssize_t offset);
312 virtual void unregisterBuffers();
313 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800314 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315
Mathias Agopian9a112062009-04-17 19:36:26 -0700316 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700318 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319 int32_t mIdentity;
320 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 };
322
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700323 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700325protected:
326 virtual void dump(String8& result, char* scratch, size_t size) const;
327
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328private:
Mathias Agopian96f08192010-06-02 23:28:45 -0700329 mutable Mutex mLock;
330 mutable wp<Surface> mClientSurface;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700331 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700332 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700333 const uint32_t mIdentity;
334 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335};
336
337// ---------------------------------------------------------------------------
338
339}; // namespace android
340
341#endif // ANDROID_LAYER_BASE_H