Web Inspector: replace HTMLCanvasElement with CanvasRenderingContext for instrumentat...
authorwebkit@devinrousso.com <webkit@devinrousso.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 5 Jan 2018 06:40:30 +0000 (06:40 +0000)
committerwebkit@devinrousso.com <webkit@devinrousso.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 5 Jan 2018 06:40:30 +0000 (06:40 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180770

Reviewed by Joseph Pecoraro.

Source/JavaScriptCore:

* inspector/protocol/Canvas.json:

Source/WebCore:

No change in functionality.

* html/HTMLCanvasElement.h:
* html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::createContext2d):
(WebCore::HTMLCanvasElement::createContextWebGL):
(WebCore::HTMLCanvasElement::createContextWebGPU):
(WebCore::HTMLCanvasElement::createContextBitmapRenderer):
(WebCore::HTMLCanvasElement::reset):
(WebCore::HTMLCanvasElement::paint):
(WebCore::HTMLCanvasElement::setImageBuffer const):
(WebCore::HTMLCanvasElement::addObserver): Deleted.
(WebCore::HTMLCanvasElement::removeObserver): Deleted.
(WebCore::HTMLCanvasElement::cssCanvasClients): Deleted.
(WebCore::HTMLCanvasElement::notifyObserversCanvasChanged): Deleted.
* html/OffscreenCanvas.h:
* html/canvas/CanvasRenderingContext.h:
* html/canvas/CanvasRenderingContext.cpp:
* html/canvas/CanvasRenderingContext2D.h:
* html/canvas/CanvasRenderingContext2D.cpp:
(WebCore::CanvasRenderingContext2D::create):
* html/canvas/CanvasRenderingContext2DBase.h:
* html/canvas/ImageBitmapRenderingContext.h:
* html/canvas/ImageBitmapRenderingContext.cpp:
(WebCore::ImageBitmapRenderingContext::create):
* html/canvas/WebGL2RenderingContext.h:
* html/canvas/WebGL2RenderingContext.cpp:
(WebCore::WebGL2RenderingContext::create):
* html/canvas/WebGLRenderingContext.h:
* html/canvas/WebGLRenderingContext.cpp:
(WebCore::WebGLRenderingContext::create):
* html/canvas/WebGLRenderingContextBase.h:
* html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::create):
* html/canvas/WebGPURenderingContext.cpp:
(WebCore::WebGPURenderingContext::create):
Instead of adding didCreateCanvasRenderingContext calls at the construction sites of each
context, we can make the constructors private and force the usage of static `create` functions.
This way, we have access to the fully constructed object and have a guaranteed path for creation.

* html/CanvasBase.h:
* html/CanvasBase.cpp:
(WebCore::CanvasBase::~CanvasBase):
(WebCore::CanvasBase::renderingContext const):
(WebCore::CanvasBase::addObserver):
(WebCore::CanvasBase::removeObserver):
(WebCore::CanvasBase::notifyObserversCanvasChanged):
(WebCore::CanvasBase::notifyObserversCanvasResized):
(WebCore::CanvasBase::notifyObserversCanvasDestroyed):
(WebCore::CanvasBase::cssCanvasClients const):
* Modules/mediastream/CanvasCaptureMediaStreamTrack.h:
* Modules/mediastream/CanvasCaptureMediaStreamTrack.cpp:
(WebCore::CanvasCaptureMediaStreamTrack::Source::canvasDestroyed):
(WebCore::CanvasCaptureMediaStreamTrack::Source::canvasResized):
(WebCore::CanvasCaptureMediaStreamTrack::Source::canvasChanged):
* css/CSSCanvasValue.h:
Move the CanvasObserver class to CanvasBase so that it can also be used for OffscreenCanvas.

* inspector/InspectorInstrumentation.h:
(WebCore::InspectorInstrumentation::didChangeCSSCanvasClientNodes):
(WebCore::InspectorInstrumentation::didCreateCanvasRenderingContext):
(WebCore::InspectorInstrumentation::didChangeCanvasMemory):
(WebCore::InspectorInstrumentation::recordCanvasAction):
(WebCore::InspectorInstrumentation::didFinishRecordingCanvasFrame):
(WebCore::InspectorInstrumentation::didEnableExtension):
(WebCore::InspectorInstrumentation::didCreateProgram):
(WebCore::InspectorInstrumentation::willDeleteProgram):
(WebCore::InspectorInstrumentation::isShaderProgramDisabled):
(WebCore::InspectorInstrumentation::consoleStartRecordingCanvas):
(WebCore::InspectorInstrumentation::didCreateCSSCanvas): Deleted.
* inspector/InspectorInstrumentation.cpp:
(WebCore::InspectorInstrumentation::consoleStartRecordingCanvasImpl):
(WebCore::InspectorInstrumentation::didChangeCSSCanvasClientNodesImpl):
(WebCore::InspectorInstrumentation::didCreateCanvasRenderingContextImpl):
(WebCore::InspectorInstrumentation::didChangeCanvasMemoryImpl):
(WebCore::InspectorInstrumentation::didFinishRecordingCanvasFrameImpl):
(WebCore::InspectorInstrumentation::didEnableExtensionImpl):
(WebCore::InspectorInstrumentation::didCreateProgramImpl):
(WebCore::InspectorInstrumentation::didCreateCSSCanvasImpl): Deleted.

* inspector/agents/InspectorCanvasAgent.h:
* inspector/agents/InspectorCanvasAgent.cpp:
(WebCore::InspectorCanvasAgent::enable):
(WebCore::InspectorCanvasAgent::requestNode):
(WebCore::InspectorCanvasAgent::requestContent):
(WebCore::InspectorCanvasAgent::requestCSSCanvasClientNodes):
(WebCore::contextAsScriptValue):
(WebCore::InspectorCanvasAgent::resolveCanvasContext):
(WebCore::InspectorCanvasAgent::startRecording):
(WebCore::InspectorCanvasAgent::stopRecording):
(WebCore::InspectorCanvasAgent::updateShader):
(WebCore::InspectorCanvasAgent::frameNavigated):
(WebCore::InspectorCanvasAgent::didChangeCSSCanvasClientNodes):
(WebCore::InspectorCanvasAgent::didCreateCanvasRenderingContext):
(WebCore::InspectorCanvasAgent::didChangeCanvasMemory):
(WebCore::InspectorCanvasAgent::recordCanvasAction):
(WebCore::InspectorCanvasAgent::canvasDestroyed):
(WebCore::InspectorCanvasAgent::didFinishRecordingCanvasFrame):
(WebCore::InspectorCanvasAgent::consoleStartRecordingCanvas):
(WebCore::InspectorCanvasAgent::didEnableExtension):
(WebCore::InspectorCanvasAgent::didCreateProgram):
(WebCore::InspectorCanvasAgent::canvasRecordingTimerFired):
(WebCore::InspectorCanvasAgent::clearCanvasData):
(WebCore::InspectorCanvasAgent::unbindCanvas):
(WebCore::InspectorCanvasAgent::findInspectorCanvas):
(WebCore::InspectorCanvasAgent::unbindProgram):
(WebCore::InspectorCanvasAgent::didCreateCSSCanvas): Deleted.

* inspector/InspectorCanvas.h:
* inspector/InspectorCanvas.cpp:
(WebCore::InspectorCanvas::create):
(WebCore::InspectorCanvas::InspectorCanvas):
(WebCore::InspectorCanvas::canvasElement):
(WebCore::InspectorCanvas::resetRecordingData):
(WebCore::InspectorCanvas::recordAction):
(WebCore::InspectorCanvas::buildObjectForCanvas):
(WebCore::InspectorCanvas::getCanvasContentAsDataURL):
(WebCore::InspectorCanvas::buildInitialState):
(WebCore::InspectorCanvas::~InspectorCanvas): Deleted.

* inspector/InspectorShaderProgram.h:
* inspector/InspectorShaderProgram.cpp:
(WebCore::InspectorShaderProgram::context const):

* page/PageConsoleClient.cpp:
(WebCore::PageConsoleClient::record):
(WebCore::PageConsoleClient::recordEnd):

* dom/Document.h:
* dom/Document.cpp:
(WebCore::Document::getCSSCanvasElement):
(WebCore::Document::nameForCSSCanvasElement const):
We have no reason to save the CSS canvas name for each InspectorCanvas object, so instead we
can just query for the name based on the CanvasRenderingContext's HTMLCanvasElement (assuming
it is not an OffscreenCanvas) when we need it.

Source/WebInspectorUI:

* UserInterface/Models/Canvas.js:
(WI.Canvas.fromPayload):
(WI.Canvas.prototype.get contextType):
(WI.Canvas.prototype.saveIdentityToCookie):
(WI.Canvas.prototype.get frame): Deleted.

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

38 files changed:
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/inspector/protocol/Canvas.json
Source/WebCore/ChangeLog
Source/WebCore/Modules/mediastream/CanvasCaptureMediaStreamTrack.cpp
Source/WebCore/Modules/mediastream/CanvasCaptureMediaStreamTrack.h
Source/WebCore/css/CSSCanvasValue.h
Source/WebCore/dom/Document.cpp
Source/WebCore/dom/Document.h
Source/WebCore/html/CanvasBase.cpp
Source/WebCore/html/CanvasBase.h
Source/WebCore/html/HTMLCanvasElement.cpp
Source/WebCore/html/HTMLCanvasElement.h
Source/WebCore/html/OffscreenCanvas.h
Source/WebCore/html/canvas/CanvasRenderingContext.cpp
Source/WebCore/html/canvas/CanvasRenderingContext.h
Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp
Source/WebCore/html/canvas/CanvasRenderingContext2D.h
Source/WebCore/html/canvas/CanvasRenderingContext2DBase.h
Source/WebCore/html/canvas/ImageBitmapRenderingContext.cpp
Source/WebCore/html/canvas/ImageBitmapRenderingContext.h
Source/WebCore/html/canvas/WebGL2RenderingContext.cpp
Source/WebCore/html/canvas/WebGL2RenderingContext.h
Source/WebCore/html/canvas/WebGLRenderingContext.cpp
Source/WebCore/html/canvas/WebGLRenderingContext.h
Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp
Source/WebCore/html/canvas/WebGLRenderingContextBase.h
Source/WebCore/html/canvas/WebGPURenderingContext.cpp
Source/WebCore/inspector/InspectorCanvas.cpp
Source/WebCore/inspector/InspectorCanvas.h
Source/WebCore/inspector/InspectorInstrumentation.cpp
Source/WebCore/inspector/InspectorInstrumentation.h
Source/WebCore/inspector/InspectorShaderProgram.cpp
Source/WebCore/inspector/InspectorShaderProgram.h
Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp
Source/WebCore/inspector/agents/InspectorCanvasAgent.h
Source/WebCore/page/PageConsoleClient.cpp
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Models/Canvas.js

index 28c17a7..6366755 100644 (file)
@@ -1,3 +1,12 @@
+2018-01-04  Devin Rousso  <webkit@devinrousso.com>
+
+        Web Inspector: replace HTMLCanvasElement with CanvasRenderingContext for instrumentation logic
+        https://bugs.webkit.org/show_bug.cgi?id=180770
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/protocol/Canvas.json:
+
 2018-01-04  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r226405.
