haikuwebkit/LayoutTests/fast/canvas/drawImage-animated-gif-draw...

73 lines
3.1 KiB
HTML
Raw Permalink Normal View History

Drawing an animated image to a canvas via drawImage should draw the first frame https://bugs.webkit.org/show_bug.cgi?id=74779 <rdar://problem/42282454> Reviewed by Said Abou-Hallawa. Source/WebCore: After this patch, when an animated image is drawn into a canvas via drawImage, the actual image that the canvas will receive will be a new Image object with the contents of the first frame of the animation. It also adds an internal setting to keep the previous behaviour (draw the current frame playing) because there are several layout tests that rely on canvas.drawImage() to check that animated images play properly. Test: fast/canvas/drawImage-animated-gif-draws-first-frame-and-no-reset-image.html * html/canvas/CanvasRenderingContext2DBase.cpp: (WebCore::CanvasRenderingContext2DBase::drawImage): * page/Settings.yaml: * testing/InternalSettings.cpp: (WebCore::InternalSettings::Backup::Backup): (WebCore::InternalSettings::Backup::restoreTo): (WebCore::InternalSettings::setAnimatedImageDebugCanvasDrawingEnabled): * testing/InternalSettings.h: * testing/InternalSettings.idl: LayoutTests: Adds a test that checks that when drawing an animated image into a canvas it draws the first frame and that the animation doesn't reset or pause. Also enable the new internal setting setAnimatedImageDebugCanvasDrawingEnabled() for the tests that rely on canvas.drawImage() drawing the current frame instead of the first one. * fast/canvas/drawImage-animated-gif-draws-first-frame-and-no-reset-image-expected.txt: Added. * fast/canvas/drawImage-animated-gif-draws-first-frame-and-no-reset-image.html: Added. The above test checks that when drawing an animated image to a canvas it draws the first frame and that the playing image doesn't get reseted and finish playing. * fast/canvas/resources/animated-red-green-blue-yellow-cyan-black-repeat-1.gif: Added. * fast/images/animated-gif-restored-from-bfcache.html: Enable internals.settings.setAnimatedImageDebugCanvasDrawingEnabled(). * fast/images/animated-image-different-dest-size.html: Ditto. * fast/images/animated-image-loop-count.html: Ditto. * fast/images/animated-image-mp4.html: Ditto. * fast/images/decode-animated-image.html: Ditto. * fast/images/decode-render-animated-image.html: Ditto. * fast/images/ordered-animated-image-frames.html: Ditto. * fast/images/reset-image-animation.html: Ditto. * fast/images/slower-animation-than-decoding-image.html: Ditto. * fast/images/slower-decoding-than-animation-image.html: Ditto. Canonical link: https://commits.webkit.org/214878@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@249162 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2019-08-27 19:50:47 +00:00
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body onload="startTest()">
<!-- This is a hack to force setAutodisplay on WK1 -->
<div style="will-change: transform"></div>
<canvas width="100" height="100">Browser does not support HTML5 Canvas.</canvas>
<script>
description("This test checks that when drawing an animated image to a canvas it draws the first frame and that the animation doesn't reset.");
// The animated image has 6 frames and the test will draw it to a canvas when the image is playing on the 4th frame (yellow)
// Then it will check two things:
// 1. That what has been drawn into the canvas is the first frame of the image (red)
// 2. That the animation itself has continued playing (from the 4th frame to the end) without pausing or going back.
// Note that the animation doesn't loop.
jsTestIsAsync = true;
if (window.internals) {
internals.clearMemoryCache();
internals.settings.setWebkitImageReadyEventEnabled(true);
}
function startTest() {
canvasAlreadyDrawed = false;
currentFrameIndex = 0;
previousFrameIndex = 0;
image = new Image();
image.src = "resources/animated-red-green-blue-yellow-cyan-black-repeat-1.gif"
image.addEventListener("webkitImageFrameReady", imageCheckDrawFrames);
image.decode()
.then(() => {
document.body.appendChild(image);
imageLastFrameIndex = internals.imageFrameCount(image) - 1;
})
.catch((e) => {
testFailed("Exception decoding image: " + e.message);
finishJSTest();
})
}
function drawAndCheckCanvas() {
canvasAlreadyDrawed = true;
canvas = document.getElementsByTagName("canvas")[0];
ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
imgData = ctx.getImageData(50, 25, 1, 1).data;
// The GIF is playing on the 4th frame (yellow), but the canvas should have drawn the first frame (red).
shouldBe("internals.imageFrameIndex(image)", "3");
shouldBe("imgData.toString()", "'255,0,0,255'");
}
function imageCheckDrawFrames() {
currentFrameIndex = internals.imageFrameIndex(image);
// Draw the canvas when the image is playing on the 4th frame (yellow)
if (!canvasAlreadyDrawed && currentFrameIndex == 3) {
drawAndCheckCanvas();
}
if (currentFrameIndex < previousFrameIndex) {
testFailed("The GIF animation should not be resetted.");
finishJSTest();
}
if (canvasAlreadyDrawed && currentFrameIndex == imageLastFrameIndex) {
testPassed("The GIF animation has completed playing and the Canvas has been drawn.");
finishJSTest();
}
previousFrameIndex = currentFrameIndex;
}
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>