[Impeller Scene] Add ColorSourceContents for drawing a node (flutter/engine#38485)

This commit is contained in:
Brandon DeRosier
2022-12-28 20:57:14 -08:00
committed by GitHub
parent 2c98047fe2
commit 52788a1e67
20 changed files with 258 additions and 27 deletions

View File

@@ -1259,6 +1259,8 @@ ORIGIN: ../../../flutter/impeller/entity/contents/rrect_shadow_contents.cc + ../
ORIGIN: ../../../flutter/impeller/entity/contents/rrect_shadow_contents.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/runtime_effect_contents.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/runtime_effect_contents.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/scene_contents.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/scene_contents.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/solid_color_contents.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/solid_color_contents.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/contents/sweep_gradient_contents.cc + ../../../flutter/LICENSE
@@ -3716,6 +3718,8 @@ FILE: ../../../flutter/impeller/entity/contents/rrect_shadow_contents.cc
FILE: ../../../flutter/impeller/entity/contents/rrect_shadow_contents.h
FILE: ../../../flutter/impeller/entity/contents/runtime_effect_contents.cc
FILE: ../../../flutter/impeller/entity/contents/runtime_effect_contents.h
FILE: ../../../flutter/impeller/entity/contents/scene_contents.cc
FILE: ../../../flutter/impeller/entity/contents/scene_contents.h
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.cc
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.h
FILE: ../../../flutter/impeller/entity/contents/sweep_gradient_contents.cc

View File

@@ -5,6 +5,7 @@
#include <array>
#include <cmath>
#include <iostream>
#include <memory>
#include <tuple>
#include <utility>
@@ -12,7 +13,9 @@
#include "impeller/aiks/aiks_playground.h"
#include "impeller/aiks/canvas.h"
#include "impeller/aiks/image.h"
#include "impeller/entity/contents/color_source_contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
#include "impeller/entity/contents/scene_contents.h"
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/geometry_unittests.h"
@@ -21,6 +24,8 @@
#include "impeller/playground/widgets.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/snapshot.h"
#include "impeller/scene/material.h"
#include "impeller/scene/node.h"
#include "impeller/typographer/backends/skia/text_frame_skia.h"
#include "impeller/typographer/backends/skia/text_render_context_skia.h"
#include "third_party/skia/include/core/SkData.h"
@@ -1700,5 +1705,53 @@ TEST_P(AiksTest, SaveLayerFiltersScaleWithTransform) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, SceneColorSource) {
// Load up the scene.
auto mapping =
flutter::testing::OpenFixtureAsMapping("flutter_logo.glb.ipscene");
ASSERT_NE(mapping, nullptr);
std::shared_ptr<scene::Node> gltf_scene = scene::Node::MakeFromFlatbuffer(
*mapping, *GetContext()->GetResourceAllocator());
ASSERT_NE(gltf_scene, nullptr);
// Assign a material (temporary stopgap).
std::shared_ptr<scene::UnlitMaterial> material = scene::Material::MakeUnlit();
auto color_baked = CreateTextureForFixture("flutter_logo_baked.png");
ASSERT_NE(color_baked, nullptr);
material->SetColorTexture(std::move(color_baked));
material->SetVertexColorWeight(0);
ASSERT_EQ(gltf_scene->GetChildren().size(), 1u);
ASSERT_EQ(gltf_scene->GetChildren()[0]->GetMesh().GetPrimitives().size(), 1u);
gltf_scene->GetChildren()[0]->GetMesh().GetPrimitives()[0].material =
std::move(material);
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
Paint paint;
paint.color_source_type = Paint::ColorSourceType::kScene;
paint.color_source = [this, gltf_scene]() {
Scalar angle = GetSecondsElapsed();
Scalar distance = 2;
auto camera_position =
Vector3(distance * std::sin(angle), 2, -distance * std::cos(angle));
auto contents = std::make_shared<SceneContents>();
contents->SetNode(gltf_scene);
contents->SetCameraTransform(
Matrix::MakePerspective(Degrees(45), GetWindowSize(), 0.1, 1000) *
Matrix::MakeLookAt(camera_position, {0, 0, 0}, {0, 1, 0}));
return contents;
};
Canvas canvas;
canvas.Scale(GetContentScale());
canvas.DrawPaint(paint);
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
};
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
} // namespace testing
} // namespace impeller

