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 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 24 | #include <utils/CallStack.h> |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 27 | #include <utils/threads.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 28 | |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
| 30 | |
| 31 | #include <gui/SurfaceTextureClient.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 33 | #include <ui/DisplayInfo.h> |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | #include <ui/GraphicBuffer.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/ISurface.h> |
| 38 | #include <surfaceflinger/ISurfaceComposer.h> |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 39 | #include <surfaceflinger/Surface.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 40 | #include <surfaceflinger/SurfaceComposerClient.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 44 | // ============================================================================ |
| 45 | // SurfaceControl |
| 46 | // ============================================================================ |
| 47 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 48 | SurfaceControl::SurfaceControl( |
| 49 | const sp<SurfaceComposerClient>& client, |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 50 | const sp<ISurface>& surface, |
Mathias Agopian | 50c24a2 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 51 | const ISurfaceComposerClient::surface_data_t& data) |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 52 | : mClient(client), mSurface(surface), |
Mathias Agopian | 50c24a2 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 53 | mToken(data.token), mIdentity(data.identity) |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 54 | { |
| 55 | } |
Mathias Agopian | 69d6209 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 56 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 57 | SurfaceControl::~SurfaceControl() |
| 58 | { |
| 59 | destroy(); |
| 60 | } |
| 61 | |
| 62 | void SurfaceControl::destroy() |
| 63 | { |
Mathias Agopian | 69d6209 | 2009-04-16 20:30:22 -0700 | [diff] [blame] | 64 | if (isValid()) { |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 65 | mClient->destroySurface(mToken); |
| 66 | } |
| 67 | |
| 68 | // clear all references and trigger an IPC now, to make sure things |
| 69 | // happen without delay, since these resources are quite heavy. |
| 70 | mClient.clear(); |
| 71 | mSurface.clear(); |
| 72 | IPCThreadState::self()->flushCommands(); |
| 73 | } |
| 74 | |
| 75 | void SurfaceControl::clear() |
| 76 | { |
| 77 | // here, the window manager tells us explicitly that we should destroy |
| 78 | // the surface's resource. Soon after this call, it will also release |
| 79 | // its last reference (which will call the dtor); however, it is possible |
| 80 | // that a client living in the same process still holds references which |
| 81 | // would delay the call to the dtor -- that is why we need this explicit |
| 82 | // "clear()" call. |
| 83 | destroy(); |
| 84 | } |
| 85 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 86 | bool SurfaceControl::isSameSurface( |
| 87 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) |
| 88 | { |
| 89 | if (lhs == 0 || rhs == 0) |
| 90 | return false; |
| 91 | return lhs->mSurface->asBinder() == rhs->mSurface->asBinder(); |
| 92 | } |
| 93 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 94 | status_t SurfaceControl::setLayer(int32_t layer) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 95 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 96 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 97 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 98 | return client->setLayer(mToken, layer); |
| 99 | } |
| 100 | status_t SurfaceControl::setPosition(int32_t x, int32_t y) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 101 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 102 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 103 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 104 | return client->setPosition(mToken, x, y); |
| 105 | } |
| 106 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 107 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 108 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 109 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 110 | return client->setSize(mToken, w, h); |
| 111 | } |
| 112 | status_t SurfaceControl::hide() { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 113 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 114 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 115 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 116 | return client->hide(mToken); |
| 117 | } |
| 118 | status_t SurfaceControl::show(int32_t layer) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 119 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 120 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 121 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 122 | return client->show(mToken, layer); |
| 123 | } |
| 124 | status_t SurfaceControl::freeze() { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 125 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 126 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 127 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 128 | return client->freeze(mToken); |
| 129 | } |
| 130 | status_t SurfaceControl::unfreeze() { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 131 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 132 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 133 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 134 | return client->unfreeze(mToken); |
| 135 | } |
| 136 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 137 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 138 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 139 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 140 | return client->setFlags(mToken, flags, mask); |
| 141 | } |
| 142 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 143 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 144 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 145 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 146 | return client->setTransparentRegionHint(mToken, transparent); |
| 147 | } |
| 148 | status_t SurfaceControl::setAlpha(float alpha) { |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 149 | status_t err = validate(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 150 | if (err < 0) return err; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 151 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 152 | return client->setAlpha(mToken, alpha); |
| 153 | } |
| 154 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { |
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->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy); |
| 159 | } |
| 160 | status_t SurfaceControl::setFreezeTint(uint32_t tint) { |
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->setFreezeTint(mToken, tint); |
| 165 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 166 | |
Mathias Agopian | 18e0260 | 2009-11-13 15:26:29 -0800 | [diff] [blame] | 167 | status_t SurfaceControl::validate() const |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 168 | { |
| 169 | if (mToken<0 || mClient==0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 170 | ALOGE("invalid token (%d, identity=%u) or client (%p)", |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 171 | mToken, mIdentity, mClient.get()); |
| 172 | return NO_INIT; |
| 173 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 174 | return NO_ERROR; |
| 175 | } |
| 176 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 177 | status_t SurfaceControl::writeSurfaceToParcel( |
| 178 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 179 | { |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 180 | sp<ISurface> sur; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 181 | uint32_t identity = 0; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 182 | if (SurfaceControl::isValid(control)) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 183 | sur = control->mSurface; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 184 | identity = control->mIdentity; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 185 | } |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 186 | parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL); |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 187 | parcel->writeStrongBinder(NULL); // NULL ISurfaceTexture in this case. |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 188 | parcel->writeInt32(identity); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 189 | return NO_ERROR; |
| 190 | } |
| 191 | |
| 192 | sp<Surface> SurfaceControl::getSurface() const |
| 193 | { |
| 194 | Mutex::Autolock _l(mLock); |
| 195 | if (mSurfaceData == 0) { |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 196 | sp<SurfaceControl> surface_control(const_cast<SurfaceControl*>(this)); |
| 197 | mSurfaceData = new Surface(surface_control); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 198 | } |
| 199 | return mSurfaceData; |
| 200 | } |
| 201 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 202 | // ============================================================================ |
| 203 | // Surface |
| 204 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 206 | // --------------------------------------------------------------------------- |
| 207 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 208 | Surface::Surface(const sp<SurfaceControl>& surface) |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 209 | : SurfaceTextureClient(), |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 210 | mSurface(surface->mSurface), |
Mathias Agopian | 50c24a2 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 211 | mIdentity(surface->mIdentity) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | { |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 213 | sp<ISurfaceTexture> st; |
| 214 | if (mSurface != NULL) { |
| 215 | st = mSurface->getSurfaceTexture(); |
| 216 | } |
| 217 | init(st); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 218 | } |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 219 | |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 220 | Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref) |
Mathias Agopian | 50c24a2 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 221 | : SurfaceTextureClient() |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 222 | { |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 223 | mSurface = interface_cast<ISurface>(ref); |
| 224 | sp<IBinder> st_binder(parcel.readStrongBinder()); |
| 225 | sp<ISurfaceTexture> st; |
| 226 | if (st_binder != NULL) { |
| 227 | st = interface_cast<ISurfaceTexture>(st_binder); |
| 228 | } else if (mSurface != NULL) { |
| 229 | st = mSurface->getSurfaceTexture(); |
| 230 | } |
| 231 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 232 | mIdentity = parcel.readInt32(); |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 233 | init(st); |
| 234 | } |
| 235 | |
| 236 | Surface::Surface(const sp<ISurfaceTexture>& st) |
| 237 | : SurfaceTextureClient(), |
| 238 | mSurface(NULL), |
| 239 | mIdentity(0) |
| 240 | { |
| 241 | init(st); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 244 | status_t Surface::writeToParcel( |
| 245 | const sp<Surface>& surface, Parcel* parcel) |
| 246 | { |
| 247 | sp<ISurface> sur; |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 248 | sp<ISurfaceTexture> st; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 249 | uint32_t identity = 0; |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 250 | if (Surface::isValid(surface)) { |
| 251 | sur = surface->mSurface; |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 252 | st = surface->getISurfaceTexture(); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 253 | identity = surface->mIdentity; |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 254 | } else if (surface != 0 && |
| 255 | (surface->mSurface != NULL || |
| 256 | surface->getISurfaceTexture() != NULL)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 257 | ALOGE("Parceling invalid surface with non-NULL ISurface/ISurfaceTexture as NULL: " |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 258 | "mSurface = %p, surfaceTexture = %p, mIdentity = %d, ", |
| 259 | surface->mSurface.get(), surface->getISurfaceTexture().get(), |
| 260 | surface->mIdentity); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 261 | } |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 262 | |
| 263 | parcel->writeStrongBinder(sur != NULL ? sur->asBinder() : NULL); |
| 264 | parcel->writeStrongBinder(st != NULL ? st->asBinder() : NULL); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 265 | parcel->writeInt32(identity); |
Mathias Agopian | 5e14010 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 266 | return NO_ERROR; |
| 267 | |
| 268 | } |
| 269 | |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 270 | Mutex Surface::sCachedSurfacesLock; |
Mathias Agopian | 06f9ebf | 2010-12-13 16:47:31 -0800 | [diff] [blame] | 271 | DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces; |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 272 | |
| 273 | sp<Surface> Surface::readFromParcel(const Parcel& data) { |
| 274 | Mutex::Autolock _l(sCachedSurfacesLock); |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 275 | sp<IBinder> binder(data.readStrongBinder()); |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 276 | sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote(); |
| 277 | if (surface == 0) { |
| 278 | surface = new Surface(data, binder); |
| 279 | sCachedSurfaces.add(binder, surface); |
Ted Bonkenburg | 3f945fa | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 280 | } else { |
| 281 | // The Surface was found in the cache, but we still should clear any |
| 282 | // remaining data from the parcel. |
| 283 | data.readStrongBinder(); // ISurfaceTexture |
| 284 | data.readInt32(); // identity |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 285 | } |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 286 | if (surface->mSurface == NULL && surface->getISurfaceTexture() == NULL) { |
| 287 | surface = 0; |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 288 | } |
Mathias Agopian | 06f9ebf | 2010-12-13 16:47:31 -0800 | [diff] [blame] | 289 | cleanCachedSurfacesLocked(); |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 290 | return surface; |
| 291 | } |
| 292 | |
| 293 | // Remove the stale entries from the surface cache. This should only be called |
| 294 | // with sCachedSurfacesLock held. |
Mathias Agopian | 06f9ebf | 2010-12-13 16:47:31 -0800 | [diff] [blame] | 295 | void Surface::cleanCachedSurfacesLocked() { |
Jamie Gennis | 5ee65f0 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 296 | for (int i = sCachedSurfaces.size()-1; i >= 0; --i) { |
| 297 | wp<Surface> s(sCachedSurfaces.valueAt(i)); |
| 298 | if (s == 0 || s.promote() == 0) { |
| 299 | sCachedSurfaces.removeItemsAt(i); |
| 300 | } |
| 301 | } |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 304 | void Surface::init(const sp<ISurfaceTexture>& surfaceTexture) |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 305 | { |
Ted Bonkenburg | 0de171b | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 306 | if (mSurface != NULL || surfaceTexture != NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 307 | ALOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface"); |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 308 | if (surfaceTexture != NULL) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 309 | setISurfaceTexture(surfaceTexture); |
| 310 | setUsage(GraphicBuffer::USAGE_HW_RENDER); |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 311 | } |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 312 | |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 313 | DisplayInfo dinfo; |
| 314 | SurfaceComposerClient::getDisplayInfo(0, &dinfo); |
| 315 | const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi; |
| 316 | const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi; |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 317 | const_cast<uint32_t&>(ANativeWindow::flags) = 0; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 318 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | Surface::~Surface() |
| 322 | { |
Mathias Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 323 | // clear all references and trigger an IPC now, to make sure things |
| 324 | // happen without delay, since these resources are quite heavy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | mSurface.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | IPCThreadState::self()->flushCommands(); |
| 327 | } |
| 328 | |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 329 | bool Surface::isValid() { |
Mathias Agopian | 50c24a2 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 330 | return getISurfaceTexture() != NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
| 332 | |
tedbo | 4e8a5c9 | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 333 | sp<ISurfaceTexture> Surface::getSurfaceTexture() { |
Mathias Agopian | 50c24a2 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 334 | return getISurfaceTexture(); |
tedbo | 4e8a5c9 | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Mathias Agopian | c8a04b5 | 2011-04-05 15:44:20 -0700 | [diff] [blame] | 337 | sp<IBinder> Surface::asBinder() const { |
| 338 | return mSurface!=0 ? mSurface->asBinder() : 0; |
Mathias Agopian | 2ce19af | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 341 | // ---------------------------------------------------------------------------- |
| 342 | |
Mathias Agopian | 7bb843c | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 343 | int Surface::query(int what, int* value) const { |
Mathias Agopian | 5b5c914 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 344 | switch (what) { |
Jamie Gennis | c4ca7c5 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 345 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 346 | *value = NATIVE_WINDOW_SURFACE; |
| 347 | return NO_ERROR; |
| 348 | } |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 349 | return SurfaceTextureClient::query(what, value); |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 352 | // ---------------------------------------------------------------------------- |
| 353 | |
Mathias Agopian | becc91d | 2011-08-23 21:09:41 -0700 | [diff] [blame] | 354 | status_t Surface::lock(SurfaceInfo* other, Region* inOutDirtyRegion) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 355 | ANativeWindow_Buffer outBuffer; |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 356 | |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 357 | ARect temp; |
| 358 | ARect* inOutDirtyBounds = NULL; |
Mathias Agopian | becc91d | 2011-08-23 21:09:41 -0700 | [diff] [blame] | 359 | if (inOutDirtyRegion) { |
| 360 | temp = inOutDirtyRegion->getBounds(); |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 361 | inOutDirtyBounds = &temp; |
Mathias Agopian | 2f7540e | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 364 | status_t err = SurfaceTextureClient::lock(&outBuffer, inOutDirtyBounds); |
Mathias Agopian | 116e541 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 365 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 366 | if (err == NO_ERROR) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 367 | other->w = uint32_t(outBuffer.width); |
| 368 | other->h = uint32_t(outBuffer.height); |
| 369 | other->s = uint32_t(outBuffer.stride); |
| 370 | other->usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 371 | other->format = uint32_t(outBuffer.format); |
| 372 | other->bits = outBuffer.bits; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 373 | } |
Mathias Agopian | becc91d | 2011-08-23 21:09:41 -0700 | [diff] [blame] | 374 | |
| 375 | if (inOutDirtyRegion) { |
| 376 | inOutDirtyRegion->set( static_cast<Rect const&>(temp) ); |
| 377 | } |
| 378 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 379 | return err; |
| 380 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 381 | |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 382 | status_t Surface::unlockAndPost() { |
| 383 | return SurfaceTextureClient::unlockAndPost(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Mathias Agopian | 2be352a | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 386 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | }; // namespace android |