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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <cutils/properties.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/StopWatch.h> |
| 27 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 28 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <ui/PixelFormat.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 30 | |
| 31 | #include <surfaceflinger/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | #include "clz.h" |
| 34 | #include "Layer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include "DisplayHardware/DisplayHardware.h" |
| 37 | |
| 38 | |
| 39 | #define DEBUG_RESIZE 0 |
| 40 | |
| 41 | |
| 42 | namespace android { |
| 43 | |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 44 | template <typename T> inline T min(T a, T b) { |
| 45 | return a<b ? a : b; |
| 46 | } |
| 47 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | // --------------------------------------------------------------------------- |
| 49 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 50 | Layer::Layer(SurfaceFlinger* flinger, |
| 51 | DisplayID display, const sp<Client>& client) |
| 52 | : LayerBaseClient(flinger, display, client), |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 53 | lcblk(NULL), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | mSecure(false), |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 55 | mNeedsBlending(true), |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 56 | mNeedsDithering(false), |
| 57 | mTextureManager(mFlags), |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 58 | mBufferManager(mTextureManager), |
| 59 | mWidth(0), mHeight(0), mFixedSize(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Layer::~Layer() |
| 64 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 65 | // FIXME: must be called from the main UI thread |
| 66 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 67 | mBufferManager.destroy(dpy); |
| 68 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 69 | // the actual buffers will be destroyed here |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 70 | delete lcblk; |
| 71 | } |
| 72 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 73 | // TODO: get rid of this |
| 74 | void Layer::setToken(int32_t token) |
| 75 | { |
| 76 | sp<Client> ourClient(client.promote()); |
| 77 | |
| 78 | mToken = token; |
| 79 | |
| 80 | // no OpenGL operation is possible here, since we might not be |
| 81 | // in the OpenGL thread. |
| 82 | lcblk = new SharedBufferServer( |
| 83 | ourClient->ctrlblk, token, mBufferManager.getDefaultBufferCount(), |
| 84 | getIdentity()); |
| 85 | |
| 86 | mBufferManager.setActiveBufferIndex( lcblk->getFrontBuffer() ); |
| 87 | } |
| 88 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 89 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 90 | // in the purgatory list |
| 91 | void Layer::onRemoved() |
| 92 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 93 | sp<Client> ourClient(client.promote()); |
| 94 | if (ourClient != 0) { |
| 95 | // wake up the condition |
| 96 | lcblk->setStatus(NO_INIT); |
| 97 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 98 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 99 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 100 | sp<LayerBaseClient::Surface> Layer::createSurface() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | { |
| 102 | return mSurface; |
| 103 | } |
| 104 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 105 | status_t Layer::ditch() |
| 106 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 107 | // NOTE: Called from the main UI thread |
| 108 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 109 | // the layer is not on screen anymore. free as much resources as possible |
Mathias Agopian | f5430db | 2009-12-11 00:56:10 -0800 | [diff] [blame] | 110 | mFreezeLock.clear(); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 111 | |
| 112 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 113 | mBufferManager.destroy(dpy); |
| 114 | mSurface.clear(); |
| 115 | |
| 116 | Mutex::Autolock _l(mLock); |
| 117 | mWidth = mHeight = 0; |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 118 | return NO_ERROR; |
| 119 | } |
| 120 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 121 | status_t Layer::setBuffers( uint32_t w, uint32_t h, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | PixelFormat format, uint32_t flags) |
| 123 | { |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 124 | // this surfaces pixel format |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | PixelFormatInfo info; |
| 126 | status_t err = getPixelFormatInfo(format, &info); |
| 127 | if (err) return err; |
| 128 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 129 | // the display's pixel format |
| 130 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 131 | uint32_t const maxSurfaceDims = min( |
| 132 | hw.getMaxTextureSize(), hw.getMaxViewportDims()); |
| 133 | |
| 134 | // never allow a surface larger than what our underlying GL implementation |
| 135 | // can handle. |
| 136 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
| 137 | return BAD_VALUE; |
| 138 | } |
| 139 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 140 | PixelFormatInfo displayInfo; |
| 141 | getPixelFormatInfo(hw.getFormat(), &displayInfo); |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 142 | const uint32_t hwFlags = hw.getFlags(); |
| 143 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 144 | mFormat = format; |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 145 | mWidth = w; |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 146 | mHeight = h; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 147 | mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 149 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 150 | // we use the red index |
| 151 | int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 152 | int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); |
| 153 | mNeedsDithering = layerRedsize > displayRedSize; |
| 154 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 155 | mSurface = new SurfaceLayer(mFlinger, this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | return NO_ERROR; |
| 157 | } |
| 158 | |
| 159 | void Layer::reloadTexture(const Region& dirty) |
| 160 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 161 | sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer()); |
Mathias Agopian | 8f03b47 | 2009-12-10 15:52:29 -0800 | [diff] [blame] | 162 | if (buffer == NULL) { |
| 163 | // this situation can happen if we ran out of memory for instance. |
| 164 | // not much we can do. continue to use whatever texture was bound |
| 165 | // to this context. |
| 166 | return; |
| 167 | } |
| 168 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 169 | #ifdef EGL_ANDROID_image_native_buffer |
Mathias Agopian | 57720c3 | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 170 | if (mFlags & DisplayHardware::DIRECT_TEXTURE) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 171 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 172 | if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) { |
| 173 | // not sure what we can do here... |
| 174 | mFlags &= ~DisplayHardware::DIRECT_TEXTURE; |
| 175 | goto slowpath; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 176 | } |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 177 | } else |
| 178 | #endif |
| 179 | { |
Mathias Agopian | fcfeb4b | 2010-03-08 11:14:20 -0800 | [diff] [blame] | 180 | slowpath: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 181 | GGLSurface t; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 182 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 183 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 184 | res, strerror(res), buffer.get()); |
| 185 | if (res == NO_ERROR) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 186 | mBufferManager.loadTexture(dirty, t); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 187 | buffer->unlock(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 188 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | void Layer::onDraw(const Region& clip) const |
| 193 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 194 | Texture tex(mBufferManager.getActiveTexture()); |
| 195 | if (tex.name == -1LU) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 197 | // in fact never been drawn into. This happens frequently with |
| 198 | // SurfaceView because the WindowManager can't know when the client |
| 199 | // has drawn the first time. |
| 200 | |
| 201 | // If there is nothing under us, we paint the screen in black, otherwise |
| 202 | // we just skip this update. |
| 203 | |
| 204 | // figure out if there is something below us |
| 205 | Region under; |
| 206 | const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ); |
| 207 | const size_t count = drawingLayers.size(); |
| 208 | for (size_t i=0 ; i<count ; ++i) { |
| 209 | const sp<LayerBase>& layer(drawingLayers[i]); |
| 210 | if (layer.get() == static_cast<LayerBase const*>(this)) |
| 211 | break; |
| 212 | under.orSelf(layer->visibleRegionScreen); |
| 213 | } |
| 214 | // if not everything below us is covered, we plug the holes! |
| 215 | Region holes(clip.subtract(under)); |
| 216 | if (!holes.isEmpty()) { |
| 217 | clearWithOpenGL(holes); |
| 218 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | return; |
| 220 | } |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 221 | drawWithOpenGL(clip, tex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 224 | bool Layer::needsFiltering() const |
| 225 | { |
| 226 | if (!(mFlags & DisplayHardware::SLOW_CONFIG)) { |
| 227 | // NOTE: there is a race here, because mFixedSize is updated in a |
| 228 | // binder transaction. however, it doesn't really matter since it is |
| 229 | // evaluated each time we draw. To be perfectly correct, this flag |
| 230 | // would have to be associated with a buffer. |
| 231 | if (mFixedSize) |
| 232 | return true; |
| 233 | } |
| 234 | return LayerBase::needsFiltering(); |
| 235 | } |
| 236 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 237 | |
| 238 | status_t Layer::setBufferCount(int bufferCount) |
| 239 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 240 | // Ensures our client doesn't go away while we're accessing |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 241 | // the shared area. |
| 242 | sp<Client> ourClient(client.promote()); |
| 243 | if (ourClient == 0) { |
| 244 | // oops, the client is already gone |
| 245 | return DEAD_OBJECT; |
| 246 | } |
| 247 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 248 | // NOTE: lcblk->resize() is protected by an internal lock |
| 249 | status_t err = lcblk->resize(bufferCount); |
| 250 | if (err == NO_ERROR) |
| 251 | mBufferManager.resize(bufferCount); |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 252 | |
| 253 | return err; |
| 254 | } |
| 255 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 256 | sp<GraphicBuffer> Layer::requestBuffer(int index, |
| 257 | uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat, |
| 258 | uint32_t usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 260 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 261 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 262 | if ((reqWidth | reqHeight | reqFormat) < 0) |
| 263 | return buffer; |
| 264 | |
| 265 | if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight)) |
| 266 | return buffer; |
| 267 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 268 | // this ensures our client doesn't go away while we're accessing |
| 269 | // the shared area. |
| 270 | sp<Client> ourClient(client.promote()); |
| 271 | if (ourClient == 0) { |
| 272 | // oops, the client is already gone |
| 273 | return buffer; |
| 274 | } |
| 275 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 276 | /* |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 277 | * This is called from the client's Surface::dequeue(). This can happen |
| 278 | * at any time, especially while we're in the middle of using the |
| 279 | * buffer 'index' as our front buffer. |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 280 | * |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 281 | * Make sure the buffer we're resizing is not the front buffer and has been |
| 282 | * dequeued. Once this condition is asserted, we are guaranteed that this |
| 283 | * buffer cannot become the front buffer under our feet, since we're called |
| 284 | * from Surface::dequeue() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 285 | */ |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 286 | status_t err = lcblk->assertReallocate(index); |
| 287 | LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 288 | if (err != NO_ERROR) { |
| 289 | // the surface may have died |
| 290 | return buffer; |
| 291 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 293 | uint32_t w, h, f; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 294 | { // scope for the lock |
| 295 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 296 | const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight); |
| 297 | const bool formatChanged = mReqFormat != reqFormat; |
| 298 | mReqWidth = reqWidth; |
| 299 | mReqHeight = reqHeight; |
| 300 | mReqFormat = reqFormat; |
| 301 | mFixedSize = reqWidth && reqHeight; |
| 302 | w = reqWidth ? reqWidth : mWidth; |
| 303 | h = reqHeight ? reqHeight : mHeight; |
| 304 | f = reqFormat ? reqFormat : mFormat; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 305 | buffer = mBufferManager.detachBuffer(index); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 306 | if (fixedSizeChanged || formatChanged) { |
| 307 | lcblk->reallocateAllExcept(index); |
| 308 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 311 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | 6d9f698 | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 312 | if (buffer!=0 && buffer->getStrongCount() == 1) { |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 313 | err = buffer->reallocate(w, h, f, effectiveUsage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 314 | } else { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 315 | // here we have to reallocate a new buffer because we could have a |
| 316 | // client in our process with a reference to it (eg: status bar), |
| 317 | // and we can't release the handle under its feet. |
| 318 | buffer.clear(); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 319 | buffer = new GraphicBuffer(w, h, f, effectiveUsage); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 320 | err = buffer->initCheck(); |
| 321 | } |
| 322 | |
| 323 | if (err || buffer->handle == 0) { |
| 324 | LOGE_IF(err || buffer->handle == 0, |
| 325 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 326 | this, index, w, h, strerror(-err)); |
| 327 | } else { |
| 328 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 329 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 330 | this, index, w, h, buffer->handle); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 334 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 335 | mBufferManager.attachBuffer(index, buffer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 336 | } |
| 337 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 340 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 341 | { |
| 342 | /* |
| 343 | * buffers used for software rendering, but h/w composition |
| 344 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 345 | * |
| 346 | * buffers used for h/w rendering and h/w composition |
| 347 | * are allocated with HW_RENDER | HW_TEXTURE |
| 348 | * |
| 349 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 350 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 351 | * |
| 352 | */ |
| 353 | |
| 354 | if (mSecure) { |
| 355 | // secure buffer, don't store it into the GPU |
| 356 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 357 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 358 | } else { |
| 359 | // it's allowed to modify the usage flags here, but generally |
| 360 | // the requested flags should be honored. |
Mathias Agopian | 89141f9 | 2010-05-10 20:10:10 -0700 | [diff] [blame] | 361 | // request EGLImage for all buffers |
| 362 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 363 | } |
| 364 | return usage; |
| 365 | } |
| 366 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | uint32_t Layer::doTransaction(uint32_t flags) |
| 368 | { |
| 369 | const Layer::State& front(drawingState()); |
| 370 | const Layer::State& temp(currentState()); |
| 371 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 372 | const bool sizeChanged = (front.requested_w != temp.requested_w) || |
| 373 | (front.requested_h != temp.requested_h); |
| 374 | |
| 375 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 376 | // the size changed, we need to ask our client to request a new buffer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 378 | "resize (layer=%p), requested (%dx%d), drawing (%d,%d)", |
| 379 | this, |
| 380 | int(temp.requested_w), int(temp.requested_h), |
| 381 | int(front.requested_w), int(front.requested_h)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 383 | if (!isFixedSize()) { |
| 384 | // we're being resized and there is a freeze display request, |
| 385 | // acquire a freeze lock, so that the screen stays put |
| 386 | // until we've redrawn at the new size; this is to avoid |
| 387 | // glitches upon orientation changes. |
| 388 | if (mFlinger->hasFreezeRequest()) { |
| 389 | // if the surface is hidden, don't try to acquire the |
| 390 | // freeze lock, since hidden surfaces may never redraw |
| 391 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 392 | mFreezeLock = mFlinger->getFreezeLock(); |
| 393 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 395 | |
| 396 | // this will make sure LayerBase::doTransaction doesn't update |
| 397 | // the drawing state's size |
| 398 | Layer::State& editDraw(mDrawingState); |
| 399 | editDraw.requested_w = temp.requested_w; |
| 400 | editDraw.requested_h = temp.requested_h; |
| 401 | |
| 402 | // record the new size, form this point on, when the client request |
| 403 | // a buffer, it'll get the new size. |
| 404 | setBufferSize(temp.requested_w, temp.requested_h); |
| 405 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 406 | sp<Client> ourClient(client.promote()); |
| 407 | if (ourClient != 0) { |
| 408 | // all buffers need reallocation |
| 409 | lcblk->reallocateAll(); |
| 410 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 411 | } else { |
| 412 | // record the new size |
| 413 | setBufferSize(temp.requested_w, temp.requested_h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 416 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | if (temp.sequence != front.sequence) { |
| 418 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 419 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 420 | // (it may never redraw, which is fine if it is hidden) |
| 421 | mFreezeLock.clear(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return LayerBase::doTransaction(flags); |
| 426 | } |
| 427 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 428 | void Layer::setBufferSize(uint32_t w, uint32_t h) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 429 | Mutex::Autolock _l(mLock); |
| 430 | mWidth = w; |
| 431 | mHeight = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 434 | bool Layer::isFixedSize() const { |
| 435 | Mutex::Autolock _l(mLock); |
| 436 | return mFixedSize; |
| 437 | } |
| 438 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | // ---------------------------------------------------------------------------- |
| 440 | // pageflip handling... |
| 441 | // ---------------------------------------------------------------------------- |
| 442 | |
| 443 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 444 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 445 | sp<Client> ourClient(client.promote()); |
| 446 | if (ourClient == 0) { |
| 447 | // client died |
| 448 | recomputeVisibleRegions = true; |
| 449 | return; |
| 450 | } |
| 451 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 452 | ssize_t buf = lcblk->retireAndLock(); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 453 | if (buf == NOT_ENOUGH_DATA) { |
| 454 | // NOTE: This is not an error, it simply means there is nothing to |
| 455 | // retire. The buffer is locked because we will use it |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 456 | // for composition later in the loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | return; |
| 458 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 459 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 460 | if (buf < NO_ERROR) { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 461 | LOGE("retireAndLock() buffer index (%d) out of range", buf); |
| 462 | mPostedDirtyRegion.clear(); |
| 463 | return; |
| 464 | } |
| 465 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 466 | // we retired a buffer, which becomes the new front buffer |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 467 | if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) { |
| 468 | LOGE("retireAndLock() buffer index (%d) out of range", buf); |
| 469 | mPostedDirtyRegion.clear(); |
| 470 | return; |
| 471 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 473 | // get the dirty region |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 474 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 475 | if (newFrontBuffer != NULL) { |
| 476 | // compute the posted region |
| 477 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 478 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 479 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 480 | // update the layer size and release freeze-lock |
| 481 | const Layer::State& front(drawingState()); |
| 482 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 483 | newFrontBuffer->getHeight() == front.requested_h) |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 484 | { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 485 | if ((front.w != front.requested_w) || |
| 486 | (front.h != front.requested_h)) |
| 487 | { |
| 488 | // Here we pretend the transaction happened by updating the |
| 489 | // current and drawing states. Drawing state is only accessed |
| 490 | // in this thread, no need to have it locked |
| 491 | Layer::State& editDraw(mDrawingState); |
| 492 | editDraw.w = editDraw.requested_w; |
| 493 | editDraw.h = editDraw.requested_h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 494 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 495 | // We also need to update the current state so that we don't |
| 496 | // end-up doing too much work during the next transaction. |
| 497 | // NOTE: We actually don't need hold the transaction lock here |
| 498 | // because State::w and State::h are only accessed from |
| 499 | // this thread |
| 500 | Layer::State& editTemp(currentState()); |
| 501 | editTemp.w = editDraw.w; |
| 502 | editTemp.h = editDraw.h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 503 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 504 | // recompute visible region |
| 505 | recomputeVisibleRegions = true; |
| 506 | } |
| 507 | |
| 508 | // we now have the correct size, unfreeze the screen |
| 509 | mFreezeLock.clear(); |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 510 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 511 | } else { |
| 512 | // this should not happen unless we ran out of memory while |
| 513 | // allocating the buffer. we're hoping that things will get back |
| 514 | // to normal the next time the app tries to draw into this buffer. |
| 515 | // meanwhile, pretend the screen didn't update. |
| 516 | mPostedDirtyRegion.clear(); |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Mathias Agopian | e700501 | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 519 | if (lcblk->getQueuedCount()) { |
| 520 | // signal an event if we have more buffers waiting |
| 521 | mFlinger->signalEvent(); |
| 522 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | |
Mathias Agopian | 245e4d7 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 524 | /* a buffer was posted, so we need to call reloadTexture(), which |
| 525 | * will update our internal data structures (eg: EGLImageKHR or |
| 526 | * texture names). we need to do this even if mPostedDirtyRegion is |
| 527 | * empty -- it's orthogonal to the fact that a new buffer was posted, |
| 528 | * for instance, a degenerate case could be that the user did an empty |
| 529 | * update but repainted the buffer with appropriate content (after a |
| 530 | * resize for instance). |
| 531 | */ |
| 532 | reloadTexture( mPostedDirtyRegion ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | void Layer::unlockPageFlip( |
| 536 | const Transform& planeTransform, Region& outDirtyRegion) |
| 537 | { |
| 538 | Region dirtyRegion(mPostedDirtyRegion); |
| 539 | if (!dirtyRegion.isEmpty()) { |
| 540 | mPostedDirtyRegion.clear(); |
| 541 | // The dirty region is given in the layer's coordinate space |
| 542 | // transform the dirty region by the surface's transformation |
| 543 | // and the global transformation. |
| 544 | const Layer::State& s(drawingState()); |
| 545 | const Transform tr(planeTransform * s.transform); |
| 546 | dirtyRegion = tr.transform(dirtyRegion); |
| 547 | |
| 548 | // At this point, the dirty region is in screen space. |
| 549 | // Make sure it's constrained by the visible region (which |
| 550 | // is in screen space as well). |
| 551 | dirtyRegion.andSelf(visibleRegionScreen); |
| 552 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 554 | if (visibleRegionScreen.isEmpty()) { |
| 555 | // an invisible layer should not hold a freeze-lock |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 556 | // (because it may never be updated and therefore never release it) |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 557 | mFreezeLock.clear(); |
| 558 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void Layer::finishPageFlip() |
| 562 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 563 | sp<Client> ourClient(client.promote()); |
| 564 | if (ourClient != 0) { |
| 565 | int buf = mBufferManager.getActiveBufferIndex(); |
| 566 | status_t err = lcblk->unlock( buf ); |
| 567 | LOGE_IF(err!=NO_ERROR, "layer %p, buffer=%d wasn't locked!", this, buf); |
| 568 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 571 | |
| 572 | void Layer::dump(String8& result, char* buffer, size_t SIZE) const |
| 573 | { |
| 574 | LayerBaseClient::dump(result, buffer, SIZE); |
| 575 | |
| 576 | SharedBufferStack::Statistics stats = lcblk->getStats(); |
| 577 | result.append( lcblk->dump(" ") ); |
| 578 | sp<const GraphicBuffer> buf0(getBuffer(0)); |
| 579 | sp<const GraphicBuffer> buf1(getBuffer(1)); |
| 580 | uint32_t w0=0, h0=0, s0=0; |
| 581 | uint32_t w1=0, h1=0, s1=0; |
| 582 | if (buf0 != 0) { |
| 583 | w0 = buf0->getWidth(); |
| 584 | h0 = buf0->getHeight(); |
| 585 | s0 = buf0->getStride(); |
| 586 | } |
| 587 | if (buf1 != 0) { |
| 588 | w1 = buf1->getWidth(); |
| 589 | h1 = buf1->getHeight(); |
| 590 | s1 = buf1->getStride(); |
| 591 | } |
| 592 | snprintf(buffer, SIZE, |
| 593 | " " |
| 594 | "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u]," |
| 595 | " freezeLock=%p, dq-q-time=%u us\n", |
| 596 | pixelFormat(), |
| 597 | w0, h0, s0, w1, h1, s1, |
| 598 | getFreezeLock().get(), stats.totalTime); |
| 599 | |
| 600 | result.append(buffer); |
| 601 | } |
| 602 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 603 | // --------------------------------------------------------------------------- |
| 604 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 605 | Layer::BufferManager::BufferManager(TextureManager& tm) |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 606 | : mNumBuffers(NUM_BUFFERS), mTextureManager(tm), |
| 607 | mActiveBuffer(0), mFailover(false) |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 608 | { |
| 609 | } |
| 610 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 611 | Layer::BufferManager::~BufferManager() |
| 612 | { |
| 613 | } |
| 614 | |
| 615 | status_t Layer::BufferManager::resize(size_t size) |
| 616 | { |
| 617 | Mutex::Autolock _l(mLock); |
| 618 | mNumBuffers = size; |
| 619 | return NO_ERROR; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | // only for debugging |
| 623 | sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const { |
| 624 | return mBufferData[index].buffer; |
| 625 | } |
| 626 | |
| 627 | status_t Layer::BufferManager::setActiveBufferIndex(size_t index) { |
| 628 | // TODO: need to validate 'index' |
| 629 | mActiveBuffer = index; |
| 630 | return NO_ERROR; |
| 631 | } |
| 632 | |
| 633 | size_t Layer::BufferManager::getActiveBufferIndex() const { |
| 634 | return mActiveBuffer; |
| 635 | } |
| 636 | |
| 637 | Texture Layer::BufferManager::getActiveTexture() const { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 638 | Texture res; |
| 639 | if (mFailover) { |
| 640 | res = mFailoverTexture; |
| 641 | } else { |
| 642 | static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture; |
| 643 | } |
| 644 | return res; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 648 | const size_t activeBuffer = mActiveBuffer; |
| 649 | BufferData const * const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 650 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 651 | return buffers[activeBuffer].buffer; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index) |
| 655 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 656 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 657 | sp<GraphicBuffer> buffer; |
| 658 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 659 | buffer = buffers[index].buffer; |
| 660 | buffers[index].buffer = 0; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 661 | return buffer; |
| 662 | } |
| 663 | |
| 664 | status_t Layer::BufferManager::attachBuffer(size_t index, |
| 665 | const sp<GraphicBuffer>& buffer) |
| 666 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 667 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 668 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 669 | buffers[index].buffer = buffer; |
| 670 | buffers[index].texture.dirty = true; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 671 | return NO_ERROR; |
| 672 | } |
| 673 | |
| 674 | status_t Layer::BufferManager::destroy(EGLDisplay dpy) |
| 675 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 676 | BufferData* const buffers = mBufferData; |
| 677 | size_t num; |
| 678 | { // scope for the lock |
| 679 | Mutex::Autolock _l(mLock); |
| 680 | num = mNumBuffers; |
| 681 | for (size_t i=0 ; i<num ; i++) { |
| 682 | buffers[i].buffer = 0; |
| 683 | } |
| 684 | } |
| 685 | for (size_t i=0 ; i<num ; i++) { |
| 686 | destroyTexture(&buffers[i].texture, dpy); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 687 | } |
| 688 | destroyTexture(&mFailoverTexture, dpy); |
| 689 | return NO_ERROR; |
| 690 | } |
| 691 | |
| 692 | status_t Layer::BufferManager::initEglImage(EGLDisplay dpy, |
| 693 | const sp<GraphicBuffer>& buffer) |
| 694 | { |
| 695 | size_t index = mActiveBuffer; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 696 | Image& texture(mBufferData[index].texture); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 697 | status_t err = mTextureManager.initEglImage(&texture, dpy, buffer); |
| 698 | // if EGLImage fails, we switch to regular texture mode, and we |
| 699 | // free all resources associated with using EGLImages. |
| 700 | if (err == NO_ERROR) { |
| 701 | mFailover = false; |
| 702 | destroyTexture(&mFailoverTexture, dpy); |
| 703 | } else { |
| 704 | mFailover = true; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 705 | const size_t num = mNumBuffers; |
| 706 | for (size_t i=0 ; i<num ; i++) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 707 | destroyTexture(&mBufferData[i].texture, dpy); |
| 708 | } |
| 709 | } |
| 710 | return err; |
| 711 | } |
| 712 | |
| 713 | status_t Layer::BufferManager::loadTexture( |
| 714 | const Region& dirty, const GGLSurface& t) |
| 715 | { |
| 716 | return mTextureManager.loadTexture(&mFailoverTexture, dirty, t); |
| 717 | } |
| 718 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 719 | status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy) |
| 720 | { |
| 721 | if (tex->name != -1U) { |
| 722 | glDeleteTextures(1, &tex->name); |
| 723 | tex->name = -1U; |
| 724 | } |
| 725 | if (tex->image != EGL_NO_IMAGE_KHR) { |
| 726 | eglDestroyImageKHR(dpy, tex->image); |
| 727 | tex->image = EGL_NO_IMAGE_KHR; |
| 728 | } |
| 729 | return NO_ERROR; |
| 730 | } |
| 731 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 732 | // --------------------------------------------------------------------------- |
| 733 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 734 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame^] | 735 | const sp<Layer>& owner) |
| 736 | : Surface(flinger, owner->getIdentity(), owner) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 737 | { |
| 738 | } |
| 739 | |
| 740 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 741 | { |
| 742 | } |
| 743 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 744 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, |
| 745 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 746 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 747 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 748 | sp<Layer> owner(getOwner()); |
| 749 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 750 | /* |
| 751 | * requestBuffer() cannot be called from the main thread |
| 752 | * as it could cause a dead-lock, since it may have to wait |
| 753 | * on conditions updated my the main thread. |
| 754 | */ |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 755 | buffer = owner->requestBuffer(index, w, h, format, usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 756 | } |
| 757 | return buffer; |
| 758 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 760 | status_t Layer::SurfaceLayer::setBufferCount(int bufferCount) |
| 761 | { |
| 762 | status_t err = DEAD_OBJECT; |
| 763 | sp<Layer> owner(getOwner()); |
| 764 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 765 | /* |
| 766 | * setBufferCount() cannot be called from the main thread |
| 767 | * as it could cause a dead-lock, since it may have to wait |
| 768 | * on conditions updated my the main thread. |
| 769 | */ |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 770 | err = owner->setBufferCount(bufferCount); |
| 771 | } |
| 772 | return err; |
| 773 | } |
| 774 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | // --------------------------------------------------------------------------- |
| 776 | |
| 777 | |
| 778 | }; // namespace android |