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 "SurfaceComposerClient" |
| 18 | |
| 19 | #include <stdint.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> |
| 23 | #include <utils/threads.h> |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 24 | #include <utils/SortedVector.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 26 | #include <utils/Singleton.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/IMemory.h> |
| 30 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 | #include <ui/DisplayInfo.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 33 | #include <surfaceflinger/ISurfaceComposer.h> |
| 34 | #include <surfaceflinger/ISurfaceFlingerClient.h> |
| 35 | #include <surfaceflinger/ISurface.h> |
| 36 | #include <surfaceflinger/SurfaceComposerClient.h> |
| 37 | |
| 38 | #include <private/surfaceflinger/LayerState.h> |
| 39 | #include <private/surfaceflinger/SharedBufferStack.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | namespace android { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | // --------------------------------------------------------------------------- |
| 44 | |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 45 | class Composer : public Singleton<Composer> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | { |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 47 | // these are constants |
| 48 | sp<ISurfaceComposer> mComposerService; |
| 49 | sp<IMemoryHeap> mServerCblkMemory; |
| 50 | surface_flinger_cblk_t volatile* mServerCblk; |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 51 | |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 52 | Mutex mLock; |
| 53 | SortedVector< wp<SurfaceComposerClient> > mActiveConnections; |
| 54 | SortedVector<sp<SurfaceComposerClient> > mOpenTransactions; |
| 55 | |
| 56 | Composer() : Singleton<Composer>() { |
| 57 | const String16 name("SurfaceFlinger"); |
| 58 | while (getService(name, &mComposerService) != NO_ERROR) { |
| 59 | usleep(250000); |
| 60 | } |
| 61 | mServerCblkMemory = mComposerService->getCblk(); |
| 62 | mServerCblk = static_cast<surface_flinger_cblk_t volatile *>( |
| 63 | mServerCblkMemory->getBase()); |
| 64 | } |
| 65 | |
| 66 | void addClientImpl(const sp<SurfaceComposerClient>& client) { |
| 67 | Mutex::Autolock _l(mLock); |
| 68 | mActiveConnections.add(client); |
| 69 | } |
| 70 | |
| 71 | void removeClientImpl(const sp<SurfaceComposerClient>& client) { |
| 72 | Mutex::Autolock _l(mLock); |
| 73 | mActiveConnections.remove(client); |
| 74 | } |
| 75 | |
| 76 | void openGlobalTransactionImpl() |
| 77 | { |
| 78 | Mutex::Autolock _l(mLock); |
| 79 | if (mOpenTransactions.size()) { |
| 80 | LOGE("openGlobalTransaction() called more than once. skipping."); |
| 81 | return; |
| 82 | } |
| 83 | const size_t N = mActiveConnections.size(); |
| 84 | for (size_t i=0; i<N; i++) { |
| 85 | sp<SurfaceComposerClient> client(mActiveConnections[i].promote()); |
| 86 | if (client != 0 && mOpenTransactions.indexOf(client) < 0) { |
| 87 | if (client->openTransaction() == NO_ERROR) { |
| 88 | mOpenTransactions.add(client); |
| 89 | } else { |
| 90 | LOGE("openTransaction on client %p failed", client.get()); |
| 91 | // let it go, it'll fail later when the user |
| 92 | // tries to do something with the transaction |
| 93 | } |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 94 | } |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 95 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 97 | |
| 98 | void closeGlobalTransactionImpl() |
| 99 | { |
| 100 | mLock.lock(); |
| 101 | SortedVector< sp<SurfaceComposerClient> > clients(mOpenTransactions); |
| 102 | mOpenTransactions.clear(); |
| 103 | mLock.unlock(); |
| 104 | |
| 105 | sp<ISurfaceComposer> sm(mComposerService); |
| 106 | sm->openGlobalTransaction(); |
| 107 | const size_t N = clients.size(); |
| 108 | for (size_t i=0; i<N; i++) { |
| 109 | clients[i]->closeTransaction(); |
| 110 | } |
| 111 | sm->closeGlobalTransaction(); |
| 112 | } |
| 113 | |
| 114 | friend class Singleton<Composer>; |
| 115 | |
| 116 | public: |
| 117 | static sp<ISurfaceComposer> getComposerService() { |
| 118 | return Composer::getInstance().mComposerService; |
| 119 | } |
| 120 | static surface_flinger_cblk_t const volatile * getControlBlock() { |
| 121 | return Composer::getInstance().mServerCblk; |
| 122 | } |
| 123 | static void addClient(const sp<SurfaceComposerClient>& client) { |
| 124 | Composer::getInstance().addClientImpl(client); |
| 125 | } |
| 126 | static void removeClient(const sp<SurfaceComposerClient>& client) { |
| 127 | Composer::getInstance().removeClientImpl(client); |
| 128 | } |
| 129 | static void openGlobalTransaction() { |
| 130 | Composer::getInstance().openGlobalTransactionImpl(); |
| 131 | } |
| 132 | static void closeGlobalTransaction() { |
| 133 | Composer::getInstance().closeGlobalTransactionImpl(); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | ANDROID_SINGLETON_STATIC_INSTANCE(Composer); |
| 138 | |
| 139 | static inline sp<ISurfaceComposer> getComposerService() { |
| 140 | return Composer::getComposerService(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 143 | static inline surface_flinger_cblk_t const volatile * get_cblk() { |
| 144 | return Composer::getControlBlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // --------------------------------------------------------------------------- |
| 148 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | static inline int compare_type( const layer_state_t& lhs, |
| 150 | const layer_state_t& rhs) { |
| 151 | if (lhs.surface < rhs.surface) return -1; |
| 152 | if (lhs.surface > rhs.surface) return 1; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | SurfaceComposerClient::SurfaceComposerClient() |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 157 | : mTransactionOpen(0), mPrebuiltLayerState(0), mStatus(NO_INIT) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 161 | void SurfaceComposerClient::onFirstRef() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | { |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 163 | sp<ISurfaceComposer> sm(getComposerService()); |
| 164 | if (sm != 0) { |
| 165 | sp<ISurfaceFlingerClient> conn = sm->createConnection(); |
| 166 | if (conn != 0) { |
| 167 | mClient = conn; |
| 168 | Composer::addClient(this); |
| 169 | mPrebuiltLayerState = new layer_state_t; |
| 170 | mStatus = NO_ERROR; |
| 171 | } |
| 172 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 175 | SurfaceComposerClient::~SurfaceComposerClient() |
| 176 | { |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 177 | delete mPrebuiltLayerState; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 178 | dispose(); |
| 179 | } |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 180 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | status_t SurfaceComposerClient::initCheck() const |
| 182 | { |
| 183 | return mStatus; |
| 184 | } |
| 185 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | sp<IBinder> SurfaceComposerClient::connection() const |
| 187 | { |
| 188 | return (mClient != 0) ? mClient->asBinder() : 0; |
| 189 | } |
| 190 | |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 191 | status_t SurfaceComposerClient::linkToComposerDeath( |
| 192 | const sp<IBinder::DeathRecipient>& recipient, |
| 193 | void* cookie, uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | { |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 195 | sp<ISurfaceComposer> sm(getComposerService()); |
| 196 | return sm->asBinder()->linkToDeath(recipient, cookie, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void SurfaceComposerClient::dispose() |
| 200 | { |
| 201 | // this can be called more than once. |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 202 | sp<ISurfaceFlingerClient> client; |
| 203 | Mutex::Autolock _lm(mLock); |
| 204 | if (mClient != 0) { |
| 205 | Composer::removeClient(this); |
| 206 | client = mClient; // hold ref while lock is held |
| 207 | mClient.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 209 | mStatus = NO_INIT; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | status_t SurfaceComposerClient::getDisplayInfo( |
| 213 | DisplayID dpy, DisplayInfo* info) |
| 214 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 215 | if (uint32_t(dpy)>=SharedBufferStack::NUM_DISPLAY_MAX) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | return BAD_VALUE; |
| 217 | |
| 218 | volatile surface_flinger_cblk_t const * cblk = get_cblk(); |
| 219 | volatile display_cblk_t const * dcblk = cblk->displays + dpy; |
| 220 | |
| 221 | info->w = dcblk->w; |
| 222 | info->h = dcblk->h; |
| 223 | info->orientation = dcblk->orientation; |
| 224 | info->xdpi = dcblk->xdpi; |
| 225 | info->ydpi = dcblk->ydpi; |
| 226 | info->fps = dcblk->fps; |
| 227 | info->density = dcblk->density; |
| 228 | return getPixelFormatInfo(dcblk->format, &(info->pixelFormatInfo)); |
| 229 | } |
| 230 | |
| 231 | ssize_t SurfaceComposerClient::getDisplayWidth(DisplayID dpy) |
| 232 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 233 | if (uint32_t(dpy)>=SharedBufferStack::NUM_DISPLAY_MAX) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | return BAD_VALUE; |
| 235 | volatile surface_flinger_cblk_t const * cblk = get_cblk(); |
| 236 | volatile display_cblk_t const * dcblk = cblk->displays + dpy; |
| 237 | return dcblk->w; |
| 238 | } |
| 239 | |
| 240 | ssize_t SurfaceComposerClient::getDisplayHeight(DisplayID dpy) |
| 241 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 242 | if (uint32_t(dpy)>=SharedBufferStack::NUM_DISPLAY_MAX) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | return BAD_VALUE; |
| 244 | volatile surface_flinger_cblk_t const * cblk = get_cblk(); |
| 245 | volatile display_cblk_t const * dcblk = cblk->displays + dpy; |
| 246 | return dcblk->h; |
| 247 | } |
| 248 | |
| 249 | ssize_t SurfaceComposerClient::getDisplayOrientation(DisplayID dpy) |
| 250 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 251 | if (uint32_t(dpy)>=SharedBufferStack::NUM_DISPLAY_MAX) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | return BAD_VALUE; |
| 253 | volatile surface_flinger_cblk_t const * cblk = get_cblk(); |
| 254 | volatile display_cblk_t const * dcblk = cblk->displays + dpy; |
| 255 | return dcblk->orientation; |
| 256 | } |
| 257 | |
| 258 | ssize_t SurfaceComposerClient::getNumberOfDisplays() |
| 259 | { |
| 260 | volatile surface_flinger_cblk_t const * cblk = get_cblk(); |
| 261 | uint32_t connected = cblk->connected; |
| 262 | int n = 0; |
| 263 | while (connected) { |
| 264 | if (connected&1) n++; |
| 265 | connected >>= 1; |
| 266 | } |
| 267 | return n; |
| 268 | } |
| 269 | |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 270 | sp<SurfaceControl> SurfaceComposerClient::createSurface( |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | int pid, |
| 272 | DisplayID display, |
| 273 | uint32_t w, |
| 274 | uint32_t h, |
| 275 | PixelFormat format, |
| 276 | uint32_t flags) |
| 277 | { |
Mathias Agopian | 285dbde | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 278 | String8 name; |
| 279 | const size_t SIZE = 128; |
| 280 | char buffer[SIZE]; |
| 281 | snprintf(buffer, SIZE, "<pid_%d>", getpid()); |
| 282 | name.append(buffer); |
| 283 | |
| 284 | return SurfaceComposerClient::createSurface(pid, name, display, |
| 285 | w, h, format, flags); |
Mathias Agopian | 285dbde | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | sp<SurfaceControl> SurfaceComposerClient::createSurface( |
| 289 | int pid, |
| 290 | const String8& name, |
| 291 | DisplayID display, |
| 292 | uint32_t w, |
| 293 | uint32_t h, |
| 294 | PixelFormat format, |
| 295 | uint32_t flags) |
| 296 | { |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 297 | sp<SurfaceControl> result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | if (mStatus == NO_ERROR) { |
| 299 | ISurfaceFlingerClient::surface_data_t data; |
Mathias Agopian | 285dbde | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 300 | sp<ISurface> surface = mClient->createSurface(&data, pid, name, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | display, w, h, format, flags); |
| 302 | if (surface != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 303 | if (uint32_t(data.token) < SharedBufferStack::NUM_LAYERS_MAX) { |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 304 | result = new SurfaceControl(this, surface, data, w, h, format, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | status_t SurfaceComposerClient::destroySurface(SurfaceID sid) |
| 312 | { |
| 313 | if (mStatus != NO_ERROR) |
| 314 | return mStatus; |
| 315 | |
| 316 | // it's okay to destroy a surface while a transaction is open, |
| 317 | // (transactions really are a client-side concept) |
| 318 | // however, this indicates probably a misuse of the API or a bug |
| 319 | // in the client code. |
| 320 | LOGW_IF(mTransactionOpen, |
| 321 | "Destroying surface while a transaction is open. " |
| 322 | "Client %p: destroying surface %d, mTransactionOpen=%d", |
| 323 | this, sid, mTransactionOpen); |
| 324 | |
| 325 | status_t err = mClient->destroySurface(sid); |
| 326 | return err; |
| 327 | } |
| 328 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | void SurfaceComposerClient::openGlobalTransaction() |
| 330 | { |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 331 | Composer::openGlobalTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void SurfaceComposerClient::closeGlobalTransaction() |
| 335 | { |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 336 | Composer::closeGlobalTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | status_t SurfaceComposerClient::freezeDisplay(DisplayID dpy, uint32_t flags) |
| 340 | { |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 341 | sp<ISurfaceComposer> sm(getComposerService()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | return sm->freezeDisplay(dpy, flags); |
| 343 | } |
| 344 | |
| 345 | status_t SurfaceComposerClient::unfreezeDisplay(DisplayID dpy, uint32_t flags) |
| 346 | { |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 347 | sp<ISurfaceComposer> sm(getComposerService()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | return sm->unfreezeDisplay(dpy, flags); |
| 349 | } |
| 350 | |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 351 | int SurfaceComposerClient::setOrientation(DisplayID dpy, |
| 352 | int orientation, uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | { |
Mathias Agopian | dd3423c | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 354 | sp<ISurfaceComposer> sm(getComposerService()); |
Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 355 | return sm->setOrientation(dpy, orientation, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | status_t SurfaceComposerClient::openTransaction() |
| 359 | { |
| 360 | if (mStatus != NO_ERROR) |
| 361 | return mStatus; |
| 362 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | mTransactionOpen++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | return NO_ERROR; |
| 365 | } |
| 366 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | status_t SurfaceComposerClient::closeTransaction() |
| 368 | { |
| 369 | if (mStatus != NO_ERROR) |
| 370 | return mStatus; |
| 371 | |
| 372 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | if (mTransactionOpen <= 0) { |
| 374 | LOGE( "closeTransaction (client %p, mTransactionOpen=%d) " |
| 375 | "called more times than openTransaction()", |
| 376 | this, mTransactionOpen); |
| 377 | return INVALID_OPERATION; |
| 378 | } |
| 379 | |
| 380 | if (mTransactionOpen >= 2) { |
| 381 | mTransactionOpen--; |
| 382 | return NO_ERROR; |
| 383 | } |
| 384 | |
| 385 | mTransactionOpen = 0; |
| 386 | const ssize_t count = mStates.size(); |
| 387 | if (count) { |
| 388 | mClient->setState(count, mStates.array()); |
| 389 | mStates.clear(); |
| 390 | } |
| 391 | return NO_ERROR; |
| 392 | } |
| 393 | |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 394 | layer_state_t* SurfaceComposerClient::get_state_l(SurfaceID index) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | // API usage error, do nothing. |
| 397 | if (mTransactionOpen<=0) { |
| 398 | LOGE("Not in transaction (client=%p, SurfaceID=%d, mTransactionOpen=%d", |
| 399 | this, int(index), mTransactionOpen); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | // use mPrebuiltLayerState just to find out if we already have it |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 404 | layer_state_t& dummy(*mPrebuiltLayerState); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | dummy.surface = index; |
| 406 | ssize_t i = mStates.indexOf(dummy); |
| 407 | if (i < 0) { |
| 408 | // we don't have it, add an initialized layer_state to our list |
| 409 | i = mStates.add(dummy); |
| 410 | } |
| 411 | return mStates.editArray() + i; |
| 412 | } |
| 413 | |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 414 | layer_state_t* SurfaceComposerClient::lockLayerState(SurfaceID id) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | { |
| 416 | layer_state_t* s; |
| 417 | mLock.lock(); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 418 | s = get_state_l(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | if (!s) mLock.unlock(); |
| 420 | return s; |
| 421 | } |
| 422 | |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 423 | void SurfaceComposerClient::unlockLayerState() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | { |
| 425 | mLock.unlock(); |
| 426 | } |
| 427 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 428 | status_t SurfaceComposerClient::setPosition(SurfaceID id, int32_t x, int32_t y) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 430 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | if (!s) return BAD_INDEX; |
| 432 | s->what |= ISurfaceComposer::ePositionChanged; |
| 433 | s->x = x; |
| 434 | s->y = y; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 435 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | return NO_ERROR; |
| 437 | } |
| 438 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 439 | status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 441 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | if (!s) return BAD_INDEX; |
| 443 | s->what |= ISurfaceComposer::eSizeChanged; |
| 444 | s->w = w; |
| 445 | s->h = h; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 446 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | return NO_ERROR; |
| 448 | } |
| 449 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 450 | status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 452 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | if (!s) return BAD_INDEX; |
| 454 | s->what |= ISurfaceComposer::eLayerChanged; |
| 455 | s->z = z; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 456 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | return NO_ERROR; |
| 458 | } |
| 459 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 460 | status_t SurfaceComposerClient::hide(SurfaceID id) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | { |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 462 | return setFlags(id, ISurfaceComposer::eLayerHidden, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | ISurfaceComposer::eLayerHidden); |
| 464 | } |
| 465 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 466 | status_t SurfaceComposerClient::show(SurfaceID id, int32_t) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | { |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 468 | return setFlags(id, 0, ISurfaceComposer::eLayerHidden); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 471 | status_t SurfaceComposerClient::freeze(SurfaceID id) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | { |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 473 | return setFlags(id, ISurfaceComposer::eLayerFrozen, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | ISurfaceComposer::eLayerFrozen); |
| 475 | } |
| 476 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 477 | status_t SurfaceComposerClient::unfreeze(SurfaceID id) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | { |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 479 | return setFlags(id, 0, ISurfaceComposer::eLayerFrozen); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 482 | status_t SurfaceComposerClient::setFlags(SurfaceID id, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | uint32_t flags, uint32_t mask) |
| 484 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 485 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | if (!s) return BAD_INDEX; |
| 487 | s->what |= ISurfaceComposer::eVisibilityChanged; |
| 488 | s->flags &= ~mask; |
| 489 | s->flags |= (flags & mask); |
| 490 | s->mask |= mask; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 491 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | return NO_ERROR; |
| 493 | } |
| 494 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | status_t SurfaceComposerClient::setTransparentRegionHint( |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 496 | SurfaceID id, const Region& transparentRegion) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 498 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | if (!s) return BAD_INDEX; |
| 500 | s->what |= ISurfaceComposer::eTransparentRegionChanged; |
| 501 | s->transparentRegion = transparentRegion; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 502 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | return NO_ERROR; |
| 504 | } |
| 505 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 506 | status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 508 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | if (!s) return BAD_INDEX; |
| 510 | s->what |= ISurfaceComposer::eAlphaChanged; |
| 511 | s->alpha = alpha; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 512 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | return NO_ERROR; |
| 514 | } |
| 515 | |
| 516 | status_t SurfaceComposerClient::setMatrix( |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 517 | SurfaceID id, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | float dsdx, float dtdx, |
| 519 | float dsdy, float dtdy ) |
| 520 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 521 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | if (!s) return BAD_INDEX; |
| 523 | s->what |= ISurfaceComposer::eMatrixChanged; |
| 524 | layer_state_t::matrix22_t matrix; |
| 525 | matrix.dsdx = dsdx; |
| 526 | matrix.dtdx = dtdx; |
| 527 | matrix.dsdy = dsdy; |
| 528 | matrix.dtdy = dtdy; |
| 529 | s->matrix = matrix; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 530 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | return NO_ERROR; |
| 532 | } |
| 533 | |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 534 | status_t SurfaceComposerClient::setFreezeTint(SurfaceID id, uint32_t tint) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | { |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 536 | layer_state_t* s = lockLayerState(id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | if (!s) return BAD_INDEX; |
| 538 | s->what |= ISurfaceComposer::eFreezeTintChanged; |
| 539 | s->tint = tint; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 540 | unlockLayerState(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | return NO_ERROR; |
| 542 | } |
| 543 | |
Mathias Agopian | d4784a3 | 2010-05-27 19:41:15 -0700 | [diff] [blame^] | 544 | // ---------------------------------------------------------------------------- |
| 545 | |
| 546 | SurfaceClient::SurfaceClient(const sp<SurfaceComposerClient>& client) |
| 547 | : mStatus(NO_INIT), mControl(0) |
| 548 | { |
| 549 | if (client != 0) { |
| 550 | sp<IBinder> conn = client->connection(); |
| 551 | init(conn); |
| 552 | } |
| 553 | } |
| 554 | SurfaceClient::SurfaceClient(const sp<IBinder>& conn) |
| 555 | : mStatus(NO_INIT), mControl(0) |
| 556 | { |
| 557 | init(conn); |
| 558 | } |
| 559 | void SurfaceClient::init(const sp<IBinder>& conn) |
| 560 | { |
| 561 | mSignalServer = getComposerService(); |
| 562 | sp<ISurfaceFlingerClient> sf(interface_cast<ISurfaceFlingerClient>(conn)); |
| 563 | if (sf != 0) { |
| 564 | mConnection = conn; |
| 565 | mControlMemory = sf->getControlBlock(); |
| 566 | mControl = static_cast<SharedClient *>(mControlMemory->getBase()); |
| 567 | mStatus = NO_ERROR; |
| 568 | } |
| 569 | } |
| 570 | status_t SurfaceClient::initCheck() const { |
| 571 | return mStatus; |
| 572 | } |
| 573 | SharedClient* SurfaceClient::getSharedClient() const { |
| 574 | return mControl; |
| 575 | } |
| 576 | void SurfaceClient::signalServer() const { |
| 577 | mSignalServer->signal(); |
| 578 | } |
| 579 | |
| 580 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | }; // namespace android |
| 582 | |