LayerBase.h revision 53a67e1663564efd691461659634f9e908407112
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 "DisplayHardware/DisplayHardware.h"
39#include "Transform.h"
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
45class DisplayHardware;
46class Client;
47class GraphicBuffer;
48class GraphicPlane;
49class LayerBaseClient;
50class SurfaceFlinger;
51class Texture;
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            void setName(const String8& name);
85            String8 getName() const;
86
87            // modify current state
88            bool setPosition(int32_t x, int32_t 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
110    virtual const char* getTypeId() const { return "LayerBase"; }
111
112    /**
113     * draw - performs some global clipping optimizations
114     * and calls onDraw().
115     * Typically this method is not overridden, instead implement onDraw()
116     * to perform the actual drawing.
117     */
118    virtual void draw(const Region& clip) const;
119    virtual void drawForSreenShot() const;
120
121    /**
122     * bypass mode
123     */
124    virtual bool setBypass(bool enable) { return false; }
125
126    /**
127     * onDraw - draws the surface.
128     */
129    virtual void onDraw(const Region& clip) const = 0;
130
131    /**
132     * initStates - called just after construction
133     */
134    virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
135
136    /**
137     * doTransaction - process the transaction. This is a good place to figure
138     * out which attributes of the surface have changed.
139     */
140    virtual uint32_t doTransaction(uint32_t transactionFlags);
141
142    /**
143     * setVisibleRegion - called to set the new visible region. This gives
144     * a chance to update the new visible region or record the fact it changed.
145     */
146    virtual void setVisibleRegion(const Region& visibleRegion);
147
148    /**
149     * setCoveredRegion - called when the covered region changes. The covered
150     * region corresponds to any area of the surface that is covered
151     * (transparently or not) by another surface.
152     */
153    virtual void setCoveredRegion(const Region& coveredRegion);
154
155    /**
156     * validateVisibility - cache a bunch of things
157     */
158    virtual void validateVisibility(const Transform& globalTransform);
159
160    /**
161     * lockPageFlip - called each time the screen is redrawn and returns whether
162     * the visible regions need to be recomputed (this is a fairly heavy
163     * operation, so this should be set only if needed). Typically this is used
164     * to figure out if the content or size of a surface has changed.
165     */
166    virtual void lockPageFlip(bool& recomputeVisibleRegions);
167
168    /**
169     * unlockPageFlip - called each time the screen is redrawn. updates the
170     * final dirty region wrt the planeTransform.
171     * At this point, all visible regions, surface position and size, etc... are
172     * correct.
173     */
174    virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
175
176    /**
177     * finishPageFlip - called after all surfaces have drawn.
178     */
179    virtual void finishPageFlip();
180
181    /**
182     * needsBlending - true if this surface needs blending
183     */
184    virtual bool needsBlending() const  { return false; }
185
186    /**
187     * needsDithering - true if this surface needs dithering
188     */
189    virtual bool needsDithering() const { return false; }
190
191    /**
192     * needsLinearFiltering - true if this surface needs filtering
193     */
194    virtual bool needsFiltering() const {
195        return (!(mFlags & DisplayHardware::SLOW_CONFIG)) && mNeedsFiltering;
196    }
197
198    /**
199     * isSecure - true if this surface is secure, that is if it prevents
200     * screenshots or VNC servers.
201     */
202    virtual bool isSecure() const       { return false; }
203
204    /** Called from the main thread, when the surface is removed from the
205     * draw list */
206    virtual status_t ditch() { return NO_ERROR; }
207
208    /** called with the state lock when the surface is removed from the
209     *  current list */
210    virtual void onRemoved() { };
211
212    /** always call base class first */
213    virtual void dump(String8& result, char* scratch, size_t size) const;
214    virtual void shortDump(String8& result, char* scratch, size_t size) const;
215
216
217    enum { // flags for doTransaction()
218        eVisibleRegion      = 0x00000002,
219    };
220
221
222    inline  const State&    drawingState() const    { return mDrawingState; }
223    inline  const State&    currentState() const    { return mCurrentState; }
224    inline  State&          currentState()          { return mCurrentState; }
225
226    int32_t  getOrientation() const { return mOrientation; }
227    int  tx() const             { return mLeft; }
228    int  ty() const             { return mTop; }
229
230protected:
231    const GraphicPlane& graphicPlane(int dpy) const;
232          GraphicPlane& graphicPlane(int dpy);
233
234          void clearWithOpenGL(const Region& clip, GLclampf r, GLclampf g,
235                               GLclampf b, GLclampf alpha) const;
236          void clearWithOpenGL(const Region& clip) const;
237          void drawWithOpenGL(const Region& clip, const Texture& texture) const;
238
239          // these must be called from the post/drawing thread
240          void setBufferCrop(const Rect& crop);
241          void setBufferTransform(uint32_t transform);
242
243                sp<SurfaceFlinger> mFlinger;
244                uint32_t        mFlags;
245
246                // post/drawing thread
247                Rect mBufferCrop;
248                uint32_t mBufferTransform;
249
250                // cached during validateVisibility()
251                bool            mNeedsFiltering;
252                int32_t         mOrientation;
253                GLfloat         mVertices[4][2];
254                Rect            mTransformedBounds;
255                int             mLeft;
256                int             mTop;
257
258                // these are protected by an external lock
259                State           mCurrentState;
260                State           mDrawingState;
261    volatile    int32_t         mTransactionFlags;
262
263                // don't change, don't need a lock
264                bool            mPremultipliedAlpha;
265                String8         mName;
266    mutable     bool            mDebug;
267
268
269                // atomic
270    volatile    int32_t         mInvalidate;
271
272
273protected:
274    virtual ~LayerBase();
275
276private:
277    LayerBase(const LayerBase& rhs);
278};
279
280
281// ---------------------------------------------------------------------------
282
283class LayerBaseClient : public LayerBase
284{
285public:
286    class Surface;
287
288            LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
289                        const sp<Client>& client);
290    virtual ~LayerBaseClient();
291
292            sp<Surface> getSurface();
293    virtual sp<Surface> createSurface() const;
294    virtual sp<LayerBaseClient> getLayerBaseClient() const {
295        return const_cast<LayerBaseClient*>(this); }
296    virtual const char* getTypeId() const { return "LayerBaseClient"; }
297
298    uint32_t getIdentity() const { return mIdentity; }
299
300    class Surface : public BnSurface  {
301    public:
302        int32_t getIdentity() const { return mIdentity; }
303
304    protected:
305        Surface(const sp<SurfaceFlinger>& flinger, int identity,
306                const sp<LayerBaseClient>& owner);
307        virtual ~Surface();
308        virtual status_t onTransact(uint32_t code, const Parcel& data,
309                Parcel* reply, uint32_t flags);
310        sp<LayerBaseClient> getOwner() const;
311
312    private:
313        virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
314                uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
315        virtual status_t setBufferCount(int bufferCount);
316
317        virtual status_t registerBuffers(const ISurface::BufferHeap& buffers);
318        virtual void postBuffer(ssize_t offset);
319        virtual void unregisterBuffers();
320        virtual sp<OverlayRef> createOverlay(uint32_t w, uint32_t h,
321                int32_t format, int32_t orientation);
322
323    protected:
324        friend class LayerBaseClient;
325        sp<SurfaceFlinger>  mFlinger;
326        int32_t             mIdentity;
327        wp<LayerBaseClient> mOwner;
328    };
329
330    friend class Surface;
331
332protected:
333    virtual void dump(String8& result, char* scratch, size_t size) const;
334    virtual void shortDump(String8& result, char* scratch, size_t size) const;
335
336private:
337    mutable Mutex mLock;
338    mutable wp<Surface> mClientSurface;
339    const wp<Client> mClientRef;
340    // only read
341    const uint32_t mIdentity;
342    static int32_t sIdentity;
343};
344
345// ---------------------------------------------------------------------------
346
347}; // namespace android
348
349#endif // ANDROID_LAYER_BASE_H
350