Title: [149928] trunk/Source/WebCore
Revision
149928
Author
[email protected]
Date
2013-05-11 01:36:42 -0700 (Sat, 11 May 2013)

Log Message

Make CanvasStyle a plain object instead of an RefCounted object
https://bugs.webkit.org/show_bug.cgi?id=115775

Patch by Benjamin Poulain <[email protected]> on 2013-05-11
Reviewed by Andreas Kling.

CanvasStyle is just 2 words wide. We do not gain anything from allocating
it on the heap. Change the object to be just a type and some data.

* bindings/js/JSCanvasRenderingContext2DCustom.cpp:
(WebCore::toJS):
(WebCore::toHTMLCanvasStyle):
* html/HTMLCanvasElement.cpp:
* html/canvas/CanvasRenderingContext2D.cpp:
(WebCore::CanvasRenderingContext2D::State::State):
(WebCore::CanvasRenderingContext2D::setStrokeStyle):
(WebCore::CanvasRenderingContext2D::setFillStyle):
(WebCore::CanvasRenderingContext2D::setStrokeColor):
(WebCore::CanvasRenderingContext2D::setFillColor):
(WebCore::CanvasRenderingContext2D::fullCanvasCompositedFill):
(WebCore::CanvasRenderingContext2D::drawTextInternal):
* html/canvas/CanvasRenderingContext2D.h:
(WebCore::CanvasRenderingContext2D::strokeStyle):
(CanvasRenderingContext2D):
(WebCore::CanvasRenderingContext2D::fillStyle):
(State):
* html/canvas/CanvasStyle.cpp:
(WebCore::CanvasStyle::CanvasStyle):
(WebCore::CanvasStyle::createFromString):
(WebCore::CanvasStyle::createFromStringWithOverrideAlpha):
(WebCore::CanvasStyle::isEquivalentColor):
(WebCore):
(WebCore::CanvasStyle::operator=):
(WebCore::CanvasStyle::applyStrokeColor):
(WebCore::CanvasStyle::applyFillColor):
* html/canvas/CanvasStyle.h:
(CanvasStyle):
(WebCore::CanvasStyle::isValid):
(WebCore::CanvasStyle::CanvasStyle):
(WebCore):
(WebCore::CanvasStyle::operator=):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (149927 => 149928)


--- trunk/Source/WebCore/ChangeLog	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/ChangeLog	2013-05-11 08:36:42 UTC (rev 149928)
@@ -1,3 +1,46 @@
+2013-05-11  Benjamin Poulain  <[email protected]>
+
+        Make CanvasStyle a plain object instead of an RefCounted object
+        https://bugs.webkit.org/show_bug.cgi?id=115775
+
+        Reviewed by Andreas Kling.
+
+        CanvasStyle is just 2 words wide. We do not gain anything from allocating
+        it on the heap. Change the object to be just a type and some data.
+
+        * bindings/js/JSCanvasRenderingContext2DCustom.cpp:
+        (WebCore::toJS):
+        (WebCore::toHTMLCanvasStyle):
+        * html/HTMLCanvasElement.cpp:
+        * html/canvas/CanvasRenderingContext2D.cpp:
+        (WebCore::CanvasRenderingContext2D::State::State):
+        (WebCore::CanvasRenderingContext2D::setStrokeStyle):
+        (WebCore::CanvasRenderingContext2D::setFillStyle):
+        (WebCore::CanvasRenderingContext2D::setStrokeColor):
+        (WebCore::CanvasRenderingContext2D::setFillColor):
+        (WebCore::CanvasRenderingContext2D::fullCanvasCompositedFill):
+        (WebCore::CanvasRenderingContext2D::drawTextInternal):
+        * html/canvas/CanvasRenderingContext2D.h:
+        (WebCore::CanvasRenderingContext2D::strokeStyle):
+        (CanvasRenderingContext2D):
+        (WebCore::CanvasRenderingContext2D::fillStyle):
+        (State):
+        * html/canvas/CanvasStyle.cpp:
+        (WebCore::CanvasStyle::CanvasStyle):
+        (WebCore::CanvasStyle::createFromString):
+        (WebCore::CanvasStyle::createFromStringWithOverrideAlpha):
+        (WebCore::CanvasStyle::isEquivalentColor):
+        (WebCore):
+        (WebCore::CanvasStyle::operator=):
+        (WebCore::CanvasStyle::applyStrokeColor):
+        (WebCore::CanvasStyle::applyFillColor):
+        * html/canvas/CanvasStyle.h:
+        (CanvasStyle):
+        (WebCore::CanvasStyle::isValid):
+        (WebCore::CanvasStyle::CanvasStyle):
+        (WebCore):
+        (WebCore::CanvasStyle::operator=):
+
 2013-05-11  Benjamin Poulain  <[email protected]>
 
         Gradient::platformGradient: Vector reserveCapacity -> reserveInitialCapacity

