|
| 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/testing/testing.h" |
| 6 | +#include "gmock/gmock.h" |
| 7 | +#include "impeller/entity/contents/content_context.h" |
| 8 | +#include "impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h" |
| 9 | +#include "impeller/renderer/testing/mocks.h" |
| 10 | + |
| 11 | +namespace impeller { |
| 12 | +namespace testing { |
| 13 | + |
| 14 | +using ::testing::_; |
| 15 | +using ::testing::An; |
| 16 | +using ::testing::Invoke; |
| 17 | +using ::testing::Return; |
| 18 | +using ::testing::ReturnRef; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +Scalar CalculateSigmaForBlurRadius(Scalar blur_radius) { |
| 23 | + // See Sigma.h |
| 24 | + return (blur_radius / kKernelRadiusPerSigma) + 0.5; |
| 25 | +} |
| 26 | +} // namespace |
| 27 | + |
| 28 | +class DirectionalGaussianBlurFilterContentsTest : public ::testing::Test { |
| 29 | + public: |
| 30 | + void SetUp() override { capabilities_ = mock_capabilities_; } |
| 31 | + |
| 32 | + std::shared_ptr<MockCapabilities> mock_capabilities_ = |
| 33 | + std::make_shared<MockCapabilities>(); |
| 34 | + std::shared_ptr<const Capabilities> capabilities_; |
| 35 | +}; |
| 36 | + |
| 37 | +TEST_F(DirectionalGaussianBlurFilterContentsTest, CoverageWithEffectTransform) { |
| 38 | + TextureDescriptor desc = { |
| 39 | + .size = ISize(100, 100), |
| 40 | + }; |
| 41 | + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); |
| 42 | + auto contents = std::make_unique<DirectionalGaussianBlurFilterContents>(); |
| 43 | + contents->SetSigma(Sigma{sigma_radius_1}); |
| 44 | + contents->SetDirection({1.0, 0.0}); |
| 45 | + std::shared_ptr<MockTexture> texture = std::make_shared<MockTexture>(desc); |
| 46 | + EXPECT_CALL(*texture, GetSize()).WillRepeatedly(Return(ISize(100, 100))); |
| 47 | + FilterInput::Vector inputs = {FilterInput::Make(texture)}; |
| 48 | + Entity entity; |
| 49 | + entity.SetTransformation(Matrix::MakeTranslation({100, 100, 0})); |
| 50 | + std::optional<Rect> coverage = contents->GetFilterCoverage( |
| 51 | + inputs, entity, /*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0})); |
| 52 | + ASSERT_EQ(coverage, Rect::MakeLTRB(100 - 2, 100, 200 + 2, 200)); |
| 53 | +} |
| 54 | + |
| 55 | +TEST_F(DirectionalGaussianBlurFilterContentsTest, FilterSourceCoverage) { |
| 56 | + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); |
| 57 | + auto contents = std::make_unique<DirectionalGaussianBlurFilterContents>(); |
| 58 | + contents->SetSigma(Sigma{sigma_radius_1}); |
| 59 | + contents->SetDirection({1.0, 0.0}); |
| 60 | + std::optional<Rect> coverage = contents->GetFilterSourceCoverage( |
| 61 | + /*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0}), |
| 62 | + /*output_limit=*/Rect::MakeLTRB(100, 100, 200, 200)); |
| 63 | + ASSERT_EQ(coverage, Rect::MakeLTRB(100 - 2, 100, 200 + 2, 200)); |
| 64 | +} |
| 65 | + |
| 66 | +TEST_F(DirectionalGaussianBlurFilterContentsTest, RenderNoCoverage) { |
| 67 | + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); |
| 68 | + auto contents = std::make_unique<DirectionalGaussianBlurFilterContents>(); |
| 69 | + contents->SetSigma(Sigma{sigma_radius_1}); |
| 70 | + contents->SetDirection({1.0, 0.0}); |
| 71 | + auto mock_context = std::make_shared<MockImpellerContext>(); |
| 72 | + auto mock_typographer_context = std::make_shared<MockTypographerContext>(); |
| 73 | + auto mock_allocator = std::make_shared<MockAllocator>(); |
| 74 | + auto mock_render_target_allocator = |
| 75 | + std::make_shared<MockRenderTargetAllocator>(mock_allocator); |
| 76 | + ContentContext renderer(mock_context, mock_typographer_context, |
| 77 | + mock_render_target_allocator); |
| 78 | + Entity entity; |
| 79 | + Rect coverage_hint = Rect::MakeLTRB(0, 0, 0, 0); |
| 80 | + std::optional<Entity> result = |
| 81 | + contents->GetEntity(renderer, entity, coverage_hint); |
| 82 | + ASSERT_FALSE(result.has_value()); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(DirectionalGaussianBlurFilterContentsTest, RenderSomething) { |
| 86 | + TextureDescriptor desc = { |
| 87 | + .size = ISize(100, 100), |
| 88 | + }; |
| 89 | + std::shared_ptr<MockTexture> texture = std::make_shared<MockTexture>(desc); |
| 90 | + EXPECT_CALL(*texture, GetSize()).WillRepeatedly(Return(ISize(100, 100))); |
| 91 | + Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0); |
| 92 | + auto contents = std::make_unique<DirectionalGaussianBlurFilterContents>(); |
| 93 | + contents->SetSigma(Sigma{sigma_radius_1}); |
| 94 | + contents->SetDirection({1.0, 0.0}); |
| 95 | + contents->SetInputs({FilterInput::Make(texture)}); |
| 96 | + auto mock_context = std::make_shared<MockImpellerContext>(); |
| 97 | + EXPECT_CALL(*mock_context, GetCapabilities()) |
| 98 | + .WillRepeatedly(ReturnRef(capabilities_)); |
| 99 | + EXPECT_CALL(*mock_context, IsValid()).WillRepeatedly(Return(true)); |
| 100 | + auto mock_sampler_library = std::make_shared<MockSamplerLibrary>(); |
| 101 | + auto mock_shader_library = std::make_shared<MockShaderLibrary>(); |
| 102 | + auto mock_pipeline_library = std::make_shared<MockPipelineLibrary>(); |
| 103 | + EXPECT_CALL(*mock_pipeline_library, GetPipeline(An<PipelineDescriptor>())) |
| 104 | + .WillRepeatedly( |
| 105 | + Invoke([&mock_pipeline_library](PipelineDescriptor descriptor) { |
| 106 | + PipelineFuture<PipelineDescriptor> result; |
| 107 | + std::promise<std::shared_ptr<Pipeline<PipelineDescriptor>>> promise; |
| 108 | + auto mock_pipeline = |
| 109 | + std::make_shared<MockPipeline<PipelineDescriptor>>( |
| 110 | + mock_pipeline_library, descriptor); |
| 111 | + EXPECT_CALL(*mock_pipeline, IsValid()).WillRepeatedly(Return(true)); |
| 112 | + promise.set_value(mock_pipeline); |
| 113 | + result.descriptor = descriptor; |
| 114 | + result.future = promise.get_future(); |
| 115 | + return result; |
| 116 | + })); |
| 117 | + EXPECT_CALL(*mock_shader_library, GetFunction(_, _)) |
| 118 | + .WillRepeatedly(Invoke([](std::string_view name, ShaderStage stage) { |
| 119 | + return std::make_shared<MockShaderFunction>(UniqueID(), |
| 120 | + std::string(name), stage); |
| 121 | + })); |
| 122 | + EXPECT_CALL(*mock_context, GetSamplerLibrary()) |
| 123 | + .WillRepeatedly(Return(mock_sampler_library)); |
| 124 | + EXPECT_CALL(*mock_context, GetShaderLibrary()) |
| 125 | + .WillRepeatedly(Return(mock_shader_library)); |
| 126 | + EXPECT_CALL(*mock_context, GetPipelineLibrary()) |
| 127 | + .WillRepeatedly(Return(mock_pipeline_library)); |
| 128 | + EXPECT_CALL(*mock_context, CreateCommandBuffer()) |
| 129 | + .WillRepeatedly(Invoke(([&mock_context]() { |
| 130 | + auto result = std::make_shared<MockCommandBuffer>(mock_context); |
| 131 | + EXPECT_CALL(*result, IsValid()).WillRepeatedly(Return(true)); |
| 132 | + EXPECT_CALL(*result, OnSubmitCommands(_)).WillRepeatedly(Return(true)); |
| 133 | + EXPECT_CALL(*result, OnCreateRenderPass(_)) |
| 134 | + .WillRepeatedly( |
| 135 | + Invoke(([&mock_context](const RenderTarget& render_target) { |
| 136 | + auto result = std::make_shared<MockRenderPass>(mock_context, |
| 137 | + render_target); |
| 138 | + EXPECT_CALL(*result, IsValid).WillRepeatedly(Return(true)); |
| 139 | + EXPECT_CALL(*result, OnEncodeCommands(_)) |
| 140 | + .WillRepeatedly(Return(true)); |
| 141 | + return result; |
| 142 | + }))); |
| 143 | + return result; |
| 144 | + }))); |
| 145 | + auto mock_typographer_context = std::make_shared<MockTypographerContext>(); |
| 146 | + auto mock_allocator = std::make_shared<MockAllocator>(); |
| 147 | + auto mock_render_target_allocator = |
| 148 | + std::make_shared<MockRenderTargetAllocator>(mock_allocator); |
| 149 | + EXPECT_CALL(*mock_render_target_allocator, CreateTexture(_)) |
| 150 | + .WillRepeatedly(Invoke(([](const TextureDescriptor& desc) { |
| 151 | + auto result = std::make_shared<MockTexture>(desc); |
| 152 | + EXPECT_CALL(*result, IsValid()).WillRepeatedly(Return(true)); |
| 153 | + return result; |
| 154 | + }))); |
| 155 | + ContentContext renderer(mock_context, mock_typographer_context, |
| 156 | + mock_render_target_allocator); |
| 157 | + Entity entity; |
| 158 | + Rect coverage_hint = Rect::MakeLTRB(0, 0, 0, 0); |
| 159 | + std::optional<Entity> result = |
| 160 | + contents->GetEntity(renderer, entity, coverage_hint); |
| 161 | + ASSERT_TRUE(result.has_value()); |
| 162 | + ASSERT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver); |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace testing |
| 166 | +} // namespace impeller |
0 commit comments