Title: [209131] trunk
Revision
209131
Author
[email protected]
Date
2016-11-30 09:10:04 -0800 (Wed, 30 Nov 2016)

Log Message

Some animated image do not animate after reseting their animations
https://bugs.webkit.org/show_bug.cgi?id=165131

Patch by Said Abou-Hallawa <[email protected]> on 2016-11-30
Reviewed by Simon Fraser.
Source/WebCore:

Test: fast/images/reset-image-animation.html

The bug happens because the ImageFrameCache does not reset its cached
frames properly when the BitmapImage rests its animation.

-- We need to ignore caching the new NativeImage of an ImageFrame if the
decoding thread is terminated after asynchronously calling
cacheFrameNativeImageAtIndex() on the main thread through callOnMainThread().
Although the decoding thread was terminated this call is still scheduled
to be executed later on the main thread.

-- We need to open the m_frameRequestQueue before starting a new decoding
thread. Opening m_frameRequestQueue clears the enqueued requests from the
previous decoding thread and allows enqueueing requests for the new thread.

-- We need to clear the ImageFrames which were being decoded at the time
the decoding queue was closing and their new NativeImages were not cached.

* platform/graphics/ImageFrameCache.cpp:
(WebCore::ImageFrameCache::cacheFrameNativeImageAtIndex): Assert the NativeImage
is cached for an ImageFrame which is still being decoded.

(WebCore::ImageFrameCache::startAsyncDecodingQueue): Open the m_frameRequestQueue
before starting the decoding thread. Don't cache the NativeImage of an ImageFrame
unless the decoding thread is still alive and it the same as the one that makes
the asynchronous call to cacheFrameNativeImageAtIndex().

(WebCore::ImageFrameCache::requestFrameAsyncDecodingAtIndex): Don't request an
image frame to be be asynchronously decoded if it is already being decoded.

(WebCore::ImageFrameCache::stopAsyncDecodingQueue): Clear the ImageFrames that
were being decoded when the decoding thread terminates.

* testing/Internals.cpp:
(WebCore::Internals::resetImageAnimation):
* testing/Internals.h:
* testing/Internals.idl:
Expose BitmapImage::resetAnimation() through Internals so tests can force it.

LayoutTests:

Ensure the image animation is played in order after the animation is reset.

* fast/images/reset-image-animation-expected.txt: Added.
* fast/images/reset-image-animation.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (209130 => 209131)


--- trunk/LayoutTests/ChangeLog	2016-11-30 17:03:48 UTC (rev 209130)
+++ trunk/LayoutTests/ChangeLog	2016-11-30 17:10:04 UTC (rev 209131)
@@ -1,3 +1,15 @@
+2016-11-30  Said Abou-Hallawa  <[email protected]>
+
+        Some animated image do not animate after reseting their animations
+        https://bugs.webkit.org/show_bug.cgi?id=165131
+
+        Reviewed by Simon Fraser.
+        
+        Ensure the image animation is played in order after the animation is reset.
+
+        * fast/images/reset-image-animation-expected.txt: Added.
+        * fast/images/reset-image-animation.html: Added.
+
 2016-11-29  Brent Fulgham  <[email protected]>
 
         Use 'childOfType' template when retrieving Shadow DOM elements

Added: trunk/LayoutTests/fast/images/reset-image-animation-expected.txt (0 => 209131)


--- trunk/LayoutTests/fast/images/reset-image-animation-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/images/reset-image-animation-expected.txt	2016-11-30 17:10:04 UTC (rev 209131)
@@ -0,0 +1,11 @@
+Ensure the image animation is played in order after the animation is reset.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS internals.imageFrameIndex(image) is 1
+PASS internals.imageFrameIndex(image) is 2
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/images/reset-image-animation.html (0 => 209131)