Modified: trunk/Source/WebCore/bindings/js/JSCanvasRenderingContext2DCustom.cpp (149927 => 149928)


--- trunk/Source/WebCore/bindings/js/JSCanvasRenderingContext2DCustom.cpp	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/bindings/js/JSCanvasRenderingContext2DCustom.cpp	2013-05-11 08:36:42 UTC (rev 149928)
@@ -38,25 +38,25 @@
 
 namespace WebCore {
 
-static JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, CanvasStyle* style)
+static JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, const CanvasStyle& style)
 {
-    if (style->canvasGradient())
-        return toJS(exec, globalObject, style->canvasGradient());
-    if (style->canvasPattern())
-        return toJS(exec, globalObject, style->canvasPattern());
-    return jsStringWithCache(exec, style->color());
+    if (style.canvasGradient())
+        return toJS(exec, globalObject, style.canvasGradient());
+    if (style.canvasPattern())
+        return toJS(exec, globalObject, style.canvasPattern());
+    return jsStringWithCache(exec, style.color());
 }
 
-static PassRefPtr<CanvasStyle> toHTMLCanvasStyle(ExecState*, JSValue value)
+static CanvasStyle toHTMLCanvasStyle(ExecState*, JSValue value)
 {
     if (!value.isObject())
-        return 0;
+        return CanvasStyle();
     JSObject* object = asObject(value);
     if (object->inherits(&JSCanvasGradient::s_info))
-        return CanvasStyle::createFromGradient(jsCast<JSCanvasGradient*>(object)->impl());
+        return CanvasStyle(jsCast<JSCanvasGradient*>(object)->impl());
     if (object->inherits(&JSCanvasPattern::s_info))
-        return CanvasStyle::createFromPattern(jsCast<JSCanvasPattern*>(object)->impl());
-    return 0;
+        return CanvasStyle(jsCast<JSCanvasPattern*>(object)->impl());
+    return CanvasStyle();
 }
 
 JSValue JSCanvasRenderingContext2D::strokeStyle(ExecState* exec) const

Modified: trunk/Source/WebCore/html/HTMLCanvasElement.cpp (149927 => 149928)


--- trunk/Source/WebCore/html/HTMLCanvasElement.cpp	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.cpp	2013-05-11 08:36:42 UTC (rev 149928)
@@ -33,7 +33,6 @@
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
 #include "CanvasRenderingContext2D.h"
-#include "CanvasStyle.h"
 #include "Chrome.h"
 #include "Document.h"
 #include "ExceptionCode.h"

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp (149927 => 149928)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp	2013-05-11 08:36:42 UTC (rev 149928)
@@ -40,7 +40,6 @@
 #include "CachedImage.h"
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
-#include "CanvasStyle.h"
 #include "Console.h"
 #include "DOMPath.h"
 #include "ExceptionCode.h"
@@ -180,8 +179,8 @@
 }
 
 CanvasRenderingContext2D::State::State()
-    : m_strokeStyle(CanvasStyle::createFromRGBA(Color::black))
-    , m_fillStyle(CanvasStyle::createFromRGBA(Color::black))
+    : m_strokeStyle(Color::black)
+    , m_fillStyle(Color::black)
     , m_lineWidth(1)
     , m_lineCap(ButtCap)
     , m_lineJoin(MiterJoin)
