[Impeller] Add Picture::RenderToImage (flutter/engine#33864)

This commit is contained in:
Brandon DeRosier
2022-06-14 10:07:42 -07:00
committed by GitHub
parent c036d1b939
commit 55b1bdb729
11 changed files with 95 additions and 18 deletions

View File

@@ -549,8 +549,6 @@ FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_in
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h
FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.cc
FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.h
FILE: ../../../flutter/impeller/entity/contents/snapshot.cc
FILE: ../../../flutter/impeller/entity/contents/snapshot.h
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.cc
FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.h
FILE: ../../../flutter/impeller/entity/contents/solid_stroke_contents.cc
@@ -770,6 +768,8 @@ FILE: ../../../flutter/impeller/renderer/shader_library.cc
FILE: ../../../flutter/impeller/renderer/shader_library.h
FILE: ../../../flutter/impeller/renderer/shader_types.cc
FILE: ../../../flutter/impeller/renderer/shader_types.h
FILE: ../../../flutter/impeller/renderer/snapshot.cc
FILE: ../../../flutter/impeller/renderer/snapshot.h
FILE: ../../../flutter/impeller/renderer/surface.cc
FILE: ../../../flutter/impeller/renderer/surface.h
FILE: ../../../flutter/impeller/renderer/texture.cc

View File

@@ -28,6 +28,10 @@ bool AiksContext::IsValid() const {
return is_valid_;
}
std::shared_ptr<Context> AiksContext::GetContext() const {
return context_;
}
bool AiksContext::Render(const Picture& picture, RenderTarget& render_target) {
if (!IsValid()) {
return false;

View File

@@ -24,6 +24,8 @@ class AiksContext {
bool IsValid() const;
std::shared_ptr<Context> GetContext() const;
bool Render(const Picture& picture, RenderTarget& render_target);
private:

View File

@@ -14,6 +14,7 @@
#include "impeller/geometry/geometry_unittests.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/playground/widgets.h"
#include "impeller/renderer/snapshot.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"
@@ -595,6 +596,9 @@ TEST_P(AiksTest, ColorWheel) {
}
};
std::shared_ptr<Image> color_wheel;
Matrix color_wheel_transform;
bool first_frame = true;
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
if (first_frame) {
@@ -621,22 +625,47 @@ TEST_P(AiksTest, ColorWheel) {
}
ImGui::End();
Canvas canvas;
canvas.Scale(GetContentScale());
Paint paint;
// Default blend is kSourceOver.
paint.color = Color::White();
canvas.DrawPaint(paint);
static Point content_scale;
Point new_content_scale = GetContentScale();
if (new_content_scale != content_scale) {
content_scale = new_content_scale;
// Render the color wheel to an image.
Canvas canvas;
canvas.Scale(content_scale);
canvas.Translate(Vector2(500, 400));
canvas.Scale(Vector2(3, 3));
draw_color_wheel(canvas);
auto color_wheel_picture = canvas.EndRecordingAsPicture();
auto snapshot = color_wheel_picture.Snapshot(renderer);
if (!snapshot.has_value() || !snapshot->texture) {
return false;
}
color_wheel = std::make_shared<Image>(snapshot->texture);
color_wheel_transform = snapshot->transform;
}
Canvas canvas;
canvas.DrawPaint({.color = Color::White()});
canvas.Save();
canvas.Transform(color_wheel_transform);
canvas.DrawImage(color_wheel, Point(), Paint());
canvas.Restore();
canvas.Scale(content_scale);
canvas.Translate(Vector2(500, 400));
canvas.Scale(Vector2(3, 3));
draw_color_wheel(canvas);
// Draw 3 circles to a subpass and blend it in.
canvas.SaveLayer({.color = Color::White().WithAlpha(alpha),
.blend_mode = blend_mode_values[current_blend_index]});
{
Paint paint;
paint.blend_mode = Entity::BlendMode::kPlus;
const Scalar x = std::sin(k2Pi / 3);
const Scalar y = -std::cos(k2Pi / 3);

View File

@@ -4,10 +4,51 @@
#include "impeller/aiks/picture.h"
#include <memory>
#include "impeller/base/validation.h"
#include "impeller/entity/entity.h"
#include "impeller/renderer/render_target.h"
#include "impeller/renderer/snapshot.h"
namespace impeller {
//
std::optional<Snapshot> Picture::Snapshot(AiksContext& context) {
auto coverage = pass->GetElementsCoverage();
if (!coverage.has_value() || coverage->IsEmpty()) {
return std::nullopt;
}
const auto translate = Matrix::MakeTranslation(-coverage->origin);
pass->IterateAllEntities([&translate](auto& entity) -> bool {
entity.SetTransformation(translate * entity.GetTransformation());
return true;
});
// This texture isn't host visible, but we might want to add host visible
// features to Image someday.
auto target = RenderTarget::CreateOffscreen(
*context.GetContext(),
ISize(coverage->size.width, coverage->size.height));
if (!target.IsValid()) {
VALIDATION_LOG << "Could not create valid RenderTarget.";
return std::nullopt;
}
if (!context.Render(*this, target)) {
VALIDATION_LOG << "Could not render Picture to Texture.";
return std::nullopt;
}
auto texture = target.GetRenderTargetTexture();
if (!texture) {
VALIDATION_LOG << "RenderTarget has no target texture.";
return std::nullopt;
}
return impeller::Snapshot{
.texture = std::move(texture),
.transform = translate.MakeTranslation(coverage->origin)};
};
} // namespace impeller

View File

@@ -8,6 +8,8 @@
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/aiks/aiks_context.h"
#include "impeller/aiks/image.h"
#include "impeller/entity/entity.h"
#include "impeller/entity/entity_pass.h"
@@ -15,6 +17,8 @@ namespace impeller {
struct Picture {
std::unique_ptr<EntityPass> pass;
std::optional<Snapshot> Snapshot(AiksContext& context);
};
} // namespace impeller

View File

@@ -71,8 +71,6 @@ impeller_component("entity") {
"contents/filters/inputs/texture_filter_input.h",
"contents/linear_gradient_contents.cc",
"contents/linear_gradient_contents.h",
"contents/snapshot.cc",
"contents/snapshot.h",
"contents/solid_color_contents.cc",
"contents/solid_color_contents.h",
"contents/solid_stroke_contents.cc",

View File

@@ -9,8 +9,8 @@
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/snapshot.h"
#include "impeller/geometry/rect.h"
#include "impeller/renderer/snapshot.h"
#include "impeller/renderer/texture.h"
namespace impeller {

View File

@@ -56,6 +56,8 @@ impeller_component("renderer") {
"shader_library.h",
"shader_types.cc",
"shader_types.h",
"snapshot.cc",
"snapshot.h",
"surface.cc",
"surface.h",
"texture.cc",

View File

@@ -2,13 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/entity/contents/snapshot.h"
#include "impeller/renderer/snapshot.h"
#include <optional>
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/texture_contents.h"
namespace impeller {
std::optional<Rect> Snapshot::GetCoverage() const {