• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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_H
18 #define ANDROID_LAYER_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <EGL/egl.h>
24 #include <EGL/eglext.h>
25 
26 #include <utils/RefBase.h>
27 #include <utils/String8.h>
28 #include <utils/Timers.h>
29 
30 #include <ui/FrameStats.h>
31 #include <ui/GraphicBuffer.h>
32 #include <ui/PixelFormat.h>
33 #include <ui/Region.h>
34 
35 #include <gui/ISurfaceComposerClient.h>
36 
37 #include <private/gui/LayerState.h>
38 
39 #include "FrameTracker.h"
40 #include "Client.h"
41 #include "MonitoredProducer.h"
42 #include "SurfaceFlinger.h"
43 #include "SurfaceFlingerConsumer.h"
44 #include "Transform.h"
45 
46 #include "DisplayHardware/HWComposer.h"
47 #include "DisplayHardware/FloatRect.h"
48 #include "RenderEngine/Mesh.h"
49 #include "RenderEngine/Texture.h"
50 
51 namespace android {
52 
53 // ---------------------------------------------------------------------------
54 
55 class Client;
56 class Colorizer;
57 class DisplayDevice;
58 class GraphicBuffer;
59 class SurfaceFlinger;
60 
61 // ---------------------------------------------------------------------------
62 
63 /*
64  * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
65  * Layer is first referenced.
66  *
67  * This also implements onFrameAvailable(), which notifies SurfaceFlinger
68  * that new data has arrived.
69  */
70 class Layer : public SurfaceFlingerConsumer::ContentsChangedListener {
71     static int32_t sSequence;
72 
73 public:
74     mutable bool contentDirty;
75     // regions below are in window-manager space
76     Region visibleRegion;
77     Region coveredRegion;
78     Region visibleNonTransparentRegion;
79 
80     // Layer serial number.  This gives layers an explicit ordering, so we
81     // have a stable sort order when their layer stack and Z-order are
82     // the same.
83     int32_t sequence;
84 
85     enum { // flags for doTransaction()
86         eDontUpdateGeometryState = 0x00000001,
87         eVisibleRegion = 0x00000002,
88     };
89 
90     struct Geometry {
91         uint32_t w;
92         uint32_t h;
93         Rect crop;
94         inline bool operator ==(const Geometry& rhs) const {
95             return (w == rhs.w && h == rhs.h && crop == rhs.crop);
96         }
97         inline bool operator !=(const Geometry& rhs) const {
98             return !operator ==(rhs);
99         }
100     };
101 
102     struct State {
103         Geometry active;
104         Geometry requested;
105         uint32_t z;
106         uint32_t layerStack;
107         uint8_t alpha;
108         uint8_t flags;
109         uint8_t reserved[2];
110         int32_t sequence; // changes when visible regions can change
111         Transform transform;
112         // the transparentRegion hint is a bit special, it's latched only
113         // when we receive a buffer -- this is because it's "content"
114         // dependent.
115         Region activeTransparentRegion;
116         Region requestedTransparentRegion;
117     };
118 
119     // -----------------------------------------------------------------------
120 
121     Layer(SurfaceFlinger* flinger, const sp<Client>& client,
122             const String8& name, uint32_t w, uint32_t h, uint32_t flags);
123 
124     virtual ~Layer();
125 
126     // the this layer's size and format
127     status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
128 
129     // modify current state
130     bool setPosition(float x, float y);
131     bool setLayer(uint32_t z);
132     bool setSize(uint32_t w, uint32_t h);
133     bool setAlpha(uint8_t alpha);
134     bool setMatrix(const layer_state_t::matrix22_t& matrix);
135     bool setTransparentRegionHint(const Region& transparent);
136     bool setFlags(uint8_t flags, uint8_t mask);
137     bool setCrop(const Rect& crop);
138     bool setLayerStack(uint32_t layerStack);
139 
140     uint32_t getTransactionFlags(uint32_t flags);
141     uint32_t setTransactionFlags(uint32_t flags);
142 
143     void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
144             bool useIdentityTransform) const;
145     Rect computeBounds() const;
146 
147     sp<IBinder> getHandle();
148     sp<IGraphicBufferProducer> getProducer() const;
149     const String8& getName() const;
150 
151     // -----------------------------------------------------------------------
152     // Virtuals
153 
getTypeId()154     virtual const char* getTypeId() const { return "Layer"; }
155 
156     /*
157      * isOpaque - true if this surface is opaque
158      *
159      * This takes into account the buffer format (i.e. whether or not the
160      * pixel format includes an alpha channel) and the "opaque" flag set
161      * on the layer.  It does not examine the current plane alpha value.
162      */
163     virtual bool isOpaque(const Layer::State& s) const;
164 
165     /*
166      * isSecure - true if this surface is secure, that is if it prevents
167      * screenshots or VNC servers.
168      */
isSecure()169     virtual bool isSecure() const           { return mSecure; }
170 
171     /*
172      * isProtected - true if the layer may contain protected content in the
173      * GRALLOC_USAGE_PROTECTED sense.
174      */
175     virtual bool isProtected() const;
176 
177     /*
178      * isVisible - true if this layer is visible, false otherwise
179      */
180     virtual bool isVisible() const;
181 
182     /*
183      * isFixedSize - true if content has a fixed size
184      */
185     virtual bool isFixedSize() const;
186 
187 protected:
188     /*
189      * onDraw - draws the surface.
190      */
191     virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
192             bool useIdentityTransform) const;
193 
194 public:
195     // -----------------------------------------------------------------------
196 
197     void setGeometry(const sp<const DisplayDevice>& hw,
198             HWComposer::HWCLayerInterface& layer);
199     void setPerFrameData(const sp<const DisplayDevice>& hw,
200             HWComposer::HWCLayerInterface& layer);
201     void setAcquireFence(const sp<const DisplayDevice>& hw,
202             HWComposer::HWCLayerInterface& layer);
203 
204     Rect getPosition(const sp<const DisplayDevice>& hw);
205 
206     /*
207      * called after page-flip
208      */
209     void onLayerDisplayed(const sp<const DisplayDevice>& hw,
210             HWComposer::HWCLayerInterface* layer);
211 
212     /*
213      * called before composition.
214      * returns true if the layer has pending updates.
215      */
216     bool onPreComposition();
217 
218     /*
219      *  called after composition.
220      */
221     void onPostComposition();
222 
223     /*
224      * draw - performs some global clipping optimizations
225      * and calls onDraw().
226      */
227     void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
228     void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform) const;
229     void draw(const sp<const DisplayDevice>& hw) const;
230 
231     /*
232      * doTransaction - process the transaction. This is a good place to figure
233      * out which attributes of the surface have changed.
234      */
235     uint32_t doTransaction(uint32_t transactionFlags);
236 
237     /*
238      * setVisibleRegion - called to set the new visible region. This gives
239      * a chance to update the new visible region or record the fact it changed.
240      */
241     void setVisibleRegion(const Region& visibleRegion);
242 
243     /*
244      * setCoveredRegion - called when the covered region changes. The covered
245      * region corresponds to any area of the surface that is covered
246      * (transparently or not) by another surface.
247      */
248     void setCoveredRegion(const Region& coveredRegion);
249 
250     /*
251      * setVisibleNonTransparentRegion - called when the visible and
252      * non-transparent region changes.
253      */
254     void setVisibleNonTransparentRegion(const Region&
255             visibleNonTransparentRegion);
256 
257     /*
258      * latchBuffer - called each time the screen is redrawn and returns whether
259      * the visible regions need to be recomputed (this is a fairly heavy
260      * operation, so this should be set only if needed). Typically this is used
261      * to figure out if the content or size of a surface has changed.
262      */
263     Region latchBuffer(bool& recomputeVisibleRegions);
264 
isPotentialCursor()265     bool isPotentialCursor() const { return mPotentialCursor;}
266 
267     /*
268      * called with the state lock when the surface is removed from the
269      * current list
270      */
271     void onRemoved();
272 
273 
274     // Updates the transform hint in our SurfaceFlingerConsumer to match
275     // the current orientation of the display device.
276     void updateTransformHint(const sp<const DisplayDevice>& hw) const;
277 
278     /*
279      * returns the rectangle that crops the content of the layer and scales it
280      * to the layer's size.
281      */
282     Rect getContentCrop() const;
283 
284     /*
285      * Returns if a frame is queued.
286      */
hasQueuedFrame()287     bool hasQueuedFrame() const { return mQueuedFrames > 0 || mSidebandStreamChanged; }
288 
289     // -----------------------------------------------------------------------
290 
291     void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
292     void setFiltering(bool filtering);
293     bool getFiltering() const;
294 
295     // only for debugging
getActiveBuffer()296     inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
297 
getDrawingState()298     inline  const State&    getDrawingState() const { return mDrawingState; }
getCurrentState()299     inline  const State&    getCurrentState() const { return mCurrentState; }
getCurrentState()300     inline  State&          getCurrentState()       { return mCurrentState; }
301 
302 
303     /* always call base class first */
304     void dump(String8& result, Colorizer& colorizer) const;
305     void dumpFrameStats(String8& result) const;
306     void clearFrameStats();
307     void logFrameStats();
308     void getFrameStats(FrameStats* outStats) const;
309 
310 protected:
311     // constant
312     sp<SurfaceFlinger> mFlinger;
313 
314     virtual void onFirstRef();
315 
316     /*
317      * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
318      * is called.
319      */
320     class LayerCleaner {
321         sp<SurfaceFlinger> mFlinger;
322         wp<Layer> mLayer;
323     protected:
324         ~LayerCleaner();
325     public:
326         LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
327     };
328 
329 
330 private:
331     // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
332     virtual void onFrameAvailable();
333     virtual void onSidebandStreamChanged();
334 
335     void commitTransaction();
336 
337     // needsLinearFiltering - true if this surface's state requires filtering
338     bool needsFiltering(const sp<const DisplayDevice>& hw) const;
339 
340     uint32_t getEffectiveUsage(uint32_t usage) const;
341     FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
342     bool isCropped() const;
343     static bool getOpacityForFormat(uint32_t format);
344 
345     // drawing
346     void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
347             float r, float g, float b, float alpha) const;
348     void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
349             bool useIdentityTransform) const;
350 
351     // Temporary - Used only for LEGACY camera mode.
352     uint32_t getProducerStickyTransform() const;
353 
354 
355     // -----------------------------------------------------------------------
356 
357     // constants
358     sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
359     sp<IGraphicBufferProducer> mProducer;
360     uint32_t mTextureName;      // from GLES
361     bool mPremultipliedAlpha;
362     String8 mName;
363     mutable bool mDebug;
364     PixelFormat mFormat;
365 
366     // these are protected by an external lock
367     State mCurrentState;
368     State mDrawingState;
369     volatile int32_t mTransactionFlags;
370 
371     // thread-safe
372     volatile int32_t mQueuedFrames;
373     volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
374     FrameTracker mFrameTracker;
375 
376     // main thread
377     sp<GraphicBuffer> mActiveBuffer;
378     sp<NativeHandle> mSidebandStream;
379     Rect mCurrentCrop;
380     uint32_t mCurrentTransform;
381     uint32_t mCurrentScalingMode;
382     bool mCurrentOpacity;
383     bool mRefreshPending;
384     bool mFrameLatencyNeeded;
385     // Whether filtering is forced on or not
386     bool mFiltering;
387     // Whether filtering is needed b/c of the drawingstate
388     bool mNeedsFiltering;
389     // The mesh used to draw the layer in GLES composition mode
390     mutable Mesh mMesh;
391     // The texture used to draw the layer in GLES composition mode
392     mutable Texture mTexture;
393 
394     // page-flip thread (currently main thread)
395     bool mSecure; // no screenshots
396     bool mProtectedByApp; // application requires protected path to external sink
397 
398     // protected by mLock
399     mutable Mutex mLock;
400     // Set to true once we've returned this surface's handle
401     mutable bool mHasSurface;
402     const wp<Client> mClientRef;
403 
404     // This layer can be a cursor on some displays.
405     bool mPotentialCursor;
406 };
407 
408 // ---------------------------------------------------------------------------
409 
410 }; // namespace android
411 
412 #endif // ANDROID_LAYER_H
413