@@ -312,68 +311,56 @@
     c->restore();
 }
 
-CanvasStyle* CanvasRenderingContext2D::strokeStyle() const
+void CanvasRenderingContext2D::setStrokeStyle(CanvasStyle style)
 {
-    return state().m_strokeStyle.get();
-}
-
-void CanvasRenderingContext2D::setStrokeStyle(PassRefPtr<CanvasStyle> prpStyle)
-{
-    RefPtr<CanvasStyle> style = prpStyle;
-
-    if (!style)
+    if (!style.isValid())
         return;
 
-    if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentColor(*style))
+    if (state().m_strokeStyle.isValid() && state().m_strokeStyle.isEquivalentColor(style))
         return;
 
-    if (style->isCurrentColor()) {
-        if (style->hasOverrideAlpha())
-            style = CanvasStyle::createFromRGBA(colorWithOverrideAlpha(currentColor(canvas()), style->overrideAlpha()));
+    if (style.isCurrentColor()) {
+        if (style.hasOverrideAlpha())
+            style = CanvasStyle(colorWithOverrideAlpha(currentColor(canvas()), style.overrideAlpha()));
         else
-            style = CanvasStyle::createFromRGBA(currentColor(canvas()));
+            style = CanvasStyle(currentColor(canvas()));
     } else
-        checkOrigin(style->canvasPattern());
+        checkOrigin(style.canvasPattern());
 
     realizeSaves();
-    modifiableState().m_strokeStyle = style.release();
+    State& state = modifiableState();
+    state.m_strokeStyle = style;
     GraphicsContext* c = drawingContext();
     if (!c)
         return;
-    state().m_strokeStyle->applyStrokeColor(c);
-    modifiableState().m_unparsedStrokeColor = String();
+    state.m_strokeStyle.applyStrokeColor(c);
+    state.m_unparsedStrokeColor = String();
 }
 
-CanvasStyle* CanvasRenderingContext2D::fillStyle() const
+void CanvasRenderingContext2D::setFillStyle(CanvasStyle style)
 {
-    return state().m_fillStyle.get();
-}
-
-void CanvasRenderingContext2D::setFillStyle(PassRefPtr<CanvasStyle> prpStyle)
-{
-    RefPtr<CanvasStyle> style = prpStyle;
-
-    if (!style)
+    if (!style.isValid())
         return;
 
-    if (state().m_fillStyle && state().m_fillStyle->isEquivalentColor(*style))
+    if (state().m_fillStyle.isValid() && state().m_fillStyle.isEquivalentColor(style))
         return;
 
-    if (style->isCurrentColor()) {
-        if (style->hasOverrideAlpha())
-            style = CanvasStyle::createFromRGBA(colorWithOverrideAlpha(currentColor(canvas()), style->overrideAlpha()));
+    if (style.isCurrentColor()) {
+        if (style.hasOverrideAlpha())
+            style = CanvasStyle(colorWithOverrideAlpha(currentColor(canvas()), style.overrideAlpha()));
         else
-            style = CanvasStyle::createFromRGBA(currentColor(canvas()));
+            style = CanvasStyle(currentColor(canvas()));
     } else
-        checkOrigin(style->canvasPattern());
+        checkOrigin(style.canvasPattern());
 
     realizeSaves();
-    modifiableState().m_fillStyle = style.release();
+    State& state = modifiableState();
+    state.m_fillStyle = style;
     GraphicsContext* c = drawingContext();
     if (!c)
         return;
-    state().m_fillStyle->applyFillColor(c);
-    modifiableState().m_unparsedFillColor = String();
+    state.m_fillStyle.applyFillColor(c);
+    state.m_unparsedFillColor = String();
 }
 
 float CanvasRenderingContext2D::lineWidth() const
@@ -782,9 +769,9 @@
 
 void CanvasRenderingContext2D::setStrokeColor(float grayLevel)
 {
-    if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, 1.0f))
+    if (state().m_strokeStyle.isValid() && state().m_strokeStyle.isEquivalentRGBA(grayLevel, grayLevel, grayLevel, 1.0f))
         return;