View File

@@ -44,6 +44,7 @@ struct Paint {
kConicalGradient,
kSweepGradient,
kRuntimeEffect,
kScene,
};
struct MaskBlurDescriptor {

View File

@@ -27,6 +27,7 @@
#include "impeller/entity/contents/linear_gradient_contents.h"
#include "impeller/entity/contents/radial_gradient_contents.h"
#include "impeller/entity/contents/runtime_effect_contents.h"
#include "impeller/entity/contents/scene_contents.h"
#include "impeller/entity/contents/sweep_gradient_contents.h"
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/entity/entity.h"
@@ -500,6 +501,20 @@ void DisplayListDispatcher::setColorSource(
};
return;
}
case Paint::ColorSourceType::kScene: {
// const flutter::DlSceneColorSource* scene_color_source =
// source->asScene(); std::shared_ptr<scene::Node> scene_node =
// scene_color_source->node(); Matrix camera_transform =
// scene_color_node->camera_transform();
paint_.color_source = [/*scene_node, camera_transform*/]() {
auto contents = std::make_shared<SceneContents>();
// contents->SetNode(scene_node);
// contents->SetCameraTransform(camera_transform);
return contents;
};
}
return;
case Paint::ColorSourceType::kConicalGradient:
UNIMPLEMENTED;
break;

View File

