| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/flow/testing/mock_layer.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "flutter/flow/layers/container_layer.h" |
| 10 | #include "flutter/flow/layers/layer.h" |
| 11 | #include "flutter/flow/testing/mock_raster_cache.h" |
| 12 | namespace flutter { |
| 13 | namespace testing { |
| 14 | |
| 15 | MockLayer::MockLayer(const SkPath& path, DlPaint paint) |
| 16 | : fake_paint_path_(path), fake_paint_(std::move(paint)) {} |
| 17 | |
| 18 | bool MockLayer::IsReplacing(DiffContext* context, const Layer* layer) const { |
| 19 | // Similar to PictureLayer, only return true for identical mock layers; |
| 20 | // That way ContainerLayer::DiffChildren can properly detect mock layer |
| 21 | // insertion |
| 22 | auto mock_layer = layer->as_mock_layer(); |
| 23 | return mock_layer && mock_layer->fake_paint_ == fake_paint_ && |
| 24 | mock_layer->fake_paint_path_ == fake_paint_path_; |
| 25 | } |
| 26 | |
| 27 | void MockLayer::Diff(DiffContext* context, const Layer* old_layer) { |
| 28 | DiffContext::AutoSubtreeRestore subtree(context); |
| 29 | context->AddLayerBounds(rect: fake_paint_path_.getBounds()); |
| 30 | context->SetLayerPaintRegion(layer: this, region: context->CurrentSubtreeRegion()); |
| 31 | } |
| 32 | |
| 33 | void MockLayer::Preroll(PrerollContext* context) { |
| 34 | context->state_stack.fill(mutators: &parent_mutators_); |
| 35 | parent_matrix_ = context->state_stack.transform_3x3(); |
| 36 | parent_cull_rect_ = context->state_stack.local_cull_rect(); |
| 37 | |
| 38 | set_parent_has_platform_view(context->has_platform_view); |
| 39 | set_parent_has_texture_layer(context->has_texture_layer); |
| 40 | |
| 41 | context->has_platform_view = fake_has_platform_view(); |
| 42 | context->has_texture_layer = fake_has_texture_layer(); |
| 43 | set_paint_bounds(fake_paint_path_.getBounds()); |
| 44 | if (fake_reads_surface()) { |
| 45 | context->surface_needs_readback = true; |
| 46 | } |
| 47 | if (fake_opacity_compatible()) { |
| 48 | context->renderable_state_flags = LayerStateStack::kCallerCanApplyOpacity; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void MockLayer::Paint(PaintContext& context) const { |
| 53 | FML_DCHECK(needs_painting(context)); |
| 54 | |
| 55 | if (expected_paint_matrix_.has_value()) { |
| 56 | SkMatrix matrix = context.canvas->GetTransform(); |
| 57 | |
| 58 | EXPECT_EQ(matrix, expected_paint_matrix_.value()); |
| 59 | } |
| 60 | |
| 61 | DlPaint paint = fake_paint_; |
| 62 | context.state_stack.fill(paint); |
| 63 | context.canvas->DrawPath(path: fake_paint_path_, paint); |
| 64 | } |
| 65 | |
| 66 | void MockCacheableContainerLayer::Preroll(PrerollContext* context) { |
| 67 | Layer::AutoPrerollSaveLayerState save = |
| 68 | Layer::AutoPrerollSaveLayerState::Create(preroll_context: context); |
| 69 | auto cache = AutoCache(layer_raster_cache_item_.get(), context, |
| 70 | context->state_stack.transform_3x3()); |
| 71 | |
| 72 | ContainerLayer::Preroll(context); |
| 73 | } |
| 74 | |
| 75 | void MockCacheableLayer::Preroll(PrerollContext* context) { |
| 76 | Layer::AutoPrerollSaveLayerState save = |
| 77 | Layer::AutoPrerollSaveLayerState::Create(preroll_context: context); |
| 78 | auto cache = AutoCache(raster_cache_item_.get(), context, |
| 79 | context->state_stack.transform_3x3()); |
| 80 | |
| 81 | MockLayer::Preroll(context); |
| 82 | } |
| 83 | |
| 84 | } // namespace testing |
| 85 | } // namespace flutter |
| 86 | |