| 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 |  | 
|  | 40 | class Client; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | class FreezeLock; | 
|  | 42 |  | 
|  | 43 | // --------------------------------------------------------------------------- | 
|  | 44 |  | 
|  | 45 | class Layer : public LayerBaseClient | 
|  | 46 | { | 
| Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 47 | public: | 
|  | 48 | // lcblk is (almost) only accessed from the main SF thread, in the places | 
|  | 49 | // where it's not, a reference to Client must be held | 
|  | 50 | SharedBufferServer*     lcblk; | 
|  | 51 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | Layer(SurfaceFlinger* flinger, DisplayID display, | 
| Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 53 | const sp<Client>& client, int32_t i); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | virtual ~Layer(); | 
|  | 56 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 57 | status_t setBuffers(uint32_t w, uint32_t h, | 
|  | 58 | PixelFormat format, uint32_t flags=0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 60 | void setDrawingSize(uint32_t w, uint32_t h); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 |  | 
|  | 62 | virtual void onDraw(const Region& clip) const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | virtual uint32_t doTransaction(uint32_t transactionFlags); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | virtual void lockPageFlip(bool& recomputeVisibleRegions); | 
|  | 65 | virtual void unlockPageFlip(const Transform& planeTransform, Region& outDirtyRegion); | 
|  | 66 | virtual void finishPageFlip(); | 
|  | 67 | virtual bool needsBlending() const      { return mNeedsBlending; } | 
| Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 68 | virtual bool needsDithering() const     { return mNeedsDithering; } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | virtual bool isSecure() const           { return mSecure; } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 70 | virtual sp<Surface> createSurface() const; | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 71 | virtual status_t ditch(); | 
| Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 72 | virtual void onRemoved(); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 73 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | // only for debugging | 
| Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 75 | inline sp<GraphicBuffer> getBuffer(int i) const { return mBufferManager.getBuffer(i); } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 76 | // only for debugging | 
|  | 77 | inline const sp<FreezeLock>&  getFreezeLock() const { return mFreezeLock; } | 
|  | 78 | // only for debugging | 
|  | 79 | inline PixelFormat pixelFormat() const { return mFormat; } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 |  | 
| Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 81 | virtual const char* getTypeId() const { return "Layer"; } | 
|  | 82 |  | 
|  | 83 | protected: | 
|  | 84 | virtual void dump(String8& result, char* scratch, size_t size) const; | 
|  | 85 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | private: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | void reloadTexture(const Region& dirty); | 
|  | 88 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 89 | uint32_t getEffectiveUsage(uint32_t usage) const; | 
|  | 90 |  | 
|  | 91 | sp<GraphicBuffer> requestBuffer(int index, int usage); | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 92 | status_t setBufferCount(int bufferCount); | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 93 | void destroy(); | 
|  | 94 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 95 | class SurfaceLayer : public LayerBaseClient::Surface { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 96 | public: | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 97 | SurfaceLayer(const sp<SurfaceFlinger>& flinger, | 
|  | 98 | SurfaceID id, const sp<Layer>& owner); | 
|  | 99 | ~SurfaceLayer(); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 100 | private: | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 101 | virtual sp<GraphicBuffer> requestBuffer(int index, int usage); | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 102 | virtual status_t setBufferCount(int bufferCount); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 103 | sp<Layer> getOwner() const { | 
|  | 104 | return static_cast<Layer*>(Surface::getOwner().get()); | 
|  | 105 | } | 
|  | 106 | }; | 
|  | 107 | friend class SurfaceLayer; | 
|  | 108 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | sp<Surface>             mSurface; | 
|  | 110 |  | 
|  | 111 | bool            mSecure; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | int32_t         mFrontBufferIndex; | 
|  | 113 | bool            mNeedsBlending; | 
| Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 114 | bool            mNeedsDithering; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | Region          mPostedDirtyRegion; | 
|  | 116 | sp<FreezeLock>  mFreezeLock; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 117 | PixelFormat     mFormat; | 
| Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | class BufferManager { | 
|  | 120 | static const size_t NUM_BUFFERS = 2; | 
|  | 121 | struct BufferData { | 
|  | 122 | sp<GraphicBuffer>   buffer; | 
|  | 123 | Texture             texture; | 
|  | 124 | }; | 
|  | 125 | mutable Mutex mLock; | 
|  | 126 | BufferData          mBufferData[NUM_BUFFERS]; | 
|  | 127 | Texture             mFailoverTexture; | 
|  | 128 | TextureManager&     mTextureManager; | 
|  | 129 | ssize_t             mActiveBuffer; | 
|  | 130 | bool                mFailover; | 
|  | 131 | static status_t destroyTexture(Texture* tex, EGLDisplay dpy); | 
|  | 132 |  | 
|  | 133 | public: | 
|  | 134 | BufferManager(TextureManager& tm); | 
|  | 135 |  | 
|  | 136 | size_t getBufferCount() const; | 
|  | 137 |  | 
|  | 138 | // detach/attach buffer from/to given index | 
|  | 139 | sp<GraphicBuffer> detachBuffer(size_t index); | 
|  | 140 | status_t attachBuffer(size_t index, const sp<GraphicBuffer>& buffer); | 
|  | 141 |  | 
|  | 142 | // ---------------------------------------------- | 
|  | 143 | // must be called from GL thread | 
|  | 144 |  | 
|  | 145 | // set/get active buffer index | 
|  | 146 | status_t setActiveBufferIndex(size_t index); | 
|  | 147 | size_t getActiveBufferIndex() const; | 
|  | 148 |  | 
|  | 149 | // return the active buffer | 
|  | 150 | sp<GraphicBuffer> getActiveBuffer() const; | 
|  | 151 |  | 
|  | 152 | // return the active texture (or fail-over) | 
|  | 153 | Texture getActiveTexture() const; | 
|  | 154 |  | 
|  | 155 | // frees resources associated with all buffers | 
|  | 156 | status_t destroy(EGLDisplay dpy); | 
|  | 157 |  | 
|  | 158 | // load bitmap data into the active buffer | 
|  | 159 | status_t loadTexture(const Region& dirty, const GGLSurface& t); | 
|  | 160 |  | 
|  | 161 | // make active buffer an EGLImage if needed | 
|  | 162 | status_t initEglImage(EGLDisplay dpy, | 
|  | 163 | const sp<GraphicBuffer>& buffer); | 
|  | 164 |  | 
|  | 165 | // ---------------------------------------------- | 
|  | 166 | // only for debugging | 
|  | 167 | sp<GraphicBuffer> getBuffer(size_t index) const; | 
|  | 168 | }; | 
|  | 169 |  | 
|  | 170 | TextureManager mTextureManager; | 
|  | 171 | BufferManager mBufferManager; | 
|  | 172 |  | 
|  | 173 | mutable Mutex mLock; | 
|  | 174 | uint32_t    mWidth; | 
|  | 175 | uint32_t    mHeight; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | }; | 
|  | 177 |  | 
|  | 178 | // --------------------------------------------------------------------------- | 
|  | 179 |  | 
|  | 180 | }; // namespace android | 
|  | 181 |  | 
|  | 182 | #endif // ANDROID_LAYER_H |