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