The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Surface" |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/Errors.h> |
| 27 | #include <utils/threads.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> |
| 29 | #include <binder/IMemory.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <ui/DisplayInfo.h> |
| 33 | #include <ui/BufferMapper.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <ui/ISurface.h> |
| 35 | #include <ui/Surface.h> |
| 36 | #include <ui/SurfaceComposerClient.h> |
| 37 | #include <ui/Rect.h> |
| 38 | |
Mathias Agopian | 7189c00 | 2009-05-05 18:11:11 -0700 | [diff] [blame] | 39 | #include <pixelflinger/pixelflinger.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | #include <private/ui/SharedState.h> |
| 42 | #include <private/ui/LayerState.h> |
Mathias Agopian | 7189c00 | 2009-05-05 18:11:11 -0700 | [diff] [blame] | 43 | #include <private/ui/SurfaceBuffer.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 | // ============================================================================ |
| 48 | // SurfaceBuffer |
| 49 | // ============================================================================ |
| 50 | |
| 51 | SurfaceBuffer::SurfaceBuffer() |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 52 | : BASE(), mOwner(false), mBufferMapper(BufferMapper::get()) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 53 | { |
| 54 | width = |
| 55 | height = |
| 56 | stride = |
| 57 | format = |
| 58 | usage = 0; |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 59 | handle = NULL; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SurfaceBuffer::SurfaceBuffer(const Parcel& data) |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 63 | : BASE(), mOwner(true), mBufferMapper(BufferMapper::get()) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 64 | { |
| 65 | // we own the handle in this case |
| 66 | width = data.readInt32(); |
| 67 | height = data.readInt32(); |
| 68 | stride = data.readInt32(); |
| 69 | format = data.readInt32(); |
| 70 | usage = data.readInt32(); |
| 71 | handle = data.readNativeHandle(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | SurfaceBuffer::~SurfaceBuffer() |
| 75 | { |
| 76 | if (handle && mOwner) { |
| 77 | native_handle_close(handle); |
| 78 | native_handle_delete(const_cast<native_handle*>(handle)); |
| 79 | } |
| 80 | } |
| 81 | |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 82 | status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr) |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 83 | { |
| 84 | const Rect lockBounds(width, height); |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 85 | status_t res = lock(usage, lockBounds, vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 86 | return res; |
| 87 | } |
| 88 | |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 89 | status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr) |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 90 | { |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 91 | if (rect.left < 0 || rect.right > this->width || |
| 92 | rect.top < 0 || rect.bottom > this->height) { |
| 93 | LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 94 | rect.left, rect.top, rect.right, rect.bottom, |
| 95 | this->width, this->height); |
| 96 | return BAD_VALUE; |
| 97 | } |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 98 | status_t res = getBufferMapper().lock(handle, usage, rect, vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 99 | return res; |
| 100 | } |
| 101 | |
| 102 | status_t SurfaceBuffer::unlock() |
| 103 | { |
| 104 | status_t res = getBufferMapper().unlock(handle); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 105 | return res; |
| 106 | } |
| 107 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 108 | status_t SurfaceBuffer::writeToParcel(Parcel* reply, |
| 109 | android_native_buffer_t const* buffer) |
| 110 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | reply->writeInt32(buffer->width); |
| 112 | reply->writeInt32(buffer->height); |
| 113 | reply->writeInt32(buffer->stride); |
| 114 | reply->writeInt32(buffer->format); |
| 115 | reply->writeInt32(buffer->usage); |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 116 | reply->writeNativeHandle(buffer->handle); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 117 | return NO_ERROR; |
| 118 | } |
| 119 | |
| 120 | // ---------------------------------------------------------------------- |
| 121 | |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 122 | static status_t copyBlt( |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 123 | const sp<SurfaceBuffer>& dst, |
| 124 | const sp<SurfaceBuffer>& src, |
| 125 | const Region& reg) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 126 | { |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 127 | status_t err; |
| 128 | uint8_t const * src_bits = NULL; |
| 129 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits); |
| 130 | LOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 131 | |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 132 | uint8_t* dst_bits = NULL; |
| 133 | err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits); |
| 134 | LOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
| 135 | |
| 136 | Region::const_iterator head(reg.begin()); |
| 137 | Region::const_iterator tail(reg.end()); |
| 138 | if (head != tail && src_bits && dst_bits) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 139 | // NOTE: dst and src must be the same format |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 140 | const size_t bpp = bytesPerPixel(src->format); |
| 141 | const size_t dbpr = dst->stride * bpp; |
| 142 | const size_t sbpr = src->stride * bpp; |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 143 | |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 144 | while (head != tail) { |
| 145 | const Rect& r(*head++); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 146 | ssize_t h = r.height(); |
| 147 | if (h <= 0) continue; |
| 148 | size_t size = r.width() * bpp; |
| 149 | uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp; |
| 150 | uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp; |
| 151 | if (dbpr==sbpr && size==sbpr) { |
| 152 | size *= h; |
| 153 | h = 1; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 154 | } |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 155 | do { |
| 156 | memcpy(d, s, size); |
| 157 | d += dbpr; |
| 158 | s += sbpr; |
| 159 | } while (--h > 0); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 162 | |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 163 | if (src_bits) |
| 164 | src->unlock(); |
| 165 | |
| 166 | if (dst_bits) |
| 167 | dst->unlock(); |
| 168 | |
| 169 | return err; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 172 | // ============================================================================ |
| 173 | // SurfaceControl |
| 174 | // ============================================================================ |
| 175 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 176 | SurfaceControl::SurfaceControl( |
| 177 | const sp<SurfaceComposerClient>& client, |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 178 | const sp<ISurface>& surface, |
| 179 | const ISurfaceFlingerClient::surface_data_t& data, |
Mathias Agopian | 18d8446 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 180 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 181 | : mClient(client), mSurface(surface), |
| 182 | mToken(data.token), mIdentity(data.identity), |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 183 | mWidth(w), mHeight(h), mFormat(format), mFlags(flags) |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 184 | { |
| 185 | } |
Mathias Agopian | 18d8446 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 186 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 187 | SurfaceControl::~SurfaceControl() |
| 188 | { |
| 189 | destroy(); |
| 190 | } |
| 191 | |
| 192 | void SurfaceControl::destroy() |
| 193 | { |
Mathias Agopian | 18d8446 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 194 | if (isValid()) { |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 195 | mClient->destroySurface(mToken); |
| 196 | } |
| 197 | |
| 198 | // clear all references and trigger an IPC now, to make sure things |
| 199 | // happen without delay, since these resources are quite heavy. |
| 200 | mClient.clear(); |
| 201 | mSurface.clear(); |
| 202 | IPCThreadState::self()->flushCommands(); |
| 203 | } |
| 204 | |
| 205 | void SurfaceControl::clear() |
| 206 | { |
| 207 | // here, the window manager tells us explicitly that we should destroy |
| 208 | // the surface's resource. Soon after this call, it will also release |
| 209 | // its last reference (which will call the dtor); however, it is possible |
| 210 | // that a client living in the same process still holds references which |
| 211 | // would delay the call to the dtor -- that is why we need this explicit |
| 212 | // "clear()" call. |
| 213 | destroy(); |
| 214 | } |
| 215 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 216 | bool SurfaceControl::isSameSurface( |
| 217 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) |
| 218 | { |
| 219 | if (lhs == 0 || rhs == 0) |
| 220 | return false; |
| 221 | return lhs->mSurface->asBinder() == rhs->mSurface->asBinder(); |
| 222 | } |
| 223 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 224 | status_t SurfaceControl::setLayer(int32_t layer) { |
| 225 | const sp<SurfaceComposerClient>& client(mClient); |
| 226 | if (client == 0) return NO_INIT; |
| 227 | status_t err = validate(client->mControl); |
| 228 | if (err < 0) return err; |
| 229 | return client->setLayer(mToken, layer); |
| 230 | } |
| 231 | status_t SurfaceControl::setPosition(int32_t x, int32_t y) { |
| 232 | const sp<SurfaceComposerClient>& client(mClient); |
| 233 | if (client == 0) return NO_INIT; |
| 234 | status_t err = validate(client->mControl); |
| 235 | if (err < 0) return err; |
| 236 | return client->setPosition(mToken, x, y); |
| 237 | } |
| 238 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { |
| 239 | const sp<SurfaceComposerClient>& client(mClient); |
| 240 | if (client == 0) return NO_INIT; |
| 241 | status_t err = validate(client->mControl); |
| 242 | if (err < 0) return err; |
| 243 | return client->setSize(mToken, w, h); |
| 244 | } |
| 245 | status_t SurfaceControl::hide() { |
| 246 | const sp<SurfaceComposerClient>& client(mClient); |
| 247 | if (client == 0) return NO_INIT; |
| 248 | status_t err = validate(client->mControl); |
| 249 | if (err < 0) return err; |
| 250 | return client->hide(mToken); |
| 251 | } |
| 252 | status_t SurfaceControl::show(int32_t layer) { |
| 253 | const sp<SurfaceComposerClient>& client(mClient); |
| 254 | if (client == 0) return NO_INIT; |
| 255 | status_t err = validate(client->mControl); |
| 256 | if (err < 0) return err; |
| 257 | return client->show(mToken, layer); |
| 258 | } |
| 259 | status_t SurfaceControl::freeze() { |
| 260 | const sp<SurfaceComposerClient>& client(mClient); |
| 261 | if (client == 0) return NO_INIT; |
| 262 | status_t err = validate(client->mControl); |
| 263 | if (err < 0) return err; |
| 264 | return client->freeze(mToken); |
| 265 | } |
| 266 | status_t SurfaceControl::unfreeze() { |
| 267 | const sp<SurfaceComposerClient>& client(mClient); |
| 268 | if (client == 0) return NO_INIT; |
| 269 | status_t err = validate(client->mControl); |
| 270 | if (err < 0) return err; |
| 271 | return client->unfreeze(mToken); |
| 272 | } |
| 273 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { |
| 274 | const sp<SurfaceComposerClient>& client(mClient); |
| 275 | if (client == 0) return NO_INIT; |
| 276 | status_t err = validate(client->mControl); |
| 277 | if (err < 0) return err; |
| 278 | return client->setFlags(mToken, flags, mask); |
| 279 | } |
| 280 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { |
| 281 | const sp<SurfaceComposerClient>& client(mClient); |
| 282 | if (client == 0) return NO_INIT; |
| 283 | status_t err = validate(client->mControl); |
| 284 | if (err < 0) return err; |
| 285 | return client->setTransparentRegionHint(mToken, transparent); |
| 286 | } |
| 287 | status_t SurfaceControl::setAlpha(float alpha) { |
| 288 | const sp<SurfaceComposerClient>& client(mClient); |
| 289 | if (client == 0) return NO_INIT; |
| 290 | status_t err = validate(client->mControl); |
| 291 | if (err < 0) return err; |
| 292 | return client->setAlpha(mToken, alpha); |
| 293 | } |
| 294 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { |
| 295 | const sp<SurfaceComposerClient>& client(mClient); |
| 296 | if (client == 0) return NO_INIT; |
| 297 | status_t err = validate(client->mControl); |
| 298 | if (err < 0) return err; |
| 299 | return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy); |
| 300 | } |
| 301 | status_t SurfaceControl::setFreezeTint(uint32_t tint) { |
| 302 | const sp<SurfaceComposerClient>& client(mClient); |
| 303 | if (client == 0) return NO_INIT; |
| 304 | status_t err = validate(client->mControl); |
| 305 | if (err < 0) return err; |
| 306 | return client->setFreezeTint(mToken, tint); |
| 307 | } |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 308 | |
| 309 | status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const |
| 310 | { |
| 311 | if (mToken<0 || mClient==0) { |
| 312 | LOGE("invalid token (%d, identity=%u) or client (%p)", |
| 313 | mToken, mIdentity, mClient.get()); |
| 314 | return NO_INIT; |
| 315 | } |
| 316 | if (cblk == 0) { |
| 317 | LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity); |
| 318 | return NO_INIT; |
| 319 | } |
| 320 | status_t err = cblk->validate(mToken); |
| 321 | if (err != NO_ERROR) { |
| 322 | LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)", |
| 323 | mToken, mIdentity, err, strerror(-err)); |
| 324 | return err; |
| 325 | } |
| 326 | if (mIdentity != uint32_t(cblk->layers[mToken].identity)) { |
| 327 | LOGE("using an invalid surface id=%d, identity=%u should be %d", |
| 328 | mToken, mIdentity, cblk->layers[mToken].identity); |
| 329 | return NO_INIT; |
| 330 | } |
| 331 | return NO_ERROR; |
| 332 | } |
| 333 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 334 | status_t SurfaceControl::writeSurfaceToParcel( |
| 335 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 336 | { |
| 337 | uint32_t flags = 0; |
| 338 | uint32_t format = 0; |
| 339 | SurfaceID token = -1; |
| 340 | uint32_t identity = 0; |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 341 | uint32_t width = 0; |
| 342 | uint32_t height = 0; |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 343 | sp<SurfaceComposerClient> client; |
| 344 | sp<ISurface> sur; |
| 345 | if (SurfaceControl::isValid(control)) { |
| 346 | token = control->mToken; |
| 347 | identity = control->mIdentity; |
| 348 | client = control->mClient; |
| 349 | sur = control->mSurface; |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 350 | width = control->mWidth; |
| 351 | height = control->mHeight; |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 352 | format = control->mFormat; |
| 353 | flags = control->mFlags; |
| 354 | } |
| 355 | parcel->writeStrongBinder(client!=0 ? client->connection() : NULL); |
| 356 | parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL); |
| 357 | parcel->writeInt32(token); |
| 358 | parcel->writeInt32(identity); |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 359 | parcel->writeInt32(width); |
| 360 | parcel->writeInt32(height); |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 361 | parcel->writeInt32(format); |
| 362 | parcel->writeInt32(flags); |
| 363 | return NO_ERROR; |
| 364 | } |
| 365 | |
| 366 | sp<Surface> SurfaceControl::getSurface() const |
| 367 | { |
| 368 | Mutex::Autolock _l(mLock); |
| 369 | if (mSurfaceData == 0) { |
| 370 | mSurfaceData = new Surface(const_cast<SurfaceControl*>(this)); |
| 371 | } |
| 372 | return mSurfaceData; |
| 373 | } |
| 374 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 375 | // ============================================================================ |
| 376 | // Surface |
| 377 | // ============================================================================ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 379 | Surface::Surface(const sp<SurfaceControl>& surface) |
| 380 | : mClient(surface->mClient), mSurface(surface->mSurface), |
| 381 | mToken(surface->mToken), mIdentity(surface->mIdentity), |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 382 | mWidth(surface->mWidth), mHeight(surface->mHeight), |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 383 | mFormat(surface->mFormat), mFlags(surface->mFlags), |
| 384 | mBufferMapper(BufferMapper::get()) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | { |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 386 | init(); |
| 387 | } |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 388 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 389 | Surface::Surface(const Parcel& parcel) |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 390 | : mBufferMapper(BufferMapper::get()) |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 391 | { |
| 392 | sp<IBinder> clientBinder = parcel.readStrongBinder(); |
| 393 | mSurface = interface_cast<ISurface>(parcel.readStrongBinder()); |
| 394 | mToken = parcel.readInt32(); |
| 395 | mIdentity = parcel.readInt32(); |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 396 | mWidth = parcel.readInt32(); |
| 397 | mHeight = parcel.readInt32(); |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 398 | mFormat = parcel.readInt32(); |
| 399 | mFlags = parcel.readInt32(); |
| 400 | |
| 401 | if (clientBinder != NULL) |
| 402 | mClient = SurfaceComposerClient::clientForConnection(clientBinder); |
| 403 | |
| 404 | init(); |
| 405 | } |
| 406 | |
| 407 | void Surface::init() |
| 408 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 409 | android_native_window_t::setSwapInterval = setSwapInterval; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 410 | android_native_window_t::dequeueBuffer = dequeueBuffer; |
| 411 | android_native_window_t::lockBuffer = lockBuffer; |
| 412 | android_native_window_t::queueBuffer = queueBuffer; |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 413 | android_native_window_t::query = query; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | mSwapRectangle.makeInvalid(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 415 | DisplayInfo dinfo; |
| 416 | SurfaceComposerClient::getDisplayInfo(0, &dinfo); |
| 417 | const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi; |
| 418 | const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi; |
| 419 | // FIXME: set real values here |
| 420 | const_cast<int&>(android_native_window_t::minSwapInterval) = 1; |
| 421 | const_cast<int&>(android_native_window_t::maxSwapInterval) = 1; |
| 422 | const_cast<uint32_t&>(android_native_window_t::flags) = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 425 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | Surface::~Surface() |
| 427 | { |
Mathias Agopian | 40b7f6e | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 428 | // this is a client-side operation, the surface is destroyed, unmap |
| 429 | // its buffers in this process. |
| 430 | for (int i=0 ; i<2 ; i++) { |
| 431 | if (mBuffers[i] != 0) { |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 432 | getBufferMapper().unregisterBuffer(mBuffers[i]->handle); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 433 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
Mathias Agopian | 40b7f6e | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 435 | |
Mathias Agopian | 40b7f6e | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 436 | // clear all references and trigger an IPC now, to make sure things |
| 437 | // happen without delay, since these resources are quite heavy. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | mClient.clear(); |
| 439 | mSurface.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | IPCThreadState::self()->flushCommands(); |
| 441 | } |
| 442 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 443 | status_t Surface::validate(per_client_cblk_t const* cblk) const |
| 444 | { |
Mathias Agopian | 40b7f6e | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 445 | if (mToken<0 || mClient==0) { |
| 446 | LOGE("invalid token (%d, identity=%u) or client (%p)", |
| 447 | mToken, mIdentity, mClient.get()); |
| 448 | return NO_INIT; |
| 449 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 450 | if (cblk == 0) { |
| 451 | LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity); |
| 452 | return NO_INIT; |
| 453 | } |
| 454 | status_t err = cblk->validate(mToken); |
| 455 | if (err != NO_ERROR) { |
| 456 | LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)", |
| 457 | mToken, mIdentity, err, strerror(-err)); |
| 458 | return err; |
| 459 | } |
| 460 | if (mIdentity != uint32_t(cblk->layers[mToken].identity)) { |
| 461 | LOGE("using an invalid surface id=%d, identity=%u should be %d", |
| 462 | mToken, mIdentity, cblk->layers[mToken].identity); |
| 463 | return NO_INIT; |
| 464 | } |
| 465 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 468 | |
| 469 | bool Surface::isSameSurface( |
| 470 | const sp<Surface>& lhs, const sp<Surface>& rhs) |
| 471 | { |
| 472 | if (lhs == 0 || rhs == 0) |
| 473 | return false; |
| 474 | return lhs->mSurface->asBinder() == rhs->mSurface->asBinder(); |
| 475 | } |
| 476 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 477 | // ---------------------------------------------------------------------------- |
| 478 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 479 | int Surface::setSwapInterval(android_native_window_t* window, int interval) |
| 480 | { |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | int Surface::dequeueBuffer(android_native_window_t* window, |
| 485 | android_native_buffer_t** buffer) |
| 486 | { |
| 487 | Surface* self = getSelf(window); |
| 488 | return self->dequeueBuffer(buffer); |
| 489 | } |
| 490 | |
| 491 | int Surface::lockBuffer(android_native_window_t* window, |
| 492 | android_native_buffer_t* buffer) |
| 493 | { |
| 494 | Surface* self = getSelf(window); |
| 495 | return self->lockBuffer(buffer); |
| 496 | } |
| 497 | |
| 498 | int Surface::queueBuffer(android_native_window_t* window, |
| 499 | android_native_buffer_t* buffer) |
| 500 | { |
| 501 | Surface* self = getSelf(window); |
| 502 | return self->queueBuffer(buffer); |
| 503 | } |
| 504 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 505 | int Surface::query(android_native_window_t* window, |
| 506 | int what, int* value) |
| 507 | { |
| 508 | Surface* self = getSelf(window); |
| 509 | return self->query(what, value); |
| 510 | } |
| 511 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 512 | // ---------------------------------------------------------------------------- |
| 513 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 514 | status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer) |
| 515 | { |
| 516 | android_native_buffer_t* out; |
| 517 | status_t err = dequeueBuffer(&out); |
| 518 | *buffer = SurfaceBuffer::getSelf(out); |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 519 | // reset the width/height with the what we get from the buffer |
| 520 | mWidth = uint32_t(out->width); |
| 521 | mHeight = uint32_t(out->height); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 522 | return err; |
| 523 | } |
| 524 | |
| 525 | status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer) |
| 526 | { |
| 527 | return lockBuffer(buffer.get()); |
| 528 | } |
| 529 | |
| 530 | status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer) |
| 531 | { |
| 532 | return queueBuffer(buffer.get()); |
| 533 | } |
| 534 | |
| 535 | // ---------------------------------------------------------------------------- |
| 536 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 537 | int Surface::dequeueBuffer(android_native_buffer_t** buffer) |
| 538 | { |
| 539 | // FIXME: dequeueBuffer() needs proper implementation |
| 540 | |
| 541 | Mutex::Autolock _l(mSurfaceLock); |
| 542 | |
| 543 | per_client_cblk_t* const cblk = mClient->mControl; |
| 544 | status_t err = validate(cblk); |
| 545 | if (err != NO_ERROR) |
| 546 | return err; |
| 547 | |
| 548 | SurfaceID index(mToken); |
| 549 | |
| 550 | int32_t backIdx = cblk->lock_layer(size_t(index), |
| 551 | per_client_cblk_t::BLOCKING); |
| 552 | |
| 553 | if (backIdx < 0) |
| 554 | return status_t(backIdx); |
| 555 | |
| 556 | mBackbufferIndex = backIdx; |
| 557 | layer_cblk_t* const lcblk = &(cblk->layers[index]); |
| 558 | |
| 559 | volatile const surface_info_t* const back = lcblk->surface + backIdx; |
| 560 | if (back->flags & surface_info_t::eNeedNewBuffer) { |
| 561 | getBufferLocked(backIdx); |
| 562 | } |
| 563 | |
| 564 | const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 565 | mDirtyRegion.set(backBuffer->width, backBuffer->height); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 566 | *buffer = backBuffer.get(); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 567 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 568 | return NO_ERROR; |
| 569 | } |
| 570 | |
| 571 | int Surface::lockBuffer(android_native_buffer_t* buffer) |
| 572 | { |
Mathias Agopian | 40b7f6e | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 573 | Mutex::Autolock _l(mSurfaceLock); |
| 574 | |
| 575 | per_client_cblk_t* const cblk = mClient->mControl; |
| 576 | status_t err = validate(cblk); |
| 577 | if (err != NO_ERROR) |
| 578 | return err; |
| 579 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 580 | // FIXME: lockBuffer() needs proper implementation |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | int Surface::queueBuffer(android_native_buffer_t* buffer) |
| 585 | { |
| 586 | Mutex::Autolock _l(mSurfaceLock); |
| 587 | |
| 588 | per_client_cblk_t* const cblk = mClient->mControl; |
| 589 | status_t err = validate(cblk); |
| 590 | if (err != NO_ERROR) |
| 591 | return err; |
| 592 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 593 | if (mSwapRectangle.isValid()) { |
| 594 | mDirtyRegion.set(mSwapRectangle); |
| 595 | } |
| 596 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 597 | // transmit the dirty region |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 598 | SurfaceID index(mToken); |
| 599 | layer_cblk_t* const lcblk = &(cblk->layers[index]); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 600 | _send_dirty_region(lcblk, mDirtyRegion); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 601 | |
| 602 | uint32_t newstate = cblk->unlock_layer_and_post(size_t(index)); |
| 603 | if (!(newstate & eNextFlipPending)) |
| 604 | mClient->signalServer(); |
| 605 | |
| 606 | return NO_ERROR; |
| 607 | } |
| 608 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame^] | 609 | int Surface::query(int what, int* value) |
| 610 | { |
| 611 | switch (what) { |
| 612 | case NATIVE_WINDOW_WIDTH: |
| 613 | *value = int(mWidth); |
| 614 | return NO_ERROR; |
| 615 | case NATIVE_WINDOW_HEIGHT: |
| 616 | *value = int(mHeight); |
| 617 | return NO_ERROR; |
| 618 | } |
| 619 | return BAD_VALUE; |
| 620 | } |
| 621 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 622 | // ---------------------------------------------------------------------------- |
| 623 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | status_t Surface::lock(SurfaceInfo* info, bool blocking) { |
| 625 | return Surface::lock(info, NULL, blocking); |
| 626 | } |
| 627 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 628 | status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 629 | { |
| 630 | // FIXME: needs some locking here |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 631 | |
| 632 | sp<SurfaceBuffer> backBuffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 633 | status_t err = dequeueBuffer(&backBuffer); |
| 634 | if (err == NO_ERROR) { |
| 635 | err = lockBuffer(backBuffer); |
| 636 | if (err == NO_ERROR) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 637 | // we handle copy-back here... |
| 638 | |
| 639 | const Rect bounds(backBuffer->width, backBuffer->height); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 640 | Region scratch(bounds); |
| 641 | Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 642 | |
| 643 | per_client_cblk_t* const cblk = mClient->mControl; |
| 644 | layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]); |
| 645 | volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex; |
| 646 | if (back->flags & surface_info_t::eBufferDirty) { |
| 647 | // content is meaningless in this case and the whole surface |
| 648 | // needs to be redrawn. |
| 649 | newDirtyRegion.set(bounds); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 650 | } else { |
| 651 | newDirtyRegion.andSelf(bounds); |
Mathias Agopian | 1499859 | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 652 | const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]); |
| 653 | if (backBuffer->width == frontBuffer->width && |
| 654 | backBuffer->height == frontBuffer->height && |
| 655 | !(lcblk->flags & eNoCopyBack)) |
| 656 | { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 657 | const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion)); |
| 658 | if (!copyback.isEmpty() && frontBuffer!=0) { |
| 659 | // copy front to back |
| 660 | copyBlt(backBuffer, frontBuffer, copyback); |
| 661 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 664 | mDirtyRegion = newDirtyRegion; |
| 665 | mOldDirtyRegion = newDirtyRegion; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 666 | |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 667 | void* vaddr; |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 668 | status_t res = backBuffer->lock( |
| 669 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 670 | newDirtyRegion.bounds(), &vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 671 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 672 | LOGW_IF(res, "failed locking buffer %d (%p)", |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 673 | mBackbufferIndex, backBuffer->handle); |
| 674 | |
| 675 | mLockedBuffer = backBuffer; |
| 676 | other->w = backBuffer->width; |
| 677 | other->h = backBuffer->height; |
| 678 | other->s = backBuffer->stride; |
| 679 | other->usage = backBuffer->usage; |
| 680 | other->format = backBuffer->format; |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 681 | other->bits = vaddr; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | status_t Surface::unlockAndPost() |
| 688 | { |
| 689 | // FIXME: needs some locking here |
| 690 | |
| 691 | if (mLockedBuffer == 0) |
| 692 | return BAD_VALUE; |
| 693 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 694 | status_t res = mLockedBuffer->unlock(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 695 | LOGW_IF(res, "failed unlocking buffer %d (%p)", |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 696 | mBackbufferIndex, mLockedBuffer->handle); |
| 697 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 698 | status_t err = queueBuffer(mLockedBuffer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 699 | mLockedBuffer = 0; |
| 700 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 703 | void Surface::_send_dirty_region( |
| 704 | layer_cblk_t* lcblk, const Region& dirty) |
| 705 | { |
| 706 | const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift; |
| 707 | flat_region_t* flat_region = lcblk->region + index; |
| 708 | status_t err = dirty.write(flat_region, sizeof(flat_region_t)); |
| 709 | if (err < NO_ERROR) { |
| 710 | // region doesn't fit, use the bounds |
| 711 | const Region reg(dirty.bounds()); |
| 712 | reg.write(flat_region, sizeof(flat_region_t)); |
| 713 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } |
| 715 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | void Surface::setSwapRectangle(const Rect& r) { |
| 717 | mSwapRectangle = r; |
| 718 | } |
| 719 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 720 | status_t Surface::getBufferLocked(int index) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 722 | status_t err = NO_MEMORY; |
| 723 | sp<SurfaceBuffer> buffer = mSurface->getBuffer(); |
| 724 | LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL"); |
| 725 | if (buffer != 0) { |
| 726 | sp<SurfaceBuffer>& currentBuffer(mBuffers[index]); |
| 727 | if (currentBuffer != 0) { |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 728 | getBufferMapper().unregisterBuffer(currentBuffer->handle); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 729 | currentBuffer.clear(); |
| 730 | } |
Mathias Agopian | 21c59d0 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 731 | err = getBufferMapper().registerBuffer(buffer->handle); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 732 | LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err)); |
| 733 | if (err == NO_ERROR) { |
| 734 | currentBuffer = buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } |
| 736 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 737 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
| 739 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | }; // namespace android |
| 741 | |