|
| 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 "impeller/entity/contents/color_source_text_contents.h" |
| 6 | + |
| 7 | +#include "impeller/entity/contents/content_context.h" |
| 8 | +#include "impeller/entity/contents/texture_contents.h" |
| 9 | +#include "impeller/renderer/render_pass.h" |
| 10 | + |
| 11 | +namespace impeller { |
| 12 | + |
| 13 | +ColorSourceTextContents::ColorSourceTextContents() = default; |
| 14 | + |
| 15 | +ColorSourceTextContents::~ColorSourceTextContents() = default; |
| 16 | + |
| 17 | +void ColorSourceTextContents::SetTextContents( |
| 18 | + std::shared_ptr<TextContents> text_contents) { |
| 19 | + text_contents_ = std::move(text_contents); |
| 20 | +} |
| 21 | + |
| 22 | +void ColorSourceTextContents::SetColorSourceContents( |
| 23 | + std::shared_ptr<ColorSourceContents> color_source_contents) { |
| 24 | + color_source_contents_ = std::move(color_source_contents); |
| 25 | +} |
| 26 | + |
| 27 | +std::optional<Rect> ColorSourceTextContents::GetCoverage( |
| 28 | + const Entity& entity) const { |
| 29 | + return text_contents_->GetCoverage(entity); |
| 30 | +} |
| 31 | + |
| 32 | +void ColorSourceTextContents::SetTextPosition(Point position) { |
| 33 | + position_ = position; |
| 34 | +} |
| 35 | + |
| 36 | +bool ColorSourceTextContents::Render(const ContentContext& renderer, |
| 37 | + const Entity& entity, |
| 38 | + RenderPass& pass) const { |
| 39 | + auto coverage = text_contents_->GetCoverage(entity); |
| 40 | + if (!coverage.has_value()) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + auto transform = entity.GetTransformation(); |
| 44 | + |
| 45 | + text_contents_->SetColor(Color::Black()); |
| 46 | + color_source_contents_->SetGeometry( |
| 47 | + Geometry::MakeRect(Rect::MakeSize(coverage->size))); |
| 48 | + |
| 49 | + // offset the color source so it behaves as if it were drawn in the original |
| 50 | + // position. |
| 51 | + auto effect_transform = |
| 52 | + color_source_contents_->GetInverseMatrix().Invert().Translate(-position_); |
| 53 | + color_source_contents_->SetEffectTransform(effect_transform); |
| 54 | + |
| 55 | + auto new_texture = renderer.MakeSubpass( |
| 56 | + "Text Color Blending", ISize::Ceil(coverage.value().size), |
| 57 | + [&](const ContentContext& context, RenderPass& pass) { |
| 58 | + Entity sub_entity; |
| 59 | + sub_entity.SetTransformation(transform); |
| 60 | + sub_entity.SetContents(text_contents_); |
| 61 | + sub_entity.SetBlendMode(BlendMode::kSource); |
| 62 | + if (!sub_entity.Render(context, pass)) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + sub_entity.SetContents(color_source_contents_); |
| 67 | + sub_entity.SetBlendMode(BlendMode::kSourceIn); |
| 68 | + return sub_entity.Render(context, pass); |
| 69 | + }); |
| 70 | + if (!new_texture) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + auto dest_rect = Rect::MakeSize(new_texture->GetSize()) |
| 75 | + .TransformBounds(transform.Invert()) |
| 76 | + .Shift(position_); |
| 77 | + |
| 78 | + auto texture_contents = TextureContents::MakeRect(dest_rect); |
| 79 | + texture_contents->SetTexture(new_texture); |
| 80 | + texture_contents->SetSourceRect(Rect::MakeSize(new_texture->GetSize())); |
| 81 | + return texture_contents->Render(renderer, entity, pass); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace impeller |
0 commit comments