From 92064621b787aa1c95f74046bc836831ba064068 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 6 Jun 2023 17:02:03 -0700 Subject: [PATCH] Convert pixel formats for animated PNG frames using the source blend mode (flutter/engine#42581) The destination buffer format may not exactly match the source format. In particular, the alpha type of the source may need to be converted to the alpha type of the destination. Fixes https://github.com/flutter/flutter/issues/128102 --- .../lib/ui/fixtures/alpha_animated.apng | Bin 0 -> 367 bytes .../lib/ui/painting/image_generator_apng.cc | 79 ++++++++++-------- .../src/flutter/testing/dart/codec_test.dart | 18 ++++ 3 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 engine/src/flutter/lib/ui/fixtures/alpha_animated.apng diff --git a/engine/src/flutter/lib/ui/fixtures/alpha_animated.apng b/engine/src/flutter/lib/ui/fixtures/alpha_animated.apng new file mode 100644 index 0000000000000000000000000000000000000000..277987993f6fc74d2a92f712b859baf1787e4bd3 GIT binary patch literal 367 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4rT@h26vm(GzJC+j>O~;9|i^nCI$uuhR?l| z3m6y}I0Jk_T$z}d{xdNAXJAk&_Aq8(U|=o@@(X4#p4on`i;sbUK`ITR52OujD2M^Z zj0{{140Q+Y*fB6L*m}A+hGg7(d)85qfq{oz%ZSu(?w~bhl&txift16 zGb44uUHqqPk*s3`xgUzbwlRRMd%D@|6$1lxHvU8DJQi^ Y!AQ^2@ZVppA0Rh)y85}Sb4q9e0PhN2LI3~& literal 0 HcmV?d00001 diff --git a/engine/src/flutter/lib/ui/painting/image_generator_apng.cc b/engine/src/flutter/lib/ui/painting/image_generator_apng.cc index 1a67c6482c..5179461d1b 100644 --- a/engine/src/flutter/lib/ui/painting/image_generator_apng.cc +++ b/engine/src/flutter/lib/ui/painting/image_generator_apng.cc @@ -156,51 +156,58 @@ bool APNGImageGenerator::GetPixels(const SkImageInfo& info, FML_DCHECK(frame_info.bytesPerPixel() == sizeof(Pixel)); - for (int y = 0; y < frame_info.height(); y++) { - auto src_row = frame.pixels.data() + y * frame_row_bytes; - auto dst_row = static_cast(pixels) + - (y + frame.y_offset) * row_bytes + - frame.x_offset * frame_info.bytesPerPixel(); + bool result = true; - switch (frame.frame_info->blend_mode) { - case SkCodecAnimation::Blend::kSrcOver: { - for (int x = 0; x < frame_info.width(); x++) { - auto x_offset_bytes = x * frame_info.bytesPerPixel(); + if (frame.frame_info->blend_mode == SkCodecAnimation::Blend::kSrc) { + SkPixmap src_pixmap(frame_info, frame.pixels.data(), frame_row_bytes); + uint8_t* dst_pixels = static_cast(pixels) + + frame.y_offset * row_bytes + + frame.x_offset * frame_info.bytesPerPixel(); + result = src_pixmap.readPixels(info, dst_pixels, row_bytes); + if (!result) { + FML_DLOG(ERROR) << "Failed to copy pixels at index " << image_index + << " (frame index: " << frame_index << ") of APNG."; + } + } else if (frame.frame_info->blend_mode == + SkCodecAnimation::Blend::kSrcOver) { + for (int y = 0; y < frame_info.height(); y++) { + auto src_row = frame.pixels.data() + y * frame_row_bytes; + auto dst_row = static_cast(pixels) + + (y + frame.y_offset) * row_bytes + + frame.x_offset * frame_info.bytesPerPixel(); - Pixel src = *reinterpret_cast(src_row + x_offset_bytes); - Pixel* dst_p = reinterpret_cast(dst_row + x_offset_bytes); - Pixel dst = *dst_p; + for (int x = 0; x < frame_info.width(); x++) { + auto x_offset_bytes = x * frame_info.bytesPerPixel(); - // Ensure both colors are premultiplied for the blending operation. - if (info.alphaType() == kUnpremul_SkAlphaType) { - dst.Premultiply(); - } - if (frame_info.alphaType() == kUnpremul_SkAlphaType) { - src.Premultiply(); - } + Pixel src = *reinterpret_cast(src_row + x_offset_bytes); + Pixel* dst_p = reinterpret_cast(dst_row + x_offset_bytes); + Pixel dst = *dst_p; - for (int i = 0; i < 4; i++) { - dst.channel[i] = src.channel[i] + - dst.channel[i] * (0xFF - src.GetAlpha()) / 0xFF; - } - - // The final color is premultiplied. Unpremultiply to match the - // backdrop surface if necessary. - if (info.alphaType() == kUnpremul_SkAlphaType) { - dst.Unpremultiply(); - } - - *dst_p = dst; + // Ensure both colors are premultiplied for the blending operation. + if (info.alphaType() == kUnpremul_SkAlphaType) { + dst.Premultiply(); } - break; + if (frame_info.alphaType() == kUnpremul_SkAlphaType) { + src.Premultiply(); + } + + for (int i = 0; i < 4; i++) { + dst.channel[i] = + src.channel[i] + dst.channel[i] * (0xFF - src.GetAlpha()) / 0xFF; + } + + // The final color is premultiplied. Unpremultiply to match the + // backdrop surface if necessary. + if (info.alphaType() == kUnpremul_SkAlphaType) { + dst.Unpremultiply(); + } + + *dst_p = dst; } - case SkCodecAnimation::Blend::kSrc: - memcpy(dst_row, src_row, frame_row_bytes); - break; } } - return true; + return result; } std::unique_ptr APNGImageGenerator::MakeFromData( diff --git a/engine/src/flutter/testing/dart/codec_test.dart b/engine/src/flutter/testing/dart/codec_test.dart index db37bb662a..1bf58a26de 100644 --- a/engine/src/flutter/testing/dart/codec_test.dart +++ b/engine/src/flutter/testing/dart/codec_test.dart @@ -202,6 +202,24 @@ void main() { } } }); + + test('Animated apng alpha type handling', () async { + // https://github.com/flutter/engine/pull/42153 + + final Uint8List data = File( + path.join('flutter', 'lib', 'ui', 'fixtures', 'alpha_animated.apng'), + ).readAsBytesSync(); + final ui.Codec codec = await ui.instantiateImageCodec(data); + + // The test image contains two frames of solid red. The first has + // alpha=0.2, and the second has alpha=0.6. + ui.Image image = (await codec.getNextFrame()).image; + ByteData imageData = (await image.toByteData())!; + expect(imageData.getUint32(0), 0x33000033); + image = (await codec.getNextFrame()).image; + imageData = (await image.toByteData())!; + expect(imageData.getUint32(0), 0x99000099); + }); } /// Returns a File handle to a file in the skia/resources directory.