@@ -134,6 +134,8 @@ impeller_component("entity") {
"contents/rrect_shadow_contents.h",
"contents/runtime_effect_contents.cc",
"contents/runtime_effect_contents.h",
"contents/scene_contents.cc",
"contents/scene_contents.h",
"contents/solid_color_contents.cc",
"contents/solid_color_contents.h",
"contents/sweep_gradient_contents.cc",
@@ -164,6 +166,7 @@ impeller_component("entity") {
"../archivist",
"../image",
"../renderer",
"../scene",
"../typographer",
]

View File

@@ -13,11 +13,11 @@ ColorSourceContents::ColorSourceContents() = default;
ColorSourceContents::~ColorSourceContents() = default;
void ColorSourceContents::SetGeometry(std::unique_ptr<Geometry> geometry) {
void ColorSourceContents::SetGeometry(std::shared_ptr<Geometry> geometry) {
geometry_ = std::move(geometry);
}
const std::unique_ptr<Geometry>& ColorSourceContents::GetGeometry() const {
const std::shared_ptr<Geometry>& ColorSourceContents::GetGeometry() const {
return geometry_;
}

View File

@@ -18,7 +18,7 @@ class ColorSourceContents : public Contents {
~ColorSourceContents() override;
void SetGeometry(std::unique_ptr<Geometry> geometry);
void SetGeometry(std::shared_ptr<Geometry> geometry);
void SetMatrix(Matrix matrix);
@@ -32,14 +32,14 @@ class ColorSourceContents : public Contents {
const std::optional<Rect>& stencil_coverage) const override;
protected:
const std::unique_ptr<Geometry>& GetGeometry() const;
const std::shared_ptr<Geometry>& GetGeometry() const;
const Matrix& GetInverseMatrix() const;
Scalar GetAlpha() const;
private:
std::unique_ptr<Geometry> geometry_;
std::shared_ptr<Geometry> geometry_;
Matrix inverse_matrix_;
Scalar alpha_ = 1.0;

View File

@@ -4,6 +4,7 @@
#include "impeller/entity/contents/content_context.h"
#include <memory>
#include <sstream>
#include "impeller/entity/entity.h"
@@ -146,7 +147,8 @@ static std::unique_ptr<PipelineT> CreateDefaultPipeline(
ContentContext::ContentContext(std::shared_ptr<Context> context)
: context_(std::move(context)),
tessellator_(std::make_shared<Tessellator>()),
glyph_atlas_context_(std::make_shared<GlyphAtlasContext>()) {
glyph_atlas_context_(std::make_shared<GlyphAtlasContext>()),
scene_context_(std::make_shared<scene::SceneContext>(context_)) {
if (!context_ || !context_->IsValid()) {
return;
}
@@ -298,6 +300,10 @@ std::shared_ptr<Texture> ContentContext::MakeSubpass(
return subpass_texture;
}
std::shared_ptr<scene::SceneContext> ContentContext::GetSceneContext() const {
return scene_context_;
}
std::shared_ptr<Tessellator> ContentContext::GetTessellator() const {
return tessellator_;
}

View File

@@ -8,8 +8,8 @@
#include <unordered_map>
#include "flutter/fml/hash_combine.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/macros.h"
#include "fml/logging.h"
#include "impeller/base/validation.h"
#include "impeller/entity/advanced_blend.vert.h"
#include "impeller/entity/advanced_blend_color.frag.h"
@@ -65,11 +65,13 @@
#include "impeller/entity/yuv_to_rgb_filter.vert.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/pipeline.h"
#include "impeller/scene/scene_context.h"
#include "impeller/entity/position.vert.h"
#include "impeller/entity/position_color.vert.h"
#include "impeller/entity/position_uv.vert.h"
#include "impeller/scene/scene_context.h"
#include "impeller/typographer/glyph_atlas.h"
#include "impeller/entity/linear_gradient_ssbo_fill.frag.h"
@@ -216,6 +218,8 @@ class ContentContext {
bool IsValid() const;
std::shared_ptr<scene::SceneContext> GetSceneContext() const;
std::shared_ptr<Tessellator> GetTessellator() const;
std::shared_ptr<Pipeline<PipelineDescriptor>> GetLinearGradientFillPipeline(
@@ -522,6 +526,7 @@ class ContentContext {
bool is_valid_ = false;
std::shared_ptr<Tessellator> tessellator_;
std::shared_ptr<GlyphAtlasContext> glyph_atlas_context_;
std::shared_ptr<scene::SceneContext> scene_context_;
FML_DISALLOW_COPY_AND_ASSIGN(ContentContext);
};

View File

@@ -0,0 +1,77 @@
// 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/entity/contents/scene_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/tiled_texture_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/formats.h"
#include "impeller/scene/camera.h"
#include "impeller/scene/scene.h"
namespace impeller {
SceneContents::SceneContents() = default;
SceneContents::~SceneContents() = default;
void SceneContents::SetCameraTransform(Matrix matrix) {
camera_transform_ = matrix;
}
void SceneContents::SetNode(std::shared_ptr<scene::Node> node) {
node_ = std::move(node);
}
bool SceneContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
if (!node_) {
return true;
}
auto coverage = GetCoverage(entity);
if (!coverage.has_value()) {
return true;
}
// This happens for CoverGeometry (DrawPaint). In this situation,
// Draw the scene to the full layer.
if (coverage.value().IsMaximum()) {
coverage = Rect::MakeSize(pass.GetRenderTargetSize());
}
RenderTarget subpass_target = RenderTarget::CreateOffscreenMSAA(
*renderer.GetContext(), // context
ISize(coverage.value().size), // size
"SceneContents", // label
StorageMode::kDeviceTransient, // color_storage_mode
StorageMode::kDevicePrivate, // color_resolve_storage_mode
LoadAction::kClear, // color_load_action
StoreAction::kMultisampleResolve, // color_store_action
StorageMode::kDeviceTransient, // stencil_storage_mode
LoadAction::kDontCare, // stencil_load_action
StoreAction::kDontCare // stencil_store_action
);
if (!subpass_target.IsValid()) {
return false;
}
scene::Scene scene(renderer.GetSceneContext());
scene.GetRoot().AddChild(node_);
if (!scene.Render(subpass_target, camera_transform_)) {
return false;
}
// Render the texture to the pass.
TiledTextureContents contents;
contents.SetGeometry(GetGeometry());
contents.SetTexture(subpass_target.GetRenderTargetTexture());
return contents.Render(renderer, entity, pass);
}
} // namespace impeller

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 <memory>
#include "impeller/entity/contents/color_source_contents.h"
#include "impeller/geometry/matrix.h"
#include "impeller/geometry/rect.h"
#include "impeller/scene/node.h"
namespace impeller {
class SceneContents final : public ColorSourceContents {
public:
SceneContents();
~SceneContents() override;
void SetCameraTransform(Matrix matrix);
void SetNode(std::shared_ptr<scene::Node> node);
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;
private:
Matrix camera_transform_;
std::shared_ptr<scene::Node> node_;
};
} // namespace impeller

View File

@@ -74,10 +74,10 @@ struct TRect {
}
constexpr static TRect MakeMaximum() {
return TRect::MakeLTRB(-std::numeric_limits<Scalar>::infinity(),
-std::numeric_limits<Scalar>::infinity(),
std::numeric_limits<Scalar>::infinity(),
std::numeric_limits<Scalar>::infinity());
return TRect::MakeLTRB(-std::numeric_limits<Type>::infinity(),
-std::numeric_limits<Type>::infinity(),
std::numeric_limits<Type>::infinity(),
std::numeric_limits<Type>::infinity());
}
template <class U>
@@ -122,6 +122,8 @@ struct TRect {
constexpr bool IsEmpty() const { return size.IsEmpty(); }
constexpr bool IsMaximum() const { return *this == MakeMaximum(); }
constexpr auto GetLeft() const {
return std::min(origin.x, origin.x + size.width);
}

View File

@@ -15,17 +15,23 @@
namespace impeller {
namespace scene {
Scene::Scene(std::shared_ptr<Context> context)
: scene_context_(std::make_unique<SceneContext>(std::move(context))) {
Scene::Scene(std::shared_ptr<SceneContext> scene_context)
: scene_context_(std::move(scene_context)) {
root_.is_root_ = true;
};
Scene::~Scene() {
for (auto& child : GetRoot().GetChildren()) {
child->parent_ = nullptr;
}
}
Node& Scene::GetRoot() {
return root_;
}
bool Scene::Render(const RenderTarget& render_target,
const Camera& camera) const {
const Matrix& camera_transform) const {
// Collect the render commands from the scene.
SceneEncoder encoder;
if (!root_.Render(encoder, Matrix())) {
@@ -36,7 +42,8 @@ bool Scene::Render(const RenderTarget& render_target,
// Encode the commands.
std::shared_ptr<CommandBuffer> command_buffer =
encoder.BuildSceneCommandBuffer(*scene_context_, camera, render_target);
encoder.BuildSceneCommandBuffer(*scene_context_, camera_transform,
render_target);
// TODO(bdero): Do post processing.
@@ -48,5 +55,11 @@ bool Scene::Render(const RenderTarget& render_target,
return true;
}
bool Scene::Render(const RenderTarget& render_target,
const Camera& camera) const {
return Render(render_target,
camera.GetTransform(render_target.GetRenderTargetSize()));
}
} // namespace scene
} // namespace impeller

View File

@@ -20,14 +20,20 @@ namespace scene {
class Scene {
public:
Scene() = delete;
explicit Scene(std::shared_ptr<Context> context);
explicit Scene(std::shared_ptr<SceneContext> scene_context);
~Scene();
Node& GetRoot();
bool Render(const RenderTarget& render_target,
const Matrix& camera_transform) const;
bool Render(const RenderTarget& render_target, const Camera& camera) const;
private:
std::unique_ptr<SceneContext> scene_context_;
std::shared_ptr<SceneContext> scene_context_;
Node root_;
FML_DISALLOW_COPY_AND_ASSIGN(Scene);

View File

@@ -46,7 +46,7 @@ static void EncodeCommand(const SceneContext& scene_context,
std::shared_ptr<CommandBuffer> SceneEncoder::BuildSceneCommandBuffer(
const SceneContext& scene_context,
const Camera& camera,
const Matrix& camera_transform,
RenderTarget render_target) const {
{
TextureDescriptor ds_texture;
@@ -91,9 +91,7 @@ std::shared_ptr<CommandBuffer> SceneEncoder::BuildSceneCommandBuffer(
}
for (auto& command : commands_) {
Matrix view_transform =
camera.GetTransform(render_pass->GetRenderTargetSize());
EncodeCommand(scene_context, view_transform, *render_pass, command);
EncodeCommand(scene_context, camera_transform, *render_pass, command);
}
if (!render_pass->EncodeCommands()) {

View File

@@ -35,7 +35,7 @@ class SceneEncoder {
std::shared_ptr<CommandBuffer> BuildSceneCommandBuffer(
const SceneContext& scene_context,
const Camera& camera,
const Matrix& camera_transform,
RenderTarget render_target) const;
std::vector<SceneCommand> commands_;

View File

@@ -33,9 +33,11 @@ using SceneTest = PlaygroundTest;
INSTANTIATE_PLAYGROUND_SUITE(SceneTest);
TEST_P(SceneTest, CuboidUnlit) {
auto scene_context = std::make_shared<SceneContext>(GetContext());
Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
auto allocator = GetContext()->GetResourceAllocator();
auto scene = Scene(GetContext());
auto scene = Scene(scene_context);
{
Mesh mesh;
@@ -87,7 +89,8 @@ TEST_P(SceneTest, FlutterLogo) {
gltf_scene->GetChildren()[0]->GetMesh().GetPrimitives()[0].material =
material;
auto scene = Scene(GetContext());
auto scene_context = std::make_shared<SceneContext>(GetContext());
auto scene = Scene(scene_context);
scene.GetRoot().AddChild(std::move(gltf_scene));
scene.GetRoot().SetLocalTransform(Matrix::MakeScale({3, 3, 3}));
@@ -121,7 +124,8 @@ TEST_P(SceneTest, TwoTriangles) {
Node::MakeFromFlatbuffer(*mapping, *allocator);
ASSERT_NE(gltf_scene, nullptr);
auto scene = Scene(GetContext());
auto scene_context = std::make_shared<SceneContext>(GetContext());
auto scene = Scene(scene_context);
scene.GetRoot().AddChild(std::move(gltf_scene));
Renderer::RenderCallback callback = [&](RenderTarget& render_target) {

View File

@@ -5,12 +5,13 @@
#include "flutter/shell/platform/android/android_surface_gl_impeller.h"
#include "flutter/fml/logging.h"
#include "flutter/impeller/entity/gles/entity_shaders_gles.h"
#include "flutter/impeller/renderer/backend/gles/context_gles.h"
#include "flutter/impeller/renderer/backend/gles/proc_table_gles.h"
#include "flutter/impeller/toolkit/egl/context.h"
#include "flutter/impeller/toolkit/egl/surface.h"
#include "flutter/shell/gpu/gpu_surface_gl_impeller.h"
#include "impeller/entity/gles/entity_shaders_gles.h"
#include "impeller/scene/shaders/gles/scene_shaders_gles.h"
namespace flutter {
@@ -59,6 +60,8 @@ static std::shared_ptr<impeller::Context> CreateImpellerContext(
std::make_shared<fml::NonOwnedMapping>(
impeller_entity_shaders_gles_data,
impeller_entity_shaders_gles_length),
std::make_shared<fml::NonOwnedMapping>(
impeller_scene_shaders_gles_data, impeller_scene_shaders_gles_length),
};
auto context =

View File

@@ -15,6 +15,7 @@
#include "flutter/vulkan/vulkan_native_surface_android.h"
#include "impeller/entity/vk/entity_shaders_vk.h"
#include "impeller/entity/vk/modern_shaders_vk.h"
#include "impeller/scene/shaders/vk/scene_shaders_vk.h"
namespace flutter {
@@ -24,6 +25,8 @@ std::shared_ptr<impeller::Context> CreateImpellerContext(
std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
impeller_entity_shaders_vk_length),
std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_vk_data,
impeller_scene_shaders_vk_length),
std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_vk_data,
impeller_modern_shaders_vk_length),
};

View File

@@ -6,11 +6,12 @@
#include "flutter/common/graphics/persistent_cache.h"
#include "flutter/fml/logging.h"
#include "flutter/impeller/entity/mtl/entity_shaders.h"
#include "flutter/impeller/renderer/backend/metal/context_mtl.h"
#include "flutter/shell/common/context_options.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#include "impeller/entity/mtl/entity_shaders.h"
#include "impeller/entity/mtl/modern_shaders.h"
#include "impeller/scene/shaders/mtl/scene_shaders.h"
FLUTTER_ASSERT_ARC
@@ -18,6 +19,8 @@ static std::shared_ptr<impeller::ContextMTL> CreateImpellerContext() {
std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_data,
impeller_entity_shaders_length),
std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_data,
impeller_scene_shaders_length),
std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_data,
impeller_modern_shaders_length),
};