SurfaceComposerClient.cpp revision 8dfa92fef9759a881e96ee58d59875d35023aab9
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 <gui/ISurface.h> 35#include <gui/ISurfaceComposer.h> 36#include <gui/ISurfaceComposerClient.h> 37#include <gui/SurfaceComposerClient.h> 38 39#include <private/gui/ComposerService.h> 40#include <private/gui/LayerState.h> 41 42namespace android { 43// --------------------------------------------------------------------------- 44 45ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService); 46 47ComposerService::ComposerService() 48: Singleton<ComposerService>() { 49 Mutex::Autolock _l(mLock); 50 connectLocked(); 51} 52 53void ComposerService::connectLocked() { 54 const String16 name("SurfaceFlinger"); 55 while (getService(name, &mComposerService) != NO_ERROR) { 56 usleep(250000); 57 } 58 assert(mComposerService != NULL); 59 60 // Create the death listener. 61 class DeathObserver : public IBinder::DeathRecipient { 62 ComposerService& mComposerService; 63 virtual void binderDied(const wp<IBinder>& who) { 64 ALOGW("ComposerService remote (surfaceflinger) died [%p]", 65 who.unsafe_get()); 66 mComposerService.composerServiceDied(); 67 } 68 public: 69 DeathObserver(ComposerService& mgr) : mComposerService(mgr) { } 70 }; 71 72 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this)); 73 mComposerService->asBinder()->linkToDeath(mDeathObserver); 74} 75 76/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() { 77 ComposerService& instance = ComposerService::getInstance(); 78 Mutex::Autolock _l(instance.mLock); 79 if (instance.mComposerService == NULL) { 80 ComposerService::getInstance().connectLocked(); 81 assert(instance.mComposerService != NULL); 82 ALOGD("ComposerService reconnected"); 83 } 84 return instance.mComposerService; 85} 86 87void ComposerService::composerServiceDied() 88{ 89 Mutex::Autolock _l(mLock); 90 mComposerService = NULL; 91 mDeathObserver = NULL; 92} 93 94// --------------------------------------------------------------------------- 95 96static inline 97int compare_type(const ComposerState& lhs, const ComposerState& rhs) { 98 if (lhs.client < rhs.client) return -1; 99 if (lhs.client > rhs.client) return 1; 100 if (lhs.state.surface < rhs.state.surface) return -1; 101 if (lhs.state.surface > rhs.state.surface) return 1; 102 return 0; 103} 104 105static inline 106int compare_type(const DisplayState& lhs, const DisplayState& rhs) { 107 return compare_type(lhs.token, rhs.token); 108} 109 110class Composer : public Singleton<Composer> 111{ 112 friend class Singleton<Composer>; 113 114 mutable Mutex mLock; 115 SortedVector<ComposerState> mComposerStates; 116 SortedVector<DisplayState > mDisplayStates; 117 uint32_t mForceSynchronous; 118 119 Composer() : Singleton<Composer>(), 120 mForceSynchronous(0) 121 { } 122 123 void closeGlobalTransactionImpl(bool synchronous); 124 125 layer_state_t* getLayerStateLocked( 126 const sp<SurfaceComposerClient>& client, SurfaceID id); 127 128 DisplayState& getDisplayStateLocked(const sp<IBinder>& token); 129 130public: 131 sp<IBinder> createDisplay(const String8& displayName); 132 sp<IBinder> getBuiltInDisplay(int32_t id); 133 134 status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id, 135 float x, float y); 136 status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id, 137 uint32_t w, uint32_t h); 138 status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id, 139 int32_t z); 140 status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id, 141 uint32_t flags, uint32_t mask); 142 status_t setTransparentRegionHint( 143 const sp<SurfaceComposerClient>& client, SurfaceID id, 144 const Region& transparentRegion); 145 status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id, 146 float alpha); 147 status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id, 148 float dsdx, float dtdx, float dsdy, float dtdy); 149 status_t setOrientation(int orientation); 150 status_t setCrop(const sp<SurfaceComposerClient>& client, SurfaceID id, 151 const Rect& crop); 152 status_t setLayerStack(const sp<SurfaceComposerClient>& client, 153 SurfaceID id, uint32_t layerStack); 154 155 void setDisplaySurface(const sp<IBinder>& token, const sp<ISurfaceTexture>& surface); 156 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack); 157 void setDisplayProjection(const sp<IBinder>& token, 158 uint32_t orientation, 159 const Rect& layerStackRect, 160 const Rect& displayRect); 161 162 static void closeGlobalTransaction(bool synchronous) { 163 Composer::getInstance().closeGlobalTransactionImpl(synchronous); 164 } 165}; 166 167ANDROID_SINGLETON_STATIC_INSTANCE(Composer); 168 169// --------------------------------------------------------------------------- 170 171sp<IBinder> Composer::createDisplay(const String8& displayName) { 172 return ComposerService::getComposerService()->createDisplay(displayName); 173} 174 175sp<IBinder> Composer::getBuiltInDisplay(int32_t id) { 176 return ComposerService::getComposerService()->getBuiltInDisplay(id); 177} 178 179void Composer::closeGlobalTransactionImpl(bool synchronous) { 180 sp<ISurfaceComposer> sm(ComposerService::getComposerService()); 181 182 Vector<ComposerState> transaction; 183 Vector<DisplayState> displayTransaction; 184 uint32_t flags = 0; 185 186 { // scope for the lock 187 Mutex::Autolock _l(mLock); 188 transaction = mComposerStates; 189 mComposerStates.clear(); 190 191 displayTransaction = mDisplayStates; 192 mDisplayStates.clear(); 193 194 if (synchronous || mForceSynchronous) { 195 flags |= ISurfaceComposer::eSynchronous; 196 } 197 mForceSynchronous = false; 198 } 199 200 sm->setTransactionState(transaction, displayTransaction, flags); 201} 202 203layer_state_t* Composer::getLayerStateLocked( 204 const sp<SurfaceComposerClient>& client, SurfaceID id) { 205 206 ComposerState s; 207 s.client = client->mClient; 208 s.state.surface = id; 209 210 ssize_t index = mComposerStates.indexOf(s); 211 if (index < 0) { 212 // we don't have it, add an initialized layer_state to our list 213 index = mComposerStates.add(s); 214 } 215 216 ComposerState* const out = mComposerStates.editArray(); 217 return &(out[index].state); 218} 219 220status_t Composer::setPosition(const sp<SurfaceComposerClient>& client, 221 SurfaceID id, float x, float y) { 222 Mutex::Autolock _l(mLock); 223 layer_state_t* s = getLayerStateLocked(client, id); 224 if (!s) 225 return BAD_INDEX; 226 s->what |= layer_state_t::ePositionChanged; 227 s->x = x; 228 s->y = y; 229 return NO_ERROR; 230} 231 232status_t Composer::setSize(const sp<SurfaceComposerClient>& client, 233 SurfaceID id, uint32_t w, uint32_t h) { 234 Mutex::Autolock _l(mLock); 235 layer_state_t* s = getLayerStateLocked(client, id); 236 if (!s) 237 return BAD_INDEX; 238 s->what |= layer_state_t::eSizeChanged; 239 s->w = w; 240 s->h = h; 241 242 // Resizing a surface makes the transaction synchronous. 243 mForceSynchronous = true; 244 245 return NO_ERROR; 246} 247 248status_t Composer::setLayer(const sp<SurfaceComposerClient>& client, 249 SurfaceID id, int32_t z) { 250 Mutex::Autolock _l(mLock); 251 layer_state_t* s = getLayerStateLocked(client, id); 252 if (!s) 253 return BAD_INDEX; 254 s->what |= layer_state_t::eLayerChanged; 255 s->z = z; 256 return NO_ERROR; 257} 258 259status_t Composer::setFlags(const sp<SurfaceComposerClient>& client, 260 SurfaceID id, uint32_t flags, 261 uint32_t mask) { 262 Mutex::Autolock _l(mLock); 263 layer_state_t* s = getLayerStateLocked(client, id); 264 if (!s) 265 return BAD_INDEX; 266 s->what |= layer_state_t::eVisibilityChanged; 267 s->flags &= ~mask; 268 s->flags |= (flags & mask); 269 s->mask |= mask; 270 return NO_ERROR; 271} 272 273status_t Composer::setTransparentRegionHint( 274 const sp<SurfaceComposerClient>& client, SurfaceID id, 275 const Region& transparentRegion) { 276 Mutex::Autolock _l(mLock); 277 layer_state_t* s = getLayerStateLocked(client, id); 278 if (!s) 279 return BAD_INDEX; 280 s->what |= layer_state_t::eTransparentRegionChanged; 281 s->transparentRegion = transparentRegion; 282 return NO_ERROR; 283} 284 285status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client, 286 SurfaceID id, float alpha) { 287 Mutex::Autolock _l(mLock); 288 layer_state_t* s = getLayerStateLocked(client, id); 289 if (!s) 290 return BAD_INDEX; 291 s->what |= layer_state_t::eAlphaChanged; 292 s->alpha = alpha; 293 return NO_ERROR; 294} 295 296status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client, 297 SurfaceID id, uint32_t layerStack) { 298 Mutex::Autolock _l(mLock); 299 layer_state_t* s = getLayerStateLocked(client, id); 300 if (!s) 301 return BAD_INDEX; 302 s->what |= layer_state_t::eLayerStackChanged; 303 s->layerStack = layerStack; 304 return NO_ERROR; 305} 306 307status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client, 308 SurfaceID id, float dsdx, float dtdx, 309 float dsdy, float dtdy) { 310 Mutex::Autolock _l(mLock); 311 layer_state_t* s = getLayerStateLocked(client, id); 312 if (!s) 313 return BAD_INDEX; 314 s->what |= layer_state_t::eMatrixChanged; 315 layer_state_t::matrix22_t matrix; 316 matrix.dsdx = dsdx; 317 matrix.dtdx = dtdx; 318 matrix.dsdy = dsdy; 319 matrix.dtdy = dtdy; 320 s->matrix = matrix; 321 return NO_ERROR; 322} 323 324status_t Composer::setCrop(const sp<SurfaceComposerClient>& client, 325 SurfaceID id, const Rect& crop) { 326 Mutex::Autolock _l(mLock); 327 layer_state_t* s = getLayerStateLocked(client, id); 328 if (!s) 329 return BAD_INDEX; 330 s->what |= layer_state_t::eCropChanged; 331 s->crop = crop; 332 return NO_ERROR; 333} 334 335// --------------------------------------------------------------------------- 336 337DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) { 338 DisplayState s; 339 s.token = token; 340 ssize_t index = mDisplayStates.indexOf(s); 341 if (index < 0) { 342 // we don't have it, add an initialized layer_state to our list 343 s.what = 0; 344 index = mDisplayStates.add(s); 345 } 346 return mDisplayStates.editItemAt(index); 347} 348 349void Composer::setDisplaySurface(const sp<IBinder>& token, 350 const sp<ISurfaceTexture>& surface) { 351 Mutex::Autolock _l(mLock); 352 DisplayState& s(getDisplayStateLocked(token)); 353 s.surface = surface; 354 s.what |= DisplayState::eSurfaceChanged; 355} 356 357void Composer::setDisplayLayerStack(const sp<IBinder>& token, 358 uint32_t layerStack) { 359 Mutex::Autolock _l(mLock); 360 DisplayState& s(getDisplayStateLocked(token)); 361 s.layerStack = layerStack; 362 s.what |= DisplayState::eLayerStackChanged; 363} 364 365void Composer::setDisplayProjection(const sp<IBinder>& token, 366 uint32_t orientation, 367 const Rect& layerStackRect, 368 const Rect& displayRect) { 369 Mutex::Autolock _l(mLock); 370 DisplayState& s(getDisplayStateLocked(token)); 371 s.orientation = orientation; 372 s.viewport = layerStackRect; 373 s.frame = displayRect; 374 s.what |= DisplayState::eDisplayProjectionChanged; 375 mForceSynchronous = true; // TODO: do we actually still need this? 376} 377 378// --------------------------------------------------------------------------- 379 380SurfaceComposerClient::SurfaceComposerClient() 381 : mStatus(NO_INIT), mComposer(Composer::getInstance()) 382{ 383} 384 385void SurfaceComposerClient::onFirstRef() { 386 sp<ISurfaceComposer> sm(ComposerService::getComposerService()); 387 if (sm != 0) { 388 sp<ISurfaceComposerClient> conn = sm->createConnection(); 389 if (conn != 0) { 390 mClient = conn; 391 mStatus = NO_ERROR; 392 } 393 } 394} 395 396SurfaceComposerClient::~SurfaceComposerClient() { 397 dispose(); 398} 399 400status_t SurfaceComposerClient::initCheck() const { 401 return mStatus; 402} 403 404sp<IBinder> SurfaceComposerClient::connection() const { 405 return (mClient != 0) ? mClient->asBinder() : 0; 406} 407 408status_t SurfaceComposerClient::linkToComposerDeath( 409 const sp<IBinder::DeathRecipient>& recipient, 410 void* cookie, uint32_t flags) { 411 sp<ISurfaceComposer> sm(ComposerService::getComposerService()); 412 return sm->asBinder()->linkToDeath(recipient, cookie, flags); 413} 414 415void SurfaceComposerClient::dispose() { 416 // this can be called more than once. 417 sp<ISurfaceComposerClient> client; 418 Mutex::Autolock _lm(mLock); 419 if (mClient != 0) { 420 client = mClient; // hold ref while lock is held 421 mClient.clear(); 422 } 423 mStatus = NO_INIT; 424} 425 426sp<SurfaceControl> SurfaceComposerClient::createSurface( 427 const String8& name, 428 uint32_t w, 429 uint32_t h, 430 PixelFormat format, 431 uint32_t flags) 432{ 433 sp<SurfaceControl> result; 434 if (mStatus == NO_ERROR) { 435 ISurfaceComposerClient::surface_data_t data; 436 sp<ISurface> surface = mClient->createSurface(&data, name, 437 w, h, format, flags); 438 if (surface != 0) { 439 result = new SurfaceControl(this, surface, data); 440 } 441 } 442 return result; 443} 444 445sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName) { 446 return Composer::getInstance().createDisplay(displayName); 447} 448 449sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) { 450 return Composer::getInstance().getBuiltInDisplay(id); 451} 452 453status_t SurfaceComposerClient::destroySurface(SurfaceID sid) { 454 if (mStatus != NO_ERROR) 455 return mStatus; 456 status_t err = mClient->destroySurface(sid); 457 return err; 458} 459 460inline Composer& SurfaceComposerClient::getComposer() { 461 return mComposer; 462} 463 464// ---------------------------------------------------------------------------- 465 466void SurfaceComposerClient::openGlobalTransaction() { 467 // Currently a no-op 468} 469 470void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) { 471 Composer::closeGlobalTransaction(synchronous); 472} 473 474// ---------------------------------------------------------------------------- 475 476status_t SurfaceComposerClient::setCrop(SurfaceID id, const Rect& crop) { 477 return getComposer().setCrop(this, id, crop); 478} 479 480status_t SurfaceComposerClient::setPosition(SurfaceID id, float x, float y) { 481 return getComposer().setPosition(this, id, x, y); 482} 483 484status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h) { 485 return getComposer().setSize(this, id, w, h); 486} 487 488status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z) { 489 return getComposer().setLayer(this, id, z); 490} 491 492status_t SurfaceComposerClient::hide(SurfaceID id) { 493 return getComposer().setFlags(this, id, 494 layer_state_t::eLayerHidden, 495 layer_state_t::eLayerHidden); 496} 497 498status_t SurfaceComposerClient::show(SurfaceID id) { 499 return getComposer().setFlags(this, id, 500 0, 501 layer_state_t::eLayerHidden); 502} 503 504status_t SurfaceComposerClient::setFlags(SurfaceID id, uint32_t flags, 505 uint32_t mask) { 506 return getComposer().setFlags(this, id, flags, mask); 507} 508 509status_t SurfaceComposerClient::setTransparentRegionHint(SurfaceID id, 510 const Region& transparentRegion) { 511 return getComposer().setTransparentRegionHint(this, id, transparentRegion); 512} 513 514status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha) { 515 return getComposer().setAlpha(this, id, alpha); 516} 517 518status_t SurfaceComposerClient::setLayerStack(SurfaceID id, uint32_t layerStack) { 519 return getComposer().setLayerStack(this, id, layerStack); 520} 521 522status_t SurfaceComposerClient::setMatrix(SurfaceID id, float dsdx, float dtdx, 523 float dsdy, float dtdy) { 524 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy); 525} 526 527// ---------------------------------------------------------------------------- 528 529void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token, 530 const sp<ISurfaceTexture>& surface) { 531 Composer::getInstance().setDisplaySurface(token, surface); 532} 533 534void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token, 535 uint32_t layerStack) { 536 Composer::getInstance().setDisplayLayerStack(token, layerStack); 537} 538 539void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token, 540 uint32_t orientation, 541 const Rect& layerStackRect, 542 const Rect& displayRect) { 543 Composer::getInstance().setDisplayProjection(token, orientation, 544 layerStackRect, displayRect); 545} 546 547// ---------------------------------------------------------------------------- 548 549status_t SurfaceComposerClient::getDisplayInfo( 550 const sp<IBinder>& display, DisplayInfo* info) 551{ 552 return ComposerService::getComposerService()->getDisplayInfo(display, info); 553} 554 555// TODO: Remove me. Do not use. 556// This is a compatibility shim for one product whose drivers are depending on 557// this legacy function (when they shouldn't). 558status_t SurfaceComposerClient::getDisplayInfo( 559 int32_t displayId, DisplayInfo* info) 560{ 561 return getDisplayInfo(getBuiltInDisplay(displayId), info); 562} 563 564// ---------------------------------------------------------------------------- 565 566ScreenshotClient::ScreenshotClient() 567 : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) { 568} 569 570status_t ScreenshotClient::update(const sp<IBinder>& display) { 571 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 572 if (s == NULL) return NO_INIT; 573 mHeap = 0; 574 return s->captureScreen(display, &mHeap, 575 &mWidth, &mHeight, &mFormat, 0, 0, 576 0, -1UL); 577} 578 579status_t ScreenshotClient::update(const sp<IBinder>& display, 580 uint32_t reqWidth, uint32_t reqHeight) { 581 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 582 if (s == NULL) return NO_INIT; 583 mHeap = 0; 584 return s->captureScreen(display, &mHeap, 585 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight, 586 0, -1UL); 587} 588 589status_t ScreenshotClient::update(const sp<IBinder>& display, 590 uint32_t reqWidth, uint32_t reqHeight, 591 uint32_t minLayerZ, uint32_t maxLayerZ) { 592 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 593 if (s == NULL) return NO_INIT; 594 mHeap = 0; 595 return s->captureScreen(display, &mHeap, 596 &mWidth, &mHeight, &mFormat, reqWidth, reqHeight, 597 minLayerZ, maxLayerZ); 598} 599 600void ScreenshotClient::release() { 601 mHeap = 0; 602} 603 604void const* ScreenshotClient::getPixels() const { 605 return mHeap->getBase(); 606} 607 608uint32_t ScreenshotClient::getWidth() const { 609 return mWidth; 610} 611 612uint32_t ScreenshotClient::getHeight() const { 613 return mHeight; 614} 615 616PixelFormat ScreenshotClient::getFormat() const { 617 return mFormat; 618} 619 620uint32_t ScreenshotClient::getStride() const { 621 return mWidth; 622} 623 624size_t ScreenshotClient::getSize() const { 625 return mHeap->getSize(); 626} 627 628// ---------------------------------------------------------------------------- 629}; // namespace android 630