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