SurfaceFlinger.cpp revision 76cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9
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#include <stdlib.h> 18#include <stdio.h> 19#include <stdint.h> 20#include <unistd.h> 21#include <fcntl.h> 22#include <errno.h> 23#include <math.h> 24#include <limits.h> 25#include <sys/types.h> 26#include <sys/stat.h> 27#include <sys/ioctl.h> 28 29#include <cutils/log.h> 30#include <cutils/properties.h> 31 32#include <binder/IPCThreadState.h> 33#include <binder/IServiceManager.h> 34#include <binder/MemoryHeapBase.h> 35 36#include <utils/String8.h> 37#include <utils/String16.h> 38#include <utils/StopWatch.h> 39 40#include <ui/GraphicBufferAllocator.h> 41#include <ui/GraphicLog.h> 42#include <ui/PixelFormat.h> 43 44#include <pixelflinger/pixelflinger.h> 45#include <GLES/gl.h> 46 47#include "clz.h" 48#include "GLExtensions.h" 49#include "Layer.h" 50#include "LayerDim.h" 51#include "SurfaceFlinger.h" 52 53#include "DisplayHardware/DisplayHardware.h" 54#include "DisplayHardware/HWComposer.h" 55 56/* ideally AID_GRAPHICS would be in a semi-public header 57 * or there would be a way to map a user/group name to its id 58 */ 59#ifndef AID_GRAPHICS 60#define AID_GRAPHICS 1003 61#endif 62 63#define DISPLAY_COUNT 1 64 65namespace android { 66// --------------------------------------------------------------------------- 67 68SurfaceFlinger::SurfaceFlinger() 69 : BnSurfaceComposer(), Thread(false), 70 mTransactionFlags(0), 71 mTransactionCount(0), 72 mResizeTransationPending(false), 73 mLayersRemoved(false), 74 mBootTime(systemTime()), 75 mHardwareTest("android.permission.HARDWARE_TEST"), 76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"), 77 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"), 78 mDump("android.permission.DUMP"), 79 mVisibleRegionsDirty(false), 80 mHwWorkListDirty(false), 81 mDeferReleaseConsole(false), 82 mFreezeDisplay(false), 83 mElectronBeamAnimationMode(0), 84 mFreezeCount(0), 85 mFreezeDisplayTime(0), 86 mDebugRegion(0), 87 mDebugBackground(0), 88 mDebugDisableHWC(0), 89 mDebugInSwapBuffers(0), 90 mLastSwapBufferTime(0), 91 mDebugInTransaction(0), 92 mLastTransactionTime(0), 93 mBootFinished(false), 94 mConsoleSignals(0), 95 mSecureFrameBuffer(0) 96{ 97 init(); 98} 99 100void SurfaceFlinger::init() 101{ 102 LOGI("SurfaceFlinger is starting"); 103 104 // debugging stuff... 105 char value[PROPERTY_VALUE_MAX]; 106 property_get("debug.sf.showupdates", value, "0"); 107 mDebugRegion = atoi(value); 108 property_get("debug.sf.showbackground", value, "0"); 109 mDebugBackground = atoi(value); 110 111 LOGI_IF(mDebugRegion, "showupdates enabled"); 112 LOGI_IF(mDebugBackground, "showbackground enabled"); 113} 114 115SurfaceFlinger::~SurfaceFlinger() 116{ 117 glDeleteTextures(1, &mWormholeTexName); 118} 119 120sp<IMemoryHeap> SurfaceFlinger::getCblk() const 121{ 122 return mServerHeap; 123} 124 125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() 126{ 127 sp<ISurfaceComposerClient> bclient; 128 sp<Client> client(new Client(this)); 129 status_t err = client->initCheck(); 130 if (err == NO_ERROR) { 131 bclient = client; 132 } 133 return bclient; 134} 135 136sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection() 137{ 138 sp<ISurfaceComposerClient> bclient; 139 sp<UserClient> client(new UserClient(this)); 140 status_t err = client->initCheck(); 141 if (err == NO_ERROR) { 142 bclient = client; 143 } 144 return bclient; 145} 146 147 148const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const 149{ 150 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy); 151 const GraphicPlane& plane(mGraphicPlanes[dpy]); 152 return plane; 153} 154 155GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) 156{ 157 return const_cast<GraphicPlane&>( 158 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy)); 159} 160 161void SurfaceFlinger::bootFinished() 162{ 163 const nsecs_t now = systemTime(); 164 const nsecs_t duration = now - mBootTime; 165 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); 166 mBootFinished = true; 167 property_set("ctl.stop", "bootanim"); 168} 169 170void SurfaceFlinger::onFirstRef() 171{ 172 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); 173 174 // Wait for the main thread to be done with its initialization 175 mReadyToRunBarrier.wait(); 176} 177 178static inline uint16_t pack565(int r, int g, int b) { 179 return (r<<11)|(g<<5)|b; 180} 181 182status_t SurfaceFlinger::readyToRun() 183{ 184 LOGI( "SurfaceFlinger's main thread ready to run. " 185 "Initializing graphics H/W..."); 186 187 // we only support one display currently 188 int dpy = 0; 189 190 { 191 // initialize the main display 192 GraphicPlane& plane(graphicPlane(dpy)); 193 DisplayHardware* const hw = new DisplayHardware(this, dpy); 194 plane.setDisplayHardware(hw); 195 } 196 197 // create the shared control-block 198 mServerHeap = new MemoryHeapBase(4096, 199 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap"); 200 LOGE_IF(mServerHeap==0, "can't create shared memory dealer"); 201 202 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase()); 203 LOGE_IF(mServerCblk==0, "can't get to shared control block's address"); 204 205 new(mServerCblk) surface_flinger_cblk_t; 206 207 // initialize primary screen 208 // (other display should be initialized in the same manner, but 209 // asynchronously, as they could come and go. None of this is supported 210 // yet). 211 const GraphicPlane& plane(graphicPlane(dpy)); 212 const DisplayHardware& hw = plane.displayHardware(); 213 const uint32_t w = hw.getWidth(); 214 const uint32_t h = hw.getHeight(); 215 const uint32_t f = hw.getFormat(); 216 hw.makeCurrent(); 217 218 // initialize the shared control block 219 mServerCblk->connected |= 1<<dpy; 220 display_cblk_t* dcblk = mServerCblk->displays + dpy; 221 memset(dcblk, 0, sizeof(display_cblk_t)); 222 dcblk->w = plane.getWidth(); 223 dcblk->h = plane.getHeight(); 224 dcblk->format = f; 225 dcblk->orientation = ISurfaceComposer::eOrientationDefault; 226 dcblk->xdpi = hw.getDpiX(); 227 dcblk->ydpi = hw.getDpiY(); 228 dcblk->fps = hw.getRefreshRate(); 229 dcblk->density = hw.getDensity(); 230 231 // Initialize OpenGL|ES 232 glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 233 glPixelStorei(GL_PACK_ALIGNMENT, 4); 234 glEnableClientState(GL_VERTEX_ARRAY); 235 glEnable(GL_SCISSOR_TEST); 236 glShadeModel(GL_FLAT); 237 glDisable(GL_DITHER); 238 glDisable(GL_CULL_FACE); 239 240 const uint16_t g0 = pack565(0x0F,0x1F,0x0F); 241 const uint16_t g1 = pack565(0x17,0x2f,0x17); 242 const uint16_t textureData[4] = { g0, g1, g1, g0 }; 243 glGenTextures(1, &mWormholeTexName); 244 glBindTexture(GL_TEXTURE_2D, mWormholeTexName); 245 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 246 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 247 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 248 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, 250 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData); 251 252 glViewport(0, 0, w, h); 253 glMatrixMode(GL_PROJECTION); 254 glLoadIdentity(); 255 glOrthof(0, w, h, 0, 0, 1); 256 257 LayerDim::initDimmer(this, w, h); 258 259 mReadyToRunBarrier.open(); 260 261 /* 262 * We're now ready to accept clients... 263 */ 264 265 // start boot animation 266 property_set("ctl.start", "bootanim"); 267 268 return NO_ERROR; 269} 270 271// ---------------------------------------------------------------------------- 272#if 0 273#pragma mark - 274#pragma mark Events Handler 275#endif 276 277void SurfaceFlinger::waitForEvent() 278{ 279 while (true) { 280 nsecs_t timeout = -1; 281 const nsecs_t freezeDisplayTimeout = ms2ns(5000); 282 if (UNLIKELY(isFrozen())) { 283 // wait 5 seconds 284 const nsecs_t now = systemTime(); 285 if (mFreezeDisplayTime == 0) { 286 mFreezeDisplayTime = now; 287 } 288 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime); 289 timeout = waitTime>0 ? waitTime : 0; 290 } 291 292 sp<MessageBase> msg = mEventQueue.waitMessage(timeout); 293 294 // see if we timed out 295 if (isFrozen()) { 296 const nsecs_t now = systemTime(); 297 nsecs_t frozenTime = (now - mFreezeDisplayTime); 298 if (frozenTime >= freezeDisplayTimeout) { 299 // we timed out and are still frozen 300 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d", 301 mFreezeDisplay, mFreezeCount); 302 mFreezeDisplayTime = 0; 303 mFreezeCount = 0; 304 mFreezeDisplay = false; 305 } 306 } 307 308 if (msg != 0) { 309 switch (msg->what) { 310 case MessageQueue::INVALIDATE: 311 // invalidate message, just return to the main loop 312 return; 313 } 314 } 315 } 316} 317 318void SurfaceFlinger::signalEvent() { 319 mEventQueue.invalidate(); 320} 321 322void SurfaceFlinger::signal() const { 323 // this is the IPC call 324 const_cast<SurfaceFlinger*>(this)->signalEvent(); 325} 326 327status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, 328 nsecs_t reltime, uint32_t flags) 329{ 330 return mEventQueue.postMessage(msg, reltime, flags); 331} 332 333status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, 334 nsecs_t reltime, uint32_t flags) 335{ 336 status_t res = mEventQueue.postMessage(msg, reltime, flags); 337 if (res == NO_ERROR) { 338 msg->wait(); 339 } 340 return res; 341} 342 343// ---------------------------------------------------------------------------- 344#if 0 345#pragma mark - 346#pragma mark Main loop 347#endif 348 349bool SurfaceFlinger::threadLoop() 350{ 351 waitForEvent(); 352 353 // check for transactions 354 if (UNLIKELY(mConsoleSignals)) { 355 handleConsoleEvents(); 356 } 357 358 if (LIKELY(mTransactionCount == 0)) { 359 // if we're in a global transaction, don't do anything. 360 const uint32_t mask = eTransactionNeeded | eTraversalNeeded; 361 uint32_t transactionFlags = getTransactionFlags(mask); 362 if (LIKELY(transactionFlags)) { 363 handleTransaction(transactionFlags); 364 } 365 } 366 367 // post surfaces (if needed) 368 handlePageFlip(); 369 370 if (UNLIKELY(mHwWorkListDirty)) { 371 // build the h/w work list 372 handleWorkList(); 373 } 374 375 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 376 if (LIKELY(hw.canDraw() && !isFrozen())) { 377 // repaint the framebuffer (if needed) 378 379 const int index = hw.getCurrentBufferIndex(); 380 GraphicLog& logger(GraphicLog::getInstance()); 381 382 logger.log(GraphicLog::SF_REPAINT, index); 383 handleRepaint(); 384 385 // inform the h/w that we're done compositing 386 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index); 387 hw.compositionComplete(); 388 389 logger.log(GraphicLog::SF_SWAP_BUFFERS, index); 390 postFramebuffer(); 391 392 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index); 393 unlockClients(); 394 395 logger.log(GraphicLog::SF_REPAINT_DONE, index); 396 } else { 397 // pretend we did the post 398 hw.compositionComplete(); 399 unlockClients(); 400 usleep(16667); // 60 fps period 401 } 402 return true; 403} 404 405void SurfaceFlinger::postFramebuffer() 406{ 407 if (!mInvalidRegion.isEmpty()) { 408 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 409 const nsecs_t now = systemTime(); 410 mDebugInSwapBuffers = now; 411 hw.flip(mInvalidRegion); 412 mLastSwapBufferTime = systemTime() - now; 413 mDebugInSwapBuffers = 0; 414 mInvalidRegion.clear(); 415 } 416} 417 418void SurfaceFlinger::handleConsoleEvents() 419{ 420 // something to do with the console 421 const DisplayHardware& hw = graphicPlane(0).displayHardware(); 422 423 int what = android_atomic_and(0, &mConsoleSignals); 424 if (what & eConsoleAcquired) { 425 hw.acquireScreen(); 426 // this is a temporary work-around, eventually this should be called 427 // by the power-manager 428 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode); 429 } 430 431 if (mDeferReleaseConsole && hw.isScreenAcquired()) { 432 // We got the release signal before the acquire signal 433 mDeferReleaseConsole = false; 434 hw.releaseScreen(); 435 } 436 437 if (what & eConsoleReleased) { 438 if (hw.isScreenAcquired()) { 439 hw.releaseScreen(); 440 } else { 441 mDeferReleaseConsole = true; 442 } 443 } 444 445 mDirtyRegion.set(hw.bounds()); 446} 447 448void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) 449{ 450 Vector< sp<LayerBase> > ditchedLayers; 451 452 /* 453 * Perform and commit the transaction 454 */ 455 456 { // scope for the lock 457 Mutex::Autolock _l(mStateLock); 458 const nsecs_t now = systemTime(); 459 mDebugInTransaction = now; 460 handleTransactionLocked(transactionFlags, ditchedLayers); 461 mLastTransactionTime = systemTime() - now; 462 mDebugInTransaction = 0; 463 mHwWorkListDirty = true; 464 // here the transaction has been committed 465 } 466 467 /* 468 * Clean-up all layers that went away 469 * (do this without the lock held) 470 */ 471 472 const size_t count = ditchedLayers.size(); 473 for (size_t i=0 ; i<count ; i++) { 474 if (ditchedLayers[i] != 0) { 475 //LOGD("ditching layer %p", ditchedLayers[i].get()); 476 ditchedLayers[i]->ditch(); 477 } 478 } 479} 480 481void SurfaceFlinger::handleTransactionLocked( 482 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers) 483{ 484 const LayerVector& currentLayers(mCurrentState.layersSortedByZ); 485 const size_t count = currentLayers.size(); 486 487 /* 488 * Traversal of the children 489 * (perform the transaction for each of them if needed) 490 */ 491 492 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded; 493 if (layersNeedTransaction) { 494 for (size_t i=0 ; i<count ; i++) { 495 const sp<LayerBase>& layer = currentLayers[i]; 496 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); 497 if (!trFlags) continue; 498 499 const uint32_t flags = layer->doTransaction(0); 500 if (flags & Layer::eVisibleRegion) 501 mVisibleRegionsDirty = true; 502 } 503 } 504 505 /* 506 * Perform our own transaction if needed 507 */ 508 509 if (transactionFlags & eTransactionNeeded) { 510 if (mCurrentState.orientation != mDrawingState.orientation) { 511 // the orientation has changed, recompute all visible regions 512 // and invalidate everything. 513 514 const int dpy = 0; 515 const int orientation = mCurrentState.orientation; 516 const uint32_t type = mCurrentState.orientationType; 517 GraphicPlane& plane(graphicPlane(dpy)); 518 plane.setOrientation(orientation); 519 520 // update the shared control block 521 const DisplayHardware& hw(plane.displayHardware()); 522 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy; 523 dcblk->orientation = orientation; 524 dcblk->w = plane.getWidth(); 525 dcblk->h = plane.getHeight(); 526 527 mVisibleRegionsDirty = true; 528 mDirtyRegion.set(hw.bounds()); 529 } 530 531 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) { 532 // freezing or unfreezing the display -> trigger animation if needed 533 mFreezeDisplay = mCurrentState.freezeDisplay; 534 if (mFreezeDisplay) 535 mFreezeDisplayTime = 0; 536 } 537 538 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) { 539 // layers have been added 540 mVisibleRegionsDirty = true; 541 } 542 543 // some layers might have been removed, so 544 // we need to update the regions they're exposing. 545 if (mLayersRemoved) { 546 mLayersRemoved = false; 547 mVisibleRegionsDirty = true; 548 const LayerVector& previousLayers(mDrawingState.layersSortedByZ); 549 const size_t count = previousLayers.size(); 550 for (size_t i=0 ; i<count ; i++) { 551 const sp<LayerBase>& layer(previousLayers[i]); 552 if (currentLayers.indexOf( layer ) < 0) { 553 // this layer is not visible anymore 554 ditchedLayers.add(layer); 555 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen); 556 } 557 } 558 } 559 } 560 561 commitTransaction(); 562} 563 564sp<FreezeLock> SurfaceFlinger::getFreezeLock() const 565{ 566 return new FreezeLock(const_cast<SurfaceFlinger *>(this)); 567} 568 569void SurfaceFlinger::computeVisibleRegions( 570 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion) 571{ 572 const GraphicPlane& plane(graphicPlane(0)); 573 const Transform& planeTransform(plane.transform()); 574 const DisplayHardware& hw(plane.displayHardware()); 575 const Region screenRegion(hw.bounds()); 576 577 Region aboveOpaqueLayers; 578 Region aboveCoveredLayers; 579 Region dirty; 580 581 bool secureFrameBuffer = false; 582 583 size_t i = currentLayers.size(); 584 while (i--) { 585 const sp<LayerBase>& layer = currentLayers[i]; 586 layer->validateVisibility(planeTransform); 587 588 // start with the whole surface at its current location 589 const Layer::State& s(layer->drawingState()); 590 591 /* 592 * opaqueRegion: area of a surface that is fully opaque. 593 */ 594 Region opaqueRegion; 595 596 /* 597 * visibleRegion: area of a surface that is visible on screen 598 * and not fully transparent. This is essentially the layer's 599 * footprint minus the opaque regions above it. 600 * Areas covered by a translucent surface are considered visible. 601 */ 602 Region visibleRegion; 603 604 /* 605 * coveredRegion: area of a surface that is covered by all 606 * visible regions above it (which includes the translucent areas). 607 */ 608 Region coveredRegion; 609 610 611 // handle hidden surfaces by setting the visible region to empty 612 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) { 613 const bool translucent = layer->needsBlending(); 614 const Rect bounds(layer->visibleBounds()); 615 visibleRegion.set(bounds); 616 visibleRegion.andSelf(screenRegion); 617 if (!visibleRegion.isEmpty()) { 618 // Remove the transparent area from the visible region 619 if (translucent) { 620 visibleRegion.subtractSelf(layer->transparentRegionScreen); 621 } 622 623 // compute the opaque region 624 const int32_t layerOrientation = layer->getOrientation(); 625 if (s.alpha==255 && !translucent && 626 ((layerOrientation & Transform::ROT_INVALID) == false)) { 627 // the opaque region is the layer's footprint 628 opaqueRegion = visibleRegion; 629 } 630 } 631 } 632 633 // Clip the covered region to the visible region 634 coveredRegion = aboveCoveredLayers.intersect(visibleRegion); 635 636 // Update aboveCoveredLayers for next (lower) layer 637 aboveCoveredLayers.orSelf(visibleRegion); 638 639 // subtract the opaque region covered by the layers above us 640 visibleRegion.subtractSelf(aboveOpaqueLayers); 641 642 // compute this layer's dirty region 643 if (layer->contentDirty) { 644 // we need to invalidate the whole region 645 dirty = visibleRegion; 646 // as well, as the old visible region 647 dirty.orSelf(layer->visibleRegionScreen); 648 layer->contentDirty = false; 649 } else { 650 /* compute the exposed region: 651 * the exposed region consists of two components: 652 * 1) what's VISIBLE now and was COVERED before 653 * 2) what's EXPOSED now less what was EXPOSED before 654 * 655 * note that (1) is conservative, we start with the whole 656 * visible region but only keep what used to be covered by 657 * something -- which mean it may have been exposed. 658 * 659 * (2) handles areas that were not covered by anything but got 660 * exposed because of a resize. 661 */ 662 const Region newExposed = visibleRegion - coveredRegion; 663 const Region oldVisibleRegion = layer->visibleRegionScreen; 664 const Region oldCoveredRegion = layer->coveredRegionScreen; 665 const Region oldExposed = oldVisibleRegion - oldCoveredRegion; 666 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); 667 } 668 dirty.subtractSelf(aboveOpaqueLayers); 669 670 // accumulate to the screen dirty region 671 dirtyRegion.orSelf(dirty); 672 673 // Update aboveOpaqueLayers for next (lower) layer 674 aboveOpaqueLayers.orSelf(opaqueRegion); 675 676 // Store the visible region is screen space 677 layer->setVisibleRegion(visibleRegion); 678 layer->setCoveredRegion(coveredRegion); 679 680 // If a secure layer is partially visible, lock-down the screen! 681 if (layer->isSecure() && !visibleRegion.isEmpty()) { 682 secureFrameBuffer = true; 683 } 684 } 685 686 // invalidate the areas where a layer was removed 687 dirtyRegion.orSelf(mDirtyRegionRemovedLayer); 688 mDirtyRegionRemovedLayer.clear(); 689 690 mSecureFrameBuffer = secureFrameBuffer; 691 opaqueRegion = aboveOpaqueLayers; 692} 693 694 695void SurfaceFlinger::commitTransaction() 696{ 697 mDrawingState = mCurrentState; 698 mResizeTransationPending = false; 699 mTransactionCV.broadcast(); 700} 701 702void SurfaceFlinger::handlePageFlip() 703{ 704 bool visibleRegions = mVisibleRegionsDirty; 705 LayerVector& currentLayers( 706 const_cast<LayerVector&>(mDrawingState.layersSortedByZ)); 707 visibleRegions |= lockPageFlip(currentLayers); 708 709 const DisplayHardware& hw = graphicPlane(0).displayHardware(); 710 const Region screenRegion(hw.bounds()); 711 if (visibleRegions) { 712 Region opaqueRegion; 713 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion); 714 715 /* 716 * rebuild the visible layer list 717 */ 718 mVisibleLayersSortedByZ.clear(); 719 const LayerVector& currentLayers(mDrawingState.layersSortedByZ); 720 size_t count = currentLayers.size(); 721 mVisibleLayersSortedByZ.setCapacity(count); 722 for (size_t i=0 ; i<count ; i++) { 723 if (!currentLayers[i]->visibleRegionScreen.isEmpty()) 724 mVisibleLayersSortedByZ.add(currentLayers[i]); 725 } 726 727 mWormholeRegion = screenRegion.subtract(opaqueRegion); 728 mVisibleRegionsDirty = false; 729 mHwWorkListDirty = true; 730 } 731 732 unlockPageFlip(currentLayers); 733 mDirtyRegion.andSelf(screenRegion); 734} 735 736bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers) 737{ 738 bool recomputeVisibleRegions = false; 739 size_t count = currentLayers.size(); 740 sp<LayerBase> const* layers = currentLayers.array(); 741 for (size_t i=0 ; i<count ; i++) { 742 const sp<LayerBase>& layer(layers[i]); 743 layer->lockPageFlip(recomputeVisibleRegions); 744 } 745 return recomputeVisibleRegions; 746} 747 748void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers) 749{ 750 const GraphicPlane& plane(graphicPlane(0)); 751 const Transform& planeTransform(plane.transform()); 752 size_t count = currentLayers.size(); 753 sp<LayerBase> const* layers = currentLayers.array(); 754 for (size_t i=0 ; i<count ; i++) { 755 const sp<LayerBase>& layer(layers[i]); 756 layer->unlockPageFlip(planeTransform, mDirtyRegion); 757 } 758} 759 760void SurfaceFlinger::handleWorkList() 761{ 762 mHwWorkListDirty = false; 763 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer()); 764 if (hwc.initCheck() == NO_ERROR) { 765 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ); 766 const size_t count = currentLayers.size(); 767 hwc.createWorkList(count); 768 hwc_layer_t* const cur(hwc.getLayers()); 769 for (size_t i=0 ; cur && i<count ; i++) { 770 currentLayers[i]->setGeometry(&cur[i]); 771 if (mDebugDisableHWC) { 772 cur[i].compositionType = HWC_FRAMEBUFFER; 773 cur[i].flags |= HWC_SKIP_LAYER; 774 } 775 } 776 } 777} 778 779void SurfaceFlinger::handleRepaint() 780{ 781 // compute the invalid region 782 mInvalidRegion.orSelf(mDirtyRegion); 783 784 if (UNLIKELY(mDebugRegion)) { 785 debugFlashRegions(); 786 } 787 788 // set the frame buffer 789 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 790 glMatrixMode(GL_MODELVIEW); 791 glLoadIdentity(); 792 793 uint32_t flags = hw.getFlags(); 794 if ((flags & DisplayHardware::SWAP_RECTANGLE) || 795 (flags & DisplayHardware::BUFFER_PRESERVED)) 796 { 797 // we can redraw only what's dirty, but since SWAP_RECTANGLE only 798 // takes a rectangle, we must make sure to update that whole 799 // rectangle in that case 800 if (flags & DisplayHardware::SWAP_RECTANGLE) { 801 // TODO: we really should be able to pass a region to 802 // SWAP_RECTANGLE so that we don't have to redraw all this. 803 mDirtyRegion.set(mInvalidRegion.bounds()); 804 } else { 805 // in the BUFFER_PRESERVED case, obviously, we can update only 806 // what's needed and nothing more. 807 // NOTE: this is NOT a common case, as preserving the backbuffer 808 // is costly and usually involves copying the whole update back. 809 } 810 } else { 811 if (flags & DisplayHardware::PARTIAL_UPDATES) { 812 // We need to redraw the rectangle that will be updated 813 // (pushed to the framebuffer). 814 // This is needed because PARTIAL_UPDATES only takes one 815 // rectangle instead of a region (see DisplayHardware::flip()) 816 mDirtyRegion.set(mInvalidRegion.bounds()); 817 } else { 818 // we need to redraw everything (the whole screen) 819 mDirtyRegion.set(hw.bounds()); 820 mInvalidRegion = mDirtyRegion; 821 } 822 } 823 824 // compose all surfaces 825 composeSurfaces(mDirtyRegion); 826 827 // clear the dirty regions 828 mDirtyRegion.clear(); 829} 830 831void SurfaceFlinger::composeSurfaces(const Region& dirty) 832{ 833 if (UNLIKELY(!mWormholeRegion.isEmpty())) { 834 // should never happen unless the window manager has a bug 835 // draw something... 836 drawWormhole(); 837 } 838 839 status_t err = NO_ERROR; 840 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); 841 size_t count = layers.size(); 842 843 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 844 HWComposer& hwc(hw.getHwComposer()); 845 hwc_layer_t* const cur(hwc.getLayers()); 846 847 LOGE_IF(cur && hwc.getNumLayers() != count, 848 "HAL number of layers (%d) doesn't match surfaceflinger (%d)", 849 hwc.getNumLayers(), count); 850 851 // just to be extra-safe, use the smallest count 852 if (hwc.initCheck() == NO_ERROR) { 853 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers(); 854 } 855 856 /* 857 * update the per-frame h/w composer data for each layer 858 * and build the transparent region of the FB 859 */ 860 Region transparent; 861 if (cur) { 862 for (size_t i=0 ; i<count ; i++) { 863 const sp<LayerBase>& layer(layers[i]); 864 layer->setPerFrameData(&cur[i]); 865 if (cur[i].hints & HWC_HINT_CLEAR_FB) { 866 if (!(layer->needsBlending())) { 867 transparent.orSelf(layer->visibleRegionScreen); 868 } 869 } 870 } 871 err = hwc.prepare(); 872 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); 873 } 874 875 /* 876 * clear the area of the FB that need to be transparent 877 */ 878 transparent.andSelf(dirty); 879 if (!transparent.isEmpty()) { 880 glClearColor(0,0,0,0); 881 Region::const_iterator it = transparent.begin(); 882 Region::const_iterator const end = transparent.end(); 883 const int32_t height = hw.getHeight(); 884 while (it != end) { 885 const Rect& r(*it++); 886 const GLint sy = height - (r.top + r.height()); 887 glScissor(r.left, sy, r.width(), r.height()); 888 glClear(GL_COLOR_BUFFER_BIT); 889 } 890 } 891 892 893 /* 894 * and then, render the layers targeted at the framebuffer 895 */ 896 for (size_t i=0 ; i<count ; i++) { 897 if (cur) { 898 if ((cur[i].compositionType != HWC_FRAMEBUFFER) && 899 !(cur[i].flags & HWC_SKIP_LAYER)) { 900 // skip layers handled by the HAL 901 continue; 902 } 903 } 904 905 const sp<LayerBase>& layer(layers[i]); 906 const Region clip(dirty.intersect(layer->visibleRegionScreen)); 907 if (!clip.isEmpty()) { 908 layer->draw(clip); 909 } 910 } 911} 912 913void SurfaceFlinger::unlockClients() 914{ 915 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ); 916 const size_t count = drawingLayers.size(); 917 sp<LayerBase> const* const layers = drawingLayers.array(); 918 for (size_t i=0 ; i<count ; ++i) { 919 const sp<LayerBase>& layer = layers[i]; 920 layer->finishPageFlip(); 921 } 922} 923 924void SurfaceFlinger::debugFlashRegions() 925{ 926 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 927 const uint32_t flags = hw.getFlags(); 928 929 if (!((flags & DisplayHardware::SWAP_RECTANGLE) || 930 (flags & DisplayHardware::BUFFER_PRESERVED))) { 931 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ? 932 mDirtyRegion.bounds() : hw.bounds()); 933 composeSurfaces(repaint); 934 } 935 936 TextureManager::deactivateTextures(); 937 938 glDisable(GL_BLEND); 939 glDisable(GL_DITHER); 940 glDisable(GL_SCISSOR_TEST); 941 942 static int toggle = 0; 943 toggle = 1 - toggle; 944 if (toggle) { 945 glColor4f(1, 0, 1, 1); 946 } else { 947 glColor4f(1, 1, 0, 1); 948 } 949 950 Region::const_iterator it = mDirtyRegion.begin(); 951 Region::const_iterator const end = mDirtyRegion.end(); 952 while (it != end) { 953 const Rect& r = *it++; 954 GLfloat vertices[][2] = { 955 { r.left, r.top }, 956 { r.left, r.bottom }, 957 { r.right, r.bottom }, 958 { r.right, r.top } 959 }; 960 glVertexPointer(2, GL_FLOAT, 0, vertices); 961 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 962 } 963 964 if (mInvalidRegion.isEmpty()) { 965 mDirtyRegion.dump("mDirtyRegion"); 966 mInvalidRegion.dump("mInvalidRegion"); 967 } 968 hw.flip(mInvalidRegion); 969 970 if (mDebugRegion > 1) 971 usleep(mDebugRegion * 1000); 972 973 glEnable(GL_SCISSOR_TEST); 974 //mDirtyRegion.dump("mDirtyRegion"); 975} 976 977void SurfaceFlinger::drawWormhole() const 978{ 979 const Region region(mWormholeRegion.intersect(mDirtyRegion)); 980 if (region.isEmpty()) 981 return; 982 983 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 984 const int32_t width = hw.getWidth(); 985 const int32_t height = hw.getHeight(); 986 987 glDisable(GL_BLEND); 988 glDisable(GL_DITHER); 989 990 if (LIKELY(!mDebugBackground)) { 991 glClearColor(0,0,0,0); 992 Region::const_iterator it = region.begin(); 993 Region::const_iterator const end = region.end(); 994 while (it != end) { 995 const Rect& r = *it++; 996 const GLint sy = height - (r.top + r.height()); 997 glScissor(r.left, sy, r.width(), r.height()); 998 glClear(GL_COLOR_BUFFER_BIT); 999 } 1000 } else { 1001 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 }, 1002 { width, height }, { 0, height } }; 1003 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } }; 1004 glVertexPointer(2, GL_SHORT, 0, vertices); 1005 glTexCoordPointer(2, GL_SHORT, 0, tcoords); 1006 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1007#if defined(GL_OES_EGL_image_external) 1008 if (GLExtensions::getInstance().haveTextureExternal()) { 1009 glDisable(GL_TEXTURE_EXTERNAL_OES); 1010 } 1011#endif 1012 glEnable(GL_TEXTURE_2D); 1013 glBindTexture(GL_TEXTURE_2D, mWormholeTexName); 1014 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 1015 glMatrixMode(GL_TEXTURE); 1016 glLoadIdentity(); 1017 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1); 1018 Region::const_iterator it = region.begin(); 1019 Region::const_iterator const end = region.end(); 1020 while (it != end) { 1021 const Rect& r = *it++; 1022 const GLint sy = height - (r.top + r.height()); 1023 glScissor(r.left, sy, r.width(), r.height()); 1024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1025 } 1026 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1027 glLoadIdentity(); 1028 glMatrixMode(GL_MODELVIEW); 1029 } 1030} 1031 1032void SurfaceFlinger::debugShowFPS() const 1033{ 1034 static int mFrameCount; 1035 static int mLastFrameCount = 0; 1036 static nsecs_t mLastFpsTime = 0; 1037 static float mFps = 0; 1038 mFrameCount++; 1039 nsecs_t now = systemTime(); 1040 nsecs_t diff = now - mLastFpsTime; 1041 if (diff > ms2ns(250)) { 1042 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff; 1043 mLastFpsTime = now; 1044 mLastFrameCount = mFrameCount; 1045 } 1046 // XXX: mFPS has the value we want 1047 } 1048 1049status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer) 1050{ 1051 Mutex::Autolock _l(mStateLock); 1052 addLayer_l(layer); 1053 setTransactionFlags(eTransactionNeeded|eTraversalNeeded); 1054 return NO_ERROR; 1055} 1056 1057status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer) 1058{ 1059 ssize_t i = mCurrentState.layersSortedByZ.add(layer); 1060 return (i < 0) ? status_t(i) : status_t(NO_ERROR); 1061} 1062 1063ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client, 1064 const sp<LayerBaseClient>& lbc) 1065{ 1066 Mutex::Autolock _l(mStateLock); 1067 1068 // attach this layer to the client 1069 ssize_t name = client->attachLayer(lbc); 1070 1071 // add this layer to the current state list 1072 addLayer_l(lbc); 1073 1074 return name; 1075} 1076 1077status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer) 1078{ 1079 Mutex::Autolock _l(mStateLock); 1080 status_t err = purgatorizeLayer_l(layer); 1081 if (err == NO_ERROR) 1082 setTransactionFlags(eTransactionNeeded); 1083 return err; 1084} 1085 1086status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase) 1087{ 1088 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient()); 1089 if (lbc != 0) { 1090 mLayerMap.removeItem( lbc->getSurface()->asBinder() ); 1091 } 1092 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase); 1093 if (index >= 0) { 1094 mLayersRemoved = true; 1095 return NO_ERROR; 1096 } 1097 return status_t(index); 1098} 1099 1100status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase) 1101{ 1102 // First add the layer to the purgatory list, which makes sure it won't 1103 // go away, then remove it from the main list (through a transaction). 1104 ssize_t err = removeLayer_l(layerBase); 1105 if (err >= 0) { 1106 mLayerPurgatory.add(layerBase); 1107 } 1108 1109 layerBase->onRemoved(); 1110 1111 // it's possible that we don't find a layer, because it might 1112 // have been destroyed already -- this is not technically an error 1113 // from the user because there is a race between Client::destroySurface(), 1114 // ~Client() and ~ISurface(). 1115 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err; 1116} 1117 1118status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer) 1119{ 1120 layer->forceVisibilityTransaction(); 1121 setTransactionFlags(eTraversalNeeded); 1122 return NO_ERROR; 1123} 1124 1125uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) 1126{ 1127 return android_atomic_and(~flags, &mTransactionFlags) & flags; 1128} 1129 1130uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) 1131{ 1132 uint32_t old = android_atomic_or(flags, &mTransactionFlags); 1133 if ((old & flags)==0) { // wake the server up 1134 signalEvent(); 1135 } 1136 return old; 1137} 1138 1139void SurfaceFlinger::openGlobalTransaction() 1140{ 1141 android_atomic_inc(&mTransactionCount); 1142} 1143 1144void SurfaceFlinger::closeGlobalTransaction() 1145{ 1146 if (android_atomic_dec(&mTransactionCount) == 1) { 1147 signalEvent(); 1148 1149 // if there is a transaction with a resize, wait for it to 1150 // take effect before returning. 1151 Mutex::Autolock _l(mStateLock); 1152 while (mResizeTransationPending) { 1153 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); 1154 if (CC_UNLIKELY(err != NO_ERROR)) { 1155 // just in case something goes wrong in SF, return to the 1156 // called after a few seconds. 1157 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!"); 1158 mResizeTransationPending = false; 1159 break; 1160 } 1161 } 1162 } 1163} 1164 1165status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags) 1166{ 1167 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) 1168 return BAD_VALUE; 1169 1170 Mutex::Autolock _l(mStateLock); 1171 mCurrentState.freezeDisplay = 1; 1172 setTransactionFlags(eTransactionNeeded); 1173 1174 // flags is intended to communicate some sort of animation behavior 1175 // (for instance fading) 1176 return NO_ERROR; 1177} 1178 1179status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags) 1180{ 1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) 1182 return BAD_VALUE; 1183 1184 Mutex::Autolock _l(mStateLock); 1185 mCurrentState.freezeDisplay = 0; 1186 setTransactionFlags(eTransactionNeeded); 1187 1188 // flags is intended to communicate some sort of animation behavior 1189 // (for instance fading) 1190 return NO_ERROR; 1191} 1192 1193int SurfaceFlinger::setOrientation(DisplayID dpy, 1194 int orientation, uint32_t flags) 1195{ 1196 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) 1197 return BAD_VALUE; 1198 1199 Mutex::Autolock _l(mStateLock); 1200 if (mCurrentState.orientation != orientation) { 1201 if (uint32_t(orientation)<=eOrientation270 || orientation==42) { 1202 mCurrentState.orientationType = flags; 1203 mCurrentState.orientation = orientation; 1204 setTransactionFlags(eTransactionNeeded); 1205 mTransactionCV.wait(mStateLock); 1206 } else { 1207 orientation = BAD_VALUE; 1208 } 1209 } 1210 return orientation; 1211} 1212 1213sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid, 1214 const String8& name, ISurfaceComposerClient::surface_data_t* params, 1215 DisplayID d, uint32_t w, uint32_t h, PixelFormat format, 1216 uint32_t flags) 1217{ 1218 sp<LayerBaseClient> layer; 1219 sp<LayerBaseClient::Surface> surfaceHandle; 1220 1221 if (int32_t(w|h) < 0) { 1222 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)", 1223 int(w), int(h)); 1224 return surfaceHandle; 1225 } 1226 1227 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h); 1228 sp<Layer> normalLayer; 1229 switch (flags & eFXSurfaceMask) { 1230 case eFXSurfaceNormal: 1231 normalLayer = createNormalSurface(client, d, w, h, flags, format); 1232 layer = normalLayer; 1233 break; 1234 case eFXSurfaceBlur: 1235 // for now we treat Blur as Dim, until we can implement it 1236 // efficiently. 1237 case eFXSurfaceDim: 1238 layer = createDimSurface(client, d, w, h, flags); 1239 break; 1240 } 1241 1242 if (layer != 0) { 1243 layer->initStates(w, h, flags); 1244 layer->setName(name); 1245 ssize_t token = addClientLayer(client, layer); 1246 1247 surfaceHandle = layer->getSurface(); 1248 if (surfaceHandle != 0) { 1249 params->token = token; 1250 params->identity = surfaceHandle->getIdentity(); 1251 params->width = w; 1252 params->height = h; 1253 params->format = format; 1254 if (normalLayer != 0) { 1255 Mutex::Autolock _l(mStateLock); 1256 mLayerMap.add(surfaceHandle->asBinder(), normalLayer); 1257 } 1258 } 1259 1260 setTransactionFlags(eTransactionNeeded); 1261 } 1262 1263 return surfaceHandle; 1264} 1265 1266sp<Layer> SurfaceFlinger::createNormalSurface( 1267 const sp<Client>& client, DisplayID display, 1268 uint32_t w, uint32_t h, uint32_t flags, 1269 PixelFormat& format) 1270{ 1271 // initialize the surfaces 1272 switch (format) { // TODO: take h/w into account 1273 case PIXEL_FORMAT_TRANSPARENT: 1274 case PIXEL_FORMAT_TRANSLUCENT: 1275 format = PIXEL_FORMAT_RGBA_8888; 1276 break; 1277 case PIXEL_FORMAT_OPAQUE: 1278#ifdef NO_RGBX_8888 1279 format = PIXEL_FORMAT_RGB_565; 1280#else 1281 format = PIXEL_FORMAT_RGBX_8888; 1282#endif 1283 break; 1284 } 1285 1286#ifdef NO_RGBX_8888 1287 if (format == PIXEL_FORMAT_RGBX_8888) 1288 format = PIXEL_FORMAT_RGBA_8888; 1289#endif 1290 1291 sp<Layer> layer = new Layer(this, display, client); 1292 status_t err = layer->setBuffers(w, h, format, flags); 1293 if (LIKELY(err != NO_ERROR)) { 1294 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err)); 1295 layer.clear(); 1296 } 1297 return layer; 1298} 1299 1300sp<LayerDim> SurfaceFlinger::createDimSurface( 1301 const sp<Client>& client, DisplayID display, 1302 uint32_t w, uint32_t h, uint32_t flags) 1303{ 1304 sp<LayerDim> layer = new LayerDim(this, display, client); 1305 layer->initStates(w, h, flags); 1306 return layer; 1307} 1308 1309status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid) 1310{ 1311 /* 1312 * called by the window manager, when a surface should be marked for 1313 * destruction. 1314 * 1315 * The surface is removed from the current and drawing lists, but placed 1316 * in the purgatory queue, so it's not destroyed right-away (we need 1317 * to wait for all client's references to go away first). 1318 */ 1319 1320 status_t err = NAME_NOT_FOUND; 1321 Mutex::Autolock _l(mStateLock); 1322 sp<LayerBaseClient> layer = client->getLayerUser(sid); 1323 if (layer != 0) { 1324 err = purgatorizeLayer_l(layer); 1325 if (err == NO_ERROR) { 1326 setTransactionFlags(eTransactionNeeded); 1327 } 1328 } 1329 return err; 1330} 1331 1332status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer) 1333{ 1334 // called by ~ISurface() when all references are gone 1335 1336 class MessageDestroySurface : public MessageBase { 1337 SurfaceFlinger* flinger; 1338 sp<LayerBaseClient> layer; 1339 public: 1340 MessageDestroySurface( 1341 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer) 1342 : flinger(flinger), layer(layer) { } 1343 virtual bool handler() { 1344 sp<LayerBaseClient> l(layer); 1345 layer.clear(); // clear it outside of the lock; 1346 Mutex::Autolock _l(flinger->mStateLock); 1347 /* 1348 * remove the layer from the current list -- chances are that it's 1349 * not in the list anyway, because it should have been removed 1350 * already upon request of the client (eg: window manager). 1351 * However, a buggy client could have not done that. 1352 * Since we know we don't have any more clients, we don't need 1353 * to use the purgatory. 1354 */ 1355 status_t err = flinger->removeLayer_l(l); 1356 if (err == NAME_NOT_FOUND) { 1357 // The surface wasn't in the current list, which means it was 1358 // removed already, which means it is in the purgatory, 1359 // and need to be removed from there. 1360 // This needs to happen from the main thread since its dtor 1361 // must run from there (b/c of OpenGL ES). Additionally, we 1362 // can't really acquire our internal lock from 1363 // destroySurface() -- see postMessage() below. 1364 ssize_t idx = flinger->mLayerPurgatory.remove(l); 1365 LOGE_IF(idx < 0, 1366 "layer=%p is not in the purgatory list", l.get()); 1367 } 1368 1369 LOGE_IF(err<0 && err != NAME_NOT_FOUND, 1370 "error removing layer=%p (%s)", l.get(), strerror(-err)); 1371 return true; 1372 } 1373 }; 1374 1375 postMessageAsync( new MessageDestroySurface(this, layer) ); 1376 return NO_ERROR; 1377} 1378 1379status_t SurfaceFlinger::setClientState( 1380 const sp<Client>& client, 1381 int32_t count, 1382 const layer_state_t* states) 1383{ 1384 Mutex::Autolock _l(mStateLock); 1385 uint32_t flags = 0; 1386 for (int i=0 ; i<count ; i++) { 1387 const layer_state_t& s(states[i]); 1388 sp<LayerBaseClient> layer(client->getLayerUser(s.surface)); 1389 if (layer != 0) { 1390 const uint32_t what = s.what; 1391 if (what & ePositionChanged) { 1392 if (layer->setPosition(s.x, s.y)) 1393 flags |= eTraversalNeeded; 1394 } 1395 if (what & eLayerChanged) { 1396 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); 1397 if (layer->setLayer(s.z)) { 1398 mCurrentState.layersSortedByZ.removeAt(idx); 1399 mCurrentState.layersSortedByZ.add(layer); 1400 // we need traversal (state changed) 1401 // AND transaction (list changed) 1402 flags |= eTransactionNeeded|eTraversalNeeded; 1403 } 1404 } 1405 if (what & eSizeChanged) { 1406 if (layer->setSize(s.w, s.h)) { 1407 flags |= eTraversalNeeded; 1408 mResizeTransationPending = true; 1409 } 1410 } 1411 if (what & eAlphaChanged) { 1412 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) 1413 flags |= eTraversalNeeded; 1414 } 1415 if (what & eMatrixChanged) { 1416 if (layer->setMatrix(s.matrix)) 1417 flags |= eTraversalNeeded; 1418 } 1419 if (what & eTransparentRegionChanged) { 1420 if (layer->setTransparentRegionHint(s.transparentRegion)) 1421 flags |= eTraversalNeeded; 1422 } 1423 if (what & eVisibilityChanged) { 1424 if (layer->setFlags(s.flags, s.mask)) 1425 flags |= eTraversalNeeded; 1426 } 1427 } 1428 } 1429 if (flags) { 1430 setTransactionFlags(flags); 1431 } 1432 return NO_ERROR; 1433} 1434 1435void SurfaceFlinger::screenReleased(int dpy) 1436{ 1437 // this may be called by a signal handler, we can't do too much in here 1438 android_atomic_or(eConsoleReleased, &mConsoleSignals); 1439 signalEvent(); 1440} 1441 1442void SurfaceFlinger::screenAcquired(int dpy) 1443{ 1444 // this may be called by a signal handler, we can't do too much in here 1445 android_atomic_or(eConsoleAcquired, &mConsoleSignals); 1446 signalEvent(); 1447} 1448 1449status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) 1450{ 1451 const size_t SIZE = 4096; 1452 char buffer[SIZE]; 1453 String8 result; 1454 if (!mDump.checkCalling()) { 1455 snprintf(buffer, SIZE, "Permission Denial: " 1456 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", 1457 IPCThreadState::self()->getCallingPid(), 1458 IPCThreadState::self()->getCallingUid()); 1459 result.append(buffer); 1460 } else { 1461 1462 // figure out if we're stuck somewhere 1463 const nsecs_t now = systemTime(); 1464 const nsecs_t inSwapBuffers(mDebugInSwapBuffers); 1465 const nsecs_t inTransaction(mDebugInTransaction); 1466 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; 1467 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; 1468 1469 // Try to get the main lock, but don't insist if we can't 1470 // (this would indicate SF is stuck, but we want to be able to 1471 // print something in dumpsys). 1472 int retry = 3; 1473 while (mStateLock.tryLock()<0 && --retry>=0) { 1474 usleep(1000000); 1475 } 1476 const bool locked(retry >= 0); 1477 if (!locked) { 1478 snprintf(buffer, SIZE, 1479 "SurfaceFlinger appears to be unresponsive, " 1480 "dumping anyways (no locks held)\n"); 1481 result.append(buffer); 1482 } 1483 1484 const LayerVector& currentLayers = mCurrentState.layersSortedByZ; 1485 const size_t count = currentLayers.size(); 1486 for (size_t i=0 ; i<count ; i++) { 1487 const sp<LayerBase>& layer(currentLayers[i]); 1488 layer->dump(result, buffer, SIZE); 1489 const Layer::State& s(layer->drawingState()); 1490 s.transparentRegion.dump(result, "transparentRegion"); 1491 layer->transparentRegionScreen.dump(result, "transparentRegionScreen"); 1492 layer->visibleRegionScreen.dump(result, "visibleRegionScreen"); 1493 } 1494 1495 mWormholeRegion.dump(result, "WormholeRegion"); 1496 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 1497 snprintf(buffer, SIZE, 1498 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n", 1499 mFreezeDisplay?"yes":"no", mFreezeCount, 1500 mCurrentState.orientation, hw.canDraw()); 1501 result.append(buffer); 1502 snprintf(buffer, SIZE, 1503 " last eglSwapBuffers() time: %f us\n" 1504 " last transaction time : %f us\n", 1505 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0); 1506 result.append(buffer); 1507 1508 if (inSwapBuffersDuration || !locked) { 1509 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n", 1510 inSwapBuffersDuration/1000.0); 1511 result.append(buffer); 1512 } 1513 1514 if (inTransactionDuration || !locked) { 1515 snprintf(buffer, SIZE, " transaction time: %f us\n", 1516 inTransactionDuration/1000.0); 1517 result.append(buffer); 1518 } 1519 1520 HWComposer& hwc(hw.getHwComposer()); 1521 snprintf(buffer, SIZE, " h/w composer %s and %s\n", 1522 hwc.initCheck()==NO_ERROR ? "present" : "not present", 1523 mDebugDisableHWC ? "disabled" : "enabled"); 1524 result.append(buffer); 1525 hwc.dump(result, buffer, SIZE); 1526 1527 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); 1528 alloc.dump(result); 1529 hw.dump(result); 1530 1531 if (locked) { 1532 mStateLock.unlock(); 1533 } 1534 } 1535 write(fd, result.string(), result.size()); 1536 return NO_ERROR; 1537} 1538 1539status_t SurfaceFlinger::onTransact( 1540 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 1541{ 1542 switch (code) { 1543 case CREATE_CONNECTION: 1544 case OPEN_GLOBAL_TRANSACTION: 1545 case CLOSE_GLOBAL_TRANSACTION: 1546 case SET_ORIENTATION: 1547 case FREEZE_DISPLAY: 1548 case UNFREEZE_DISPLAY: 1549 case BOOT_FINISHED: 1550 case TURN_ELECTRON_BEAM_OFF: 1551 case TURN_ELECTRON_BEAM_ON: 1552 { 1553 // codes that require permission check 1554 IPCThreadState* ipc = IPCThreadState::self(); 1555 const int pid = ipc->getCallingPid(); 1556 const int uid = ipc->getCallingUid(); 1557 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) { 1558 LOGE("Permission Denial: " 1559 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); 1560 return PERMISSION_DENIED; 1561 } 1562 break; 1563 } 1564 case CAPTURE_SCREEN: 1565 { 1566 // codes that require permission check 1567 IPCThreadState* ipc = IPCThreadState::self(); 1568 const int pid = ipc->getCallingPid(); 1569 const int uid = ipc->getCallingUid(); 1570 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) { 1571 LOGE("Permission Denial: " 1572 "can't read framebuffer pid=%d, uid=%d", pid, uid); 1573 return PERMISSION_DENIED; 1574 } 1575 break; 1576 } 1577 } 1578 1579 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); 1580 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { 1581 CHECK_INTERFACE(ISurfaceComposer, data, reply); 1582 if (UNLIKELY(!mHardwareTest.checkCalling())) { 1583 IPCThreadState* ipc = IPCThreadState::self(); 1584 const int pid = ipc->getCallingPid(); 1585 const int uid = ipc->getCallingUid(); 1586 LOGE("Permission Denial: " 1587 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); 1588 return PERMISSION_DENIED; 1589 } 1590 int n; 1591 switch (code) { 1592 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE 1593 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE 1594 return NO_ERROR; 1595 case 1002: // SHOW_UPDATES 1596 n = data.readInt32(); 1597 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); 1598 return NO_ERROR; 1599 case 1003: // SHOW_BACKGROUND 1600 n = data.readInt32(); 1601 mDebugBackground = n ? 1 : 0; 1602 return NO_ERROR; 1603 case 1008: // toggle use of hw composer 1604 n = data.readInt32(); 1605 mDebugDisableHWC = n ? 1 : 0; 1606 mHwWorkListDirty = true; 1607 // fall-through... 1608 case 1004:{ // repaint everything 1609 Mutex::Autolock _l(mStateLock); 1610 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 1611 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe 1612 signalEvent(); 1613 return NO_ERROR; 1614 } 1615 case 1005:{ // force transaction 1616 setTransactionFlags(eTransactionNeeded|eTraversalNeeded); 1617 return NO_ERROR; 1618 } 1619 case 1006:{ // enable/disable GraphicLog 1620 int enabled = data.readInt32(); 1621 GraphicLog::getInstance().setEnabled(enabled); 1622 return NO_ERROR; 1623 } 1624 case 1007: // set mFreezeCount 1625 mFreezeCount = data.readInt32(); 1626 mFreezeDisplayTime = 0; 1627 return NO_ERROR; 1628 case 1010: // interrogate. 1629 reply->writeInt32(0); 1630 reply->writeInt32(0); 1631 reply->writeInt32(mDebugRegion); 1632 reply->writeInt32(mDebugBackground); 1633 return NO_ERROR; 1634 case 1013: { 1635 Mutex::Autolock _l(mStateLock); 1636 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 1637 reply->writeInt32(hw.getPageFlipCount()); 1638 } 1639 return NO_ERROR; 1640 } 1641 } 1642 return err; 1643} 1644 1645// --------------------------------------------------------------------------- 1646 1647status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy, 1648 GLuint* textureName, GLfloat* uOut, GLfloat* vOut) 1649{ 1650 if (!GLExtensions::getInstance().haveFramebufferObject()) 1651 return INVALID_OPERATION; 1652 1653 // get screen geometry 1654 const DisplayHardware& hw(graphicPlane(dpy).displayHardware()); 1655 const uint32_t hw_w = hw.getWidth(); 1656 const uint32_t hw_h = hw.getHeight(); 1657 GLfloat u = 1; 1658 GLfloat v = 1; 1659 1660 // make sure to clear all GL error flags 1661 while ( glGetError() != GL_NO_ERROR ) ; 1662 1663 // create a FBO 1664 GLuint name, tname; 1665 glGenTextures(1, &tname); 1666 glBindTexture(GL_TEXTURE_2D, tname); 1667 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1668 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 1669 if (glGetError() != GL_NO_ERROR) { 1670 while ( glGetError() != GL_NO_ERROR ) ; 1671 GLint tw = (2 << (31 - clz(hw_w))); 1672 GLint th = (2 << (31 - clz(hw_h))); 1673 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1674 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 1675 u = GLfloat(hw_w) / tw; 1676 v = GLfloat(hw_h) / th; 1677 } 1678 glGenFramebuffersOES(1, &name); 1679 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); 1680 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, 1681 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0); 1682 1683 // redraw the screen entirely... 1684 glClearColor(0,0,0,1); 1685 glClear(GL_COLOR_BUFFER_BIT); 1686 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); 1687 const size_t count = layers.size(); 1688 for (size_t i=0 ; i<count ; ++i) { 1689 const sp<LayerBase>& layer(layers[i]); 1690 layer->drawForSreenShot(); 1691 } 1692 1693 // back to main framebuffer 1694 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); 1695 glDisable(GL_SCISSOR_TEST); 1696 glDeleteFramebuffersOES(1, &name); 1697 1698 *textureName = tname; 1699 *uOut = u; 1700 *vOut = v; 1701 return NO_ERROR; 1702} 1703 1704// --------------------------------------------------------------------------- 1705 1706status_t SurfaceFlinger::electronBeamOffAnimationImplLocked() 1707{ 1708 status_t result = PERMISSION_DENIED; 1709 1710 if (!GLExtensions::getInstance().haveFramebufferObject()) 1711 return INVALID_OPERATION; 1712 1713 // get screen geometry 1714 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 1715 const uint32_t hw_w = hw.getWidth(); 1716 const uint32_t hw_h = hw.getHeight(); 1717 const Region screenBounds(hw.bounds()); 1718 1719 GLfloat u, v; 1720 GLuint tname; 1721 result = renderScreenToTextureLocked(0, &tname, &u, &v); 1722 if (result != NO_ERROR) { 1723 return result; 1724 } 1725 1726 GLfloat vtx[8]; 1727 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} }; 1728 glEnable(GL_TEXTURE_2D); 1729 glBindTexture(GL_TEXTURE_2D, tname); 1730 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 1731 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1732 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1733 glTexCoordPointer(2, GL_FLOAT, 0, texCoords); 1734 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1735 glVertexPointer(2, GL_FLOAT, 0, vtx); 1736 1737 class s_curve_interpolator { 1738 const float nbFrames, s, v; 1739 public: 1740 s_curve_interpolator(int nbFrames, float s) 1741 : nbFrames(1.0f / (nbFrames-1)), s(s), 1742 v(1.0f + expf(-s + 0.5f*s)) { 1743 } 1744 float operator()(int f) { 1745 const float x = f * nbFrames; 1746 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; 1747 } 1748 }; 1749 1750 class v_stretch { 1751 const GLfloat hw_w, hw_h; 1752 public: 1753 v_stretch(uint32_t hw_w, uint32_t hw_h) 1754 : hw_w(hw_w), hw_h(hw_h) { 1755 } 1756 void operator()(GLfloat* vtx, float v) { 1757 const GLfloat w = hw_w + (hw_w * v); 1758 const GLfloat h = hw_h - (hw_h * v); 1759 const GLfloat x = (hw_w - w) * 0.5f; 1760 const GLfloat y = (hw_h - h) * 0.5f; 1761 vtx[0] = x; vtx[1] = y; 1762 vtx[2] = x; vtx[3] = y + h; 1763 vtx[4] = x + w; vtx[5] = y + h; 1764 vtx[6] = x + w; vtx[7] = y; 1765 } 1766 }; 1767 1768 class h_stretch { 1769 const GLfloat hw_w, hw_h; 1770 public: 1771 h_stretch(uint32_t hw_w, uint32_t hw_h) 1772 : hw_w(hw_w), hw_h(hw_h) { 1773 } 1774 void operator()(GLfloat* vtx, float v) { 1775 const GLfloat w = hw_w - (hw_w * v); 1776 const GLfloat h = 1.0f; 1777 const GLfloat x = (hw_w - w) * 0.5f; 1778 const GLfloat y = (hw_h - h) * 0.5f; 1779 vtx[0] = x; vtx[1] = y; 1780 vtx[2] = x; vtx[3] = y + h; 1781 vtx[4] = x + w; vtx[5] = y + h; 1782 vtx[6] = x + w; vtx[7] = y; 1783 } 1784 }; 1785 1786 // the full animation is 24 frames 1787 const int nbFrames = 12; 1788 s_curve_interpolator itr(nbFrames, 7.5f); 1789 s_curve_interpolator itg(nbFrames, 8.0f); 1790 s_curve_interpolator itb(nbFrames, 8.5f); 1791 1792 v_stretch vverts(hw_w, hw_h); 1793 glEnable(GL_BLEND); 1794 glBlendFunc(GL_ONE, GL_ONE); 1795 for (int i=0 ; i<nbFrames ; i++) { 1796 float x, y, w, h; 1797 const float vr = itr(i); 1798 const float vg = itg(i); 1799 const float vb = itb(i); 1800 1801 // clear screen 1802 glColorMask(1,1,1,1); 1803 glClear(GL_COLOR_BUFFER_BIT); 1804 glEnable(GL_TEXTURE_2D); 1805 1806 // draw the red plane 1807 vverts(vtx, vr); 1808 glColorMask(1,0,0,1); 1809 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1810 1811 // draw the green plane 1812 vverts(vtx, vg); 1813 glColorMask(0,1,0,1); 1814 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1815 1816 // draw the blue plane 1817 vverts(vtx, vb); 1818 glColorMask(0,0,1,1); 1819 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1820 1821 // draw the white highlight (we use the last vertices) 1822 glDisable(GL_TEXTURE_2D); 1823 glColorMask(1,1,1,1); 1824 glColor4f(vg, vg, vg, 1); 1825 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1826 hw.flip(screenBounds); 1827 } 1828 1829 h_stretch hverts(hw_w, hw_h); 1830 glDisable(GL_BLEND); 1831 glDisable(GL_TEXTURE_2D); 1832 glColorMask(1,1,1,1); 1833 for (int i=0 ; i<nbFrames ; i++) { 1834 const float v = itg(i); 1835 hverts(vtx, v); 1836 glClear(GL_COLOR_BUFFER_BIT); 1837 glColor4f(1-v, 1-v, 1-v, 1); 1838 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1839 hw.flip(screenBounds); 1840 } 1841 1842 glColorMask(1,1,1,1); 1843 glEnable(GL_SCISSOR_TEST); 1844 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1845 glDeleteTextures(1, &tname); 1846 return NO_ERROR; 1847} 1848 1849status_t SurfaceFlinger::electronBeamOnAnimationImplLocked() 1850{ 1851 status_t result = PERMISSION_DENIED; 1852 1853 if (!GLExtensions::getInstance().haveFramebufferObject()) 1854 return INVALID_OPERATION; 1855 1856 1857 // get screen geometry 1858 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 1859 const uint32_t hw_w = hw.getWidth(); 1860 const uint32_t hw_h = hw.getHeight(); 1861 const Region screenBounds(hw.bounds()); 1862 1863 GLfloat u, v; 1864 GLuint tname; 1865 result = renderScreenToTextureLocked(0, &tname, &u, &v); 1866 if (result != NO_ERROR) { 1867 return result; 1868 } 1869 1870 // back to main framebuffer 1871 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); 1872 glDisable(GL_SCISSOR_TEST); 1873 1874 GLfloat vtx[8]; 1875 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} }; 1876 glEnable(GL_TEXTURE_2D); 1877 glBindTexture(GL_TEXTURE_2D, tname); 1878 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 1879 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1880 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1881 glTexCoordPointer(2, GL_FLOAT, 0, texCoords); 1882 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1883 glVertexPointer(2, GL_FLOAT, 0, vtx); 1884 1885 class s_curve_interpolator { 1886 const float nbFrames, s, v; 1887 public: 1888 s_curve_interpolator(int nbFrames, float s) 1889 : nbFrames(1.0f / (nbFrames-1)), s(s), 1890 v(1.0f + expf(-s + 0.5f*s)) { 1891 } 1892 float operator()(int f) { 1893 const float x = f * nbFrames; 1894 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; 1895 } 1896 }; 1897 1898 class v_stretch { 1899 const GLfloat hw_w, hw_h; 1900 public: 1901 v_stretch(uint32_t hw_w, uint32_t hw_h) 1902 : hw_w(hw_w), hw_h(hw_h) { 1903 } 1904 void operator()(GLfloat* vtx, float v) { 1905 const GLfloat w = hw_w + (hw_w * v); 1906 const GLfloat h = hw_h - (hw_h * v); 1907 const GLfloat x = (hw_w - w) * 0.5f; 1908 const GLfloat y = (hw_h - h) * 0.5f; 1909 vtx[0] = x; vtx[1] = y; 1910 vtx[2] = x; vtx[3] = y + h; 1911 vtx[4] = x + w; vtx[5] = y + h; 1912 vtx[6] = x + w; vtx[7] = y; 1913 } 1914 }; 1915 1916 class h_stretch { 1917 const GLfloat hw_w, hw_h; 1918 public: 1919 h_stretch(uint32_t hw_w, uint32_t hw_h) 1920 : hw_w(hw_w), hw_h(hw_h) { 1921 } 1922 void operator()(GLfloat* vtx, float v) { 1923 const GLfloat w = hw_w - (hw_w * v); 1924 const GLfloat h = 1.0f; 1925 const GLfloat x = (hw_w - w) * 0.5f; 1926 const GLfloat y = (hw_h - h) * 0.5f; 1927 vtx[0] = x; vtx[1] = y; 1928 vtx[2] = x; vtx[3] = y + h; 1929 vtx[4] = x + w; vtx[5] = y + h; 1930 vtx[6] = x + w; vtx[7] = y; 1931 } 1932 }; 1933 1934 // the full animation is 12 frames 1935 int nbFrames = 8; 1936 s_curve_interpolator itr(nbFrames, 7.5f); 1937 s_curve_interpolator itg(nbFrames, 8.0f); 1938 s_curve_interpolator itb(nbFrames, 8.5f); 1939 1940 h_stretch hverts(hw_w, hw_h); 1941 glDisable(GL_BLEND); 1942 glDisable(GL_TEXTURE_2D); 1943 glColorMask(1,1,1,1); 1944 for (int i=nbFrames-1 ; i>=0 ; i--) { 1945 const float v = itg(i); 1946 hverts(vtx, v); 1947 glClear(GL_COLOR_BUFFER_BIT); 1948 glColor4f(1-v, 1-v, 1-v, 1); 1949 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1950 hw.flip(screenBounds); 1951 } 1952 1953 nbFrames = 4; 1954 v_stretch vverts(hw_w, hw_h); 1955 glEnable(GL_BLEND); 1956 glBlendFunc(GL_ONE, GL_ONE); 1957 for (int i=nbFrames-1 ; i>=0 ; i--) { 1958 float x, y, w, h; 1959 const float vr = itr(i); 1960 const float vg = itg(i); 1961 const float vb = itb(i); 1962 1963 // clear screen 1964 glColorMask(1,1,1,1); 1965 glClear(GL_COLOR_BUFFER_BIT); 1966 glEnable(GL_TEXTURE_2D); 1967 1968 // draw the red plane 1969 vverts(vtx, vr); 1970 glColorMask(1,0,0,1); 1971 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1972 1973 // draw the green plane 1974 vverts(vtx, vg); 1975 glColorMask(0,1,0,1); 1976 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1977 1978 // draw the blue plane 1979 vverts(vtx, vb); 1980 glColorMask(0,0,1,1); 1981 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1982 1983 hw.flip(screenBounds); 1984 } 1985 1986 glColorMask(1,1,1,1); 1987 glEnable(GL_SCISSOR_TEST); 1988 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1989 glDeleteTextures(1, &tname); 1990 1991 return NO_ERROR; 1992} 1993 1994// --------------------------------------------------------------------------- 1995 1996status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode) 1997{ 1998 DisplayHardware& hw(graphicPlane(0).editDisplayHardware()); 1999 if (!hw.canDraw()) { 2000 // we're already off 2001 return NO_ERROR; 2002 } 2003 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) { 2004 electronBeamOffAnimationImplLocked(); 2005 } 2006 2007 // always clear the whole screen at the end of the animation 2008 glClearColor(0,0,0,1); 2009 glDisable(GL_SCISSOR_TEST); 2010 glClear(GL_COLOR_BUFFER_BIT); 2011 glEnable(GL_SCISSOR_TEST); 2012 hw.flip( Region(hw.bounds()) ); 2013 2014 hw.setCanDraw(false); 2015 return NO_ERROR; 2016} 2017 2018status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode) 2019{ 2020 class MessageTurnElectronBeamOff : public MessageBase { 2021 SurfaceFlinger* flinger; 2022 int32_t mode; 2023 status_t result; 2024 public: 2025 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode) 2026 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { 2027 } 2028 status_t getResult() const { 2029 return result; 2030 } 2031 virtual bool handler() { 2032 Mutex::Autolock _l(flinger->mStateLock); 2033 result = flinger->turnElectronBeamOffImplLocked(mode); 2034 return true; 2035 } 2036 }; 2037 2038 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode); 2039 status_t res = postMessageSync(msg); 2040 if (res == NO_ERROR) { 2041 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult(); 2042 2043 // work-around: when the power-manager calls us we activate the 2044 // animation. eventually, the "on" animation will be called 2045 // by the power-manager itself 2046 mElectronBeamAnimationMode = mode; 2047 } 2048 return res; 2049} 2050 2051// --------------------------------------------------------------------------- 2052 2053status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode) 2054{ 2055 DisplayHardware& hw(graphicPlane(0).editDisplayHardware()); 2056 if (hw.canDraw()) { 2057 // we're already on 2058 return NO_ERROR; 2059 } 2060 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) { 2061 electronBeamOnAnimationImplLocked(); 2062 } 2063 hw.setCanDraw(true); 2064 2065 // make sure to redraw the whole screen when the animation is done 2066 mDirtyRegion.set(hw.bounds()); 2067 signalEvent(); 2068 2069 return NO_ERROR; 2070} 2071 2072status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode) 2073{ 2074 class MessageTurnElectronBeamOn : public MessageBase { 2075 SurfaceFlinger* flinger; 2076 int32_t mode; 2077 status_t result; 2078 public: 2079 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode) 2080 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { 2081 } 2082 status_t getResult() const { 2083 return result; 2084 } 2085 virtual bool handler() { 2086 Mutex::Autolock _l(flinger->mStateLock); 2087 result = flinger->turnElectronBeamOnImplLocked(mode); 2088 return true; 2089 } 2090 }; 2091 2092 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) ); 2093 return NO_ERROR; 2094} 2095 2096// --------------------------------------------------------------------------- 2097 2098status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy, 2099 sp<IMemoryHeap>* heap, 2100 uint32_t* w, uint32_t* h, PixelFormat* f, 2101 uint32_t sw, uint32_t sh, 2102 uint32_t minLayerZ, uint32_t maxLayerZ) 2103{ 2104 status_t result = PERMISSION_DENIED; 2105 2106 // only one display supported for now 2107 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) 2108 return BAD_VALUE; 2109 2110 if (!GLExtensions::getInstance().haveFramebufferObject()) 2111 return INVALID_OPERATION; 2112 2113 // get screen geometry 2114 const DisplayHardware& hw(graphicPlane(dpy).displayHardware()); 2115 const uint32_t hw_w = hw.getWidth(); 2116 const uint32_t hw_h = hw.getHeight(); 2117 2118 if ((sw > hw_w) || (sh > hw_h)) 2119 return BAD_VALUE; 2120 2121 sw = (!sw) ? hw_w : sw; 2122 sh = (!sh) ? hw_h : sh; 2123 const size_t size = sw * sh * 4; 2124 2125 // make sure to clear all GL error flags 2126 while ( glGetError() != GL_NO_ERROR ) ; 2127 2128 // create a FBO 2129 GLuint name, tname; 2130 glGenRenderbuffersOES(1, &tname); 2131 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname); 2132 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh); 2133 glGenFramebuffersOES(1, &name); 2134 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); 2135 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, 2136 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname); 2137 2138 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); 2139 if (status == GL_FRAMEBUFFER_COMPLETE_OES) { 2140 2141 // invert everything, b/c glReadPixel() below will invert the FB 2142 glViewport(0, 0, sw, sh); 2143 glScissor(0, 0, sw, sh); 2144 glMatrixMode(GL_PROJECTION); 2145 glPushMatrix(); 2146 glLoadIdentity(); 2147 glOrthof(0, hw_w, 0, hw_h, 0, 1); 2148 glMatrixMode(GL_MODELVIEW); 2149 2150 // redraw the screen entirely... 2151 glClearColor(0,0,0,1); 2152 glClear(GL_COLOR_BUFFER_BIT); 2153 2154 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); 2155 const size_t count = layers.size(); 2156 for (size_t i=0 ; i<count ; ++i) { 2157 const sp<LayerBase>& layer(layers[i]); 2158 const uint32_t z = layer->drawingState().z; 2159 if (z >= minLayerZ && z <= maxLayerZ) { 2160 layer->drawForSreenShot(); 2161 } 2162 } 2163 2164 // XXX: this is needed on tegra 2165 glScissor(0, 0, sw, sh); 2166 2167 // check for errors and return screen capture 2168 if (glGetError() != GL_NO_ERROR) { 2169 // error while rendering 2170 result = INVALID_OPERATION; 2171 } else { 2172 // allocate shared memory large enough to hold the 2173 // screen capture 2174 sp<MemoryHeapBase> base( 2175 new MemoryHeapBase(size, 0, "screen-capture") ); 2176 void* const ptr = base->getBase(); 2177 if (ptr) { 2178 // capture the screen with glReadPixels() 2179 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr); 2180 if (glGetError() == GL_NO_ERROR) { 2181 *heap = base; 2182 *w = sw; 2183 *h = sh; 2184 *f = PIXEL_FORMAT_RGBA_8888; 2185 result = NO_ERROR; 2186 } 2187 } else { 2188 result = NO_MEMORY; 2189 } 2190 } 2191 glEnable(GL_SCISSOR_TEST); 2192 glViewport(0, 0, hw_w, hw_h); 2193 glMatrixMode(GL_PROJECTION); 2194 glPopMatrix(); 2195 glMatrixMode(GL_MODELVIEW); 2196 2197 2198 } else { 2199 result = BAD_VALUE; 2200 } 2201 2202 // release FBO resources 2203 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); 2204 glDeleteRenderbuffersOES(1, &tname); 2205 glDeleteFramebuffersOES(1, &name); 2206 2207 hw.compositionComplete(); 2208 2209 return result; 2210} 2211 2212 2213status_t SurfaceFlinger::captureScreen(DisplayID dpy, 2214 sp<IMemoryHeap>* heap, 2215 uint32_t* width, uint32_t* height, PixelFormat* format, 2216 uint32_t sw, uint32_t sh, 2217 uint32_t minLayerZ, uint32_t maxLayerZ) 2218{ 2219 // only one display supported for now 2220 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) 2221 return BAD_VALUE; 2222 2223 if (!GLExtensions::getInstance().haveFramebufferObject()) 2224 return INVALID_OPERATION; 2225 2226 class MessageCaptureScreen : public MessageBase { 2227 SurfaceFlinger* flinger; 2228 DisplayID dpy; 2229 sp<IMemoryHeap>* heap; 2230 uint32_t* w; 2231 uint32_t* h; 2232 PixelFormat* f; 2233 uint32_t sw; 2234 uint32_t sh; 2235 uint32_t minLayerZ; 2236 uint32_t maxLayerZ; 2237 status_t result; 2238 public: 2239 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy, 2240 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f, 2241 uint32_t sw, uint32_t sh, 2242 uint32_t minLayerZ, uint32_t maxLayerZ) 2243 : flinger(flinger), dpy(dpy), 2244 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), 2245 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ), 2246 result(PERMISSION_DENIED) 2247 { 2248 } 2249 status_t getResult() const { 2250 return result; 2251 } 2252 virtual bool handler() { 2253 Mutex::Autolock _l(flinger->mStateLock); 2254 2255 // if we have secure windows, never allow the screen capture 2256 if (flinger->mSecureFrameBuffer) 2257 return true; 2258 2259 result = flinger->captureScreenImplLocked(dpy, 2260 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ); 2261 2262 return true; 2263 } 2264 }; 2265 2266 sp<MessageBase> msg = new MessageCaptureScreen(this, 2267 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ); 2268 status_t res = postMessageSync(msg); 2269 if (res == NO_ERROR) { 2270 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult(); 2271 } 2272 return res; 2273} 2274 2275// --------------------------------------------------------------------------- 2276 2277sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const 2278{ 2279 sp<Layer> result; 2280 Mutex::Autolock _l(mStateLock); 2281 result = mLayerMap.valueFor( sur->asBinder() ).promote(); 2282 return result; 2283} 2284 2285// --------------------------------------------------------------------------- 2286 2287sp<GraphicBuffer> SurfaceFlinger::createGraphicBuffer(uint32_t w, uint32_t h, 2288 PixelFormat format, uint32_t usage) const { 2289 // XXX: HACK HACK HACK!!! This should NOT be static, but it is to fix a 2290 // race between SurfaceFlinger unref'ing the buffer and the client ref'ing 2291 // it. 2292 static sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage)); 2293 status_t err = graphicBuffer->initCheck(); 2294 if (err != 0) { 2295 LOGE("createGraphicBuffer: init check failed: %d", err); 2296 return 0; 2297 } else if (graphicBuffer->handle == 0) { 2298 LOGE("createGraphicBuffer: unable to create GraphicBuffer"); 2299 return 0; 2300 } 2301 return graphicBuffer; 2302} 2303 2304// --------------------------------------------------------------------------- 2305 2306Client::Client(const sp<SurfaceFlinger>& flinger) 2307 : mFlinger(flinger), mNameGenerator(1) 2308{ 2309} 2310 2311Client::~Client() 2312{ 2313 const size_t count = mLayers.size(); 2314 for (size_t i=0 ; i<count ; i++) { 2315 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote()); 2316 if (layer != 0) { 2317 mFlinger->removeLayer(layer); 2318 } 2319 } 2320} 2321 2322status_t Client::initCheck() const { 2323 return NO_ERROR; 2324} 2325 2326ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer) 2327{ 2328 int32_t name = android_atomic_inc(&mNameGenerator); 2329 mLayers.add(name, layer); 2330 return name; 2331} 2332 2333void Client::detachLayer(const LayerBaseClient* layer) 2334{ 2335 // we do a linear search here, because this doesn't happen often 2336 const size_t count = mLayers.size(); 2337 for (size_t i=0 ; i<count ; i++) { 2338 if (mLayers.valueAt(i) == layer) { 2339 mLayers.removeItemsAt(i, 1); 2340 break; 2341 } 2342 } 2343} 2344sp<LayerBaseClient> Client::getLayerUser(int32_t i) const { 2345 sp<LayerBaseClient> lbc; 2346 const wp<LayerBaseClient>& layer(mLayers.valueFor(i)); 2347 if (layer != 0) { 2348 lbc = layer.promote(); 2349 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i)); 2350 } 2351 return lbc; 2352} 2353 2354sp<IMemoryHeap> Client::getControlBlock() const { 2355 return 0; 2356} 2357ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const { 2358 return -1; 2359} 2360sp<ISurface> Client::createSurface( 2361 ISurfaceComposerClient::surface_data_t* params, int pid, 2362 const String8& name, 2363 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 2364 uint32_t flags) 2365{ 2366 return mFlinger->createSurface(this, pid, name, params, 2367 display, w, h, format, flags); 2368} 2369status_t Client::destroySurface(SurfaceID sid) { 2370 return mFlinger->removeSurface(this, sid); 2371} 2372status_t Client::setState(int32_t count, const layer_state_t* states) { 2373 return mFlinger->setClientState(this, count, states); 2374} 2375 2376// --------------------------------------------------------------------------- 2377 2378UserClient::UserClient(const sp<SurfaceFlinger>& flinger) 2379 : ctrlblk(0), mBitmap(0), mFlinger(flinger) 2380{ 2381 const int pgsize = getpagesize(); 2382 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1)); 2383 2384 mCblkHeap = new MemoryHeapBase(cblksize, 0, 2385 "SurfaceFlinger Client control-block"); 2386 2387 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase()); 2388 if (ctrlblk) { // construct the shared structure in-place. 2389 new(ctrlblk) SharedClient; 2390 } 2391} 2392 2393UserClient::~UserClient() 2394{ 2395 if (ctrlblk) { 2396 ctrlblk->~SharedClient(); // destroy our shared-structure. 2397 } 2398 2399 /* 2400 * When a UserClient dies, it's unclear what to do exactly. 2401 * We could go ahead and destroy all surfaces linked to that client 2402 * however, it wouldn't be fair to the main Client 2403 * (usually the the window-manager), which might want to re-target 2404 * the layer to another UserClient. 2405 * I think the best is to do nothing, or not much; in most cases the 2406 * WM itself will go ahead and clean things up when it detects a client of 2407 * his has died. 2408 * The remaining question is what to display? currently we keep 2409 * just keep the current buffer. 2410 */ 2411} 2412 2413status_t UserClient::initCheck() const { 2414 return ctrlblk == 0 ? NO_INIT : NO_ERROR; 2415} 2416 2417void UserClient::detachLayer(const Layer* layer) 2418{ 2419 int32_t name = layer->getToken(); 2420 if (name >= 0) { 2421 int32_t mask = 1LU<<name; 2422 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) { 2423 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap)); 2424 } 2425 } 2426} 2427 2428sp<IMemoryHeap> UserClient::getControlBlock() const { 2429 return mCblkHeap; 2430} 2431 2432ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const 2433{ 2434 int32_t name = NAME_NOT_FOUND; 2435 sp<Layer> layer(mFlinger->getLayer(sur)); 2436 if (layer == 0) { 2437 return name; 2438 } 2439 2440 // if this layer already has a token, just return it 2441 name = layer->getToken(); 2442 if ((name >= 0) && (layer->getClient() == this)) { 2443 return name; 2444 } 2445 2446 name = 0; 2447 do { 2448 int32_t mask = 1LU<<name; 2449 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) { 2450 // we found and locked that name 2451 status_t err = layer->setToken( 2452 const_cast<UserClient*>(this), ctrlblk, name); 2453 if (err != NO_ERROR) { 2454 // free the name 2455 android_atomic_and(~mask, &mBitmap); 2456 name = err; 2457 } 2458 break; 2459 } 2460 if (++name > 31) 2461 name = NO_MEMORY; 2462 } while(name >= 0); 2463 2464 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)", 2465 // sur->asBinder().get(), name, this, mBitmap); 2466 return name; 2467} 2468 2469sp<ISurface> UserClient::createSurface( 2470 ISurfaceComposerClient::surface_data_t* params, int pid, 2471 const String8& name, 2472 DisplayID display, uint32_t w, uint32_t h, PixelFormat format, 2473 uint32_t flags) { 2474 return 0; 2475} 2476status_t UserClient::destroySurface(SurfaceID sid) { 2477 return INVALID_OPERATION; 2478} 2479status_t UserClient::setState(int32_t count, const layer_state_t* states) { 2480 return INVALID_OPERATION; 2481} 2482 2483// --------------------------------------------------------------------------- 2484 2485GraphicPlane::GraphicPlane() 2486 : mHw(0) 2487{ 2488} 2489 2490GraphicPlane::~GraphicPlane() { 2491 delete mHw; 2492} 2493 2494bool GraphicPlane::initialized() const { 2495 return mHw ? true : false; 2496} 2497 2498int GraphicPlane::getWidth() const { 2499 return mWidth; 2500} 2501 2502int GraphicPlane::getHeight() const { 2503 return mHeight; 2504} 2505 2506void GraphicPlane::setDisplayHardware(DisplayHardware *hw) 2507{ 2508 mHw = hw; 2509 2510 // initialize the display orientation transform. 2511 // it's a constant that should come from the display driver. 2512 int displayOrientation = ISurfaceComposer::eOrientationDefault; 2513 char property[PROPERTY_VALUE_MAX]; 2514 if (property_get("ro.sf.hwrotation", property, NULL) > 0) { 2515 //displayOrientation 2516 switch (atoi(property)) { 2517 case 90: 2518 displayOrientation = ISurfaceComposer::eOrientation90; 2519 break; 2520 case 270: 2521 displayOrientation = ISurfaceComposer::eOrientation270; 2522 break; 2523 } 2524 } 2525 2526 const float w = hw->getWidth(); 2527 const float h = hw->getHeight(); 2528 GraphicPlane::orientationToTransfrom(displayOrientation, w, h, 2529 &mDisplayTransform); 2530 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) { 2531 mDisplayWidth = h; 2532 mDisplayHeight = w; 2533 } else { 2534 mDisplayWidth = w; 2535 mDisplayHeight = h; 2536 } 2537 2538 setOrientation(ISurfaceComposer::eOrientationDefault); 2539} 2540 2541status_t GraphicPlane::orientationToTransfrom( 2542 int orientation, int w, int h, Transform* tr) 2543{ 2544 uint32_t flags = 0; 2545 switch (orientation) { 2546 case ISurfaceComposer::eOrientationDefault: 2547 flags = Transform::ROT_0; 2548 break; 2549 case ISurfaceComposer::eOrientation90: 2550 flags = Transform::ROT_90; 2551 break; 2552 case ISurfaceComposer::eOrientation180: 2553 flags = Transform::ROT_180; 2554 break; 2555 case ISurfaceComposer::eOrientation270: 2556 flags = Transform::ROT_270; 2557 break; 2558 default: 2559 return BAD_VALUE; 2560 } 2561 tr->set(flags, w, h); 2562 return NO_ERROR; 2563} 2564 2565status_t GraphicPlane::setOrientation(int orientation) 2566{ 2567 // If the rotation can be handled in hardware, this is where 2568 // the magic should happen. 2569 2570 const DisplayHardware& hw(displayHardware()); 2571 const float w = mDisplayWidth; 2572 const float h = mDisplayHeight; 2573 mWidth = int(w); 2574 mHeight = int(h); 2575 2576 Transform orientationTransform; 2577 GraphicPlane::orientationToTransfrom(orientation, w, h, 2578 &orientationTransform); 2579 if (orientation & ISurfaceComposer::eOrientationSwapMask) { 2580 mWidth = int(h); 2581 mHeight = int(w); 2582 } 2583 2584 mOrientation = orientation; 2585 mGlobalTransform = mDisplayTransform * orientationTransform; 2586 return NO_ERROR; 2587} 2588 2589const DisplayHardware& GraphicPlane::displayHardware() const { 2590 return *mHw; 2591} 2592 2593DisplayHardware& GraphicPlane::editDisplayHardware() { 2594 return *mHw; 2595} 2596 2597const Transform& GraphicPlane::transform() const { 2598 return mGlobalTransform; 2599} 2600 2601EGLDisplay GraphicPlane::getEGLDisplay() const { 2602 return mHw->getEGLDisplay(); 2603} 2604 2605// --------------------------------------------------------------------------- 2606 2607}; // namespace android 2608