Title: [118942] trunk/Source/WebCore
Revision
118942
Author
timothy_hor...@apple.com
Date
2012-05-30 11:28:28 -0700 (Wed, 30 May 2012)

Log Message

Factor DeferrableOneShotTimer out of GraphicsContextCG/GeneratorGeneratedImage
https://bugs.webkit.org/show_bug.cgi?id=87824

Reviewed by Simon Fraser.

DeferrableOneShotTimer is a timer which takes a class and method to call (as Timer does),
and a delay. It exposes restart(), stop(), and isActive(), and none of the rest of
the TimerBase interface. On restart(), the timer is started with the given delay,
unless it's already running, in which case a flag is set to automatically restart
the timer when it fires instead of calling the given method. This provides a
low-overhead way to implement a deferrable timer.

No new tests, refactoring.

* platform/Timer.h:
(DeferrableOneShotTimer):
(WebCore::DeferrableOneShotTimer::DeferrableOneShotTimer): New class.
(WebCore::DeferrableOneShotTimer::restart):
(WebCore::DeferrableOneShotTimer::fired):
* platform/graphics/GeneratorGeneratedImage.cpp:
(WebCore::GeneratorGeneratedImage::invalidateCacheTimerFired):
* platform/graphics/GeneratorGeneratedImage.h:
(WebCore::GeneratorGeneratedImage::GeneratorGeneratedImage): Make use of DeferrableOneShotTimer.
* platform/graphics/cg/GraphicsContextCG.cpp:
(WebCore::SubimageCacheWithTimer::SubimageCacheWithTimer): Make use of DeferrableOneShotTimer.
(WebCore::SubimageCacheWithTimer::invalidateCacheTimerFired):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (118941 => 118942)


--- trunk/Source/WebCore/ChangeLog	2012-05-30 18:09:44 UTC (rev 118941)
+++ trunk/Source/WebCore/ChangeLog	2012-05-30 18:28:28 UTC (rev 118942)
@@ -1,3 +1,32 @@
+2012-05-30  Tim Horton  <timothy_hor...@apple.com>
+
+        Factor DeferrableOneShotTimer out of GraphicsContextCG/GeneratorGeneratedImage
+        https://bugs.webkit.org/show_bug.cgi?id=87824
+
+        Reviewed by Simon Fraser.
+
+        DeferrableOneShotTimer is a timer which takes a class and method to call (as Timer does),
+        and a delay. It exposes restart(), stop(), and isActive(), and none of the rest of
+        the TimerBase interface. On restart(), the timer is started with the given delay,
+        unless it's already running, in which case a flag is set to automatically restart
+        the timer when it fires instead of calling the given method. This provides a
+        low-overhead way to implement a deferrable timer.
+
+        No new tests, refactoring.
+
+        * platform/Timer.h:
+        (DeferrableOneShotTimer):
+        (WebCore::DeferrableOneShotTimer::DeferrableOneShotTimer): New class.
+        (WebCore::DeferrableOneShotTimer::restart):
+        (WebCore::DeferrableOneShotTimer::fired):
+        * platform/graphics/GeneratorGeneratedImage.cpp:
+        (WebCore::GeneratorGeneratedImage::invalidateCacheTimerFired):
+        * platform/graphics/GeneratorGeneratedImage.h:
+        (WebCore::GeneratorGeneratedImage::GeneratorGeneratedImage): Make use of DeferrableOneShotTimer.
+        * platform/graphics/cg/GraphicsContextCG.cpp:
+        (WebCore::SubimageCacheWithTimer::SubimageCacheWithTimer): Make use of DeferrableOneShotTimer.
+        (WebCore::SubimageCacheWithTimer::invalidateCacheTimerFired):
+
 2012-05-30  Xueqing Huang  <huangxueq...@baidu.com>
 
         eventSender.beginDragWithFiles should be implemented in Windows, which blocked drag and drop related tests.

Modified: trunk/Source/WebCore/platform/Timer.h (118941 => 118942)


--- trunk/Source/WebCore/platform/Timer.h	2012-05-30 18:09:44 UTC (rev 118941)
+++ trunk/Source/WebCore/platform/Timer.h	2012-05-30 18:28:28 UTC (rev 118942)
@@ -109,6 +109,52 @@
     return m_nextFireTime;
 }
 
+template <typename TimerFiredClass> class DeferrableOneShotTimer : private TimerBase {
+public:
+    typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*);
+
+    DeferrableOneShotTimer(TimerFiredClass* o, TimerFiredFunction f, double delay)
+        : m_object(o)
+        , m_function(f)
+        , m_delay(delay)
+        , m_shouldRestartWhenTimerFires(false)
+    {
+    }
+
+    void restart()
+    {
+        // Setting this boolean is much more efficient than calling startOneShot
+        // again, which might result in rescheduling the system timer which
+        // can be quite expensive.
+
+        if (isActive()) {
+            m_shouldRestartWhenTimerFires = true;
+            return;
+        }
+        startOneShot(m_delay);
+    }
+
+    using TimerBase::stop;
+    using TimerBase::isActive;
+private:
+    virtual void fired()
+    {
+        if (m_shouldRestartWhenTimerFires) {
+            m_shouldRestartWhenTimerFires = false;
+            startOneShot(m_delay);
+            return;
+        }
+
+        (m_object->*m_function)(this);
+    }
+
+    TimerFiredClass* m_object;
+    TimerFiredFunction m_function;
+
+    double m_delay;
+    bool m_shouldRestartWhenTimerFires;
+};
+
 }
 
 #endif