index 73f472d..8bab3b5 100644 (file)
@@ -46,7 +46,6 @@
             "properties": [
                 { "name": "canvasId", "$ref": "CanvasId", "description": "Canvas identifier." },
                 { "name": "contextType", "$ref": "ContextType", "description": "The type of rendering context backing the canvas." },
-                { "name": "frameId", "$ref": "Network.FrameId", "description": "Parent frame identifier." },
                 { "name": "nodeId", "$ref": "DOM.NodeId", "optional": true, "description": "The corresponding DOM node id." },
                 { "name": "cssCanvasName", "type": "string", "optional": true, "description": "The CSS canvas identifier, for canvases created with <code>document.getCSSCanvasContext</code>." },
                 { "name": "contextAttributes", "$ref": "ContextAttributes", "optional": true, "description": "Context attributes for rendering contexts." },
index af6cee8..b46b4e0 100644 (file)
@@ -1,3 +1,146 @@
+2018-01-04  Devin Rousso  <webkit@devinrousso.com>
+
+        Web Inspector: replace HTMLCanvasElement with CanvasRenderingContext for instrumentation logic
+        https://bugs.webkit.org/show_bug.cgi?id=180770
+
+        Reviewed by Joseph Pecoraro.
+
+        No change in functionality.
+
+        * html/HTMLCanvasElement.h:
+        * html/HTMLCanvasElement.cpp:
+        (WebCore::HTMLCanvasElement::createContext2d):
+        (WebCore::HTMLCanvasElement::createContextWebGL):
+        (WebCore::HTMLCanvasElement::createContextWebGPU):
+        (WebCore::HTMLCanvasElement::createContextBitmapRenderer):
+        (WebCore::HTMLCanvasElement::reset):
+        (WebCore::HTMLCanvasElement::paint):
+        (WebCore::HTMLCanvasElement::setImageBuffer const):
+        (WebCore::HTMLCanvasElement::addObserver): Deleted.
+        (WebCore::HTMLCanvasElement::removeObserver): Deleted.
+        (WebCore::HTMLCanvasElement::cssCanvasClients): Deleted.
+        (WebCore::HTMLCanvasElement::notifyObserversCanvasChanged): Deleted.
+        * html/OffscreenCanvas.h:
+        * html/canvas/CanvasRenderingContext.h:
+        * html/canvas/CanvasRenderingContext.cpp:
+        * html/canvas/CanvasRenderingContext2D.h:
+        * html/canvas/CanvasRenderingContext2D.cpp:
+        (WebCore::CanvasRenderingContext2D::create):
+        * html/canvas/CanvasRenderingContext2DBase.h:
+        * html/canvas/ImageBitmapRenderingContext.h:
+        * html/canvas/ImageBitmapRenderingContext.cpp:
+        (WebCore::ImageBitmapRenderingContext::create):
+        * html/canvas/WebGL2RenderingContext.h:
+        * html/canvas/WebGL2RenderingContext.cpp:
+        (WebCore::WebGL2RenderingContext::create):
+        * html/canvas/WebGLRenderingContext.h:
+        * html/canvas/WebGLRenderingContext.cpp:
+        (WebCore::WebGLRenderingContext::create):
+        * html/canvas/WebGLRenderingContextBase.h:
+        * html/canvas/WebGLRenderingContextBase.cpp:
+        (WebCore::WebGLRenderingContextBase::create):
+        * html/canvas/WebGPURenderingContext.cpp:
+        (WebCore::WebGPURenderingContext::create):
+        Instead of adding didCreateCanvasRenderingContext calls at the construction sites of each
+        context, we can make the constructors private and force the usage of static `create` functions.
+        This way, we have access to the fully constructed object and have a guaranteed path for creation.
+
+        * html/CanvasBase.h:
+        * html/CanvasBase.cpp:
+        (WebCore::CanvasBase::~CanvasBase):
+        (WebCore::CanvasBase::renderingContext const):
+        (WebCore::CanvasBase::addObserver):
+        (WebCore::CanvasBase::removeObserver):
+        (WebCore::CanvasBase::notifyObserversCanvasChanged):
+        (WebCore::CanvasBase::notifyObserversCanvasResized):
+        (WebCore::CanvasBase::notifyObserversCanvasDestroyed):
+        (WebCore::CanvasBase::cssCanvasClients const):
+        * Modules/mediastream/CanvasCaptureMediaStreamTrack.h:
+        * Modules/mediastream/CanvasCaptureMediaStreamTrack.cpp:
+        (WebCore::CanvasCaptureMediaStreamTrack::Source::canvasDestroyed):
+        (WebCore::CanvasCaptureMediaStreamTrack::Source::canvasResized):
+        (WebCore::CanvasCaptureMediaStreamTrack::Source::canvasChanged):
+        * css/CSSCanvasValue.h:
+        Move the CanvasObserver class to CanvasBase so that it can also be used for OffscreenCanvas.
+
+        * inspector/InspectorInstrumentation.h:
+        (WebCore::InspectorInstrumentation::didChangeCSSCanvasClientNodes):
+        (WebCore::InspectorInstrumentation::didCreateCanvasRenderingContext):
+        (WebCore::InspectorInstrumentation::didChangeCanvasMemory):
+        (WebCore::InspectorInstrumentation::recordCanvasAction):
+        (WebCore::InspectorInstrumentation::didFinishRecordingCanvasFrame):
+        (WebCore::InspectorInstrumentation::didEnableExtension):
+        (WebCore::InspectorInstrumentation::didCreateProgram):
+        (WebCore::InspectorInstrumentation::willDeleteProgram):
+        (WebCore::InspectorInstrumentation::isShaderProgramDisabled):
+        (WebCore::InspectorInstrumentation::consoleStartRecordingCanvas):
+        (WebCore::InspectorInstrumentation::didCreateCSSCanvas): Deleted.
+        * inspector/InspectorInstrumentation.cpp:
+        (WebCore::InspectorInstrumentation::consoleStartRecordingCanvasImpl):
+        (WebCore::InspectorInstrumentation::didChangeCSSCanvasClientNodesImpl):
+        (WebCore::InspectorInstrumentation::didCreateCanvasRenderingContextImpl):
+        (WebCore::InspectorInstrumentation::didChangeCanvasMemoryImpl):
+        (WebCore::InspectorInstrumentation::didFinishRecordingCanvasFrameImpl):
+        (WebCore::InspectorInstrumentation::didEnableExtensionImpl):
+        (WebCore::InspectorInstrumentation::didCreateProgramImpl):
+        (WebCore::InspectorInstrumentation::didCreateCSSCanvasImpl): Deleted.
+
+        * inspector/agents/InspectorCanvasAgent.h:
+        * inspector/agents/InspectorCanvasAgent.cpp:
+        (WebCore::InspectorCanvasAgent::enable):
+        (WebCore::InspectorCanvasAgent::requestNode):
+        (WebCore::InspectorCanvasAgent::requestContent):
+        (WebCore::InspectorCanvasAgent::requestCSSCanvasClientNodes):
+        (WebCore::contextAsScriptValue):
+        (WebCore::InspectorCanvasAgent::resolveCanvasContext):
+        (WebCore::InspectorCanvasAgent::startRecording):
+        (WebCore::InspectorCanvasAgent::stopRecording):
+        (WebCore::InspectorCanvasAgent::updateShader):
+        (WebCore::InspectorCanvasAgent::frameNavigated):
+        (WebCore::InspectorCanvasAgent::didChangeCSSCanvasClientNodes):
+        (WebCore::InspectorCanvasAgent::didCreateCanvasRenderingContext):
+        (WebCore::InspectorCanvasAgent::didChangeCanvasMemory):
+        (WebCore::InspectorCanvasAgent::recordCanvasAction):
+        (WebCore::InspectorCanvasAgent::canvasDestroyed):
+        (WebCore::InspectorCanvasAgent::didFinishRecordingCanvasFrame):
+        (WebCore::InspectorCanvasAgent::consoleStartRecordingCanvas):
+        (WebCore::InspectorCanvasAgent::didEnableExtension):
+        (WebCore::InspectorCanvasAgent::didCreateProgram):
+        (WebCore::InspectorCanvasAgent::canvasRecordingTimerFired):
+        (WebCore::InspectorCanvasAgent::clearCanvasData):
+        (WebCore::InspectorCanvasAgent::unbindCanvas):
+        (WebCore::InspectorCanvasAgent::findInspectorCanvas):
+        (WebCore::InspectorCanvasAgent::unbindProgram):
+        (WebCore::InspectorCanvasAgent::didCreateCSSCanvas): Deleted.
+
+        * inspector/InspectorCanvas.h:
+        * inspector/InspectorCanvas.cpp:
+        (WebCore::InspectorCanvas::create):
+        (WebCore::InspectorCanvas::InspectorCanvas):
+        (WebCore::InspectorCanvas::canvasElement):
+        (WebCore::InspectorCanvas::resetRecordingData):
+        (WebCore::InspectorCanvas::recordAction):
+        (WebCore::InspectorCanvas::buildObjectForCanvas):
+        (WebCore::InspectorCanvas::getCanvasContentAsDataURL):
+        (WebCore::InspectorCanvas::buildInitialState):
+        (WebCore::InspectorCanvas::~InspectorCanvas): Deleted.
+
+        * inspector/InspectorShaderProgram.h:
+        * inspector/InspectorShaderProgram.cpp:
+        (WebCore::InspectorShaderProgram::context const):
+
+        * page/PageConsoleClient.cpp:
+        (WebCore::PageConsoleClient::record):
+        (WebCore::PageConsoleClient::recordEnd):
+
+        * dom/Document.h:
+        * dom/Document.cpp:
+        (WebCore::Document::getCSSCanvasElement):
+        (WebCore::Document::nameForCSSCanvasElement const):
+        We have no reason to save the CSS canvas name for each InspectorCanvas object, so instead we
+        can just query for the name based on the CanvasRenderingContext's HTMLCanvasElement (assuming
+        it is not an OffscreenCanvas) when we need it.
+
 2018-01-04  Chris Fleizach  <cfleizach@apple.com>
 
         AX: Implement updated CSS3 Speech for 'speak' and 'speak-as' properties
index 91d8b3a..e75072c 100644 (file)
@@ -26,6 +26,7 @@
 #include "CanvasCaptureMediaStreamTrack.h"
 
 #include "GraphicsContext.h"
+#include "HTMLCanvasElement.h"
 #include "WebGLRenderingContextBase.h"
 
 #if ENABLE(MEDIA_STREAM)
@@ -101,7 +102,7 @@ void CanvasCaptureMediaStreamTrack::Source::requestFrameTimerFired()
     requestFrame();
 }
 
