[Impeller] Implement 'Picture::ToImage' (flutter/engine#35624)

This commit is contained in:
ColdPaleLight
2022-08-30 05:52:20 +08:00
committed by GitHub
parent b6699cea07
commit 23d68071ee
3 changed files with 78 additions and 12 deletions

View File

@@ -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;

View File

@@ -20,36 +20,56 @@ std::optional<Snapshot> 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<Image> Picture::ToImage(AiksContext& context, ISize size) {
if (size.IsEmpty()) {
return nullptr;
}
auto texture = RenderToTexture(context, size);
return texture ? std::make_shared<Image>(texture) : nullptr;
}
std::shared_ptr<Texture> Picture::RenderToTexture(
AiksContext& context,
ISize size,
std::optional<const Matrix> 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

View File

@@ -6,6 +6,7 @@
#include <deque>
#include <memory>
#include <optional>
#include "flutter/fml/macros.h"
#include "impeller/aiks/aiks_context.h"
@@ -19,6 +20,14 @@ struct Picture {
std::unique_ptr<EntityPass> pass;
std::optional<Snapshot> Snapshot(AiksContext& context);
std::shared_ptr<Image> ToImage(AiksContext& context, ISize size);
private:
std::shared_ptr<Texture> RenderToTexture(
AiksContext& context,
ISize size,
std::optional<const Matrix> translate = std::nullopt);
};
} // namespace impeller