blob: afc5ec8d5073c04861e4cfcc0efa1103a0f3cb23 [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
Mathias Agopian22c67842010-11-01 23:32:18 -070038#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include "Transform.h"
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class DisplayHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046class Client;
Mathias Agopian3330b202009-10-05 17:07:12 -070047class GraphicBuffer;
48class GraphicPlane;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070049class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070050class SurfaceFlinger;
Mathias Agopiand606de62010-05-10 20:06:11 -070051class Texture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53// ---------------------------------------------------------------------------
54
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070057 static int32_t sSequence;
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070060 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062 DisplayID dpy;
63 mutable bool contentDirty;
64 Region visibleRegionScreen;
65 Region transparentRegionScreen;
66 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070067 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
69 struct State {
70 uint32_t w;
71 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070072 uint32_t requested_w;
73 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 uint32_t z;
75 uint8_t alpha;
76 uint8_t flags;
77 uint8_t reserved[2];
78 int32_t sequence; // changes when visible regions can change
79 uint32_t tint;
80 Transform transform;
81 Region transparentRegion;
82 };
83
Mathias Agopiand1296592010-03-09 19:17:47 -080084 void setName(const String8& name);
85 String8 getName() const;
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 // modify current state
88 bool setPosition(int32_t x, int32_t y);
89 bool setLayer(uint32_t z);
90 bool setSize(uint32_t w, uint32_t h);
91 bool setAlpha(uint8_t alpha);
92 bool setMatrix(const layer_state_t::matrix22_t& matrix);
93 bool setTransparentRegionHint(const Region& opaque);
94 bool setFlags(uint8_t flags, uint8_t mask);
95
Mathias Agopianba6be542009-09-29 22:32:36 -070096 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 bool requestTransaction();
98 void forceVisibilityTransaction();
99
100 uint32_t getTransactionFlags(uint32_t flags);
101 uint32_t setTransactionFlags(uint32_t flags);
102
103 Rect visibleBounds() const;
104 void drawRegion(const Region& reg) const;
105
106 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800107
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700108 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
109
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700110 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 /**
113 * draw - performs some global clipping optimizations
114 * and calls onDraw().
115 * Typically this method is not overridden, instead implement onDraw()
116 * to perform the actual drawing.
117 */
118 virtual void draw(const Region& clip) const;
Mathias Agopiandf85c452010-09-29 13:02:36 -0700119 virtual void drawForSreenShot() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
121 /**
Mathias Agopian22c67842010-11-01 23:32:18 -0700122 * bypass mode
123 */
124 virtual bool setBypass(bool enable) { return false; }
125
126 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 * onDraw - draws the surface.
128 */
129 virtual void onDraw(const Region& clip) const = 0;
130
131 /**
132 * initStates - called just after construction
133 */
134 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
135
136 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 * doTransaction - process the transaction. This is a good place to figure
138 * out which attributes of the surface have changed.
139 */
140 virtual uint32_t doTransaction(uint32_t transactionFlags);
141
142 /**
143 * setVisibleRegion - called to set the new visible region. This gives
144 * a chance to update the new visible region or record the fact it changed.
145 */
146 virtual void setVisibleRegion(const Region& visibleRegion);
147
148 /**
149 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700150 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 * (transparently or not) by another surface.
152 */
153 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 * validateVisibility - cache a bunch of things
157 */
158 virtual void validateVisibility(const Transform& globalTransform);
159
160 /**
161 * lockPageFlip - called each time the screen is redrawn and returns whether
162 * the visible regions need to be recomputed (this is a fairly heavy
163 * operation, so this should be set only if needed). Typically this is used
164 * to figure out if the content or size of a surface has changed.
165 */
166 virtual void lockPageFlip(bool& recomputeVisibleRegions);
167
168 /**
169 * unlockPageFlip - called each time the screen is redrawn. updates the
170 * final dirty region wrt the planeTransform.
171 * At this point, all visible regions, surface position and size, etc... are
172 * correct.
173 */
174 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
175
176 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 * needsBlending - true if this surface needs blending
178 */
179 virtual bool needsBlending() const { return false; }
180
181 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700182 * needsDithering - true if this surface needs dithering
183 */
184 virtual bool needsDithering() const { return false; }
185
186 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700187 * needsLinearFiltering - true if this surface needs filtering
188 */
Mathias Agopian733189d2010-12-02 21:32:29 -0800189 virtual bool needsFiltering() const {
190 return (!(mFlags & DisplayHardware::SLOW_CONFIG)) && mNeedsFiltering;
191 }
Mathias Agopiana7f66922010-05-26 22:08:52 -0700192
193 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700195 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 */
197 virtual bool isSecure() const { return false; }
198
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700199 /** Called from the main thread, when the surface is removed from the
200 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700201 virtual status_t ditch() { return NO_ERROR; }
202
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700203 /** called with the state lock when the surface is removed from the
204 * current list */
205 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700206
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700207 /** always call base class first */
208 virtual void dump(String8& result, char* scratch, size_t size) const;
209
210
Mathias Agopian9a112062009-04-17 19:36:26 -0700211 enum { // flags for doTransaction()
212 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700213 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214
215
216 inline const State& drawingState() const { return mDrawingState; }
217 inline const State& currentState() const { return mCurrentState; }
218 inline State& currentState() { return mCurrentState; }
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 int32_t getOrientation() const { return mOrientation; }
221 int tx() const { return mLeft; }
222 int ty() const { return mTop; }
223
224protected:
225 const GraphicPlane& graphicPlane(int dpy) const;
226 GraphicPlane& graphicPlane(int dpy);
227
Mathias Agopian010fccb2010-05-26 22:26:12 -0700228 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
229 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700231 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700232
Mathias Agopianb661d662010-08-19 17:01:19 -0700233 // these must be called from the post/drawing thread
234 void setBufferCrop(const Rect& crop);
235 void setBufferTransform(uint32_t transform);
236
Mathias Agopian9a112062009-04-17 19:36:26 -0700237 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 uint32_t mFlags;
239
Mathias Agopianb661d662010-08-19 17:01:19 -0700240 // post/drawing thread
241 Rect mBufferCrop;
242 uint32_t mBufferTransform;
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700245 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700247 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 int mLeft;
250 int mTop;
251
252 // these are protected by an external lock
253 State mCurrentState;
254 State mDrawingState;
255 volatile int32_t mTransactionFlags;
256
257 // don't change, don't need a lock
258 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800259 String8 mName;
260 mutable bool mDebug;
261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 // atomic
264 volatile int32_t mInvalidate;
265
266
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700267protected:
268 virtual ~LayerBase();
269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272};
273
274
275// ---------------------------------------------------------------------------
276
277class LayerBaseClient : public LayerBase
278{
279public:
280 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
Mathias Agopian96f08192010-06-02 23:28:45 -0700282 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
283 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 virtual ~LayerBaseClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 sp<Surface> getSurface();
287 virtual sp<Surface> createSurface() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700288 virtual sp<LayerBaseClient> getLayerBaseClient() const {
289 return const_cast<LayerBaseClient*>(this); }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700290 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800291
Mathias Agopian96f08192010-06-02 23:28:45 -0700292 uint32_t getIdentity() const { return mIdentity; }
293
294 class Surface : public BnSurface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700296 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298 protected:
Mathias Agopian96f08192010-06-02 23:28:45 -0700299 Surface(const sp<SurfaceFlinger>& flinger, int identity,
Mathias Agopian9a112062009-04-17 19:36:26 -0700300 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700301 virtual ~Surface();
302 virtual status_t onTransact(uint32_t code, const Parcel& data,
303 Parcel* reply, uint32_t flags);
304 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
306 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700307 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
308 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700309 virtual status_t setBufferCount(int bufferCount);
310
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700311 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
312 virtual void postBuffer(ssize_t offset);
313 virtual void unregisterBuffers();
314 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800315 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316
Mathias Agopian9a112062009-04-17 19:36:26 -0700317 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700319 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700320 int32_t mIdentity;
321 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 };
323
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700326protected:
327 virtual void dump(String8& result, char* scratch, size_t size) const;
328
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329private:
Mathias Agopian96f08192010-06-02 23:28:45 -0700330 mutable Mutex mLock;
Mathias Agopianf7662af2011-03-09 16:13:31 -0800331 mutable wp<Surface> mClientSurface;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700332 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700333 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700334 const uint32_t mIdentity;
335 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336};
337
338// ---------------------------------------------------------------------------
339
340}; // namespace android
341
342#endif // ANDROID_LAYER_BASE_H