2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_LAYER_H
18 #define ANDROID_LAYER_H
21 #include <sys/types.h>
23 #include <ui/GraphicBuffer.h>
24 #include <ui/PixelFormat.h>
25 #include <pixelflinger/pixelflinger.h>
28 #include <EGL/eglext.h>
30 #include <GLES/glext.h>
32 #include "LayerBase.h"
33 #include "Transform.h"
34 #include "TextureManager.h"
38 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 class Layer : public LayerBaseClient
50 Layer(SurfaceFlinger* flinger, DisplayID display,
51 const sp<Client>& client);
55 virtual const char* getTypeId() const { return "Layer"; }
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);
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;
66 // Set this Layer's buffers size
67 void setBufferSize(uint32_t w, uint32_t h);
68 bool isFixedSize() const;
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 bool needsBlending(const sp<GraphicBuffer>& buffer) const;
79 virtual bool needsBlending() const;
80 virtual bool needsDithering() const { return mNeedsDithering; }
81 virtual bool needsFiltering() const;
82 virtual bool isSecure() const { return mSecure; }
83 virtual bool isProtected() const;
84 virtual sp<Surface> createSurface() const;
85 virtual void onRemoved();
88 inline sp<GraphicBuffer> getBuffer(int i) const {
89 return mBufferManager.getBuffer(i); }
91 inline const sp<FreezeLock>& getFreezeLock() const {
95 virtual void destroy() const;
96 virtual void dump(String8& result, char* scratch, size_t size) const;
99 void reloadTexture(const Region& dirty);
100 uint32_t getEffectiveUsage(uint32_t usage) const;
101 sp<GraphicBuffer> requestBuffer(int bufferIdx,
102 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
103 status_t setBufferCount(int bufferCount);
105 // -----------------------------------------------------------------------
107 class SurfaceLayer : public LayerBaseClient::Surface {
109 SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner);
112 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
113 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
114 virtual status_t setBufferCount(int bufferCount);
115 sp<Layer> getOwner() const {
116 return static_cast<Layer*>(Surface::getOwner().get());
119 friend class SurfaceLayer;
121 // -----------------------------------------------------------------------
124 ClientRef(const ClientRef& rhs);
125 ClientRef& operator = (const ClientRef& rhs);
127 // binder thread, page-flip thread
128 sp<SharedBufferServer> mControlBlock;
129 wp<UserClient> mUserClient;
134 int32_t getToken() const;
135 sp<UserClient> getClient() const;
136 status_t setToken(const sp<UserClient>& uc,
137 const sp<SharedBufferServer>& sharedClient, int32_t token);
138 sp<UserClient> getUserClientUnsafe() const;
140 Access(const Access& rhs);
141 Access& operator = (const Access& rhs);
142 sp<UserClient> mUserClientStrongRef;
143 sp<SharedBufferServer> mControlBlock;
145 Access(const ClientRef& ref);
147 inline SharedBufferServer* get() const { return mControlBlock.get(); }
152 // -----------------------------------------------------------------------
154 class BufferManager {
155 static const size_t NUM_BUFFERS = 2;
157 sp<GraphicBuffer> buffer;
160 // this lock protect mBufferData[].buffer but since there
161 // is very little contention, we have only one like for
162 // the whole array, we also use it to protect mNumBuffers.
164 BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX];
166 Texture mFailoverTexture;
167 TextureManager& mTextureManager;
168 ssize_t mActiveBufferIndex;
169 sp<GraphicBuffer> mActiveBuffer;
171 static status_t destroyTexture(Image* tex, EGLDisplay dpy);
174 static size_t getDefaultBufferCount() { return NUM_BUFFERS; }
175 BufferManager(TextureManager& tm);
178 // detach/attach buffer from/to given index
179 sp<GraphicBuffer> detachBuffer(size_t index);
180 status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer);
181 // resize the number of active buffers
182 status_t resize(size_t size, const sp<SurfaceFlinger>& flinger,
185 // ----------------------------------------------
186 // must be called from GL thread
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 wether we have an active buffer
194 bool hasActiveBuffer() const;
195 // return the active texture (or fail-over)
196 Texture getActiveTexture() const;
197 // frees resources associated with all buffers
198 status_t destroy(EGLDisplay dpy);
199 // load bitmap data into the active buffer
200 status_t loadTexture(const Region& dirty, const GGLSurface& t);
201 // make active buffer an EGLImage if needed
202 status_t initEglImage(EGLDisplay dpy,
203 const sp<GraphicBuffer>& buffer);
205 // ----------------------------------------------
206 // only for debugging
207 sp<GraphicBuffer> getBuffer(size_t index) const;
210 // -----------------------------------------------------------------------
213 ClientRef mUserClientRef;
217 const GLExtensions& mGLExtensions;
219 bool mNeedsDithering;
221 // page-flip thread (currently main thread)
222 bool mSecure; // no screenshots
223 bool mProtectedByApp; // application requires protected path to external sink
224 Region mPostedDirtyRegion;
226 // page-flip thread and transaction thread (currently main thread)
227 sp<FreezeLock> mFreezeLock;
229 // see threading usage in declaration
230 TextureManager mTextureManager;
231 BufferManager mBufferManager;
233 // binder thread, transaction thread
244 // ---------------------------------------------------------------------------
246 }; // namespace android
248 #endif // ANDROID_LAYER_H