Layer.cpp revision 063121849890da78b1ad7fb96c54c795de5d1fd6
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 ATRACE_TAG ATRACE_TAG_GRAPHICS 18 19#include <stdlib.h> 20#include <stdint.h> 21#include <sys/types.h> 22#include <math.h> 23 24#include <cutils/compiler.h> 25#include <cutils/native_handle.h> 26#include <cutils/properties.h> 27 28#include <utils/Errors.h> 29#include <utils/Log.h> 30#include <utils/NativeHandle.h> 31#include <utils/StopWatch.h> 32#include <utils/Trace.h> 33 34#include <ui/GraphicBuffer.h> 35#include <ui/PixelFormat.h> 36 37#include <gui/BufferItem.h> 38#include <gui/Surface.h> 39 40#include "clz.h" 41#include "Colorizer.h" 42#include "DisplayDevice.h" 43#include "Layer.h" 44#include "MonitoredProducer.h" 45#include "SurfaceFlinger.h" 46 47#include "DisplayHardware/HWComposer.h" 48 49#include "RenderEngine/RenderEngine.h" 50 51#define DEBUG_RESIZE 0 52 53namespace android { 54 55// --------------------------------------------------------------------------- 56 57int32_t Layer::sSequence = 1; 58 59Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, 60 const String8& name, uint32_t w, uint32_t h, uint32_t flags) 61 : contentDirty(false), 62 sequence(uint32_t(android_atomic_inc(&sSequence))), 63 mFlinger(flinger), 64 mTextureName(-1U), 65 mPremultipliedAlpha(true), 66 mName("unnamed"), 67 mFormat(PIXEL_FORMAT_NONE), 68 mTransactionFlags(0), 69 mPendingStateMutex(), 70 mPendingStates(), 71 mQueuedFrames(0), 72 mSidebandStreamChanged(false), 73 mCurrentTransform(0), 74 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), 75 mCurrentOpacity(true), 76 mRefreshPending(false), 77 mFrameLatencyNeeded(false), 78 mFiltering(false), 79 mNeedsFiltering(false), 80 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2), 81 mProtectedByApp(false), 82 mHasSurface(false), 83 mClientRef(client), 84 mPotentialCursor(false), 85 mQueueItemLock(), 86 mQueueItemCondition(), 87 mQueueItems(), 88 mLastFrameNumberReceived(0), 89 mUpdateTexImageFailed(false) 90{ 91 mCurrentCrop.makeInvalid(); 92 mFlinger->getRenderEngine().genTextures(1, &mTextureName); 93 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); 94 95 uint32_t layerFlags = 0; 96 if (flags & ISurfaceComposerClient::eHidden) 97 layerFlags |= layer_state_t::eLayerHidden; 98 if (flags & ISurfaceComposerClient::eOpaque) 99 layerFlags |= layer_state_t::eLayerOpaque; 100 if (flags & ISurfaceComposerClient::eSecure) 101 layerFlags |= layer_state_t::eLayerSecure; 102 103 if (flags & ISurfaceComposerClient::eNonPremultiplied) 104 mPremultipliedAlpha = false; 105 106 mName = name; 107 108 mCurrentState.active.w = w; 109 mCurrentState.active.h = h; 110 mCurrentState.active.crop.makeInvalid(); 111 mCurrentState.z = 0; 112 mCurrentState.alpha = 0xFF; 113 mCurrentState.layerStack = 0; 114 mCurrentState.flags = layerFlags; 115 mCurrentState.sequence = 0; 116 mCurrentState.transform.set(0, 0); 117 mCurrentState.requested = mCurrentState.active; 118 119 // drawing state & current state are identical 120 mDrawingState = mCurrentState; 121 122 nsecs_t displayPeriod = 123 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); 124 mFrameTracker.setDisplayRefreshPeriod(displayPeriod); 125} 126 127void Layer::onFirstRef() { 128 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use 129 sp<IGraphicBufferProducer> producer; 130 sp<IGraphicBufferConsumer> consumer; 131 BufferQueue::createBufferQueue(&producer, &consumer); 132 mProducer = new MonitoredProducer(producer, mFlinger); 133 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName); 134 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); 135 mSurfaceFlingerConsumer->setContentsChangedListener(this); 136 mSurfaceFlingerConsumer->setName(mName); 137 138#ifdef TARGET_DISABLE_TRIPLE_BUFFERING 139#warning "disabling triple buffering" 140#else 141 mProducer->setMaxDequeuedBufferCount(2); 142#endif 143 144 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); 145 updateTransformHint(hw); 146} 147 148Layer::~Layer() { 149 sp<Client> c(mClientRef.promote()); 150 if (c != 0) { 151 c->detachLayer(this); 152 } 153 mFlinger->deleteTextureAsync(mTextureName); 154 mFrameTracker.logAndResetStats(mName); 155} 156 157// --------------------------------------------------------------------------- 158// callbacks 159// --------------------------------------------------------------------------- 160 161void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */, 162 HWComposer::HWCLayerInterface* layer) { 163 if (layer) { 164 layer->onDisplayed(); 165 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence()); 166 } 167} 168 169void Layer::markSyncPointsAvailable(const BufferItem& item) { 170 auto pointIter = mLocalSyncPoints.begin(); 171 while (pointIter != mLocalSyncPoints.end()) { 172 if ((*pointIter)->getFrameNumber() == item.mFrameNumber) { 173 auto syncPoint = *pointIter; 174 pointIter = mLocalSyncPoints.erase(pointIter); 175 Mutex::Autolock lock(mAvailableFrameMutex); 176 mAvailableFrames.push_back(std::move(syncPoint)); 177 } else { 178 ++pointIter; 179 } 180 } 181} 182 183void Layer::onFrameAvailable(const BufferItem& item) { 184 // Add this buffer from our internal queue tracker 185 { // Autolock scope 186 Mutex::Autolock lock(mQueueItemLock); 187 188 // Reset the frame number tracker when we receive the first buffer after 189 // a frame number reset 190 if (item.mFrameNumber == 1) { 191 mLastFrameNumberReceived = 0; 192 } 193 194 // Ensure that callbacks are handled in order 195 while (item.mFrameNumber != mLastFrameNumberReceived + 1) { 196 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, 197 ms2ns(500)); 198 if (result != NO_ERROR) { 199 ALOGE("[%s] Timed out waiting on callback", mName.string()); 200 } 201 } 202 203 mQueueItems.push_back(item); 204 android_atomic_inc(&mQueuedFrames); 205 206 // Wake up any pending callbacks 207 mLastFrameNumberReceived = item.mFrameNumber; 208 mQueueItemCondition.broadcast(); 209 } 210 211 markSyncPointsAvailable(item); 212 213 mFlinger->signalLayerUpdate(); 214} 215 216void Layer::onFrameReplaced(const BufferItem& item) { 217 { // Autolock scope 218 Mutex::Autolock lock(mQueueItemLock); 219 220 // Ensure that callbacks are handled in order 221 while (item.mFrameNumber != mLastFrameNumberReceived + 1) { 222 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, 223 ms2ns(500)); 224 if (result != NO_ERROR) { 225 ALOGE("[%s] Timed out waiting on callback", mName.string()); 226 } 227 } 228 229 if (mQueueItems.empty()) { 230 ALOGE("Can't replace a frame on an empty queue"); 231 return; 232 } 233 mQueueItems.editItemAt(0) = item; 234 235 // Wake up any pending callbacks 236 mLastFrameNumberReceived = item.mFrameNumber; 237 mQueueItemCondition.broadcast(); 238 } 239 240 markSyncPointsAvailable(item); 241} 242 243void Layer::onSidebandStreamChanged() { 244 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) { 245 // mSidebandStreamChanged was false 246 mFlinger->signalLayerUpdate(); 247 } 248} 249 250// called with SurfaceFlinger::mStateLock from the drawing thread after 251// the layer has been remove from the current state list (and just before 252// it's removed from the drawing state list) 253void Layer::onRemoved() { 254 mSurfaceFlingerConsumer->abandon(); 255} 256 257// --------------------------------------------------------------------------- 258// set-up 259// --------------------------------------------------------------------------- 260 261const String8& Layer::getName() const { 262 return mName; 263} 264 265status_t Layer::setBuffers( uint32_t w, uint32_t h, 266 PixelFormat format, uint32_t flags) 267{ 268 uint32_t const maxSurfaceDims = min( 269 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); 270 271 // never allow a surface larger than what our underlying GL implementation 272 // can handle. 273 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { 274 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); 275 return BAD_VALUE; 276 } 277 278 mFormat = format; 279 280 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false; 281 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; 282 mCurrentOpacity = getOpacityForFormat(format); 283 284 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); 285 mSurfaceFlingerConsumer->setDefaultBufferFormat(format); 286 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); 287 288 return NO_ERROR; 289} 290 291/* 292 * The layer handle is just a BBinder object passed to the client 293 * (remote process) -- we don't keep any reference on our side such that 294 * the dtor is called when the remote side let go of its reference. 295 * 296 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for 297 * this layer when the handle is destroyed. 298 */ 299class Layer::Handle : public BBinder, public LayerCleaner { 300 public: 301 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer) 302 : LayerCleaner(flinger, layer), owner(layer) {} 303 304 wp<Layer> owner; 305}; 306 307sp<IBinder> Layer::getHandle() { 308 Mutex::Autolock _l(mLock); 309 310 LOG_ALWAYS_FATAL_IF(mHasSurface, 311 "Layer::getHandle() has already been called"); 312 313 mHasSurface = true; 314 315 return new Handle(mFlinger, this); 316} 317 318sp<IGraphicBufferProducer> Layer::getProducer() const { 319 return mProducer; 320} 321 322// --------------------------------------------------------------------------- 323// h/w composer set-up 324// --------------------------------------------------------------------------- 325 326Rect Layer::getContentCrop() const { 327 // this is the crop rectangle that applies to the buffer 328 // itself (as opposed to the window) 329 Rect crop; 330 if (!mCurrentCrop.isEmpty()) { 331 // if the buffer crop is defined, we use that 332 crop = mCurrentCrop; 333 } else if (mActiveBuffer != NULL) { 334 // otherwise we use the whole buffer 335 crop = mActiveBuffer->getBounds(); 336 } else { 337 // if we don't have a buffer yet, we use an empty/invalid crop 338 crop.makeInvalid(); 339 } 340 return crop; 341} 342 343static Rect reduce(const Rect& win, const Region& exclude) { 344 if (CC_LIKELY(exclude.isEmpty())) { 345 return win; 346 } 347 if (exclude.isRect()) { 348 return win.reduce(exclude.getBounds()); 349 } 350 return Region(win).subtract(exclude).getBounds(); 351} 352 353Rect Layer::computeBounds() const { 354 const Layer::State& s(getDrawingState()); 355 return computeBounds(s.activeTransparentRegion); 356} 357 358Rect Layer::computeBounds(const Region& activeTransparentRegion) const { 359 const Layer::State& s(getDrawingState()); 360 Rect win(s.active.w, s.active.h); 361 if (!s.active.crop.isEmpty()) { 362 win.intersect(s.active.crop, &win); 363 } 364 // subtract the transparent region and snap to the bounds 365 return reduce(win, activeTransparentRegion); 366} 367 368FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const { 369 // the content crop is the area of the content that gets scaled to the 370 // layer's size. 371 FloatRect crop(getContentCrop()); 372 373 // the active.crop is the area of the window that gets cropped, but not 374 // scaled in any ways. 375 const State& s(getDrawingState()); 376 377 // apply the projection's clipping to the window crop in 378 // layerstack space, and convert-back to layer space. 379 // if there are no window scaling involved, this operation will map to full 380 // pixels in the buffer. 381 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have 382 // a viewport clipping and a window transform. we should use floating point to fix this. 383 384 Rect activeCrop(s.active.w, s.active.h); 385 if (!s.active.crop.isEmpty()) { 386 activeCrop = s.active.crop; 387 } 388 389 activeCrop = s.transform.transform(activeCrop); 390 activeCrop.intersect(hw->getViewport(), &activeCrop); 391 activeCrop = s.transform.inverse().transform(activeCrop); 392 393 // This needs to be here as transform.transform(Rect) computes the 394 // transformed rect and then takes the bounding box of the result before 395 // returning. This means 396 // transform.inverse().transform(transform.transform(Rect)) != Rect 397 // in which case we need to make sure the final rect is clipped to the 398 // display bounds. 399 activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop); 400 401 // subtract the transparent region and snap to the bounds 402 activeCrop = reduce(activeCrop, s.activeTransparentRegion); 403 404 if (!activeCrop.isEmpty()) { 405 // Transform the window crop to match the buffer coordinate system, 406 // which means using the inverse of the current transform set on the 407 // SurfaceFlingerConsumer. 408 uint32_t invTransform = mCurrentTransform; 409 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 410 /* 411 * the code below applies the display's inverse transform to the buffer 412 */ 413 uint32_t invTransformOrient = hw->getOrientationTransform(); 414 // calculate the inverse transform 415 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) { 416 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 417 NATIVE_WINDOW_TRANSFORM_FLIP_H; 418 // If the transform has been rotated the axis of flip has been swapped 419 // so we need to swap which flip operations we are performing 420 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; 421 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; 422 if (is_h_flipped != is_v_flipped) { 423 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 424 NATIVE_WINDOW_TRANSFORM_FLIP_H; 425 } 426 } 427 // and apply to the current transform 428 invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation(); 429 } 430 431 int winWidth = s.active.w; 432 int winHeight = s.active.h; 433 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 434 // If the activeCrop has been rotate the ends are rotated but not 435 // the space itself so when transforming ends back we can't rely on 436 // a modification of the axes of rotation. To account for this we 437 // need to reorient the inverse rotation in terms of the current 438 // axes of rotation. 439 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; 440 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; 441 if (is_h_flipped == is_v_flipped) { 442 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 443 NATIVE_WINDOW_TRANSFORM_FLIP_H; 444 } 445 winWidth = s.active.h; 446 winHeight = s.active.w; 447 } 448 const Rect winCrop = activeCrop.transform( 449 invTransform, s.active.w, s.active.h); 450 451 // below, crop is intersected with winCrop expressed in crop's coordinate space 452 float xScale = crop.getWidth() / float(winWidth); 453 float yScale = crop.getHeight() / float(winHeight); 454 455 float insetL = winCrop.left * xScale; 456 float insetT = winCrop.top * yScale; 457 float insetR = (winWidth - winCrop.right ) * xScale; 458 float insetB = (winHeight - winCrop.bottom) * yScale; 459 460 crop.left += insetL; 461 crop.top += insetT; 462 crop.right -= insetR; 463 crop.bottom -= insetB; 464 } 465 return crop; 466} 467 468void Layer::setGeometry( 469 const sp<const DisplayDevice>& hw, 470 HWComposer::HWCLayerInterface& layer) 471{ 472 layer.setDefaultState(); 473 474 // enable this layer 475 layer.setSkip(false); 476 477 if (isSecure() && !hw->isSecure()) { 478 layer.setSkip(true); 479 } 480 481 // this gives us only the "orientation" component of the transform 482 const State& s(getDrawingState()); 483 if (!isOpaque(s) || s.alpha != 0xFF) { 484 layer.setBlending(mPremultipliedAlpha ? 485 HWC_BLENDING_PREMULT : 486 HWC_BLENDING_COVERAGE); 487 } 488 489 // apply the layer's transform, followed by the display's global transform 490 // here we're guaranteed that the layer's transform preserves rects 491 Region activeTransparentRegion(s.activeTransparentRegion); 492 if (!s.active.crop.isEmpty()) { 493 Rect activeCrop(s.active.crop); 494 activeCrop = s.transform.transform(activeCrop); 495 activeCrop.intersect(hw->getViewport(), &activeCrop); 496 activeCrop = s.transform.inverse().transform(activeCrop); 497 // This needs to be here as transform.transform(Rect) computes the 498 // transformed rect and then takes the bounding box of the result before 499 // returning. This means 500 // transform.inverse().transform(transform.transform(Rect)) != Rect 501 // in which case we need to make sure the final rect is clipped to the 502 // display bounds. 503 activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop); 504 // mark regions outside the crop as transparent 505 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top)); 506 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, 507 s.active.w, s.active.h)); 508 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, 509 activeCrop.left, activeCrop.bottom)); 510 activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top, 511 s.active.w, activeCrop.bottom)); 512 } 513 Rect frame(s.transform.transform(computeBounds(activeTransparentRegion))); 514 frame.intersect(hw->getViewport(), &frame); 515 const Transform& tr(hw->getTransform()); 516 layer.setFrame(tr.transform(frame)); 517 layer.setCrop(computeCrop(hw)); 518 layer.setPlaneAlpha(s.alpha); 519 520 /* 521 * Transformations are applied in this order: 522 * 1) buffer orientation/flip/mirror 523 * 2) state transformation (window manager) 524 * 3) layer orientation (screen orientation) 525 * (NOTE: the matrices are multiplied in reverse order) 526 */ 527 528 const Transform bufferOrientation(mCurrentTransform); 529 Transform transform(tr * s.transform * bufferOrientation); 530 531 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 532 /* 533 * the code below applies the display's inverse transform to the buffer 534 */ 535 uint32_t invTransform = hw->getOrientationTransform(); 536 uint32_t t_orientation = transform.getOrientation(); 537 // calculate the inverse transform 538 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 539 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 540 NATIVE_WINDOW_TRANSFORM_FLIP_H; 541 // If the transform has been rotated the axis of flip has been swapped 542 // so we need to swap which flip operations we are performing 543 bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; 544 bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; 545 if (is_h_flipped != is_v_flipped) { 546 t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 547 NATIVE_WINDOW_TRANSFORM_FLIP_H; 548 } 549 } 550 // and apply to the current transform 551 transform = Transform(t_orientation) * Transform(invTransform); 552 } 553 554 // this gives us only the "orientation" component of the transform 555 const uint32_t orientation = transform.getOrientation(); 556 if (orientation & Transform::ROT_INVALID) { 557 // we can only handle simple transformation 558 layer.setSkip(true); 559 } else { 560 layer.setTransform(orientation); 561 } 562} 563 564void Layer::setPerFrameData(const sp<const DisplayDevice>& hw, 565 HWComposer::HWCLayerInterface& layer) { 566 // we have to set the visible region on every frame because 567 // we currently free it during onLayerDisplayed(), which is called 568 // after HWComposer::commit() -- every frame. 569 // Apply this display's projection's viewport to the visible region 570 // before giving it to the HWC HAL. 571 const Transform& tr = hw->getTransform(); 572 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport())); 573 layer.setVisibleRegionScreen(visible); 574 layer.setSurfaceDamage(surfaceDamageRegion); 575 576 if (mSidebandStream.get()) { 577 layer.setSidebandStream(mSidebandStream); 578 } else { 579 // NOTE: buffer can be NULL if the client never drew into this 580 // layer yet, or if we ran out of memory 581 layer.setBuffer(mActiveBuffer); 582 } 583} 584 585void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */, 586 HWComposer::HWCLayerInterface& layer) { 587 int fenceFd = -1; 588 589 // TODO: there is a possible optimization here: we only need to set the 590 // acquire fence the first time a new buffer is acquired on EACH display. 591 592 if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) { 593 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence(); 594 if (fence->isValid()) { 595 fenceFd = fence->dup(); 596 if (fenceFd == -1) { 597 ALOGW("failed to dup layer fence, skipping sync: %d", errno); 598 } 599 } 600 } 601 layer.setAcquireFenceFd(fenceFd); 602} 603 604Rect Layer::getPosition( 605 const sp<const DisplayDevice>& hw) 606{ 607 // this gives us only the "orientation" component of the transform 608 const State& s(getCurrentState()); 609 610 // apply the layer's transform, followed by the display's global transform 611 // here we're guaranteed that the layer's transform preserves rects 612 Rect win(s.active.w, s.active.h); 613 if (!s.active.crop.isEmpty()) { 614 win.intersect(s.active.crop, &win); 615 } 616 // subtract the transparent region and snap to the bounds 617 Rect bounds = reduce(win, s.activeTransparentRegion); 618 Rect frame(s.transform.transform(bounds)); 619 frame.intersect(hw->getViewport(), &frame); 620 const Transform& tr(hw->getTransform()); 621 return Rect(tr.transform(frame)); 622} 623 624// --------------------------------------------------------------------------- 625// drawing... 626// --------------------------------------------------------------------------- 627 628void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const { 629 onDraw(hw, clip, false); 630} 631 632void Layer::draw(const sp<const DisplayDevice>& hw, 633 bool useIdentityTransform) const { 634 onDraw(hw, Region(hw->bounds()), useIdentityTransform); 635} 636 637void Layer::draw(const sp<const DisplayDevice>& hw) const { 638 onDraw(hw, Region(hw->bounds()), false); 639} 640 641void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip, 642 bool useIdentityTransform) const 643{ 644 ATRACE_CALL(); 645 646 if (CC_UNLIKELY(mActiveBuffer == 0)) { 647 // the texture has not been created yet, this Layer has 648 // in fact never been drawn into. This happens frequently with 649 // SurfaceView because the WindowManager can't know when the client 650 // has drawn the first time. 651 652 // If there is nothing under us, we paint the screen in black, otherwise 653 // we just skip this update. 654 655 // figure out if there is something below us 656 Region under; 657 const SurfaceFlinger::LayerVector& drawingLayers( 658 mFlinger->mDrawingState.layersSortedByZ); 659 const size_t count = drawingLayers.size(); 660 for (size_t i=0 ; i<count ; ++i) { 661 const sp<Layer>& layer(drawingLayers[i]); 662 if (layer.get() == static_cast<Layer const*>(this)) 663 break; 664 under.orSelf( hw->getTransform().transform(layer->visibleRegion) ); 665 } 666 // if not everything below us is covered, we plug the holes! 667 Region holes(clip.subtract(under)); 668 if (!holes.isEmpty()) { 669 clearWithOpenGL(hw, holes, 0, 0, 0, 1); 670 } 671 return; 672 } 673 674 // Bind the current buffer to the GL texture, and wait for it to be 675 // ready for us to draw into. 676 status_t err = mSurfaceFlingerConsumer->bindTextureImage(); 677 if (err != NO_ERROR) { 678 ALOGW("onDraw: bindTextureImage failed (err=%d)", err); 679 // Go ahead and draw the buffer anyway; no matter what we do the screen 680 // is probably going to have something visibly wrong. 681 } 682 683 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure()); 684 685 RenderEngine& engine(mFlinger->getRenderEngine()); 686 687 if (!blackOutLayer) { 688 // TODO: we could be more subtle with isFixedSize() 689 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize(); 690 691 // Query the texture matrix given our current filtering mode. 692 float textureMatrix[16]; 693 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering); 694 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix); 695 696 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 697 698 /* 699 * the code below applies the display's inverse transform to the texture transform 700 */ 701 702 // create a 4x4 transform matrix from the display transform flags 703 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1); 704 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1); 705 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1); 706 707 mat4 tr; 708 uint32_t transform = hw->getOrientationTransform(); 709 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) 710 tr = tr * rot90; 711 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) 712 tr = tr * flipH; 713 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) 714 tr = tr * flipV; 715 716 // calculate the inverse 717 tr = inverse(tr); 718 719 // and finally apply it to the original texture matrix 720 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); 721 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); 722 } 723 724 // Set things up for texturing. 725 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); 726 mTexture.setFiltering(useFiltering); 727 mTexture.setMatrix(textureMatrix); 728 729 engine.setupLayerTexturing(mTexture); 730 } else { 731 engine.setupLayerBlackedOut(); 732 } 733 drawWithOpenGL(hw, clip, useIdentityTransform); 734 engine.disableTexturing(); 735} 736 737 738void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw, 739 const Region& /* clip */, float red, float green, float blue, 740 float alpha) const 741{ 742 RenderEngine& engine(mFlinger->getRenderEngine()); 743 computeGeometry(hw, mMesh, false); 744 engine.setupFillWithColor(red, green, blue, alpha); 745 engine.drawMesh(mMesh); 746} 747 748void Layer::clearWithOpenGL( 749 const sp<const DisplayDevice>& hw, const Region& clip) const { 750 clearWithOpenGL(hw, clip, 0,0,0,0); 751} 752 753void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw, 754 const Region& /* clip */, bool useIdentityTransform) const { 755 const State& s(getDrawingState()); 756 757 computeGeometry(hw, mMesh, useIdentityTransform); 758 759 /* 760 * NOTE: the way we compute the texture coordinates here produces 761 * different results than when we take the HWC path -- in the later case 762 * the "source crop" is rounded to texel boundaries. 763 * This can produce significantly different results when the texture 764 * is scaled by a large amount. 765 * 766 * The GL code below is more logical (imho), and the difference with 767 * HWC is due to a limitation of the HWC API to integers -- a question 768 * is suspend is whether we should ignore this problem or revert to 769 * GL composition when a buffer scaling is applied (maybe with some 770 * minimal value)? Or, we could make GL behave like HWC -- but this feel 771 * like more of a hack. 772 */ 773 const Rect win(computeBounds()); 774 775 float left = float(win.left) / float(s.active.w); 776 float top = float(win.top) / float(s.active.h); 777 float right = float(win.right) / float(s.active.w); 778 float bottom = float(win.bottom) / float(s.active.h); 779 780 // TODO: we probably want to generate the texture coords with the mesh 781 // here we assume that we only have 4 vertices 782 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>()); 783 texCoords[0] = vec2(left, 1.0f - top); 784 texCoords[1] = vec2(left, 1.0f - bottom); 785 texCoords[2] = vec2(right, 1.0f - bottom); 786 texCoords[3] = vec2(right, 1.0f - top); 787 788 RenderEngine& engine(mFlinger->getRenderEngine()); 789 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha); 790 engine.drawMesh(mMesh); 791 engine.disableBlending(); 792} 793 794uint32_t Layer::getProducerStickyTransform() const { 795 int producerStickyTransform = 0; 796 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform); 797 if (ret != OK) { 798 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__, 799 strerror(-ret), ret); 800 return 0; 801 } 802 return static_cast<uint32_t>(producerStickyTransform); 803} 804 805void Layer::addSyncPoint(std::shared_ptr<SyncPoint> point) { 806 uint64_t headFrameNumber = 0; 807 { 808 Mutex::Autolock lock(mQueueItemLock); 809 if (!mQueueItems.empty()) { 810 headFrameNumber = mQueueItems[0].mFrameNumber; 811 } else { 812 headFrameNumber = mLastFrameNumberReceived; 813 } 814 } 815 816 if (point->getFrameNumber() <= headFrameNumber) { 817 point->setFrameAvailable(); 818 } else { 819 mLocalSyncPoints.push_back(std::move(point)); 820 } 821} 822 823void Layer::setFiltering(bool filtering) { 824 mFiltering = filtering; 825} 826 827bool Layer::getFiltering() const { 828 return mFiltering; 829} 830 831// As documented in libhardware header, formats in the range 832// 0x100 - 0x1FF are specific to the HAL implementation, and 833// are known to have no alpha channel 834// TODO: move definition for device-specific range into 835// hardware.h, instead of using hard-coded values here. 836#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) 837 838bool Layer::getOpacityForFormat(uint32_t format) { 839 if (HARDWARE_IS_DEVICE_FORMAT(format)) { 840 return true; 841 } 842 switch (format) { 843 case HAL_PIXEL_FORMAT_RGBA_8888: 844 case HAL_PIXEL_FORMAT_BGRA_8888: 845 return false; 846 } 847 // in all other case, we have no blending (also for unknown formats) 848 return true; 849} 850 851// ---------------------------------------------------------------------------- 852// local state 853// ---------------------------------------------------------------------------- 854 855void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, 856 bool useIdentityTransform) const 857{ 858 const Layer::State& s(getDrawingState()); 859 const Transform tr(useIdentityTransform ? 860 hw->getTransform() : hw->getTransform() * s.transform); 861 const uint32_t hw_h = hw->getHeight(); 862 Rect win(s.active.w, s.active.h); 863 if (!s.active.crop.isEmpty()) { 864 win.intersect(s.active.crop, &win); 865 } 866 // subtract the transparent region and snap to the bounds 867 win = reduce(win, s.activeTransparentRegion); 868 869 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); 870 position[0] = tr.transform(win.left, win.top); 871 position[1] = tr.transform(win.left, win.bottom); 872 position[2] = tr.transform(win.right, win.bottom); 873 position[3] = tr.transform(win.right, win.top); 874 for (size_t i=0 ; i<4 ; i++) { 875 position[i].y = hw_h - position[i].y; 876 } 877} 878 879bool Layer::isOpaque(const Layer::State& s) const 880{ 881 // if we don't have a buffer yet, we're translucent regardless of the 882 // layer's opaque flag. 883 if (mActiveBuffer == 0) { 884 return false; 885 } 886 887 // if the layer has the opaque flag, then we're always opaque, 888 // otherwise we use the current buffer's format. 889 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; 890} 891 892bool Layer::isSecure() const 893{ 894 const Layer::State& s(mDrawingState); 895 return (s.flags & layer_state_t::eLayerSecure); 896} 897 898bool Layer::isProtected() const 899{ 900 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); 901 return (activeBuffer != 0) && 902 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); 903} 904 905bool Layer::isFixedSize() const { 906 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; 907} 908 909bool Layer::isCropped() const { 910 return !mCurrentCrop.isEmpty(); 911} 912 913bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const { 914 return mNeedsFiltering || hw->needsFiltering(); 915} 916 917void Layer::setVisibleRegion(const Region& visibleRegion) { 918 // always called from main thread 919 this->visibleRegion = visibleRegion; 920} 921 922void Layer::setCoveredRegion(const Region& coveredRegion) { 923 // always called from main thread 924 this->coveredRegion = coveredRegion; 925} 926 927void Layer::setVisibleNonTransparentRegion(const Region& 928 setVisibleNonTransparentRegion) { 929 // always called from main thread 930 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion; 931} 932 933// ---------------------------------------------------------------------------- 934// transaction 935// ---------------------------------------------------------------------------- 936 937void Layer::pushPendingState() { 938 if (!mCurrentState.modified) { 939 return; 940 } 941 942 Mutex::Autolock lock(mPendingStateMutex); 943 944 // If this transaction is waiting on the receipt of a frame, generate a sync 945 // point and send it to the remote layer. 946 if (mCurrentState.handle != nullptr) { 947 sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get()); 948 sp<Layer> handleLayer = handle->owner.promote(); 949 if (handleLayer == nullptr) { 950 ALOGE("[%s] Unable to promote Layer handle", mName.string()); 951 // If we can't promote the layer we are intended to wait on, 952 // then it is expired or otherwise invalid. Allow this transaction 953 // to be applied as per normal (no synchronization). 954 mCurrentState.handle = nullptr; 955 } 956 957 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber); 958 handleLayer->addSyncPoint(syncPoint); 959 mRemoteSyncPoints.push_back(std::move(syncPoint)); 960 961 // Wake us up to check if the frame has been received 962 setTransactionFlags(eTransactionNeeded); 963 } 964 mPendingStates.push_back(mCurrentState); 965} 966 967void Layer::popPendingState() { 968 auto oldFlags = mCurrentState.flags; 969 mCurrentState = mPendingStates[0]; 970 mCurrentState.flags = (oldFlags & ~mCurrentState.mask) | 971 (mCurrentState.flags & mCurrentState.mask); 972 973 mPendingStates.removeAt(0); 974} 975 976bool Layer::applyPendingStates() { 977 Mutex::Autolock lock(mPendingStateMutex); 978 979 bool stateUpdateAvailable = false; 980 while (!mPendingStates.empty()) { 981 if (mPendingStates[0].handle != nullptr) { 982 if (mRemoteSyncPoints.empty()) { 983 // If we don't have a sync point for this, apply it anyway. It 984 // will be visually wrong, but it should keep us from getting 985 // into too much trouble. 986 ALOGE("[%s] No local sync point found", mName.string()); 987 popPendingState(); 988 stateUpdateAvailable = true; 989 continue; 990 } 991 992 if (mRemoteSyncPoints.front()->frameIsAvailable()) { 993 // Apply the state update 994 popPendingState(); 995 stateUpdateAvailable = true; 996 997 // Signal our end of the sync point and then dispose of it 998 mRemoteSyncPoints.front()->setTransactionApplied(); 999 mRemoteSyncPoints.pop_front(); 1000 } 1001 break; 1002 } else { 1003 popPendingState(); 1004 stateUpdateAvailable = true; 1005 } 1006 } 1007 1008 // If we still have pending updates, wake SurfaceFlinger back up and point 1009 // it at this layer so we can process them 1010 if (!mPendingStates.empty()) { 1011 setTransactionFlags(eTransactionNeeded); 1012 mFlinger->setTransactionFlags(eTraversalNeeded); 1013 } 1014 1015 mCurrentState.modified = false; 1016 return stateUpdateAvailable; 1017} 1018 1019void Layer::notifyAvailableFrames() { 1020 Mutex::Autolock lock(mAvailableFrameMutex); 1021 for (auto frame : mAvailableFrames) { 1022 frame->setFrameAvailable(); 1023 } 1024} 1025 1026uint32_t Layer::doTransaction(uint32_t flags) { 1027 ATRACE_CALL(); 1028 1029 pushPendingState(); 1030 if (!applyPendingStates()) { 1031 return 0; 1032 } 1033 1034 const Layer::State& s(getDrawingState()); 1035 const Layer::State& c(getCurrentState()); 1036 1037 const bool sizeChanged = (c.requested.w != s.requested.w) || 1038 (c.requested.h != s.requested.h); 1039 1040 if (sizeChanged) { 1041 // the size changed, we need to ask our client to request a new buffer 1042 ALOGD_IF(DEBUG_RESIZE, 1043 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n" 1044 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1045 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n" 1046 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1047 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", 1048 this, getName().string(), mCurrentTransform, mCurrentScalingMode, 1049 c.active.w, c.active.h, 1050 c.active.crop.left, 1051 c.active.crop.top, 1052 c.active.crop.right, 1053 c.active.crop.bottom, 1054 c.active.crop.getWidth(), 1055 c.active.crop.getHeight(), 1056 c.requested.w, c.requested.h, 1057 c.requested.crop.left, 1058 c.requested.crop.top, 1059 c.requested.crop.right, 1060 c.requested.crop.bottom, 1061 c.requested.crop.getWidth(), 1062 c.requested.crop.getHeight(), 1063 s.active.w, s.active.h, 1064 s.active.crop.left, 1065 s.active.crop.top, 1066 s.active.crop.right, 1067 s.active.crop.bottom, 1068 s.active.crop.getWidth(), 1069 s.active.crop.getHeight(), 1070 s.requested.w, s.requested.h, 1071 s.requested.crop.left, 1072 s.requested.crop.top, 1073 s.requested.crop.right, 1074 s.requested.crop.bottom, 1075 s.requested.crop.getWidth(), 1076 s.requested.crop.getHeight()); 1077 1078 // record the new size, form this point on, when the client request 1079 // a buffer, it'll get the new size. 1080 mSurfaceFlingerConsumer->setDefaultBufferSize( 1081 c.requested.w, c.requested.h); 1082 } 1083 1084 if (!isFixedSize()) { 1085 1086 const bool resizePending = (c.requested.w != c.active.w) || 1087 (c.requested.h != c.active.h); 1088 1089 if (resizePending && mSidebandStream == NULL) { 1090 // don't let Layer::doTransaction update the drawing state 1091 // if we have a pending resize, unless we are in fixed-size mode. 1092 // the drawing state will be updated only once we receive a buffer 1093 // with the correct size. 1094 // 1095 // in particular, we want to make sure the clip (which is part 1096 // of the geometry state) is latched together with the size but is 1097 // latched immediately when no resizing is involved. 1098 // 1099 // If a sideband stream is attached, however, we want to skip this 1100 // optimization so that transactions aren't missed when a buffer 1101 // never arrives 1102 1103 flags |= eDontUpdateGeometryState; 1104 } 1105 } 1106 1107 // always set active to requested, unless we're asked not to 1108 // this is used by Layer, which special cases resizes. 1109 if (flags & eDontUpdateGeometryState) { 1110 } else { 1111 Layer::State& editCurrentState(getCurrentState()); 1112 editCurrentState.active = c.requested; 1113 } 1114 1115 if (s.active != c.active) { 1116 // invalidate and recompute the visible regions if needed 1117 flags |= Layer::eVisibleRegion; 1118 } 1119 1120 if (c.sequence != s.sequence) { 1121 // invalidate and recompute the visible regions if needed 1122 flags |= eVisibleRegion; 1123 this->contentDirty = true; 1124 1125 // we may use linear filtering, if the matrix scales us 1126 const uint8_t type = c.transform.getType(); 1127 mNeedsFiltering = (!c.transform.preserveRects() || 1128 (type >= Transform::SCALE)); 1129 } 1130 1131 // Commit the transaction 1132 commitTransaction(); 1133 return flags; 1134} 1135 1136void Layer::commitTransaction() { 1137 mDrawingState = mCurrentState; 1138} 1139 1140uint32_t Layer::getTransactionFlags(uint32_t flags) { 1141 return android_atomic_and(~flags, &mTransactionFlags) & flags; 1142} 1143 1144uint32_t Layer::setTransactionFlags(uint32_t flags) { 1145 return android_atomic_or(flags, &mTransactionFlags); 1146} 1147 1148bool Layer::setPosition(float x, float y) { 1149 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) 1150 return false; 1151 mCurrentState.sequence++; 1152 mCurrentState.transform.set(x, y); 1153 mCurrentState.modified = true; 1154 setTransactionFlags(eTransactionNeeded); 1155 return true; 1156} 1157bool Layer::setLayer(uint32_t z) { 1158 if (mCurrentState.z == z) 1159 return false; 1160 mCurrentState.sequence++; 1161 mCurrentState.z = z; 1162 mCurrentState.modified = true; 1163 setTransactionFlags(eTransactionNeeded); 1164 return true; 1165} 1166bool Layer::setSize(uint32_t w, uint32_t h) { 1167 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) 1168 return false; 1169 mCurrentState.requested.w = w; 1170 mCurrentState.requested.h = h; 1171 mCurrentState.modified = true; 1172 setTransactionFlags(eTransactionNeeded); 1173 return true; 1174} 1175bool Layer::setAlpha(uint8_t alpha) { 1176 if (mCurrentState.alpha == alpha) 1177 return false; 1178 mCurrentState.sequence++; 1179 mCurrentState.alpha = alpha; 1180 mCurrentState.modified = true; 1181 setTransactionFlags(eTransactionNeeded); 1182 return true; 1183} 1184bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) { 1185 mCurrentState.sequence++; 1186 mCurrentState.transform.set( 1187 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy); 1188 mCurrentState.modified = true; 1189 setTransactionFlags(eTransactionNeeded); 1190 return true; 1191} 1192bool Layer::setTransparentRegionHint(const Region& transparent) { 1193 mCurrentState.requestedTransparentRegion = transparent; 1194 mCurrentState.modified = true; 1195 setTransactionFlags(eTransactionNeeded); 1196 return true; 1197} 1198bool Layer::setFlags(uint8_t flags, uint8_t mask) { 1199 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); 1200 if (mCurrentState.flags == newFlags) 1201 return false; 1202 mCurrentState.sequence++; 1203 mCurrentState.flags = newFlags; 1204 mCurrentState.mask = mask; 1205 mCurrentState.modified = true; 1206 setTransactionFlags(eTransactionNeeded); 1207 return true; 1208} 1209bool Layer::setCrop(const Rect& crop) { 1210 if (mCurrentState.requested.crop == crop) 1211 return false; 1212 mCurrentState.sequence++; 1213 mCurrentState.requested.crop = crop; 1214 mCurrentState.modified = true; 1215 setTransactionFlags(eTransactionNeeded); 1216 return true; 1217} 1218 1219bool Layer::setLayerStack(uint32_t layerStack) { 1220 if (mCurrentState.layerStack == layerStack) 1221 return false; 1222 mCurrentState.sequence++; 1223 mCurrentState.layerStack = layerStack; 1224 mCurrentState.modified = true; 1225 setTransactionFlags(eTransactionNeeded); 1226 return true; 1227} 1228 1229void Layer::deferTransactionUntil(const sp<IBinder>& handle, 1230 uint64_t frameNumber) { 1231 mCurrentState.handle = handle; 1232 mCurrentState.frameNumber = frameNumber; 1233 // We don't set eTransactionNeeded, because just receiving a deferral 1234 // request without any other state updates shouldn't actually induce a delay 1235 mCurrentState.modified = true; 1236 pushPendingState(); 1237 mCurrentState.modified = false; 1238} 1239 1240void Layer::useSurfaceDamage() { 1241 if (mFlinger->mForceFullDamage) { 1242 surfaceDamageRegion = Region::INVALID_REGION; 1243 } else { 1244 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage(); 1245 } 1246} 1247 1248void Layer::useEmptyDamage() { 1249 surfaceDamageRegion.clear(); 1250} 1251 1252// ---------------------------------------------------------------------------- 1253// pageflip handling... 1254// ---------------------------------------------------------------------------- 1255 1256bool Layer::shouldPresentNow(const DispSync& dispSync) const { 1257 if (mSidebandStreamChanged || mSingleBufferMode) { 1258 return true; 1259 } 1260 1261 Mutex::Autolock lock(mQueueItemLock); 1262 if (mQueueItems.empty()) { 1263 return false; 1264 } 1265 auto timestamp = mQueueItems[0].mTimestamp; 1266 nsecs_t expectedPresent = 1267 mSurfaceFlingerConsumer->computeExpectedPresent(dispSync); 1268 1269 // Ignore timestamps more than a second in the future 1270 bool isPlausible = timestamp < (expectedPresent + s2ns(1)); 1271 ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible " 1272 "relative to expectedPresent %" PRId64, mName.string(), timestamp, 1273 expectedPresent); 1274 1275 bool isDue = timestamp < expectedPresent; 1276 return isDue || !isPlausible; 1277} 1278 1279bool Layer::onPreComposition() { 1280 mRefreshPending = false; 1281 return mQueuedFrames > 0 || mSidebandStreamChanged || mSingleBufferMode; 1282} 1283 1284void Layer::onPostComposition() { 1285 if (mFrameLatencyNeeded) { 1286 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp(); 1287 mFrameTracker.setDesiredPresentTime(desiredPresentTime); 1288 1289 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence(); 1290 if (frameReadyFence->isValid()) { 1291 mFrameTracker.setFrameReadyFence(frameReadyFence); 1292 } else { 1293 // There was no fence for this frame, so assume that it was ready 1294 // to be presented at the desired present time. 1295 mFrameTracker.setFrameReadyTime(desiredPresentTime); 1296 } 1297 1298 const HWComposer& hwc = mFlinger->getHwComposer(); 1299 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY); 1300 if (presentFence->isValid()) { 1301 mFrameTracker.setActualPresentFence(presentFence); 1302 } else { 1303 // The HWC doesn't support present fences, so use the refresh 1304 // timestamp instead. 1305 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY); 1306 mFrameTracker.setActualPresentTime(presentTime); 1307 } 1308 1309 mFrameTracker.advanceFrame(); 1310 mFrameLatencyNeeded = false; 1311 } 1312} 1313 1314bool Layer::isVisible() const { 1315 const Layer::State& s(mDrawingState); 1316 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha 1317 && (mActiveBuffer != NULL || mSidebandStream != NULL); 1318} 1319 1320Region Layer::latchBuffer(bool& recomputeVisibleRegions) 1321{ 1322 ATRACE_CALL(); 1323 1324 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) { 1325 // mSidebandStreamChanged was true 1326 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream(); 1327 if (mSidebandStream != NULL) { 1328 setTransactionFlags(eTransactionNeeded); 1329 mFlinger->setTransactionFlags(eTraversalNeeded); 1330 } 1331 recomputeVisibleRegions = true; 1332 1333 const State& s(getDrawingState()); 1334 return s.transform.transform(Region(Rect(s.active.w, s.active.h))); 1335 } 1336 1337 Region outDirtyRegion; 1338 if (mQueuedFrames > 0 || mSingleBufferMode) { 1339 1340 // if we've already called updateTexImage() without going through 1341 // a composition step, we have to skip this layer at this point 1342 // because we cannot call updateTeximage() without a corresponding 1343 // compositionComplete() call. 1344 // we'll trigger an update in onPreComposition(). 1345 if (mRefreshPending) { 1346 return outDirtyRegion; 1347 } 1348 1349 // Capture the old state of the layer for comparisons later 1350 const State& s(getDrawingState()); 1351 const bool oldOpacity = isOpaque(s); 1352 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer; 1353 1354 struct Reject : public SurfaceFlingerConsumer::BufferRejecter { 1355 Layer::State& front; 1356 Layer::State& current; 1357 bool& recomputeVisibleRegions; 1358 bool stickyTransformSet; 1359 Reject(Layer::State& front, Layer::State& current, 1360 bool& recomputeVisibleRegions, bool stickySet) 1361 : front(front), current(current), 1362 recomputeVisibleRegions(recomputeVisibleRegions), 1363 stickyTransformSet(stickySet) { 1364 } 1365 1366 virtual bool reject(const sp<GraphicBuffer>& buf, 1367 const BufferItem& item) { 1368 if (buf == NULL) { 1369 return false; 1370 } 1371 1372 uint32_t bufWidth = buf->getWidth(); 1373 uint32_t bufHeight = buf->getHeight(); 1374 1375 // check that we received a buffer of the right size 1376 // (Take the buffer's orientation into account) 1377 if (item.mTransform & Transform::ROT_90) { 1378 swap(bufWidth, bufHeight); 1379 } 1380 1381 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; 1382 if (front.active != front.requested) { 1383 1384 if (isFixedSize || 1385 (bufWidth == front.requested.w && 1386 bufHeight == front.requested.h)) 1387 { 1388 // Here we pretend the transaction happened by updating the 1389 // current and drawing states. Drawing state is only accessed 1390 // in this thread, no need to have it locked 1391 front.active = front.requested; 1392 1393 // We also need to update the current state so that 1394 // we don't end-up overwriting the drawing state with 1395 // this stale current state during the next transaction 1396 // 1397 // NOTE: We don't need to hold the transaction lock here 1398 // because State::active is only accessed from this thread. 1399 current.active = front.active; 1400 1401 // recompute visible region 1402 recomputeVisibleRegions = true; 1403 } 1404 1405 ALOGD_IF(DEBUG_RESIZE, 1406 "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n" 1407 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1408 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", 1409 bufWidth, bufHeight, item.mTransform, item.mScalingMode, 1410 front.active.w, front.active.h, 1411 front.active.crop.left, 1412 front.active.crop.top, 1413 front.active.crop.right, 1414 front.active.crop.bottom, 1415 front.active.crop.getWidth(), 1416 front.active.crop.getHeight(), 1417 front.requested.w, front.requested.h, 1418 front.requested.crop.left, 1419 front.requested.crop.top, 1420 front.requested.crop.right, 1421 front.requested.crop.bottom, 1422 front.requested.crop.getWidth(), 1423 front.requested.crop.getHeight()); 1424 } 1425 1426 if (!isFixedSize && !stickyTransformSet) { 1427 if (front.active.w != bufWidth || 1428 front.active.h != bufHeight) { 1429 // reject this buffer 1430 ALOGE("rejecting buffer: bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}", 1431 bufWidth, bufHeight, front.active.w, front.active.h); 1432 return true; 1433 } 1434 } 1435 1436 // if the transparent region has changed (this test is 1437 // conservative, but that's fine, worst case we're doing 1438 // a bit of extra work), we latch the new one and we 1439 // trigger a visible-region recompute. 1440 if (!front.activeTransparentRegion.isTriviallyEqual( 1441 front.requestedTransparentRegion)) { 1442 front.activeTransparentRegion = front.requestedTransparentRegion; 1443 1444 // We also need to update the current state so that 1445 // we don't end-up overwriting the drawing state with 1446 // this stale current state during the next transaction 1447 // 1448 // NOTE: We don't need to hold the transaction lock here 1449 // because State::active is only accessed from this thread. 1450 current.activeTransparentRegion = front.activeTransparentRegion; 1451 1452 // recompute visible region 1453 recomputeVisibleRegions = true; 1454 } 1455 1456 return false; 1457 } 1458 }; 1459 1460 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions, 1461 getProducerStickyTransform() != 0); 1462 1463 uint64_t maxFrameNumber = 0; 1464 uint64_t headFrameNumber = 0; 1465 { 1466 Mutex::Autolock lock(mQueueItemLock); 1467 maxFrameNumber = mLastFrameNumberReceived; 1468 if (!mQueueItems.empty()) { 1469 headFrameNumber = mQueueItems[0].mFrameNumber; 1470 } 1471 } 1472 1473 bool availableFramesEmpty = true; 1474 { 1475 Mutex::Autolock lock(mAvailableFrameMutex); 1476 availableFramesEmpty = mAvailableFrames.empty(); 1477 } 1478 if (!availableFramesEmpty) { 1479 Mutex::Autolock lock(mAvailableFrameMutex); 1480 bool matchingFramesFound = false; 1481 bool allTransactionsApplied = true; 1482 for (auto& frame : mAvailableFrames) { 1483 if (headFrameNumber != frame->getFrameNumber()) { 1484 break; 1485 } 1486 matchingFramesFound = true; 1487 allTransactionsApplied &= frame->transactionIsApplied(); 1488 } 1489 if (matchingFramesFound && !allTransactionsApplied) { 1490 mFlinger->signalLayerUpdate(); 1491 return outDirtyRegion; 1492 } 1493 } 1494 1495 // This boolean is used to make sure that SurfaceFlinger's shadow copy 1496 // of the buffer queue isn't modified when the buffer queue is returning 1497 // BufferItem's that weren't actually queued. This can happen in single 1498 // buffer mode. 1499 bool queuedBuffer = false; 1500 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r, 1501 mFlinger->mPrimaryDispSync, &mSingleBufferMode, &queuedBuffer, 1502 maxFrameNumber); 1503 if (updateResult == BufferQueue::PRESENT_LATER) { 1504 // Producer doesn't want buffer to be displayed yet. Signal a 1505 // layer update so we check again at the next opportunity. 1506 mFlinger->signalLayerUpdate(); 1507 return outDirtyRegion; 1508 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) { 1509 // If the buffer has been rejected, remove it from the shadow queue 1510 // and return early 1511 if (queuedBuffer) { 1512 Mutex::Autolock lock(mQueueItemLock); 1513 mQueueItems.removeAt(0); 1514 android_atomic_dec(&mQueuedFrames); 1515 } 1516 return outDirtyRegion; 1517 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) { 1518 // This can occur if something goes wrong when trying to create the 1519 // EGLImage for this buffer. If this happens, the buffer has already 1520 // been released, so we need to clean up the queue and bug out 1521 // early. 1522 if (queuedBuffer) { 1523 Mutex::Autolock lock(mQueueItemLock); 1524 mQueueItems.clear(); 1525 android_atomic_and(0, &mQueuedFrames); 1526 } 1527 1528 // Once we have hit this state, the shadow queue may no longer 1529 // correctly reflect the incoming BufferQueue's contents, so even if 1530 // updateTexImage starts working, the only safe course of action is 1531 // to continue to ignore updates. 1532 mUpdateTexImageFailed = true; 1533 1534 return outDirtyRegion; 1535 } 1536 1537 if (queuedBuffer) { 1538 // Autolock scope 1539 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); 1540 1541 Mutex::Autolock lock(mQueueItemLock); 1542 1543 // Remove any stale buffers that have been dropped during 1544 // updateTexImage 1545 while (mQueueItems[0].mFrameNumber != currentFrameNumber) { 1546 mQueueItems.removeAt(0); 1547 android_atomic_dec(&mQueuedFrames); 1548 } 1549 1550 mQueueItems.removeAt(0); 1551 } 1552 1553 1554 // Decrement the queued-frames count. Signal another event if we 1555 // have more frames pending. 1556 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) 1557 || mSingleBufferMode) { 1558 mFlinger->signalLayerUpdate(); 1559 } 1560 1561 if (!availableFramesEmpty) { 1562 Mutex::Autolock lock(mAvailableFrameMutex); 1563 auto frameNumber = mSurfaceFlingerConsumer->getFrameNumber(); 1564 while (!mAvailableFrames.empty() && 1565 frameNumber == mAvailableFrames.front()->getFrameNumber()) { 1566 mAvailableFrames.pop_front(); 1567 } 1568 } 1569 1570 if (updateResult != NO_ERROR) { 1571 // something happened! 1572 recomputeVisibleRegions = true; 1573 return outDirtyRegion; 1574 } 1575 1576 // update the active buffer 1577 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(); 1578 if (mActiveBuffer == NULL) { 1579 // this can only happen if the very first buffer was rejected. 1580 return outDirtyRegion; 1581 } 1582 1583 mRefreshPending = true; 1584 mFrameLatencyNeeded = true; 1585 if (oldActiveBuffer == NULL) { 1586 // the first time we receive a buffer, we need to trigger a 1587 // geometry invalidation. 1588 recomputeVisibleRegions = true; 1589 } 1590 1591 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop()); 1592 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform()); 1593 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode()); 1594 if ((crop != mCurrentCrop) || 1595 (transform != mCurrentTransform) || 1596 (scalingMode != mCurrentScalingMode)) 1597 { 1598 mCurrentCrop = crop; 1599 mCurrentTransform = transform; 1600 mCurrentScalingMode = scalingMode; 1601 recomputeVisibleRegions = true; 1602 } 1603 1604 if (oldActiveBuffer != NULL) { 1605 uint32_t bufWidth = mActiveBuffer->getWidth(); 1606 uint32_t bufHeight = mActiveBuffer->getHeight(); 1607 if (bufWidth != uint32_t(oldActiveBuffer->width) || 1608 bufHeight != uint32_t(oldActiveBuffer->height)) { 1609 recomputeVisibleRegions = true; 1610 } 1611 } 1612 1613 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); 1614 if (oldOpacity != isOpaque(s)) { 1615 recomputeVisibleRegions = true; 1616 } 1617 1618 // FIXME: postedRegion should be dirty & bounds 1619 Region dirtyRegion(Rect(s.active.w, s.active.h)); 1620 1621 // transform the dirty region to window-manager space 1622 outDirtyRegion = (s.transform.transform(dirtyRegion)); 1623 } 1624 return outDirtyRegion; 1625} 1626 1627uint32_t Layer::getEffectiveUsage(uint32_t usage) const 1628{ 1629 // TODO: should we do something special if mSecure is set? 1630 if (mProtectedByApp) { 1631 // need a hardware-protected path to external video sink 1632 usage |= GraphicBuffer::USAGE_PROTECTED; 1633 } 1634 if (mPotentialCursor) { 1635 usage |= GraphicBuffer::USAGE_CURSOR; 1636 } 1637 usage |= GraphicBuffer::USAGE_HW_COMPOSER; 1638 return usage; 1639} 1640 1641void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const { 1642 uint32_t orientation = 0; 1643 if (!mFlinger->mDebugDisableTransformHint) { 1644 // The transform hint is used to improve performance, but we can 1645 // only have a single transform hint, it cannot 1646 // apply to all displays. 1647 const Transform& planeTransform(hw->getTransform()); 1648 orientation = planeTransform.getOrientation(); 1649 if (orientation & Transform::ROT_INVALID) { 1650 orientation = 0; 1651 } 1652 } 1653 mSurfaceFlingerConsumer->setTransformHint(orientation); 1654} 1655 1656// ---------------------------------------------------------------------------- 1657// debugging 1658// ---------------------------------------------------------------------------- 1659 1660void Layer::dump(String8& result, Colorizer& colorizer) const 1661{ 1662 const Layer::State& s(getDrawingState()); 1663 1664 colorizer.colorize(result, Colorizer::GREEN); 1665 result.appendFormat( 1666 "+ %s %p (%s)\n", 1667 getTypeId(), this, getName().string()); 1668 colorizer.reset(result); 1669 1670 s.activeTransparentRegion.dump(result, "transparentRegion"); 1671 visibleRegion.dump(result, "visibleRegion"); 1672 surfaceDamageRegion.dump(result, "surfaceDamageRegion"); 1673 sp<Client> client(mClientRef.promote()); 1674 1675 result.appendFormat( " " 1676 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), " 1677 "isOpaque=%1d, invalidate=%1d, " 1678 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" 1679 " client=%p\n", 1680 s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h, 1681 s.active.crop.left, s.active.crop.top, 1682 s.active.crop.right, s.active.crop.bottom, 1683 isOpaque(s), contentDirty, 1684 s.alpha, s.flags, 1685 s.transform[0][0], s.transform[0][1], 1686 s.transform[1][0], s.transform[1][1], 1687 client.get()); 1688 1689 sp<const GraphicBuffer> buf0(mActiveBuffer); 1690 uint32_t w0=0, h0=0, s0=0, f0=0; 1691 if (buf0 != 0) { 1692 w0 = buf0->getWidth(); 1693 h0 = buf0->getHeight(); 1694 s0 = buf0->getStride(); 1695 f0 = buf0->format; 1696 } 1697 result.appendFormat( 1698 " " 1699 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X]," 1700 " queued-frames=%d, mRefreshPending=%d\n", 1701 mFormat, w0, h0, s0,f0, 1702 mQueuedFrames, mRefreshPending); 1703 1704 if (mSurfaceFlingerConsumer != 0) { 1705 mSurfaceFlingerConsumer->dump(result, " "); 1706 } 1707} 1708 1709void Layer::dumpFrameStats(String8& result) const { 1710 mFrameTracker.dumpStats(result); 1711} 1712 1713void Layer::clearFrameStats() { 1714 mFrameTracker.clearStats(); 1715} 1716 1717void Layer::logFrameStats() { 1718 mFrameTracker.logAndResetStats(mName); 1719} 1720 1721void Layer::getFrameStats(FrameStats* outStats) const { 1722 mFrameTracker.getStats(outStats); 1723} 1724 1725// --------------------------------------------------------------------------- 1726 1727Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger, 1728 const sp<Layer>& layer) 1729 : mFlinger(flinger), mLayer(layer) { 1730} 1731 1732Layer::LayerCleaner::~LayerCleaner() { 1733 // destroy client resources 1734 mFlinger->onLayerDestroyed(mLayer); 1735} 1736 1737// --------------------------------------------------------------------------- 1738}; // namespace android 1739 1740#if defined(__gl_h_) 1741#error "don't include gl/gl.h in this file" 1742#endif 1743 1744#if defined(__gl2_h_) 1745#error "don't include gl2/gl2.h in this file" 1746#endif 1747