-    setStrokeStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, 1.0f));
+    setStrokeStyle(CanvasStyle(grayLevel, 1.0f));
 }
 
 void CanvasRenderingContext2D::setStrokeColor(const String& color, float alpha)
@@ -794,23 +781,23 @@
 
 void CanvasRenderingContext2D::setStrokeColor(float grayLevel, float alpha)
 {
-    if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, alpha))
+    if (state().m_strokeStyle.isValid() && state().m_strokeStyle.isEquivalentRGBA(grayLevel, grayLevel, grayLevel, alpha))
         return;
-    setStrokeStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, alpha));
+    setStrokeStyle(CanvasStyle(grayLevel, alpha));
 }
 
 void CanvasRenderingContext2D::setStrokeColor(float r, float g, float b, float a)
 {
-    if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentRGBA(r, g, b, a))
+    if (state().m_strokeStyle.isValid() && state().m_strokeStyle.isEquivalentRGBA(r, g, b, a))
         return;
-    setStrokeStyle(CanvasStyle::createFromRGBAChannels(r, g, b, a));
+    setStrokeStyle(CanvasStyle(r, g, b, a));
 }
 
 void CanvasRenderingContext2D::setStrokeColor(float c, float m, float y, float k, float a)
 {
-    if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentCMYKA(c, m, y, k, a))
+    if (state().m_strokeStyle.isValid() && state().m_strokeStyle.isEquivalentCMYKA(c, m, y, k, a))
         return;
-    setStrokeStyle(CanvasStyle::createFromCMYKAChannels(c, m, y, k, a));
+    setStrokeStyle(CanvasStyle(c, m, y, k, a));
 }
 
 void CanvasRenderingContext2D::setFillColor(const String& color)
@@ -824,9 +811,9 @@
 
 void CanvasRenderingContext2D::setFillColor(float grayLevel)
 {
-    if (state().m_fillStyle && state().m_fillStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, 1.0f))
+    if (state().m_fillStyle.isValid() && state().m_fillStyle.isEquivalentRGBA(grayLevel, grayLevel, grayLevel, 1.0f))
         return;
-    setFillStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, 1.0f));
+    setFillStyle(CanvasStyle(grayLevel, 1.0f));
 }
 
 void CanvasRenderingContext2D::setFillColor(const String& color, float alpha)
@@ -836,23 +823,23 @@
 
 void CanvasRenderingContext2D::setFillColor(float grayLevel, float alpha)
 {
-    if (state().m_fillStyle && state().m_fillStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, alpha))
+    if (state().m_fillStyle.isValid() && state().m_fillStyle.isEquivalentRGBA(grayLevel, grayLevel, grayLevel, alpha))
         return;
-    setFillStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, alpha));
+    setFillStyle(CanvasStyle(grayLevel, alpha));
 }
 
 void CanvasRenderingContext2D::setFillColor(float r, float g, float b, float a)
 {
-    if (state().m_fillStyle && state().m_fillStyle->isEquivalentRGBA(r, g, b, a))
+    if (state().m_fillStyle.isValid() && state().m_fillStyle.isEquivalentRGBA(r, g, b, a))
         return;
-    setFillStyle(CanvasStyle::createFromRGBAChannels(r, g, b, a));
+    setFillStyle(CanvasStyle(r, g, b, a));
 }
 
 void CanvasRenderingContext2D::setFillColor(float c, float m, float y, float k, float a)
 {
-    if (state().m_fillStyle && state().m_fillStyle->isEquivalentCMYKA(c, m, y, k, a))
+    if (state().m_fillStyle.isValid() && state().m_fillStyle.isEquivalentCMYKA(c, m, y, k, a))
         return;
-    setFillStyle(CanvasStyle::createFromCMYKAChannels(c, m, y, k, a));
+    setFillStyle(CanvasStyle(c, m, y, k, a));
 }
 
 void CanvasRenderingContext2D::beginPath()
@@ -1662,7 +1649,7 @@
     path.translate(FloatSize(-bufferRect.x(), -bufferRect.y()));
 
     buffer->context()->setCompositeOperation(CompositeSourceOver);
