Skip to content

Commit

Permalink
Setup a paint pass delgate.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent d00efdd commit ead8058
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 15 deletions.
2 changes: 2 additions & 0 deletions impeller/aiks/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 4 additions & 1 deletion impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion impeller/aiks/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion impeller/aiks/paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Paint {
kStroke,
};

Color color;
Color color = Color::Black();
Scalar stroke_width = 0.0;
Style style = Style::kFill;

Expand Down
35 changes: 35 additions & 0 deletions impeller/aiks/paint_pass_delegate.cc
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions impeller/aiks/paint_pass_delegate.h
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions impeller/entity/contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions impeller/entity/contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class TextureContents final : public Contents {

void SetSourceRect(const IRect& source_rect);

void SetOpacity(Scalar opacity);

const IRect& GetSourceRect() const;

// |Contents|
Expand All @@ -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);
};
Expand Down
16 changes: 9 additions & 7 deletions impeller/entity/entity_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions impeller/entity/entity_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/entity_pass_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/entity_pass_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions impeller/entity/shaders/texture_fill.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions impeller/entity/shaders/texture_fill.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions impeller/geometry/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ struct Color {
}

constexpr bool IsTransparent() const { return alpha == 0.0; }

constexpr bool IsOpaque() const { return alpha == 1.0; }
};

/**
Expand Down

0 comments on commit ead8058

Please sign in to comment.