-void CanvasCaptureMediaStreamTrack::Source::canvasDestroyed(HTMLCanvasElement& canvas)
+void CanvasCaptureMediaStreamTrack::Source::canvasDestroyed(CanvasBase& canvas)
 {
     ASSERT_UNUSED(canvas, m_canvas == &canvas);
 
@@ -109,7 +110,7 @@ void CanvasCaptureMediaStreamTrack::Source::canvasDestroyed(HTMLCanvasElement& c
     m_canvas = nullptr;
 }
 
-void CanvasCaptureMediaStreamTrack::Source::canvasResized(HTMLCanvasElement& canvas)
+void CanvasCaptureMediaStreamTrack::Source::canvasResized(CanvasBase& canvas)
 {
     ASSERT_UNUSED(canvas, m_canvas == &canvas);
 
@@ -119,7 +120,7 @@ void CanvasCaptureMediaStreamTrack::Source::canvasResized(HTMLCanvasElement& can
     settingsDidChange();
 }
 
-void CanvasCaptureMediaStreamTrack::Source::canvasChanged(HTMLCanvasElement& canvas, const FloatRect&)
+void CanvasCaptureMediaStreamTrack::Source::canvasChanged(CanvasBase& canvas, const FloatRect&)
 {
     ASSERT_UNUSED(canvas, m_canvas == &canvas);
 
@@ -129,7 +130,7 @@ void CanvasCaptureMediaStreamTrack::Source::canvasChanged(HTMLCanvasElement& can
     if (is<WebGLRenderingContextBase>(canvas.renderingContext())) {
         auto& context = downcast<WebGLRenderingContextBase>(*canvas.renderingContext());
         if (!context.isPreservingDrawingBuffer()) {
-            canvas.document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("Turning drawing buffer preservation for the WebGL canvas being captured"));
+            canvas.scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("Turning drawing buffer preservation for the WebGL canvas being captured"));
             context.setPreserveDrawingBuffer(true);
         }
     }
index d1cc731..635b4b1 100644 (file)
@@ -26,7 +26,7 @@
 
 #if ENABLE(MEDIA_STREAM)
 
-#include "HTMLCanvasElement.h"
+#include "CanvasBase.h"
 #include "MediaStreamTrack.h"
 #include "Timer.h"
 #include <wtf/TypeCasts.h>
@@ -58,9 +58,9 @@ private:
         Source(HTMLCanvasElement&, std::optional<double>&&);
 
         // CanvasObserver API
-        void canvasChanged(HTMLCanvasElement&, const FloatRect&) final;
-        void canvasResized(HTMLCanvasElement&) final;
-        void canvasDestroyed(HTMLCanvasElement&) final;
+        void canvasChanged(CanvasBase&, const FloatRect&) final;
+        void canvasResized(CanvasBase&) final;
+        void canvasDestroyed(CanvasBase&) final;
 
         // RealtimeMediaSource API
         void startProducingData() final;
index 83b1625..74f8762 100644 (file)
 #pragma once
 
 #include "CSSImageGeneratorValue.h"
+#include "CanvasBase.h"
 #include "HTMLCanvasElement.h"
+#include "RenderElement.h"
 
 namespace WebCore {
 
 class Document;
-class HTMLCanvasElement;
 
 class CSSCanvasValue final : public CSSImageGeneratorValue {
 public:
@@ -60,26 +61,25 @@ public:
         {
         }
 
-        virtual ~CanvasObserverProxy()
-        {
-        }
-
         bool isCanvasObserverProxy() const final { return true; }
 
         const CSSCanvasValue& ownerValue() const { return m_ownerValue; }
 
     private:
-        void canvasChanged(HTMLCanvasElement& canvas, const FloatRect& changedRect) final
+        void canvasChanged(CanvasBase& canvasBase, const FloatRect& changedRect) final
         {
-            m_ownerValue.canvasChanged(canvas, changedRect);
+            ASSERT(is<HTMLCanvasElement>(canvasBase));
+            m_ownerValue.canvasChanged(downcast<HTMLCanvasElement>(canvasBase), changedRect);
         }
-        void canvasResized(HTMLCanvasElement& canvas) final
+        void canvasResized(CanvasBase& canvasBase) final
         {
-            m_ownerValue.canvasResized(canvas);
+            ASSERT(is<HTMLCanvasElement>(canvasBase));
+            m_ownerValue.canvasResized(downcast<HTMLCanvasElement>(canvasBase));
         }
-        void canvasDestroyed(HTMLCanvasElement& canvas) final
+        void canvasDestroyed(CanvasBase& canvasBase) final
         {
-            m_ownerValue.canvasDestroyed(canvas);
+            ASSERT(is<HTMLCanvasElement>(canvasBase));
+            m_ownerValue.canvasDestroyed(downcast<HTMLCanvasElement>(canvasBase));
         }
 
         CSSCanvasValue& m_ownerValue;
index 0e6c6b0..db04736 100644 (file)
@@ -5699,13 +5699,20 @@ std::optional<RenderingContext> Document::getCSSCanvasContext(const String& type
 HTMLCanvasElement* Document::getCSSCanvasElement(const String& name)
 {
     RefPtr<HTMLCanvasElement>& element = m_cssCanvasElements.add(name, nullptr).iterator->value;
-    if (!element) {
+    if (!element)
         element = HTMLCanvasElement::create(*this);
-        InspectorInstrumentation::didCreateCSSCanvas(*element, name);
-    }
     return element.get();
 }
 
+String Document::nameForCSSCanvasElement(const HTMLCanvasElement& canvasElement) const
+{
+    for (const auto& entry : m_cssCanvasElements) {
+        if (entry.value.get() == &canvasElement)
+            return entry.key;
+    }
+    return String();
+}
+
 #if ENABLE(TEXT_AUTOSIZING)
 TextAutoSizing& Document::textAutoSizing()
 {
index ebabd9b..4d2ed6b 100644 (file)
@@ -988,6 +988,7 @@ public:
     // Extension for manipulating canvas drawing contexts for use in CSS
     std::optional<RenderingContext> getCSSCanvasContext(const String& type, const String& name, int width, int height);
     HTMLCanvasElement* getCSSCanvasElement(const String& name);
+    String nameForCSSCanvasElement(const HTMLCanvasElement&) const;
 
     bool isDNSPrefetchEnabled() const { return m_isDNSPrefetchEnabled; }
     void parseDNSPrefetchControlHeader(const String&);
index ecb13bb..85939b2 100644 (file)
 #include "config.h"
 #include "CanvasBase.h"
 
-#include "HTMLCanvasElement.h"
+#include "CSSCanvasValue.h"
+#include "CanvasRenderingContext.h"
+#include "Element.h"
+#include "FloatRect.h"
+#include "InspectorInstrumentation.h"
 
 namespace WebCore {
 
@@ -35,4 +39,66 @@ CanvasBase::CanvasBase(ScriptExecutionContext* scriptExecutionContext)
 {
 }
 
+CanvasBase::~CanvasBase()
+{
+    notifyObserversCanvasDestroyed();
+}
+
+CanvasRenderingContext* CanvasBase::renderingContext() const
+{
+    return m_context.get();
+}
+
+void CanvasBase::addObserver(CanvasObserver& observer)
+{
+    m_observers.add(&observer);
+
+    if (is<CSSCanvasValue::CanvasObserverProxy>(observer))
+        InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
+}
+
+void CanvasBase::removeObserver(CanvasObserver& observer)
+{
+    m_observers.remove(&observer);
+
+    if (is<CSSCanvasValue::CanvasObserverProxy>(observer))
+        InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
+}
+
+void CanvasBase::notifyObserversCanvasChanged(const FloatRect& rect)
+{
+    for (auto& observer : m_observers)
+        observer->canvasChanged(*this, rect);
+}
+
+void CanvasBase::notifyObserversCanvasResized()
+{
+    for (auto& observer : m_observers)
+        observer->canvasResized(*this);
+}
+
+void CanvasBase::notifyObserversCanvasDestroyed()
+{
+    for (auto& observer : m_observers)
+        observer->canvasDestroyed(*this);
+
+    m_observers.clear();
+}
+
+HashSet<Element*> CanvasBase::cssCanvasClients() const
+{
+    HashSet<Element*> cssCanvasClients;
+    for (auto& observer : m_observers) {
+        if (!is<CSSCanvasValue::CanvasObserverProxy>(observer))
+            continue;
+
+        auto clients = downcast<CSSCanvasValue::CanvasObserverProxy>(observer)->ownerValue().clients();
+        for (auto& entry : clients) {
+            if (RefPtr<Element> element = entry.key->element())
+                cssCanvasClients.add(element.get());
+        }
+    }
+    return cssCanvasClients;
+}
+
 }
index 618a36f..5dfe293 100644 (file)
 
 #pragma once
 
+#include <wtf/HashSet.h>
 #include <wtf/TypeCasts.h>
 
 namespace WebCore {
 
-class HTMLCanvasElement;
+class CanvasBase;
+class CanvasRenderingContext;
+class Element;
 class IntSize;
+class FloatRect;
 class ScriptExecutionContext;
 class SecurityOrigin;
 
+class CanvasObserver {
+public:
+    virtual ~CanvasObserver() = default;
+
+    virtual bool isCanvasObserverProxy() const { return false; }
+
+    virtual void canvasChanged(CanvasBase&, const FloatRect& changedRect) = 0;
+    virtual void canvasResized(CanvasBase&) = 0;
+    virtual void canvasDestroyed(CanvasBase&) = 0;
+};
+
 class CanvasBase {
 public:
-    virtual ~CanvasBase() = default;
+    virtual ~CanvasBase();
 
     virtual void refCanvasBase() = 0;
     virtual void derefCanvasBase() = 0;
@@ -56,12 +71,25 @@ public:
     virtual SecurityOrigin* securityOrigin() const { return nullptr; }
     ScriptExecutionContext* scriptExecutionContext() const { return m_scriptExecutionContext; }
 
+    CanvasRenderingContext* renderingContext() const;
+
+    void addObserver(CanvasObserver&);
+    void removeObserver(CanvasObserver&);
+    void notifyObserversCanvasChanged(const FloatRect&);
+    void notifyObserversCanvasResized();
+    void notifyObserversCanvasDestroyed();
+
+    HashSet<Element*> cssCanvasClients() const;
+
 protected:
     CanvasBase(ScriptExecutionContext*);
 
+    std::unique_ptr<CanvasRenderingContext> m_context;
+
 private:
     bool m_originClean { true };
     ScriptExecutionContext* m_scriptExecutionContext;
+    HashSet<CanvasObserver*> m_observers;
 };
 
 } // namespace WebCore
index 2038f32..5970094 100644 (file)
@@ -30,7 +30,6 @@
 
 #include "Blob.h"
 #include "BlobCallback.h"
-#include "CSSCanvasValue.h"
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
 #include "CanvasRenderingContext2D.h"
@@ -140,8 +139,7 @@ static void removeFromActivePixelMemory(size_t pixelsReleased)
     
 HTMLCanvasElement::~HTMLCanvasElement()
 {
-    for (auto& observer : m_observers)
-        observer->canvasDestroyed(*this);
+    notifyObserversCanvasDestroyed();
 
     m_context = nullptr; // Ensure this goes away before the ImageBuffer.
 
@@ -173,38 +171,6 @@ bool HTMLCanvasElement::canStartSelection() const
     return false;
 }
 
-void HTMLCanvasElement::addObserver(CanvasObserver& observer)
-{
-    m_observers.add(&observer);
-
-    if (is<CSSCanvasValue::CanvasObserverProxy>(observer))
-        InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
-}
-
-void HTMLCanvasElement::removeObserver(CanvasObserver& observer)
-{
-    m_observers.remove(&observer);
-
-    if (is<CSSCanvasValue::CanvasObserverProxy>(observer))
-        InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
-}
-
-HashSet<Element*> HTMLCanvasElement::cssCanvasClients() const
-{
-    HashSet<Element*> cssCanvasClients;
-    for (auto& observer : m_observers) {
-        if (!is<CSSCanvasValue::CanvasObserverProxy>(observer))
-            continue;
-
-        auto clients = downcast<CSSCanvasValue::CanvasObserverProxy>(observer)->ownerValue().clients();
-        for (auto& entry : clients) {
-            if (RefPtr<Element> element = entry.key->element())
-                cssCanvasClients.add(element.get());
-        }
-    }
-    return cssCanvasClients;
-}
-
 ExceptionOr<void> HTMLCanvasElement::setHeight(unsigned value)
 {
     if (m_context && m_context->isPlaceholder())
@@ -370,13 +336,11 @@ CanvasRenderingContext2D* HTMLCanvasElement::createContext2d(const String& type)
         return nullptr;
     }
 
-    m_context = std::make_unique<CanvasRenderingContext2D>(*this, document().inQuirksMode(), usesDashboardCompatibilityMode);
+    m_context = CanvasRenderingContext2D::create(*this, document().inQuirksMode(), usesDashboardCompatibilityMode);
 
     downcast<CanvasRenderingContext2D>(*m_context).setUsesDisplayListDrawing(m_usesDisplayListDrawing);
     downcast<CanvasRenderingContext2D>(*m_context).setTracksDisplayListReplay(m_tracksDisplayListReplay);
 
-    InspectorInstrumentation::didCreateCanvasRenderingContext(*this);
-
 #if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
     // Need to make sure a RenderLayer and compositing layer get created for the Canvas.
     invalidateStyleAndLayerComposition();
@@ -441,8 +405,6 @@ WebGLRenderingContextBase* HTMLCanvasElement::createContextWebGL(const String& t
     if (m_context) {
         // Need to make sure a RenderLayer and compositing layer get created for the Canvas.
         invalidateStyleAndLayerComposition();
-
-        InspectorInstrumentation::didCreateCanvasRenderingContext(*this);
     }
 
     return downcast<WebGLRenderingContextBase>(m_context.get());
@@ -484,8 +446,6 @@ WebGPURenderingContext* HTMLCanvasElement::createContextWebGPU(const String& typ
     if (m_context) {
         // Need to make sure a RenderLayer and compositing layer get created for the Canvas.
         invalidateStyleAndLayerComposition();
-
-        InspectorInstrumentation::didCreateCanvasRenderingContext(*this);
     }
 
     return static_cast<WebGPURenderingContext*>(m_context.get());
@@ -517,9 +477,7 @@ ImageBitmapRenderingContext* HTMLCanvasElement::createContextBitmapRenderer(cons
     ASSERT_UNUSED(type, HTMLCanvasElement::isBitmapRendererType(type));
     ASSERT(!m_context);
 
-    m_context = std::make_unique<ImageBitmapRenderingContext>(*this, WTFMove(settings));
-
-    InspectorInstrumentation::didCreateCanvasRenderingContext(*this);
+    m_context = ImageBitmapRenderingContext::create(*this, WTFMove(settings));
 
 #if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
     // Need to make sure a RenderLayer and compositing layer get created for the Canvas.
@@ -557,12 +515,6 @@ void HTMLCanvasElement::didDraw(const FloatRect& rect)
     notifyObserversCanvasChanged(dirtyRect);
 }
 
-void HTMLCanvasElement::notifyObserversCanvasChanged(const FloatRect& rect)
-{
-    for (auto& observer : m_observers)
-        observer->canvasChanged(*this, rect);
-}
-
 void HTMLCanvasElement::reset()
 {
     if (m_ignoreReset)
@@ -609,8 +561,7 @@ void HTMLCanvasElement::reset()
             canvasRenderer.repaint();
     }
 
-    for (auto& observer : m_observers)
-        observer->canvasResized(*this);
+    notifyObserversCanvasResized();
 }
 
 bool HTMLCanvasElement::paintsIntoCanvasBuffer() const
@@ -634,7 +585,7 @@ bool HTMLCanvasElement::paintsIntoCanvasBuffer() const
 void HTMLCanvasElement::paint(GraphicsContext& context, const LayoutRect& r)
 {
     if (UNLIKELY(m_context && m_context->callTracingActive()))
-        InspectorInstrumentation::didFinishRecordingCanvasFrame(*this);
+        InspectorInstrumentation::didFinishRecordingCanvasFrame(*m_context);
 
     // Clear the dirty rect
     m_dirtyRect = FloatRect();
@@ -989,8 +940,8 @@ void HTMLCanvasElement::setImageBuffer(std::unique_ptr<ImageBuffer>&& buffer) co
     size_t currentMemoryCost = memoryCost();
     activePixelMemory += currentMemoryCost;
 
-    if (m_imageBuffer && previousMemoryCost != currentMemoryCost)
-        InspectorInstrumentation::didChangeCanvasMemory(const_cast<HTMLCanvasElement&>(*this));
+    if (m_context && m_imageBuffer && previousMemoryCost != currentMemoryCost)
+        InspectorInstrumentation::didChangeCanvasMemory(*m_context);
 }
 
 void HTMLCanvasElement::setImageBufferAndMarkDirty(std::unique_ptr<ImageBuffer>&& buffer)
index dc98a2f..f0bcd64 100644 (file)
@@ -34,7 +34,6 @@
 #include "IntSize.h"
 #include <memory>
 #include <wtf/Forward.h>
-#include <wtf/HashSet.h>
 
 #if ENABLE(WEBGL)
 #include "WebGLContextAttributes.h"
@@ -44,10 +43,8 @@ namespace WebCore {
 
 class BlobCallback;
 class CanvasRenderingContext2D;
-class CanvasRenderingContext;
 class GraphicsContext;
 class GraphicsContextStateSaver;
-class HTMLCanvasElement;
 class Image;
 class ImageBuffer;
 class ImageData;
@@ -61,27 +58,12 @@ namespace DisplayList {
 using AsTextFlags = unsigned;
 }
 
-class CanvasObserver {
-public:
-    virtual ~CanvasObserver() = default;
-
-    virtual bool isCanvasObserverProxy() const { return false; }
-
-    virtual void canvasChanged(HTMLCanvasElement&, const FloatRect& changedRect) = 0;
-    virtual void canvasResized(HTMLCanvasElement&) = 0;
-    virtual void canvasDestroyed(HTMLCanvasElement&) = 0;
-};
-
 class HTMLCanvasElement final : public HTMLElement, public CanvasBase {
 public:
     static Ref<HTMLCanvasElement> create(Document&);
     static Ref<HTMLCanvasElement> create(const QualifiedName&, Document&);
     virtual ~HTMLCanvasElement();
 
-    void addObserver(CanvasObserver&);
-    void removeObserver(CanvasObserver&);
-    HashSet<Element*> cssCanvasClients() const;
-
     unsigned width() const final { return size().width(); }
     unsigned height() const final { return size().height(); }
 
@@ -130,15 +112,12 @@ public:
 
     // Used for rendering
     void didDraw(const FloatRect&);
-    void notifyObserversCanvasChanged(const FloatRect&);
 
     void paint(GraphicsContext&, const LayoutRect&);
 
     GraphicsContext* drawingContext() const;
     GraphicsContext* existingDrawingContext() const;
 
-    CanvasRenderingContext* renderingContext() const { return m_context.get(); }
-
 #if ENABLE(MEDIA_STREAM)
     RefPtr<MediaSample> toMediaSample();
     ExceptionOr<Ref<MediaStream>> captureStream(ScriptExecutionContext&, std::optional<double>&& frameRequestRate);
@@ -199,9 +178,6 @@ private:
     void refCanvasBase() final { HTMLElement::ref(); }
     void derefCanvasBase() final { HTMLElement::deref(); }
 
-    HashSet<CanvasObserver*> m_observers;
-    std::unique_ptr<CanvasRenderingContext> m_context;
-
     FloatRect m_dirtyRect;
     mutable IntSize m_size;
 
index e72c2a1..c272ed6 100644 (file)
@@ -26,7 +26,6 @@
 #pragma once
 
 #include "CanvasBase.h"
-#include "CanvasRenderingContext.h"
 #include "EventTarget.h"
 #include "ExceptionOr.h"
 #include "IntSize.h"
@@ -60,7 +59,7 @@ public:
     };
 
     static Ref<OffscreenCanvas> create(ScriptExecutionContext&, unsigned width, unsigned height);
-    ~OffscreenCanvas();
+    virtual ~OffscreenCanvas();
 
     unsigned width() const final;
     void setWidth(unsigned);
@@ -95,7 +94,6 @@ private:
     void derefCanvasBase() final { deref(); }
 
     IntSize m_size;
-    std::unique_ptr<CanvasRenderingContext> m_context;
 };
 
 }
index e76bd2b..2a6c768 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "CachedImage.h"
 #include "CanvasPattern.h"
+#include "HTMLCanvasElement.h"
 #include "HTMLImageElement.h"
 #include "HTMLVideoElement.h"
 #include "Image.h"
index 80cd95e..5a7d2a1 100644 (file)
@@ -25,8 +25,8 @@
 
 #pragma once
 
+#include "CanvasBase.h"
 #include "GraphicsLayer.h"
-#include "HTMLCanvasElement.h"
 #include "ScriptWrappable.h"
 #include <wtf/Noncopyable.h>
 #include <wtf/text/StringHash.h>
index aecf56b..9a66da7 100644 (file)
@@ -42,6 +42,7 @@
 #include "ImageBitmap.h"
 #include "ImageBuffer.h"
 #include "ImageData.h"
+#include "InspectorInstrumentation.h"
 #include "Path2D.h"
 #include "RenderElement.h"
 #include "RenderImage.h"
@@ -67,6 +68,15 @@ namespace WebCore {
 
 using namespace HTMLNames;
 
+std::unique_ptr<CanvasRenderingContext2D> CanvasRenderingContext2D::create(CanvasBase& canvas, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode)
+{
+    auto renderingContext = std::unique_ptr<CanvasRenderingContext2D>(new CanvasRenderingContext2D(canvas, usesCSSCompatibilityParseMode, usesDashboardCompatibilityMode));
+
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
+    return renderingContext;
+}
+
 CanvasRenderingContext2D::CanvasRenderingContext2D(CanvasBase& canvas, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode)
     : CanvasRenderingContext2DBase(canvas, usesCSSCompatibilityParseMode, usesDashboardCompatibilityMode)
 {
index dffb5a7..e67160a 100644 (file)
@@ -30,6 +30,8 @@
 #include "CanvasTextBaseline.h"
 #include "FontCascade.h"
 #include "FontSelectorClient.h"
+#include "HTMLCanvasElement.h"
+#include <memory>
 
 namespace WebCore {
 
@@ -37,7 +39,8 @@ class TextMetrics;
 
 class CanvasRenderingContext2D final : public CanvasRenderingContext2DBase {
 public:
-    CanvasRenderingContext2D(CanvasBase&, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode);
+    static std::unique_ptr<CanvasRenderingContext2D> create(CanvasBase&, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode);
+
     virtual ~CanvasRenderingContext2D();
 
     HTMLCanvasElement& canvas() const { return downcast<HTMLCanvasElement>(canvasBase()); }
@@ -66,6 +69,8 @@ public:
     bool is2d() const override { return true; }
 
 private:
+    CanvasRenderingContext2D(CanvasBase&, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode);
+
     // The relationship between FontCascade and CanvasRenderingContext2D::FontProxy must hold certain invariants.
     // Therefore, all font operations must pass through the State.
     const FontProxy& fontProxy();
index f75e454..e572fdf 100644 (file)
@@ -61,6 +61,7 @@ class HTMLVideoElement;
 class ImageBitmap;
 class ImageData;
 class Path2D;
+class RenderStyle;
 class TextMetrics;
 
 struct DOMMatrix2DInit;
index 9b2dd05..84899fa 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "ImageBitmap.h"
 #include "ImageBuffer.h"
+#include "InspectorInstrumentation.h"
 
 namespace WebCore {
 
@@ -37,6 +38,15 @@ static RenderingMode bufferRenderingMode = Accelerated;
 static RenderingMode bufferRenderingMode = Unaccelerated;
 #endif
 
+std::unique_ptr<ImageBitmapRenderingContext> ImageBitmapRenderingContext::create(CanvasBase& canvas, ImageBitmapRenderingContextSettings&& settings)
+{
+    auto renderingContext = std::unique_ptr<ImageBitmapRenderingContext>(new ImageBitmapRenderingContext(canvas, WTFMove(settings)));
+
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
+    return renderingContext;
+}
+
 ImageBitmapRenderingContext::ImageBitmapRenderingContext(CanvasBase& canvas, ImageBitmapRenderingContextSettings&& settings)
     : CanvasRenderingContext(canvas)
     , m_settings(WTFMove(settings))
index 6887bc0..257926b 100644 (file)
@@ -30,6 +30,7 @@
 #include "ExceptionOr.h"
 #include "ImageBitmapRenderingContextSettings.h"
 #include "ImageBufferData.h"
+#include <memory>
 #include <wtf/RefPtr.h>
 
 namespace WebCore {
@@ -39,13 +40,13 @@ class ImageBuffer;
 
 class ImageBitmapRenderingContext final : public CanvasRenderingContext {
 public:
+    static std::unique_ptr<ImageBitmapRenderingContext> create(CanvasBase&, ImageBitmapRenderingContextSettings&&);
 
     enum class BitmapMode {
         Valid,
         Blank
     };
 
-    ImageBitmapRenderingContext(CanvasBase&, ImageBitmapRenderingContextSettings&&);
     ~ImageBitmapRenderingContext();
 
     HTMLCanvasElement* canvas() const;
@@ -56,6 +57,8 @@ public:
     bool hasAlpha() { return m_settings.alpha; }
 
 private:
+    ImageBitmapRenderingContext(CanvasBase&, ImageBitmapRenderingContextSettings&&);
+
     bool isBitmapRenderer() const final { return true; }
     bool isAccelerated() const override;
 
index b969325..b24a0fa 100644 (file)
 
 namespace WebCore {
 
+std::unique_ptr<WebGL2RenderingContext> WebGL2RenderingContext::create(CanvasBase& canvas, GraphicsContext3DAttributes attributes)
+{
+    auto renderingContext = std::unique_ptr<WebGL2RenderingContext>(new WebGL2RenderingContext(canvas, attributes));
+
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
+    return renderingContext;
+}
+
+std::unique_ptr<WebGL2RenderingContext> WebGL2RenderingContext::create(CanvasBase& canvas, Ref<GraphicsContext3D>&& context, GraphicsContext3DAttributes attributes)
+{
+    auto renderingContext = std::unique_ptr<WebGL2RenderingContext>(new WebGL2RenderingContext(canvas, WTFMove(context), attributes));
+
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
+    return renderingContext;
+}
+
 WebGL2RenderingContext::WebGL2RenderingContext(CanvasBase& canvas, GraphicsContext3DAttributes attributes)
     : WebGLRenderingContextBase(canvas, attributes)
 {
index a1760d9..655285d 100644 (file)
@@ -28,6 +28,7 @@
 #if ENABLE(WEBGL2)
 
 #include "WebGLRenderingContextBase.h"
+#include <memory>
 
 namespace WebCore {
 
@@ -39,8 +40,8 @@ class WebGLVertexArrayObject;
 
 class WebGL2RenderingContext final : public WebGLRenderingContextBase {
 public:
-    WebGL2RenderingContext(CanvasBase&, WebGLContextAttributes);
-    WebGL2RenderingContext(CanvasBase&, Ref<GraphicsContext3D>&&, WebGLContextAttributes);
+    static std::unique_ptr<WebGL2RenderingContext> create(CanvasBase&, GraphicsContext3DAttributes);
+    static std::unique_ptr<WebGL2RenderingContext> create(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes);
 
     // Buffer objects
     using WebGLRenderingContextBase::bufferData;
@@ -204,6 +205,9 @@ public:
     void hint(GC3Denum target, GC3Denum mode) final;
 
 private:
+    WebGL2RenderingContext(CanvasBase&, GraphicsContext3DAttributes);
+    WebGL2RenderingContext(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes);
+
     bool isWebGL2() const final { return true; }
 
     void initializeVertexArrayObjects() final;
index f4f948e..6dcd7d4 100644 (file)
 
 namespace WebCore {
 
+std::unique_ptr<WebGLRenderingContext> WebGLRenderingContext::create(CanvasBase& canvas, GraphicsContext3DAttributes attributes)
+{
+    auto renderingContext = std::unique_ptr<WebGLRenderingContext>(new WebGLRenderingContext(canvas, attributes));
+
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
+    return renderingContext;
+}
+
+std::unique_ptr<WebGLRenderingContext> WebGLRenderingContext::create(CanvasBase& canvas, Ref<GraphicsContext3D>&& context, GraphicsContext3DAttributes attributes)
+{
+    auto renderingContext = std::unique_ptr<WebGLRenderingContext>(new WebGLRenderingContext(canvas, WTFMove(context), attributes));
+
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
+    return renderingContext;
+}
+
 WebGLRenderingContext::WebGLRenderingContext(CanvasBase& canvas, GraphicsContext3DAttributes attributes)
     : WebGLRenderingContextBase(canvas, attributes)
 {
index 240e079..bacf9eb 100644 (file)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "WebGLRenderingContextBase.h"
+#include <memory>
 
 #if ENABLE(WEBGL)
 
@@ -33,8 +34,8 @@ namespace WebCore {
 
 class WebGLRenderingContext final : public WebGLRenderingContextBase {
 public:
-    WebGLRenderingContext(CanvasBase&, GraphicsContext3DAttributes);
-    WebGLRenderingContext(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes);
+    static std::unique_ptr<WebGLRenderingContext> create(CanvasBase&, GraphicsContext3DAttributes);
+    static std::unique_ptr<WebGLRenderingContext> create(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes);
 
     bool isWebGL1() const final { return true; }
 
@@ -54,6 +55,10 @@ public:
     bool validateIndexArrayConservative(GC3Denum type, unsigned& numElementsRequired) final;
     bool validateBlendEquation(const char* functionName, GC3Denum mode) final;
     bool validateCapability(const char* functionName, GC3Denum cap) final;
+
+private:
+    WebGLRenderingContext(CanvasBase&, GraphicsContext3DAttributes);
+    WebGLRenderingContext(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes);
 };
 
 } // namespace WebCore
index 1255c39..70e2b3e 100644 (file)
@@ -438,10 +438,10 @@ std::unique_ptr<WebGLRenderingContextBase> WebGLRenderingContextBase::create(Can
         std::unique_ptr<WebGLRenderingContextBase> renderingContext = nullptr;
 #if ENABLE(WEBGL2)
         if (type == "webgl2")
-            renderingContext = std::make_unique<WebGL2RenderingContext>(canvas, attributes);
+            renderingContext = WebGL2RenderingContext::create(canvas, attributes);
         else
 #endif
-            renderingContext = std::make_unique<WebGLRenderingContext>(canvas, attributes);
+            renderingContext = WebGLRenderingContext::create(canvas, attributes);
         renderingContext->suspendIfNeeded();
         return renderingContext;
     }
@@ -467,10 +467,10 @@ std::unique_ptr<WebGLRenderingContextBase> WebGLRenderingContextBase::create(Can
     std::unique_ptr<WebGLRenderingContextBase> renderingContext;
 #if ENABLE(WEBGL2)
     if (type == "webgl2")
-        renderingContext = std::make_unique<WebGL2RenderingContext>(canvas, context.releaseNonNull(), attributes);
+        renderingContext = WebGL2RenderingContext::create(canvas, context.releaseNonNull(), attributes);
     else
 #endif
-        renderingContext = std::make_unique<WebGLRenderingContext>(canvas, context.releaseNonNull(), attributes);
+        renderingContext = WebGLRenderingContext::create(canvas, context.releaseNonNull(), attributes);
     renderingContext->suspendIfNeeded();
 
     return renderingContext;
index 8bdd3a3..2f798a3 100644 (file)
@@ -28,6 +28,7 @@
 #if ENABLE(WEBGL)
 
 #include "ActivityStateChangeObserver.h"
+#include "ExceptionOr.h"
 #include "GPUBasedCanvasRenderingContext.h"
 #include "GraphicsContext3D.h"
 #include "ImageBuffer.h"
@@ -42,6 +43,7 @@
 #include "WebGLTexture.h"
 #include "WebGLVertexArrayObjectOES.h"
 #include <memory>
+#include <runtime/ConsoleTypes.h>
 
 #if ENABLE(WEBGL2)
 #include "WebGLVertexArrayObject.h"
index 59d8dfc..b4c259e 100644 (file)
@@ -32,6 +32,7 @@
 #include "Document.h"
 #include "FrameView.h"
 #include "GPUDevice.h"
+#include "InspectorInstrumentation.h"
 #include "WebGPUBuffer.h"
 #include "WebGPUCommandQueue.h"
 #include "WebGPUComputePipelineState.h"
@@ -80,10 +81,11 @@ std::unique_ptr<WebGPURenderingContext> WebGPURenderingContext::create(CanvasBas
         return nullptr;
     }
 
-    std::unique_ptr<WebGPURenderingContext> renderingContext = nullptr;
-    renderingContext = std::unique_ptr<WebGPURenderingContext>(new WebGPURenderingContext(canvas, device.releaseNonNull()));
+    auto renderingContext = std::unique_ptr<WebGPURenderingContext>(new WebGPURenderingContext(canvas, device.releaseNonNull()));
     renderingContext->suspendIfNeeded();
 
+    InspectorInstrumentation::didCreateCanvasRenderingContext(*renderingContext);
+
     return renderingContext;
 }
 
index 87a8bd2..e7ca2c8 100644 (file)
 #include "CachedImage.h"
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
+#include "CanvasRenderingContext.h"
 #include "CanvasRenderingContext2D.h"
 #include "Document.h"
 #include "FloatPoint.h"
-#include "Frame.h"
 #include "Gradient.h"
 #include "HTMLCanvasElement.h"
 #include "HTMLImageElement.h"
@@ -44,7 +44,6 @@
 #include "ImageBuffer.h"
 #include "ImageData.h"
 #include "InspectorDOMAgent.h"
-#include "InspectorPageAgent.h"
 #include "InstrumentingAgents.h"
 #include "JSCanvasDirection.h"
 #include "JSCanvasFillRule.h"
@@ -78,21 +77,23 @@ namespace WebCore {
 
 using namespace Inspector;
 
-Ref<InspectorCanvas> InspectorCanvas::create(HTMLCanvasElement& canvas, const String& cssCanvasName)
+Ref<InspectorCanvas> InspectorCanvas::create(CanvasRenderingContext& context)
 {
-    return adoptRef(*new InspectorCanvas(canvas, cssCanvasName));
+    return adoptRef(*new InspectorCanvas(context));
 }
 
-InspectorCanvas::InspectorCanvas(HTMLCanvasElement& canvas, const String& cssCanvasName)
+InspectorCanvas::InspectorCanvas(CanvasRenderingContext& context)
     : m_identifier("canvas:" + IdentifiersFactory::createIdentifier())
-    , m_canvas(canvas)
-    , m_cssCanvasName(cssCanvasName)
+    , m_context(context)
 {
 }
 
-InspectorCanvas::~InspectorCanvas()
+HTMLCanvasElement* InspectorCanvas::canvasElement()
 {
-    resetRecordingData();
+    auto* canvasBase = &m_context.canvasBase();
+    if (is<HTMLCanvasElement>(canvasBase))
+        return downcast<HTMLCanvasElement>(canvasBase);
+    return nullptr;
 }
 
 void InspectorCanvas::resetRecordingData()
@@ -108,7 +109,7 @@ void InspectorCanvas::resetRecordingData()
     m_bufferUsed = 0;
     m_singleFrame = true;
 
-    m_canvas.renderingContext()->setCallTracingActive(false);
+    m_context.setCallTracingActive(false);
 }
 
 bool InspectorCanvas::hasRecordingData() const
@@ -157,7 +158,7 @@ void InspectorCanvas::recordAction(const String& name, Vector<RecordCanvasAction
     m_currentActions->addItem(action);
 
 #if ENABLE(WEBGL)
-    if (is<WebGLRenderingContext>(m_canvas.renderingContext()) && shouldSnapshotWebGLAction(name))
+    if (is<WebGLRenderingContext>(m_context) && shouldSnapshotWebGLAction(name))
         m_actionNeedingSnapshot = action;
 #endif
 }
@@ -212,25 +213,21 @@ bool InspectorCanvas::hasBufferSpace() const
 
 Ref<Inspector::Protocol::Canvas::Canvas> InspectorCanvas::buildObjectForCanvas(InstrumentingAgents& instrumentingAgents, bool captureBacktrace)
 {
-    Document& document = m_canvas.document();
-    Frame* frame = document.frame();
-    CanvasRenderingContext* context = m_canvas.renderingContext();
-
     Inspector::Protocol::Canvas::ContextType contextType;
-    if (is<CanvasRenderingContext2D>(context))
+    if (is<CanvasRenderingContext2D>(m_context))
         contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
-    else if (is<ImageBitmapRenderingContext>(context))
+    else if (is<ImageBitmapRenderingContext>(m_context))
         contextType = Inspector::Protocol::Canvas::ContextType::BitmapRenderer;
 #if ENABLE(WEBGL)
-    else if (is<WebGLRenderingContext>(context))
+    else if (is<WebGLRenderingContext>(m_context))
         contextType = Inspector::Protocol::Canvas::ContextType::WebGL;
 #endif
 #if ENABLE(WEBGL2)
-    else if (is<WebGL2RenderingContext>(context))
+    else if (is<WebGL2RenderingContext>(m_context))
         contextType = Inspector::Protocol::Canvas::ContextType::WebGL2;
 #endif
 #if ENABLE(WEBGPU)
-    else if (is<WebGPURenderingContext>(context))
+    else if (is<WebGPURenderingContext>(m_context))
         contextType = Inspector::Protocol::Canvas::ContextType::WebGPU;
 #endif
     else {
@@ -240,35 +237,37 @@ Ref<Inspector::Protocol::Canvas::Canvas> InspectorCanvas::buildObjectForCanvas(I
 
     auto canvas = Inspector::Protocol::Canvas::Canvas::create()
         .setCanvasId(m_identifier)
-        .setFrameId(instrumentingAgents.inspectorPageAgent()->frameId(frame))
         .setContextType(contextType)
         .release();
 
-    if (!m_cssCanvasName.isEmpty())
-        canvas->setCssCanvasName(m_cssCanvasName);
-    else {
-        InspectorDOMAgent* domAgent = instrumentingAgents.inspectorDOMAgent();
-        int nodeId = domAgent->boundNodeId(&m_canvas);
-        if (!nodeId) {
-            if (int documentNodeId = domAgent->boundNodeId(&m_canvas.document())) {
-                ErrorString ignored;
-                nodeId = domAgent->pushNodeToFrontend(ignored, documentNodeId, &m_canvas);
+    if (auto* node = canvasElement()) {
+        String cssCanvasName = node->document().nameForCSSCanvasElement(*node);
+        if (!cssCanvasName.isEmpty())
+            canvas->setCssCanvasName(cssCanvasName);
+        else {
+            InspectorDOMAgent* domAgent = instrumentingAgents.inspectorDOMAgent();
+            int nodeId = domAgent->boundNodeId(node);
+            if (!nodeId) {
+                if (int documentNodeId = domAgent->boundNodeId(&node->document())) {
+                    ErrorString ignored;
+                    nodeId = domAgent->pushNodeToFrontend(ignored, documentNodeId, node);
+                }
             }
-        }
 
-        if (nodeId)
-            canvas->setNodeId(nodeId);
+            if (nodeId)
+                canvas->setNodeId(nodeId);
+        }
     }
 
-    if (is<ImageBitmapRenderingContext>(context)) {
+    if (is<ImageBitmapRenderingContext>(m_context)) {
         auto contextAttributes = Inspector::Protocol::Canvas::ContextAttributes::create()
             .release();
-        contextAttributes->setAlpha(downcast<ImageBitmapRenderingContext>(context)->hasAlpha());
+        contextAttributes->setAlpha(downcast<ImageBitmapRenderingContext>(m_context).hasAlpha());
         canvas->setContextAttributes(WTFMove(contextAttributes));
     }
 #if ENABLE(WEBGL)
-    else if (is<WebGLRenderingContextBase>(context)) {
-        if (std::optional<WebGLContextAttributes> attributes = downcast<WebGLRenderingContextBase>(context)->getContextAttributes()) {
+    else if (is<WebGLRenderingContextBase>(m_context)) {
+        if (std::optional<WebGLContextAttributes> attributes = downcast<WebGLRenderingContextBase>(m_context).getContextAttributes()) {
             auto contextAttributes = Inspector::Protocol::Canvas::ContextAttributes::create()
                 .release();
             contextAttributes->setAlpha(attributes->alpha);
@@ -283,8 +282,12 @@ Ref<Inspector::Protocol::Canvas::Canvas> InspectorCanvas::buildObjectForCanvas(I
     }
 #endif
 
-    if (size_t memoryCost = m_canvas.memoryCost())
-        canvas->setMemoryCost(memoryCost);
+    // FIXME: <https://webkit.org/b/180833> Web Inspector: support OffscreenCanvas for Canvas related operations
+
+    if (auto* node = canvasElement()) {
+        if (size_t memoryCost = node->memoryCost())
+            canvas->setMemoryCost(memoryCost);
+    }
 
     if (captureBacktrace) {
         auto stackTrace = Inspector::createScriptCallStack(JSMainThreadExecState::currentState(), Inspector::ScriptCallStack::maxCallStackSizeToCapture);
@@ -305,17 +308,22 @@ void InspectorCanvas::appendActionSnapshotIfNeeded()
 
 String InspectorCanvas::getCanvasContentAsDataURL()
 {
+    // FIXME: <https://webkit.org/b/180833> Web Inspector: support OffscreenCanvas for Canvas related operations
+
+    auto* node = canvasElement();
+    if (!node)
+        return String();
+
 #if ENABLE(WEBGL)
-    CanvasRenderingContext* canvasRenderingContext = m_canvas.renderingContext();
-    if (is<WebGLRenderingContextBase>(canvasRenderingContext))
-        downcast<WebGLRenderingContextBase>(canvasRenderingContext)->setPreventBufferClearForInspector(true);
+    if (is<WebGLRenderingContextBase>(m_context))
+        downcast<WebGLRenderingContextBase>(m_context).setPreventBufferClearForInspector(true);
 #endif
 
-    ExceptionOr<UncachedString> result = m_canvas.toDataURL(ASCIILiteral("image/png"));
+    ExceptionOr<UncachedString> result = node->toDataURL(ASCIILiteral("image/png"));
 
 #if ENABLE(WEBGL)
-    if (is<WebGLRenderingContextBase>(canvasRenderingContext))
-        downcast<WebGLRenderingContextBase>(canvasRenderingContext)->setPreventBufferClearForInspector(false);
+    if (is<WebGLRenderingContextBase>(m_context))
+        downcast<WebGLRenderingContextBase>(m_context).setPreventBufferClearForInspector(false);
 #endif
 
     if (result.hasException())
@@ -431,27 +439,26 @@ RefPtr<Inspector::Protocol::Recording::InitialState> InspectorCanvas::buildIniti
         .release();
 
     auto attributes = JSON::Object::create();
-    attributes->setInteger(ASCIILiteral("width"), canvas().width());
-    attributes->setInteger(ASCIILiteral("height"), canvas().height());
+    attributes->setInteger(ASCIILiteral("width"), m_context.canvasBase().width());
+    attributes->setInteger(ASCIILiteral("height"), m_context.canvasBase().height());
 
     auto parameters = JSON::ArrayOf<JSON::Value>::create();
 
-    CanvasRenderingContext* canvasRenderingContext = canvas().renderingContext();
-    if (is<CanvasRenderingContext2D>(canvasRenderingContext)) {
-        const CanvasRenderingContext2D* context2d = downcast<CanvasRenderingContext2D>(canvasRenderingContext);
-        const CanvasRenderingContext2D::State& state = context2d->state();
+    if (is<CanvasRenderingContext2D>(m_context)) {
+        const CanvasRenderingContext2D& context2d = downcast<CanvasRenderingContext2D>(m_context);
+        const CanvasRenderingContext2D::State& state = context2d.state();
 
         attributes->setArray(ASCIILiteral("setTransform"), buildArrayForAffineTransform(state.transform));
-        attributes->setDouble(ASCIILiteral("globalAlpha"), context2d->globalAlpha());
-        attributes->setInteger(ASCIILiteral("globalCompositeOperation"), indexForData(context2d->globalCompositeOperation()));
-        attributes->setDouble(ASCIILiteral("lineWidth"), context2d->lineWidth());
-        attributes->setInteger(ASCIILiteral("lineCap"), indexForData(convertEnumerationToString(context2d->lineCap())));
-        attributes->setInteger(ASCIILiteral("lineJoin"), indexForData(convertEnumerationToString(context2d->lineJoin())));
-        attributes->setDouble(ASCIILiteral("miterLimit"), context2d->miterLimit());
-        attributes->setDouble(ASCIILiteral("shadowOffsetX"), context2d->shadowOffsetX());
-        attributes->setDouble(ASCIILiteral("shadowOffsetY"), context2d->shadowOffsetY());
-        attributes->setDouble(ASCIILiteral("shadowBlur"), context2d->shadowBlur());
-        attributes->setInteger(ASCIILiteral("shadowColor"), indexForData(context2d->shadowColor()));
+        attributes->setDouble(ASCIILiteral("globalAlpha"), context2d.globalAlpha());
+        attributes->setInteger(ASCIILiteral("globalCompositeOperation"), indexForData(context2d.globalCompositeOperation()));
+        attributes->setDouble(ASCIILiteral("lineWidth"), context2d.lineWidth());
+        attributes->setInteger(ASCIILiteral("lineCap"), indexForData(convertEnumerationToString(context2d.lineCap())));
+        attributes->setInteger(ASCIILiteral("lineJoin"), indexForData(convertEnumerationToString(context2d.lineJoin())));
+        attributes->setDouble(ASCIILiteral("miterLimit"), context2d.miterLimit());
+        attributes->setDouble(ASCIILiteral("shadowOffsetX"), context2d.shadowOffsetX());
+        attributes->setDouble(ASCIILiteral("shadowOffsetY"), context2d.shadowOffsetY());
+        attributes->setDouble(ASCIILiteral("shadowBlur"), context2d.shadowBlur());
+        attributes->setInteger(ASCIILiteral("shadowColor"), indexForData(context2d.shadowColor()));
 
         // The parameter to `setLineDash` is itself an array, so we need to wrap the parameters
         // list in an array to allow spreading.
@@ -459,11 +466,11 @@ RefPtr<Inspector::Protocol::Recording::InitialState> InspectorCanvas::buildIniti
         setLineDash->addItem(buildArrayForVector(state.lineDash));
         attributes->setArray(ASCIILiteral("setLineDash"), WTFMove(setLineDash));
 
-        attributes->setDouble(ASCIILiteral("lineDashOffset"), context2d->lineDashOffset());
-        attributes->setInteger(ASCIILiteral("font"), indexForData(context2d->font()));
-        attributes->setInteger(ASCIILiteral("textAlign"), indexForData(convertEnumerationToString(context2d->textAlign())));
-        attributes->setInteger(ASCIILiteral("textBaseline"), indexForData(convertEnumerationToString(context2d->textBaseline())));
-        attributes->setInteger(ASCIILiteral("direction"), indexForData(convertEnumerationToString(context2d->direction())));
+        attributes->setDouble(ASCIILiteral("lineDashOffset"), context2d.lineDashOffset());
+        attributes->setInteger(ASCIILiteral("font"), indexForData(context2d.font()));
+        attributes->setInteger(ASCIILiteral("textAlign"), indexForData(convertEnumerationToString(context2d.textAlign())));
+        attributes->setInteger(ASCIILiteral("textBaseline"), indexForData(convertEnumerationToString(context2d.textBaseline())));
+        attributes->setInteger(ASCIILiteral("direction"), indexForData(convertEnumerationToString(context2d.direction())));
 
         int strokeStyleIndex;
         if (auto canvasGradient = state.strokeStyle.canvasGradient())
@@ -483,17 +490,17 @@ RefPtr<Inspector::Protocol::Recording::InitialState> InspectorCanvas::buildIniti
             fillStyleIndex = indexForData(state.fillStyle.color());
         attributes->setInteger(ASCIILiteral("fillStyle"), fillStyleIndex);
 
-        attributes->setBoolean(ASCIILiteral("imageSmoothingEnabled"), context2d->imageSmoothingEnabled());
-        attributes->setInteger(ASCIILiteral("imageSmoothingQuality"), indexForData(convertEnumerationToString(context2d->imageSmoothingQuality())));
+        attributes->setBoolean(ASCIILiteral("imageSmoothingEnabled"), context2d.imageSmoothingEnabled());
+        attributes->setInteger(ASCIILiteral("imageSmoothingQuality"), indexForData(convertEnumerationToString(context2d.imageSmoothingQuality())));
 
         auto setPath = JSON::ArrayOf<JSON::Value>::create();
-        setPath->addItem(indexForData(buildStringFromPath(context2d->getPath()->path())));
+        setPath->addItem(indexForData(buildStringFromPath(context2d.getPath()->path())));
         attributes->setArray(ASCIILiteral("setPath"), WTFMove(setPath));
     }
 #if ENABLE(WEBGL)
-    else if (is<WebGLRenderingContextBase>(canvasRenderingContext)) {
-        WebGLRenderingContextBase* contextWebGLBase = downcast<WebGLRenderingContextBase>(canvasRenderingContext);
-        if (std::optional<WebGLContextAttributes> attributes = contextWebGLBase->getContextAttributes()) {
+    else if (is<WebGLRenderingContextBase>(m_context)) {
+        WebGLRenderingContextBase& contextWebGLBase = downcast<WebGLRenderingContextBase>(m_context);
+        if (std::optional<WebGLContextAttributes> attributes = contextWebGLBase.getContextAttributes()) {
             RefPtr<JSON::Object> contextAttributes = JSON::Object::create();
             contextAttributes->setBoolean(ASCIILiteral("alpha"), attributes->alpha);
             contextAttributes->setBoolean(ASCIILiteral("depth"), attributes->depth);
index e91547f..81d1901 100644 (file)
@@ -39,6 +39,7 @@ namespace WebCore {
 
 class CanvasGradient;
 class CanvasPattern;
+class CanvasRenderingContext;
 class HTMLCanvasElement;
 class HTMLImageElement;
 class HTMLVideoElement;
@@ -50,11 +51,12 @@ typedef String ErrorString;
 
 class InspectorCanvas final : public RefCounted<InspectorCanvas> {
 public:
-    static Ref<InspectorCanvas> create(HTMLCanvasElement&, const String& cssCanvasName);
+    static Ref<InspectorCanvas> create(CanvasRenderingContext&);
 
     const String& identifier() { return m_identifier; }
-    HTMLCanvasElement& canvas() { return m_canvas; }
-    const String& cssCanvasName() { return m_cssCanvasName; }
+    CanvasRenderingContext& context() { return m_context; }
+
+    HTMLCanvasElement* canvasElement();
 
     void resetRecordingData();
     bool hasRecordingData() const;
@@ -80,10 +82,8 @@ public:
 
     Ref<Inspector::Protocol::Canvas::Canvas> buildObjectForCanvas(InstrumentingAgents&, bool captureBacktrace);
 
-    ~InspectorCanvas();
-
 private:
-    InspectorCanvas(HTMLCanvasElement&, const String& cssCanvasName);
+    InspectorCanvas(CanvasRenderingContext&);
     void appendActionSnapshotIfNeeded();
     String getCanvasContentAsDataURL();
 
@@ -109,8 +109,7 @@ private:
     RefPtr<JSON::ArrayOf<JSON::Value>> buildArrayForImageData(const ImageData&);
 
     String m_identifier;
-    HTMLCanvasElement& m_canvas;
-    String m_cssCanvasName;
+    CanvasRenderingContext& m_context;
 
     RefPtr<Inspector::Protocol::Recording::InitialState> m_initialState;
     RefPtr<JSON::ArrayOf<Inspector::Protocol::Recording::Frame>> m_frames;
index 82cbe7f..c68be76 100644 (file)
@@ -874,10 +874,10 @@ void InspectorInstrumentation::stopProfilingImpl(InstrumentingAgents& instrument
         timelineAgent->stopFromConsole(exec, title);
 }
 
-void InspectorInstrumentation::consoleStartRecordingCanvasImpl(InstrumentingAgents& instrumentingAgents, HTMLCanvasElement& canvasElement, JSC::ExecState& exec, JSC::JSObject* options)
+void InspectorInstrumentation::consoleStartRecordingCanvasImpl(InstrumentingAgents& instrumentingAgents, CanvasRenderingContext& context, JSC::ExecState& exec, JSC::JSObject* options)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->consoleStartRecordingCanvas(canvasElement, exec, options);
+        canvasAgent->consoleStartRecordingCanvas(context, exec, options);
 }
 
 void InspectorInstrumentation::didOpenDatabaseImpl(InstrumentingAgents& instrumentingAgents, RefPtr<Database>&& database, const String& domain, const String& name, const String& version)
@@ -958,28 +958,22 @@ void InspectorInstrumentation::didSendWebSocketFrameImpl(InstrumentingAgents& in
         networkAgent->didSendWebSocketFrame(identifier, frame);
 }
 
-void InspectorInstrumentation::didCreateCSSCanvasImpl(InstrumentingAgents& instrumentingAgents, HTMLCanvasElement& canvasElement, const String& name)
+void InspectorInstrumentation::didChangeCSSCanvasClientNodesImpl(InstrumentingAgents& instrumentingAgents, CanvasBase& canvasBase)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didCreateCSSCanvas(canvasElement, name);
+        canvasAgent->didChangeCSSCanvasClientNodes(canvasBase);
 }
 
-void InspectorInstrumentation::didChangeCSSCanvasClientNodesImpl(InstrumentingAgents& instrumentingAgents, HTMLCanvasElement& canvasElement)
+void InspectorInstrumentation::didCreateCanvasRenderingContextImpl(InstrumentingAgents& instrumentingAgents, CanvasRenderingContext& context)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didChangeCSSCanvasClientNodes(canvasElement);
+        canvasAgent->didCreateCanvasRenderingContext(context);
 }
 
-void InspectorInstrumentation::didCreateCanvasRenderingContextImpl(InstrumentingAgents& instrumentingAgents, HTMLCanvasElement& canvasElement)
+void InspectorInstrumentation::didChangeCanvasMemoryImpl(InstrumentingAgents& instrumentingAgents, CanvasRenderingContext& context)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didCreateCanvasRenderingContext(canvasElement);
-}
-
-void InspectorInstrumentation::didChangeCanvasMemoryImpl(InstrumentingAgents& instrumentingAgents, HTMLCanvasElement& canvasElement)
-{
-    if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didChangeCanvasMemory(canvasElement);
+        canvasAgent->didChangeCanvasMemory(context);
 }
 
 void InspectorInstrumentation::recordCanvasActionImpl(InstrumentingAgents& instrumentingAgents, CanvasRenderingContext& canvasRenderingContext, const String& name, Vector<RecordCanvasActionVariant>&& parameters)
@@ -988,23 +982,23 @@ void InspectorInstrumentation::recordCanvasActionImpl(InstrumentingAgents& instr
         canvasAgent->recordCanvasAction(canvasRenderingContext, name, WTFMove(parameters));
 }
 
-void InspectorInstrumentation::didFinishRecordingCanvasFrameImpl(InstrumentingAgents& instrumentingAgents, HTMLCanvasElement& canvasElement, bool forceDispatch)
+void InspectorInstrumentation::didFinishRecordingCanvasFrameImpl(InstrumentingAgents& instrumentingAgents, CanvasRenderingContext& context, bool forceDispatch)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didFinishRecordingCanvasFrame(canvasElement, forceDispatch);
+        canvasAgent->didFinishRecordingCanvasFrame(context, forceDispatch);
 }
 
 #if ENABLE(WEBGL)
-void InspectorInstrumentation::didEnableExtensionImpl(InstrumentingAgents& instrumentingAgents, WebGLRenderingContextBase& context, const String& extension)
+void InspectorInstrumentation::didEnableExtensionImpl(InstrumentingAgents& instrumentingAgents, WebGLRenderingContextBase& contextWebGLBase, const String& extension)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didEnableExtension(context, extension);
+        canvasAgent->didEnableExtension(contextWebGLBase, extension);
 }
 
-void InspectorInstrumentation::didCreateProgramImpl(InstrumentingAgents& instrumentingAgents, WebGLRenderingContextBase& context, WebGLProgram& program)
+void InspectorInstrumentation::didCreateProgramImpl(InstrumentingAgents& instrumentingAgents, WebGLRenderingContextBase& contextWebGLBase, WebGLProgram& program)
 {
     if (InspectorCanvasAgent* canvasAgent = instrumentingAgents.inspectorCanvasAgent())
-        canvasAgent->didCreateProgram(context, program);
+        canvasAgent->didCreateProgram(contextWebGLBase, program);
 }
 
 void InspectorInstrumentation::willDeleteProgramImpl(InstrumentingAgents& instrumentingAgents, WebGLProgram& program)
index a0ad311..fa4a2d1 100644 (file)
 
 #include "CSSSelector.h"
 #include "CallTracerTypes.h"
+#include "CanvasBase.h"
 #include "CanvasRenderingContext.h"
 #include "DocumentThreadableLoader.h"
 #include "Element.h"
 #include "EventTarget.h"
 #include "FormData.h"
 #include "Frame.h"
-#include "HTMLCanvasElement.h"
 #include "HitTestResult.h"
 #include "InspectorController.h"
 #include "InspectorInstrumentationCookie.h"
@@ -225,7 +225,7 @@ public:
     static void consoleTimeStamp(Frame&, Ref<Inspector::ScriptArguments>&&);
     static void startProfiling(Page&, JSC::ExecState*, const String& title);
     static void stopProfiling(Page&, JSC::ExecState*, const String& title);
-    static void consoleStartRecordingCanvas(HTMLCanvasElement&, JSC::ExecState&, JSC::JSObject* options);
+    static void consoleStartRecordingCanvas(CanvasRenderingContext&, JSC::ExecState&, JSC::JSObject* options);
 
     static void didRequestAnimationFrame(Document&, int callbackId);
     static void didCancelAnimationFrame(Document&, int callbackId);
@@ -252,12 +252,11 @@ public:
     static void didHandleMemoryPressure(Page&, Critical);
 #endif
 
-    static void didCreateCSSCanvas(HTMLCanvasElement&, const String&);
-    static void didChangeCSSCanvasClientNodes(HTMLCanvasElement&);
-    static void didCreateCanvasRenderingContext(HTMLCanvasElement&);
-    static void didChangeCanvasMemory(HTMLCanvasElement&);
+    static void didChangeCSSCanvasClientNodes(CanvasBase&);
+    static void didCreateCanvasRenderingContext(CanvasRenderingContext&);
+    static void didChangeCanvasMemory(CanvasRenderingContext&);
     static void recordCanvasAction(CanvasRenderingContext&, const String&, Vector<RecordCanvasActionVariant>&& = { });
-    static void didFinishRecordingCanvasFrame(HTMLCanvasElement&, bool forceDispatch = false);
+    static void didFinishRecordingCanvasFrame(CanvasRenderingContext&, bool forceDispatch = false);
 #if ENABLE(WEBGL)
     static void didEnableExtension(WebGLRenderingContextBase&, const String&);
     static void didCreateProgram(WebGLRenderingContextBase&, WebGLProgram&);
@@ -393,7 +392,7 @@ private:
     static void consoleTimeStampImpl(InstrumentingAgents&, Frame&, Ref<Inspector::ScriptArguments>&&);
     static void startProfilingImpl(InstrumentingAgents&, JSC::ExecState*, const String& title);
     static void stopProfilingImpl(InstrumentingAgents&, JSC::ExecState*, const String& title);
-    static void consoleStartRecordingCanvasImpl(InstrumentingAgents&, HTMLCanvasElement&, JSC::ExecState&, JSC::JSObject* options);
+    static void consoleStartRecordingCanvasImpl(InstrumentingAgents&, CanvasRenderingContext&, JSC::ExecState&, JSC::JSObject* options);
 
     static void didRequestAnimationFrameImpl(InstrumentingAgents&, int callbackId, Document&);
     static void didCancelAnimationFrameImpl(InstrumentingAgents&, int callbackId, Document&);
@@ -423,12 +422,11 @@ private:
     static void networkStateChangedImpl(InstrumentingAgents&);
     static void updateApplicationCacheStatusImpl(InstrumentingAgents&, Frame&);
 
-    static void didCreateCSSCanvasImpl(InstrumentingAgents&, HTMLCanvasElement&, const String&);
-    static void didChangeCSSCanvasClientNodesImpl(InstrumentingAgents&, HTMLCanvasElement&);
-    static void didCreateCanvasRenderingContextImpl(InstrumentingAgents&, HTMLCanvasElement&);
-    static void didChangeCanvasMemoryImpl(InstrumentingAgents&, HTMLCanvasElement&);
+    static void didChangeCSSCanvasClientNodesImpl(InstrumentingAgents&, CanvasBase&);
+    static void didCreateCanvasRenderingContextImpl(InstrumentingAgents&, CanvasRenderingContext&);
+    static void didChangeCanvasMemoryImpl(InstrumentingAgents&, CanvasRenderingContext&);
     static void recordCanvasActionImpl(InstrumentingAgents&, CanvasRenderingContext&, const String&, Vector<RecordCanvasActionVariant>&& = { });
-    static void didFinishRecordingCanvasFrameImpl(InstrumentingAgents&, HTMLCanvasElement&, bool forceDispatch = false);
+    static void didFinishRecordingCanvasFrameImpl(InstrumentingAgents&, CanvasRenderingContext&, bool forceDispatch = false);
 #if ENABLE(WEBGL)
     static void didEnableExtensionImpl(InstrumentingAgents&, WebGLRenderingContextBase&, const String&);
     static void didCreateProgramImpl(InstrumentingAgents&, WebGLRenderingContextBase&, WebGLProgram&);
@@ -1201,105 +1199,66 @@ inline void InspectorInstrumentation::didHandleMemoryPressure(Page& page, Critic
 }
 #endif
 
-inline void InspectorInstrumentation::didCreateCSSCanvas(HTMLCanvasElement& canvasElement, const String& name)
-{
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement.document()))
-        didCreateCSSCanvasImpl(*instrumentingAgents, canvasElement, name);
-}
-
-inline void InspectorInstrumentation::didChangeCSSCanvasClientNodes(HTMLCanvasElement& canvasElement)
+inline void InspectorInstrumentation::didChangeCSSCanvasClientNodes(CanvasBase& canvasBase)
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement.document()))
-        didChangeCSSCanvasClientNodesImpl(*instrumentingAgents, canvasElement);
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(canvasBase.scriptExecutionContext()))
+        didChangeCSSCanvasClientNodesImpl(*instrumentingAgents, canvasBase);
 }
 
-inline void InspectorInstrumentation::didCreateCanvasRenderingContext(HTMLCanvasElement& canvasElement)
+inline void InspectorInstrumentation::didCreateCanvasRenderingContext(CanvasRenderingContext& context)
 {
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement.document()))
-        didCreateCanvasRenderingContextImpl(*instrumentingAgents, canvasElement);
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context.canvasBase().scriptExecutionContext()))
+        didCreateCanvasRenderingContextImpl(*instrumentingAgents, context);
 }
 
-inline void InspectorInstrumentation::didChangeCanvasMemory(HTMLCanvasElement& canvasElement)
+inline void InspectorInstrumentation::didChangeCanvasMemory(CanvasRenderingContext& context)
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement.document()))
-        didChangeCanvasMemoryImpl(*instrumentingAgents, canvasElement);
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context.canvasBase().scriptExecutionContext()))
+        didChangeCanvasMemoryImpl(*instrumentingAgents, context);
 }
 
-inline void InspectorInstrumentation::recordCanvasAction(CanvasRenderingContext& canvasRenderingContext, const String& name, Vector<RecordCanvasActionVariant>&& parameters)
+inline void InspectorInstrumentation::recordCanvasAction(CanvasRenderingContext& context, const String& name, Vector<RecordCanvasActionVariant>&& parameters)
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
-    auto& canvasBase = canvasRenderingContext.canvasBase();
-    auto* canvasElement = is<HTMLCanvasElement>(canvasBase) ? &downcast<HTMLCanvasElement>(canvasBase) : nullptr;
-    if (canvasElement) {
-        if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement->document()))
-            recordCanvasActionImpl(*instrumentingAgents, canvasRenderingContext, name, WTFMove(parameters));
-    }
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context.canvasBase().scriptExecutionContext()))
+        recordCanvasActionImpl(*instrumentingAgents, context, name, WTFMove(parameters));
 }
 
-inline void InspectorInstrumentation::didFinishRecordingCanvasFrame(HTMLCanvasElement& canvasElement, bool forceDispatch)
+inline void InspectorInstrumentation::didFinishRecordingCanvasFrame(CanvasRenderingContext& context, bool forceDispatch)
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement.document()))
-        didFinishRecordingCanvasFrameImpl(*instrumentingAgents, canvasElement, forceDispatch);
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context.canvasBase().scriptExecutionContext()))
+        didFinishRecordingCanvasFrameImpl(*instrumentingAgents, context, forceDispatch);
 }
 
 #if ENABLE(WEBGL)
-inline void InspectorInstrumentation::didEnableExtension(WebGLRenderingContextBase& context, const String& extension)
+inline void InspectorInstrumentation::didEnableExtension(WebGLRenderingContextBase& contextWebGLBase, const String& extension)
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
-    auto canvas = context.canvas();
-    WTF::switchOn(canvas,
-        [&] (const RefPtr<HTMLCanvasElement>& htmlCanvasElement) {
-            if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(htmlCanvasElement->document()))
-                didEnableExtensionImpl(*instrumentingAgents, context, extension);
-        },
-        [&] (const RefPtr<OffscreenCanvas>&) {
-        }
-    );
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(contextWebGLBase.canvasBase().scriptExecutionContext()))
+        didEnableExtensionImpl(*instrumentingAgents, contextWebGLBase, extension);
 }
 
-inline void InspectorInstrumentation::didCreateProgram(WebGLRenderingContextBase& context, WebGLProgram& program)
+inline void InspectorInstrumentation::didCreateProgram(WebGLRenderingContextBase& contextWebGLBase, WebGLProgram& program)
 {
-    auto canvas = context.canvas();
-    WTF::switchOn(canvas,
-        [&] (const RefPtr<HTMLCanvasElement>& htmlCanvasElement) {
-            if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(htmlCanvasElement->document()))
-                didCreateProgramImpl(*instrumentingAgents, context, program);
-        },
-        [&] (const RefPtr<OffscreenCanvas>&) {
-        }
-    );
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(contextWebGLBase.canvasBase().scriptExecutionContext()))
+        didCreateProgramImpl(*instrumentingAgents, contextWebGLBase, program);
 }
 
-inline void InspectorInstrumentation::willDeleteProgram(WebGLRenderingContextBase& context, WebGLProgram& program)
+inline void InspectorInstrumentation::willDeleteProgram(WebGLRenderingContextBase& contextWebGLBase, WebGLProgram& program)
 {
-    auto canvas = context.canvas();
-    WTF::switchOn(canvas,
-        [&] (const RefPtr<HTMLCanvasElement>& htmlCanvasElement) {
-            if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(htmlCanvasElement->document()))
-                willDeleteProgramImpl(*instrumentingAgents, program);
-        },
-        [&] (const RefPtr<OffscreenCanvas>&) {
-        }
-    );
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(contextWebGLBase.canvasBase().scriptExecutionContext()))
+        willDeleteProgramImpl(*instrumentingAgents, program);
 }
 
