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 0000000000..277987993f Binary files /dev/null and b/engine/src/flutter/lib/ui/fixtures/alpha_animated.apng differ 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.