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