-    state().m_fillStyle->applyFillColor(buffer->context());
+    modifiableState().m_fillStyle.applyFillColor(buffer->context());
     buffer->context()->fillPath(path);
 
     compositeBuffer(buffer.get(), bufferRect, state().m_globalComposite);
@@ -2252,8 +2239,8 @@
         inflateStrokeRect(textRect);
 
 #if USE(CG)
-    CanvasStyle* drawStyle = fill ? state().m_fillStyle.get() : state().m_strokeStyle.get();
-    if (drawStyle->canvasGradient() || drawStyle->canvasPattern()) {
+    const CanvasStyle& drawStyle = fill ? state().m_fillStyle : state().m_strokeStyle;
+    if (drawStyle.canvasGradient() || drawStyle.canvasPattern()) {
         IntRect maskRect = enclosingIntRect(textRect);
 
         OwnPtr<ImageBuffer> maskImage = c->createCompatibleBuffer(maskRect.size());
@@ -2281,7 +2268,7 @@
 
         GraphicsContextStateSaver stateSaver(*c);
         c->clipToImageBuffer(maskImage.get(), maskRect);
-        drawStyle->applyFillColor(c);
+        drawStyle.applyFillColor(c);
         c->fillRect(maskRect);
         return;
     }

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.h (149927 => 149928)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.h	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.h	2013-05-11 08:36:42 UTC (rev 149928)
@@ -29,6 +29,7 @@
 #include "AffineTransform.h"
 #include "CanvasPathMethods.h"
 #include "CanvasRenderingContext.h"
+#include "CanvasStyle.h"
 #include "Color.h"
 #include "ColorSpace.h"
 #include "DashArray.h"
@@ -48,7 +49,6 @@
 
 class CanvasGradient;
 class CanvasPattern;
-class CanvasStyle;
 #if ENABLE(CANVAS_PATH)
 class DOMPath;
 #endif
@@ -70,11 +70,11 @@
     }
     virtual ~CanvasRenderingContext2D();
 
-    CanvasStyle* strokeStyle() const;
-    void setStrokeStyle(PassRefPtr<CanvasStyle>);
+    const CanvasStyle& strokeStyle() const { return state().m_strokeStyle; }
+    void setStrokeStyle(CanvasStyle);
 
-    CanvasStyle* fillStyle() const;
-    void setFillStyle(PassRefPtr<CanvasStyle>);
+    const CanvasStyle& fillStyle() const { return state().m_fillStyle; }
+    void setFillStyle(CanvasStyle);
 
     float lineWidth() const;
     void setLineWidth(float);
@@ -240,8 +240,8 @@
 
         String m_unparsedStrokeColor;
         String m_unparsedFillColor;
-        RefPtr<CanvasStyle> m_strokeStyle;
-        RefPtr<CanvasStyle> m_fillStyle;
+        CanvasStyle m_strokeStyle;
+        CanvasStyle m_fillStyle;
         float m_lineWidth;
         LineCap m_lineCap;
         LineJoin m_lineJoin;

Modified: trunk/Source/WebCore/html/canvas/CanvasStyle.cpp (149927 => 149928)


--- trunk/Source/WebCore/html/canvas/CanvasStyle.cpp	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/html/canvas/CanvasStyle.cpp	2013-05-11 08:36:42 UTC (rev 149928)
@@ -92,46 +92,44 @@
     }
 }
 
-CanvasStyle::CanvasStyle(Type type, float overrideAlpha)
-    : m_type(type)
-    , m_overrideAlpha(overrideAlpha)
-{
-}
-
 CanvasStyle::CanvasStyle(RGBA32 rgba)
-    : m_type(RGBA)
-    , m_rgba(rgba)
+    : m_rgba(rgba)
+    , m_type(RGBA)
 {
 }
 
 CanvasStyle::CanvasStyle(float grayLevel, float alpha)
-    : m_type(RGBA)
-    , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha))
+    : m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha))
+    , m_type(RGBA)
 {
 }
 
 CanvasStyle::CanvasStyle(float r, float g, float b, float a)
