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