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