-    : m_type(RGBA)
-    , m_rgba(makeRGBA32FromFloats(r, g, b, a))
+    : m_rgba(makeRGBA32FromFloats(r, g, b, a))
+    , m_type(RGBA)
 {
 }
 
 CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
-    : m_type(CMYKA)
-    , m_cmyka(new CMYKAValues(makeRGBAFromCMYKA(c, m, y, k, a), c, m, y, k, a))
+    : m_cmyka(new CMYKAValues(makeRGBAFromCMYKA(c, m, y, k, a), c, m, y, k, a))
+    , m_type(CMYKA)
 {
 }
 
 CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
-    : m_type(Gradient)
-    , m_gradient(gradient.leakRef())
+    : m_gradient(gradient.leakRef())
+    , m_type(Gradient)
 {
+    if (!m_gradient)
+        m_type = Invalid;
 }
 
 CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
-    : m_type(ImagePattern)
-    , m_pattern(pattern.leakRef())
+    : m_pattern(pattern.leakRef())
+    , m_type(ImagePattern)
 {
+    if (!m_pattern)
+        m_type = Invalid;
 }
 
 CanvasStyle::~CanvasStyle()
@@ -144,54 +142,41 @@
         delete m_cmyka;
 }
 
-PassRefPtr<CanvasStyle> CanvasStyle::createFromString(const String& color, Document* document)
+CanvasStyle CanvasStyle::createFromString(const String& color, Document* document)
 {
     RGBA32 rgba;
     ColorParseResult parseResult = parseColor(rgba, color, document);
     switch (parseResult) {
     case ParsedRGBA:
     case ParsedSystemColor:
-        return adoptRef(new CanvasStyle(rgba));
+        return CanvasStyle(rgba);
     case ParsedCurrentColor:
-        return adoptRef(new CanvasStyle(CurrentColor));
+        return CanvasStyle(ConstructCurrentColor);
     case ParseFailed:
-        return 0;
+        return CanvasStyle();
     default:
         ASSERT_NOT_REACHED();
-        return 0;
+        return CanvasStyle();
     }
 }
 
-PassRefPtr<CanvasStyle> CanvasStyle::createFromStringWithOverrideAlpha(const String& color, float alpha)
+CanvasStyle CanvasStyle::createFromStringWithOverrideAlpha(const String& color, float alpha)
 {
     RGBA32 rgba;
     ColorParseResult parseResult = parseColor(rgba, color);
     switch (parseResult) {
     case ParsedRGBA:
-        return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
+        return CanvasStyle(colorWithOverrideAlpha(rgba, alpha));
     case ParsedCurrentColor:
-        return adoptRef(new CanvasStyle(CurrentColorWithOverrideAlpha, alpha));
+        return CanvasStyle(CurrentColorWithOverrideAlpha, alpha);
     case ParseFailed:
-        return 0;
+        return CanvasStyle();
     default:
         ASSERT_NOT_REACHED();
-        return 0;
+        return CanvasStyle();
     }
 }
 
-PassRefPtr<CanvasStyle> CanvasStyle::createFromGradient(PassRefPtr<CanvasGradient> gradient)
-{
-    if (!gradient)
-        return 0;
-    return adoptRef(new CanvasStyle(gradient));
-}
-PassRefPtr<CanvasStyle> CanvasStyle::createFromPattern(PassRefPtr<CanvasPattern> pattern)
-{
-    if (!pattern)
-        return 0;
-    return adoptRef(new CanvasStyle(pattern));
-}
-
 bool CanvasStyle::isEquivalentColor(const CanvasStyle& other) const
 {
     if (m_type != other.m_type)
@@ -211,6 +196,8 @@
     case CurrentColor:
     case CurrentColorWithOverrideAlpha:
         return false;
+    case Invalid:
+        break;
     }
 
     ASSERT_NOT_REACHED();
@@ -237,8 +224,33 @@
         && a == m_cmyka->a;
 }
 
