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