-inline bool InspectorInstrumentation::isShaderProgramDisabled(WebGLRenderingContextBase& context, WebGLProgram& program)
+inline bool InspectorInstrumentation::isShaderProgramDisabled(WebGLRenderingContextBase& contextWebGLBase, WebGLProgram& program)
 {
     FAST_RETURN_IF_NO_FRONTENDS(false);
-    auto canvas = context.canvas();
-    return WTF::switchOn(canvas,
-        [&] (const RefPtr<HTMLCanvasElement>& htmlCanvasElement) {
-            if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(htmlCanvasElement->document()))
-                return isShaderProgramDisabledImpl(*instrumentingAgents, program);
-            return false;
-        },
-        [&] (const RefPtr<OffscreenCanvas>&) {
-            return false;
-        }
-    );
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(contextWebGLBase.canvasBase().scriptExecutionContext()))
+        return isShaderProgramDisabledImpl(*instrumentingAgents, program);
+    return false;
 }
 #endif
 
@@ -1382,11 +1341,11 @@ inline void InspectorInstrumentation::stopProfiling(Page& page, JSC::ExecState*
     stopProfilingImpl(instrumentingAgentsForPage(page), exec, title);
 }
 
-inline void InspectorInstrumentation::consoleStartRecordingCanvas(HTMLCanvasElement& canvasElement, JSC::ExecState& exec, JSC::JSObject* options)
+inline void InspectorInstrumentation::consoleStartRecordingCanvas(CanvasRenderingContext& context, JSC::ExecState& exec, JSC::JSObject* options)
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForDocument(&canvasElement.document()))
-        consoleStartRecordingCanvasImpl(*instrumentingAgents, canvasElement, exec, options);
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context.canvasBase().scriptExecutionContext()))
+        consoleStartRecordingCanvasImpl(*instrumentingAgents, context, exec, options);
 }
 
 inline void InspectorInstrumentation::didRequestAnimationFrame(Document& document, int callbackId)
