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