/*
*收到VSYNC后 REFRESH显示
*/
413void SurfaceFlinger::onMessageReceived(int32_t what)
{
419 // if we're in a global transaction, don't do anything.
420 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
421 uint32_t transactionFlags = peekTransactionFlags(mask);
422 if (CC_UNLIKELY(transactionFlags)) {
423 handleTransaction(transactionFlags);
424 }
425
426 // post surfaces (if needed)
427 handlePageFlip();
428
435 handleRefresh();
436
437 const DisplayHardware& hw(graphicPlane(0).displayHardware());
438
443 if (CC_UNLIKELY(mHwWorkListDirty)) {
444 // build the h/w work list
445 handleWorkList();
446 }
447
448 if (CC_LIKELY(hw.canDraw())) {
449 // repaint the framebuffer (if needed)
450 handleRepaint();
451 // inform the h/w that we're done compositing
452 hw.compositionComplete();
453 postFramebuffer();
454 } else {
455 // pretend we did the post
456 hw.compositionComplete();
457 }
}
511void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
512{
513 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
514 const size_t count = currentLayers.size();
515
516 /*
517 * Traversal of the children
518 * (perform the transaction for each of them if needed)
519 */
520
/*
* 针对每个Layer,提交其所做的状态变化
*/
521 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
522 if (layersNeedTransaction) {
523 for (size_t i=0 ; i<count ; i++) {
524 const sp<LayerBase>& layer = currentLayers[i];
525 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
526 if (!trFlags) continue;
527
528 const uint32_t flags = layer->doTransaction(0);
529 if (flags & Layer::eVisibleRegion)
530 mVisibleRegionsDirty = true;
531 }
532 }
533
534 /*
535 * Perform our own transaction if needed
536 */
537 /*
* 处理SurfaceFlinger全局状态变化
*/
538 if (transactionFlags & eTransactionNeeded) {
/*
* 如果屏幕发生旋转,则设置mDirtyRegion为整个屏幕范围,更新mServerCblk
* 客户端可以访问到,通知HWC屏幕位向改变重新设置其参数。
*/
539 if (mCurrentState.orientation != mDrawingState.orientation) {
540 // the orientation has changed, recompute all visible regions
541 // and invalidate everything.
542
543 const int dpy = 0;
544 const int orientation = mCurrentState.orientation;
545 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
546 GraphicPlane& plane(graphicPlane(dpy));
547 plane.setOrientation(orientation);
548 const Transform& planeTransform(plane.transform());
549
550 // update the shared control block
551 const DisplayHardware& hw(plane.displayHardware());
552 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
553 dcblk->orientation = orientation;
554 dcblk->w = plane.getWidth();
555 dcblk->h = plane.getHeight();
556
557 mVisibleRegionsDirty = true;
558 mDirtyRegion.set(hw.bounds());
559
560 //set the new orientation to HWC
561 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
562 hwc.eventControl(DisplayHardware::EVENT_ORIENTATION,
563 planeTransform.getOrientation());
564
565 }
566
/*
* 如果有Layer增加,设置赃区域标志,此时mDirtyRegion还为空,
* 每次Repaint后mDirtyRegion就清空了。
* 此处的判断条件使用Layer个数比较,需要与下面mLayersRemoved结合看。
* 如果Layer有减少,即使增加的个数小于减少的个数,
* 那么mVisibleRegionsDirty一定会被设置。
* 如果没有减少,增加Layer后数目一定会增多。可读性不好。
*/
567 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
568 // layers have been added
569 mVisibleRegionsDirty = true;
570 }
571
/*
* 有减少的Layer,那么其下Layer可能会暴露出来,需要Invalidate该Layer
* 暴露出来的区域,所以需要记录这块区域。
* 所有移除layer暴露出来的区域累积,记录在mDirtyRegionRemovedLayer中。
* Invalidate的效果是在lockPageFlip后,将mDirtyRegionRemovedLayer加到
* mDirtyRegion中。
* 用户绘图后Post的赃区域在unlockPageFlip时做。
*/
572 // some layers might have been removed, so
573 // we need to update the regions they're exposing.
574 if (mLayersRemoved) {
575 mLayersRemoved = false;
576 mVisibleRegionsDirty = true;
577 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
578 const size_t count = previousLayers.size();
579 for (size_t i=0 ; i<count ; i++) {
580 const sp<LayerBase>& layer(previousLayers[i]);
581 if (currentLayers.indexOf( layer ) < 0) {
582 // this layer is not visible anymore
583 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
584 }
585 }
586 }
587 }
588
/*
* 复制CurrentState到DrawingState中,即提交,下面代码处理Repaint时使用DrawingState
*/
589 commitTransaction();
590}
735void SurfaceFlinger::handlePageFlip()
736{
737 ATRACE_CALL();
738 const DisplayHardware& hw = graphicPlane(0).displayHardware();
739 const Region screenRegion(hw.bounds());
740
/*
* 更新每个Layer的脏区域,获取每Layer这次重绘所需要的GraphicBuffer,
* 为每Layer生成纹理供GPU render使用。
*/
741 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
742 const bool visibleRegions = lockPageFlip(currentLayers);
743
744 if (visibleRegions || mVisibleRegionsDirty) {
745 Region opaqueRegion;
/*
* 计算更新mDirtyRegion,得到所有Opaqued Layers的总的Region。
*/
746 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
747
748 /*
749 * rebuild the visible layer list; 重建mVisibleLayersSortedByZ
750 */
751 const size_t count = currentLayers.size();
752 mVisibleLayersSortedByZ.clear();
753 mVisibleLayersSortedByZ.setCapacity(count);
754 for (size_t i=0 ; i<count ; i++) {
755 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
756 mVisibleLayersSortedByZ.add(currentLayers[i]);
757 }
758
/*
* opaqueRegion区域外的区域绘制“虫洞”,记录该区域
*/
759 mWormholeRegion = screenRegion.subtract(opaqueRegion);
/*
* 本轮处理中mVisibleRegionsDirty标志使用完毕,重置。
*/
760 mVisibleRegionsDirty = false;
761 invalidateHwcGeometry();
762 }
763
/*
* 主要是将每个Layer用户Post的区域并到赃区域上
*/
764 unlockPageFlip(currentLayers);
765
766 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
767 mDirtyRegion.andSelf(screenRegion);
768}
775bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
776{
777 bool recomputeVisibleRegions = false;
778 size_t count = currentLayers.size();
779 sp<LayerBase> const* layers = currentLayers.array();
780 for (size_t i=0 ; i<count ; i++) {
781 const sp<LayerBase>& layer(layers[i]);
782 layer->lockPageFlip(recomputeVisibleRegions);
783 }
784 return recomputeVisibleRegions;
785}
527void Layer::lockPageFlip(bool& recomputeVisibleRegions)
528{
529 ATRACE_CALL();
530
/*
* 本Layer有新Queued Buffer才需要更新纹理。
*/
531 if (mQueuedFrames > 0) {
532
533 // if we've already called updateTexImage() without going through
534 // a composition step, we have to skip this layer at this point
535 // because we cannot call updateTeximage() without a corresponding
536 // compositionComplete() call.
537 // we'll trigger an update in onPreComposition().
/*
* 如果上次的重绘还没有显示,本轮又要显示了,直接返回。
*/
538 if (mRefreshPending) {
539 mPostedDirtyRegion.clear();
540 return;
541 }
542
543 // Capture the old state of the layer for comparisons later
544 const bool oldOpacity = isOpaque();
545 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
546
/*
* 因为有mRefreshPending时导致直接return,所有需要“引发”下个Frame的显示;
* signalLayerUpdate()即是requestNextVsync(),因为setRefreshRate(0)时,
* 不接收VSYNC,所以需要显式要求下一个VSYNC发过来,“引发”下帧显示
*/
547 // signal another event if we have more frames pending
548 if (android_atomic_dec(&mQueuedFrames) > 1) {
549 mFlinger->signalLayerUpdate();
550 }
551
/*
* 内部类用于检验Queued过来的Buffer是否符合该Layer的显示要求,
* 不符合则reject,不予显示。
* 如Camera或Video图像buffer的大小,格式等要符合。
*/
552 struct Reject : public SurfaceTexture::BufferRejecter {
553 Layer::State& front;
554 Layer::State& current;
555 bool& recomputeVisibleRegions;
556 Reject(Layer::State& front, Layer::State& current,
557 bool& recomputeVisibleRegions)
558 : front(front), current(current),
559 recomputeVisibleRegions(recomputeVisibleRegions) {
560 }
561
562 virtual bool reject(const sp<GraphicBuffer>& buf,
563 const BufferQueue::BufferItem& item) {
564 if (buf == NULL) {
565 return false;
566 }
567
568 uint32_t bufWidth = buf->getWidth();
569 uint32_t bufHeight = buf->getHeight();
570
571 // check that we received a buffer of the right size
572 // (Take the buffer's orientation into account)
573 if (item.mTransform & Transform::ROT_90) {
574 swap(bufWidth, bufHeight);
575 }
576
577
578 bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
579 if (front.active != front.requested) {
580
581 if (isFixedSize ||
582 (bufWidth == front.requested.w &&
583 bufHeight == front.requested.h))
584 {
585 // Here we pretend the transaction happened by updating the
586 // current and drawing states. Drawing state is only accessed
587 // in this thread, no need to have it locked
588 front.active = front.requested;
589
590 // We also need to update the current state so that
591 // we don't end-up overwriting the drawing state with
592 // this stale current state during the next transaction
593 //
594 // NOTE: We don't need to hold the transaction lock here
595 // because State::active is only accessed from this thread.
596 current.active = front.active;
597
598 // recompute visible region
599 recomputeVisibleRegions = true;
600 }
601
622
623 if (!isFixedSize) {
624 if (front.active.w != bufWidth ||
625 front.active.h != bufHeight) {
626 // reject this buffer
627 return true;
628 }
629 }
630 return false;
631 }
632 };
633
634
635 Reject r(mDrawingState, currentState(), recomputeVisibleRegions);
636
/*
* 使用该Layer的mActiveBuffer生成SurfaceTexture,用于OpenGL/3D GPU render。
* 图像格式不符合时,Reject::reject()被回调。
*/
637 if (mSurfaceTexture->updateTexImage(&r) < NO_ERROR) {
638 // something happened!
639 recomputeVisibleRegions = true;
640 return;
641 }
/*************
* updateTexImage会释放上轮该Layer使用的GraphicBuffer;
*也即本轮使用的GraphicBuffer持续到下次需要重绘时释放。
* 记得其申请是在lockPageFlip中记录在mActiveBuffer。
*/
642
/*
* 记录或更新当前使用的即mActiveBuffer字段
* 注意该buffer直到该Layer下轮重绘Repaint时才Release,
* 期间SurfaceTexture对该Buffer是不可用的。
*/
643 // update the active buffer
644 mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
645 if (mActiveBuffer == NULL) {
646 // this can only happen if the very first buffer was rejected.
647 return;
648 }
649
/*
* 设置mRefreshPending标志了,如果本轮还没有Paint而下次又来了,直接返回。
*/
650 mRefreshPending = true;
651 mFrameLatencyNeeded = true;
652 if (oldActiveBuffer == NULL) {
653 // the first time we receive a buffer, we need to trigger a
654 // geometry invalidation.
655 mFlinger->invalidateHwcGeometry();
656 }
657
/*
* 如果Crop & Transform & Scale改变,重设HWC参数
*/
658 Rect crop(mSurfaceTexture->getCurrentCrop());
659 const uint32_t transform(mSurfaceTexture->getCurrentTransform());
660 const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
661 if ((crop != mCurrentCrop) ||
662 (transform != mCurrentTransform) ||
663 (scalingMode != mCurrentScalingMode))
664 {
665 mCurrentCrop = crop;
666 mCurrentTransform = transform;
667 mCurrentScalingMode = scalingMode;
668 mFlinger->invalidateHwcGeometry();
669 }
670
/*
* 比较GraphicBuffer的维度是否有改变,用于更新HWC的维度参数,
* 从而使HWC知道该准备多大的buffer空间,和图像参数用于合成。
*/
671 if (oldActiveBuffer != NULL) {
672 uint32_t bufWidth = mActiveBuffer->getWidth();
673 uint32_t bufHeight = mActiveBuffer->getHeight();
674 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
675 bufHeight != uint32_t(oldActiveBuffer->height)) {
676 mFlinger->invalidateHwcGeometry();
677 }
678 }
679
680 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
681 if (oldOpacity != isOpaque()) {
682 recomputeVisibleRegions = true;
683 }
684
/*
* FIXME? 每个layer的dirty 是在后面调用的computeVisibleRegions()中计算出来的,
* 可以在彼时设置给Layer,记录脏区域是个很好的优化。
* 但是Region mPostedDirtyRegion是class Layer而不是class LayerBase的成员,
* 慢慢FIX! 此dirty非computeVisibleRegions中的dirty
*/
685 // FIXME: mPostedDirtyRegion = dirty & bounds
686 const Layer::State& front(drawingState());
687 mPostedDirtyRegion.set(front.active.w, front.active.h);
688
689 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
690 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
691 }
692}
Layer Clip精髓所在----
592void SurfaceFlinger::computeVisibleRegions(
593 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
594{
595 ATRACE_CALL();
596
597 const GraphicPlane& plane(graphicPlane(0));
598 const Transform& planeTransform(plane.transform());
599 const DisplayHardware& hw(plane.displayHardware());
600 const Region screenRegion(hw.bounds());
601
602 Region aboveOpaqueLayers;
603 Region aboveCoveredLayers;
604 Region dirty;
605
606 bool secureFrameBuffer = false;
607
608 size_t i = currentLayers.size();
/*
* Clip不就是计算遮挡吗?z-order从顶向底,合乎逻辑。
*/
609 while (i--) {
610 const sp<LayerBase>& layer = currentLayers[i];
611 layer->validateVisibility(planeTransform);
612
613 // start with the whole surface at its current location
614 const Layer::State& s(layer->drawingState());
615
616 /*
617 * opaqueRegion: area of a surface that is fully opaque.
618 */
619 Region opaqueRegion;
620
621 /*
622 * visibleRegion: area of a surface that is visible on screen
623 * and not fully transparent. This is essentially the layer's
624 * footprint minus the opaque regions above it.
625 * Areas covered by a translucent surface are considered visible.
626 */
627 Region visibleRegion;
628
629 /*
630 * coveredRegion: area of a surface that is covered by all
631 * visible regions above it (which includes the translucent areas).
632 */
633 Region coveredRegion;
634
635
636 // handle hidden surfaces by setting the visible region to empty
637 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
// 该Layer是否半透
638 const bool translucent = !layer->isOpaque();
// 该Layer可见范围
639 const Rect bounds(layer->visibleBounds());
640 visibleRegion.set(bounds);
641 visibleRegion.andSelf(screenRegion);
642 if (!visibleRegion.isEmpty()) {
/*
* 如果本layer具有全透明区域(全透明子窗口),如Video或Camera,
* 本Layer该区域一定是不可见的,visibleRegion应该减去全透区域,
* translucent的判断条件并不表示该Layer为半透,而是有全透区域时,
* 该Layer的Opaque属性应该设置为false,表并非Full Opaque。
* setTransparentRegion/setTransparentRegionWindow
* => setTransparentRegionHint设置透明的。
* 那半透明子窗口如何呢?因为Layer的地位相当于该应用的Top most window,
* 所以半透子窗口下的区域也一定是本Layer的子窗口,而不可能是别的Layer,
* 从而该半透子窗口在本Layer范围内部就做Alpha混叠了,对于本Layer来说是
* Opaque的,所以不需要半透部分区域。半透属性是针对整个Layer的。
*/
643 // Remove the transparent area from the visible region
644 if (translucent) {
645 visibleRegion.subtractSelf(layer->transparentRegionScreen);
646 }
647
648 // compute the opaque region
649 const int32_t layerOrientation = layer->getOrientation();
/*
* 如果该