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