--- trunk/LayoutTests/fast/images/reset-image-animation.html	                        (rev 0)
+++ trunk/LayoutTests/fast/images/reset-image-animation.html	2016-11-30 17:10:04 UTC (rev 209131)
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <script src=""
+</head>
+<body>
+    <canvas id="canvas"></canvas>
+    <script>
+        description("Ensure the image animation is played in order after the animation is reset.");
+        jsTestIsAsync = true;
+
+        internals.clearMemoryCache();
+
+        var image = new Image;
+        image._onload_ = imageLoaded;
+        image.src = ""
+        
+        function imageLoaded()
+        {
+            if (!window.internals)
+                return;
+            internals.setImageFrameDecodingDuration(image, 0.040);
+            drawImage();
+            drawLoop();
+        }
+
+        function drawImage()
+        {
+            if (drawImage.count == undefined)
+                drawImage.count = 0;
+            var canvas = document.getElementById("canvas");
+            var ctx = canvas.getContext("2d");
+            ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
+            return ++drawImage.count;
+        }
+                
+        function drawLoop()
+        {
+            setTimeout(function() {
+                switch (drawImage()) {
+                case 2:
+                    shouldBe("internals.imageFrameIndex(image)", "1");
+                    internals.resetImageAnimation(image);
+                    drawLoop();
+                    break;
+
+                case 5:
+                    // The animation was reset at drawCount = 2. Three more
+                    // drawings should make current_frame = 2.
+                    shouldBe("internals.imageFrameIndex(image)", "2");
+                    finishJSTest();
+                    break;
+                    
+                default:
+                    drawLoop();
+                }
+            }, 50);
+        }
+    </script>
+    <script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (209130 => 209131)


--- trunk/Source/WebCore/ChangeLog	2016-11-30 17:03:48 UTC (rev 209130)
+++ trunk/Source/WebCore/ChangeLog	2016-11-30 17:10:04 UTC (rev 209131)
@@ -1,3 +1,49 @@
+2016-11-30  Said Abou-Hallawa  <[email protected]>
+
+        Some animated image do not animate after reseting their animations
+        https://bugs.webkit.org/show_bug.cgi?id=165131
+
+        Reviewed by Simon Fraser.
+
+        Test: fast/images/reset-image-animation.html
+
+        The bug happens because the ImageFrameCache does not reset its cached
+        frames properly when the BitmapImage rests its animation.
+
+        -- We need to ignore caching the new NativeImage of an ImageFrame if the
+        decoding thread is terminated after asynchronously calling  
+        cacheFrameNativeImageAtIndex() on the main thread through callOnMainThread().
+        Although the decoding thread was terminated this call is still scheduled
+        to be executed later on the main thread.
+
+        -- We need to open the m_frameRequestQueue before starting a new decoding
+        thread. Opening m_frameRequestQueue clears the enqueued requests from the 
+        previous decoding thread and allows enqueueing requests for the new thread.
+
+        -- We need to clear the ImageFrames which were being decoded at the time
+        the decoding queue was closing and their new NativeImages were not cached.
+
+        * platform/graphics/ImageFrameCache.cpp:
+        (WebCore::ImageFrameCache::cacheFrameNativeImageAtIndex): Assert the NativeImage
+        is cached for an ImageFrame which is still being decoded.
+
+        (WebCore::ImageFrameCache::startAsyncDecodingQueue): Open the m_frameRequestQueue
+        before starting the decoding thread. Don't cache the NativeImage of an ImageFrame
+        unless the decoding thread is still alive and it the same as the one that makes
+        the asynchronous call to cacheFrameNativeImageAtIndex().
+        
+        (WebCore::ImageFrameCache::requestFrameAsyncDecodingAtIndex): Don't request an
+        image frame to be be asynchronously decoded if it is already being decoded.
+        
+        (WebCore::ImageFrameCache::stopAsyncDecodingQueue): Clear the ImageFrames that
+        were being decoded when the decoding thread terminates.
+        
+        * testing/Internals.cpp:
+        (WebCore::Internals::resetImageAnimation):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+        Expose BitmapImage::resetAnimation() through Internals so tests can force it.
+
 2016-11-29  Brent Fulgham  <[email protected]>
 
         Use 'childOfType' template when retrieving Shadow DOM elements

Modified: trunk/Source/WebCore/platform/graphics/ImageFrameCache.cpp (209130 => 209131)


--- trunk/Source/WebCore/platform/graphics/ImageFrameCache.cpp	2016-11-30 17:03:48 UTC (rev 209130)
+++ trunk/Source/WebCore/platform/graphics/ImageFrameCache.cpp	2016-11-30 17:10:04 UTC (rev 209131)
@@ -242,6 +242,9 @@
     if (!isDecoderAvailable())
         return;
 
+    ASSERT(index < m_frames.size());
+    ASSERT(m_frames[index].isBeingDecoded());
+
     // Clean the old native image and set a new one
     replaceFrameNativeImageAtIndex(WTFMove(nativeImage), index, subsamplingLevel);
 
@@ -263,6 +266,8 @@
     if (hasDecodingQueue() || !isDecoderAvailable())
         return;
 
