blob: 7f6214510d33b75e85131afa5347d7863ef68d99 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopian999543b2009-06-23 18:08:22 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
Mathias Agopian8c20ca32010-02-22 03:15:57 -080025#include <GLES/gl.h>
Mathias Agopian999543b2009-06-23 18:08:22 -070026
Mathias Agopian1473f462009-04-10 14:24:30 -070027#include <utils/RefBase.h>
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Mathias Agopian770492c2010-05-28 14:22:23 -070031#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080032#include <private/surfaceflinger/LayerState.h>
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <pixelflinger/pixelflinger.h>
35
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070036#include <hardware/hwcomposer.h>
37
Mathias Agopian0b38aa02010-12-07 23:41:55 -080038#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include "Transform.h"
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045class Client;
Mathias Agopian7bb843c2011-04-20 14:20:59 -070046class DisplayHardware;
Mathias Agopian6950e422009-10-05 17:07:12 -070047class GraphicBuffer;
48class GraphicPlane;
Mathias Agopian951c12e2011-09-20 15:13:14 -070049class Layer;
Mathias Agopian7623da42010-06-01 15:12:58 -070050class LayerBaseClient;
Mathias Agopian6950e422009-10-05 17:07:12 -070051class SurfaceFlinger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53// ---------------------------------------------------------------------------
54
Mathias Agopian1473f462009-04-10 14:24:30 -070055class LayerBase : public RefBase
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056{
Mathias Agopian1efba9a2010-08-10 18:09:09 -070057 static int32_t sSequence;
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059public:
Mathias Agopian9bce8732010-04-20 17:55:49 -070060 LayerBase(SurfaceFlinger* flinger, DisplayID display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 DisplayID dpy;
63 mutable bool contentDirty;
64 Region visibleRegionScreen;
65 Region transparentRegionScreen;
66 Region coveredRegionScreen;
Mathias Agopian1efba9a2010-08-10 18:09:09 -070067 int32_t sequence;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69 struct State {
70 uint32_t w;
71 uint32_t h;
Mathias Agopiane1b6f242009-09-29 22:39:22 -070072 uint32_t requested_w;
73 uint32_t requested_h;
The Android Open Source Project9066cfe2009-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
Jamie Gennis5b315da2011-09-16 17:31:54 -070084 virtual void setName(const String8& name);
Mathias Agopian015b5972010-03-09 19:17:47 -080085 String8 getName() const;
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 // modify current state
Mathias Agopian34cb9f2a2011-08-30 18:51:54 -070088 bool setPosition(float x, float y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 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 Agopian88516172009-09-29 22:32:36 -070096 void commitTransaction();
The Android Open Source Project9066cfe2009-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 Agopian015b5972010-03-09 19:17:47 -0800107
Mathias Agopian7623da42010-06-01 15:12:58 -0700108 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
Mathias Agopian951c12e2011-09-20 15:13:14 -0700109 virtual sp<Layer> getLayer() const { return 0; }
Mathias Agopian7623da42010-06-01 15:12:58 -0700110
Mathias Agopian9bce8732010-04-20 17:55:49 -0700111 virtual const char* getTypeId() const { return "LayerBase"; }
Mathias Agopian9bce8732010-04-20 17:55:49 -0700112
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700113 virtual void setGeometry(hwc_layer_t* hwcl);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700114 virtual void setPerFrameData(hwc_layer_t* hwcl);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700115 void setOverlay(bool inOverlay);
116 bool isOverlay() const;
117
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
120 * draw - performs some global clipping optimizations
121 * and calls onDraw().
122 * Typically this method is not overridden, instead implement onDraw()
123 * to perform the actual drawing.
124 */
125 virtual void draw(const Region& clip) const;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700126 virtual void drawForSreenShot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
128 /**
129 * onDraw - draws the surface.
130 */
131 virtual void onDraw(const Region& clip) const = 0;
132
133 /**
134 * initStates - called just after construction
135 */
136 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
137
138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * doTransaction - process the transaction. This is a good place to figure
140 * out which attributes of the surface have changed.
141 */
142 virtual uint32_t doTransaction(uint32_t transactionFlags);
143
144 /**
145 * setVisibleRegion - called to set the new visible region. This gives
146 * a chance to update the new visible region or record the fact it changed.
147 */
148 virtual void setVisibleRegion(const Region& visibleRegion);
149
150 /**
151 * setCoveredRegion - called when the covered region changes. The covered
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700152 * region corresponds to any area of the surface that is covered
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 * (transparently or not) by another surface.
154 */
155 virtual void setCoveredRegion(const Region& coveredRegion);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 * validateVisibility - cache a bunch of things
159 */
160 virtual void validateVisibility(const Transform& globalTransform);
161
162 /**
163 * lockPageFlip - called each time the screen is redrawn and returns whether
164 * the visible regions need to be recomputed (this is a fairly heavy
165 * operation, so this should be set only if needed). Typically this is used
166 * to figure out if the content or size of a surface has changed.
167 */
168 virtual void lockPageFlip(bool& recomputeVisibleRegions);
169
170 /**
171 * unlockPageFlip - called each time the screen is redrawn. updates the
172 * final dirty region wrt the planeTransform.
173 * At this point, all visible regions, surface position and size, etc... are
174 * correct.
175 */
176 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
177
178 /**
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700179 * isOpaque - true if this surface is opaque
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 */
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700181 virtual bool isOpaque() const { return true; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
183 /**
Mathias Agopiancc934762009-09-23 19:16:27 -0700184 * needsDithering - true if this surface needs dithering
185 */
186 virtual bool needsDithering() const { return false; }
187
188 /**
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700189 * needsLinearFiltering - true if this surface's state requires filtering
Mathias Agopian92377032010-05-26 22:08:52 -0700190 */
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700191 virtual bool needsFiltering() const { return mNeedsFiltering; }
Mathias Agopian92377032010-05-26 22:08:52 -0700192
193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * isSecure - true if this surface is secure, that is if it prevents
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700195 * screenshots or VNC servers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 */
197 virtual bool isSecure() const { return false; }
198
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800199 /**
Jamie Gennisf72606c2011-03-09 17:05:02 -0800200 * isProtected - true if the layer may contain protected content in the
201 * GRALLOC_USAGE_PROTECTED sense.
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800202 */
Jamie Gennisf72606c2011-03-09 17:05:02 -0800203 virtual bool isProtected() const { return false; }
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800204
Mathias Agopian0c4cec72009-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 Agopian6cf0db22009-04-17 19:36:26 -0700208
Mathias Agopian9bce8732010-04-20 17:55:49 -0700209 /** always call base class first */
210 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian06a61e22011-01-19 16:15:53 -0800211 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian9bce8732010-04-20 17:55:49 -0700212
213
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700214 enum { // flags for doTransaction()
215 eVisibleRegion = 0x00000002,
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700216 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
218
219 inline const State& drawingState() const { return mDrawingState; }
220 inline const State& currentState() const { return mCurrentState; }
221 inline State& currentState() { return mCurrentState; }
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 int32_t getOrientation() const { return mOrientation; }
Jamie Gennisfd2429d2011-09-23 15:54:34 -0700224 int32_t getPlaneOrientation() const { return mPlaneOrientation; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
226protected:
227 const GraphicPlane& graphicPlane(int dpy) const;
228 GraphicPlane& graphicPlane(int dpy);
229
Mathias Agopian0d3c0062010-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 Project9066cfe2009-03-03 19:31:44 -0800232 void clearWithOpenGL(const Region& clip) const;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700233 void drawWithOpenGL(const Region& clip) const;
234
235 void setFiltering(bool filtering);
236 bool getFiltering() const;
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700237
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700238 sp<SurfaceFlinger> mFlinger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 uint32_t mFlags;
240
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700241private:
242 // accessed only in the main thread
243 // Whether filtering is forced on or not
244 bool mFiltering;
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 // cached during validateVisibility()
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700247 // Whether filtering is needed b/c of the drawingstate
Mathias Agopian92377032010-05-26 22:08:52 -0700248 bool mNeedsFiltering;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700249
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700250 // this layer is currently handled by the hwc. this is
251 // updated at composition time, always frmo the composition
252 // thread.
253 bool mInOverlay;
254
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700255protected:
256 // cached during validateVisibility()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 int32_t mOrientation;
Jamie Gennisfd2429d2011-09-23 15:54:34 -0700258 int32_t mPlaneOrientation;
Mathias Agopian3f883272011-08-18 18:31:00 -0700259 Transform mTransform;
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700260 GLfloat mVertices[4][2];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 Rect mTransformedBounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 // these are protected by an external lock
264 State mCurrentState;
265 State mDrawingState;
266 volatile int32_t mTransactionFlags;
267
268 // don't change, don't need a lock
269 bool mPremultipliedAlpha;
Mathias Agopian015b5972010-03-09 19:17:47 -0800270 String8 mName;
271 mutable bool mDebug;
272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 // atomic
275 volatile int32_t mInvalidate;
276
277
Mathias Agopian69608112011-05-19 15:38:14 -0700278public:
279 // called from class SurfaceFlinger
Mathias Agopian1473f462009-04-10 14:24:30 -0700280 virtual ~LayerBase();
281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282private:
Mathias Agopian1473f462009-04-10 14:24:30 -0700283 LayerBase(const LayerBase& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284};
285
286
287// ---------------------------------------------------------------------------
288
289class LayerBaseClient : public LayerBase
290{
291public:
Mathias Agopian593c05c2010-06-02 23:28:45 -0700292 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
293 const sp<Client>& client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700295 virtual ~LayerBaseClient();
296
297 sp<ISurface> getSurface();
Mathias Agopiand35c6662011-01-25 20:17:45 -0800298 wp<IBinder> getSurfaceBinder() const;
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700299 virtual wp<IBinder> getSurfaceTextureBinder() const;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700300
Mathias Agopian7623da42010-06-01 15:12:58 -0700301 virtual sp<LayerBaseClient> getLayerBaseClient() const {
302 return const_cast<LayerBaseClient*>(this); }
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700303
Mathias Agopian9bce8732010-04-20 17:55:49 -0700304 virtual const char* getTypeId() const { return "LayerBaseClient"; }
Mathias Agopian5d26c1e2010-03-01 16:09:43 -0800305
Mathias Agopian593c05c2010-06-02 23:28:45 -0700306 uint32_t getIdentity() const { return mIdentity; }
307
Mathias Agopian9bce8732010-04-20 17:55:49 -0700308protected:
309 virtual void dump(String8& result, char* scratch, size_t size) const;
Mathias Agopian06a61e22011-01-19 16:15:53 -0800310 virtual void shortDump(String8& result, char* scratch, size_t size) const;
Mathias Agopian9bce8732010-04-20 17:55:49 -0700311
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700312 class LayerCleaner {
313 sp<SurfaceFlinger> mFlinger;
314 wp<LayerBaseClient> mLayer;
315 protected:
316 ~LayerCleaner();
317 public:
318 LayerCleaner(const sp<SurfaceFlinger>& flinger,
319 const sp<LayerBaseClient>& layer);
320 };
321
Mathias Agopian1473f462009-04-10 14:24:30 -0700322private:
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700323 virtual sp<ISurface> createSurface();
324
Mathias Agopian593c05c2010-06-02 23:28:45 -0700325 mutable Mutex mLock;
Mathias Agopian1b0114f2011-02-15 19:01:06 -0800326 mutable bool mHasSurface;
Mathias Agopiand35c6662011-01-25 20:17:45 -0800327 wp<IBinder> mClientSurfaceBinder;
Mathias Agopian7623da42010-06-01 15:12:58 -0700328 const wp<Client> mClientRef;
Mathias Agopianc6603952009-06-23 20:06:46 -0700329 // only read
Mathias Agopian593c05c2010-06-02 23:28:45 -0700330 const uint32_t mIdentity;
331 static int32_t sIdentity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332};
333
334// ---------------------------------------------------------------------------
335
336}; // namespace android
337
338#endif // ANDROID_LAYER_BASE_H