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