Separate save layer calls into a canvas pass.

This commit is contained in:
Chinmay Garde
2021-11-12 14:16:21 -08:00
committed by Dan Field
parent 40992281d5
commit d226108835
14 changed files with 106 additions and 49 deletions

View File

@@ -8,6 +8,8 @@ impeller_component("aiks") {
sources = [
"canvas.cc",
"canvas.h",
"canvas_pass.cc",
"canvas_pass.h",
"image.cc",
"image.h",
"paint.cc",

View File

@@ -12,10 +12,7 @@ namespace impeller {
Canvas::Canvas() {
xformation_stack_.push({});
Paint default_paint;
default_paint.color = Color::White();
paint_stack_.push(default_paint);
passes_.emplace_back(CanvasPass{});
}
Canvas::~Canvas() = default;
@@ -23,7 +20,6 @@ Canvas::~Canvas() = default;
void Canvas::Save() {
FML_DCHECK(xformation_stack_.size() > 0);
xformation_stack_.push(xformation_stack_.top());
paint_stack_.push(paint_stack_.top());
}
bool Canvas::Restore() {
@@ -32,7 +28,6 @@ bool Canvas::Restore() {
return false;
}
xformation_stack_.pop();
paint_stack_.pop();
return true;
}
@@ -69,21 +64,15 @@ void Canvas::RestoreToCount(size_t count) {
}
void Canvas::DrawPath(Path path, Paint paint) {
Color color = paint.color;
color.alpha *= paint_stack_.top().color.alpha;
Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetBackgroundColor(color);
entity.SetContents(std::move(paint.contents));
ops_.emplace_back(std::move(entity));
entity.SetContents(paint.CreateContentsForEntity());
GetCurrentPass().PushEntity(std::move(entity));
}
void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {
Save();
paint_stack_.top() = paint;
}
void Canvas::ClipPath(Path path) {
@@ -91,24 +80,33 @@ void Canvas::ClipPath(Path path) {
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetIsClip(true);
ops_.emplace_back(std::move(entity));
GetCurrentPass().PushEntity(std::move(entity));
}
void Canvas::DrawShadow(Path path, Color color, Scalar elevation) {}
void Canvas::DrawPicture(const Picture& picture) {
for (const auto& entity : picture.entities) {
auto new_entity = entity;
new_entity.SetTransformation(GetCurrentTransformation() *
new_entity.GetTransformation());
ops_.emplace_back(std::move(new_entity));
for (const auto& pass : picture.passes) {
CanvasPass new_pass;
for (const auto& entity : pass.GetPassEntities()) {
auto new_entity = entity;
new_entity.SetTransformation(GetCurrentTransformation() *
entity.GetTransformation());
new_pass.PushEntity(std::move(new_entity));
}
passes_.emplace_back(std::move(new_pass));
}
}
Picture Canvas::EndRecordingAsPicture() {
Picture picture;
picture.entities = std::move(ops_);
picture.passes = std::move(passes_);
return picture;
}
CanvasPass& Canvas::GetCurrentPass() {
FML_DCHECK(!passes_.empty());
return passes_.back();
}
} // namespace impeller

View File

@@ -10,6 +10,7 @@
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/aiks/canvas_pass.h"
#include "impeller/aiks/paint.h"
#include "impeller/aiks/picture.h"
#include "impeller/geometry/matrix.h"
@@ -58,8 +59,9 @@ class Canvas {
private:
std::stack<Matrix> xformation_stack_;
std::stack<Paint> paint_stack_;
std::vector<Entity> ops_;
std::vector<CanvasPass> passes_;
CanvasPass& GetCurrentPass();
FML_DISALLOW_COPY_AND_ASSIGN(Canvas);
};

View File

@@ -0,0 +1,21 @@
// 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/canvas_pass.h"
namespace impeller {
CanvasPass::CanvasPass() = default;
CanvasPass::~CanvasPass() = default;
void CanvasPass::PushEntity(Entity entity) {
ops_.emplace_back(std::move(entity));
}
const std::vector<Entity>& CanvasPass::GetPassEntities() const {
return ops_;
}
} // namespace impeller

View File

@@ -0,0 +1,28 @@
// 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 <vector>
#include "flutter/fml/macros.h"
#include "impeller/entity/entity.h"
namespace impeller {
class CanvasPass {
public:
CanvasPass();
~CanvasPass();
void PushEntity(Entity entity);
const std::vector<Entity>& GetPassEntities() const;
private:
std::vector<Entity> ops_;
};
} // namespace impeller

View File

@@ -6,6 +6,14 @@
namespace impeller {
//
std::shared_ptr<Contents> Paint::CreateContentsForEntity() const {
if (!color.IsTransparent()) {
auto solid_color = std::make_shared<SolidColorContents>();
solid_color->SetColor(color);
return solid_color;
}
return nullptr;
}
} // namespace impeller

View File

@@ -14,7 +14,8 @@ namespace impeller {
struct Paint {
Color color;
Scalar stroke_width = 0.0;
std::shared_ptr<Contents> contents;
std::shared_ptr<Contents> CreateContentsForEntity() const;
};
} // namespace impeller

View File

@@ -8,12 +8,13 @@
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/aiks/canvas_pass.h"
#include "impeller/entity/entity.h"
namespace impeller {
struct Picture {
std::vector<Entity> entities;
std::vector<CanvasPass> passes;
};
} // namespace impeller

View File

@@ -29,10 +29,14 @@ bool PictureRenderer::Render(const Surface& surface,
return false;
}
return entity_renderer_.RenderEntities(surface, //
onscreen_pass, //
picture.entities //
);
for (const auto& pass : picture.passes) {
if (!entity_renderer_.RenderEntities(surface, onscreen_pass,
pass.GetPassEntities())) {
return false;
}
}
return true;
}
} // namespace impeller

View File

@@ -95,6 +95,10 @@ bool LinearGradientContents::Render(const ContentRenderer& renderer,
******* SolidColorContents
******************************************************************************/
SolidColorContents::SolidColorContents() = default;
SolidColorContents::~SolidColorContents() = default;
void SolidColorContents::SetColor(Color color) {
color_ = color;
}
@@ -135,7 +139,7 @@ bool SolidColorContents::Render(const ContentRenderer& renderer,
VS::FrameInfo frame_info;
frame_info.mvp =
Matrix::MakeOrthographic(surface.GetSize()) * entity.GetTransformation();
frame_info.color = entity.GetBackgroundColor();
frame_info.color = color_;
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
cmd.primitive_type = PrimitiveType::kTriangle;

View File

@@ -84,7 +84,7 @@ class SolidStrokeContents final : public Contents {
public:
SolidStrokeContents();
~SolidStrokeContents();
~SolidStrokeContents() override;
void SetColor(Color color);

View File

@@ -18,14 +18,6 @@ void Entity::SetTransformation(const Matrix& transformation) {
transformation_ = transformation;
}
const Color& Entity::GetBackgroundColor() const {
return background_color_;
}
void Entity::SetBackgroundColor(const Color& backgroundColor) {
background_color_ = backgroundColor;
}
const Color& Entity::GetStrokeColor() const {
return stroke_color_;
}

View File

@@ -23,10 +23,6 @@ class Entity {
void SetTransformation(const Matrix& transformation);
const Color& GetBackgroundColor() const;
void SetBackgroundColor(const Color& backgroundColor);
const Color& GetStrokeColor() const;
void SetStrokeColor(const Color& strokeColor);
@@ -49,9 +45,7 @@ class Entity {
private:
Matrix transformation_;
Color background_color_;
std::shared_ptr<Contents> contents_;
Path path_;
Color stroke_color_;
double stroke_size_ = 1.0;

View File

@@ -37,9 +37,11 @@ bool EntityRenderer::RenderEntities(const Surface& surface,
}
for (const auto& entity : entities) {
if (auto contents = entity.GetContents();
!contents->Render(*content_renderer_, entity, surface, onscreen_pass)) {
return false;
if (auto contents = entity.GetContents()) {
if (!contents->Render(*content_renderer_, entity, surface,
onscreen_pass)) {
return false;
}
}
}