2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_LAYER_BASE_H
18 #define ANDROID_LAYER_BASE_H
21 #include <sys/types.h>
24 #include <EGL/eglext.h>
27 #include <utils/RefBase.h>
28 #include <utils/String8.h>
30 #include <ui/Region.h>
32 #include <gui/ISurfaceComposerClient.h>
34 #include <private/gui/LayerState.h>
36 #include "Transform.h"
37 #include "DisplayHardware/HWComposer.h"
41 // ---------------------------------------------------------------------------
47 class LayerBaseClient;
50 // ---------------------------------------------------------------------------
52 class LayerBase : virtual public RefBase
54 static int32_t sSequence;
57 LayerBase(SurfaceFlinger* flinger);
59 mutable bool contentDirty;
60 // regions below are in window-manager space
63 Region visibleNonTransparentRegion;
70 inline bool operator == (const Geometry& rhs) const {
71 return (w==rhs.w && h==rhs.h && crop==rhs.crop);
73 inline bool operator != (const Geometry& rhs) const {
74 return !operator == (rhs);
86 int32_t sequence; // changes when visible regions can change
88 Region transparentRegion;
92 friend class LayerBase;
93 GLfloat mVertices[4][2];
96 LayerMesh() : mNumVertices(4) { }
97 GLfloat const* getVertices() const {
98 return &mVertices[0][0];
100 size_t getVertexCount() const {
105 virtual void setName(const String8& name);
106 String8 getName() const;
108 // modify current state
109 bool setPosition(float x, float y);
110 bool setLayer(uint32_t z);
111 bool setSize(uint32_t w, uint32_t h);
112 bool setAlpha(uint8_t alpha);
113 bool setMatrix(const layer_state_t::matrix22_t& matrix);
114 bool setTransparentRegionHint(const Region& transparent);
115 bool setFlags(uint8_t flags, uint8_t mask);
116 bool setCrop(const Rect& crop);
117 bool setLayerStack(uint32_t layerStack);
119 void commitTransaction();
120 bool requestTransaction();
121 void forceVisibilityTransaction();
123 uint32_t getTransactionFlags(uint32_t flags);
124 uint32_t setTransactionFlags(uint32_t flags);
126 void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
127 Rect computeBounds() const;
130 virtual sp<LayerBaseClient> getLayerBaseClient() const;
131 virtual sp<Layer> getLayer() const;
133 virtual const char* getTypeId() const { return "LayerBase"; }
135 virtual void setGeometry(const sp<const DisplayDevice>& hw,
136 HWComposer::HWCLayerInterface& layer);
137 virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
138 HWComposer::HWCLayerInterface& layer);
139 virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
140 HWComposer::HWCLayerInterface& layer);
143 * draw - performs some global clipping optimizations
144 * and calls onDraw().
145 * Typically this method is not overridden, instead implement onDraw()
146 * to perform the actual drawing.
148 virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
149 virtual void draw(const sp<const DisplayDevice>& hw);
152 * onDraw - draws the surface.
154 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const = 0;
157 * initStates - called just after construction
159 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
162 * doTransaction - process the transaction. This is a good place to figure
163 * out which attributes of the surface have changed.
165 virtual uint32_t doTransaction(uint32_t transactionFlags);
168 * setVisibleRegion - called to set the new visible region. This gives
169 * a chance to update the new visible region or record the fact it changed.
171 virtual void setVisibleRegion(const Region& visibleRegion);
174 * setCoveredRegion - called when the covered region changes. The covered
175 * region corresponds to any area of the surface that is covered
176 * (transparently or not) by another surface.
178 virtual void setCoveredRegion(const Region& coveredRegion);
181 * setVisibleNonTransparentRegion - called when the visible and
182 * non-transparent region changes.
184 virtual void setVisibleNonTransparentRegion(const Region&
185 visibleNonTransparentRegion);
188 * latchBuffer - called each time the screen is redrawn and returns whether
189 * the visible regions need to be recomputed (this is a fairly heavy
190 * operation, so this should be set only if needed). Typically this is used
191 * to figure out if the content or size of a surface has changed.
193 virtual Region latchBuffer(bool& recomputeVisibleRegions);
196 * isOpaque - true if this surface is opaque
198 virtual bool isOpaque() const { return true; }
201 * needsDithering - true if this surface needs dithering
203 virtual bool needsDithering() const { return false; }
206 * needsLinearFiltering - true if this surface's state requires filtering
208 virtual bool needsFiltering(const sp<const DisplayDevice>& hw) const;
211 * isSecure - true if this surface is secure, that is if it prevents
212 * screenshots or VNC servers.
214 virtual bool isSecure() const { return false; }
217 * isProtected - true if the layer may contain protected content in the
218 * GRALLOC_USAGE_PROTECTED sense.
220 virtual bool isProtected() const { return false; }
223 * isVisible - true if this layer is visibile, false otherwise
225 virtual bool isVisible() const;
227 /** called with the state lock when the surface is removed from the
229 virtual void onRemoved() { }
231 /** called after page-flip
233 virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
234 HWComposer::HWCLayerInterface* layer);
236 /** called before composition.
237 * returns true if the layer has pending updates.
239 virtual bool onPreComposition() { return false; }
241 /** called before composition.
243 virtual void onPostComposition() { }
246 * Updates the SurfaceTexture's transform hint, for layers that have
249 virtual void updateTransformHint() const { }
251 /** always call base class first */
252 virtual void dump(String8& result, char* scratch, size_t size) const;
253 virtual void shortDump(String8& result, char* scratch, size_t size) const;
254 virtual void dumpStats(String8& result, char* buffer, size_t SIZE) const;
255 virtual void clearStats();
258 enum { // flags for doTransaction()
259 eDontUpdateGeometryState = 0x00000001,
260 eVisibleRegion = 0x00000002,
264 inline const State& drawingState() const { return mDrawingState; }
265 inline const State& currentState() const { return mCurrentState; }
266 inline State& currentState() { return mCurrentState; }
268 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
270 void setFiltering(bool filtering);
271 bool getFiltering() const;
274 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
275 GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
276 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
278 sp<SurfaceFlinger> mFlinger;
281 // accessed only in the main thread
282 // Whether filtering is forced on or not
285 // Whether filtering is needed b/c of the drawingstate
286 bool mNeedsFiltering;
289 // these are protected by an external lock
292 volatile int32_t mTransactionFlags;
294 // don't change, don't need a lock
295 bool mPremultipliedAlpha;
301 // called from class SurfaceFlinger
302 virtual ~LayerBase();
305 LayerBase(const LayerBase& rhs);
309 // ---------------------------------------------------------------------------
311 class LayerBaseClient : public LayerBase
314 LayerBaseClient(SurfaceFlinger* flinger, const sp<Client>& client);
316 virtual ~LayerBaseClient();
318 sp<ISurface> getSurface();
319 wp<IBinder> getSurfaceBinder() const;
320 virtual wp<IBinder> getSurfaceTextureBinder() const;
322 virtual sp<LayerBaseClient> getLayerBaseClient() const {
323 return const_cast<LayerBaseClient*>(this); }
325 virtual const char* getTypeId() const { return "LayerBaseClient"; }
327 uint32_t getIdentity() const { return mIdentity; }
330 virtual void dump(String8& result, char* scratch, size_t size) const;
331 virtual void shortDump(String8& result, char* scratch, size_t size) const;
334 sp<SurfaceFlinger> mFlinger;
335 wp<LayerBaseClient> mLayer;
339 LayerCleaner(const sp<SurfaceFlinger>& flinger,
340 const sp<LayerBaseClient>& layer);
344 virtual sp<ISurface> createSurface();
347 mutable bool mHasSurface;
348 wp<IBinder> mClientSurfaceBinder;
349 const wp<Client> mClientRef;
351 const uint32_t mIdentity;
352 static int32_t sIdentity;
355 // ---------------------------------------------------------------------------
357 }; // namespace android
359 #endif // ANDROID_LAYER_BASE_H