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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/Errors.h> |
| 25 | #include <utils/threads.h> |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 26 | #include <utils/CallStack.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
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 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <ui/DisplayInfo.h> |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 33 | #include <ui/GraphicBuffer.h> |
| 34 | #include <ui/GraphicBufferMapper.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <ui/Rect.h> |
| 36 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 37 | #include <surfaceflinger/Surface.h> |
| 38 | #include <surfaceflinger/ISurface.h> |
| 39 | #include <surfaceflinger/ISurfaceComposer.h> |
| 40 | #include <surfaceflinger/SurfaceComposerClient.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 42 | #include <private/surfaceflinger/SharedBufferStack.h> |
| 43 | #include <private/surfaceflinger/LayerState.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 | // ---------------------------------------------------------------------- |
| 48 | |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 49 | static status_t copyBlt( |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 50 | const sp<GraphicBuffer>& dst, |
| 51 | const sp<GraphicBuffer>& src, |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 52 | const Region& reg) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 53 | { |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 54 | // src and dst with, height and format must be identical. no verification |
| 55 | // is done here. |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 56 | status_t err; |
| 57 | uint8_t const * src_bits = NULL; |
| 58 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits); |
| 59 | LOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 60 | |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 61 | uint8_t* dst_bits = NULL; |
| 62 | err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits); |
| 63 | LOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
| 64 | |
| 65 | Region::const_iterator head(reg.begin()); |
| 66 | Region::const_iterator tail(reg.end()); |
| 67 | if (head != tail && src_bits && dst_bits) { |
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); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 155 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 156 | if (err < 0) return err; |
| 157 | return client->setLayer(mToken, layer); |
| 158 | } |
| 159 | status_t SurfaceControl::setPosition(int32_t x, int32_t y) { |
| 160 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 161 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 162 | if (err < 0) return err; |
| 163 | return client->setPosition(mToken, x, y); |
| 164 | } |
| 165 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { |
| 166 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 167 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 168 | if (err < 0) return err; |
| 169 | return client->setSize(mToken, w, h); |
| 170 | } |
| 171 | status_t SurfaceControl::hide() { |
| 172 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 173 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 174 | if (err < 0) return err; |
| 175 | return client->hide(mToken); |
| 176 | } |
| 177 | status_t SurfaceControl::show(int32_t layer) { |
| 178 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 179 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 180 | if (err < 0) return err; |
| 181 | return client->show(mToken, layer); |
| 182 | } |
| 183 | status_t SurfaceControl::freeze() { |
| 184 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 185 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 186 | if (err < 0) return err; |
| 187 | return client->freeze(mToken); |
| 188 | } |
| 189 | status_t SurfaceControl::unfreeze() { |
| 190 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 191 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 192 | if (err < 0) return err; |
| 193 | return client->unfreeze(mToken); |
| 194 | } |
| 195 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { |
| 196 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 197 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 198 | if (err < 0) return err; |
| 199 | return client->setFlags(mToken, flags, mask); |
| 200 | } |
| 201 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { |
| 202 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 203 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 204 | if (err < 0) return err; |
| 205 | return client->setTransparentRegionHint(mToken, transparent); |
| 206 | } |
| 207 | status_t SurfaceControl::setAlpha(float alpha) { |
| 208 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 209 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 210 | if (err < 0) return err; |
| 211 | return client->setAlpha(mToken, alpha); |
| 212 | } |
| 213 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { |
| 214 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 215 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 216 | if (err < 0) return err; |
| 217 | return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy); |
| 218 | } |
| 219 | status_t SurfaceControl::setFreezeTint(uint32_t tint) { |
| 220 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 221 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 222 | if (err < 0) return err; |
| 223 | return client->setFreezeTint(mToken, tint); |
| 224 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 225 | |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 226 | status_t SurfaceControl::validate() const |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 227 | { |
| 228 | if (mToken<0 || mClient==0) { |
| 229 | LOGE("invalid token (%d, identity=%u) or client (%p)", |
| 230 | mToken, mIdentity, mClient.get()); |
| 231 | return NO_INIT; |
| 232 | } |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 233 | SharedClient const* cblk = mClient->mControl; |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 234 | if (cblk == 0) { |
| 235 | LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity); |
| 236 | return NO_INIT; |
| 237 | } |
| 238 | status_t err = cblk->validate(mToken); |
| 239 | if (err != NO_ERROR) { |
| 240 | LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)", |
| 241 | mToken, mIdentity, err, strerror(-err)); |
| 242 | return err; |
| 243 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 244 | uint32_t identity = cblk->getIdentity(mToken); |
| 245 | if (mIdentity != identity) { |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 246 | LOGE("using an invalid surface id=%d, identity=%u should be %d", |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 247 | mToken, mIdentity, identity); |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 248 | return NO_INIT; |
| 249 | } |
| 250 | return NO_ERROR; |
| 251 | } |
| 252 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 253 | status_t SurfaceControl::writeSurfaceToParcel( |
| 254 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 255 | { |
| 256 | uint32_t flags = 0; |
| 257 | uint32_t format = 0; |
| 258 | SurfaceID token = -1; |
| 259 | uint32_t identity = 0; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 260 | uint32_t width = 0; |
| 261 | uint32_t height = 0; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 262 | sp<SurfaceComposerClient> client; |
| 263 | sp<ISurface> sur; |
| 264 | if (SurfaceControl::isValid(control)) { |
| 265 | token = control->mToken; |
| 266 | identity = control->mIdentity; |
| 267 | client = control->mClient; |
| 268 | sur = control->mSurface; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 269 | width = control->mWidth; |
| 270 | height = control->mHeight; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 271 | format = control->mFormat; |
| 272 | flags = control->mFlags; |
| 273 | } |
| 274 | parcel->writeStrongBinder(client!=0 ? client->connection() : NULL); |
| 275 | parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL); |
| 276 | parcel->writeInt32(token); |
| 277 | parcel->writeInt32(identity); |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 278 | parcel->writeInt32(width); |
| 279 | parcel->writeInt32(height); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 280 | parcel->writeInt32(format); |
| 281 | parcel->writeInt32(flags); |
| 282 | return NO_ERROR; |
| 283 | } |
| 284 | |
| 285 | sp<Surface> SurfaceControl::getSurface() const |
| 286 | { |
| 287 | Mutex::Autolock _l(mLock); |
| 288 | if (mSurfaceData == 0) { |
| 289 | mSurfaceData = new Surface(const_cast<SurfaceControl*>(this)); |
| 290 | } |
| 291 | return mSurfaceData; |
| 292 | } |
| 293 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 294 | // ============================================================================ |
| 295 | // Surface |
| 296 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 298 | Surface::Surface(const sp<SurfaceControl>& surface) |
| 299 | : mClient(surface->mClient), mSurface(surface->mSurface), |
| 300 | mToken(surface->mToken), mIdentity(surface->mIdentity), |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 301 | mFormat(surface->mFormat), mFlags(surface->mFlags), |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 302 | mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL), |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 303 | mWidth(surface->mWidth), mHeight(surface->mHeight) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 305 | mSharedBufferClient = new SharedBufferClient( |
Mathias Agopian | 4961c95 | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 306 | mClient->mControl, mToken, 2, mIdentity); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 307 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 308 | init(); |
| 309 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 310 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 311 | Surface::Surface(const Parcel& parcel) |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 312 | : mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL) |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 313 | { |
| 314 | sp<IBinder> clientBinder = parcel.readStrongBinder(); |
| 315 | mSurface = interface_cast<ISurface>(parcel.readStrongBinder()); |
| 316 | mToken = parcel.readInt32(); |
| 317 | mIdentity = parcel.readInt32(); |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 318 | mWidth = parcel.readInt32(); |
| 319 | mHeight = parcel.readInt32(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 320 | mFormat = parcel.readInt32(); |
| 321 | mFlags = parcel.readInt32(); |
| 322 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 323 | // FIXME: what does that mean if clientBinder is NULL here? |
| 324 | if (clientBinder != NULL) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 325 | mClient = SurfaceComposerClient::clientForConnection(clientBinder); |
| 326 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 327 | mSharedBufferClient = new SharedBufferClient( |
Mathias Agopian | 4961c95 | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 328 | mClient->mControl, mToken, 2, mIdentity); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 331 | init(); |
| 332 | } |
| 333 | |
| 334 | void Surface::init() |
| 335 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 336 | android_native_window_t::setSwapInterval = setSwapInterval; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 337 | android_native_window_t::dequeueBuffer = dequeueBuffer; |
| 338 | android_native_window_t::lockBuffer = lockBuffer; |
| 339 | android_native_window_t::queueBuffer = queueBuffer; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 340 | android_native_window_t::query = query; |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 341 | android_native_window_t::perform = perform; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | mSwapRectangle.makeInvalid(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 343 | DisplayInfo dinfo; |
| 344 | SurfaceComposerClient::getDisplayInfo(0, &dinfo); |
| 345 | const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi; |
| 346 | const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi; |
| 347 | // FIXME: set real values here |
| 348 | const_cast<int&>(android_native_window_t::minSwapInterval) = 1; |
| 349 | const_cast<int&>(android_native_window_t::maxSwapInterval) = 1; |
| 350 | const_cast<uint32_t&>(android_native_window_t::flags) = 0; |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 351 | // be default we request a hardware surface |
| 352 | mUsage = GRALLOC_USAGE_HW_RENDER; |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 353 | mConnected = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | } |
| 355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | Surface::~Surface() |
| 357 | { |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 358 | // this is a client-side operation, the surface is destroyed, unmap |
| 359 | // its buffers in this process. |
| 360 | for (int i=0 ; i<2 ; i++) { |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 361 | if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) { |
Mathias Agopian | e633f93 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 362 | getBufferMapper().unregisterBuffer(mBuffers[i]->handle); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 363 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 365 | |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 366 | // clear all references and trigger an IPC now, to make sure things |
| 367 | // happen without delay, since these resources are quite heavy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | mClient.clear(); |
| 369 | mSurface.clear(); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 370 | delete mSharedBufferClient; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | IPCThreadState::self()->flushCommands(); |
| 372 | } |
| 373 | |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 374 | sp<SurfaceComposerClient> Surface::getClient() const { |
| 375 | return mClient; |
| 376 | } |
| 377 | |
| 378 | sp<ISurface> Surface::getISurface() const { |
| 379 | return mSurface; |
| 380 | } |
| 381 | |
| 382 | bool Surface::isValid() { |
| 383 | return mToken>=0 && mClient!=0; |
| 384 | } |
| 385 | |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 386 | status_t Surface::validate() const |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 387 | { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 388 | sp<SurfaceComposerClient> client(getClient()); |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 389 | if (mToken<0 || mClient==0) { |
| 390 | LOGE("invalid token (%d, identity=%u) or client (%p)", |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 391 | mToken, mIdentity, client.get()); |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 392 | return NO_INIT; |
| 393 | } |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 394 | SharedClient const* cblk = mClient->mControl; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 395 | if (cblk == 0) { |
| 396 | LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity); |
| 397 | return NO_INIT; |
| 398 | } |
| 399 | status_t err = cblk->validate(mToken); |
| 400 | if (err != NO_ERROR) { |
| 401 | LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)", |
| 402 | mToken, mIdentity, err, strerror(-err)); |
| 403 | return err; |
| 404 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 405 | uint32_t identity = cblk->getIdentity(mToken); |
| 406 | if (mIdentity != identity) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 407 | LOGE("using an invalid surface id=%d, identity=%u should be %d", |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 408 | mToken, mIdentity, identity); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 409 | return NO_INIT; |
| 410 | } |
| 411 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 414 | |
| 415 | bool Surface::isSameSurface( |
| 416 | const sp<Surface>& lhs, const sp<Surface>& rhs) |
| 417 | { |
| 418 | if (lhs == 0 || rhs == 0) |
| 419 | return false; |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 420 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 421 | return lhs->mSurface->asBinder() == rhs->mSurface->asBinder(); |
| 422 | } |
| 423 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 424 | // ---------------------------------------------------------------------------- |
| 425 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 426 | int Surface::setSwapInterval(android_native_window_t* window, int interval) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int Surface::dequeueBuffer(android_native_window_t* window, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 431 | android_native_buffer_t** buffer) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 432 | Surface* self = getSelf(window); |
| 433 | return self->dequeueBuffer(buffer); |
| 434 | } |
| 435 | |
| 436 | int Surface::lockBuffer(android_native_window_t* window, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 437 | android_native_buffer_t* buffer) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 438 | Surface* self = getSelf(window); |
| 439 | return self->lockBuffer(buffer); |
| 440 | } |
| 441 | |
| 442 | int Surface::queueBuffer(android_native_window_t* window, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 443 | android_native_buffer_t* buffer) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 444 | Surface* self = getSelf(window); |
| 445 | return self->queueBuffer(buffer); |
| 446 | } |
| 447 | |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 448 | int Surface::query(android_native_window_t* window, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 449 | int what, int* value) { |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 450 | Surface* self = getSelf(window); |
| 451 | return self->query(what, value); |
| 452 | } |
| 453 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 454 | int Surface::perform(android_native_window_t* window, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 455 | int operation, ...) { |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 456 | va_list args; |
| 457 | va_start(args, operation); |
| 458 | Surface* self = getSelf(window); |
| 459 | int res = self->perform(operation, args); |
| 460 | va_end(args); |
| 461 | return res; |
| 462 | } |
| 463 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 464 | // ---------------------------------------------------------------------------- |
| 465 | |
| 466 | int Surface::dequeueBuffer(android_native_buffer_t** buffer) |
| 467 | { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 468 | sp<SurfaceComposerClient> client(getClient()); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 469 | status_t err = validate(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 470 | if (err != NO_ERROR) |
| 471 | return err; |
| 472 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 473 | ssize_t bufIdx = mSharedBufferClient->dequeue(); |
| 474 | if (bufIdx < 0) { |
| 475 | LOGE("error dequeuing a buffer (%s)", strerror(bufIdx)); |
| 476 | return bufIdx; |
Mathias Agopian | 9e2be20 | 2009-08-21 15:44:17 -0700 | [diff] [blame] | 477 | } |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 478 | |
| 479 | // below we make sure we AT LEAST have the usage flags we want |
| 480 | const uint32_t usage(getUsage()); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 481 | const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]); |
Mathias Agopian | db3647f | 2010-04-08 18:34:07 -0700 | [diff] [blame] | 482 | |
| 483 | // Always call needNewBuffer(), since it clears the needed buffers flags |
| 484 | bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx); |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 485 | if (backBuffer == 0 || |
| 486 | ((uint32_t(backBuffer->usage) & usage) != usage) || |
Mathias Agopian | db3647f | 2010-04-08 18:34:07 -0700 | [diff] [blame] | 487 | needNewBuffer) |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 488 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 489 | err = getBufferLocked(bufIdx, usage); |
| 490 | LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)", |
| 491 | bufIdx, usage, strerror(-err)); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 492 | if (err == NO_ERROR) { |
| 493 | // reset the width/height with the what we get from the buffer |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 494 | mWidth = uint32_t(backBuffer->width); |
| 495 | mHeight = uint32_t(backBuffer->height); |
| 496 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 499 | // if we still don't have a buffer here, we probably ran out of memory |
| 500 | if (!err && backBuffer==0) { |
| 501 | err = NO_MEMORY; |
| 502 | } |
| 503 | |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 504 | if (err == NO_ERROR) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 505 | mDirtyRegion.set(backBuffer->width, backBuffer->height); |
| 506 | *buffer = backBuffer.get(); |
| 507 | } else { |
| 508 | mSharedBufferClient->undoDequeue(bufIdx); |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 509 | } |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 510 | |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 511 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | int Surface::lockBuffer(android_native_buffer_t* buffer) |
| 515 | { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 516 | sp<SurfaceComposerClient> client(getClient()); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 517 | status_t err = validate(); |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 518 | if (err != NO_ERROR) |
| 519 | return err; |
| 520 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 521 | int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer)); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 522 | err = mSharedBufferClient->lock(bufIdx); |
| 523 | LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err)); |
| 524 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int Surface::queueBuffer(android_native_buffer_t* buffer) |
| 528 | { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 529 | sp<SurfaceComposerClient> client(getClient()); |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 530 | status_t err = validate(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 531 | if (err != NO_ERROR) |
| 532 | return err; |
| 533 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 534 | if (mSwapRectangle.isValid()) { |
| 535 | mDirtyRegion.set(mSwapRectangle); |
| 536 | } |
| 537 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 538 | int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer)); |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 539 | mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 540 | mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion); |
| 541 | err = mSharedBufferClient->queue(bufIdx); |
| 542 | LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 543 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 544 | if (err == NO_ERROR) { |
| 545 | // 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] | 546 | client->signalServer(); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 547 | } |
| 548 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 551 | int Surface::query(int what, int* value) |
| 552 | { |
| 553 | switch (what) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 554 | case NATIVE_WINDOW_WIDTH: |
| 555 | *value = int(mWidth); |
| 556 | return NO_ERROR; |
| 557 | case NATIVE_WINDOW_HEIGHT: |
| 558 | *value = int(mHeight); |
| 559 | return NO_ERROR; |
| 560 | case NATIVE_WINDOW_FORMAT: |
| 561 | *value = int(mFormat); |
| 562 | return NO_ERROR; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 563 | } |
| 564 | return BAD_VALUE; |
| 565 | } |
| 566 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 567 | int Surface::perform(int operation, va_list args) |
| 568 | { |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 569 | status_t err = validate(); |
| 570 | if (err != NO_ERROR) |
| 571 | return err; |
| 572 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 573 | int res = NO_ERROR; |
| 574 | switch (operation) { |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 575 | case NATIVE_WINDOW_SET_USAGE: |
| 576 | dispatch_setUsage( args ); |
| 577 | break; |
| 578 | case NATIVE_WINDOW_CONNECT: |
| 579 | res = dispatch_connect( args ); |
| 580 | break; |
| 581 | case NATIVE_WINDOW_DISCONNECT: |
| 582 | res = dispatch_disconnect( args ); |
| 583 | break; |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 584 | case NATIVE_WINDOW_SET_CROP: |
| 585 | res = dispatch_crop( args ); |
| 586 | break; |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 587 | default: |
| 588 | res = NAME_NOT_FOUND; |
| 589 | break; |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 590 | } |
| 591 | return res; |
| 592 | } |
| 593 | |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 594 | void Surface::dispatch_setUsage(va_list args) { |
| 595 | int usage = va_arg(args, int); |
| 596 | setUsage( usage ); |
| 597 | } |
| 598 | int Surface::dispatch_connect(va_list args) { |
| 599 | int api = va_arg(args, int); |
| 600 | return connect( api ); |
| 601 | } |
| 602 | int Surface::dispatch_disconnect(va_list args) { |
| 603 | int api = va_arg(args, int); |
| 604 | return disconnect( api ); |
| 605 | } |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 606 | int Surface::dispatch_crop(va_list args) { |
| 607 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 608 | return crop( reinterpret_cast<Rect const*>(rect) ); |
| 609 | } |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 610 | |
| 611 | |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 612 | void Surface::setUsage(uint32_t reqUsage) |
| 613 | { |
| 614 | Mutex::Autolock _l(mSurfaceLock); |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 615 | mUsage = reqUsage; |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 618 | int Surface::connect(int api) |
| 619 | { |
| 620 | Mutex::Autolock _l(mSurfaceLock); |
| 621 | int err = NO_ERROR; |
| 622 | switch (api) { |
| 623 | case NATIVE_WINDOW_API_EGL: |
| 624 | if (mConnected) { |
| 625 | err = -EINVAL; |
| 626 | } else { |
| 627 | mConnected = api; |
| 628 | } |
| 629 | break; |
| 630 | default: |
| 631 | err = -EINVAL; |
| 632 | break; |
| 633 | } |
| 634 | return err; |
| 635 | } |
| 636 | |
| 637 | int Surface::disconnect(int api) |
| 638 | { |
| 639 | Mutex::Autolock _l(mSurfaceLock); |
| 640 | int err = NO_ERROR; |
| 641 | switch (api) { |
| 642 | case NATIVE_WINDOW_API_EGL: |
| 643 | if (mConnected == api) { |
| 644 | mConnected = 0; |
| 645 | } else { |
| 646 | err = -EINVAL; |
| 647 | } |
| 648 | break; |
| 649 | default: |
| 650 | err = -EINVAL; |
| 651 | break; |
| 652 | } |
| 653 | return err; |
| 654 | } |
| 655 | |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 656 | uint32_t Surface::getUsage() const |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 657 | { |
| 658 | Mutex::Autolock _l(mSurfaceLock); |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 659 | return mUsage; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 662 | int Surface::getConnectedApi() const |
| 663 | { |
| 664 | Mutex::Autolock _l(mSurfaceLock); |
| 665 | return mConnected; |
| 666 | } |
| 667 | |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 668 | int Surface::crop(Rect const* rect) |
| 669 | { |
| 670 | Mutex::Autolock _l(mSurfaceLock); |
| 671 | // TODO: validate rect size |
| 672 | mNextBufferCrop = *rect; |
| 673 | return NO_ERROR; |
| 674 | } |
| 675 | |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame^] | 676 | int Surface::setBufferCount(int bufferCount) |
| 677 | { |
| 678 | sp<ISurface> s(mSurface); |
| 679 | if (s == 0) return NO_INIT; |
| 680 | |
| 681 | // FIXME: this needs to be synchronized dequeue/queue |
| 682 | |
| 683 | status_t err = s->setBufferCount(bufferCount); |
| 684 | LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s", |
| 685 | bufferCount, strerror(-err)); |
| 686 | if (err == NO_ERROR) { |
| 687 | err = mSharedBufferClient->getStatus(); |
| 688 | LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err); |
| 689 | if (!err) { |
| 690 | // update our local copy of the buffer count |
| 691 | mSharedBufferClient->setBufferCount(bufferCount); |
| 692 | } |
| 693 | } |
| 694 | return err; |
| 695 | } |
| 696 | |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 697 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 698 | // ---------------------------------------------------------------------------- |
| 699 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | status_t Surface::lock(SurfaceInfo* info, bool blocking) { |
| 701 | return Surface::lock(info, NULL, blocking); |
| 702 | } |
| 703 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 704 | status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 705 | { |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 706 | if (getConnectedApi()) { |
| 707 | LOGE("Surface::lock(%p) failed. Already connected to another API", |
| 708 | (android_native_window_t*)this); |
| 709 | CallStack stack; |
| 710 | stack.update(); |
| 711 | stack.dump(""); |
| 712 | return INVALID_OPERATION; |
| 713 | } |
| 714 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 715 | if (mApiLock.tryLock() != NO_ERROR) { |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 716 | LOGE("calling Surface::lock from different threads!"); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 717 | CallStack stack; |
| 718 | stack.update(); |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 719 | stack.dump(""); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 720 | return WOULD_BLOCK; |
| 721 | } |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 722 | |
| 723 | /* Here we're holding mApiLock */ |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 724 | |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 725 | if (mLockedBuffer != 0) { |
| 726 | LOGE("Surface::lock failed, already locked"); |
| 727 | mApiLock.unlock(); |
| 728 | return INVALID_OPERATION; |
| 729 | } |
| 730 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 731 | // we're intending to do software rendering from this point |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 732 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 733 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 734 | android_native_buffer_t* out; |
| 735 | status_t err = dequeueBuffer(&out); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 736 | LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 737 | if (err == NO_ERROR) { |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 738 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 739 | err = lockBuffer(backBuffer.get()); |
| 740 | LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)", |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 741 | getBufferIndex(backBuffer), strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 742 | if (err == NO_ERROR) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 743 | const Rect bounds(backBuffer->width, backBuffer->height); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 744 | const Region boundsRegion(bounds); |
| 745 | Region scratch(boundsRegion); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 746 | Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 747 | newDirtyRegion &= boundsRegion; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 748 | |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 749 | // figure out if we can copy the frontbuffer back |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 750 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 751 | const bool canCopyBack = (frontBuffer != 0 && |
| 752 | backBuffer->width == frontBuffer->width && |
| 753 | backBuffer->height == frontBuffer->height && |
| 754 | backBuffer->format == frontBuffer->format && |
| 755 | !(mFlags & ISurfaceComposer::eDestroyBackbuffer)); |
| 756 | |
| 757 | // the dirty region we report to surfaceflinger is the one |
| 758 | // given by the user (as opposed to the one *we* return to the |
| 759 | // user). |
| 760 | mDirtyRegion = newDirtyRegion; |
| 761 | |
| 762 | if (canCopyBack) { |
| 763 | // copy the area that is invalid and not repainted this round |
| 764 | const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion)); |
| 765 | if (!copyback.isEmpty()) |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 766 | copyBlt(backBuffer, frontBuffer, copyback); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 767 | } else { |
| 768 | // if we can't copy-back anything, modify the user's dirty |
| 769 | // region to make sure they redraw the whole buffer |
| 770 | newDirtyRegion = boundsRegion; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 771 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 772 | |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 773 | // keep track of the are of the buffer that is "clean" |
| 774 | // (ie: that will be redrawn) |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 775 | mOldDirtyRegion = newDirtyRegion; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 776 | |
Mathias Agopian | 430f2ed | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 777 | void* vaddr; |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 778 | status_t res = backBuffer->lock( |
| 779 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Mathias Agopian | 430f2ed | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 780 | newDirtyRegion.bounds(), &vaddr); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 781 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 782 | LOGW_IF(res, "failed locking buffer (handle = %p)", |
| 783 | backBuffer->handle); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 784 | |
| 785 | mLockedBuffer = backBuffer; |
| 786 | other->w = backBuffer->width; |
| 787 | other->h = backBuffer->height; |
| 788 | other->s = backBuffer->stride; |
| 789 | other->usage = backBuffer->usage; |
| 790 | other->format = backBuffer->format; |
Mathias Agopian | 430f2ed | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 791 | other->bits = vaddr; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 794 | mApiLock.unlock(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 795 | return err; |
| 796 | } |
| 797 | |
| 798 | status_t Surface::unlockAndPost() |
| 799 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 800 | if (mLockedBuffer == 0) { |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 801 | LOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 802 | return INVALID_OPERATION; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 803 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 804 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 805 | status_t err = mLockedBuffer->unlock(); |
| 806 | LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 807 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 808 | err = queueBuffer(mLockedBuffer.get()); |
| 809 | LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)", |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 810 | getBufferIndex(mLockedBuffer), strerror(-err)); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 811 | |
| 812 | mPostedBuffer = mLockedBuffer; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 813 | mLockedBuffer = 0; |
| 814 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | } |
| 816 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | void Surface::setSwapRectangle(const Rect& r) { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 818 | Mutex::Autolock _l(mSurfaceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | mSwapRectangle = r; |
| 820 | } |
| 821 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 822 | int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const |
| 823 | { |
| 824 | return buffer->getIndex(); |
| 825 | } |
| 826 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 827 | status_t Surface::getBufferLocked(int index, int usage) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 829 | sp<ISurface> s(mSurface); |
| 830 | if (s == 0) return NO_INIT; |
| 831 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 832 | status_t err = NO_MEMORY; |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 833 | |
| 834 | // free the current buffer |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 835 | sp<GraphicBuffer>& currentBuffer(mBuffers[index]); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 836 | if (currentBuffer != 0) { |
| 837 | getBufferMapper().unregisterBuffer(currentBuffer->handle); |
| 838 | currentBuffer.clear(); |
| 839 | } |
| 840 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 841 | sp<GraphicBuffer> buffer = s->requestBuffer(index, usage); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 842 | LOGE_IF(buffer==0, |
| 843 | "ISurface::getBuffer(%d, %08x) returned NULL", |
| 844 | index, usage); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 845 | if (buffer != 0) { // this should never happen by construction |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 846 | LOGE_IF(buffer->handle == NULL, |
Mathias Agopian | 0c4cec7 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 847 | "Surface (identity=%d) requestBuffer(%d, %08x) returned" |
| 848 | "a buffer with a null handle", mIdentity, index, usage); |
| 849 | err = mSharedBufferClient->getStatus(); |
| 850 | LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err); |
| 851 | if (!err && buffer->handle != NULL) { |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 852 | err = getBufferMapper().registerBuffer(buffer->handle); |
| 853 | LOGW_IF(err, "registerBuffer(...) failed %d (%s)", |
| 854 | err, strerror(-err)); |
| 855 | if (err == NO_ERROR) { |
| 856 | currentBuffer = buffer; |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 857 | currentBuffer->setIndex(index); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 858 | } |
Mathias Agopian | 0da41a3 | 2009-10-06 15:58:44 -0700 | [diff] [blame] | 859 | } else { |
| 860 | err = err<0 ? err : NO_MEMORY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | } |
| 862 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 863 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | } |
| 865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | }; // namespace android |
| 867 | |