diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 8e8c52e2f8..c8a09f043f 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -481,6 +481,43 @@ TEST_P(AiksTest, CanRenderDifferentShapesWithSameColorSource) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, CanPictureConvertToImage) { + bool first_frame = true; + auto callback = [&](AiksContext& renderer, RenderTarget& render_target) { + if (first_frame) { + first_frame = false; + ImGui::SetNextWindowSize({480, 100}); + ImGui::SetNextWindowPos({100, 550}); + } + + static int size[2] = {1000, 1000}; + ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize); + ImGui::SliderInt2("Size", size, 0, 1000); + ImGui::End(); + + Canvas recorder_canvas; + Paint paint; + paint.color = Color{0.9568, 0.2627, 0.2118, 1.0}; + recorder_canvas.DrawRect({100.0, 100.0, 600, 600}, paint); + paint.color = Color{0.1294, 0.5882, 0.9529, 1.0}; + recorder_canvas.DrawRect({200.0, 200.0, 600, 600}, paint); + + Canvas canvas; + paint.color = Color::BlackTransparent(); + canvas.DrawPaint(paint); + Picture picture = recorder_canvas.EndRecordingAsPicture(); + auto image = picture.ToImage(renderer, ISize{size[0], size[1]}); + if (image) { + canvas.DrawImage(image, Point(), Paint()); + paint.color = Color{0.1, 0.1, 0.1, 0.2}; + canvas.DrawRect(Rect::MakeSize(ISize{size[0], size[1]}), paint); + } + + return renderer.Render(canvas.EndRecordingAsPicture(), render_target); + }; + ASSERT_TRUE(OpenPlaygroundHere(callback)); +} + TEST_P(AiksTest, BlendModeShouldCoverWholeScreen) { Canvas canvas; Paint paint; diff --git a/engine/src/flutter/impeller/aiks/picture.cc b/engine/src/flutter/impeller/aiks/picture.cc index 79230c75f0..7b64686a17 100644 --- a/engine/src/flutter/impeller/aiks/picture.cc +++ b/engine/src/flutter/impeller/aiks/picture.cc @@ -20,36 +20,56 @@ std::optional Picture::Snapshot(AiksContext& context) { return std::nullopt; } - const auto translate = Matrix::MakeTranslation(-coverage->origin); + const auto translate = Matrix::MakeTranslation(-coverage.value().origin); + auto texture = + RenderToTexture(context, ISize(coverage.value().size), translate); + return impeller::Snapshot{ + .texture = std::move(texture), + .transform = Matrix::MakeTranslation(coverage.value().origin)}; +} + +std::shared_ptr Picture::ToImage(AiksContext& context, ISize size) { + if (size.IsEmpty()) { + return nullptr; + } + auto texture = RenderToTexture(context, size); + return texture ? std::make_shared(texture) : nullptr; +} + +std::shared_ptr Picture::RenderToTexture( + AiksContext& context, + ISize size, + std::optional translate) { + FML_DCHECK(!size.IsEmpty()); + pass->IterateAllEntities([&translate](auto& entity) -> bool { - entity.SetTransformation(translate * entity.GetTransformation()); + auto matrix = translate.has_value() + ? translate.value() * entity.GetTransformation() + : entity.GetTransformation(); + entity.SetTransformation(matrix); 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)); + auto target = RenderTarget::CreateOffscreen(*context.GetContext(), size); if (!target.IsValid()) { VALIDATION_LOG << "Could not create valid RenderTarget."; - return std::nullopt; + return nullptr; } if (!context.Render(*this, target)) { VALIDATION_LOG << "Could not render Picture to Texture."; - return std::nullopt; + return nullptr; } auto texture = target.GetRenderTargetTexture(); if (!texture) { VALIDATION_LOG << "RenderTarget has no target texture."; - return std::nullopt; + return nullptr; } - return impeller::Snapshot{ - .texture = std::move(texture), - .transform = translate.MakeTranslation(coverage->origin)}; -}; + return texture; +} } // namespace impeller diff --git a/engine/src/flutter/impeller/aiks/picture.h b/engine/src/flutter/impeller/aiks/picture.h index b0270e1da1..7224af3a7b 100644 --- a/engine/src/flutter/impeller/aiks/picture.h +++ b/engine/src/flutter/impeller/aiks/picture.h @@ -6,6 +6,7 @@ #include #include +#include #include "flutter/fml/macros.h" #include "impeller/aiks/aiks_context.h" @@ -19,6 +20,14 @@ struct Picture { std::unique_ptr pass; std::optional Snapshot(AiksContext& context); + + std::shared_ptr ToImage(AiksContext& context, ISize size); + + private: + std::shared_ptr RenderToTexture( + AiksContext& context, + ISize size, + std::optional translate = std::nullopt); }; } // namespace impeller