The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 23 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <ui/PixelFormat.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <pixelflinger/pixelflinger.h> |
| 26 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 27 | #include <EGL/egl.h> |
| 28 | #include <EGL/eglext.h> |
| 29 | #include <GLES/gl.h> |
| 30 | #include <GLES/glext.h> |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "LayerBase.h" |
| 33 | #include "Transform.h" |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 34 | #include "TextureManager.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | // --------------------------------------------------------------------------- |
| 39 | |
Andreas Huber | 330dd30 | 2010-06-25 09:25:19 -0700 | [diff] [blame] | 40 | class FreezeLock; |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 41 | class Client; |
| 42 | class GLExtensions; |
| 43 | class UserClient; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | class Layer : public LayerBaseClient |
| 48 | { |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 49 | public: |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 50 | Layer(SurfaceFlinger* flinger, DisplayID display, |
| 51 | const sp<Client>& client); |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 52 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 53 | virtual ~Layer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 55 | virtual const char* getTypeId() const { return "Layer"; } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 57 | // the this layer's size and format |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 58 | status_t setBuffers(uint32_t w, uint32_t h, |
| 59 | PixelFormat format, uint32_t flags=0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 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; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 64 | sp<UserClient> getClient() const; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 65 | |
| 66 | // Set this Layer's buffers size |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 67 | void setBufferSize(uint32_t w, uint32_t h); |
| 68 | bool isFixedSize() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 70 | // LayerBase interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | virtual void onDraw(const Region& clip) const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | virtual uint32_t doTransaction(uint32_t transactionFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | virtual void lockPageFlip(bool& recomputeVisibleRegions); |
| 74 | virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion); |
| 75 | virtual void finishPageFlip(); |
| 76 | virtual bool needsBlending() const { return mNeedsBlending; } |
Mathias Agopian | cc93476 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 77 | virtual bool needsDithering() const { return mNeedsDithering; } |
Mathias Agopian | 9237703 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 78 | virtual bool needsFiltering() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | virtual bool isSecure() const { return mSecure; } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 80 | virtual sp<Surface> createSurface() const; |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 81 | virtual status_t ditch(); |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 82 | virtual void onRemoved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 84 | // only for debugging |
| 85 | inline sp<GraphicBuffer> getBuffer(int i) const { |
| 86 | return mBufferManager.getBuffer(i); } |
| 87 | // only for debugging |
| 88 | inline const sp<FreezeLock>& getFreezeLock() const { |
| 89 | return mFreezeLock; } |
Mathias Agopian | 9bce873 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 90 | |
| 91 | protected: |
| 92 | virtual void dump(String8& result, char* scratch, size_t size) const; |
| 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | private: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | void reloadTexture(const Region& dirty); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 96 | uint32_t getEffectiveUsage(uint32_t usage) const; |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 97 | sp<GraphicBuffer> requestBuffer(int bufferIdx, |
| 98 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage); |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 99 | status_t setBufferCount(int bufferCount); |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 100 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 101 | // ----------------------------------------------------------------------- |
| 102 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 103 | class SurfaceLayer : public LayerBaseClient::Surface { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 104 | public: |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 105 | SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 106 | ~SurfaceLayer(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 107 | private: |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 108 | virtual sp<GraphicBuffer> requestBuffer(int bufferIdx, |
| 109 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage); |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 110 | virtual status_t setBufferCount(int bufferCount); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | sp<Layer> getOwner() const { |
| 112 | return static_cast<Layer*>(Surface::getOwner().get()); |
| 113 | } |
| 114 | }; |
| 115 | friend class SurfaceLayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 117 | // ----------------------------------------------------------------------- |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 118 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 119 | class ClientRef { |
| 120 | ClientRef(const ClientRef& rhs); |
| 121 | ClientRef& operator = (const ClientRef& rhs); |
| 122 | mutable Mutex mLock; |
| 123 | // binder thread, page-flip thread |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 124 | sp<SharedBufferServer> mControlBlock; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 125 | wp<UserClient> mUserClient; |
| 126 | int32_t mToken; |
| 127 | public: |
| 128 | ClientRef(); |
| 129 | ~ClientRef(); |
| 130 | int32_t getToken() const; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 131 | sp<UserClient> getClient() const; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 132 | status_t setToken(const sp<UserClient>& uc, |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 133 | const sp<SharedBufferServer>& sharedClient, int32_t token); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 134 | sp<UserClient> getUserClientUnsafe() const; |
| 135 | class Access { |
| 136 | Access(const Access& rhs); |
| 137 | Access& operator = (const Access& rhs); |
| 138 | sp<UserClient> mUserClientStrongRef; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 139 | sp<SharedBufferServer> mControlBlock; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 140 | public: |
| 141 | Access(const ClientRef& ref); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 142 | ~Access(); |
| 143 | inline SharedBufferServer* get() const { return mControlBlock.get(); } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 144 | }; |
| 145 | friend class Access; |
| 146 | }; |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 147 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 148 | // ----------------------------------------------------------------------- |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 149 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 150 | class BufferManager { |
| 151 | static const size_t NUM_BUFFERS = 2; |
| 152 | struct BufferData { |
| 153 | sp<GraphicBuffer> buffer; |
| 154 | Image texture; |
| 155 | }; |
| 156 | // this lock protect mBufferData[].buffer but since there |
| 157 | // is very little contention, we have only one like for |
| 158 | // the whole array, we also use it to protect mNumBuffers. |
| 159 | mutable Mutex mLock; |
| 160 | BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX]; |
| 161 | size_t mNumBuffers; |
| 162 | Texture mFailoverTexture; |
| 163 | TextureManager& mTextureManager; |
| 164 | ssize_t mActiveBuffer; |
| 165 | bool mFailover; |
| 166 | static status_t destroyTexture(Image* tex, EGLDisplay dpy); |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 167 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 168 | public: |
| 169 | static size_t getDefaultBufferCount() { return NUM_BUFFERS; } |
| 170 | BufferManager(TextureManager& tm); |
| 171 | ~BufferManager(); |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 172 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 173 | // detach/attach buffer from/to given index |
| 174 | sp<GraphicBuffer> detachBuffer(size_t index); |
| 175 | status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer); |
| 176 | // resize the number of active buffers |
| 177 | status_t resize(size_t size); |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 178 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 179 | // ---------------------------------------------- |
| 180 | // must be called from GL thread |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 181 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 182 | // set/get active buffer index |
| 183 | status_t setActiveBufferIndex(size_t index); |
| 184 | size_t getActiveBufferIndex() const; |
| 185 | // return the active buffer |
| 186 | sp<GraphicBuffer> getActiveBuffer() const; |
| 187 | // return the active texture (or fail-over) |
| 188 | Texture getActiveTexture() const; |
| 189 | // frees resources associated with all buffers |
| 190 | status_t destroy(EGLDisplay dpy); |
| 191 | // load bitmap data into the active buffer |
| 192 | status_t loadTexture(const Region& dirty, const GGLSurface& t); |
| 193 | // make active buffer an EGLImage if needed |
| 194 | status_t initEglImage(EGLDisplay dpy, |
| 195 | const sp<GraphicBuffer>& buffer); |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 196 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 197 | // ---------------------------------------------- |
| 198 | // only for debugging |
| 199 | sp<GraphicBuffer> getBuffer(size_t index) const; |
| 200 | }; |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 201 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 202 | // ----------------------------------------------------------------------- |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 203 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 204 | // thread-safe |
| 205 | ClientRef mUserClientRef; |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 206 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 207 | // constants |
| 208 | sp<Surface> mSurface; |
| 209 | PixelFormat mFormat; |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 210 | const GLExtensions& mGLExtensions; |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 211 | bool mNeedsBlending; |
| 212 | bool mNeedsDithering; |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 213 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 214 | // page-flip thread (currently main thread) |
| 215 | bool mSecure; |
| 216 | Region mPostedDirtyRegion; |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 217 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 218 | // page-flip thread and transaction thread (currently main thread) |
| 219 | sp<FreezeLock> mFreezeLock; |
Mathias Agopian | 9f2c4fd | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 220 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 221 | // see threading usage in declaration |
| 222 | TextureManager mTextureManager; |
| 223 | BufferManager mBufferManager; |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 224 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 225 | // binder thread, transaction thread |
| 226 | mutable Mutex mLock; |
| 227 | uint32_t mWidth; |
| 228 | uint32_t mHeight; |
| 229 | uint32_t mReqWidth; |
| 230 | uint32_t mReqHeight; |
| 231 | uint32_t mReqFormat; |
| 232 | bool mFixedSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | // --------------------------------------------------------------------------- |
| 236 | |
| 237 | }; // namespace android |
| 238 | |
| 239 | #endif // ANDROID_LAYER_H |