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