index 41eba0f..217f131 100644 (file)
@@ -52,11 +52,10 @@ InspectorShaderProgram::InspectorShaderProgram(WebGLProgram& program, InspectorC
 {
 }
 
-WebGLRenderingContextBase* InspectorShaderProgram::context() const
+WebGLRenderingContextBase& InspectorShaderProgram::context() const
 {
-    auto* context = m_canvas.canvas().renderingContext();
-    ASSERT(context && is<WebGLRenderingContextBase>(context));
-    return downcast<WebGLRenderingContextBase>(context);
+    ASSERT(is<WebGLRenderingContextBase>(m_canvas.context()));
+    return downcast<WebGLRenderingContextBase>(m_canvas.context());
 }
 
 WebGLShader* InspectorShaderProgram::shaderForType(const String& protocolType)
index 50256df..c915170 100644 (file)
@@ -42,7 +42,7 @@ public:
 
     const String& identifier() const { return m_identifier; }
     InspectorCanvas& canvas() const { return m_canvas; }
-    WebGLRenderingContextBase* context() const;
+    WebGLRenderingContextBase& context() const;
     WebGLProgram& program() const { return m_program; }
     WebGLShader* shaderForType(const String&);
 
index 612370b..8515be2 100644 (file)
@@ -103,12 +103,11 @@ void InspectorCanvasAgent::enable(ErrorString&)
         m_frontendDispatcher->canvasAdded(inspectorCanvas->buildObjectForCanvas(m_instrumentingAgents, captureBacktrace));
 
 #if ENABLE(WEBGL)
-        CanvasRenderingContext* context = inspectorCanvas->canvas().renderingContext();
-        if (is<WebGLRenderingContextBase>(context)) {
-            WebGLRenderingContextBase* contextWebGL = downcast<WebGLRenderingContextBase>(context);
-            if (std::optional<Vector<String>> extensions = contextWebGL->getSupportedExtensions()) {
+        if (is<WebGLRenderingContextBase>(inspectorCanvas->context())) {
+            WebGLRenderingContextBase& contextWebGL = downcast<WebGLRenderingContextBase>(inspectorCanvas->context());
+            if (std::optional<Vector<String>> extensions = contextWebGL.getSupportedExtensions()) {
                 for (const String& extension : *extensions) {
-                    if (contextWebGL->extensionIsEnabled(extension))
+                    if (contextWebGL.extensionIsEnabled(extension))
                         m_frontendDispatcher->extensionEnabled(inspectorCanvas->identifier(), extension);
                 }
             }
@@ -149,13 +148,19 @@ void InspectorCanvasAgent::requestNode(ErrorString& errorString, const String& c
     if (!inspectorCanvas)
         return;
 
-    int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&inspectorCanvas->canvas().document());
+    auto* node = inspectorCanvas->canvasElement();
+    if (!node) {
+        errorString = ASCIILiteral("No node for canvas");
+        return;
+    }
+
+    int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&node->document());
     if (!documentNodeId) {
         errorString = ASCIILiteral("Document has not been requested");
         return;
     }
 
-    *nodeId = m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, &inspectorCanvas->canvas());
+    *nodeId = m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, node);
 }
 
 void InspectorCanvasAgent::requestContent(ErrorString& errorString, const String& canvasId, String* content)