+    m_frameRequestQueue.open();
+
     Ref<ImageFrameCache> protectedThis = Ref<ImageFrameCache>(*this);
     Ref<WorkQueue> protectedQueue = decodingQueue();
 
@@ -275,9 +280,9 @@
             NativeImagePtr nativeImage = m_decoder->createFrameImageAtIndex(frameRequest.index, frameRequest.subsamplingLevel, DecodingMode::Immediate);
 
             // Update the cached frames on the main thread to avoid updating the MemoryCache from a different thread.
-            callOnMainThread([this, nativeImage, frameRequest] () mutable {
+            callOnMainThread([this, protectedQueue = protectedQueue.copyRef(), nativeImage, frameRequest] () mutable {
                 // The queue may be closed if after we got the frame NativeImage, stopAsyncDecodingQueue() was called
-                if (hasDecodingQueue())
+                if (protectedQueue.ptr() == m_decodingQueue)
                     cacheFrameNativeImageAtIndex(WTFMove(nativeImage), frameRequest.index, frameRequest.subsamplingLevel);
             });
         }
@@ -294,13 +299,19 @@
 
     ASSERT(index < m_frames.size());
     ImageFrame& frame = m_frames[index];
-    
+
+    // We need to coalesce multiple requests for decoding the same ImageFrame while it
+    // is still being decoded. This may happen if the image rectangle is repainted
+    // multiple times while the ImageFrame has not finished decoding.
+    if (frame.isBeingDecoded())
+        return true;
+
     if (subsamplingLevel == SubsamplingLevel::Undefinded)
         subsamplingLevel = frame.subsamplingLevel();
-    
+
     if (frame.hasValidNativeImage(subsamplingLevel))
         return false;
-    
+
     frame.setDecoding(ImageFrame::Decoding::BeingDecoded);
     m_frameRequestQueue.enqueue({ index, subsamplingLevel });
     return true;
@@ -313,6 +324,11 @@
     
     m_frameRequestQueue.close();
     m_decodingQueue = nullptr;
+
+    for (ImageFrame& frame : m_frames) {
+        if (frame.isBeingDecoded())
+            frame.clear();
+    }
 }
 
 const ImageFrame& ImageFrameCache::frameAtIndex(size_t index, SubsamplingLevel subsamplingLevel, ImageFrame::Caching caching)

Modified: trunk/Source/WebCore/testing/Internals.cpp (209130 => 209131)


--- trunk/Source/WebCore/testing/Internals.cpp	2016-11-30 17:03:48 UTC (rev 209130)
+++ trunk/Source/WebCore/testing/Internals.cpp	2016-11-30 17:10:04 UTC (rev 209131)
@@ -662,6 +662,19 @@
     downcast<BitmapImage>(*image).setFrameDecodingDurationForTesting(duration);
 }
 
+void Internals::resetImageAnimation(HTMLImageElement& element)
+{
+    auto* cachedImage = element.cachedImage();
+    if (!cachedImage)
+        return;
+
+    auto* image = cachedImage->image();
+    if (!is<BitmapImage>(image))
+        return;
+
+    image->resetAnimation();
+}
+
 void Internals::clearPageCache()
 {
     PageCache::singleton().pruneToSizeNow(0, PruningReason::None);

Modified: trunk/Source/WebCore/testing/Internals.h (209130 => 209131)


--- trunk/Source/WebCore/testing/Internals.h	2016-11-30 17:03:48 UTC (rev 209130)
+++ trunk/Source/WebCore/testing/Internals.h	2016-11-30 17:10:04 UTC (rev 209131)
@@ -104,6 +104,7 @@
 
     unsigned imageFrameIndex(HTMLImageElement&);
     void setImageFrameDecodingDuration(HTMLImageElement&, float duration);
+    void resetImageAnimation(HTMLImageElement&);
 
     void clearPageCache();
     unsigned pageCacheSize() const;

Modified: trunk/Source/WebCore/testing/Internals.idl (209130 => 209131)


--- trunk/Source/WebCore/testing/Internals.idl	2016-11-30 17:03:48 UTC (rev 209130)
+++ trunk/Source/WebCore/testing/Internals.idl	2016-11-30 17:10:04 UTC (rev 209131)
@@ -227,6 +227,7 @@
 
     unsigned long imageFrameIndex(HTMLImageElement element);
     void setImageFrameDecodingDuration(HTMLImageElement element, unrestricted float duration);
+    void resetImageAnimation(HTMLImageElement element);
 
     readonly attribute InternalSettings settings;
     readonly attribute unsigned long workerThreadCount;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to