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 | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 212 | |
| 213 | status_t Layer::setBufferCount(int bufferCount) |
| 214 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 215 | // Ensures our client doesn't go away while we're accessing |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 216 | // the shared area. |
| 217 | sp<Client> ourClient(client.promote()); |
| 218 | if (ourClient == 0) { |
| 219 | // oops, the client is already gone |
| 220 | return DEAD_OBJECT; |
| 221 | } |
| 222 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 223 | // NOTE: lcblk->resize() is protected by an internal lock |
| 224 | status_t err = lcblk->resize(bufferCount); |
| 225 | if (err == NO_ERROR) |
| 226 | mBufferManager.resize(bufferCount); |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 227 | |
| 228 | return err; |
| 229 | } |
| 230 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 231 | sp<GraphicBuffer> Layer::requestBuffer(int index, |
| 232 | uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat, |
| 233 | uint32_t usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 235 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 236 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 237 | if ((reqWidth | reqHeight | reqFormat) < 0) |
| 238 | return buffer; |
| 239 | |
| 240 | if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight)) |
| 241 | return buffer; |
| 242 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 243 | // this ensures our client doesn't go away while we're accessing |
| 244 | // the shared area. |
| 245 | sp<Client> ourClient(client.promote()); |
| 246 | if (ourClient == 0) { |
| 247 | // oops, the client is already gone |
| 248 | return buffer; |
| 249 | } |
| 250 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 251 | /* |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 252 | * This is called from the client's Surface::dequeue(). This can happen |
| 253 | * at any time, especially while we're in the middle of using the |
| 254 | * buffer 'index' as our front buffer. |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 255 | * |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 256 | * Make sure the buffer we're resizing is not the front buffer and has been |
| 257 | * dequeued. Once this condition is asserted, we are guaranteed that this |
| 258 | * buffer cannot become the front buffer under our feet, since we're called |
| 259 | * from Surface::dequeue() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 260 | */ |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 261 | status_t err = lcblk->assertReallocate(index); |
| 262 | LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 263 | if (err != NO_ERROR) { |
| 264 | // the surface may have died |
| 265 | return buffer; |
| 266 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 268 | uint32_t w, h, f; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 269 | { // scope for the lock |
| 270 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 271 | const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight); |
| 272 | const bool formatChanged = mReqFormat != reqFormat; |
| 273 | mReqWidth = reqWidth; |
| 274 | mReqHeight = reqHeight; |
| 275 | mReqFormat = reqFormat; |
| 276 | mFixedSize = reqWidth && reqHeight; |
| 277 | w = reqWidth ? reqWidth : mWidth; |
| 278 | h = reqHeight ? reqHeight : mHeight; |
| 279 | f = reqFormat ? reqFormat : mFormat; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 280 | buffer = mBufferManager.detachBuffer(index); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 281 | if (fixedSizeChanged || formatChanged) { |
| 282 | lcblk->reallocateAllExcept(index); |
| 283 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 286 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | 6d9f698 | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 287 | if (buffer!=0 && buffer->getStrongCount() == 1) { |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 288 | err = buffer->reallocate(w, h, f, effectiveUsage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 289 | } else { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 290 | // here we have to reallocate a new buffer because we could have a |
| 291 | // client in our process with a reference to it (eg: status bar), |
| 292 | // and we can't release the handle under its feet. |
| 293 | buffer.clear(); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 294 | buffer = new GraphicBuffer(w, h, f, effectiveUsage); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 295 | err = buffer->initCheck(); |
| 296 | } |
| 297 | |
| 298 | if (err || buffer->handle == 0) { |
| 299 | LOGE_IF(err || buffer->handle == 0, |
| 300 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 301 | this, index, w, h, strerror(-err)); |
| 302 | } else { |
| 303 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 304 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 305 | this, index, w, h, buffer->handle); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 309 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 310 | mBufferManager.attachBuffer(index, buffer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 311 | } |
| 312 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 315 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 316 | { |
| 317 | /* |
| 318 | * buffers used for software rendering, but h/w composition |
| 319 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 320 | * |
| 321 | * buffers used for h/w rendering and h/w composition |
| 322 | * are allocated with HW_RENDER | HW_TEXTURE |
| 323 | * |
| 324 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 325 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 326 | * |
| 327 | */ |
| 328 | |
| 329 | if (mSecure) { |
| 330 | // secure buffer, don't store it into the GPU |
| 331 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 332 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 333 | } else { |
| 334 | // it's allowed to modify the usage flags here, but generally |
| 335 | // the requested flags should be honored. |
Mathias Agopian | 89141f9 | 2010-05-10 20:10:10 -0700 | [diff] [blame] | 336 | // request EGLImage for all buffers |
| 337 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 338 | } |
| 339 | return usage; |
| 340 | } |
| 341 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | uint32_t Layer::doTransaction(uint32_t flags) |
| 343 | { |
| 344 | const Layer::State& front(drawingState()); |
| 345 | const Layer::State& temp(currentState()); |
| 346 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 347 | const bool sizeChanged = (front.requested_w != temp.requested_w) || |
| 348 | (front.requested_h != temp.requested_h); |
| 349 | |
| 350 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 351 | // 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] | 352 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 353 | "resize (layer=%p), requested (%dx%d), drawing (%d,%d)", |
| 354 | this, |
| 355 | int(temp.requested_w), int(temp.requested_h), |
| 356 | int(front.requested_w), int(front.requested_h)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 358 | if (!isFixedSize()) { |
| 359 | // we're being resized and there is a freeze display request, |
| 360 | // acquire a freeze lock, so that the screen stays put |
| 361 | // until we've redrawn at the new size; this is to avoid |
| 362 | // glitches upon orientation changes. |
| 363 | if (mFlinger->hasFreezeRequest()) { |
| 364 | // if the surface is hidden, don't try to acquire the |
| 365 | // freeze lock, since hidden surfaces may never redraw |
| 366 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 367 | mFreezeLock = mFlinger->getFreezeLock(); |
| 368 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 370 | |
| 371 | // this will make sure LayerBase::doTransaction doesn't update |
| 372 | // the drawing state's size |
| 373 | Layer::State& editDraw(mDrawingState); |
| 374 | editDraw.requested_w = temp.requested_w; |
| 375 | editDraw.requested_h = temp.requested_h; |
| 376 | |
| 377 | // record the new size, form this point on, when the client request |
| 378 | // a buffer, it'll get the new size. |
| 379 | setBufferSize(temp.requested_w, temp.requested_h); |
| 380 | |
| 381 | // all buffers need reallocation |
| 382 | lcblk->reallocateAll(); |
| 383 | } else { |
| 384 | // record the new size |
| 385 | setBufferSize(temp.requested_w, temp.requested_h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 388 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | if (temp.sequence != front.sequence) { |
| 390 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 391 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 392 | // (it may never redraw, which is fine if it is hidden) |
| 393 | mFreezeLock.clear(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return LayerBase::doTransaction(flags); |
| 398 | } |
| 399 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 400 | void Layer::setBufferSize(uint32_t w, uint32_t h) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 401 | Mutex::Autolock _l(mLock); |
| 402 | mWidth = w; |
| 403 | mHeight = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 406 | bool Layer::isFixedSize() const { |
| 407 | Mutex::Autolock _l(mLock); |
| 408 | return mFixedSize; |
| 409 | } |
| 410 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | // ---------------------------------------------------------------------------- |
| 412 | // pageflip handling... |
| 413 | // ---------------------------------------------------------------------------- |
| 414 | |
| 415 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 416 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 417 | ssize_t buf = lcblk->retireAndLock(); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 418 | if (buf == NOT_ENOUGH_DATA) { |
| 419 | // NOTE: This is not an error, it simply means there is nothing to |
| 420 | // retire. The buffer is locked because we will use it |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 421 | // for composition later in the loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | return; |
| 423 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 424 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 425 | if (buf < NO_ERROR) { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 426 | LOGE("retireAndLock() buffer index (%d) out of range", buf); |
| 427 | mPostedDirtyRegion.clear(); |
| 428 | return; |
| 429 | } |
| 430 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 431 | // we retired a buffer, which becomes the new front buffer |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 432 | if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) { |
| 433 | LOGE("retireAndLock() buffer index (%d) out of range", buf); |
| 434 | mPostedDirtyRegion.clear(); |
| 435 | return; |
| 436 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 438 | // get the dirty region |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 439 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 440 | if (newFrontBuffer != NULL) { |
| 441 | // compute the posted region |
| 442 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 443 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 444 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 445 | // update the layer size and release freeze-lock |
| 446 | const Layer::State& front(drawingState()); |
| 447 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 448 | newFrontBuffer->getHeight() == front.requested_h) |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 449 | { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 450 | if ((front.w != front.requested_w) || |
| 451 | (front.h != front.requested_h)) |
| 452 | { |
| 453 | // Here we pretend the transaction happened by updating the |
| 454 | // current and drawing states. Drawing state is only accessed |
| 455 | // in this thread, no need to have it locked |
| 456 | Layer::State& editDraw(mDrawingState); |
| 457 | editDraw.w = editDraw.requested_w; |
| 458 | editDraw.h = editDraw.requested_h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 459 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 460 | // We also need to update the current state so that we don't |
| 461 | // end-up doing too much work during the next transaction. |
| 462 | // NOTE: We actually don't need hold the transaction lock here |
| 463 | // because State::w and State::h are only accessed from |
| 464 | // this thread |
| 465 | Layer::State& editTemp(currentState()); |
| 466 | editTemp.w = editDraw.w; |
| 467 | editTemp.h = editDraw.h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 468 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 469 | // recompute visible region |
| 470 | recomputeVisibleRegions = true; |
| 471 | } |
| 472 | |
| 473 | // we now have the correct size, unfreeze the screen |
| 474 | mFreezeLock.clear(); |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 475 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 476 | } else { |
| 477 | // this should not happen unless we ran out of memory while |
| 478 | // allocating the buffer. we're hoping that things will get back |
| 479 | // to normal the next time the app tries to draw into this buffer. |
| 480 | // meanwhile, pretend the screen didn't update. |
| 481 | mPostedDirtyRegion.clear(); |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Mathias Agopian | e700501 | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 484 | if (lcblk->getQueuedCount()) { |
| 485 | // signal an event if we have more buffers waiting |
| 486 | mFlinger->signalEvent(); |
| 487 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | |
Mathias Agopian | 245e4d7 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 489 | /* a buffer was posted, so we need to call reloadTexture(), which |
| 490 | * will update our internal data structures (eg: EGLImageKHR or |
| 491 | * texture names). we need to do this even if mPostedDirtyRegion is |
| 492 | * empty -- it's orthogonal to the fact that a new buffer was posted, |
| 493 | * for instance, a degenerate case could be that the user did an empty |
| 494 | * update but repainted the buffer with appropriate content (after a |
| 495 | * resize for instance). |
| 496 | */ |
| 497 | reloadTexture( mPostedDirtyRegion ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void Layer::unlockPageFlip( |
| 501 | const Transform& planeTransform, Region& outDirtyRegion) |
| 502 | { |
| 503 | Region dirtyRegion(mPostedDirtyRegion); |
| 504 | if (!dirtyRegion.isEmpty()) { |
| 505 | mPostedDirtyRegion.clear(); |
| 506 | // The dirty region is given in the layer's coordinate space |
| 507 | // transform the dirty region by the surface's transformation |
| 508 | // and the global transformation. |
| 509 | const Layer::State& s(drawingState()); |
| 510 | const Transform tr(planeTransform * s.transform); |
| 511 | dirtyRegion = tr.transform(dirtyRegion); |
| 512 | |
| 513 | // At this point, the dirty region is in screen space. |
| 514 | // Make sure it's constrained by the visible region (which |
| 515 | // is in screen space as well). |
| 516 | dirtyRegion.andSelf(visibleRegionScreen); |
| 517 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 519 | if (visibleRegionScreen.isEmpty()) { |
| 520 | // an invisible layer should not hold a freeze-lock |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 521 | // (because it may never be updated and therefore never release it) |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 522 | mFreezeLock.clear(); |
| 523 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void Layer::finishPageFlip() |
| 527 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 528 | int buf = mBufferManager.getActiveBufferIndex(); |
| 529 | status_t err = lcblk->unlock( buf ); |
| 530 | 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] | 531 | } |
| 532 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 533 | |
| 534 | void Layer::dump(String8& result, char* buffer, size_t SIZE) const |
| 535 | { |
| 536 | LayerBaseClient::dump(result, buffer, SIZE); |
| 537 | |
| 538 | SharedBufferStack::Statistics stats = lcblk->getStats(); |
| 539 | result.append( lcblk->dump(" ") ); |
| 540 | sp<const GraphicBuffer> buf0(getBuffer(0)); |
| 541 | sp<const GraphicBuffer> buf1(getBuffer(1)); |
| 542 | uint32_t w0=0, h0=0, s0=0; |
| 543 | uint32_t w1=0, h1=0, s1=0; |
| 544 | if (buf0 != 0) { |
| 545 | w0 = buf0->getWidth(); |
| 546 | h0 = buf0->getHeight(); |
| 547 | s0 = buf0->getStride(); |
| 548 | } |
| 549 | if (buf1 != 0) { |
| 550 | w1 = buf1->getWidth(); |
| 551 | h1 = buf1->getHeight(); |
| 552 | s1 = buf1->getStride(); |
| 553 | } |
| 554 | snprintf(buffer, SIZE, |
| 555 | " " |
| 556 | "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u]," |
| 557 | " freezeLock=%p, dq-q-time=%u us\n", |
| 558 | pixelFormat(), |
| 559 | w0, h0, s0, w1, h1, s1, |
| 560 | getFreezeLock().get(), stats.totalTime); |
| 561 | |
| 562 | result.append(buffer); |
| 563 | } |
| 564 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 565 | // --------------------------------------------------------------------------- |
| 566 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 567 | Layer::BufferManager::BufferManager(TextureManager& tm) |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 568 | : mNumBuffers(NUM_BUFFERS), mTextureManager(tm), |
| 569 | mActiveBuffer(0), mFailover(false) |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 570 | { |
| 571 | } |
| 572 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 573 | Layer::BufferManager::~BufferManager() |
| 574 | { |
| 575 | } |
| 576 | |
| 577 | status_t Layer::BufferManager::resize(size_t size) |
| 578 | { |
| 579 | Mutex::Autolock _l(mLock); |
| 580 | mNumBuffers = size; |
| 581 | return NO_ERROR; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // only for debugging |
| 585 | sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const { |
| 586 | return mBufferData[index].buffer; |
| 587 | } |
| 588 | |
| 589 | status_t Layer::BufferManager::setActiveBufferIndex(size_t index) { |
| 590 | // TODO: need to validate 'index' |
| 591 | mActiveBuffer = index; |
| 592 | return NO_ERROR; |
| 593 | } |
| 594 | |
| 595 | size_t Layer::BufferManager::getActiveBufferIndex() const { |
| 596 | return mActiveBuffer; |
| 597 | } |
| 598 | |
| 599 | Texture Layer::BufferManager::getActiveTexture() const { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 600 | Texture res; |
| 601 | if (mFailover) { |
| 602 | res = mFailoverTexture; |
| 603 | } else { |
| 604 | static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture; |
| 605 | } |
| 606 | return res; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 610 | const size_t activeBuffer = mActiveBuffer; |
| 611 | BufferData const * const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 612 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 613 | return buffers[activeBuffer].buffer; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index) |
| 617 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 618 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 619 | sp<GraphicBuffer> buffer; |
| 620 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 621 | buffer = buffers[index].buffer; |
| 622 | buffers[index].buffer = 0; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 623 | return buffer; |
| 624 | } |
| 625 | |
| 626 | status_t Layer::BufferManager::attachBuffer(size_t index, |
| 627 | const sp<GraphicBuffer>& buffer) |
| 628 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 629 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 630 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 631 | buffers[index].buffer = buffer; |
| 632 | buffers[index].texture.dirty = true; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 633 | return NO_ERROR; |
| 634 | } |
| 635 | |
| 636 | status_t Layer::BufferManager::destroy(EGLDisplay dpy) |
| 637 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 638 | BufferData* const buffers = mBufferData; |
| 639 | size_t num; |
| 640 | { // scope for the lock |
| 641 | Mutex::Autolock _l(mLock); |
| 642 | num = mNumBuffers; |
| 643 | for (size_t i=0 ; i<num ; i++) { |
| 644 | buffers[i].buffer = 0; |
| 645 | } |
| 646 | } |
| 647 | for (size_t i=0 ; i<num ; i++) { |
| 648 | destroyTexture(&buffers[i].texture, dpy); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 649 | } |
| 650 | destroyTexture(&mFailoverTexture, dpy); |
| 651 | return NO_ERROR; |
| 652 | } |
| 653 | |
| 654 | status_t Layer::BufferManager::initEglImage(EGLDisplay dpy, |
| 655 | const sp<GraphicBuffer>& buffer) |
| 656 | { |
| 657 | size_t index = mActiveBuffer; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 658 | Image& texture(mBufferData[index].texture); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 659 | status_t err = mTextureManager.initEglImage(&texture, dpy, buffer); |
| 660 | // if EGLImage fails, we switch to regular texture mode, and we |
| 661 | // free all resources associated with using EGLImages. |
| 662 | if (err == NO_ERROR) { |
| 663 | mFailover = false; |
| 664 | destroyTexture(&mFailoverTexture, dpy); |
| 665 | } else { |
| 666 | mFailover = true; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 667 | const size_t num = mNumBuffers; |
| 668 | for (size_t i=0 ; i<num ; i++) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 669 | destroyTexture(&mBufferData[i].texture, dpy); |
| 670 | } |
| 671 | } |
| 672 | return err; |
| 673 | } |
| 674 | |
| 675 | status_t Layer::BufferManager::loadTexture( |
| 676 | const Region& dirty, const GGLSurface& t) |
| 677 | { |
| 678 | return mTextureManager.loadTexture(&mFailoverTexture, dirty, t); |
| 679 | } |
| 680 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 681 | status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy) |
| 682 | { |
| 683 | if (tex->name != -1U) { |
| 684 | glDeleteTextures(1, &tex->name); |
| 685 | tex->name = -1U; |
| 686 | } |
| 687 | if (tex->image != EGL_NO_IMAGE_KHR) { |
| 688 | eglDestroyImageKHR(dpy, tex->image); |
| 689 | tex->image = EGL_NO_IMAGE_KHR; |
| 690 | } |
| 691 | return NO_ERROR; |
| 692 | } |
| 693 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 694 | // --------------------------------------------------------------------------- |
| 695 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 696 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
| 697 | SurfaceID id, const sp<Layer>& owner) |
| 698 | : Surface(flinger, id, owner->getIdentity(), owner) |
| 699 | { |
| 700 | } |
| 701 | |
| 702 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 703 | { |
| 704 | } |
| 705 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 706 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, |
| 707 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 708 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 709 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 710 | sp<Layer> owner(getOwner()); |
| 711 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 712 | /* |
| 713 | * requestBuffer() cannot be called from the main thread |
| 714 | * as it could cause a dead-lock, since it may have to wait |
| 715 | * on conditions updated my the main thread. |
| 716 | */ |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame^] | 717 | buffer = owner->requestBuffer(index, w, h, format, usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 718 | } |
| 719 | return buffer; |
| 720 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 722 | status_t Layer::SurfaceLayer::setBufferCount(int bufferCount) |
| 723 | { |
| 724 | status_t err = DEAD_OBJECT; |
| 725 | sp<Layer> owner(getOwner()); |
| 726 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 727 | /* |
| 728 | * setBufferCount() cannot be called from the main thread |
| 729 | * as it could cause a dead-lock, since it may have to wait |
| 730 | * on conditions updated my the main thread. |
| 731 | */ |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 732 | err = owner->setBufferCount(bufferCount); |
| 733 | } |
| 734 | return err; |
| 735 | } |
| 736 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | // --------------------------------------------------------------------------- |
| 738 | |
| 739 | |
| 740 | }; // namespace android |