@@ -164,33 +169,39 @@ void InspectorCanvasAgent::requestContent(ErrorString& errorString, const String
     if (!inspectorCanvas)
         return;
 
-    CanvasRenderingContext* context = inspectorCanvas->canvas().renderingContext();
-    if (is<CanvasRenderingContext2D>(context) || is<ImageBitmapRenderingContext>(context)) {
-        auto result = inspectorCanvas->canvas().toDataURL(ASCIILiteral("image/png"));
-        if (result.hasException()) {
-            errorString = result.releaseException().releaseMessage();
+    // FIXME: <https://webkit.org/b/180833> Web Inspector: support OffscreenCanvas for Canvas related operations
+
+    if (auto* node = inspectorCanvas->canvasElement()) {
+        if (is<CanvasRenderingContext2D>(inspectorCanvas->context()) || is<ImageBitmapRenderingContext>(inspectorCanvas->context())) {
+            auto result = node->toDataURL(ASCIILiteral("image/png"));
+            if (result.hasException()) {
+                errorString = result.releaseException().releaseMessage();
+                return;
+            }
+            *content = result.releaseReturnValue().string;
             return;
         }
-        *content = result.releaseReturnValue().string;
-    }
+
 #if ENABLE(WEBGL)
-    else if (is<WebGLRenderingContextBase>(context)) {
-        WebGLRenderingContextBase* gl = downcast<WebGLRenderingContextBase>(context);
+        if (is<WebGLRenderingContextBase>(inspectorCanvas->context())) {
+            WebGLRenderingContextBase& contextWebGLBase = downcast<WebGLRenderingContextBase>(inspectorCanvas->context());
 
-        gl->setPreventBufferClearForInspector(true);
-        auto result = inspectorCanvas->canvas().toDataURL(ASCIILiteral("image/png"));
-        gl->setPreventBufferClearForInspector(false);
+            contextWebGLBase.setPreventBufferClearForInspector(true);
+            auto result = node->toDataURL(ASCIILiteral("image/png"));
+            contextWebGLBase.setPreventBufferClearForInspector(false);
 
-        if (result.hasException()) {
-            errorString = result.releaseException().releaseMessage();
+            if (result.hasException()) {
+                errorString = result.releaseException().releaseMessage();
+                return;
+            }
+            *content = result.releaseReturnValue().string;
             return;
         }
-        *content = result.releaseReturnValue().string;
-    }
 #endif
-    // FIXME: <https://webkit.org/b/173621> Web Inspector: Support getting the content of WebGPU contexts
-    else
-        errorString = ASCIILiteral("Unsupported canvas context type");
+    }
+
+    // FIXME: <https://webkit.org/b/173621> Web Inspector: Support getting the content of WebGPU context;
+    errorString = ASCIILiteral("Unsupported canvas context type");
 }
 
 void InspectorCanvasAgent::requestCSSCanvasClientNodes(ErrorString& errorString, const String& canvasId, RefPtr<JSON::ArrayOf<int>>& result)
@@ -200,13 +211,13 @@ void InspectorCanvasAgent::requestCSSCanvasClientNodes(ErrorString& errorString,
         return;
 
     result = JSON::ArrayOf<int>::create();
-    for (Element* element : inspectorCanvas->canvas().cssCanvasClients()) {
-        if (int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&element->document()))
-            result->addItem(m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, element));
+    for (auto* client : inspectorCanvas->context().canvasBase().cssCanvasClients()) {
+        if (int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&client->document()))
+            result->addItem(m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, client));
     }
 }
 
