Reuse buffer allocation if canvas size does not change
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 20 Mar 2012 21:39:27 +0000 (21:39 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 20 Mar 2012 21:39:27 +0000 (21:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=80871

Patch by Sami Kyostila <skyostil@chromium.org> on 2012-03-20
Reviewed by Stephen White.

Source/WebCore:

If the user changes the width or height attributes of a canvas element,
the contents of the canvas should be cleared and the context state
should be reset. This has become a common idiom to clear the canvas
"efficiently" at the start of a frame.

Previously, this code path triggered a full reallocation of the image
buffer backing the canvas, leading to reduced performance. This patch
implements an optimization where we reuse the previous image buffer
allocation if the size of the canvas did not change. Also, instead of
clearing the canvas every time its dimensions are touched, we only clear
if anything has been drawn into the canvas since it was previously
cleared.

Note that for now this optimization only applies for 2D canvases,
since it is not entirely clear how touching the dimensions of a WebGL
canvas should work.

Test: fast/canvas/canvas-resize-after-paint-without-layout.html +
      existing layout tests for canvas resetting.

* html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::HTMLCanvasElement):
(WebCore::HTMLCanvasElement::reset):
(WebCore::HTMLCanvasElement::createImageBuffer):
(WebCore::HTMLCanvasElement::clearImageBuffer):
(WebCore):
(WebCore::HTMLCanvasElement::clearCopiedImage):
* html/HTMLCanvasElement.h:
(HTMLCanvasElement):
* html/canvas/CanvasRenderingContext2D.cpp:
(WebCore::CanvasRenderingContext2D::CanvasRenderingContext2D): Save the
initial graphics context state so we can restore it without creating a
new context.
(WebCore::CanvasRenderingContext2D::unwindStateStack):
(WebCore::CanvasRenderingContext2D::reset): No need to notify the
compositor when the context is reset, because clearing the image buffer
does the same thing. We can also skip the notification if we did not
have an image buffer at the time of the reset, because the reset will
not have any visual impact in this case. Finally, if the canvas size
did change, the notification is also unnecessary because of the call
to RenderObject::repaint() from HTMLCanvasElement::reset().

LayoutTests:

Add layout test to check canvas resizing without changing its layout size.

We also update the expected image one canvas clearing test. The test
is setting the size of a canvas and expecting it to be cleared in the process.
With the optimization to retain the underlying ImageBuffer, we no longer call
RenderReplaced::repaint() as a part of this process. This function used to
repaint both the canvas itself (100x50) as well as its local selection
rectangle (100x54).

In this case the local selection rectangle is larger than the canvas because
the canvas is contained within an anonymous RenderBlock that also has two empty
text nodes. The extra 4 pixels are needed for drawing the selection rectangle
around any descenders in the the text of those nodes.

Since clearing the canvas has no effect on the selection rectangle, we only
need to repaint the area of the canvas itself.

* fast/canvas/canvas-resize-after-paint-without-layout.html: Added.
* fast/canvas/canvas-resize-after-paint-without-layout-expected.txt: Added.
* platform/chromium-linux/fast/canvas/canvas-resize-after-paint-without-layout-expected.png: Added.
* platform/chromium-linux/fast/canvas/setWidthResetAfterForcedRender-expected.png: Updated.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@111442 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/canvas/canvas-resize-after-paint-without-layout-expected.txt [new file with mode: 0644]
LayoutTests/fast/canvas/canvas-resize-after-paint-without-layout.html [new file with mode: 0644]
LayoutTests/platform/chromium-linux/fast/canvas/canvas-resize-after-paint-without-layout-expected.png [new file with mode: 0644]
LayoutTests/platform/chromium-linux/fast/canvas/setWidthResetAfterForcedRender-expected.png
LayoutTests/platform/chromium-linux/platform/chromium/virtual/gpu/fast/canvas/canvas-resize-after-paint-without-layout-expected.png [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/html/HTMLCanvasElement.cpp
Source/WebCore/html/HTMLCanvasElement.h
Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp

index a4beec8..0efd882 100644 (file)
@@ -1,3 +1,32 @@
+2012-03-20  Sami Kyostila  <skyostil@chromium.org>
+
+        Reuse buffer allocation if canvas size does not change
+        https://bugs.webkit.org/show_bug.cgi?id=80871
+
+        Reviewed by Stephen White.
+
+        Add layout test to check canvas resizing without changing its layout size.
+
+        We also update the expected image one canvas clearing test. The test
+        is setting the size of a canvas and expecting it to be cleared in the process.
+        With the optimization to retain the underlying ImageBuffer, we no longer call
+        RenderReplaced::repaint() as a part of this process. This function used to
+        repaint both the canvas itself (100x50) as well as its local selection
+        rectangle (100x54).
+
+        In this case the local selection rectangle is larger than the canvas because
+        the canvas is contained within an anonymous RenderBlock that also has two empty
+        text nodes. The extra 4 pixels are needed for drawing the selection rectangle
+        around any descenders in the the text of those nodes.
+
+        Since clearing the canvas has no effect on the selection rectangle, we only
+        need to repaint the area of the canvas itself.
+
+        * fast/canvas/canvas-resize-after-paint-without-layout.html: Added.
+        * fast/canvas/canvas-resize-after-paint-without-layout-expected.txt: Added.
+        * platform/chromium-linux/fast/canvas/canvas-resize-after-paint-without-layout-expected.png: Added.
+        * platform/chromium-linux/fast/canvas/setWidthResetAfterForcedRender-expected.png: Updated.
+
 2012-03-20  Dan Bernstein  <mitz@apple.com>
 
         Added Mac WebKit2 expected results for this test. <http://webkit.org/b/81700> tracks the
diff --git a/LayoutTests/fast/canvas/canvas-resize-after-paint-without-layout-expected.txt b/LayoutTests/fast/canvas/canvas-resize-after-paint-without-layout-expected.txt
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+
diff --git a/LayoutTests/fast/canvas/canvas-resize-after-paint-without-layout.html b/LayoutTests/fast/canvas/canvas-resize-after-paint-without-layout.html
new file mode 100644 (file)
index 0000000..6aa01c9
--- /dev/null
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<!-- Check that resizing a (potentially accelerated) canvas properly clears its
+     contents even if the layout size of the canvas does not change. Expected
+     output is a blank canvas.
+     https://bugs.webkit.org/show_bug.cgi?id=80871 -->
+<html>
+  <head>
+    <style>
+      #canvas {
+        outline: solid 1px black;
+        width: 300px;
+        height: 300px;
+      }
+    </style>
+    <script src="resources/repaint.js"></script>
+    <script>
+      if (window.layoutTestController)
+        layoutTestController.dumpAsText(true);
+
+      function runTest() {
+        var canvas = document.getElementById('canvas');
+        var ctx = canvas.getContext('2d');
+        ctx.fillStyle = 'red';
+        ctx.fillRect(0, 0, 300, 300);
+        runRepaintTest();
+      }
+
+      function repaintTest() {
+        var canvas = document.getElementById('canvas');
+        // This changes the resolution of the canvas but keeps its layout size constant.
+        canvas.width = canvas.width / 2;
+      }
+    </script>
+  </head>
+  <body onload="runTest();">
+    <canvas id="canvas" width="300" height="300"/>
+  </body>
+</html>
diff --git a/LayoutTests/platform/chromium-linux/fast/canvas/canvas-resize-after-paint-without-layout-expected.png b/LayoutTests/platform/chromium-linux/fast/canvas/canvas-resize-after-paint-without-layout-expected.png
new file mode 100644 (file)
index 0000000..91f145c
Binary files /dev/null and b/LayoutTests/platform/chromium-linux/fast/canvas/canvas-resize-after-paint-without-layout-expected.png differ
index 092b0e8..62d07ef 100644 (file)
Binary files a/LayoutTests/platform/chromium-linux/fast/canvas/setWidthResetAfterForcedRender-expected.png and b/LayoutTests/platform/chromium-linux/fast/canvas/setWidthResetAfterForcedRender-expected.png differ
diff --git a/LayoutTests/platform/chromium-linux/platform/chromium/virtual/gpu/fast/canvas/canvas-resize-after-paint-without-layout-expected.png b/LayoutTests/platform/chromium-linux/platform/chromium/virtual/gpu/fast/canvas/canvas-resize-after-paint-without-layout-expected.png
new file mode 100644 (file)
index 0000000..d901ca1
Binary files /dev/null and b/LayoutTests/platform/chromium-linux/platform/chromium/virtual/gpu/fast/canvas/canvas-resize-after-paint-without-layout-expected.png differ
index b95d9d7..9ca7612 100644 (file)
@@ -1,3 +1,52 @@
+2012-03-20  Sami Kyostila  <skyostil@chromium.org>
+
+        Reuse buffer allocation if canvas size does not change
+        https://bugs.webkit.org/show_bug.cgi?id=80871
+
+        Reviewed by Stephen White.
+
+        If the user changes the width or height attributes of a canvas element,
+        the contents of the canvas should be cleared and the context state
+        should be reset. This has become a common idiom to clear the canvas
+        "efficiently" at the start of a frame.
+
+        Previously, this code path triggered a full reallocation of the image
+        buffer backing the canvas, leading to reduced performance. This patch
+        implements an optimization where we reuse the previous image buffer
+        allocation if the size of the canvas did not change. Also, instead of
+        clearing the canvas every time its dimensions are touched, we only clear
+        if anything has been drawn into the canvas since it was previously
+        cleared.
+
+        Note that for now this optimization only applies for 2D canvases,
+        since it is not entirely clear how touching the dimensions of a WebGL
+        canvas should work.
+
+        Test: fast/canvas/canvas-resize-after-paint-without-layout.html +
+              existing layout tests for canvas resetting.
+
+        * html/HTMLCanvasElement.cpp:
+        (WebCore::HTMLCanvasElement::HTMLCanvasElement):
+        (WebCore::HTMLCanvasElement::reset):
+        (WebCore::HTMLCanvasElement::createImageBuffer):
+        (WebCore::HTMLCanvasElement::clearImageBuffer):
+        (WebCore):
+        (WebCore::HTMLCanvasElement::clearCopiedImage):
+        * html/HTMLCanvasElement.h:
+        (HTMLCanvasElement):
+        * html/canvas/CanvasRenderingContext2D.cpp:
+        (WebCore::CanvasRenderingContext2D::CanvasRenderingContext2D): Save the
+        initial graphics context state so we can restore it without creating a
+        new context.
+        (WebCore::CanvasRenderingContext2D::unwindStateStack):
+        (WebCore::CanvasRenderingContext2D::reset): No need to notify the
+        compositor when the context is reset, because clearing the image buffer
+        does the same thing. We can also skip the notification if we did not
+        have an image buffer at the time of the reset, because the reset will
+        not have any visual impact in this case. Finally, if the canvas size
+        did change, the notification is also unnecessary because of the call
+        to RenderObject::repaint() from HTMLCanvasElement::reset().
+
 2012-03-20  Dana Jansens  <danakj@chromium.org>
 
         [chromium] Allow us to disable overdraw metrics when tracing is off
index 07ab309..83c1965 100644 (file)
@@ -92,6 +92,7 @@ HTMLCanvasElement::HTMLCanvasElement(const QualifiedName& tagName, Document* doc
 #endif
     , m_originClean(true)
     , m_hasCreatedImageBuffer(false)
+    , m_didClearImageBuffer(false)
 {
     ASSERT(hasTagName(canvasTag));
 }
@@ -253,7 +254,14 @@ void HTMLCanvasElement::reset()
     }
 
     IntSize oldSize = size();
-    setSurfaceSize(IntSize(w, h)); // The image buffer gets cleared here.
+    // If the size of an existing buffer matches, we can just clear it instead of reallocating.
+    // This optimization is only done for 2D canvases for now.
+    if (m_hasCreatedImageBuffer && oldSize == IntSize(w, h) && (!m_context || m_context->is2d())) {
+        if (!m_didClearImageBuffer)
+            clearImageBuffer();
+        return;
+    }
+    setSurfaceSize(IntSize(w, h));
 
 #if ENABLE(WEBGL)
     if (m_context && m_context->is3d() && oldSize != size())
@@ -262,8 +270,13 @@ void HTMLCanvasElement::reset()
 
     if (RenderObject* renderer = this->renderer()) {
         if (m_rendererIsCanvas) {
-            if (oldSize != size())
+            if (oldSize != size()) {
                 toRenderHTMLCanvas(renderer)->canvasSizeChanged();
+#if USE(ACCELERATED_COMPOSITING)
+                if (renderBox() && renderBox()->hasLayer() && renderBox()->layer()->hasAcceleratedCompositing())
+                    renderBox()->layer()->contentChanged(RenderLayer::CanvasChanged);
+#endif
+            }
             if (hadImageBuffer)
                 renderer->repaint();
         }
@@ -498,6 +511,7 @@ void HTMLCanvasElement::createImageBuffer() const
     ASSERT(!m_imageBuffer);
 
     m_hasCreatedImageBuffer = true;
+    m_didClearImageBuffer = true;
 
     FloatSize logicalSize = size();
     FloatSize deviceSize = convertLogicalToDevice(logicalSize);
@@ -573,9 +587,27 @@ Image* HTMLCanvasElement::copiedImage() const
     return m_copiedImage.get();
 }
 
+void HTMLCanvasElement::clearImageBuffer() const
+{
+    ASSERT(m_hasCreatedImageBuffer);
+    ASSERT(!m_didClearImageBuffer);
+
+    if (!m_context)
+        return;
+
+    m_didClearImageBuffer = true;
+
+    if (m_context->is2d()) {
+        CanvasRenderingContext2D* context2D = static_cast<CanvasRenderingContext2D*>(m_context.get());
+        // No need to undo transforms/clip/etc. because we are called right after the context is reset.
+        context2D->clearRect(0, 0, width(), height());
+    }
+}
+
 void HTMLCanvasElement::clearCopiedImage()
 {
     m_copiedImage.clear();
+    m_didClearImageBuffer = false;
 }
 
 AffineTransform HTMLCanvasElement::baseTransform() const
index e2c4888..92d7625 100644 (file)
@@ -143,6 +143,7 @@ private:
     void reset();
 
     void createImageBuffer() const;
+    void clearImageBuffer() const;
 
     void setSurfaceSize(const IntSize&);
 
@@ -166,6 +167,7 @@ private:
 
     // m_createdImageBuffer means we tried to malloc the buffer.  We didn't necessarily get it.
     mutable bool m_hasCreatedImageBuffer;
+    mutable bool m_didClearImageBuffer;
     mutable OwnPtr<ImageBuffer> m_imageBuffer;
     
     mutable RefPtr<Image> m_presentedImage;
index 279b2a1..eca5419 100644 (file)
@@ -124,6 +124,8 @@ CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, bo
 #if !ENABLE(DASHBOARD_SUPPORT)
     ASSERT_UNUSED(usesDashboardCompatibilityMode, !usesDashboardCompatibilityMode);
 #endif
+    if (GraphicsContext* context = canvas->drawingContext())
+        context->save();
 }
 
 void CanvasRenderingContext2D::unwindStateStack()
@@ -133,7 +135,7 @@ void CanvasRenderingContext2D::unwindStateStack()
     // GraphicsContext dtor.
     if (size_t stackSize = m_stateStack.size()) {
         if (GraphicsContext* context = canvas()->existingDrawingContext()) {
-            while (--stackSize)
+            while (stackSize--)
                 context->restore();
         }
     }
@@ -161,11 +163,8 @@ void CanvasRenderingContext2D::reset()
     m_stateStack.resize(1);
     m_stateStack.first() = State();
     m_path.clear();
-#if USE(ACCELERATED_COMPOSITING)
-    RenderBox* renderBox = canvas()->renderBox();
-    if (renderBox && renderBox->hasLayer() && renderBox->layer()->hasAcceleratedCompositing())
-        renderBox->layer()->contentChanged(RenderLayer::CanvasChanged);
-#endif
+    if (GraphicsContext* context = canvas()->drawingContext())
+        context->save();
 }
 
 CanvasRenderingContext2D::State::State()