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_H
18 #define ANDROID_LAYER_H
21 #include <sys/types.h>
24 #include <EGL/eglext.h>
26 #include <utils/RefBase.h>
27 #include <utils/String8.h>
28 #include <utils/Timers.h>
30 #include <ui/GraphicBuffer.h>
31 #include <ui/PixelFormat.h>
32 #include <ui/Region.h>
34 #include <gui/ISurfaceComposerClient.h>
36 #include <private/gui/LayerState.h>
38 #include "FrameTracker.h"
40 #include "SurfaceFlinger.h"
41 #include "SurfaceFlingerConsumer.h"
42 #include "SurfaceTextureLayer.h"
43 #include "Transform.h"
45 #include "DisplayHardware/HWComposer.h"
46 #include "DisplayHardware/FloatRect.h"
47 #include "RenderEngine/Mesh.h"
51 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
62 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
63 * Layer is first referenced.
65 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
66 * that new data has arrived.
68 class Layer : public SurfaceFlingerConsumer::FrameAvailableListener {
69 static int32_t sSequence;
72 mutable bool contentDirty;
73 // regions below are in window-manager space
76 Region visibleNonTransparentRegion;
79 enum { // flags for doTransaction()
80 eDontUpdateGeometryState = 0x00000001,
81 eVisibleRegion = 0x00000002,
88 inline bool operator ==(const Geometry& rhs) const {
89 return (w == rhs.w && h == rhs.h && crop == rhs.crop);
91 inline bool operator !=(const Geometry& rhs) const {
92 return !operator ==(rhs);
104 int32_t sequence; // changes when visible regions can change
106 // the transparentRegion hint is a bit special, it's latched only
107 // when we receive a buffer -- this is because it's "content"
109 Region activeTransparentRegion;
110 Region requestedTransparentRegion;
113 // -----------------------------------------------------------------------
115 Layer(SurfaceFlinger* flinger, const sp<Client>& client,
116 const String8& name, uint32_t w, uint32_t h, uint32_t flags);
120 // the this layer's size and format
121 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
123 // modify current state
124 bool setPosition(float x, float y);
125 bool setLayer(uint32_t z);
126 bool setSize(uint32_t w, uint32_t h);
127 bool setAlpha(uint8_t alpha);
128 bool setMatrix(const layer_state_t::matrix22_t& matrix);
129 bool setTransparentRegionHint(const Region& transparent);
130 bool setFlags(uint8_t flags, uint8_t mask);
131 bool setCrop(const Rect& crop);
132 bool setLayerStack(uint32_t layerStack);
134 uint32_t getTransactionFlags(uint32_t flags);
135 uint32_t setTransactionFlags(uint32_t flags);
137 void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh) const;
138 Rect computeBounds() const;
140 sp<IBinder> getHandle();
141 sp<IGraphicBufferProducer> getBufferQueue() const;
142 const String8& getName() const;
144 // -----------------------------------------------------------------------
147 virtual const char* getTypeId() const { return "Layer"; }
150 * isOpaque - true if this surface is opaque
152 virtual bool isOpaque() const;
155 * isSecure - true if this surface is secure, that is if it prevents
156 * screenshots or VNC servers.
158 virtual bool isSecure() const { return mSecure; }
161 * isProtected - true if the layer may contain protected content in the
162 * GRALLOC_USAGE_PROTECTED sense.
164 virtual bool isProtected() const;
167 * isVisible - true if this layer is visible, false otherwise
169 virtual bool isVisible() const;
172 * isFixedSize - true if content has a fixed size
174 virtual bool isFixedSize() const;
178 * onDraw - draws the surface.
180 virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
183 // -----------------------------------------------------------------------
185 void setGeometry(const sp<const DisplayDevice>& hw,
186 HWComposer::HWCLayerInterface& layer);
187 void setPerFrameData(const sp<const DisplayDevice>& hw,
188 HWComposer::HWCLayerInterface& layer);
189 void setAcquireFence(const sp<const DisplayDevice>& hw,
190 HWComposer::HWCLayerInterface& layer);
193 * called after page-flip
195 void onLayerDisplayed(const sp<const DisplayDevice>& hw,
196 HWComposer::HWCLayerInterface* layer);
199 * called before composition.
200 * returns true if the layer has pending updates.
202 bool onPreComposition();
205 * called after composition.
207 void onPostComposition();
210 * draw - performs some global clipping optimizations
211 * and calls onDraw().
213 void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
214 void draw(const sp<const DisplayDevice>& hw);
217 * doTransaction - process the transaction. This is a good place to figure
218 * out which attributes of the surface have changed.
220 uint32_t doTransaction(uint32_t transactionFlags);
223 * setVisibleRegion - called to set the new visible region. This gives
224 * a chance to update the new visible region or record the fact it changed.
226 void setVisibleRegion(const Region& visibleRegion);
229 * setCoveredRegion - called when the covered region changes. The covered
230 * region corresponds to any area of the surface that is covered
231 * (transparently or not) by another surface.
233 void setCoveredRegion(const Region& coveredRegion);
236 * setVisibleNonTransparentRegion - called when the visible and
237 * non-transparent region changes.
239 void setVisibleNonTransparentRegion(const Region&
240 visibleNonTransparentRegion);
243 * latchBuffer - called each time the screen is redrawn and returns whether
244 * the visible regions need to be recomputed (this is a fairly heavy
245 * operation, so this should be set only if needed). Typically this is used
246 * to figure out if the content or size of a surface has changed.
248 Region latchBuffer(bool& recomputeVisibleRegions);
251 * called with the state lock when the surface is removed from the
257 // Updates the transform hint in our SurfaceFlingerConsumer to match
258 // the current orientation of the display device.
259 void updateTransformHint(const sp<const DisplayDevice>& hw) const;
262 * returns the rectangle that crops the content of the layer and scales it
263 * to the layer's size.
265 Rect getContentCrop() const;
267 // -----------------------------------------------------------------------
269 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
270 void setFiltering(bool filtering);
271 bool getFiltering() const;
273 // only for debugging
274 inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
276 inline const State& getDrawingState() const { return mDrawingState; }
277 inline const State& getCurrentState() const { return mCurrentState; }
278 inline State& getCurrentState() { return mCurrentState; }
281 /* always call base class first */
282 void dump(String8& result, Colorizer& colorizer) const;
283 void dumpStats(String8& result) const;
285 void logFrameStats();
289 sp<SurfaceFlinger> mFlinger;
291 virtual void onFirstRef();
294 * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
298 sp<SurfaceFlinger> mFlinger;
303 LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
308 // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
309 virtual void onFrameAvailable();
311 void commitTransaction();
313 // needsLinearFiltering - true if this surface's state requires filtering
314 bool needsFiltering(const sp<const DisplayDevice>& hw) const;
316 uint32_t getEffectiveUsage(uint32_t usage) const;
317 FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
318 bool isCropped() const;
319 static bool getOpacityForFormat(uint32_t format);
322 void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
323 float r, float g, float b, float alpha) const;
324 void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
327 // -----------------------------------------------------------------------
330 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
331 sp<BufferQueue> mBufferQueue;
332 uint32_t mTextureName;
333 bool mPremultipliedAlpha;
339 // these are protected by an external lock
342 volatile int32_t mTransactionFlags;
345 volatile int32_t mQueuedFrames;
346 FrameTracker mFrameTracker;
349 sp<GraphicBuffer> mActiveBuffer;
351 uint32_t mCurrentTransform;
352 uint32_t mCurrentScalingMode;
353 bool mCurrentOpacity;
354 bool mRefreshPending;
355 bool mFrameLatencyNeeded;
356 // Whether filtering is forced on or not
358 // Whether filtering is needed b/c of the drawingstate
359 bool mNeedsFiltering;
360 // The mesh used to draw the layer in GLES composition mode
363 // page-flip thread (currently main thread)
364 bool mSecure; // no screenshots
365 bool mProtectedByApp; // application requires protected path to external sink
367 // protected by mLock
369 // Set to true once we've returned this surface's handle
370 mutable bool mHasSurface;
371 const wp<Client> mClientRef;
374 // ---------------------------------------------------------------------------
376 }; // namespace android
378 #endif // ANDROID_LAYER_H