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