Modified: trunk/Source/WebCore/platform/graphics/GeneratorGeneratedImage.cpp (118941 => 118942)


--- trunk/Source/WebCore/platform/graphics/GeneratorGeneratedImage.cpp	2012-05-30 18:09:44 UTC (rev 118941)
+++ trunk/Source/WebCore/platform/graphics/GeneratorGeneratedImage.cpp	2012-05-30 18:28:28 UTC (rev 118942)
@@ -90,4 +90,10 @@
     intrinsicRatio = FloatSize();
 }
 
+void GeneratorGeneratedImage::invalidateCacheTimerFired(DeferrableOneShotTimer<GeneratorGeneratedImage>*)
+{
+    m_cachedImageBuffer.clear();
+    m_cachedAdjustedSize = IntSize();
 }
+
+}

Modified: trunk/Source/WebCore/platform/graphics/GeneratorGeneratedImage.h (118941 => 118942)


--- trunk/Source/WebCore/platform/graphics/GeneratorGeneratedImage.h	2012-05-30 18:09:44 UTC (rev 118941)
+++ trunk/Source/WebCore/platform/graphics/GeneratorGeneratedImage.h	2012-05-30 18:28:28 UTC (rev 118942)
@@ -58,49 +58,19 @@
     virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
                              const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect);
 
+    void invalidateCacheTimerFired(DeferrableOneShotTimer<GeneratorGeneratedImage>*);
+
     GeneratorGeneratedImage(PassRefPtr<Generator> generator, const IntSize& size)
         : m_generator(generator)
-        , m_cacheTimer(this)
+        , m_cacheTimer(this, &GeneratorGeneratedImage::invalidateCacheTimerFired, generatedImageCacheClearDelay)
     {
         m_size = size;
     }
-    
-    class GeneratedImageCacheTimer : public TimerBase {
-    public:
-        GeneratedImageCacheTimer(GeneratorGeneratedImage * parent)
-        : m_shouldRestartWhenTimerFires(false)
-        , m_parent(parent) { }
-        
-        void restart()
-        {
-            if (isActive()) {
-                m_shouldRestartWhenTimerFires = true;
-                return;
-            }
-            startOneShot(generatedImageCacheClearDelay);
-        };
-    private:
-        virtual void fired() OVERRIDE
-        {
-            if (m_shouldRestartWhenTimerFires) {
-                m_shouldRestartWhenTimerFires = false;
-                startOneShot(generatedImageCacheClearDelay);
-                return;
-            }
-            
-            if (m_parent) {
-                m_parent->m_cachedImageBuffer.clear();
-                m_parent->m_cachedAdjustedSize = IntSize();
-            }
-        };
-        bool m_shouldRestartWhenTimerFires;
-        GeneratorGeneratedImage* m_parent;
-    };
 
     RefPtr<Generator> m_generator;
 
     OwnPtr<ImageBuffer> m_cachedImageBuffer;
-    GeneratedImageCacheTimer m_cacheTimer;
+    DeferrableOneShotTimer<GeneratorGeneratedImage> m_cacheTimer;
     IntSize m_cachedAdjustedSize;
     unsigned m_cachedGeneratorHash;
 };

Modified: trunk/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp (118941 => 118942)


--- trunk/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp	2012-05-30 18:09:44 UTC (rev 118941)
+++ trunk/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp	2012-05-30 18:28:28 UTC (rev 118942)
@@ -132,18 +132,16 @@
 
 typedef HashSet<SubimageCacheEntry, SubimageCacheHash, SubimageCacheEntryTraits> SubimageCache;
 
-class SubimageCacheTimer : private TimerBase {
-public:
-    SubimageCacheTimer() : m_shouldRestartWhenTimerFires(false) { }
-    void restart();
-private:
-    virtual void fired() OVERRIDE;
-    bool m_shouldRestartWhenTimerFires;
-};
-
 struct SubimageCacheWithTimer {
     SubimageCache cache;
-    SubimageCacheTimer timer;
+    DeferrableOneShotTimer<SubimageCacheWithTimer> timer;
+
+    SubimageCacheWithTimer()
+        : timer(this, &SubimageCacheWithTimer::invalidateCacheTimerFired, subimageCacheClearDelay)
+    {
+    }
+
+    void invalidateCacheTimerFired(DeferrableOneShotTimer<SubimageCacheWithTimer>*);
 };
 
 static SubimageCacheWithTimer& subimageCache()
@@ -152,25 +150,8 @@
     return cache;
 }
 
-inline void SubimageCacheTimer::restart()
+void SubimageCacheWithTimer::invalidateCacheTimerFired(DeferrableOneShotTimer<SubimageCacheWithTimer>*)
 {
-    // Setting this boolean is much more efficient than calling startOneShot
-    // again, which might result in rescheduling the system timer and that
-    // can be quite expensive.
-    if (isActive()) {
-        m_shouldRestartWhenTimerFires = true;
-        return;
-    }
-    startOneShot(subimageCacheClearDelay);
-}
-
-void SubimageCacheTimer::fired()
-{
-    if (m_shouldRestartWhenTimerFires) {
-        m_shouldRestartWhenTimerFires = false;
-        startOneShot(subimageCacheClearDelay);
-        return;
-    }
     subimageCache().cache.clear();
 }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to