-void CanvasStyle::applyStrokeColor(GraphicsContext* context)
+CanvasStyle::CanvasStyle(const CanvasStyle& other)
 {
+    memcpy(this, &other, sizeof(CanvasStyle));
+    if (m_type == Gradient)
+        m_gradient->ref();
+    else if (m_type == ImagePattern)
+        m_pattern->ref();
+    else if (m_type == CMYKA)
+        m_cmyka = new CMYKAValues(other.m_cmyka->rgba, other.m_cmyka->c, other.m_cmyka->m, other.m_cmyka->y, other.m_cmyka->k, other.m_cmyka->a);
+}
+
+CanvasStyle& CanvasStyle::operator=(const CanvasStyle& other)
+{
+    if (this != &other) {
+        memcpy(this, &other, sizeof(CanvasStyle));
+        if (m_type == Gradient)
+            m_gradient->ref();
+        else if (m_type == ImagePattern)
+            m_pattern->ref();
+        else if (m_type == CMYKA)
+            m_cmyka = new CMYKAValues(other.m_cmyka->rgba, other.m_cmyka->c, other.m_cmyka->m, other.m_cmyka->y, other.m_cmyka->k, other.m_cmyka->a);
+    }
+    return *this;
+}
+
+void CanvasStyle::applyStrokeColor(GraphicsContext* context) const
+{
     if (!context)
         return;
     switch (m_type) {
@@ -269,12 +281,13 @@
         break;
     case CurrentColor:
     case CurrentColorWithOverrideAlpha:
+    case Invalid:
         ASSERT_NOT_REACHED();
         break;
     }
 }
 
-void CanvasStyle::applyFillColor(GraphicsContext* context)
+void CanvasStyle::applyFillColor(GraphicsContext* context) const
 {
     if (!context)
         return;
@@ -306,6 +319,7 @@
         break;
     case CurrentColor:
     case CurrentColorWithOverrideAlpha:
+    case Invalid:
         ASSERT_NOT_REACHED();
         break;
     }

Modified: trunk/Source/WebCore/html/canvas/CanvasStyle.h (149927 => 149928)


--- trunk/Source/WebCore/html/canvas/CanvasStyle.h	2013-05-11 07:56:09 UTC (rev 149927)
+++ trunk/Source/WebCore/html/canvas/CanvasStyle.h	2013-05-11 08:36:42 UTC (rev 149928)
@@ -40,21 +40,21 @@
     class GraphicsContext;
     class HTMLCanvasElement;
 
