Layer.h revision 733189d408e13b54fd70971b265244367efd0f51
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_H
18#define ANDROID_LAYER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <ui/GraphicBuffer.h>
24#include <ui/PixelFormat.h>
25#include <pixelflinger/pixelflinger.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
29#include <GLES/gl.h>
30#include <GLES/glext.h>
31
32#include "LayerBase.h"
33#include "Transform.h"
34#include "TextureManager.h"
35
36namespace android {
37
38// ---------------------------------------------------------------------------
39
40class FreezeLock;
41class Client;
42class GLExtensions;
43class UserClient;
44
45// ---------------------------------------------------------------------------
46
47class Layer : public LayerBaseClient
48{
49public:
50            Layer(SurfaceFlinger* flinger, DisplayID display,
51                    const sp<Client>& client);
52
53    virtual ~Layer();
54
55    virtual const char* getTypeId() const { return "Layer"; }
56
57    // the this layer's size and format
58    status_t setBuffers(uint32_t w, uint32_t h,
59            PixelFormat format, uint32_t flags=0);
60
61    // associate a UserClient to this Layer
62    status_t setToken(const sp<UserClient>& uc, SharedClient* sc, int32_t idx);
63    int32_t getToken() const;
64    sp<UserClient> getClient() const;
65
66    // Set this Layer's buffers size
67    void setBufferSize(uint32_t w, uint32_t h);
68    bool isFixedSize() const;
69
70    // LayerBase interface
71    virtual void drawForSreenShot() const;
72    virtual void onDraw(const Region& clip) const;
73    virtual uint32_t doTransaction(uint32_t transactionFlags);
74    virtual void lockPageFlip(bool& recomputeVisibleRegions);
75    virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion);
76    virtual void finishPageFlip();
77    virtual bool needsBlending() const      { return mNeedsBlending; }
78    virtual bool needsDithering() const     { return mNeedsDithering; }
79    virtual bool needsFiltering() const;
80    virtual bool isSecure() const           { return mSecure; }
81    virtual sp<Surface> createSurface() const;
82    virtual status_t ditch();
83    virtual void onRemoved();
84
85    // only for debugging
86    inline sp<GraphicBuffer> getBuffer(int i) const {
87        return mBufferManager.getBuffer(i); }
88    // only for debugging
89    inline const sp<FreezeLock>&  getFreezeLock() const {
90        return mFreezeLock; }
91
92protected:
93    virtual void dump(String8& result, char* scratch, size_t size) const;
94
95private:
96    void reloadTexture(const Region& dirty);
97    uint32_t getEffectiveUsage(uint32_t usage) const;
98    sp<GraphicBuffer> requestBuffer(int bufferIdx,
99            uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
100    status_t setBufferCount(int bufferCount);
101
102    // -----------------------------------------------------------------------
103
104    class SurfaceLayer : public LayerBaseClient::Surface {
105    public:
106        SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner);
107        ~SurfaceLayer();
108    private:
109        virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
110                uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
111        virtual status_t setBufferCount(int bufferCount);
112        sp<Layer> getOwner() const {
113            return static_cast<Layer*>(Surface::getOwner().get());
114        }
115    };
116    friend class SurfaceLayer;
117
118    // -----------------------------------------------------------------------
119
120    class ClientRef {
121        ClientRef(const ClientRef& rhs);
122        ClientRef& operator = (const ClientRef& rhs);
123        mutable Mutex mLock;
124        // binder thread, page-flip thread
125        sp<SharedBufferServer> mControlBlock;
126        wp<UserClient> mUserClient;
127        int32_t mToken;
128    public:
129        ClientRef();
130        ~ClientRef();
131        int32_t getToken() const;
132        sp<UserClient> getClient() const;
133        status_t setToken(const sp<UserClient>& uc,
134                const sp<SharedBufferServer>& sharedClient, int32_t token);
135        sp<UserClient> getUserClientUnsafe() const;
136        class Access {
137            Access(const Access& rhs);
138            Access& operator = (const Access& rhs);
139            sp<UserClient> mUserClientStrongRef;
140            sp<SharedBufferServer> mControlBlock;
141        public:
142            Access(const ClientRef& ref);
143            ~Access();
144            inline SharedBufferServer* get() const { return mControlBlock.get(); }
145        };
146        friend class Access;
147    };
148
149    // -----------------------------------------------------------------------
150
151    class BufferManager {
152        static const size_t NUM_BUFFERS = 2;
153        struct BufferData {
154            sp<GraphicBuffer>   buffer;
155            Image               texture;
156        };
157        // this lock protect mBufferData[].buffer but since there
158        // is very little contention, we have only one like for
159        // the whole array, we also use it to protect mNumBuffers.
160        mutable Mutex mLock;
161        BufferData          mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
162        size_t              mNumBuffers;
163        Texture             mFailoverTexture;
164        TextureManager&     mTextureManager;
165        ssize_t             mActiveBuffer;
166        bool                mFailover;
167        static status_t destroyTexture(Image* tex, EGLDisplay dpy);
168
169    public:
170        static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
171        BufferManager(TextureManager& tm);
172        ~BufferManager();
173
174        // detach/attach buffer from/to given index
175        sp<GraphicBuffer> detachBuffer(size_t index);
176        status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
177        // resize the number of active buffers
178        status_t resize(size_t size);
179
180        // ----------------------------------------------
181        // must be called from GL thread
182
183        // set/get active buffer index
184        status_t setActiveBufferIndex(size_t index);
185        size_t getActiveBufferIndex() const;
186        // return the active buffer
187        sp<GraphicBuffer> getActiveBuffer() const;
188        // return the active texture (or fail-over)
189        Texture getActiveTexture() const;
190        // frees resources associated with all buffers
191        status_t destroy(EGLDisplay dpy);
192        // load bitmap data into the active buffer
193        status_t loadTexture(const Region& dirty, const GGLSurface& t);
194        // make active buffer an EGLImage if needed
195        status_t initEglImage(EGLDisplay dpy,
196                const sp<GraphicBuffer>& buffer);
197
198        // ----------------------------------------------
199        // only for debugging
200        sp<GraphicBuffer> getBuffer(size_t index) const;
201    };
202
203    // -----------------------------------------------------------------------
204
205    // thread-safe
206    ClientRef mUserClientRef;
207
208    // constants
209    sp<Surface> mSurface;
210    PixelFormat mFormat;
211    const GLExtensions& mGLExtensions;
212    bool mNeedsBlending;
213    bool mNeedsDithering;
214
215    // page-flip thread (currently main thread)
216    bool mSecure;
217    Region mPostedDirtyRegion;
218
219    // page-flip thread and transaction thread (currently main thread)
220    sp<FreezeLock>  mFreezeLock;
221
222    // see threading usage in declaration
223    TextureManager mTextureManager;
224    BufferManager mBufferManager;
225
226    // binder thread, transaction thread
227    mutable Mutex mLock;
228    uint32_t mWidth;
229    uint32_t mHeight;
230    uint32_t mReqWidth;
231    uint32_t mReqHeight;
232    uint32_t mReqFormat;
233    bool mNeedsScaling;
234    bool mFixedSize;
235};
236
237// ---------------------------------------------------------------------------
238
239}; // namespace android
240
241#endif // ANDROID_LAYER_H
242