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