forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
320 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
impeller/entity/contents/filters/border_mask_blur_filter_contents.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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 "impeller/entity/contents/filters/border_mask_blur_filter_contents.h" | ||
#include "impeller/entity/contents/content_context.h" | ||
|
||
#include "impeller/entity/contents/contents.h" | ||
#include "impeller/renderer/render_pass.h" | ||
#include "impeller/renderer/sampler_library.h" | ||
|
||
namespace impeller { | ||
|
||
BorderMaskBlurFilterContents::BorderMaskBlurFilterContents() = default; | ||
|
||
BorderMaskBlurFilterContents::~BorderMaskBlurFilterContents() = default; | ||
|
||
void BorderMaskBlurFilterContents::SetSigma(Sigma sigma_x, Sigma sigma_y) { | ||
sigma_x_ = sigma_x; | ||
sigma_y_ = sigma_y; | ||
} | ||
|
||
void BorderMaskBlurFilterContents::SetBlurStyle(BlurStyle blur_style) { | ||
blur_style_ = blur_style; | ||
|
||
switch (blur_style) { | ||
case FilterContents::BlurStyle::kNormal: | ||
src_color_factor_ = false; | ||
inner_blur_factor_ = true; | ||
outer_blur_factor_ = true; | ||
break; | ||
case FilterContents::BlurStyle::kSolid: | ||
src_color_factor_ = true; | ||
inner_blur_factor_ = false; | ||
outer_blur_factor_ = true; | ||
break; | ||
case FilterContents::BlurStyle::kOuter: | ||
src_color_factor_ = false; | ||
inner_blur_factor_ = false; | ||
outer_blur_factor_ = true; | ||
break; | ||
case FilterContents::BlurStyle::kInner: | ||
src_color_factor_ = false; | ||
inner_blur_factor_ = true; | ||
outer_blur_factor_ = false; | ||
break; | ||
} | ||
} | ||
|
||
bool BorderMaskBlurFilterContents::RenderFilter( | ||
const FilterInput::Vector& inputs, | ||
const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass, | ||
const Rect& coverage) const { | ||
if (inputs.empty()) { | ||
return true; | ||
} | ||
|
||
using VS = BorderMaskBlurPipeline::VertexShader; | ||
using FS = BorderMaskBlurPipeline::FragmentShader; | ||
|
||
auto& host_buffer = pass.GetTransientsBuffer(); | ||
|
||
auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity); | ||
if (!input_snapshot.has_value()) { | ||
return true; | ||
} | ||
auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage); | ||
if (!maybe_input_uvs.has_value()) { | ||
return true; | ||
} | ||
auto input_uvs = maybe_input_uvs.value(); | ||
|
||
VertexBufferBuilder<VS::PerVertexData> vtx_builder; | ||
vtx_builder.AddVertices({ | ||
{Point(0, 0), input_uvs[0]}, | ||
{Point(1, 0), input_uvs[1]}, | ||
{Point(1, 1), input_uvs[3]}, | ||
{Point(0, 0), input_uvs[0]}, | ||
{Point(1, 1), input_uvs[3]}, | ||
{Point(0, 1), input_uvs[2]}, | ||
}); | ||
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer); | ||
|
||
Command cmd; | ||
cmd.label = "Border Mask Blur Filter"; | ||
auto options = OptionsFromPass(pass); | ||
options.blend_mode = Entity::BlendMode::kSource; | ||
cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options); | ||
cmd.BindVertices(vtx_buffer); | ||
|
||
VS::FrameInfo frame_info; | ||
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1)); | ||
auto scale = entity.GetTransformation().GetScale(); | ||
frame_info.sigma_uv = Vector2(scale.x, scale.y) * | ||
Vector2(sigma_x_.sigma, sigma_y_.sigma).Abs() / | ||
input_snapshot->texture->GetSize(); | ||
frame_info.src_factor = src_color_factor_; | ||
frame_info.inner_blur_factor = inner_blur_factor_; | ||
frame_info.outer_blur_factor = outer_blur_factor_; | ||
auto uniform_view = host_buffer.EmplaceUniform(frame_info); | ||
VS::BindFrameInfo(cmd, uniform_view); | ||
|
||
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({}); | ||
FS::BindTextureSampler(cmd, input_snapshot->texture, sampler); | ||
|
||
return pass.AddCommand(std::move(cmd)); | ||
} | ||
|
||
std::optional<Rect> BorderMaskBlurFilterContents::GetCoverage( | ||
const Entity& entity) const { | ||
auto coverage = FilterContents::GetCoverage(entity); | ||
if (!coverage.has_value()) { | ||
return std::nullopt; | ||
} | ||
|
||
// Technically this works with all of our current filters, but this should be | ||
// using the input[0] transform, not the entity transform! | ||
// See: https://github.com/flutter/impeller/pull/130#issuecomment-1098892423 | ||
auto transformed_blur_vector = | ||
entity.GetTransformation() | ||
.TransformDirection( | ||
Vector2(Radius{sigma_x_}.radius, Radius{sigma_y_}.radius)) | ||
.Abs(); | ||
auto extent = coverage->size + transformed_blur_vector * 2; | ||
return Rect(coverage->origin - transformed_blur_vector, | ||
Size(extent.x, extent.y)); | ||
} | ||
|
||
} // namespace impeller |
44 changes: 44 additions & 0 deletions
44
impeller/entity/contents/filters/border_mask_blur_filter_contents.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include "impeller/entity/contents/filters/filter_contents.h" | ||
#include "impeller/entity/contents/filters/filter_input.h" | ||
|
||
namespace impeller { | ||
|
||
class BorderMaskBlurFilterContents final : public FilterContents { | ||
public: | ||
BorderMaskBlurFilterContents(); | ||
|
||
~BorderMaskBlurFilterContents() override; | ||
|
||
void SetSigma(Sigma sigma_x, Sigma sigma_y); | ||
|
||
void SetBlurStyle(BlurStyle blur_style); | ||
|
||
// |Contents| | ||
std::optional<Rect> GetCoverage(const Entity& entity) const override; | ||
|
||
private: | ||
// |FilterContents| | ||
bool RenderFilter(const FilterInput::Vector& input_textures, | ||
const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass, | ||
const Rect& coverage) const override; | ||
Sigma sigma_x_; | ||
Sigma sigma_y_; | ||
BlurStyle blur_style_ = BlurStyle::kNormal; | ||
bool src_color_factor_ = false; | ||
bool inner_blur_factor_ = true; | ||
bool outer_blur_factor_ = true; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(BorderMaskBlurFilterContents); | ||
}; | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.