[Impeller] generate mipmaps for toImage. (flutter/engine#55655)

Fixes https://github.com/flutter/flutter/issues/156161

Mips must be eagerly generated for Picture.toImage and friends so that filter quality (mip sampling) works as expected.
This commit is contained in:
Jonah Williams
2024-10-04 12:49:55 -05:00
committed by GitHub
parent a2ac734e10
commit 9d8929af60
4 changed files with 49 additions and 6 deletions

View File

@@ -1368,7 +1368,12 @@ std::shared_ptr<Texture> DisplayListToTexture(
const sk_sp<flutter::DisplayList>& display_list,
ISize size,
AiksContext& context,
bool reset_host_buffer) {
bool reset_host_buffer,
bool generate_mips) {
int mip_count = 1;
if (generate_mips) {
mip_count = size.MipCount();
}
// Do not use the render target cache as the lifecycle of this texture
// will outlive a particular frame.
impeller::RenderTargetAllocator render_target_allocator =
@@ -1379,7 +1384,7 @@ std::shared_ptr<Texture> DisplayListToTexture(
target = render_target_allocator.CreateOffscreenMSAA(
*context.GetContext(), // context
size, // size
/*mip_count=*/1,
/*mip_count=*/mip_count,
"Picture Snapshot MSAA", // label
impeller::RenderTarget::
kDefaultColorAttachmentConfigMSAA // color_attachment_config
@@ -1388,7 +1393,7 @@ std::shared_ptr<Texture> DisplayListToTexture(
target = render_target_allocator.CreateOffscreen(
*context.GetContext(), // context
size, // size
/*mip_count=*/1,
/*mip_count=*/mip_count,
"Picture Snapshot", // label
impeller::RenderTarget::
kDefaultColorAttachmentConfig // color_attachment_config

View File

@@ -370,7 +370,8 @@ std::shared_ptr<Texture> DisplayListToTexture(
const sk_sp<flutter::DisplayList>& display_list,
ISize size,
AiksContext& context,
bool reset_host_buffer = true);
bool reset_host_buffer = true,
bool generate_mips = false);
/// Render the provided display list to the render target.
bool RenderToOnscreen(ContentContext& context, RenderTarget render_target,

View File

@@ -14,7 +14,6 @@
#include "flutter/impeller/geometry/size.h"
#include "flutter/shell/common/snapshot_controller.h"
#include "impeller/entity/contents/runtime_effect_contents.h"
#include "impeller/renderer/render_target.h"
namespace flutter {
@@ -49,7 +48,8 @@ sk_sp<DlImage> DoMakeRasterSnapshot(
return impeller::DlImageImpeller::Make(
impeller::DisplayListToTexture(display_list, render_target_size, *context,
/*reset_host_buffer=*/false),
/*reset_host_buffer=*/false,
/*generate_mips=*/true),
DlImage::OwningContext::kRaster);
}

View File

@@ -114,4 +114,41 @@ void main() {
'ImageFilter.matrix($data, FilterQuality.medium)',
);
});
test('Picture.toImage generates mip maps', () async {
// Draw a grid of red and blue squares. When averaged together via
// mip maps, this should result in a purplish color. If there are no
// mip maps, the original red and blue colors will be preserved regardless
// of scale or number of pixels.
late final Image image;
{
final PictureRecorder recorder = PictureRecorder();
final Canvas canvas = Canvas(recorder);
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
final Color color = (i + j).isEven ? const Color(0xFFFF0000) : const Color(0xFF0000FF);
canvas.drawRect(Rect.fromLTWH(i * 5, j * 5, 5, 5), Paint()..color = color);
}
}
final Picture picture = recorder.endRecording();
image = await picture.toImage(100, 100);
}
final PictureRecorder recorder = PictureRecorder();
final Canvas canvas = Canvas(recorder);
canvas.save();
canvas.scale(0.25, 0.25);
canvas.drawImage(image, Offset.zero, Paint()..filterQuality = FilterQuality.medium);
canvas.restore();
final Picture picture = recorder.endRecording();
final Image resultImage = await picture.toImage(10, 10);
final ByteData data = (await resultImage.toByteData())!;
final Int32List colors = data.buffer.asInt32List();
for (int i = 0; i < colors.length; i++) {
expect(colors[i], isNot(const Color(0xFFFF0000)));
expect(colors[i], isNot(const Color(0xFF0000FF)));
}
});
}