The Android Open Source Project | edbf3b6 | 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 | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 23 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <ui/PixelFormat.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <pixelflinger/pixelflinger.h> |
| 26 | |
Mathias Agopian | 076b1cc | 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 | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "LayerBase.h" |
| 33 | #include "Transform.h" |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 34 | #include "TextureManager.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | // --------------------------------------------------------------------------- |
| 39 | |
Andreas Huber | e049a95 | 2010-06-25 09:25:19 -0700 | [diff] [blame] | 40 | class FreezeLock; |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 41 | class Client; |
| 42 | class GLExtensions; |
| 43 | class UserClient; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | class Layer : public LayerBaseClient |
| 48 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 49 | public: |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 50 | Layer(SurfaceFlinger* flinger, DisplayID display, |
| 51 | const sp<Client>& client); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 52 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 53 | virtual ~Layer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 55 | virtual const char* getTypeId() const { return "Layer"; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 57 | // the this layer's size and format |
Mathias Agopian | cbb288b | 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 | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | b7e930d | 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 | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 64 | sp<UserClient> getClient() const; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 65 | |
| 66 | // Set this Layer's buffers size |
Mathias Agopian | a138f89 | 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 | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 70 | // LayerBase interface |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 71 | virtual void setGeometry(hwc_layer_t* hwcl); |
| 72 | virtual void setPerFrameData(hwc_layer_t* hwcl); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 73 | virtual void drawForSreenShot() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | virtual void onDraw(const Region& clip) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | virtual uint32_t doTransaction(uint32_t transactionFlags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | virtual void lockPageFlip(bool& recomputeVisibleRegions); |
| 77 | virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion); |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 78 | virtual bool needsBlending(const sp<GraphicBuffer>& buffer) const; |
| 79 | virtual bool needsBlending() const; |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 80 | virtual bool needsDithering() const { return mNeedsDithering; } |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 81 | virtual bool needsFiltering() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | virtual bool isSecure() const { return mSecure; } |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 83 | virtual bool isProtected() const; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 84 | virtual sp<Surface> createSurface() const; |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 85 | virtual status_t ditch(); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 86 | virtual void onRemoved(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 88 | // only for debugging |
| 89 | inline sp<GraphicBuffer> getBuffer(int i) const { |
| 90 | return mBufferManager.getBuffer(i); } |
| 91 | // only for debugging |
| 92 | inline const sp<FreezeLock>& getFreezeLock() const { |
| 93 | return mFreezeLock; } |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 94 | |
| 95 | protected: |
| 96 | virtual void dump(String8& result, char* scratch, size_t size) const; |
| 97 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | private: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | void reloadTexture(const Region& dirty); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 100 | uint32_t getEffectiveUsage(uint32_t usage) const; |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 101 | sp<GraphicBuffer> requestBuffer(int bufferIdx, |
| 102 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage); |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 103 | status_t setBufferCount(int bufferCount); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 104 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 105 | // ----------------------------------------------------------------------- |
| 106 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 107 | class SurfaceLayer : public LayerBaseClient::Surface { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 108 | public: |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 109 | SurfaceLayer(const sp<SurfaceFlinger>& flinger, const sp<Layer>& owner); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 110 | ~SurfaceLayer(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | private: |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 112 | virtual sp<GraphicBuffer> requestBuffer(int bufferIdx, |
| 113 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage); |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 114 | virtual status_t setBufferCount(int bufferCount); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 115 | sp<Layer> getOwner() const { |
| 116 | return static_cast<Layer*>(Surface::getOwner().get()); |
| 117 | } |
| 118 | }; |
| 119 | friend class SurfaceLayer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 121 | // ----------------------------------------------------------------------- |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 122 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 123 | class ClientRef { |
| 124 | ClientRef(const ClientRef& rhs); |
| 125 | ClientRef& operator = (const ClientRef& rhs); |
| 126 | mutable Mutex mLock; |
| 127 | // binder thread, page-flip thread |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 128 | sp<SharedBufferServer> mControlBlock; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 129 | wp<UserClient> mUserClient; |
| 130 | int32_t mToken; |
| 131 | public: |
| 132 | ClientRef(); |
| 133 | ~ClientRef(); |
| 134 | int32_t getToken() const; |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 135 | sp<UserClient> getClient() const; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 136 | status_t setToken(const sp<UserClient>& uc, |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 137 | const sp<SharedBufferServer>& sharedClient, int32_t token); |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 138 | sp<UserClient> getUserClientUnsafe() const; |
| 139 | class Access { |
| 140 | Access(const Access& rhs); |
| 141 | Access& operator = (const Access& rhs); |
| 142 | sp<UserClient> mUserClientStrongRef; |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 143 | sp<SharedBufferServer> mControlBlock; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 144 | public: |
| 145 | Access(const ClientRef& ref); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 146 | ~Access(); |
| 147 | inline SharedBufferServer* get() const { return mControlBlock.get(); } |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 148 | }; |
| 149 | friend class Access; |
| 150 | }; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 151 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 152 | // ----------------------------------------------------------------------- |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 153 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 154 | class BufferManager { |
| 155 | static const size_t NUM_BUFFERS = 2; |
| 156 | struct BufferData { |
| 157 | sp<GraphicBuffer> buffer; |
| 158 | Image texture; |
| 159 | }; |
| 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. |
| 163 | mutable Mutex mLock; |
| 164 | BufferData mBufferData[SharedBufferStack::NUM_BUFFER_MAX]; |
| 165 | size_t mNumBuffers; |
| 166 | Texture mFailoverTexture; |
| 167 | TextureManager& mTextureManager; |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 168 | ssize_t mActiveBufferIndex; |
| 169 | sp<GraphicBuffer> mActiveBuffer; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 170 | bool mFailover; |
| 171 | static status_t destroyTexture(Image* tex, EGLDisplay dpy); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 172 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 173 | public: |
| 174 | static size_t getDefaultBufferCount() { return NUM_BUFFERS; } |
| 175 | BufferManager(TextureManager& tm); |
| 176 | ~BufferManager(); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 177 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 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 |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 182 | status_t resize(size_t size, const sp<SurfaceFlinger>& flinger, |
| 183 | EGLDisplay dpy); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 184 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 185 | // ---------------------------------------------- |
| 186 | // must be called from GL thread |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 187 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 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; |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 193 | // return wether we have an active buffer |
| 194 | bool hasActiveBuffer() const; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 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); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 204 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 205 | // ---------------------------------------------- |
| 206 | // only for debugging |
| 207 | sp<GraphicBuffer> getBuffer(size_t index) const; |
| 208 | }; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 209 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 210 | // ----------------------------------------------------------------------- |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 211 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 212 | // thread-safe |
| 213 | ClientRef mUserClientRef; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 214 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 215 | // constants |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 216 | PixelFormat mFormat; |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 217 | const GLExtensions& mGLExtensions; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 218 | bool mNeedsBlending; |
| 219 | bool mNeedsDithering; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 220 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 221 | // page-flip thread (currently main thread) |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 222 | bool mSecure; // no screenshots |
| 223 | bool mProtectedByApp; // application requires protected path to external sink |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 224 | Region mPostedDirtyRegion; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 225 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 226 | // page-flip thread and transaction thread (currently main thread) |
| 227 | sp<FreezeLock> mFreezeLock; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 228 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 229 | // see threading usage in declaration |
| 230 | TextureManager mTextureManager; |
| 231 | BufferManager mBufferManager; |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 232 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 233 | // binder thread, transaction thread |
| 234 | mutable Mutex mLock; |
| 235 | uint32_t mWidth; |
| 236 | uint32_t mHeight; |
| 237 | uint32_t mReqWidth; |
| 238 | uint32_t mReqHeight; |
| 239 | uint32_t mReqFormat; |
Mathias Agopian | 733189d | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 240 | bool mNeedsScaling; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 241 | bool mFixedSize; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | // --------------------------------------------------------------------------- |
| 245 | |
| 246 | }; // namespace android |
| 247 | |
| 248 | #endif // ANDROID_LAYER_H |