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>
29 #include <ui/Region.h>
30 #include <ui/Overlay.h>
32 #include <surfaceflinger/ISurfaceComposerClient.h>
33 #include <private/surfaceflinger/SharedBufferStack.h>
34 #include <private/surfaceflinger/LayerState.h>
36 #include <pixelflinger/pixelflinger.h>
38 #include "DisplayHardware/DisplayHardware.h"
39 #include "Transform.h"
43 // ---------------------------------------------------------------------------
45 class DisplayHardware;
49 class LayerBaseClient;
53 // ---------------------------------------------------------------------------
55 class LayerBase : public RefBase
57 static int32_t sSequence;
60 LayerBase(SurfaceFlinger* flinger, DisplayID display);
63 mutable bool contentDirty;
64 Region visibleRegionScreen;
65 Region transparentRegionScreen;
66 Region coveredRegionScreen;
78 int32_t sequence; // changes when visible regions can change
81 Region transparentRegion;
84 void setName(const String8& name);
85 String8 getName() const;
87 // 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);
96 void commitTransaction();
97 bool requestTransaction();
98 void forceVisibilityTransaction();
100 uint32_t getTransactionFlags(uint32_t flags);
101 uint32_t setTransactionFlags(uint32_t flags);
103 Rect visibleBounds() const;
104 void drawRegion(const Region& reg) const;
108 virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
110 virtual const char* getTypeId() const { return "LayerBase"; }
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.
118 virtual void draw(const Region& clip) const;
119 virtual void drawForSreenShot() const;
124 virtual bool setBypass(bool enable) { return false; }
127 * onDraw - draws the surface.
129 virtual void onDraw(const Region& clip) const = 0;
132 * initStates - called just after construction
134 virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
137 * doTransaction - process the transaction. This is a good place to figure
138 * out which attributes of the surface have changed.
140 virtual uint32_t doTransaction(uint32_t transactionFlags);
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.
146 virtual void setVisibleRegion(const Region& visibleRegion);
149 * setCoveredRegion - called when the covered region changes. The covered
150 * region corresponds to any area of the surface that is covered
151 * (transparently or not) by another surface.
153 virtual void setCoveredRegion(const Region& coveredRegion);
156 * validateVisibility - cache a bunch of things
158 virtual void validateVisibility(const Transform& globalTransform);
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.
166 virtual void lockPageFlip(bool& recomputeVisibleRegions);
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
174 virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
177 * needsBlending - true if this surface needs blending
179 virtual bool needsBlending() const { return false; }
182 * needsDithering - true if this surface needs dithering
184 virtual bool needsDithering() const { return false; }
187 * needsLinearFiltering - true if this surface needs filtering
189 virtual bool needsFiltering() const {
190 return (!(mFlags & DisplayHardware::SLOW_CONFIG)) && mNeedsFiltering;
194 * isSecure - true if this surface is secure, that is if it prevents
195 * screenshots or VNC servers.
197 virtual bool isSecure() const { return false; }
199 /** Called from the main thread, when the surface is removed from the
201 virtual status_t ditch() { return NO_ERROR; }
203 /** called with the state lock when the surface is removed from the
205 virtual void onRemoved() { };
207 /** always call base class first */
208 virtual void dump(String8& result, char* scratch, size_t size) const;
209 virtual void shortDump(String8& result, char* scratch, size_t size) const;
212 enum { // flags for doTransaction()
213 eVisibleRegion = 0x00000002,
217 inline const State& drawingState() const { return mDrawingState; }
218 inline const State& currentState() const { return mCurrentState; }
219 inline State& currentState() { return mCurrentState; }
221 int32_t getOrientation() const { return mOrientation; }
222 int tx() const { return mLeft; }
223 int ty() const { return mTop; }
226 const GraphicPlane& graphicPlane(int dpy) const;
227 GraphicPlane& graphicPlane(int dpy);
229 void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
230 GLclampf b, GLclampf alpha) const;
231 void clearWithOpenGL(const Region& clip) const;
232 void drawWithOpenGL(const Region& clip, const Texture& texture) const;
234 // these must be called from the post/drawing thread
235 void setBufferCrop(const Rect& crop);
236 void setBufferTransform(uint32_t transform);
238 sp<SurfaceFlinger> mFlinger;
241 // post/drawing thread
243 uint32_t mBufferTransform;
245 // cached during validateVisibility()
246 bool mNeedsFiltering;
247 int32_t mOrientation;
248 GLfloat mVertices[4][2];
249 Rect mTransformedBounds;
253 // these are protected by an external lock
256 volatile int32_t mTransactionFlags;
258 // don't change, don't need a lock
259 bool mPremultipliedAlpha;
265 volatile int32_t mInvalidate;
269 virtual ~LayerBase();
272 LayerBase(const LayerBase& rhs);
276 // ---------------------------------------------------------------------------
278 class LayerBaseClient : public LayerBase
283 LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
284 const sp<Client>& client);
285 virtual ~LayerBaseClient();
287 sp<Surface> getSurface();
288 virtual sp<Surface> createSurface() const;
289 virtual sp<LayerBaseClient> getLayerBaseClient() const {
290 return const_cast<LayerBaseClient*>(this); }
291 virtual const char* getTypeId() const { return "LayerBaseClient"; }
293 uint32_t getIdentity() const { return mIdentity; }
295 class Surface : public BnSurface {
297 int32_t getIdentity() const { return mIdentity; }
300 Surface(const sp<SurfaceFlinger>& flinger, int identity,
301 const sp<LayerBaseClient>& owner);
303 virtual status_t onTransact(uint32_t code, const Parcel& data,
304 Parcel* reply, uint32_t flags);
305 sp<LayerBaseClient> getOwner() const;
308 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
309 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
310 virtual status_t setBufferCount(int bufferCount);
312 virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
313 virtual void postBuffer(ssize_t offset);
314 virtual void unregisterBuffers();
315 virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
316 int32_t format, int32_t orientation);
319 friend class LayerBaseClient;
320 sp<SurfaceFlinger> mFlinger;
322 wp<LayerBaseClient> mOwner;
325 friend class Surface;
328 virtual void dump(String8& result, char* scratch, size_t size) const;
329 virtual void shortDump(String8& result, char* scratch, size_t size) const;
333 mutable wp<Surface> mClientSurface;
334 const wp<Client> mClientRef;
336 const uint32_t mIdentity;
337 static int32_t sIdentity;
340 // ---------------------------------------------------------------------------
342 }; // namespace android
344 #endif // ANDROID_LAYER_BASE_H