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