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