OSDN Git Service

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