diff --git a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc index 21d7948b31..ceabd4ff52 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc @@ -1368,7 +1368,12 @@ std::shared_ptr DisplayListToTexture( const sk_sp& 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 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 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 diff --git a/engine/src/flutter/impeller/display_list/dl_dispatcher.h b/engine/src/flutter/impeller/display_list/dl_dispatcher.h index f2b4a8add6..1418229430 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.h +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.h @@ -370,7 +370,8 @@ std::shared_ptr DisplayListToTexture( const sk_sp& 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, diff --git a/engine/src/flutter/shell/common/snapshot_controller_impeller.cc b/engine/src/flutter/shell/common/snapshot_controller_impeller.cc index 4fae09f588..3da0f19619 100644 --- a/engine/src/flutter/shell/common/snapshot_controller_impeller.cc +++ b/engine/src/flutter/shell/common/snapshot_controller_impeller.cc @@ -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 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); } diff --git a/engine/src/flutter/testing/dart/painting_test.dart b/engine/src/flutter/testing/dart/painting_test.dart index 68388a09a4..e965b9a09f 100644 --- a/engine/src/flutter/testing/dart/painting_test.dart +++ b/engine/src/flutter/testing/dart/painting_test.dart @@ -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))); + } + }); }