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