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