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 | 9779b221 | 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> |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 35 | #include <ui/GraphicLog.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <ui/Rect.h> |
| 37 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 38 | #include <surfaceflinger/Surface.h> |
| 39 | #include <surfaceflinger/ISurface.h> |
| 40 | #include <surfaceflinger/ISurfaceComposer.h> |
| 41 | #include <surfaceflinger/SurfaceComposerClient.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 42 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 43 | #include <private/surfaceflinger/SharedBufferStack.h> |
| 44 | #include <private/surfaceflinger/LayerState.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | namespace android { |
| 47 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 48 | // ---------------------------------------------------------------------- |
| 49 | |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 50 | static status_t copyBlt( |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 51 | const sp<GraphicBuffer>& dst, |
| 52 | const sp<GraphicBuffer>& src, |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 53 | const Region& reg) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 54 | { |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 55 | // src and dst with, height and format must be identical. no verification |
| 56 | // is done here. |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 57 | status_t err; |
| 58 | uint8_t const * src_bits = NULL; |
| 59 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits); |
| 60 | LOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 61 | |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 62 | uint8_t* dst_bits = NULL; |
| 63 | err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits); |
| 64 | LOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
| 65 | |
| 66 | Region::const_iterator head(reg.begin()); |
| 67 | Region::const_iterator tail(reg.end()); |
| 68 | if (head != tail && src_bits && dst_bits) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 69 | const size_t bpp = bytesPerPixel(src->format); |
| 70 | const size_t dbpr = dst->stride * bpp; |
| 71 | const size_t sbpr = src->stride * bpp; |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 72 | |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 73 | while (head != tail) { |
| 74 | const Rect& r(*head++); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 75 | ssize_t h = r.height(); |
| 76 | if (h <= 0) continue; |
| 77 | size_t size = r.width() * bpp; |
| 78 | uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp; |
| 79 | uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp; |
| 80 | if (dbpr==sbpr && size==sbpr) { |
| 81 | size *= h; |
| 82 | h = 1; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 83 | } |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 84 | do { |
| 85 | memcpy(d, s, size); |
| 86 | d += dbpr; |
| 87 | s += sbpr; |
| 88 | } while (--h > 0); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 91 | |
Mathias Agopian | 8d9a5eff | 2009-07-13 18:29:59 -0700 | [diff] [blame] | 92 | if (src_bits) |
| 93 | src->unlock(); |
| 94 | |
| 95 | if (dst_bits) |
| 96 | dst->unlock(); |
| 97 | |
| 98 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 101 | // ============================================================================ |
| 102 | // SurfaceControl |
| 103 | // ============================================================================ |
| 104 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 105 | SurfaceControl::SurfaceControl( |
| 106 | const sp<SurfaceComposerClient>& client, |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 107 | const sp<ISurface>& surface, |
Mathias Agopian | 770492c | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 108 | const ISurfaceComposerClient::surface_data_t& data, |
Mathias Agopian | 69d6209 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 109 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 110 | : mClient(client), mSurface(surface), |
| 111 | mToken(data.token), mIdentity(data.identity), |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 112 | mWidth(data.width), mHeight(data.height), mFormat(data.format), |
| 113 | mFlags(flags) |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 114 | { |
| 115 | } |
Mathias Agopian | 69d6209 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 116 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 117 | SurfaceControl::~SurfaceControl() |
| 118 | { |
| 119 | destroy(); |
| 120 | } |
| 121 | |
| 122 | void SurfaceControl::destroy() |
| 123 | { |
Mathias Agopian | 69d6209 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 124 | if (isValid()) { |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 125 | mClient->destroySurface(mToken); |
| 126 | } |
| 127 | |
| 128 | // clear all references and trigger an IPC now, to make sure things |
| 129 | // happen without delay, since these resources are quite heavy. |
| 130 | mClient.clear(); |
| 131 | mSurface.clear(); |
| 132 | IPCThreadState::self()->flushCommands(); |
| 133 | } |
| 134 | |
| 135 | void SurfaceControl::clear() |
| 136 | { |
| 137 | // here, the window manager tells us explicitly that we should destroy |
| 138 | // the surface's resource. Soon after this call, it will also release |
| 139 | // its last reference (which will call the dtor); however, it is possible |
| 140 | // that a client living in the same process still holds references which |
| 141 | // would delay the call to the dtor -- that is why we need this explicit |
| 142 | // "clear()" call. |
| 143 | destroy(); |
| 144 | } |
| 145 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 146 | bool SurfaceControl::isSameSurface( |
| 147 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) |
| 148 | { |
| 149 | if (lhs == 0 || rhs == 0) |
| 150 | return false; |
| 151 | return lhs->mSurface->asBinder() == rhs->mSurface->asBinder(); |
| 152 | } |
| 153 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 154 | status_t SurfaceControl::setLayer(int32_t layer) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 157 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 158 | return client->setLayer(mToken, layer); |
| 159 | } |
| 160 | status_t SurfaceControl::setPosition(int32_t x, int32_t y) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 163 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 164 | return client->setPosition(mToken, x, y); |
| 165 | } |
| 166 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 169 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 170 | return client->setSize(mToken, w, h); |
| 171 | } |
| 172 | status_t SurfaceControl::hide() { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 175 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 176 | return client->hide(mToken); |
| 177 | } |
| 178 | status_t SurfaceControl::show(int32_t layer) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 181 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 182 | return client->show(mToken, layer); |
| 183 | } |
| 184 | status_t SurfaceControl::freeze() { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 187 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 188 | return client->freeze(mToken); |
| 189 | } |
| 190 | status_t SurfaceControl::unfreeze() { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 193 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 194 | return client->unfreeze(mToken); |
| 195 | } |
| 196 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 199 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 200 | return client->setFlags(mToken, flags, mask); |
| 201 | } |
| 202 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 205 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 206 | return client->setTransparentRegionHint(mToken, transparent); |
| 207 | } |
| 208 | status_t SurfaceControl::setAlpha(float alpha) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 211 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 212 | return client->setAlpha(mToken, alpha); |
| 213 | } |
| 214 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 217 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 218 | return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy); |
| 219 | } |
| 220 | status_t SurfaceControl::setFreezeTint(uint32_t tint) { |
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; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 223 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 224 | return client->setFreezeTint(mToken, tint); |
| 225 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 226 | |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 227 | status_t SurfaceControl::validate() const |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 228 | { |
| 229 | if (mToken<0 || mClient==0) { |
| 230 | LOGE("invalid token (%d, identity=%u) or client (%p)", |
| 231 | mToken, mIdentity, mClient.get()); |
| 232 | return NO_INIT; |
| 233 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 234 | return NO_ERROR; |
| 235 | } |
| 236 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 237 | status_t SurfaceControl::writeSurfaceToParcel( |
| 238 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 239 | { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 240 | sp<ISurface> sur; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 241 | uint32_t identity = 0; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 242 | uint32_t width = 0; |
| 243 | uint32_t height = 0; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 244 | uint32_t format = 0; |
| 245 | uint32_t flags = 0; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 246 | if (SurfaceControl::isValid(control)) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 247 | sur = control->mSurface; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 248 | identity = control->mIdentity; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 249 | width = control->mWidth; |
| 250 | height = control->mHeight; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 251 | format = control->mFormat; |
| 252 | flags = control->mFlags; |
| 253 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 254 | parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 255 | parcel->writeInt32(identity); |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 256 | parcel->writeInt32(width); |
| 257 | parcel->writeInt32(height); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 258 | parcel->writeInt32(format); |
| 259 | parcel->writeInt32(flags); |
| 260 | return NO_ERROR; |
| 261 | } |
| 262 | |
| 263 | sp<Surface> SurfaceControl::getSurface() const |
| 264 | { |
| 265 | Mutex::Autolock _l(mLock); |
| 266 | if (mSurfaceData == 0) { |
| 267 | mSurfaceData = new Surface(const_cast<SurfaceControl*>(this)); |
| 268 | } |
| 269 | return mSurfaceData; |
| 270 | } |
| 271 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 272 | // ============================================================================ |
| 273 | // Surface |
| 274 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 276 | class SurfaceClient : public Singleton<SurfaceClient> |
| 277 | { |
| 278 | // all these attributes are constants |
| 279 | sp<ISurfaceComposer> mComposerService; |
| 280 | sp<ISurfaceComposerClient> mClient; |
| 281 | status_t mStatus; |
| 282 | SharedClient* mControl; |
| 283 | sp<IMemoryHeap> mControlMemory; |
| 284 | |
| 285 | SurfaceClient() |
| 286 | : Singleton<SurfaceClient>(), mStatus(NO_INIT) |
| 287 | { |
| 288 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 289 | mComposerService = sf; |
| 290 | mClient = sf->createClientConnection(); |
| 291 | if (mClient != NULL) { |
| 292 | mControlMemory = mClient->getControlBlock(); |
| 293 | if (mControlMemory != NULL) { |
| 294 | mControl = static_cast<SharedClient *>( |
| 295 | mControlMemory->getBase()); |
| 296 | if (mControl) { |
| 297 | mStatus = NO_ERROR; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | friend class Singleton<SurfaceClient>; |
| 303 | public: |
| 304 | status_t initCheck() const { |
| 305 | return mStatus; |
| 306 | } |
| 307 | SharedClient* getSharedClient() const { |
| 308 | return mControl; |
| 309 | } |
| 310 | ssize_t getTokenForSurface(const sp<ISurface>& sur) const { |
| 311 | // TODO: we could cache a few tokens here to avoid an IPC |
| 312 | return mClient->getTokenForSurface(sur); |
| 313 | } |
| 314 | void signalServer() const { |
| 315 | mComposerService->signal(); |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | ANDROID_SINGLETON_STATIC_INSTANCE(SurfaceClient); |
| 320 | |
| 321 | // --------------------------------------------------------------------------- |
| 322 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 323 | Surface::Surface(const sp<SurfaceControl>& surface) |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 324 | : mBufferMapper(GraphicBufferMapper::get()), |
| 325 | mClient(SurfaceClient::getInstance()), |
| 326 | mSharedBufferClient(NULL), |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 327 | mInitCheck(NO_INIT), |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 328 | mSurface(surface->mSurface), |
| 329 | mIdentity(surface->mIdentity), |
| 330 | mFormat(surface->mFormat), mFlags(surface->mFlags), |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 331 | mWidth(surface->mWidth), mHeight(surface->mHeight) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 333 | init(); |
| 334 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 335 | |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 336 | Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref) |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 337 | : mBufferMapper(GraphicBufferMapper::get()), |
| 338 | mClient(SurfaceClient::getInstance()), |
| 339 | mSharedBufferClient(NULL), |
| 340 | mInitCheck(NO_INIT) |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 341 | { |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 342 | mSurface = interface_cast<ISurface>(ref); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 343 | mIdentity = parcel.readInt32(); |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 344 | mWidth = parcel.readInt32(); |
| 345 | mHeight = parcel.readInt32(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 346 | mFormat = parcel.readInt32(); |
| 347 | mFlags = parcel.readInt32(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 348 | init(); |
| 349 | } |
| 350 | |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 351 | status_t Surface::writeToParcel( |
| 352 | const sp<Surface>& surface, Parcel* parcel) |
| 353 | { |
| 354 | sp<ISurface> sur; |
| 355 | uint32_t identity = 0; |
| 356 | uint32_t width = 0; |
| 357 | uint32_t height = 0; |
| 358 | uint32_t format = 0; |
| 359 | uint32_t flags = 0; |
| 360 | if (Surface::isValid(surface)) { |
| 361 | sur = surface->mSurface; |
| 362 | identity = surface->mIdentity; |
| 363 | width = surface->mWidth; |
| 364 | height = surface->mHeight; |
| 365 | format = surface->mFormat; |
| 366 | flags = surface->mFlags; |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 367 | } else if (surface != 0 && surface->mSurface != 0) { |
| 368 | LOGW("Parceling invalid surface with non-NULL ISurface as NULL: " |
| 369 | "mSurface = %p, mIdentity = %d, mWidth = %d, mHeight = %d, " |
| 370 | "mFormat = %d, mFlags = 0x%08x, mInitCheck = %d", |
| 371 | surface->mSurface.get(), surface->mIdentity, surface->mWidth, |
| 372 | surface->mHeight, surface->mFormat, surface->mFlags, |
| 373 | surface->mInitCheck); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 374 | } |
| 375 | parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL); |
| 376 | parcel->writeInt32(identity); |
| 377 | parcel->writeInt32(width); |
| 378 | parcel->writeInt32(height); |
| 379 | parcel->writeInt32(format); |
| 380 | parcel->writeInt32(flags); |
| 381 | return NO_ERROR; |
| 382 | |
| 383 | } |
| 384 | |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 385 | |
| 386 | Mutex Surface::sCachedSurfacesLock; |
| 387 | DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces(wp<Surface>(0)); |
| 388 | |
| 389 | sp<Surface> Surface::readFromParcel(const Parcel& data) { |
| 390 | Mutex::Autolock _l(sCachedSurfacesLock); |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 391 | sp<IBinder> binder(data.readStrongBinder()); |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 392 | sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote(); |
| 393 | if (surface == 0) { |
| 394 | surface = new Surface(data, binder); |
| 395 | sCachedSurfaces.add(binder, surface); |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 396 | } |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 397 | if (surface->mSurface == 0) { |
| 398 | surface = 0; |
| 399 | } |
| 400 | cleanCachedSurfaces(); |
| 401 | return surface; |
| 402 | } |
| 403 | |
| 404 | // Remove the stale entries from the surface cache. This should only be called |
| 405 | // with sCachedSurfacesLock held. |
| 406 | void Surface::cleanCachedSurfaces() { |
| 407 | for (int i = sCachedSurfaces.size()-1; i >= 0; --i) { |
| 408 | wp<Surface> s(sCachedSurfaces.valueAt(i)); |
| 409 | if (s == 0 || s.promote() == 0) { |
| 410 | sCachedSurfaces.removeItemsAt(i); |
| 411 | } |
| 412 | } |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 415 | void Surface::init() |
| 416 | { |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 417 | ANativeWindow::setSwapInterval = setSwapInterval; |
| 418 | ANativeWindow::dequeueBuffer = dequeueBuffer; |
| 419 | ANativeWindow::lockBuffer = lockBuffer; |
| 420 | ANativeWindow::queueBuffer = queueBuffer; |
| 421 | ANativeWindow::query = query; |
| 422 | ANativeWindow::perform = perform; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 423 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 424 | DisplayInfo dinfo; |
| 425 | SurfaceComposerClient::getDisplayInfo(0, &dinfo); |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 426 | const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi; |
| 427 | const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 428 | // FIXME: set real values here |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 429 | const_cast<int&>(ANativeWindow::minSwapInterval) = 1; |
| 430 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; |
| 431 | const_cast<uint32_t&>(ANativeWindow::flags) = 0; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 432 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 433 | mNextBufferTransform = 0; |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 434 | mConnected = 0; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 435 | mSwapRectangle.makeInvalid(); |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 436 | mNextBufferCrop = Rect(0,0); |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 437 | // two buffers by default |
| 438 | mBuffers.setCapacity(2); |
| 439 | mBuffers.insertAt(0, 2); |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 440 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 441 | if (mSurface != 0 && mClient.initCheck() == NO_ERROR) { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 442 | int32_t token = mClient.getTokenForSurface(mSurface); |
| 443 | if (token >= 0) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 444 | mSharedBufferClient = new SharedBufferClient( |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 445 | mClient.getSharedClient(), token, 2, mIdentity); |
| 446 | mInitCheck = mClient.getSharedClient()->validate(token); |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 447 | } else { |
| 448 | LOGW("Not initializing the shared buffer client because token = %d", |
| 449 | token); |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 450 | } |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
| 453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | Surface::~Surface() |
| 455 | { |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 456 | // this is a client-side operation, the surface is destroyed, unmap |
| 457 | // its buffers in this process. |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 458 | size_t size = mBuffers.size(); |
| 459 | for (size_t i=0 ; i<size ; i++) { |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 460 | if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) { |
Mathias Agopian | e633f93 | 2009-05-05 00:59:23 -0700 | [diff] [blame] | 461 | getBufferMapper().unregisterBuffer(mBuffers[i]->handle); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 464 | |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 465 | // clear all references and trigger an IPC now, to make sure things |
| 466 | // happen without delay, since these resources are quite heavy. |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 467 | mBuffers.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | mSurface.clear(); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 469 | delete mSharedBufferClient; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | IPCThreadState::self()->flushCommands(); |
| 471 | } |
| 472 | |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 473 | bool Surface::isValid() { |
| 474 | return mInitCheck == NO_ERROR; |
| 475 | } |
| 476 | |
| 477 | status_t Surface::validate() const |
| 478 | { |
| 479 | // check that we initialized ourself properly |
| 480 | if (mInitCheck != NO_ERROR) { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 481 | LOGE("invalid token (identity=%u)", mIdentity); |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 482 | return mInitCheck; |
| 483 | } |
| 484 | |
| 485 | // verify the identity of this surface |
Mathias Agopian | 770492c | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 486 | uint32_t identity = mSharedBufferClient->getIdentity(); |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 487 | |
| 488 | // this is a bit of a (temporary) special case, identity==0 means that |
| 489 | // no operation are allowed from the client (eg: dequeue/queue), this |
| 490 | // is used with PUSH_BUFFER surfaces for instance |
| 491 | if (identity == 0) { |
| 492 | LOGE("[Surface] invalid operation (identity=%u)", mIdentity); |
| 493 | return INVALID_OPERATION; |
| 494 | } |
| 495 | |
| 496 | if (mIdentity != identity) { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 497 | LOGE("[Surface] using an invalid surface, " |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 498 | "identity=%u should be %d", |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 499 | mIdentity, identity); |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 500 | return NO_INIT; |
| 501 | } |
| 502 | |
| 503 | // check the surface didn't become invalid |
Mathias Agopian | 770492c | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 504 | status_t err = mSharedBufferClient->getStatus(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 505 | if (err != NO_ERROR) { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 506 | LOGE("surface (identity=%u) is invalid, err=%d (%s)", |
| 507 | mIdentity, err, strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 508 | return err; |
| 509 | } |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 510 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 511 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 514 | sp<ISurface> Surface::getISurface() const { |
| 515 | return mSurface; |
| 516 | } |
| 517 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 518 | // ---------------------------------------------------------------------------- |
| 519 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 520 | int Surface::setSwapInterval(ANativeWindow* window, int interval) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 524 | int Surface::dequeueBuffer(ANativeWindow* window, |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 525 | android_native_buffer_t** buffer) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 526 | Surface* self = getSelf(window); |
| 527 | return self->dequeueBuffer(buffer); |
| 528 | } |
| 529 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 530 | int Surface::lockBuffer(ANativeWindow* window, |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 531 | android_native_buffer_t* buffer) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 532 | Surface* self = getSelf(window); |
| 533 | return self->lockBuffer(buffer); |
| 534 | } |
| 535 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 536 | int Surface::queueBuffer(ANativeWindow* window, |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 537 | android_native_buffer_t* buffer) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 538 | Surface* self = getSelf(window); |
| 539 | return self->queueBuffer(buffer); |
| 540 | } |
| 541 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 542 | int Surface::query(ANativeWindow* window, |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 543 | int what, int* value) { |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 544 | Surface* self = getSelf(window); |
| 545 | return self->query(what, value); |
| 546 | } |
| 547 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 548 | int Surface::perform(ANativeWindow* window, |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 549 | int operation, ...) { |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 550 | va_list args; |
| 551 | va_start(args, operation); |
| 552 | Surface* self = getSelf(window); |
| 553 | int res = self->perform(operation, args); |
| 554 | va_end(args); |
| 555 | return res; |
| 556 | } |
| 557 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 558 | // ---------------------------------------------------------------------------- |
| 559 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 560 | bool Surface::needNewBuffer(int bufIdx, |
| 561 | uint32_t *pWidth, uint32_t *pHeight, |
| 562 | uint32_t *pFormat, uint32_t *pUsage) const |
| 563 | { |
| 564 | Mutex::Autolock _l(mSurfaceLock); |
| 565 | |
| 566 | // Always call needNewBuffer(), since it clears the needed buffers flags |
| 567 | bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx); |
| 568 | bool validBuffer = mBufferInfo.validateBuffer(mBuffers[bufIdx]); |
| 569 | bool newNeewBuffer = needNewBuffer || !validBuffer; |
| 570 | if (newNeewBuffer) { |
| 571 | mBufferInfo.get(pWidth, pHeight, pFormat, pUsage); |
| 572 | } |
| 573 | return newNeewBuffer; |
| 574 | } |
| 575 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 576 | int Surface::dequeueBuffer(android_native_buffer_t** buffer) |
| 577 | { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 578 | status_t err = validate(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 579 | if (err != NO_ERROR) |
| 580 | return err; |
| 581 | |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 582 | GraphicLog& logger(GraphicLog::getInstance()); |
| 583 | logger.log(GraphicLog::SF_APP_DEQUEUE_BEFORE, mIdentity, -1); |
| 584 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 585 | ssize_t bufIdx = mSharedBufferClient->dequeue(); |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 586 | |
| 587 | logger.log(GraphicLog::SF_APP_DEQUEUE_AFTER, mIdentity, bufIdx); |
| 588 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 589 | if (bufIdx < 0) { |
| 590 | LOGE("error dequeuing a buffer (%s)", strerror(bufIdx)); |
| 591 | return bufIdx; |
Mathias Agopian | 9e2be20 | 2009-08-21 15:44:17 -0700 | [diff] [blame] | 592 | } |
Mathias Agopian | e611a6e | 2009-09-15 19:10:47 -0700 | [diff] [blame] | 593 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 594 | // grow the buffer array if needed |
| 595 | const size_t size = mBuffers.size(); |
| 596 | const size_t needed = bufIdx+1; |
| 597 | if (size < needed) { |
| 598 | mBuffers.insertAt(size, needed-size); |
| 599 | } |
Mathias Agopian | db3647f | 2010-04-08 18:34:07 -0700 | [diff] [blame] | 600 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 601 | uint32_t w, h, format, usage; |
| 602 | if (needNewBuffer(bufIdx, &w, &h, &format, &usage)) { |
| 603 | err = getBufferLocked(bufIdx, w, h, format, usage); |
| 604 | LOGE_IF(err, "getBufferLocked(%ld, %u, %u, %u, %08x) failed (%s)", |
| 605 | bufIdx, w, h, format, usage, strerror(-err)); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 606 | if (err == NO_ERROR) { |
| 607 | // reset the width/height with the what we get from the buffer |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 608 | const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 609 | mWidth = uint32_t(backBuffer->width); |
| 610 | mHeight = uint32_t(backBuffer->height); |
| 611 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 614 | // if we still don't have a buffer here, we probably ran out of memory |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 615 | const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 616 | if (!err && backBuffer==0) { |
| 617 | err = NO_MEMORY; |
| 618 | } |
| 619 | |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 620 | if (err == NO_ERROR) { |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 621 | mDirtyRegion.set(backBuffer->width, backBuffer->height); |
| 622 | *buffer = backBuffer.get(); |
| 623 | } else { |
| 624 | mSharedBufferClient->undoDequeue(bufIdx); |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 625 | } |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 626 | |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 627 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | int Surface::lockBuffer(android_native_buffer_t* buffer) |
| 631 | { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 632 | status_t err = validate(); |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 633 | if (err != NO_ERROR) |
| 634 | return err; |
| 635 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 636 | int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer)); |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 637 | |
| 638 | GraphicLog& logger(GraphicLog::getInstance()); |
| 639 | logger.log(GraphicLog::SF_APP_LOCK_BEFORE, mIdentity, bufIdx); |
| 640 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 641 | err = mSharedBufferClient->lock(bufIdx); |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 642 | |
| 643 | logger.log(GraphicLog::SF_APP_LOCK_AFTER, mIdentity, bufIdx); |
| 644 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 645 | LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err)); |
| 646 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | int Surface::queueBuffer(android_native_buffer_t* buffer) |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 650 | { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 651 | status_t err = validate(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 652 | if (err != NO_ERROR) |
| 653 | return err; |
| 654 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 655 | if (mSwapRectangle.isValid()) { |
| 656 | mDirtyRegion.set(mSwapRectangle); |
| 657 | } |
| 658 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 659 | int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer)); |
Mathias Agopian | 04262e9 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 660 | |
| 661 | GraphicLog::getInstance().log(GraphicLog::SF_APP_QUEUE, mIdentity, bufIdx); |
| 662 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 663 | mSharedBufferClient->setTransform(bufIdx, mNextBufferTransform); |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 664 | mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 665 | mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion); |
| 666 | err = mSharedBufferClient->queue(bufIdx); |
| 667 | LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 668 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 669 | if (err == NO_ERROR) { |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 670 | // TODO: can we avoid this IPC if we know there is one pending? |
| 671 | mClient.signalServer(); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 672 | } |
| 673 | return err; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 676 | int Surface::query(int what, int* value) |
| 677 | { |
| 678 | switch (what) { |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 679 | case NATIVE_WINDOW_WIDTH: |
| 680 | *value = int(mWidth); |
| 681 | return NO_ERROR; |
| 682 | case NATIVE_WINDOW_HEIGHT: |
| 683 | *value = int(mHeight); |
| 684 | return NO_ERROR; |
| 685 | case NATIVE_WINDOW_FORMAT: |
| 686 | *value = int(mFormat); |
| 687 | return NO_ERROR; |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 688 | } |
| 689 | return BAD_VALUE; |
| 690 | } |
| 691 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 692 | int Surface::perform(int operation, va_list args) |
| 693 | { |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 694 | status_t err = validate(); |
| 695 | if (err != NO_ERROR) |
| 696 | return err; |
| 697 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 698 | int res = NO_ERROR; |
| 699 | switch (operation) { |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 700 | case NATIVE_WINDOW_SET_USAGE: |
| 701 | dispatch_setUsage( args ); |
| 702 | break; |
| 703 | case NATIVE_WINDOW_CONNECT: |
| 704 | res = dispatch_connect( args ); |
| 705 | break; |
| 706 | case NATIVE_WINDOW_DISCONNECT: |
| 707 | res = dispatch_disconnect( args ); |
| 708 | break; |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 709 | case NATIVE_WINDOW_SET_CROP: |
| 710 | res = dispatch_crop( args ); |
| 711 | break; |
Mathias Agopian | 25f0bda | 2010-05-21 14:19:50 -0700 | [diff] [blame] | 712 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 713 | res = dispatch_set_buffer_count( args ); |
| 714 | break; |
Mathias Agopian | 663badd | 2010-05-26 21:31:09 -0700 | [diff] [blame] | 715 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 716 | res = dispatch_set_buffers_geometry( args ); |
| 717 | break; |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 718 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 719 | res = dispatch_set_buffers_transform( args ); |
| 720 | break; |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 721 | default: |
| 722 | res = NAME_NOT_FOUND; |
| 723 | break; |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 724 | } |
| 725 | return res; |
| 726 | } |
| 727 | |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 728 | void Surface::dispatch_setUsage(va_list args) { |
| 729 | int usage = va_arg(args, int); |
| 730 | setUsage( usage ); |
| 731 | } |
| 732 | int Surface::dispatch_connect(va_list args) { |
| 733 | int api = va_arg(args, int); |
| 734 | return connect( api ); |
| 735 | } |
| 736 | int Surface::dispatch_disconnect(va_list args) { |
| 737 | int api = va_arg(args, int); |
| 738 | return disconnect( api ); |
| 739 | } |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 740 | int Surface::dispatch_crop(va_list args) { |
| 741 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 742 | return crop( reinterpret_cast<Rect const*>(rect) ); |
| 743 | } |
Mathias Agopian | 25f0bda | 2010-05-21 14:19:50 -0700 | [diff] [blame] | 744 | int Surface::dispatch_set_buffer_count(va_list args) { |
| 745 | size_t bufferCount = va_arg(args, size_t); |
| 746 | return setBufferCount(bufferCount); |
| 747 | } |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 748 | int Surface::dispatch_set_buffers_geometry(va_list args) { |
| 749 | int w = va_arg(args, int); |
| 750 | int h = va_arg(args, int); |
| 751 | int f = va_arg(args, int); |
| 752 | return setBuffersGeometry(w, h, f); |
| 753 | } |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 754 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 755 | int Surface::dispatch_set_buffers_transform(va_list args) { |
| 756 | int transform = va_arg(args, int); |
| 757 | return setBuffersTransform(transform); |
| 758 | } |
| 759 | |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 760 | void Surface::setUsage(uint32_t reqUsage) |
| 761 | { |
| 762 | Mutex::Autolock _l(mSurfaceLock); |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 763 | mBufferInfo.set(reqUsage); |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 766 | int Surface::connect(int api) |
| 767 | { |
| 768 | Mutex::Autolock _l(mSurfaceLock); |
| 769 | int err = NO_ERROR; |
| 770 | switch (api) { |
| 771 | case NATIVE_WINDOW_API_EGL: |
| 772 | if (mConnected) { |
| 773 | err = -EINVAL; |
| 774 | } else { |
| 775 | mConnected = api; |
| 776 | } |
| 777 | break; |
| 778 | default: |
| 779 | err = -EINVAL; |
| 780 | break; |
| 781 | } |
| 782 | return err; |
| 783 | } |
| 784 | |
| 785 | int Surface::disconnect(int api) |
| 786 | { |
| 787 | Mutex::Autolock _l(mSurfaceLock); |
| 788 | int err = NO_ERROR; |
| 789 | switch (api) { |
| 790 | case NATIVE_WINDOW_API_EGL: |
| 791 | if (mConnected == api) { |
| 792 | mConnected = 0; |
| 793 | } else { |
| 794 | err = -EINVAL; |
| 795 | } |
| 796 | break; |
| 797 | default: |
| 798 | err = -EINVAL; |
| 799 | break; |
| 800 | } |
| 801 | return err; |
| 802 | } |
| 803 | |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 804 | int Surface::crop(Rect const* rect) |
| 805 | { |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 806 | // empty/invalid rects are not allowed |
| 807 | if (rect->isEmpty()) |
| 808 | return BAD_VALUE; |
| 809 | |
Mathias Agopian | 16a86ee | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 810 | Mutex::Autolock _l(mSurfaceLock); |
| 811 | // TODO: validate rect size |
| 812 | mNextBufferCrop = *rect; |
| 813 | return NO_ERROR; |
| 814 | } |
| 815 | |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 816 | int Surface::setBufferCount(int bufferCount) |
| 817 | { |
| 818 | sp<ISurface> s(mSurface); |
| 819 | if (s == 0) return NO_INIT; |
| 820 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 821 | class SetBufferCountIPC : public SharedBufferClient::SetBufferCountCallback { |
| 822 | sp<ISurface> surface; |
| 823 | virtual status_t operator()(int bufferCount) const { |
| 824 | return surface->setBufferCount(bufferCount); |
| 825 | } |
| 826 | public: |
| 827 | SetBufferCountIPC(const sp<ISurface>& surface) : surface(surface) { } |
| 828 | } ipc(s); |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 829 | |
Mathias Agopian | 898c4c9 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 830 | status_t err = mSharedBufferClient->setBufferCount(bufferCount, ipc); |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 831 | LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s", |
| 832 | bufferCount, strerror(-err)); |
Mathias Agopian | 59751db | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 833 | return err; |
| 834 | } |
| 835 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 836 | int Surface::setBuffersGeometry(int w, int h, int format) |
| 837 | { |
| 838 | if (w<0 || h<0 || format<0) |
| 839 | return BAD_VALUE; |
| 840 | |
| 841 | if ((w && !h) || (!w && h)) |
| 842 | return BAD_VALUE; |
| 843 | |
| 844 | Mutex::Autolock _l(mSurfaceLock); |
| 845 | mBufferInfo.set(w, h, format); |
| 846 | return NO_ERROR; |
| 847 | } |
| 848 | |
Mathias Agopian | e96aa3e | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 849 | int Surface::setBuffersTransform(int transform) |
| 850 | { |
| 851 | Mutex::Autolock _l(mSurfaceLock); |
| 852 | mNextBufferTransform = transform; |
| 853 | return NO_ERROR; |
| 854 | } |
| 855 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 856 | // ---------------------------------------------------------------------------- |
| 857 | |
| 858 | int Surface::getConnectedApi() const |
| 859 | { |
| 860 | Mutex::Autolock _l(mSurfaceLock); |
| 861 | return mConnected; |
| 862 | } |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 863 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 864 | // ---------------------------------------------------------------------------- |
| 865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | status_t Surface::lock(SurfaceInfo* info, bool blocking) { |
| 867 | return Surface::lock(info, NULL, blocking); |
| 868 | } |
| 869 | |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 870 | status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 871 | { |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 872 | if (getConnectedApi()) { |
| 873 | LOGE("Surface::lock(%p) failed. Already connected to another API", |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 874 | (ANativeWindow*)this); |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 875 | CallStack stack; |
| 876 | stack.update(); |
| 877 | stack.dump(""); |
| 878 | return INVALID_OPERATION; |
| 879 | } |
| 880 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 881 | if (mApiLock.tryLock() != NO_ERROR) { |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 882 | LOGE("calling Surface::lock from different threads!"); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 883 | CallStack stack; |
| 884 | stack.update(); |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 885 | stack.dump(""); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 886 | return WOULD_BLOCK; |
| 887 | } |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 888 | |
| 889 | /* Here we're holding mApiLock */ |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 890 | |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 891 | if (mLockedBuffer != 0) { |
| 892 | LOGE("Surface::lock failed, already locked"); |
| 893 | mApiLock.unlock(); |
| 894 | return INVALID_OPERATION; |
| 895 | } |
| 896 | |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 897 | // we're intending to do software rendering from this point |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 898 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 899 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 900 | android_native_buffer_t* out; |
| 901 | status_t err = dequeueBuffer(&out); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 902 | LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 903 | if (err == NO_ERROR) { |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 904 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 905 | err = lockBuffer(backBuffer.get()); |
| 906 | LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)", |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 907 | getBufferIndex(backBuffer), strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 908 | if (err == NO_ERROR) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 909 | const Rect bounds(backBuffer->width, backBuffer->height); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 910 | const Region boundsRegion(bounds); |
| 911 | Region scratch(boundsRegion); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 912 | Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 913 | newDirtyRegion &= boundsRegion; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 914 | |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 915 | // figure out if we can copy the frontbuffer back |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 916 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 917 | const bool canCopyBack = (frontBuffer != 0 && |
| 918 | backBuffer->width == frontBuffer->width && |
| 919 | backBuffer->height == frontBuffer->height && |
| 920 | backBuffer->format == frontBuffer->format && |
| 921 | !(mFlags & ISurfaceComposer::eDestroyBackbuffer)); |
| 922 | |
| 923 | // the dirty region we report to surfaceflinger is the one |
| 924 | // given by the user (as opposed to the one *we* return to the |
| 925 | // user). |
| 926 | mDirtyRegion = newDirtyRegion; |
| 927 | |
| 928 | if (canCopyBack) { |
| 929 | // copy the area that is invalid and not repainted this round |
| 930 | const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion)); |
| 931 | if (!copyback.isEmpty()) |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 932 | copyBlt(backBuffer, frontBuffer, copyback); |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 933 | } else { |
| 934 | // if we can't copy-back anything, modify the user's dirty |
| 935 | // region to make sure they redraw the whole buffer |
| 936 | newDirtyRegion = boundsRegion; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 937 | } |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 938 | |
Mathias Agopian | a8a0aa8 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 939 | // keep track of the are of the buffer that is "clean" |
| 940 | // (ie: that will be redrawn) |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 941 | mOldDirtyRegion = newDirtyRegion; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 942 | |
Mathias Agopian | 430f2ed | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 943 | void* vaddr; |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 944 | status_t res = backBuffer->lock( |
| 945 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Mathias Agopian | 430f2ed | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 946 | newDirtyRegion.bounds(), &vaddr); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 947 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 948 | LOGW_IF(res, "failed locking buffer (handle = %p)", |
| 949 | backBuffer->handle); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 950 | |
| 951 | mLockedBuffer = backBuffer; |
| 952 | other->w = backBuffer->width; |
| 953 | other->h = backBuffer->height; |
| 954 | other->s = backBuffer->stride; |
| 955 | other->usage = backBuffer->usage; |
| 956 | other->format = backBuffer->format; |
Mathias Agopian | 430f2ed | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 957 | other->bits = vaddr; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 960 | mApiLock.unlock(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 961 | return err; |
| 962 | } |
| 963 | |
| 964 | status_t Surface::unlockAndPost() |
| 965 | { |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 966 | if (mLockedBuffer == 0) { |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 967 | LOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 968 | return INVALID_OPERATION; |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 969 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 970 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 971 | status_t err = mLockedBuffer->unlock(); |
| 972 | LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 973 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 974 | err = queueBuffer(mLockedBuffer.get()); |
| 975 | LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)", |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 976 | getBufferIndex(mLockedBuffer), strerror(-err)); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 977 | |
| 978 | mPostedBuffer = mLockedBuffer; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 979 | mLockedBuffer = 0; |
| 980 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | } |
| 982 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | void Surface::setSwapRectangle(const Rect& r) { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 984 | Mutex::Autolock _l(mSurfaceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | mSwapRectangle = r; |
| 986 | } |
| 987 | |
Mathias Agopian | f590f70 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 988 | int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const |
| 989 | { |
| 990 | return buffer->getIndex(); |
| 991 | } |
| 992 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 993 | status_t Surface::getBufferLocked(int index, |
| 994 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | { |
Mathias Agopian | 321abdb | 2009-08-14 18:52:17 -0700 | [diff] [blame] | 996 | sp<ISurface> s(mSurface); |
| 997 | if (s == 0) return NO_INIT; |
| 998 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 999 | status_t err = NO_MEMORY; |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 1000 | |
| 1001 | // free the current buffer |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1002 | sp<GraphicBuffer>& currentBuffer(mBuffers.editItemAt(index)); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 1003 | if (currentBuffer != 0) { |
| 1004 | getBufferMapper().unregisterBuffer(currentBuffer->handle); |
| 1005 | currentBuffer.clear(); |
| 1006 | } |
| 1007 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1008 | sp<GraphicBuffer> buffer = s->requestBuffer(index, w, h, format, usage); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1009 | LOGE_IF(buffer==0, |
| 1010 | "ISurface::getBuffer(%d, %08x) returned NULL", |
| 1011 | index, usage); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 1012 | if (buffer != 0) { // this should never happen by construction |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1013 | LOGE_IF(buffer->handle == NULL, |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1014 | "Surface (identity=%d) requestBuffer(%d, %u, %u, %u, %08x) " |
| 1015 | "returned a buffer with a null handle", |
| 1016 | mIdentity, index, w, h, format, usage); |
Mathias Agopian | 0c4cec7 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 1017 | err = mSharedBufferClient->getStatus(); |
| 1018 | LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err); |
| 1019 | if (!err && buffer->handle != NULL) { |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 1020 | err = getBufferMapper().registerBuffer(buffer->handle); |
| 1021 | LOGW_IF(err, "registerBuffer(...) failed %d (%s)", |
| 1022 | err, strerror(-err)); |
| 1023 | if (err == NO_ERROR) { |
| 1024 | currentBuffer = buffer; |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1025 | currentBuffer->setIndex(index); |
Mathias Agopian | b2f8450 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 1026 | } |
Mathias Agopian | 0da41a3 | 2009-10-06 15:58:44 -0700 | [diff] [blame] | 1027 | } else { |
Mathias Agopian | 25f0bda | 2010-05-21 14:19:50 -0700 | [diff] [blame] | 1028 | err = err<0 ? err : status_t(NO_MEMORY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | } |
| 1030 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1031 | return err; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1034 | // ---------------------------------------------------------------------------- |
| 1035 | Surface::BufferInfo::BufferInfo() |
| 1036 | : mWidth(0), mHeight(0), mFormat(0), |
| 1037 | mUsage(GRALLOC_USAGE_HW_RENDER), mDirty(0) |
| 1038 | { |
| 1039 | } |
| 1040 | |
| 1041 | void Surface::BufferInfo::set(uint32_t w, uint32_t h, uint32_t format) { |
| 1042 | if ((mWidth != w) || (mHeight != h) || (mFormat != format)) { |
| 1043 | mWidth = w; |
| 1044 | mHeight = h; |
| 1045 | mFormat = format; |
| 1046 | mDirty |= GEOMETRY; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | void Surface::BufferInfo::set(uint32_t usage) { |
| 1051 | mUsage = usage; |
| 1052 | } |
| 1053 | |
| 1054 | void Surface::BufferInfo::get(uint32_t *pWidth, uint32_t *pHeight, |
| 1055 | uint32_t *pFormat, uint32_t *pUsage) const { |
| 1056 | *pWidth = mWidth; |
| 1057 | *pHeight = mHeight; |
| 1058 | *pFormat = mFormat; |
| 1059 | *pUsage = mUsage; |
| 1060 | } |
| 1061 | |
| 1062 | bool Surface::BufferInfo::validateBuffer(const sp<GraphicBuffer>& buffer) const { |
| 1063 | // make sure we AT LEAST have the usage flags we want |
| 1064 | if (mDirty || buffer==0 || |
| 1065 | ((buffer->usage & mUsage) != mUsage)) { |
| 1066 | mDirty = 0; |
| 1067 | return false; |
| 1068 | } |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
| 1072 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | }; // namespace android |