Layer.h revision 6b44267a3beb457e220cad0666c039d3a765cdb2
1343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih/*
2343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Copyright (C) 2007 The Android Open Source Project
3343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *
4343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Licensed under the Apache License, Version 2.0 (the "License");
5343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * you may not use this file except in compliance with the License.
6343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * You may obtain a copy of the License at
7343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *
8343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *      http://www.apache.org/licenses/LICENSE-2.0
9343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *
10343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Unless required by applicable law or agreed to in writing, software
11343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * distributed under the License is distributed on an "AS IS" BASIS,
12343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * See the License for the specific language governing permissions and
14343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * limitations under the License.
15343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih */
16343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
17343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#ifndef ANDROID_LAYER_H
18343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#define ANDROID_LAYER_H
19343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
20343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <stdint.h>
21343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <sys/types.h>
22343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
23343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <EGL/egl.h>
24343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <EGL/eglext.h>
25343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
26343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/RefBase.h>
27343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/String8.h>
28343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/Timers.h>
29343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
30343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <ui/GraphicBuffer.h>
31343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <ui/PixelFormat.h>
32343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <ui/Region.h>
33343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
34343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <gui/ISurfaceComposerClient.h>
35343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
36343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <private/gui/LayerState.h>
37343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
38343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "FrameTracker.h"
39343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "Client.h"
40343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "SurfaceFlinger.h"
41343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "SurfaceFlingerConsumer.h"
42343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "SurfaceTextureLayer.h"
43343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "Transform.h"
44343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
45343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "DisplayHardware/HWComposer.h"
46343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "DisplayHardware/FloatRect.h"
47
48namespace android {
49
50// ---------------------------------------------------------------------------
51
52class Client;
53class Colorizer;
54class DisplayDevice;
55class GraphicBuffer;
56class SurfaceFlinger;
57
58// ---------------------------------------------------------------------------
59
60/*
61 * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
62 * Layer is first referenced.
63 *
64 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
65 * that new data has arrived.
66 */
67class Layer : public SurfaceFlingerConsumer::FrameAvailableListener {
68    static int32_t sSequence;
69
70public:
71    mutable bool contentDirty;
72    // regions below are in window-manager space
73    Region visibleRegion;
74    Region coveredRegion;
75    Region visibleNonTransparentRegion;
76    int32_t sequence;
77
78    enum { // flags for doTransaction()
79        eDontUpdateGeometryState = 0x00000001,
80        eVisibleRegion = 0x00000002,
81    };
82
83    struct Geometry {
84        uint32_t w;
85        uint32_t h;
86        Rect crop;
87        inline bool operator ==(const Geometry& rhs) const {
88            return (w == rhs.w && h == rhs.h && crop == rhs.crop);
89        }
90        inline bool operator !=(const Geometry& rhs) const {
91            return !operator ==(rhs);
92        }
93    };
94
95    struct State {
96        Geometry active;
97        Geometry requested;
98        uint32_t z;
99        uint32_t layerStack;
100        uint8_t alpha;
101        uint8_t flags;
102        uint8_t reserved[2];
103        int32_t sequence; // changes when visible regions can change
104        Transform transform;
105        // the transparentRegion hint is a bit special, it's latched only
106        // when we receive a buffer -- this is because it's "content"
107        // dependent.
108        Region activeTransparentRegion;
109        Region requestedTransparentRegion;
110    };
111
112    class LayerMesh {
113        friend class Layer;
114        typedef GLfloat float2[2];
115        float2 mVertices[4];
116        size_t mNumVertices;
117    public:
118        LayerMesh() :
119                mNumVertices(4) {
120        }
121        float2 const* getVertices() const {
122            return mVertices;
123        }
124        size_t getVertexCount() const {
125            return mNumVertices;
126        }
127    };
128
129    // -----------------------------------------------------------------------
130
131    Layer(SurfaceFlinger* flinger, const sp<Client>& client,
132            const String8& name, uint32_t w, uint32_t h, uint32_t flags);
133
134    virtual ~Layer();
135
136    // the this layer's size and format
137    status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
138
139    // modify current state
140    bool setPosition(float x, float y);
141    bool setLayer(uint32_t z);
142    bool setSize(uint32_t w, uint32_t h);
143    bool setAlpha(uint8_t alpha);
144    bool setMatrix(const layer_state_t::matrix22_t& matrix);
145    bool setTransparentRegionHint(const Region& transparent);
146    bool setFlags(uint8_t flags, uint8_t mask);
147    bool setCrop(const Rect& crop);
148    bool setLayerStack(uint32_t layerStack);
149
150    uint32_t getTransactionFlags(uint32_t flags);
151    uint32_t setTransactionFlags(uint32_t flags);
152
153    void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
154    Rect computeBounds() const;
155
156    sp<IBinder> getHandle();
157    sp<BufferQueue> getBufferQueue() const;
158    const String8& getName() const;
159
160    // -----------------------------------------------------------------------
161    // Virtuals
162
163    virtual const char* getTypeId() const { return "Layer"; }
164
165    /*
166     * isOpaque - true if this surface is opaque
167     */
168    virtual bool isOpaque() const;
169
170    /*
171     * isSecure - true if this surface is secure, that is if it prevents
172     * screenshots or VNC servers.
173     */
174    virtual bool isSecure() const           { return mSecure; }
175
176    /*
177     * isProtected - true if the layer may contain protected content in the
178     * GRALLOC_USAGE_PROTECTED sense.
179     */
180    virtual bool isProtected() const;
181
182    /*
183     * isVisible - true if this layer is visible, false otherwise
184     */
185    virtual bool isVisible() const;
186
187    /*
188     * isFixedSize - true if content has a fixed size
189     */
190    virtual bool isFixedSize() const;
191
192protected:
193    /*
194     * onDraw - draws the surface.
195     */
196    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
197
198public:
199    // -----------------------------------------------------------------------
200
201    void setGeometry(const sp<const DisplayDevice>& hw,
202            HWComposer::HWCLayerInterface& layer);
203    void setPerFrameData(const sp<const DisplayDevice>& hw,
204            HWComposer::HWCLayerInterface& layer);
205    void setAcquireFence(const sp<const DisplayDevice>& hw,
206            HWComposer::HWCLayerInterface& layer);
207
208    /*
209     * called after page-flip
210     */
211    void onLayerDisplayed(const sp<const DisplayDevice>& hw,
212            HWComposer::HWCLayerInterface* layer);
213
214    /*
215     * called before composition.
216     * returns true if the layer has pending updates.
217     */
218    bool onPreComposition();
219
220    /*
221     *  called after composition.
222     */
223    void onPostComposition();
224
225    /*
226     * draw - performs some global clipping optimizations
227     * and calls onDraw().
228     */
229    void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
230    void draw(const sp<const DisplayDevice>& hw);
231
232    /*
233     * doTransaction - process the transaction. This is a good place to figure
234     * out which attributes of the surface have changed.
235     */
236    uint32_t doTransaction(uint32_t transactionFlags);
237
238    /*
239     * setVisibleRegion - called to set the new visible region. This gives
240     * a chance to update the new visible region or record the fact it changed.
241     */
242    void setVisibleRegion(const Region& visibleRegion);
243
244    /*
245     * setCoveredRegion - called when the covered region changes. The covered
246     * region corresponds to any area of the surface that is covered
247     * (transparently or not) by another surface.
248     */
249    void setCoveredRegion(const Region& coveredRegion);
250
251    /*
252     * setVisibleNonTransparentRegion - called when the visible and
253     * non-transparent region changes.
254     */
255    void setVisibleNonTransparentRegion(const Region&
256            visibleNonTransparentRegion);
257
258    /*
259     * latchBuffer - called each time the screen is redrawn and returns whether
260     * the visible regions need to be recomputed (this is a fairly heavy
261     * operation, so this should be set only if needed). Typically this is used
262     * to figure out if the content or size of a surface has changed.
263     */
264    Region latchBuffer(bool& recomputeVisibleRegions);
265
266    /*
267     * called with the state lock when the surface is removed from the
268     * current list
269     */
270    void onRemoved();
271
272
273    // Updates the transform hint in our SurfaceFlingerConsumer to match
274    // the current orientation of the display device.
275    void updateTransformHint(const sp<const DisplayDevice>& hw) const;
276
277    /*
278     * returns the rectangle that crops the content of the layer and scales it
279     * to the layer's size.
280     */
281    Rect getContentCrop() const;
282
283    // -----------------------------------------------------------------------
284
285    void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
286    void setFiltering(bool filtering);
287    bool getFiltering() const;
288
289    // only for debugging
290    inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
291
292    inline  const State&    getDrawingState() const { return mDrawingState; }
293    inline  const State&    getCurrentState() const { return mCurrentState; }
294    inline  State&          getCurrentState()       { return mCurrentState; }
295
296
297    /* always call base class first */
298    void dump(String8& result, Colorizer& colorizer) const;
299    void dumpStats(String8& result) const;
300    void clearStats();
301
302protected:
303    // constant
304    sp<SurfaceFlinger> mFlinger;
305
306    virtual void onFirstRef();
307
308    /*
309     * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
310     * is called.
311     */
312    class LayerCleaner {
313        sp<SurfaceFlinger> mFlinger;
314        wp<Layer> mLayer;
315    protected:
316        ~LayerCleaner();
317    public:
318        LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
319    };
320
321
322private:
323    // Interface implementation for SurfaceFlingerConsumer::FrameAvailableListener
324    virtual void onFrameAvailable();
325
326    void commitTransaction();
327
328    // needsLinearFiltering - true if this surface's state requires filtering
329    bool needsFiltering(const sp<const DisplayDevice>& hw) const;
330
331    uint32_t getEffectiveUsage(uint32_t usage) const;
332    FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
333    bool isCropped() const;
334    static bool getOpacityForFormat(uint32_t format);
335
336    // drawing
337    void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
338            GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
339    void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
340
341
342    // -----------------------------------------------------------------------
343
344    // constants
345    sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
346    GLuint mTextureName;
347    bool mPremultipliedAlpha;
348    String8 mName;
349    mutable bool mDebug;
350    PixelFormat mFormat;
351    bool mOpaqueLayer;
352
353    // these are protected by an external lock
354    State mCurrentState;
355    State mDrawingState;
356    volatile int32_t mTransactionFlags;
357
358    // thread-safe
359    volatile int32_t mQueuedFrames;
360    FrameTracker mFrameTracker;
361
362    // main thread
363    sp<GraphicBuffer> mActiveBuffer;
364    Rect mCurrentCrop;
365    uint32_t mCurrentTransform;
366    uint32_t mCurrentScalingMode;
367    bool mCurrentOpacity;
368    bool mRefreshPending;
369    bool mFrameLatencyNeeded;
370    // Whether filtering is forced on or not
371    bool mFiltering;
372    // Whether filtering is needed b/c of the drawingstate
373    bool mNeedsFiltering;
374
375    // page-flip thread (currently main thread)
376    bool mSecure; // no screenshots
377    bool mProtectedByApp; // application requires protected path to external sink
378
379    // protected by mLock
380    mutable Mutex mLock;
381    // Set to true once we've returned this surface's handle
382    mutable bool mHasSurface;
383    const wp<Client> mClientRef;
384};
385
386// ---------------------------------------------------------------------------
387
388}; // namespace android
389
390#endif // ANDROID_LAYER_H
391