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