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