Layer.cpp revision 4125a4ffaf374ca9c0773f256998557d3325343e
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/StopWatch.h> 31#include <utils/Trace.h> 32 33#include <ui/GraphicBuffer.h> 34#include <ui/PixelFormat.h> 35 36#include <gui/Surface.h> 37 38#include "clz.h" 39#include "Colorizer.h" 40#include "DisplayDevice.h" 41#include "Layer.h" 42#include "SurfaceFlinger.h" 43#include "SurfaceTextureLayer.h" 44 45#include "DisplayHardware/HWComposer.h" 46 47#include "RenderEngine/RenderEngine.h" 48 49#define DEBUG_RESIZE 0 50 51namespace android { 52 53// --------------------------------------------------------------------------- 54 55int32_t Layer::sSequence = 1; 56 57Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, 58 const String8& name, uint32_t w, uint32_t h, uint32_t flags) 59 : contentDirty(false), 60 sequence(uint32_t(android_atomic_inc(&sSequence))), 61 mFlinger(flinger), 62 mTextureName(-1U), 63 mPremultipliedAlpha(true), 64 mName("unnamed"), 65 mDebug(false), 66 mFormat(PIXEL_FORMAT_NONE), 67 mTransactionFlags(0), 68 mQueuedFrames(0), 69 mCurrentTransform(0), 70 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), 71 mCurrentOpacity(true), 72 mRefreshPending(false), 73 mFrameLatencyNeeded(false), 74 mFiltering(false), 75 mNeedsFiltering(false), 76 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2), 77 mSecure(false), 78 mProtectedByApp(false), 79 mHasSurface(false), 80 mClientRef(client) 81{ 82 mCurrentCrop.makeInvalid(); 83 mFlinger->getRenderEngine().genTextures(1, &mTextureName); 84 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); 85 86 uint32_t layerFlags = 0; 87 if (flags & ISurfaceComposerClient::eHidden) 88 layerFlags |= layer_state_t::eLayerHidden; 89 if (flags & ISurfaceComposerClient::eOpaque) 90 layerFlags |= layer_state_t::eLayerOpaque; 91 92 if (flags & ISurfaceComposerClient::eNonPremultiplied) 93 mPremultipliedAlpha = false; 94 95 mName = name; 96 97 mCurrentState.active.w = w; 98 mCurrentState.active.h = h; 99 mCurrentState.active.crop.makeInvalid(); 100 mCurrentState.z = 0; 101 mCurrentState.alpha = 0xFF; 102 mCurrentState.layerStack = 0; 103 mCurrentState.flags = layerFlags; 104 mCurrentState.sequence = 0; 105 mCurrentState.transform.set(0, 0); 106 mCurrentState.requested = mCurrentState.active; 107 108 // drawing state & current state are identical 109 mDrawingState = mCurrentState; 110 111 nsecs_t displayPeriod = 112 flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); 113 mFrameTracker.setDisplayRefreshPeriod(displayPeriod); 114} 115 116void Layer::onFirstRef() { 117 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use 118 mBufferQueue = new SurfaceTextureLayer(mFlinger); 119 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(mBufferQueue, mTextureName); 120 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); 121 mSurfaceFlingerConsumer->setFrameAvailableListener(this); 122 mSurfaceFlingerConsumer->setName(mName); 123 124#ifdef TARGET_DISABLE_TRIPLE_BUFFERING 125#warning "disabling triple buffering" 126 mSurfaceFlingerConsumer->setDefaultMaxBufferCount(2); 127#else 128 mSurfaceFlingerConsumer->setDefaultMaxBufferCount(3); 129#endif 130 131 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); 132 updateTransformHint(hw); 133} 134 135Layer::~Layer() { 136 sp<Client> c(mClientRef.promote()); 137 if (c != 0) { 138 c->detachLayer(this); 139 } 140 mFlinger->deleteTextureAsync(mTextureName); 141 mFrameTracker.logAndResetStats(mName); 142} 143 144// --------------------------------------------------------------------------- 145// callbacks 146// --------------------------------------------------------------------------- 147 148void Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw, 149 HWComposer::HWCLayerInterface* layer) { 150 if (layer) { 151 layer->onDisplayed(); 152 mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence()); 153 } 154} 155 156void Layer::onFrameAvailable() { 157 android_atomic_inc(&mQueuedFrames); 158 mFlinger->signalLayerUpdate(); 159} 160 161// called with SurfaceFlinger::mStateLock from the drawing thread after 162// the layer has been remove from the current state list (and just before 163// it's removed from the drawing state list) 164void Layer::onRemoved() { 165 mSurfaceFlingerConsumer->abandon(); 166} 167 168// --------------------------------------------------------------------------- 169// set-up 170// --------------------------------------------------------------------------- 171 172const String8& Layer::getName() const { 173 return mName; 174} 175 176status_t Layer::setBuffers( uint32_t w, uint32_t h, 177 PixelFormat format, uint32_t flags) 178{ 179 uint32_t const maxSurfaceDims = min( 180 mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); 181 182 // never allow a surface larger than what our underlying GL implementation 183 // can handle. 184 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { 185 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); 186 return BAD_VALUE; 187 } 188 189 mFormat = format; 190 191 mSecure = (flags & ISurfaceComposerClient::eSecure) ? true : false; 192 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; 193 mCurrentOpacity = getOpacityForFormat(format); 194 195 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); 196 mSurfaceFlingerConsumer->setDefaultBufferFormat(format); 197 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); 198 199 return NO_ERROR; 200} 201 202sp<IBinder> Layer::getHandle() { 203 Mutex::Autolock _l(mLock); 204 205 LOG_ALWAYS_FATAL_IF(mHasSurface, 206 "Layer::getHandle() has already been called"); 207 208 mHasSurface = true; 209 210 /* 211 * The layer handle is just a BBinder object passed to the client 212 * (remote process) -- we don't keep any reference on our side such that 213 * the dtor is called when the remote side let go of its reference. 214 * 215 * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for 216 * this layer when the handle is destroyed. 217 */ 218 219 class Handle : public BBinder, public LayerCleaner { 220 wp<const Layer> mOwner; 221 public: 222 Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer) 223 : LayerCleaner(flinger, layer), mOwner(layer) { 224 } 225 }; 226 227 return new Handle(mFlinger, this); 228} 229 230sp<IGraphicBufferProducer> Layer::getBufferQueue() const { 231 return mBufferQueue; 232} 233 234// --------------------------------------------------------------------------- 235// h/w composer set-up 236// --------------------------------------------------------------------------- 237 238Rect Layer::getContentCrop() const { 239 // this is the crop rectangle that applies to the buffer 240 // itself (as opposed to the window) 241 Rect crop; 242 if (!mCurrentCrop.isEmpty()) { 243 // if the buffer crop is defined, we use that 244 crop = mCurrentCrop; 245 } else if (mActiveBuffer != NULL) { 246 // otherwise we use the whole buffer 247 crop = mActiveBuffer->getBounds(); 248 } else { 249 // if we don't have a buffer yet, we use an empty/invalid crop 250 crop.makeInvalid(); 251 } 252 return crop; 253} 254 255static Rect reduce(const Rect& win, const Region& exclude) { 256 if (CC_LIKELY(exclude.isEmpty())) { 257 return win; 258 } 259 if (exclude.isRect()) { 260 return win.reduce(exclude.getBounds()); 261 } 262 return Region(win).subtract(exclude).getBounds(); 263} 264 265Rect Layer::computeBounds() const { 266 const Layer::State& s(getDrawingState()); 267 Rect win(s.active.w, s.active.h); 268 if (!s.active.crop.isEmpty()) { 269 win.intersect(s.active.crop, &win); 270 } 271 // subtract the transparent region and snap to the bounds 272 return reduce(win, s.activeTransparentRegion); 273} 274 275FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const { 276 // the content crop is the area of the content that gets scaled to the 277 // layer's size. 278 FloatRect crop(getContentCrop()); 279 280 // the active.crop is the area of the window that gets cropped, but not 281 // scaled in any ways. 282 const State& s(getDrawingState()); 283 284 // apply the projection's clipping to the window crop in 285 // layerstack space, and convert-back to layer space. 286 // if there are no window scaling involved, this operation will map to full 287 // pixels in the buffer. 288 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have 289 // a viewport clipping and a window transform. we should use floating point to fix this. 290 291 Rect activeCrop(s.active.w, s.active.h); 292 if (!s.active.crop.isEmpty()) { 293 activeCrop = s.active.crop; 294 } 295 296 activeCrop = s.transform.transform(activeCrop); 297 activeCrop.intersect(hw->getViewport(), &activeCrop); 298 activeCrop = s.transform.inverse().transform(activeCrop); 299 300 // paranoia: make sure the window-crop is constrained in the 301 // window's bounds 302 activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop); 303 304 // subtract the transparent region and snap to the bounds 305 activeCrop = reduce(activeCrop, s.activeTransparentRegion); 306 307 if (!activeCrop.isEmpty()) { 308 // Transform the window crop to match the buffer coordinate system, 309 // which means using the inverse of the current transform set on the 310 // SurfaceFlingerConsumer. 311 uint32_t invTransform = mCurrentTransform; 312 int winWidth = s.active.w; 313 int winHeight = s.active.h; 314 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 315 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 316 NATIVE_WINDOW_TRANSFORM_FLIP_H; 317 winWidth = s.active.h; 318 winHeight = s.active.w; 319 } 320 const Rect winCrop = activeCrop.transform( 321 invTransform, s.active.w, s.active.h); 322 323 // below, crop is intersected with winCrop expressed in crop's coordinate space 324 float xScale = crop.getWidth() / float(winWidth); 325 float yScale = crop.getHeight() / float(winHeight); 326 327 float insetL = winCrop.left * xScale; 328 float insetT = winCrop.top * yScale; 329 float insetR = (winWidth - winCrop.right ) * xScale; 330 float insetB = (winHeight - winCrop.bottom) * yScale; 331 332 crop.left += insetL; 333 crop.top += insetT; 334 crop.right -= insetR; 335 crop.bottom -= insetB; 336 } 337 return crop; 338} 339 340void Layer::setGeometry( 341 const sp<const DisplayDevice>& hw, 342 HWComposer::HWCLayerInterface& layer) 343{ 344 layer.setDefaultState(); 345 346 // enable this layer 347 layer.setSkip(false); 348 349 if (isSecure() && !hw->isSecure()) { 350 layer.setSkip(true); 351 } 352 353 // this gives us only the "orientation" component of the transform 354 const State& s(getDrawingState()); 355 if (!isOpaque(s) || s.alpha != 0xFF) { 356 layer.setBlending(mPremultipliedAlpha ? 357 HWC_BLENDING_PREMULT : 358 HWC_BLENDING_COVERAGE); 359 } 360 361 // apply the layer's transform, followed by the display's global transform 362 // here we're guaranteed that the layer's transform preserves rects 363 Rect frame(s.transform.transform(computeBounds())); 364 frame.intersect(hw->getViewport(), &frame); 365 const Transform& tr(hw->getTransform()); 366 layer.setFrame(tr.transform(frame)); 367 layer.setCrop(computeCrop(hw)); 368 layer.setPlaneAlpha(s.alpha); 369 370 /* 371 * Transformations are applied in this order: 372 * 1) buffer orientation/flip/mirror 373 * 2) state transformation (window manager) 374 * 3) layer orientation (screen orientation) 375 * (NOTE: the matrices are multiplied in reverse order) 376 */ 377 378 const Transform bufferOrientation(mCurrentTransform); 379 Transform transform(tr * s.transform * bufferOrientation); 380 381 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 382 /* 383 * the code below applies the display's inverse transform to the buffer 384 */ 385 uint32_t invTransform = hw->getOrientationTransform(); 386 // calculate the inverse transform 387 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { 388 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | 389 NATIVE_WINDOW_TRANSFORM_FLIP_H; 390 } 391 // and apply to the current transform 392 transform = transform * Transform(invTransform); 393 } 394 395 // this gives us only the "orientation" component of the transform 396 const uint32_t orientation = transform.getOrientation(); 397 if (orientation & Transform::ROT_INVALID) { 398 // we can only handle simple transformation 399 layer.setSkip(true); 400 } else { 401 layer.setTransform(orientation); 402 } 403} 404 405void Layer::setPerFrameData(const sp<const DisplayDevice>& hw, 406 HWComposer::HWCLayerInterface& layer) { 407 // we have to set the visible region on every frame because 408 // we currently free it during onLayerDisplayed(), which is called 409 // after HWComposer::commit() -- every frame. 410 // Apply this display's projection's viewport to the visible region 411 // before giving it to the HWC HAL. 412 const Transform& tr = hw->getTransform(); 413 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport())); 414 layer.setVisibleRegionScreen(visible); 415 416 // NOTE: buffer can be NULL if the client never drew into this 417 // layer yet, or if we ran out of memory 418 layer.setBuffer(mActiveBuffer); 419} 420 421void Layer::setAcquireFence(const sp<const DisplayDevice>& hw, 422 HWComposer::HWCLayerInterface& layer) { 423 int fenceFd = -1; 424 425 // TODO: there is a possible optimization here: we only need to set the 426 // acquire fence the first time a new buffer is acquired on EACH display. 427 428 if (layer.getCompositionType() == HWC_OVERLAY) { 429 sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence(); 430 if (fence->isValid()) { 431 fenceFd = fence->dup(); 432 if (fenceFd == -1) { 433 ALOGW("failed to dup layer fence, skipping sync: %d", errno); 434 } 435 } 436 } 437 layer.setAcquireFenceFd(fenceFd); 438} 439 440// --------------------------------------------------------------------------- 441// drawing... 442// --------------------------------------------------------------------------- 443 444void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const { 445 onDraw(hw, clip); 446} 447 448void Layer::draw(const sp<const DisplayDevice>& hw) { 449 onDraw( hw, Region(hw->bounds()) ); 450} 451 452void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const 453{ 454 ATRACE_CALL(); 455 456 if (CC_UNLIKELY(mActiveBuffer == 0)) { 457 // the texture has not been created yet, this Layer has 458 // in fact never been drawn into. This happens frequently with 459 // SurfaceView because the WindowManager can't know when the client 460 // has drawn the first time. 461 462 // If there is nothing under us, we paint the screen in black, otherwise 463 // we just skip this update. 464 465 // figure out if there is something below us 466 Region under; 467 const SurfaceFlinger::LayerVector& drawingLayers( 468 mFlinger->mDrawingState.layersSortedByZ); 469 const size_t count = drawingLayers.size(); 470 for (size_t i=0 ; i<count ; ++i) { 471 const sp<Layer>& layer(drawingLayers[i]); 472 if (layer.get() == static_cast<Layer const*>(this)) 473 break; 474 under.orSelf( hw->getTransform().transform(layer->visibleRegion) ); 475 } 476 // if not everything below us is covered, we plug the holes! 477 Region holes(clip.subtract(under)); 478 if (!holes.isEmpty()) { 479 clearWithOpenGL(hw, holes, 0, 0, 0, 1); 480 } 481 return; 482 } 483 484 // Bind the current buffer to the GL texture, and wait for it to be 485 // ready for us to draw into. 486 status_t err = mSurfaceFlingerConsumer->bindTextureImage(); 487 if (err != NO_ERROR) { 488 ALOGW("onDraw: bindTextureImage failed (err=%d)", err); 489 // Go ahead and draw the buffer anyway; no matter what we do the screen 490 // is probably going to have something visibly wrong. 491 } 492 493 bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure()); 494 495 RenderEngine& engine(mFlinger->getRenderEngine()); 496 497 if (!blackOutLayer) { 498 // TODO: we could be more subtle with isFixedSize() 499 const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize(); 500 501 // Query the texture matrix given our current filtering mode. 502 float textureMatrix[16]; 503 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering); 504 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix); 505 506 if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { 507 508 /* 509 * the code below applies the display's inverse transform to the texture transform 510 */ 511 512 // create a 4x4 transform matrix from the display transform flags 513 const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1); 514 const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1); 515 const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1); 516 517 mat4 tr; 518 uint32_t transform = hw->getOrientationTransform(); 519 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) 520 tr = tr * rot90; 521 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) 522 tr = tr * flipH; 523 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) 524 tr = tr * flipV; 525 526 // calculate the inverse 527 tr = inverse(tr); 528 529 // and finally apply it to the original texture matrix 530 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); 531 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); 532 } 533 534 // Set things up for texturing. 535 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); 536 mTexture.setFiltering(useFiltering); 537 mTexture.setMatrix(textureMatrix); 538 539 engine.setupLayerTexturing(mTexture); 540 } else { 541 engine.setupLayerBlackedOut(); 542 } 543 drawWithOpenGL(hw, clip); 544 engine.disableTexturing(); 545} 546 547 548void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip, 549 float red, float green, float blue, float alpha) const 550{ 551 RenderEngine& engine(mFlinger->getRenderEngine()); 552 computeGeometry(hw, mMesh); 553 engine.setupFillWithColor(red, green, blue, alpha); 554 engine.drawMesh(mMesh); 555} 556 557void Layer::clearWithOpenGL( 558 const sp<const DisplayDevice>& hw, const Region& clip) const { 559 clearWithOpenGL(hw, clip, 0,0,0,0); 560} 561 562void Layer::drawWithOpenGL( 563 const sp<const DisplayDevice>& hw, const Region& clip) const { 564 const uint32_t fbHeight = hw->getHeight(); 565 const State& s(getDrawingState()); 566 567 computeGeometry(hw, mMesh); 568 569 /* 570 * NOTE: the way we compute the texture coordinates here produces 571 * different results than when we take the HWC path -- in the later case 572 * the "source crop" is rounded to texel boundaries. 573 * This can produce significantly different results when the texture 574 * is scaled by a large amount. 575 * 576 * The GL code below is more logical (imho), and the difference with 577 * HWC is due to a limitation of the HWC API to integers -- a question 578 * is suspend is whether we should ignore this problem or revert to 579 * GL composition when a buffer scaling is applied (maybe with some 580 * minimal value)? Or, we could make GL behave like HWC -- but this feel 581 * like more of a hack. 582 */ 583 const Rect win(computeBounds()); 584 585 float left = float(win.left) / float(s.active.w); 586 float top = float(win.top) / float(s.active.h); 587 float right = float(win.right) / float(s.active.w); 588 float bottom = float(win.bottom) / float(s.active.h); 589 590 // TODO: we probably want to generate the texture coords with the mesh 591 // here we assume that we only have 4 vertices 592 Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>()); 593 texCoords[0] = vec2(left, 1.0f - top); 594 texCoords[1] = vec2(left, 1.0f - bottom); 595 texCoords[2] = vec2(right, 1.0f - bottom); 596 texCoords[3] = vec2(right, 1.0f - top); 597 598 RenderEngine& engine(mFlinger->getRenderEngine()); 599 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha); 600 engine.drawMesh(mMesh); 601 engine.disableBlending(); 602} 603 604void Layer::setFiltering(bool filtering) { 605 mFiltering = filtering; 606} 607 608bool Layer::getFiltering() const { 609 return mFiltering; 610} 611 612// As documented in libhardware header, formats in the range 613// 0x100 - 0x1FF are specific to the HAL implementation, and 614// are known to have no alpha channel 615// TODO: move definition for device-specific range into 616// hardware.h, instead of using hard-coded values here. 617#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) 618 619bool Layer::getOpacityForFormat(uint32_t format) { 620 if (HARDWARE_IS_DEVICE_FORMAT(format)) { 621 return true; 622 } 623 switch (format) { 624 case HAL_PIXEL_FORMAT_RGBA_8888: 625 case HAL_PIXEL_FORMAT_BGRA_8888: 626 case HAL_PIXEL_FORMAT_sRGB_A_8888: 627 return false; 628 } 629 // in all other case, we have no blending (also for unknown formats) 630 return true; 631} 632 633// ---------------------------------------------------------------------------- 634// local state 635// ---------------------------------------------------------------------------- 636 637void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh) const 638{ 639 const Layer::State& s(getDrawingState()); 640 const Transform tr(hw->getTransform() * s.transform); 641 const uint32_t hw_h = hw->getHeight(); 642 Rect win(s.active.w, s.active.h); 643 if (!s.active.crop.isEmpty()) { 644 win.intersect(s.active.crop, &win); 645 } 646 // subtract the transparent region and snap to the bounds 647 win = reduce(win, s.activeTransparentRegion); 648 649 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); 650 position[0] = tr.transform(win.left, win.top); 651 position[1] = tr.transform(win.left, win.bottom); 652 position[2] = tr.transform(win.right, win.bottom); 653 position[3] = tr.transform(win.right, win.top); 654 for (size_t i=0 ; i<4 ; i++) { 655 position[i].y = hw_h - position[i].y; 656 } 657} 658 659bool Layer::isOpaque(const Layer::State& s) const 660{ 661 // if we don't have a buffer yet, we're translucent regardless of the 662 // layer's opaque flag. 663 if (mActiveBuffer == 0) { 664 return false; 665 } 666 667 // if the layer has the opaque flag, then we're always opaque, 668 // otherwise we use the current buffer's format. 669 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; 670} 671 672bool Layer::isProtected() const 673{ 674 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); 675 return (activeBuffer != 0) && 676 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); 677} 678 679bool Layer::isFixedSize() const { 680 return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; 681} 682 683bool Layer::isCropped() const { 684 return !mCurrentCrop.isEmpty(); 685} 686 687bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const { 688 return mNeedsFiltering || hw->needsFiltering(); 689} 690 691void Layer::setVisibleRegion(const Region& visibleRegion) { 692 // always called from main thread 693 this->visibleRegion = visibleRegion; 694} 695 696void Layer::setCoveredRegion(const Region& coveredRegion) { 697 // always called from main thread 698 this->coveredRegion = coveredRegion; 699} 700 701void Layer::setVisibleNonTransparentRegion(const Region& 702 setVisibleNonTransparentRegion) { 703 // always called from main thread 704 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion; 705} 706 707// ---------------------------------------------------------------------------- 708// transaction 709// ---------------------------------------------------------------------------- 710 711uint32_t Layer::doTransaction(uint32_t flags) { 712 ATRACE_CALL(); 713 714 const Layer::State& s(getDrawingState()); 715 const Layer::State& c(getCurrentState()); 716 717 const bool sizeChanged = (c.requested.w != s.requested.w) || 718 (c.requested.h != s.requested.h); 719 720 if (sizeChanged) { 721 // the size changed, we need to ask our client to request a new buffer 722 ALOGD_IF(DEBUG_RESIZE, 723 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n" 724 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 725 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n" 726 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 727 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", 728 this, getName().string(), mCurrentTransform, mCurrentScalingMode, 729 c.active.w, c.active.h, 730 c.active.crop.left, 731 c.active.crop.top, 732 c.active.crop.right, 733 c.active.crop.bottom, 734 c.active.crop.getWidth(), 735 c.active.crop.getHeight(), 736 c.requested.w, c.requested.h, 737 c.requested.crop.left, 738 c.requested.crop.top, 739 c.requested.crop.right, 740 c.requested.crop.bottom, 741 c.requested.crop.getWidth(), 742 c.requested.crop.getHeight(), 743 s.active.w, s.active.h, 744 s.active.crop.left, 745 s.active.crop.top, 746 s.active.crop.right, 747 s.active.crop.bottom, 748 s.active.crop.getWidth(), 749 s.active.crop.getHeight(), 750 s.requested.w, s.requested.h, 751 s.requested.crop.left, 752 s.requested.crop.top, 753 s.requested.crop.right, 754 s.requested.crop.bottom, 755 s.requested.crop.getWidth(), 756 s.requested.crop.getHeight()); 757 758 // record the new size, form this point on, when the client request 759 // a buffer, it'll get the new size. 760 mSurfaceFlingerConsumer->setDefaultBufferSize( 761 c.requested.w, c.requested.h); 762 } 763 764 if (!isFixedSize()) { 765 766 const bool resizePending = (c.requested.w != c.active.w) || 767 (c.requested.h != c.active.h); 768 769 if (resizePending) { 770 // don't let Layer::doTransaction update the drawing state 771 // if we have a pending resize, unless we are in fixed-size mode. 772 // the drawing state will be updated only once we receive a buffer 773 // with the correct size. 774 // 775 // in particular, we want to make sure the clip (which is part 776 // of the geometry state) is latched together with the size but is 777 // latched immediately when no resizing is involved. 778 779 flags |= eDontUpdateGeometryState; 780 } 781 } 782 783 // always set active to requested, unless we're asked not to 784 // this is used by Layer, which special cases resizes. 785 if (flags & eDontUpdateGeometryState) { 786 } else { 787 Layer::State& editCurrentState(getCurrentState()); 788 editCurrentState.active = c.requested; 789 } 790 791 if (s.active != c.active) { 792 // invalidate and recompute the visible regions if needed 793 flags |= Layer::eVisibleRegion; 794 } 795 796 if (c.sequence != s.sequence) { 797 // invalidate and recompute the visible regions if needed 798 flags |= eVisibleRegion; 799 this->contentDirty = true; 800 801 // we may use linear filtering, if the matrix scales us 802 const uint8_t type = c.transform.getType(); 803 mNeedsFiltering = (!c.transform.preserveRects() || 804 (type >= Transform::SCALE)); 805 } 806 807 // Commit the transaction 808 commitTransaction(); 809 return flags; 810} 811 812void Layer::commitTransaction() { 813 mDrawingState = mCurrentState; 814} 815 816uint32_t Layer::getTransactionFlags(uint32_t flags) { 817 return android_atomic_and(~flags, &mTransactionFlags) & flags; 818} 819 820uint32_t Layer::setTransactionFlags(uint32_t flags) { 821 return android_atomic_or(flags, &mTransactionFlags); 822} 823 824bool Layer::setPosition(float x, float y) { 825 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) 826 return false; 827 mCurrentState.sequence++; 828 mCurrentState.transform.set(x, y); 829 setTransactionFlags(eTransactionNeeded); 830 return true; 831} 832bool Layer::setLayer(uint32_t z) { 833 if (mCurrentState.z == z) 834 return false; 835 mCurrentState.sequence++; 836 mCurrentState.z = z; 837 setTransactionFlags(eTransactionNeeded); 838 return true; 839} 840bool Layer::setSize(uint32_t w, uint32_t h) { 841 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) 842 return false; 843 mCurrentState.requested.w = w; 844 mCurrentState.requested.h = h; 845 setTransactionFlags(eTransactionNeeded); 846 return true; 847} 848bool Layer::setAlpha(uint8_t alpha) { 849 if (mCurrentState.alpha == alpha) 850 return false; 851 mCurrentState.sequence++; 852 mCurrentState.alpha = alpha; 853 setTransactionFlags(eTransactionNeeded); 854 return true; 855} 856bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) { 857 mCurrentState.sequence++; 858 mCurrentState.transform.set( 859 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy); 860 setTransactionFlags(eTransactionNeeded); 861 return true; 862} 863bool Layer::setTransparentRegionHint(const Region& transparent) { 864 mCurrentState.requestedTransparentRegion = transparent; 865 setTransactionFlags(eTransactionNeeded); 866 return true; 867} 868bool Layer::setFlags(uint8_t flags, uint8_t mask) { 869 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); 870 if (mCurrentState.flags == newFlags) 871 return false; 872 mCurrentState.sequence++; 873 mCurrentState.flags = newFlags; 874 setTransactionFlags(eTransactionNeeded); 875 return true; 876} 877bool Layer::setCrop(const Rect& crop) { 878 if (mCurrentState.requested.crop == crop) 879 return false; 880 mCurrentState.sequence++; 881 mCurrentState.requested.crop = crop; 882 setTransactionFlags(eTransactionNeeded); 883 return true; 884} 885 886bool Layer::setLayerStack(uint32_t layerStack) { 887 if (mCurrentState.layerStack == layerStack) 888 return false; 889 mCurrentState.sequence++; 890 mCurrentState.layerStack = layerStack; 891 setTransactionFlags(eTransactionNeeded); 892 return true; 893} 894 895// ---------------------------------------------------------------------------- 896// pageflip handling... 897// ---------------------------------------------------------------------------- 898 899bool Layer::onPreComposition() { 900 mRefreshPending = false; 901 return mQueuedFrames > 0; 902} 903 904void Layer::onPostComposition() { 905 if (mFrameLatencyNeeded) { 906 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp(); 907 mFrameTracker.setDesiredPresentTime(desiredPresentTime); 908 909 sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence(); 910 if (frameReadyFence->isValid()) { 911 mFrameTracker.setFrameReadyFence(frameReadyFence); 912 } else { 913 // There was no fence for this frame, so assume that it was ready 914 // to be presented at the desired present time. 915 mFrameTracker.setFrameReadyTime(desiredPresentTime); 916 } 917 918 const HWComposer& hwc = mFlinger->getHwComposer(); 919 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY); 920 if (presentFence->isValid()) { 921 mFrameTracker.setActualPresentFence(presentFence); 922 } else { 923 // The HWC doesn't support present fences, so use the refresh 924 // timestamp instead. 925 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY); 926 mFrameTracker.setActualPresentTime(presentTime); 927 } 928 929 mFrameTracker.advanceFrame(); 930 mFrameLatencyNeeded = false; 931 } 932} 933 934bool Layer::isVisible() const { 935 const Layer::State& s(mDrawingState); 936 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha 937 && (mActiveBuffer != NULL); 938} 939 940Region Layer::latchBuffer(bool& recomputeVisibleRegions) 941{ 942 ATRACE_CALL(); 943 944 Region outDirtyRegion; 945 if (mQueuedFrames > 0) { 946 947 // if we've already called updateTexImage() without going through 948 // a composition step, we have to skip this layer at this point 949 // because we cannot call updateTeximage() without a corresponding 950 // compositionComplete() call. 951 // we'll trigger an update in onPreComposition(). 952 if (mRefreshPending) { 953 return outDirtyRegion; 954 } 955 956 // Capture the old state of the layer for comparisons later 957 const State& s(getDrawingState()); 958 const bool oldOpacity = isOpaque(s); 959 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer; 960 961 struct Reject : public SurfaceFlingerConsumer::BufferRejecter { 962 Layer::State& front; 963 Layer::State& current; 964 bool& recomputeVisibleRegions; 965 Reject(Layer::State& front, Layer::State& current, 966 bool& recomputeVisibleRegions) 967 : front(front), current(current), 968 recomputeVisibleRegions(recomputeVisibleRegions) { 969 } 970 971 virtual bool reject(const sp<GraphicBuffer>& buf, 972 const IGraphicBufferConsumer::BufferItem& item) { 973 if (buf == NULL) { 974 return false; 975 } 976 977 uint32_t bufWidth = buf->getWidth(); 978 uint32_t bufHeight = buf->getHeight(); 979 980 // check that we received a buffer of the right size 981 // (Take the buffer's orientation into account) 982 if (item.mTransform & Transform::ROT_90) { 983 swap(bufWidth, bufHeight); 984 } 985 986 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; 987 if (front.active != front.requested) { 988 989 if (isFixedSize || 990 (bufWidth == front.requested.w && 991 bufHeight == front.requested.h)) 992 { 993 // Here we pretend the transaction happened by updating the 994 // current and drawing states. Drawing state is only accessed 995 // in this thread, no need to have it locked 996 front.active = front.requested; 997 998 // We also need to update the current state so that 999 // we don't end-up overwriting the drawing state with 1000 // this stale current state during the next transaction 1001 // 1002 // NOTE: We don't need to hold the transaction lock here 1003 // because State::active is only accessed from this thread. 1004 current.active = front.active; 1005 1006 // recompute visible region 1007 recomputeVisibleRegions = true; 1008 } 1009 1010 ALOGD_IF(DEBUG_RESIZE, 1011 "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n" 1012 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" 1013 " requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n", 1014 bufWidth, bufHeight, item.mTransform, item.mScalingMode, 1015 front.active.w, front.active.h, 1016 front.active.crop.left, 1017 front.active.crop.top, 1018 front.active.crop.right, 1019 front.active.crop.bottom, 1020 front.active.crop.getWidth(), 1021 front.active.crop.getHeight(), 1022 front.requested.w, front.requested.h, 1023 front.requested.crop.left, 1024 front.requested.crop.top, 1025 front.requested.crop.right, 1026 front.requested.crop.bottom, 1027 front.requested.crop.getWidth(), 1028 front.requested.crop.getHeight()); 1029 } 1030 1031 if (!isFixedSize) { 1032 if (front.active.w != bufWidth || 1033 front.active.h != bufHeight) { 1034 // reject this buffer 1035 //ALOGD("rejecting buffer: bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}", 1036 // bufWidth, bufHeight, front.active.w, front.active.h); 1037 return true; 1038 } 1039 } 1040 1041 // if the transparent region has changed (this test is 1042 // conservative, but that's fine, worst case we're doing 1043 // a bit of extra work), we latch the new one and we 1044 // trigger a visible-region recompute. 1045 if (!front.activeTransparentRegion.isTriviallyEqual( 1046 front.requestedTransparentRegion)) { 1047 front.activeTransparentRegion = front.requestedTransparentRegion; 1048 1049 // We also need to update the current state so that 1050 // we don't end-up overwriting the drawing state with 1051 // this stale current state during the next transaction 1052 // 1053 // NOTE: We don't need to hold the transaction lock here 1054 // because State::active is only accessed from this thread. 1055 current.activeTransparentRegion = front.activeTransparentRegion; 1056 1057 // recompute visible region 1058 recomputeVisibleRegions = true; 1059 } 1060 1061 return false; 1062 } 1063 }; 1064 1065 1066 Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions); 1067 1068 status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r); 1069 if (updateResult == BufferQueue::PRESENT_LATER) { 1070 // Producer doesn't want buffer to be displayed yet. Signal a 1071 // layer update so we check again at the next opportunity. 1072 mFlinger->signalLayerUpdate(); 1073 return outDirtyRegion; 1074 } 1075 1076 // Decrement the queued-frames count. Signal another event if we 1077 // have more frames pending. 1078 if (android_atomic_dec(&mQueuedFrames) > 1) { 1079 mFlinger->signalLayerUpdate(); 1080 } 1081 1082 if (updateResult != NO_ERROR) { 1083 // something happened! 1084 recomputeVisibleRegions = true; 1085 return outDirtyRegion; 1086 } 1087 1088 // update the active buffer 1089 mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(); 1090 if (mActiveBuffer == NULL) { 1091 // this can only happen if the very first buffer was rejected. 1092 return outDirtyRegion; 1093 } 1094 1095 mRefreshPending = true; 1096 mFrameLatencyNeeded = true; 1097 if (oldActiveBuffer == NULL) { 1098 // the first time we receive a buffer, we need to trigger a 1099 // geometry invalidation. 1100 recomputeVisibleRegions = true; 1101 } 1102 1103 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop()); 1104 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform()); 1105 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode()); 1106 if ((crop != mCurrentCrop) || 1107 (transform != mCurrentTransform) || 1108 (scalingMode != mCurrentScalingMode)) 1109 { 1110 mCurrentCrop = crop; 1111 mCurrentTransform = transform; 1112 mCurrentScalingMode = scalingMode; 1113 recomputeVisibleRegions = true; 1114 } 1115 1116 if (oldActiveBuffer != NULL) { 1117 uint32_t bufWidth = mActiveBuffer->getWidth(); 1118 uint32_t bufHeight = mActiveBuffer->getHeight(); 1119 if (bufWidth != uint32_t(oldActiveBuffer->width) || 1120 bufHeight != uint32_t(oldActiveBuffer->height)) { 1121 recomputeVisibleRegions = true; 1122 } 1123 } 1124 1125 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); 1126 if (oldOpacity != isOpaque(s)) { 1127 recomputeVisibleRegions = true; 1128 } 1129 1130 // FIXME: postedRegion should be dirty & bounds 1131 Region dirtyRegion(Rect(s.active.w, s.active.h)); 1132 1133 // transform the dirty region to window-manager space 1134 outDirtyRegion = (s.transform.transform(dirtyRegion)); 1135 } 1136 return outDirtyRegion; 1137} 1138 1139uint32_t Layer::getEffectiveUsage(uint32_t usage) const 1140{ 1141 // TODO: should we do something special if mSecure is set? 1142 if (mProtectedByApp) { 1143 // need a hardware-protected path to external video sink 1144 usage |= GraphicBuffer::USAGE_PROTECTED; 1145 } 1146 usage |= GraphicBuffer::USAGE_HW_COMPOSER; 1147 return usage; 1148} 1149 1150void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const { 1151 uint32_t orientation = 0; 1152 if (!mFlinger->mDebugDisableTransformHint) { 1153 // The transform hint is used to improve performance, but we can 1154 // only have a single transform hint, it cannot 1155 // apply to all displays. 1156 const Transform& planeTransform(hw->getTransform()); 1157 orientation = planeTransform.getOrientation(); 1158 if (orientation & Transform::ROT_INVALID) { 1159 orientation = 0; 1160 } 1161 } 1162 mSurfaceFlingerConsumer->setTransformHint(orientation); 1163} 1164 1165// ---------------------------------------------------------------------------- 1166// debugging 1167// ---------------------------------------------------------------------------- 1168 1169void Layer::dump(String8& result, Colorizer& colorizer) const 1170{ 1171 const Layer::State& s(getDrawingState()); 1172 1173 colorizer.colorize(result, Colorizer::GREEN); 1174 result.appendFormat( 1175 "+ %s %p (%s)\n", 1176 getTypeId(), this, getName().string()); 1177 colorizer.reset(result); 1178 1179 s.activeTransparentRegion.dump(result, "transparentRegion"); 1180 visibleRegion.dump(result, "visibleRegion"); 1181 sp<Client> client(mClientRef.promote()); 1182 1183 result.appendFormat( " " 1184 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), " 1185 "isOpaque=%1d, invalidate=%1d, " 1186 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" 1187 " client=%p\n", 1188 s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h, 1189 s.active.crop.left, s.active.crop.top, 1190 s.active.crop.right, s.active.crop.bottom, 1191 isOpaque(s), contentDirty, 1192 s.alpha, s.flags, 1193 s.transform[0][0], s.transform[0][1], 1194 s.transform[1][0], s.transform[1][1], 1195 client.get()); 1196 1197 sp<const GraphicBuffer> buf0(mActiveBuffer); 1198 uint32_t w0=0, h0=0, s0=0, f0=0; 1199 if (buf0 != 0) { 1200 w0 = buf0->getWidth(); 1201 h0 = buf0->getHeight(); 1202 s0 = buf0->getStride(); 1203 f0 = buf0->format; 1204 } 1205 result.appendFormat( 1206 " " 1207 "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X]," 1208 " queued-frames=%d, mRefreshPending=%d\n", 1209 mFormat, w0, h0, s0,f0, 1210 mQueuedFrames, mRefreshPending); 1211 1212 if (mSurfaceFlingerConsumer != 0) { 1213 mSurfaceFlingerConsumer->dump(result, " "); 1214 } 1215} 1216 1217void Layer::dumpStats(String8& result) const { 1218 mFrameTracker.dump(result); 1219} 1220 1221void Layer::clearStats() { 1222 mFrameTracker.clear(); 1223} 1224 1225void Layer::logFrameStats() { 1226 mFrameTracker.logAndResetStats(mName); 1227} 1228 1229// --------------------------------------------------------------------------- 1230 1231Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger, 1232 const sp<Layer>& layer) 1233 : mFlinger(flinger), mLayer(layer) { 1234} 1235 1236Layer::LayerCleaner::~LayerCleaner() { 1237 // destroy client resources 1238 mFlinger->onLayerDestroyed(mLayer); 1239} 1240 1241// --------------------------------------------------------------------------- 1242}; // namespace android 1243 1244#if defined(__gl_h_) 1245#error "don't include gl/gl.h in this file" 1246#endif 1247 1248#if defined(__gl2_h_) 1249#error "don't include gl2/gl2.h in this file" 1250#endif 1251