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