|
| 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 "filter_contents.h" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | +#include <optional> |
| 9 | +#include <variant> |
| 10 | + |
| 11 | +#include "flutter/fml/logging.h" |
| 12 | +#include "impeller/base/validation.h" |
| 13 | +#include "impeller/entity/contents/content_context.h" |
| 14 | +#include "impeller/entity/contents/solid_color_contents.h" |
| 15 | +#include "impeller/entity/contents/texture_contents.h" |
| 16 | +#include "impeller/entity/entity.h" |
| 17 | +#include "impeller/geometry/path_builder.h" |
| 18 | +#include "impeller/renderer/command_buffer.h" |
| 19 | +#include "impeller/renderer/render_pass.h" |
| 20 | +#include "impeller/renderer/sampler_library.h" |
| 21 | + |
| 22 | +namespace impeller { |
| 23 | + |
| 24 | +/******************************************************************************* |
| 25 | + ******* FilterContents |
| 26 | + ******************************************************************************/ |
| 27 | + |
| 28 | +std::shared_ptr<FilterContents> FilterContents::MakeBlend( |
| 29 | + Entity::BlendMode blend_mode, |
| 30 | + InputTextures input_textures) { |
| 31 | + auto blend = std::make_shared<BlendFilterContents>(); |
| 32 | + blend->SetInputTextures(input_textures); |
| 33 | + blend->SetBlendMode(blend_mode); |
| 34 | + return blend; |
| 35 | +} |
| 36 | + |
| 37 | +FilterContents::FilterContents() = default; |
| 38 | + |
| 39 | +FilterContents::~FilterContents() = default; |
| 40 | + |
| 41 | +void FilterContents::SetInputTextures(InputTextures input_textures) { |
| 42 | + input_textures_ = std::move(input_textures); |
| 43 | +} |
| 44 | + |
| 45 | +bool FilterContents::Render(const ContentContext& renderer, |
| 46 | + const Entity& entity, |
| 47 | + RenderPass& pass) const { |
| 48 | + // Run the filter. |
| 49 | + |
| 50 | + auto maybe_texture = RenderFilterToTexture(renderer, entity, pass); |
| 51 | + if (!maybe_texture.has_value()) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + auto& texture = maybe_texture.value(); |
| 55 | + |
| 56 | + // Draw the resulting texture to the given destination rect, respecting the |
| 57 | + // transform and clip stack. |
| 58 | + |
| 59 | + auto contents = std::make_shared<TextureContents>(); |
| 60 | + contents->SetTexture(texture); |
| 61 | + contents->SetSourceRect(IRect::MakeSize(texture->GetSize())); |
| 62 | + |
| 63 | + return contents->Render(renderer, entity, pass); |
| 64 | +} |
| 65 | + |
| 66 | +std::optional<std::shared_ptr<Texture>> FilterContents::RenderFilterToTexture( |
| 67 | + const ContentContext& renderer, |
| 68 | + const Entity& entity, |
| 69 | + RenderPass& pass) const { |
| 70 | + auto output_size = GetOutputSize(); |
| 71 | + if (output_size.IsZero()) { |
| 72 | + return std::nullopt; |
| 73 | + } |
| 74 | + |
| 75 | + // Resolve all inputs as textures. |
| 76 | + |
| 77 | + std::vector<std::shared_ptr<Texture>> input_textures; |
| 78 | + input_textures.reserve(input_textures_.size()); |
| 79 | + for (const auto& input : input_textures_) { |
| 80 | + if (auto filter = std::get_if<std::shared_ptr<FilterContents>>(&input)) { |
| 81 | + auto texture = |
| 82 | + filter->get()->RenderFilterToTexture(renderer, entity, pass); |
| 83 | + if (!texture.has_value()) { |
| 84 | + return std::nullopt; |
| 85 | + } |
| 86 | + input_textures.push_back(std::move(texture.value())); |
| 87 | + } else if (auto texture = std::get_if<std::shared_ptr<Texture>>(&input)) { |
| 88 | + input_textures.push_back(*texture); |
| 89 | + } else { |
| 90 | + FML_UNREACHABLE(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Create a new texture and render the filter to it. |
| 95 | + |
| 96 | + auto context = renderer.GetContext(); |
| 97 | + |
| 98 | + auto subpass_target = RenderTarget::CreateOffscreen(*context, output_size); |
| 99 | + auto subpass_texture = subpass_target.GetRenderTargetTexture(); |
| 100 | + if (!subpass_texture) { |
| 101 | + return std::nullopt; |
| 102 | + } |
| 103 | + |
| 104 | + auto sub_command_buffer = context->CreateRenderCommandBuffer(); |
| 105 | + sub_command_buffer->SetLabel("Offscreen Filter Command Buffer"); |
| 106 | + if (!sub_command_buffer) { |
| 107 | + return std::nullopt; |
| 108 | + } |
| 109 | + |
| 110 | + auto sub_renderpass = sub_command_buffer->CreateRenderPass(subpass_target); |
| 111 | + if (!sub_renderpass) { |
| 112 | + return std::nullopt; |
| 113 | + } |
| 114 | + sub_renderpass->SetLabel("OffscreenFilterPass"); |
| 115 | + |
| 116 | + if (!RenderFilter(input_textures, renderer, *sub_renderpass)) { |
| 117 | + return std::nullopt; |
| 118 | + } |
| 119 | + |
| 120 | + if (!sub_renderpass->EncodeCommands(*context->GetTransientsAllocator())) { |
| 121 | + return std::nullopt; |
| 122 | + } |
| 123 | + |
| 124 | + if (!sub_command_buffer->SubmitCommands()) { |
| 125 | + return std::nullopt; |
| 126 | + } |
| 127 | + |
| 128 | + return subpass_texture; |
| 129 | +} |
| 130 | + |
| 131 | +ISize FilterContents::GetOutputSize() const { |
| 132 | + if (input_textures_.empty()) { |
| 133 | + return {}; |
| 134 | + } |
| 135 | + |
| 136 | + if (auto filter = |
| 137 | + std::get_if<std::shared_ptr<FilterContents>>(&input_textures_[0])) { |
| 138 | + return filter->get()->GetOutputSize(); |
| 139 | + } |
| 140 | + |
| 141 | + if (auto texture = |
| 142 | + std::get_if<std::shared_ptr<Texture>>(&input_textures_[0])) { |
| 143 | + return texture->get()->GetSize(); |
| 144 | + } |
| 145 | + |
| 146 | + FML_UNREACHABLE(); |
| 147 | +} |
| 148 | + |
| 149 | +/******************************************************************************* |
| 150 | + ******* BlendFilterContents |
| 151 | + ******************************************************************************/ |
| 152 | + |
| 153 | +BlendFilterContents::BlendFilterContents() = default; |
| 154 | + |
| 155 | +BlendFilterContents::~BlendFilterContents() = default; |
| 156 | + |
| 157 | +void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) { |
| 158 | + blend_mode_ = blend_mode; |
| 159 | +} |
| 160 | + |
| 161 | +bool BlendFilterContents::RenderFilter( |
| 162 | + const std::vector<std::shared_ptr<Texture>>& input_textures, |
| 163 | + const ContentContext& renderer, |
| 164 | + RenderPass& pass) const { |
| 165 | + using VS = TexturePipeline::VertexShader; |
| 166 | + using FS = TexturePipeline::FragmentShader; |
| 167 | + |
| 168 | + auto& host_buffer = pass.GetTransientsBuffer(); |
| 169 | + |
| 170 | + VertexBufferBuilder<VS::PerVertexData> vtx_builder; |
| 171 | + vtx_builder.AddVertices({ |
| 172 | + {Point(0, 0), Point(0, 0)}, |
| 173 | + {Point(1, 0), Point(1, 0)}, |
| 174 | + {Point(1, 1), Point(1, 1)}, |
| 175 | + {Point(0, 0), Point(0, 0)}, |
| 176 | + {Point(1, 1), Point(1, 1)}, |
| 177 | + {Point(0, 1), Point(0, 1)}, |
| 178 | + }); |
| 179 | + auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer); |
| 180 | + |
| 181 | + VS::FrameInfo frame_info; |
| 182 | + frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1)); |
| 183 | + frame_info.alpha = 1; |
| 184 | + |
| 185 | + auto uniform_view = host_buffer.EmplaceUniform(frame_info); |
| 186 | + auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({}); |
| 187 | + |
| 188 | + Command cmd; |
| 189 | + cmd.label = "Blend Filter"; |
| 190 | + auto options = OptionsFromPass(pass); |
| 191 | + options.blend_mode = blend_mode_; |
| 192 | + cmd.pipeline = renderer.GetTexturePipeline(options); |
| 193 | + cmd.BindVertices(vtx_buffer); |
| 194 | + VS::BindFrameInfo(cmd, uniform_view); |
| 195 | + for (const auto& texture : input_textures) { |
| 196 | + FS::BindTextureSampler(cmd, texture, sampler); |
| 197 | + pass.AddCommand(cmd); |
| 198 | + } |
| 199 | + |
| 200 | + return true; |
| 201 | +} |
| 202 | + |
| 203 | +} // namespace impeller |
0 commit comments