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