-    class CanvasStyle : public RefCounted<CanvasStyle> {
-        WTF_MAKE_FAST_ALLOCATED;
-        WTF_MAKE_NONCOPYABLE(CanvasStyle);
+    class CanvasStyle {
     public:
+        CanvasStyle();
+        explicit CanvasStyle(RGBA32);
+        CanvasStyle(float grayLevel, float alpha);
+        CanvasStyle(float r, float g, float b, float alpha);
+        CanvasStyle(float c, float m, float y, float k, float alpha);
+        explicit CanvasStyle(PassRefPtr<CanvasGradient>);
+        explicit CanvasStyle(PassRefPtr<CanvasPattern>);
         ~CanvasStyle();
 
-        static PassRefPtr<CanvasStyle> createFromRGBA(RGBA32 rgba) { return adoptRef(new CanvasStyle(rgba)); }
-        static PassRefPtr<CanvasStyle> createFromString(const String& color, Document* = 0);
-        static PassRefPtr<CanvasStyle> createFromStringWithOverrideAlpha(const String& color, float alpha);
-        static PassRefPtr<CanvasStyle> createFromGrayLevelWithAlpha(float grayLevel, float alpha) { return adoptRef(new CanvasStyle(grayLevel, alpha)); }
-        static PassRefPtr<CanvasStyle> createFromRGBAChannels(float r, float g, float b, float a) { return adoptRef(new CanvasStyle(r, g, b, a)); }
-        static PassRefPtr<CanvasStyle> createFromCMYKAChannels(float c, float m, float y, float k, float a) { return adoptRef(new CanvasStyle(c, m, y, k, a)); }
-        static PassRefPtr<CanvasStyle> createFromGradient(PassRefPtr<CanvasGradient>);
-        static PassRefPtr<CanvasStyle> createFromPattern(PassRefPtr<CanvasPattern>);
+        static CanvasStyle createFromString(const String& color, Document* = 0);
+        static CanvasStyle createFromStringWithOverrideAlpha(const String& color, float alpha);
 
+        bool isValid() const { return m_type != Invalid; }
         bool isCurrentColor() const { return m_type == CurrentColor || m_type == CurrentColorWithOverrideAlpha; }
         bool hasOverrideAlpha() const { return m_type == CurrentColorWithOverrideAlpha; }
         float overrideAlpha() const { ASSERT(m_type == CurrentColorWithOverrideAlpha); return m_overrideAlpha; }
@@ -63,15 +63,22 @@
         CanvasGradient* canvasGradient() const;
         CanvasPattern* canvasPattern() const;
 
-        void applyFillColor(GraphicsContext*);
-        void applyStrokeColor(GraphicsContext*);
+        void applyFillColor(GraphicsContext*) const;
+        void applyStrokeColor(GraphicsContext*) const;
 
         bool isEquivalentColor(const CanvasStyle&) const;
         bool isEquivalentRGBA(float r, float g, float b, float a) const;
         bool isEquivalentCMYKA(float c, float m, float y, float k, float a) const;
 
+        CanvasStyle(const CanvasStyle&);
+        CanvasStyle& operator=(const CanvasStyle&);
+#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
+        CanvasStyle(CanvasStyle&&);
+        CanvasStyle& operator=(CanvasStyle&&);
+#endif
+
     private:
-        enum Type { RGBA, CMYKA, Gradient, ImagePattern, CurrentColor, CurrentColorWithOverrideAlpha };
+        enum Type { RGBA, CMYKA, Gradient, ImagePattern, CurrentColor, CurrentColorWithOverrideAlpha, Invalid };
         struct CMYKAValues {
             WTF_MAKE_FAST_ALLOCATED;
             WTF_MAKE_NONCOPYABLE(CMYKAValues);
@@ -86,16 +93,14 @@
             float a;
         };
 
-        CanvasStyle(Type, float overrideAlpha = 0);
-        CanvasStyle(RGBA32 rgba);
-        CanvasStyle(float grayLevel, float alpha);
-        CanvasStyle(float r, float g, float b, float a);
-        CanvasStyle(float c, float m, float y, float k, float a);
-        CanvasStyle(PassRefPtr<CanvasGradient>);
-        CanvasStyle(PassRefPtr<CanvasPattern>);
+        enum ConstructCurrentColorTag { ConstructCurrentColor };
+        CanvasStyle(ConstructCurrentColorTag) : m_type(CurrentColor) { }
+        CanvasStyle(Type type, float overrideAlpha)
+            : m_overrideAlpha(overrideAlpha)
+            , m_type(type)
+        {
+        }
 
-        Type m_type;
-
         union {
             RGBA32 m_rgba;
             float m_overrideAlpha;
@@ -103,11 +108,17 @@
             CanvasPattern* m_pattern;
             CMYKAValues* m_cmyka;
         };
+        Type m_type;
     };
 
     RGBA32 currentColor(HTMLCanvasElement*);
     bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HTMLCanvasElement*);
 
+    inline CanvasStyle::CanvasStyle()
+        : m_type(Invalid)
+    {
+    }
+
     inline CanvasGradient* CanvasStyle::canvasGradient() const
     {
         if (m_type == Gradient)
@@ -130,6 +141,23 @@
         return Color(m_cmyka->rgba).serialized();
     }
 
+#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
+    inline CanvasStyle::CanvasStyle(CanvasStyle&& other)
+    {
+        memcpy(this, &other, sizeof(CanvasStyle));
+        other.m_type = Invalid;
+    }
+
+    inline CanvasStyle& CanvasStyle::operator=(CanvasStyle&& other)
+    {
+        if (this != &other) {
+            memcpy(this, &other, sizeof(CanvasStyle));
+            other.m_type = Invalid;
+        }
+        return *this;
+    }
+#endif
+
 } // namespace WebCore
 
 #endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to