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