Skip to content

Commit

Permalink
Cherry-pick 5ec2341. rdar://problem/112439220
Browse files Browse the repository at this point in the history
    Cherry-pick 266238@main (5ec2341). rdar://112439220

        Avoid creating a new CSSParserContext each time a color is assigned to fillStyle/strokeStyle on a canvas context
        https://bugs.webkit.org/show_bug.cgi?id=259431
        rdar://112439220

        Reviewed by Tim Nguyen.

        Creating the CSSParserContext is slightly expensive, since it fetches all of the relevant document Settings.

        * Source/WebCore/html/HTMLCanvasElement.h:
        (WebCore::HTMLCanvasElement::cssParserContext):
        * Source/WebCore/html/canvas/CanvasStyle.cpp:
        (WebCore::parseColor):

        Canonical link: https://commits.webkit.org/266238@main

Identifier: 265870.287@safari-7616.1.27.13-branch
  • Loading branch information
heycam authored and MyahCobbs committed Aug 2, 2023
1 parent 9165679 commit 995ad45
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Source/WebCore/html/HTMLCanvasElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#pragma once

#include "ActiveDOMObject.h"
#include "CSSParserContext.h"
#include "CanvasBase.h"
#include "Document.h"
#include "FloatRect.h"
Expand Down Expand Up @@ -144,6 +145,7 @@ class HTMLCanvasElement final : public HTMLElement, public CanvasBase, public Ac

using HTMLElement::ref;
using HTMLElement::deref;
CSSParserContext& cssParserContext();

private:
HTMLCanvasElement(const QualifiedName&, Document&);
Expand Down Expand Up @@ -189,6 +191,7 @@ class HTMLCanvasElement final : public HTMLElement, public CanvasBase, public Ac

std::unique_ptr<CanvasRenderingContext> m_context;
mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
std::unique_ptr<CSSParserContext> m_cssParserContext;

std::optional<bool> m_usesDisplayListDrawing;

Expand All @@ -203,6 +206,13 @@ class HTMLCanvasElement final : public HTMLElement, public CanvasBase, public Ac
bool m_isSnapshotting { false };
};

inline CSSParserContext& HTMLCanvasElement::cssParserContext()
{
if (!m_cssParserContext)
m_cssParserContext = WTF::makeUnique<CSSParserContext>(document());
return *m_cssParserContext;
}

WebCoreOpaqueRoot root(HTMLCanvasElement*);

} // namespace WebCore
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/canvas/CanvasStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Color parseColor(const String& colorString, CanvasBase& canvasBase)

Color color;
if (is<HTMLCanvasElement>(canvasBase))
color = CSSParser::parseColor(colorString, CSSParserContext { downcast<HTMLCanvasElement>(canvasBase).document() });
color = CSSParser::parseColor(colorString, downcast<HTMLCanvasElement>(canvasBase).cssParserContext());
else
color = CSSParser::parseColorWithoutContext(colorString);

Expand Down

0 comments on commit 995ad45

Please sign in to comment.