Setup a paint pass delgate.

This commit is contained in:
Chinmay Garde
2021-11-28 14:28:44 -08:00
committed by Dan Field
parent eef8ccc51c
commit ed27debb52
16 changed files with 108 additions and 15 deletions

View File

@@ -14,6 +14,8 @@ impeller_component("aiks") {
"image.h",
"paint.cc",
"paint.h",
"paint_pass_delegate.cc",
"paint_pass_delegate.h",
"picture.cc",
"picture.h",
"picture_recorder.cc",

View File

@@ -112,7 +112,7 @@ TEST_F(AiksTest, CanRenderGroupOpacity) {
Paint red;
red.color = Color::Red();
Paint green;
green.color = Color::Green();
green.color = Color::Green().WithAlpha(0.5);
Paint blue;
blue.color = Color::Blue();

View File

@@ -7,6 +7,7 @@
#include <algorithm>
#include "flutter/fml/logging.h"
#include "impeller/aiks/paint_pass_delegate.h"
#include "impeller/geometry/path_builder.h"
namespace impeller {
@@ -90,7 +91,9 @@ void Canvas::DrawPath(Path path, Paint paint) {
GetCurrentPass().AddEntity(std::move(entity));
}
void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {
void Canvas::SaveLayer(Paint paint, std::optional<Rect> bounds) {
GetCurrentPass().SetDelegate(
std::make_unique<PaintPassDelegate>(std::move(paint)));
Save(true);
}

View File

@@ -31,7 +31,7 @@ class Canvas {
void Save();
void SaveLayer(const Paint& paint, std::optional<Rect> bounds = std::nullopt);
void SaveLayer(Paint paint, std::optional<Rect> bounds = std::nullopt);
bool Restore();

View File

@@ -18,7 +18,7 @@ struct Paint {
kStroke,
};
Color color;
Color color = Color::Black();
Scalar stroke_width = 0.0;
Style style = Style::kFill;

View File

@@ -0,0 +1,35 @@
// 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/aiks/paint_pass_delegate.h"
#include "impeller/entity/contents.h"
namespace impeller {
PaintPassDelegate::PaintPassDelegate(Paint paint) : paint_(std::move(paint)) {}
// |EntityPassDelgate|
PaintPassDelegate::~PaintPassDelegate() = default;
// |EntityPassDelgate|
bool PaintPassDelegate::CanCollapseIntoParentPass() {
if (paint_.color.IsOpaque()) {
return true;
}
return false;
}
// |EntityPassDelgate|
std::shared_ptr<Contents> PaintPassDelegate::CreateContentsForSubpassTarget(
std::shared_ptr<Texture> target) {
auto contents = std::make_shared<TextureContents>();
contents->SetTexture(target);
contents->SetSourceRect(IRect::MakeSize(target->GetSize()));
contents->SetOpacity(paint_.color.alpha);
return contents;
}
} // namespace impeller

View File

@@ -0,0 +1,33 @@
// 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 "flutter/fml/macros.h"
#include "impeller/aiks/paint.h"
#include "impeller/entity/entity_pass_delegate.h"
namespace impeller {
class PaintPassDelegate final : public EntityPassDelegate {
public:
PaintPassDelegate(Paint paint);
// |EntityPassDelgate|
~PaintPassDelegate() override;
// |EntityPassDelgate|
bool CanCollapseIntoParentPass() override;
// |EntityPassDelgate|
std::shared_ptr<Contents> CreateContentsForSubpassTarget(
std::shared_ptr<Texture> target) override;
private:
const Paint paint_;
FML_DISALLOW_COPY_AND_ASSIGN(PaintPassDelegate);
};
} // namespace impeller

View File

@@ -184,6 +184,10 @@ std::shared_ptr<Texture> TextureContents::GetTexture() const {
return texture_;
}
void TextureContents::SetOpacity(Scalar opacity) {
opacity_ = opacity;
}
bool TextureContents::Render(const ContentRenderer& renderer,
const Entity& entity,
RenderPass& pass) const {
@@ -233,6 +237,7 @@ bool TextureContents::Render(const ContentRenderer& renderer,
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
frame_info.alpha = opacity_;
Command cmd;
cmd.label = "TextureFill";

View File

@@ -94,6 +94,8 @@ class TextureContents final : public Contents {
void SetSourceRect(const IRect& source_rect);
void SetOpacity(Scalar opacity);
const IRect& GetSourceRect() const;
// |Contents|
@@ -104,6 +106,7 @@ class TextureContents final : public Contents {
public:
std::shared_ptr<Texture> texture_;
IRect source_rect_;
Scalar opacity_ = 1.0f;
FML_DISALLOW_COPY_AND_ASSIGN(TextureContents);
};

View File

@@ -11,15 +11,17 @@
namespace impeller {
EntityPass::EntityPass(std::unique_ptr<EntityPassDelegate> delegate)
: delegate_(std::move(delegate)) {
if (!delegate_) {
delegate_ = EntityPassDelegate::MakeDefault();
}
}
EntityPass::EntityPass() = default;
EntityPass::~EntityPass() = default;
void EntityPass::SetDelegate(std::unique_ptr<EntityPassDelegate> delegate) {
if (!delegate) {
return;
}
delegate_ = std::move(delegate);
}
void EntityPass::AddEntity(Entity entity) {
entities_.emplace_back(std::move(entity));
}
@@ -117,7 +119,7 @@ bool EntityPass::Render(ContentRenderer& renderer,
}
auto offscreen_texture_contents =
delegate_->CreateContentsForSubpassTarget(*subpass_texture);
delegate_->CreateContentsForSubpassTarget(subpass_texture);
if (!offscreen_texture_contents) {
// This is an error because the subpass delegate said the pass couldn't be

View File

@@ -23,10 +23,12 @@ class EntityPass {
using Entities = std::vector<Entity>;
using Subpasses = std::vector<std::unique_ptr<EntityPass>>;
EntityPass(std::unique_ptr<EntityPassDelegate> delegate = nullptr);
EntityPass();
~EntityPass();
void SetDelegate(std::unique_ptr<EntityPassDelegate> delgate);
size_t GetSubpassesDepth() const;
std::unique_ptr<EntityPass> Clone() const;
@@ -61,7 +63,8 @@ class EntityPass {
EntityPass* superpass_ = nullptr;
Matrix xformation_;
size_t stencil_depth_ = 0u;
std::unique_ptr<EntityPassDelegate> delegate_;
std::unique_ptr<EntityPassDelegate> delegate_ =
EntityPassDelegate::MakeDefault();
FML_DISALLOW_COPY_AND_ASSIGN(EntityPass);
};

View File

@@ -19,7 +19,7 @@ class DefaultEntityPassDelegate final : public EntityPassDelegate {
bool CanCollapseIntoParentPass() override { return true; }
std::shared_ptr<Contents> CreateContentsForSubpassTarget(
const Texture& target) override {
std::shared_ptr<Texture> target) override {
// Not possible since this pass always collapses into its parent.
FML_UNREACHABLE();
}

View File

@@ -23,7 +23,7 @@ class EntityPassDelegate {
virtual bool CanCollapseIntoParentPass() = 0;
virtual std::shared_ptr<Contents> CreateContentsForSubpassTarget(
const Texture& target) = 0;
std::shared_ptr<Texture> target) = 0;
private:
FML_DISALLOW_COPY_AND_ASSIGN(EntityPassDelegate);

View File

@@ -5,10 +5,12 @@
uniform sampler2D texture_sampler;
in vec2 v_texture_coords;
in float v_alpha;
out vec4 frag_color;
void main() {
vec4 sampled = texture(texture_sampler, v_texture_coords);
sampled.w *= v_alpha;
frag_color = sampled;
}

View File

@@ -4,14 +4,17 @@
uniform FrameInfo {
mat4 mvp;
float alpha;
} frame_info;
in vec2 vertices;
in vec2 texture_coords;
out vec2 v_texture_coords;
out float v_alpha;
void main() {
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
v_texture_coords = texture_coords;
v_alpha = frame_info.alpha;
}

View File

@@ -644,6 +644,8 @@ struct Color {
}
constexpr bool IsTransparent() const { return alpha == 0.0; }
constexpr bool IsOpaque() const { return alpha == 1.0; }
};
/**