-static JSC::JSValue contextAsScriptValue(JSC::ExecState& state, CanvasRenderingContext* context)
+static JSC::JSValue contextAsScriptValue(JSC::ExecState& state, CanvasRenderingContext& context)
 {
     JSC::JSLockHolder lock(&state);
 
@@ -236,18 +247,11 @@ void InspectorCanvasAgent::resolveCanvasContext(ErrorString& errorString, const
     if (!inspectorCanvas)
         return;
 
-    Frame* frame = inspectorCanvas->canvas().document().frame();
-    if (!frame) {
-        errorString = ASCIILiteral("Canvas belongs to a document without a frame");
-        return;
-    }
-
-    auto& state = *mainWorldExecState(frame);
+    auto& state = *inspectorCanvas->context().canvasBase().scriptExecutionContext()->execState();
     auto injectedScript = m_injectedScriptManager.injectedScriptFor(&state);
     ASSERT(!injectedScript.hasNoValue());
 
-    CanvasRenderingContext* context = inspectorCanvas->canvas().renderingContext();
-    JSC::JSValue value = contextAsScriptValue(state, context);
+    JSC::JSValue value = contextAsScriptValue(state, inspectorCanvas->context());
     if (!value) {
         ASSERT_NOT_REACHED();
         errorString = ASCIILiteral("Unknown context type");
@@ -264,7 +268,7 @@ void InspectorCanvasAgent::startRecording(ErrorString& errorString, const String
     if (!inspectorCanvas)
         return;
 
-    if (inspectorCanvas->canvas().renderingContext()->callTracingActive()) {
+    if (inspectorCanvas->context().callTracingActive()) {
         errorString = ASCIILiteral("Already recording canvas");
         return;
     }
@@ -275,7 +279,7 @@ void InspectorCanvasAgent::startRecording(ErrorString& errorString, const String
     if (memoryLimit)
         inspectorCanvas->setBufferLimit(*memoryLimit);
 
-    inspectorCanvas->canvas().renderingContext()->setCallTracingActive(true);
+    inspectorCanvas->context().setCallTracingActive(true);
 }
 
 void InspectorCanvasAgent::stopRecording(ErrorString& errorString, const String& canvasId)
@@ -284,12 +288,12 @@ void InspectorCanvasAgent::stopRecording(ErrorString& errorString, const String&
     if (!inspectorCanvas)
         return;
 
-    if (!inspectorCanvas->canvas().renderingContext()->callTracingActive()) {
+    if (!inspectorCanvas->context().callTracingActive()) {
         errorString = ASCIILiteral("No active recording for canvas");
         return;
     }
 
-    didFinishRecordingCanvasFrame(inspectorCanvas->canvas(), true);
+    didFinishRecordingCanvasFrame(inspectorCanvas->context(), true);
 }
 
 void InspectorCanvasAgent::requestShaderSource(ErrorString& errorString, const String& programId, const String& shaderType, String* content)
@@ -327,16 +331,16 @@ void InspectorCanvasAgent::updateShader(ErrorString& errorString, const String&
         return;
     }
 
-    WebGLRenderingContextBase* contextWebGL = inspectorProgram->context();
-    contextWebGL->shaderSource(shader, source);
-    contextWebGL->compileShader(shader);
+    WebGLRenderingContextBase& contextWebGL = inspectorProgram->context();
+    contextWebGL.shaderSource(shader, source);
+    contextWebGL.compileShader(shader);
 
     if (!shader->isValid()) {
         errorString = ASCIILiteral("Shader compilation failed.");
         return;
     }
 
-    contextWebGL->linkProgramWithoutInvalidatingAttribLocations(&inspectorProgram->program());
+    contextWebGL.linkProgramWithoutInvalidatingAttribLocations(&inspectorProgram->program());
 #else
     UNUSED_PARAM(programId);
     UNUSED_PARAM(shaderType);
@@ -369,48 +373,47 @@ void InspectorCanvasAgent::frameNavigated(Frame& frame)
 
     Vector<InspectorCanvas*> inspectorCanvases;
     for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values()) {
-        if (inspectorCanvas->canvas().document().frame() == &frame)
-            inspectorCanvases.append(inspectorCanvas.get());
+        if (auto* canvasElement = inspectorCanvas->canvasElement()) {
+            if (canvasElement->document().frame() == &frame)
+                inspectorCanvases.append(inspectorCanvas.get());
+        }
     }
 
     for (auto* inspectorCanvas : inspectorCanvases) {
+        inspectorCanvas->context().canvasBase().removeObserver(*this);
+
         String identifier = unbindCanvas(*inspectorCanvas);
         if (m_enabled)
             m_frontendDispatcher->canvasRemoved(identifier);
     }
 }
 
-void InspectorCanvasAgent::didCreateCSSCanvas(HTMLCanvasElement& canvasElement, const String& name)
+void InspectorCanvasAgent::didChangeCSSCanvasClientNodes(CanvasBase& canvasBase)
 {
-    if (findInspectorCanvas(canvasElement)) {
+    auto* context = canvasBase.renderingContext();
+    if (!context) {
         ASSERT_NOT_REACHED();
         return;
     }
 
-    ASSERT(!m_canvasToCSSCanvasName.contains(&canvasElement));
-    m_canvasToCSSCanvasName.set(&canvasElement, name);
-}
-
-void InspectorCanvasAgent::didChangeCSSCanvasClientNodes(HTMLCanvasElement& canvasElement)
-{
-    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    auto* inspectorCanvas = findInspectorCanvas(*context);
+    ASSERT(inspectorCanvas);
     if (!inspectorCanvas)
         return;
 
     m_frontendDispatcher->cssCanvasClientNodesChanged(inspectorCanvas->identifier());
 }
 
-void InspectorCanvasAgent::didCreateCanvasRenderingContext(HTMLCanvasElement& canvasElement)
+void InspectorCanvasAgent::didCreateCanvasRenderingContext(CanvasRenderingContext& context)
 {
-    if (findInspectorCanvas(canvasElement)) {
+    if (findInspectorCanvas(context)) {
         ASSERT_NOT_REACHED();
         return;
     }
 
-    canvasElement.addObserver(*this);
+    context.canvasBase().addObserver(*this);
 
-    String cssCanvasName = m_canvasToCSSCanvasName.take(&canvasElement);
-    auto inspectorCanvas = InspectorCanvas::create(canvasElement, cssCanvasName);
+    auto inspectorCanvas = InspectorCanvas::create(context);
 
     if (m_enabled) {
         const bool captureBacktrace = true;
@@ -420,23 +423,22 @@ void InspectorCanvasAgent::didCreateCanvasRenderingContext(HTMLCanvasElement& ca
     m_identifierToInspectorCanvas.set(inspectorCanvas->identifier(), WTFMove(inspectorCanvas));
 }
 
-void InspectorCanvasAgent::didChangeCanvasMemory(HTMLCanvasElement& canvasElement)
+void InspectorCanvasAgent::didChangeCanvasMemory(CanvasRenderingContext& context)
 {
-    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    auto* inspectorCanvas = findInspectorCanvas(context);
+    ASSERT(inspectorCanvas);
     if (!inspectorCanvas)
         return;
 
-    m_frontendDispatcher->canvasMemoryChanged(inspectorCanvas->identifier(), canvasElement.memoryCost());
+    // FIXME: <https://webkit.org/b/180833> Web Inspector: support OffscreenCanvas for Canvas related operations
+
+    if (auto* node = inspectorCanvas->canvasElement())
+        m_frontendDispatcher->canvasMemoryChanged(inspectorCanvas->identifier(), node->memoryCost());
 }
 
 void InspectorCanvasAgent::recordCanvasAction(CanvasRenderingContext& canvasRenderingContext, const String& name, Vector<RecordCanvasActionVariant>&& parameters)
 {
-    auto& canvasBase = canvasRenderingContext.canvasBase();
-    auto* canvasElement = is<HTMLCanvasElement>(canvasBase) ? &downcast<HTMLCanvasElement>(canvasBase) : nullptr;
-    if (!canvasElement)
-        return;
-
-    auto* inspectorCanvas = findInspectorCanvas(*canvasElement);
+    auto* inspectorCanvas = findInspectorCanvas(canvasRenderingContext);
     ASSERT(inspectorCanvas);
     if (!inspectorCanvas)
         return;
@@ -451,12 +453,16 @@ void InspectorCanvasAgent::recordCanvasAction(CanvasRenderingContext& canvasRend
         m_canvasRecordingTimer.startOneShot(0_s);
 
     if (!inspectorCanvas->hasBufferSpace())
-        didFinishRecordingCanvasFrame(*canvasElement, true);
+        didFinishRecordingCanvasFrame(inspectorCanvas->context(), true);
 }
 
-void InspectorCanvasAgent::canvasDestroyed(HTMLCanvasElement& canvasElement)
+void InspectorCanvasAgent::canvasDestroyed(CanvasBase& canvasBase)
 {
-    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    auto* context = canvasBase.renderingContext();
+    if (!context)
+        return;
+
+    auto* inspectorCanvas = findInspectorCanvas(*context);
     ASSERT(inspectorCanvas);
     if (!inspectorCanvas)
         return;
@@ -465,7 +471,7 @@ void InspectorCanvasAgent::canvasDestroyed(HTMLCanvasElement& canvasElement)
     if (!m_enabled)
         return;
 
-    // WebCore::CanvasObserver::canvasDestroyed is called in response to the GC destroying the HTMLCanvasElement.
+    // WebCore::CanvasObserver::canvasDestroyed is called in response to the GC destroying the CanvasBase.
     // Due to the single-process model used in WebKit1, the event must be dispatched from a timer to prevent
     // the frontend from making JS allocations while the GC is still active.
     m_removedCanvasIdentifiers.append(identifier);
@@ -474,16 +480,14 @@ void InspectorCanvasAgent::canvasDestroyed(HTMLCanvasElement& canvasElement)
         m_canvasDestroyedTimer.startOneShot(0_s);
 }
 
-void InspectorCanvasAgent::didFinishRecordingCanvasFrame(HTMLCanvasElement& canvasElement, bool forceDispatch)
+void InspectorCanvasAgent::didFinishRecordingCanvasFrame(CanvasRenderingContext& context, bool forceDispatch)
 {
-    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    auto* inspectorCanvas = findInspectorCanvas(context);
     ASSERT(inspectorCanvas);
     if (!inspectorCanvas)
         return;
 
-    CanvasRenderingContext* canvasRenderingContext = inspectorCanvas->canvas().renderingContext();
-    ASSERT(canvasRenderingContext->callTracingActive());
-    if (!canvasRenderingContext->callTracingActive())
+    if (!inspectorCanvas->context().callTracingActive())
         return;
 
     if (!inspectorCanvas->hasRecordingData()) {
@@ -508,10 +512,10 @@ void InspectorCanvasAgent::didFinishRecordingCanvasFrame(HTMLCanvasElement& canv
     // FIXME: <https://webkit.org/b/176008> Web Inspector: Record actions performed on WebGL2RenderingContext
 
     Inspector::Protocol::Recording::Type type;
-    if (is<CanvasRenderingContext2D>(canvasRenderingContext))
+    if (is<CanvasRenderingContext2D>(inspectorCanvas->context()))
         type = Inspector::Protocol::Recording::Type::Canvas2D;
 #if ENABLE(WEBGL)
-    else if (is<WebGLRenderingContext>(canvasRenderingContext))
+    else if (is<WebGLRenderingContext>(inspectorCanvas->context()))
         type = Inspector::Protocol::Recording::Type::CanvasWebGL;
 #endif
     else {
@@ -535,13 +539,14 @@ void InspectorCanvasAgent::didFinishRecordingCanvasFrame(HTMLCanvasElement& canv
     inspectorCanvas->resetRecordingData();
 }
 
-void InspectorCanvasAgent::consoleStartRecordingCanvas(HTMLCanvasElement& canvasElement, JSC::ExecState& exec, JSC::JSObject* options)
+void InspectorCanvasAgent::consoleStartRecordingCanvas(CanvasRenderingContext& context, JSC::ExecState& exec, JSC::JSObject* options)
 {
-    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    auto* inspectorCanvas = findInspectorCanvas(context);
+    ASSERT(inspectorCanvas);
     if (!inspectorCanvas)
         return;
 
-    if (inspectorCanvas->canvas().renderingContext()->callTracingActive())
+    if (inspectorCanvas->context().callTracingActive())
         return;
 
     inspectorCanvas->resetRecordingData();
@@ -555,38 +560,33 @@ void InspectorCanvasAgent::consoleStartRecordingCanvas(HTMLCanvasElement& canvas
             inspectorCanvas->setBufferLimit(optionMemoryLimit.toNumber(&exec));
     }
 
-    inspectorCanvas->canvas().renderingContext()->setCallTracingActive(true);
+    inspectorCanvas->context().setCallTracingActive(true);
 }
 
 #if ENABLE(WEBGL)
 void InspectorCanvasAgent::didEnableExtension(WebGLRenderingContextBase& context, const String& extension)
 {
-    auto canvas = context.canvas();
-    if (WTF::holds_alternative<RefPtr<HTMLCanvasElement>>(canvas)) {
-        auto* inspectorCanvas = findInspectorCanvas(*WTF::get<RefPtr<HTMLCanvasElement>>(canvas));
-        if (!inspectorCanvas)
-            return;
+    auto* inspectorCanvas = findInspectorCanvas(context);
+    ASSERT(inspectorCanvas);
+    if (!inspectorCanvas)
+        return;
 
-        m_frontendDispatcher->extensionEnabled(inspectorCanvas->identifier(), extension);
-    }
+    m_frontendDispatcher->extensionEnabled(inspectorCanvas->identifier(), extension);
 }
 
 void InspectorCanvasAgent::didCreateProgram(WebGLRenderingContextBase& context, WebGLProgram& program)
 {
-    auto canvas = context.canvas();
-    if (WTF::holds_alternative<RefPtr<HTMLCanvasElement>>(canvas)) {
-        auto* inspectorCanvas = findInspectorCanvas(*WTF::get<RefPtr<HTMLCanvasElement>>(canvas));
-        ASSERT(inspectorCanvas);
-        if (!inspectorCanvas)
-            return;
+    auto* inspectorCanvas = findInspectorCanvas(context);
+    ASSERT(inspectorCanvas);
+    if (!inspectorCanvas)
+        return;
 
-        auto inspectorProgram = InspectorShaderProgram::create(program, *inspectorCanvas);
-        String programIdentifier = inspectorProgram->identifier();
-        m_identifierToInspectorProgram.set(programIdentifier, WTFMove(inspectorProgram));
+    auto inspectorProgram = InspectorShaderProgram::create(program, *inspectorCanvas);
+    String programIdentifier = inspectorProgram->identifier();
+    m_identifierToInspectorProgram.set(programIdentifier, WTFMove(inspectorProgram));
 
-        if (m_enabled)
-            m_frontendDispatcher->programCreated(inspectorCanvas->identifier(), programIdentifier);
-    }
+    if (m_enabled)
+        m_frontendDispatcher->programCreated(inspectorCanvas->identifier(), programIdentifier);
 }
 
 void InspectorCanvasAgent::willDeleteProgram(WebGLProgram& program)
@@ -626,20 +626,19 @@ void InspectorCanvasAgent::canvasDestroyedTimerFired()
 void InspectorCanvasAgent::canvasRecordingTimerFired()
 {
     for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values()) {
-        if (!inspectorCanvas->canvas().renderingContext()->callTracingActive())
+        if (!inspectorCanvas->context().callTracingActive())
             continue;
 
-        didFinishRecordingCanvasFrame(inspectorCanvas->canvas());
+        didFinishRecordingCanvasFrame(inspectorCanvas->context());
     }
 }
 
 void InspectorCanvasAgent::clearCanvasData()
 {
     for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values())
-        inspectorCanvas->canvas().removeObserver(*this);
+        inspectorCanvas->context().canvasBase().removeObserver(*this);
 
     m_identifierToInspectorCanvas.clear();
-    m_canvasToCSSCanvasName.clear();
     m_removedCanvasIdentifiers.clear();
 #if ENABLE(WEBGL)
     m_identifierToInspectorProgram.clear();
@@ -654,8 +653,6 @@ void InspectorCanvasAgent::clearCanvasData()
 
 String InspectorCanvasAgent::unbindCanvas(InspectorCanvas& inspectorCanvas)
 {
-    ASSERT(!m_canvasToCSSCanvasName.contains(&inspectorCanvas.canvas()));
-
 #if ENABLE(WEBGL)
     Vector<InspectorShaderProgram*> programsToRemove;
     for (auto& inspectorProgram : m_identifierToInspectorProgram.values()) {
@@ -684,10 +681,10 @@ InspectorCanvas* InspectorCanvasAgent::assertInspectorCanvas(ErrorString& errorS
     return inspectorCanvas.get();
 }
 
-InspectorCanvas* InspectorCanvasAgent::findInspectorCanvas(HTMLCanvasElement& canvasElement)
+InspectorCanvas* InspectorCanvasAgent::findInspectorCanvas(CanvasRenderingContext& context)
 {
     for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values()) {
-        if (&inspectorCanvas->canvas() == &canvasElement)
+        if (&inspectorCanvas->context() == &context)
             return inspectorCanvas.get();
     }
 
@@ -697,8 +694,6 @@ InspectorCanvas* InspectorCanvasAgent::findInspectorCanvas(HTMLCanvasElement& ca
 #if ENABLE(WEBGL)
 String InspectorCanvasAgent::unbindProgram(InspectorShaderProgram& inspectorProgram)
 {
-    ASSERT(inspectorProgram.context());
-
     String identifier = inspectorProgram.identifier();
     m_identifierToInspectorProgram.remove(identifier);
 
index cd302f4..4aeda19 100644 (file)
@@ -26,7 +26,7 @@
 #pragma once
 
 #include "CallTracerTypes.h"
-#include "HTMLCanvasElement.h"
+#include "CanvasBase.h"
 #include "InspectorCanvas.h"
 #include "InspectorWebAgentBase.h"
 #include "Timer.h"
@@ -81,13 +81,13 @@ public:
 
     // InspectorInstrumentation
     void frameNavigated(Frame&);
-    void didCreateCSSCanvas(HTMLCanvasElement&, const String&);
-    void didChangeCSSCanvasClientNodes(HTMLCanvasElement&);
-    void didCreateCanvasRenderingContext(HTMLCanvasElement&);
-    void didChangeCanvasMemory(HTMLCanvasElement&);
+    void didChangeCSSCanvasClientNodes(CanvasBase&);
+    void didCreateCanvasRenderingContext(CanvasRenderingContext&);
+    void willDestroyCanvasRenderingContext(CanvasRenderingContext&);
+    void didChangeCanvasMemory(CanvasRenderingContext&);
     void recordCanvasAction(CanvasRenderingContext&, const String&, Vector<RecordCanvasActionVariant>&& = { });
-    void didFinishRecordingCanvasFrame(HTMLCanvasElement&, bool forceDispatch = false);
-    void consoleStartRecordingCanvas(HTMLCanvasElement&, JSC::ExecState&, JSC::JSObject* options);
+    void didFinishRecordingCanvasFrame(CanvasRenderingContext&, bool forceDispatch = false);
+    void consoleStartRecordingCanvas(CanvasRenderingContext&, JSC::ExecState&, JSC::JSObject* options);
 #if ENABLE(WEBGL)
     void didEnableExtension(WebGLRenderingContextBase&, const String&);
     void didCreateProgram(WebGLRenderingContextBase&, WebGLProgram&);
@@ -96,9 +96,9 @@ public:
 #endif
 
     // CanvasObserver
-    void canvasChanged(HTMLCanvasElement&, const FloatRect&) override { }
-    void canvasResized(HTMLCanvasElement&) override { }
-    void canvasDestroyed(HTMLCanvasElement&) override;
+    void canvasChanged(CanvasBase&, const FloatRect&) override { }
+    void canvasResized(CanvasBase&) override { }
+    void canvasDestroyed(CanvasBase&) override;
 
 private:
     void canvasDestroyedTimerFired();
@@ -106,7 +106,7 @@ private:
     void clearCanvasData();
     String unbindCanvas(InspectorCanvas&);
     InspectorCanvas* assertInspectorCanvas(ErrorString&, const String& identifier);
-    InspectorCanvas* findInspectorCanvas(HTMLCanvasElement&);
+    InspectorCanvas* findInspectorCanvas(CanvasRenderingContext&);
 #if ENABLE(WEBGL)
     String unbindProgram(InspectorShaderProgram&);
     InspectorShaderProgram* assertInspectorProgram(ErrorString&, const String& identifier);
@@ -119,7 +119,6 @@ private:
     RefPtr<Inspector::CanvasBackendDispatcher> m_backendDispatcher;
     Inspector::InjectedScriptManager& m_injectedScriptManager;
     HashMap<String, RefPtr<InspectorCanvas>> m_identifierToInspectorCanvas;
-    HashMap<HTMLCanvasElement*, String> m_canvasToCSSCanvasName;
     Vector<String> m_removedCanvasIdentifiers;
     Timer m_canvasDestroyedTimer;
     Timer m_canvasRecordingTimer;
index 960279e..2d1c490 100644 (file)
@@ -40,7 +40,9 @@
 #include "JSCanvasRenderingContext2D.h"
 #include "JSHTMLCanvasElement.h"
 #include "JSMainThreadExecState.h"
+#include "JSOffscreenCanvas.h"
 #include "MainFrame.h"
+#include "OffscreenCanvas.h"
 #include "Page.h"
 #include "ScriptableDocumentParser.h"
 #include "Settings.h"
@@ -231,16 +233,17 @@ void PageConsoleClient::record(JSC::ExecState* exec, Ref<ScriptArguments>&& argu
     if (arguments->argumentCount() >= 2)
         options = arguments->argumentAt(1).jsValue().getObject();
 
-    if (HTMLCanvasElement* canvasElement = JSHTMLCanvasElement::toWrapped(*target->vm(), target))
-        InspectorInstrumentation::consoleStartRecordingCanvas(*canvasElement, *exec, options);
-    else if (CanvasRenderingContext2D* context2d = JSCanvasRenderingContext2D::toWrapped(*target->vm(), target))
-        InspectorInstrumentation::consoleStartRecordingCanvas(context2d->canvas(), *exec, options);
+    if (HTMLCanvasElement* canvasElement = JSHTMLCanvasElement::toWrapped(*target->vm(), target)) {
+        if (CanvasRenderingContext* context = canvasElement->renderingContext())
+            InspectorInstrumentation::consoleStartRecordingCanvas(*context, *exec, options);
+    } else if (OffscreenCanvas* offscreenCanvas = JSOffscreenCanvas::toWrapped(*target->vm(), target)) {
+        if (CanvasRenderingContext* context = offscreenCanvas->renderingContext())
+            InspectorInstrumentation::consoleStartRecordingCanvas(*context, *exec, options);
+    } else if (CanvasRenderingContext2D* context2d = JSCanvasRenderingContext2D::toWrapped(*target->vm(), target))
+        InspectorInstrumentation::consoleStartRecordingCanvas(*context2d, *exec, options);
 #if ENABLE(WEBGL)
-    else if (WebGLRenderingContext* contextWebGL = JSWebGLRenderingContext::toWrapped(*target->vm(), target)) {
-        auto canvas = contextWebGL->canvas();
-        if (WTF::holds_alternative<RefPtr<HTMLCanvasElement>>(canvas))
-            InspectorInstrumentation::consoleStartRecordingCanvas(*WTF::get<RefPtr<HTMLCanvasElement>>(canvas), *exec, options);
-    }
+    else if (WebGLRenderingContext* contextWebGL = JSWebGLRenderingContext::toWrapped(*target->vm(), target))
+        InspectorInstrumentation::consoleStartRecordingCanvas(*contextWebGL, *exec, options);
 #endif
 }
 
@@ -253,16 +256,17 @@ void PageConsoleClient::recordEnd(JSC::ExecState*, Ref<ScriptArguments>&& argume
     if (!target)
         return;
 
-    if (HTMLCanvasElement* canvasElement = JSHTMLCanvasElement::toWrapped(*target->vm(), target))
-        InspectorInstrumentation::didFinishRecordingCanvasFrame(*canvasElement, true);
-    else if (CanvasRenderingContext2D* context2d = JSCanvasRenderingContext2D::toWrapped(*target->vm(), target))
-        InspectorInstrumentation::didFinishRecordingCanvasFrame(context2d->canvas(), true);
+    if (HTMLCanvasElement* canvasElement = JSHTMLCanvasElement::toWrapped(*target->vm(), target)) {
+        if (CanvasRenderingContext* context = canvasElement->renderingContext())
+            InspectorInstrumentation::didFinishRecordingCanvasFrame(*context, true);
+    } else if (OffscreenCanvas* offscreenCanvas = JSOffscreenCanvas::toWrapped(*target->vm(), target)) {
+        if (CanvasRenderingContext* context = offscreenCanvas->renderingContext())
+            InspectorInstrumentation::didFinishRecordingCanvasFrame(*context, true);
+    } else if (CanvasRenderingContext2D* context2d = JSCanvasRenderingContext2D::toWrapped(*target->vm(), target))
+        InspectorInstrumentation::didFinishRecordingCanvasFrame(*context2d, true);
 #if ENABLE(WEBGL)
-    else if (WebGLRenderingContext* contextWebGL = JSWebGLRenderingContext::toWrapped(*target->vm(), target)) {
-        auto canvas = contextWebGL->canvas();
-        if (WTF::holds_alternative<RefPtr<HTMLCanvasElement>>(canvas))
-            InspectorInstrumentation::didFinishRecordingCanvasFrame(*WTF::get<RefPtr<HTMLCanvasElement>>(canvas), true);
-    }
+    else if (WebGLRenderingContext* contextWebGL = JSWebGLRenderingContext::toWrapped(*target->vm(), target))
+        InspectorInstrumentation::didFinishRecordingCanvasFrame(*contextWebGL, true);
 #endif
 }
 
index f6f1e56..7be1626 100644 (file)
@@ -1,3 +1,16 @@
+2018-01-04  Devin Rousso  <webkit@devinrousso.com>
+
+        Web Inspector: replace HTMLCanvasElement with CanvasRenderingContext for instrumentation logic
+        https://bugs.webkit.org/show_bug.cgi?id=180770
+
+        Reviewed by Joseph Pecoraro.
+
+        * UserInterface/Models/Canvas.js:
+        (WI.Canvas.fromPayload):
+        (WI.Canvas.prototype.get contextType):
+        (WI.Canvas.prototype.saveIdentityToCookie):
+        (WI.Canvas.prototype.get frame): Deleted.
+
 2018-01-04  Joseph Pecoraro  <pecoraro@apple.com>
 
         REGRESSION (r225709): Web Inspector: CSS Source maps not loading
index cd077d9..76a0e0f 100644 (file)
 
 WI.Canvas = class Canvas extends WI.Object
 {
-    constructor(identifier, contextType, frame, {domNode, cssCanvasName, contextAttributes, memoryCost, backtrace} = {})
+    constructor(identifier, contextType, {domNode, cssCanvasName, contextAttributes, memoryCost, backtrace} = {})
     {
         super();
 
         console.assert(identifier);
         console.assert(contextType);
-        console.assert(frame instanceof WI.Frame);
 
         this._identifier = identifier;
         this._contextType = contextType;
-        this._frame = frame;
         this._domNode = domNode || null;
         this._cssCanvasName = cssCanvasName || "";
         this._contextAttributes = contextAttributes || {};
@@ -77,8 +75,7 @@ WI.Canvas = class Canvas extends WI.Object
             console.error("Invalid canvas context type", payload.contextType);
         }
 
-        let frame = WI.frameResourceManager.frameForIdentifier(payload.frameId);
-        return new WI.Canvas(payload.canvasId, contextType, frame, {
+        return new WI.Canvas(payload.canvasId, contextType, {
             domNode: payload.nodeId ? WI.domTreeManager.nodeForId(payload.nodeId) : null,
             cssCanvasName: payload.cssCanvasName,
             contextAttributes: payload.contextAttributes,
@@ -114,7 +111,6 @@ WI.Canvas = class Canvas extends WI.Object
 
     get identifier() { return this._identifier; }
     get contextType() { return this._contextType; }
-    get frame() { return this._frame; }
     get cssCanvasName() { return this._cssCanvasName; }
     get contextAttributes() { return this._contextAttributes; }
     get extensions() { return this._extensions; }
@@ -263,8 +259,6 @@ WI.Canvas = class Canvas extends WI.Object
 
     saveIdentityToCookie(cookie)
     {
-        cookie[WI.Canvas.FrameURLCookieKey] = this._frame.url.hash;
-
         if (this._cssCanvasName)
             cookie[WI.Canvas.CSSCanvasNameCookieKey] = this._cssCanvasName;
         else if (this._domNode)