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