blob: dd1cd05f1b9346a9aa1868a572a83c9e653b9578 [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 Agopiana350ff92010-08-10 17:14:02 -070038#include <hardware/hwcomposer.h>
39
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include "Transform.h"
41
42namespace android {
43
44// ---------------------------------------------------------------------------
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046class DisplayHardware;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047class Client;
Mathias Agopian3330b202009-10-05 17:07:12 -070048class GraphicBuffer;
49class GraphicPlane;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070050class LayerBaseClient;
Mathias Agopian3330b202009-10-05 17:07:12 -070051class SurfaceFlinger;
Mathias Agopiand606de62010-05-10 20:06:11 -070052class Texture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54// ---------------------------------------------------------------------------
55
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056class LayerBase : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057{
Mathias Agopianf6679fc2010-08-10 18:09:09 -070058 static int32_t sSequence;
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060public:
Mathias Agopian1b5e1022010-04-20 17:55:49 -070061 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 DisplayID dpy;
64 mutable bool contentDirty;
65 Region visibleRegionScreen;
66 Region transparentRegionScreen;
67 Region coveredRegionScreen;
Mathias Agopianf6679fc2010-08-10 18:09:09 -070068 int32_t sequence;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
70 struct State {
71 uint32_t w;
72 uint32_t h;
Mathias Agopian7e4a5872009-09-29 22:39:22 -070073 uint32_t requested_w;
74 uint32_t requested_h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 uint32_t z;
76 uint8_t alpha;
77 uint8_t flags;
78 uint8_t reserved[2];
79 int32_t sequence; // changes when visible regions can change
80 uint32_t tint;
81 Transform transform;
82 Region transparentRegion;
83 };
84
Mathias Agopiand1296592010-03-09 19:17:47 -080085 void setName(const String8& name);
86 String8 getName() const;
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 // modify current state
89 bool setPosition(int32_t x, int32_t y);
90 bool setLayer(uint32_t z);
91 bool setSize(uint32_t w, uint32_t h);
92 bool setAlpha(uint8_t alpha);
93 bool setMatrix(const layer_state_t::matrix22_t& matrix);
94 bool setTransparentRegionHint(const Region& opaque);
95 bool setFlags(uint8_t flags, uint8_t mask);
96
Mathias Agopianba6be542009-09-29 22:32:36 -070097 void commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 bool requestTransaction();
99 void forceVisibilityTransaction();
100
101 uint32_t getTransactionFlags(uint32_t flags);
102 uint32_t setTransactionFlags(uint32_t flags);
103
104 Rect visibleBounds() const;
105 void drawRegion(const Region& reg) const;
106
107 void invalidate();
Mathias Agopiand1296592010-03-09 19:17:47 -0800108
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700109 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
110
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700111 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700112
Mathias Agopiana350ff92010-08-10 17:14:02 -0700113 virtual void setGeometry(hwc_layer_t* hwcl);
114
115 virtual void setPerFrameData(hwc_layer_t* hwcl);
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 /**
118 * draw - performs some global clipping optimizations
119 * and calls onDraw().
120 * Typically this method is not overridden, instead implement onDraw()
121 * to perform the actual drawing.
122 */
123 virtual void draw(const Region& clip) const;
124
125 /**
126 * onDraw - draws the surface.
127 */
128 virtual void onDraw(const Region& clip) const = 0;
129
130 /**
131 * initStates - called just after construction
132 */
133 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
134
135 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 * doTransaction - process the transaction. This is a good place to figure
137 * out which attributes of the surface have changed.
138 */
139 virtual uint32_t doTransaction(uint32_t transactionFlags);
140
141 /**
142 * setVisibleRegion - called to set the new visible region. This gives
143 * a chance to update the new visible region or record the fact it changed.
144 */
145 virtual void setVisibleRegion(const Region& visibleRegion);
146
147 /**
148 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopianab028732010-03-16 16:41:46 -0700149 * region corresponds to any area of the surface that is covered
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 * (transparently or not) by another surface.
151 */
152 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopianab028732010-03-16 16:41:46 -0700153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 * validateVisibility - cache a bunch of things
156 */
157 virtual void validateVisibility(const Transform& globalTransform);
158
159 /**
160 * lockPageFlip - called each time the screen is redrawn and returns whether
161 * the visible regions need to be recomputed (this is a fairly heavy
162 * operation, so this should be set only if needed). Typically this is used
163 * to figure out if the content or size of a surface has changed.
164 */
165 virtual void lockPageFlip(bool& recomputeVisibleRegions);
166
167 /**
168 * unlockPageFlip - called each time the screen is redrawn. updates the
169 * final dirty region wrt the planeTransform.
170 * At this point, all visible regions, surface position and size, etc... are
171 * correct.
172 */
173 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
174
175 /**
176 * finishPageFlip - called after all surfaces have drawn.
177 */
178 virtual void finishPageFlip();
179
180 /**
181 * needsBlending - true if this surface needs blending
182 */
183 virtual bool needsBlending() const { return false; }
184
185 /**
Mathias Agopian401c2572009-09-23 19:16:27 -0700186 * needsDithering - true if this surface needs dithering
187 */
188 virtual bool needsDithering() const { return false; }
189
190 /**
Mathias Agopiana7f66922010-05-26 22:08:52 -0700191 * needsLinearFiltering - true if this surface needs filtering
192 */
193 virtual bool needsFiltering() const { return mNeedsFiltering; }
194
195 /**
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian9a112062009-04-17 19:36:26 -0700197 * screenshots or VNC servers.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 */
199 virtual bool isSecure() const { return false; }
200
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700201 /** Called from the main thread, when the surface is removed from the
202 * draw list */
Mathias Agopian9a112062009-04-17 19:36:26 -0700203 virtual status_t ditch() { return NO_ERROR; }
204
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700205 /** called with the state lock when the surface is removed from the
206 * current list */
207 virtual void onRemoved() { };
Mathias Agopian9a112062009-04-17 19:36:26 -0700208
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700209 /** always call base class first */
210 virtual void dump(String8& result, char* scratch, size_t size) const;
211
212
Mathias Agopian9a112062009-04-17 19:36:26 -0700213 enum { // flags for doTransaction()
214 eVisibleRegion = 0x00000002,
Mathias Agopian9a112062009-04-17 19:36:26 -0700215 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216
217
218 inline const State& drawingState() const { return mDrawingState; }
219 inline const State& currentState() const { return mCurrentState; }
220 inline State& currentState() { return mCurrentState; }
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 int32_t getOrientation() const { return mOrientation; }
223 int tx() const { return mLeft; }
224 int ty() const { return mTop; }
225
226protected:
227 const GraphicPlane& graphicPlane(int dpy) const;
228 GraphicPlane& graphicPlane(int dpy);
229
Mathias Agopian010fccb2010-05-26 22:26:12 -0700230 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
231 GLclampf b, GLclampf alpha) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700233 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700234
Mathias Agopian9a112062009-04-17 19:36:26 -0700235 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 uint32_t mFlags;
237
238 // cached during validateVisibility()
Mathias Agopiana7f66922010-05-26 22:08:52 -0700239 bool mNeedsFiltering;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 int32_t mOrientation;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700241 GLfloat mVertices[4][2];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 Rect mTransformedBounds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 int mLeft;
244 int mTop;
245
246 // these are protected by an external lock
247 State mCurrentState;
248 State mDrawingState;
249 volatile int32_t mTransactionFlags;
250
251 // don't change, don't need a lock
252 bool mPremultipliedAlpha;
Mathias Agopiand1296592010-03-09 19:17:47 -0800253 String8 mName;
254 mutable bool mDebug;
255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 // atomic
258 volatile int32_t mInvalidate;
259
260
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700261protected:
262 virtual ~LayerBase();
263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265 LayerBase(const LayerBase& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266};
267
268
269// ---------------------------------------------------------------------------
270
271class LayerBaseClient : public LayerBase
272{
273public:
274 class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275
Mathias Agopian96f08192010-06-02 23:28:45 -0700276 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
277 const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 virtual ~LayerBaseClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 sp<Surface> getSurface();
281 virtual sp<Surface> createSurface() const;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700282 virtual sp<LayerBaseClient> getLayerBaseClient() const {
283 return const_cast<LayerBaseClient*>(this); }
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700284 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian285dbde2010-03-01 16:09:43 -0800285
Mathias Agopian96f08192010-06-02 23:28:45 -0700286 uint32_t getIdentity() const { return mIdentity; }
287
288 class Surface : public BnSurface {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 public:
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700290 int32_t getIdentity() const { return mIdentity; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700291
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700292 protected:
Mathias Agopian96f08192010-06-02 23:28:45 -0700293 Surface(const sp<SurfaceFlinger>& flinger, int identity,
Mathias Agopian9a112062009-04-17 19:36:26 -0700294 const sp<LayerBaseClient>& owner);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700295 virtual ~Surface();
296 virtual status_t onTransact(uint32_t code, const Parcel& data,
297 Parcel* reply, uint32_t flags);
298 sp<LayerBaseClient> getOwner() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
300 private:
Mathias Agopiana138f892010-05-21 17:24:35 -0700301 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
302 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700303 virtual status_t setBufferCount(int bufferCount);
304
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700305 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
306 virtual void postBuffer(ssize_t offset);
307 virtual void unregisterBuffers();
308 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800309 int32_t format, int32_t orientation);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700310
Mathias Agopian9a112062009-04-17 19:36:26 -0700311 protected:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312 friend class LayerBaseClient;
Mathias Agopian9a112062009-04-17 19:36:26 -0700313 sp<SurfaceFlinger> mFlinger;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314 int32_t mIdentity;
315 wp<LayerBaseClient> mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 };
317
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318 friend class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700320protected:
321 virtual void dump(String8& result, char* scratch, size_t size) const;
322
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700323private:
Mathias Agopian96f08192010-06-02 23:28:45 -0700324 mutable Mutex mLock;
325 mutable wp<Surface> mClientSurface;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700326 const wp<Client> mClientRef;
Mathias Agopian2e123242009-06-23 20:06:46 -0700327 // only read
Mathias Agopian96f08192010-06-02 23:28:45 -0700328 const uint32_t mIdentity;
329 static int32_t sIdentity;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330};
331
332// ---------------------------------------------------------------------------
333
334}; // namespace android
335
336#endif // ANDROID_LAYER_BASE_H