| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "flutter/flow/layers/display_list_layer.h" |
| |
| #include "flutter/flow/display_list_canvas.h" |
| |
| namespace flutter { |
| |
| DisplayListLayer::DisplayListLayer(const SkPoint& offset, |
| sk_sp<DisplayList> display_list, |
| bool is_complex, |
| bool will_change) |
| : offset_(offset), |
| display_list_(display_list), |
| is_complex_(is_complex), |
| will_change_(will_change) {} |
| |
| #ifdef FLUTTER_ENABLE_DIFF_CONTEXT |
| |
| bool DisplayListLayer::IsReplacing(DiffContext* context, |
| const Layer* layer) const { |
| // Only return true for identical display lists; This way |
| // ContainerLayer::DiffChildren can detect when a display list layer |
| // got inserted between other display list layers |
| auto old_layer = layer->as_display_list_layer(); |
| return old_layer != nullptr && offset_ == old_layer->offset_ && |
| Compare(context->statistics(), this, old_layer); |
| } |
| |
| void DisplayListLayer::Diff(DiffContext* context, const Layer* old_layer) { |
| DiffContext::AutoSubtreeRestore subtree(context); |
| if (!context->IsSubtreeDirty()) { |
| #ifndef NDEBUG |
| FML_DCHECK(old_layer); |
| auto prev = old_layer->as_display_list_layer(); |
| DiffContext::Statistics dummy_statistics; |
| // IsReplacing has already determined that the display list is same |
| FML_DCHECK(prev->offset_ == offset_ && |
| Compare(dummy_statistics, this, prev)); |
| #endif |
| } |
| context->PushTransform(SkMatrix::Translate(offset_.x(), offset_.y())); |
| context->AddLayerBounds(display_list_->bounds()); |
| context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion()); |
| } |
| |
| bool DisplayListLayer::Compare(DiffContext::Statistics& statistics, |
| const DisplayListLayer* l1, |
| const DisplayListLayer* l2) { |
| const auto& dl1 = l1->display_list_; |
| const auto& dl2 = l2->display_list_; |
| if (dl1.get() == dl2.get()) { |
| statistics.AddSameInstancePicture(); |
| return true; |
| } |
| const auto op_cnt_1 = dl1->op_count(); |
| const auto op_cnt_2 = dl2->op_count(); |
| const auto op_bytes_1 = dl1->bytes(); |
| const auto op_bytes_2 = dl2->bytes(); |
| if (op_cnt_1 != op_cnt_2 || op_bytes_1 != op_bytes_2 || |
| dl1->bounds() != dl2->bounds()) { |
| statistics.AddNewPicture(); |
| return false; |
| } |
| |
| if (op_bytes_1 > kMaxBytesToCompare) { |
| statistics.AddPictureTooComplexToCompare(); |
| return false; |
| } |
| |
| statistics.AddDeepComparePicture(); |
| |
| auto res = dl1->Equals(*dl2); |
| if (res) { |
| statistics.AddDifferentInstanceButEqualPicture(); |
| } else { |
| statistics.AddNewPicture(); |
| } |
| return res; |
| } |
| |
| #endif // FLUTTER_ENABLE_DIFF_CONTEXT |
| |
| void DisplayListLayer::Preroll(PrerollContext* context, |
| const SkMatrix& matrix) { |
| TRACE_EVENT0("flutter", "DisplayListLayer::Preroll"); |
| |
| DisplayList* disp_list = display_list(); |
| |
| if (auto* cache = context->raster_cache) { |
| TRACE_EVENT0("flutter", "DisplayListLayer::RasterCache (Preroll)"); |
| |
| SkMatrix ctm = matrix; |
| ctm.preTranslate(offset_.x(), offset_.y()); |
| #ifndef SUPPORT_FRACTIONAL_TRANSLATION |
| ctm = RasterCache::GetIntegralTransCTM(ctm); |
| #endif |
| cache->Prepare(context->gr_context, disp_list, ctm, |
| context->dst_color_space, is_complex_, will_change_); |
| } |
| |
| SkRect bounds = disp_list->bounds().makeOffset(offset_.x(), offset_.y()); |
| set_paint_bounds(bounds); |
| } |
| |
| void DisplayListLayer::Paint(PaintContext& context) const { |
| TRACE_EVENT0("flutter", "DisplayListLayer::Paint"); |
| FML_DCHECK(display_list_.get()); |
| FML_DCHECK(needs_painting(context)); |
| |
| SkAutoCanvasRestore save(context.leaf_nodes_canvas, true); |
| context.leaf_nodes_canvas->translate(offset_.x(), offset_.y()); |
| #ifndef SUPPORT_FRACTIONAL_TRANSLATION |
| context.leaf_nodes_canvas->setMatrix(RasterCache::GetIntegralTransCTM( |
| context.leaf_nodes_canvas->getTotalMatrix())); |
| #endif |
| |
| if (context.raster_cache && |
| context.raster_cache->Draw(*display_list(), *context.leaf_nodes_canvas)) { |
| TRACE_EVENT_INSTANT0("flutter", "raster cache hit"); |
| return; |
| } |
| |
| display_list()->RenderTo(context.leaf_nodes_canvas); |
| } |
| |
| } // namespace flutter |