Web Inspector: replace Canvas stack iteration with createScriptCallStack
[WebKit-https.git] / Source / WebCore / inspector / InspectorCanvas.cpp
1 /*
2  * Copyright (C) 2017 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "InspectorCanvas.h"
28
29 #include "AffineTransform.h"
30 #include "CachedImage.h"
31 #include "CanvasGradient.h"
32 #include "CanvasPattern.h"
33 #include "CanvasRenderingContext2D.h"
34 #include "Document.h"
35 #include "FloatPoint.h"
36 #include "Frame.h"
37 #include "Gradient.h"
38 #include "HTMLCanvasElement.h"
39 #include "HTMLImageElement.h"
40 #include "HTMLVideoElement.h"
41 #include "Image.h"
42 #include "ImageBitmap.h"
43 #include "ImageBuffer.h"
44 #include "ImageData.h"
45 #include "InspectorDOMAgent.h"
46 #include "InspectorPageAgent.h"
47 #include "InstrumentingAgents.h"
48 #include "JSCanvasDirection.h"
49 #include "JSCanvasFillRule.h"
50 #include "JSCanvasLineCap.h"
51 #include "JSCanvasLineJoin.h"
52 #include "JSCanvasTextAlign.h"
53 #include "JSCanvasTextBaseline.h"
54 #include "JSImageSmoothingQuality.h"
55 #include "JSMainThreadExecState.h"
56 #include "Path2D.h"
57 #include "Pattern.h"
58 #include "RecordingSwizzleTypes.h"
59 #include "SVGPathUtilities.h"
60 #include "StringAdaptors.h"
61 #if ENABLE(WEBGL)
62 #include "WebGLRenderingContext.h"
63 #endif
64 #if ENABLE(WEBGL2)
65 #include "WebGL2RenderingContext.h"
66 #endif
67 #if ENABLE(WEBGPU)
68 #include "WebGPURenderingContext.h"
69 #endif
70 #include <inspector/IdentifiersFactory.h>
71 #include <inspector/ScriptCallStack.h>
72 #include <inspector/ScriptCallStackFactory.h>
73
74
75 namespace WebCore {
76 using namespace Inspector;
77
78 Ref<InspectorCanvas> InspectorCanvas::create(HTMLCanvasElement& canvas, const String& cssCanvasName)
79 {
80     return adoptRef(*new InspectorCanvas(canvas, cssCanvasName));
81 }
82
83 InspectorCanvas::InspectorCanvas(HTMLCanvasElement& canvas, const String& cssCanvasName)
84     : m_identifier("canvas:" + IdentifiersFactory::createIdentifier())
85     , m_canvas(canvas)
86     , m_cssCanvasName(cssCanvasName)
87 {
88 }
89
90 InspectorCanvas::~InspectorCanvas()
91 {
92     resetRecordingData();
93 }
94
95 void InspectorCanvas::resetRecordingData()
96 {
97     m_initialState = nullptr;
98     m_frames = nullptr;
99     m_currentActions = nullptr;
100     m_actionNeedingSnapshot = nullptr;
101     m_serializedDuplicateData = nullptr;
102     m_indexedDuplicateData.clear();
103     m_bufferLimit = 100 * 1024 * 1024;
104     m_bufferUsed = 0;
105     m_singleFrame = true;
106
107     m_canvas.renderingContext()->setCallTracingActive(false);
108 }
109
110 bool InspectorCanvas::hasRecordingData() const
111 {
112     return m_initialState && m_frames;
113 }
114
115 static bool shouldSnapshotWebGLAction(const String& name)
116 {
117     return name == "clear"
118         || name == "drawArrays"
119         || name == "drawElements";
120 }
121
122 void InspectorCanvas::recordAction(const String& name, Vector<RecordCanvasActionVariant>&& parameters)
123 {
124     if (!hasRecordingData()) {
125         m_initialState = buildInitialState();
126         m_bufferUsed += m_initialState->memoryCost();
127
128         m_frames = Inspector::Protocol::Array<Inspector::Protocol::Recording::Frame>::create();
129     }
130
131     if (!m_currentActions) {
132         m_currentActions = Inspector::Protocol::Array<InspectorValue>::create();
133
134         auto frame = Inspector::Protocol::Recording::Frame::create()
135             .setActions(m_currentActions)
136             .release();
137
138         m_frames->addItem(WTFMove(frame));
139     }
140
141     appendActionSnapshotIfNeeded();
142
143     auto action = buildAction(name, WTFMove(parameters));
144     m_bufferUsed += action->memoryCost();
145     m_currentActions->addItem(action);
146
147 #if ENABLE(WEBGL)
148     if (is<WebGLRenderingContext>(m_canvas.renderingContext()) && shouldSnapshotWebGLAction(name))
149         m_actionNeedingSnapshot = action;
150 #endif
151 }
152
153 RefPtr<Inspector::Protocol::Recording::InitialState>&& InspectorCanvas::releaseInitialState()
154 {
155     return WTFMove(m_initialState);
156 }
157
158 RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Recording::Frame>>&& InspectorCanvas::releaseFrames()
159 {
160     appendActionSnapshotIfNeeded();
161
162     return WTFMove(m_frames);
163 }
164
165 RefPtr<Inspector::Protocol::Array<InspectorValue>>&& InspectorCanvas::releaseData()
166 {
167     m_indexedDuplicateData.clear();
168     return WTFMove(m_serializedDuplicateData);
169 }
170
171 void InspectorCanvas::markNewFrame()
172 {
173     m_currentActions = nullptr;
174 }
175
176 void InspectorCanvas::markCurrentFrameIncomplete()
177 {
178     if (!m_currentActions)
179         return;
180
181     static_cast<Inspector::Protocol::Recording::Frame*>(m_frames->get(m_frames->length() - 1).get())->setIncomplete(true);
182 }
183
184 void InspectorCanvas::setBufferLimit(long memoryLimit)
185 {
186     m_bufferLimit = std::min<long>(memoryLimit, std::numeric_limits<int>::max());
187 }
188
189 bool InspectorCanvas::hasBufferSpace() const
190 {
191     return m_bufferUsed < m_bufferLimit;
192 }
193
194 Ref<Inspector::Protocol::Canvas::Canvas> InspectorCanvas::buildObjectForCanvas(InstrumentingAgents& instrumentingAgents, bool captureBacktrace)
195 {
196     Document& document = m_canvas.document();
197     Frame* frame = document.frame();
198     CanvasRenderingContext* context = m_canvas.renderingContext();
199
200     Inspector::Protocol::Canvas::ContextType contextType;
201     if (is<CanvasRenderingContext2D>(context))
202         contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
203 #if ENABLE(WEBGL)
204     else if (is<WebGLRenderingContext>(context))
205         contextType = Inspector::Protocol::Canvas::ContextType::WebGL;
206 #endif
207 #if ENABLE(WEBGL2)
208     else if (is<WebGL2RenderingContext>(context))
209         contextType = Inspector::Protocol::Canvas::ContextType::WebGL2;
210 #endif
211 #if ENABLE(WEBGPU)
212     else if (is<WebGPURenderingContext>(context))
213         contextType = Inspector::Protocol::Canvas::ContextType::WebGPU;
214 #endif
215     else {
216         ASSERT_NOT_REACHED();
217         contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
218     }
219
220     auto canvas = Inspector::Protocol::Canvas::Canvas::create()
221         .setCanvasId(m_identifier)
222         .setFrameId(instrumentingAgents.inspectorPageAgent()->frameId(frame))
223         .setContextType(contextType)
224         .release();
225
226     if (!m_cssCanvasName.isEmpty())
227         canvas->setCssCanvasName(m_cssCanvasName);
228     else {
229         InspectorDOMAgent* domAgent = instrumentingAgents.inspectorDOMAgent();
230         int nodeId = domAgent->boundNodeId(&m_canvas);
231         if (!nodeId) {
232             if (int documentNodeId = domAgent->boundNodeId(&m_canvas.document())) {
233                 ErrorString ignored;
234                 nodeId = domAgent->pushNodeToFrontend(ignored, documentNodeId, &m_canvas);
235             }
236         }
237
238         if (nodeId)
239             canvas->setNodeId(nodeId);
240     }
241
242 #if ENABLE(WEBGL)
243     if (is<WebGLRenderingContextBase>(context)) {
244         if (std::optional<WebGLContextAttributes> attributes = downcast<WebGLRenderingContextBase>(context)->getContextAttributes()) {
245             canvas->setContextAttributes(Inspector::Protocol::Canvas::ContextAttributes::create()
246                 .setAlpha(attributes->alpha)
247                 .setDepth(attributes->depth)
248                 .setStencil(attributes->stencil)
249                 .setAntialias(attributes->antialias)
250                 .setPremultipliedAlpha(attributes->premultipliedAlpha)
251                 .setPreserveDrawingBuffer(attributes->preserveDrawingBuffer)
252                 .setFailIfMajorPerformanceCaveat(attributes->failIfMajorPerformanceCaveat)
253                 .release());
254         }
255     }
256 #endif
257
258     if (size_t memoryCost = m_canvas.memoryCost())
259         canvas->setMemoryCost(memoryCost);
260
261     if (captureBacktrace) {
262         auto stackTrace = Inspector::createScriptCallStack(JSMainThreadExecState::currentState(), Inspector::ScriptCallStack::maxCallStackSizeToCapture);
263         canvas->setBacktrace(stackTrace->buildInspectorArray());
264     }
265
266     return canvas;
267 }
268
269 void InspectorCanvas::appendActionSnapshotIfNeeded()
270 {
271     if (!m_actionNeedingSnapshot)
272         return;
273
274     m_actionNeedingSnapshot->addItem(indexForData(getCanvasContentAsDataURL()));
275     m_actionNeedingSnapshot = nullptr;
276 }
277
278 String InspectorCanvas::getCanvasContentAsDataURL()
279 {
280 #if ENABLE(WEBGL)
281     CanvasRenderingContext* canvasRenderingContext = m_canvas.renderingContext();
282     if (is<WebGLRenderingContextBase>(canvasRenderingContext))
283         downcast<WebGLRenderingContextBase>(canvasRenderingContext)->setPreventBufferClearForInspector(true);
284 #endif
285
286     ExceptionOr<UncachedString> result = m_canvas.toDataURL(ASCIILiteral("image/png"));
287
288 #if ENABLE(WEBGL)
289     if (is<WebGLRenderingContextBase>(canvasRenderingContext))
290         downcast<WebGLRenderingContextBase>(canvasRenderingContext)->setPreventBufferClearForInspector(false);
291 #endif
292
293     if (result.hasException())
294         return String();
295
296     return result.releaseReturnValue().string;
297 }
298
299 int InspectorCanvas::indexForData(DuplicateDataVariant data)
300 {
301     size_t index = m_indexedDuplicateData.find(data);
302     if (index != notFound) {
303         ASSERT(index < std::numeric_limits<int>::max());
304         return static_cast<int>(index);
305     }
306
307     if (!m_serializedDuplicateData)
308         m_serializedDuplicateData = Inspector::Protocol::Array<InspectorValue>::create();
309
310     RefPtr<InspectorValue> item;
311     WTF::switchOn(data,
312         [&] (const HTMLImageElement* imageElement) {
313             String dataURL = ASCIILiteral("data:,");
314
315             if (CachedImage* cachedImage = imageElement->cachedImage()) {
316                 Image* image = cachedImage->image();
317                 if (image && image != &Image::nullImage()) {
318                     std::unique_ptr<ImageBuffer> imageBuffer = ImageBuffer::create(image->size(), RenderingMode::Unaccelerated);
319                     imageBuffer->context().drawImage(*image, FloatPoint(0, 0));
320                     dataURL = imageBuffer->toDataURL("image/png");
321                 }
322             }
323
324             item = InspectorValue::create(dataURL);
325         },
326 #if ENABLE(VIDEO)
327         [&] (HTMLVideoElement* videoElement) {
328             String dataURL = ASCIILiteral("data:,");
329
330             unsigned videoWidth = videoElement->videoWidth();
331             unsigned videoHeight = videoElement->videoHeight();
332             std::unique_ptr<ImageBuffer> imageBuffer = ImageBuffer::create(FloatSize(videoWidth, videoHeight), RenderingMode::Unaccelerated);
333             if (imageBuffer) {
334                 videoElement->paintCurrentFrameInContext(imageBuffer->context(), FloatRect(0, 0, videoWidth, videoHeight));
335                 dataURL = imageBuffer->toDataURL("image/png");
336             }
337
338             item = InspectorValue::create(dataURL);
339         },
340 #endif
341         [&] (HTMLCanvasElement* canvasElement) {
342             String dataURL = ASCIILiteral("data:,");
343
344             ExceptionOr<UncachedString> result = canvasElement->toDataURL(ASCIILiteral("image/png"));
345             if (!result.hasException())
346                 dataURL = result.releaseReturnValue().string;
347
348             item = InspectorValue::create(dataURL);
349         },
350         [&] (const CanvasGradient* canvasGradient) { item = buildArrayForCanvasGradient(*canvasGradient); },
351         [&] (const CanvasPattern* canvasPattern) { item = buildArrayForCanvasPattern(*canvasPattern); },
352         [&] (const ImageData* imageData) { item = buildArrayForImageData(*imageData); },
353         [&] (const ImageBitmap* imageBitmap) { item = buildArrayForImageBitmap(*imageBitmap); },
354         [&] (const ScriptCallFrame& scriptCallFrame) {
355             auto array = Inspector::Protocol::Array<double>::create();
356             array->addItem(indexForData(scriptCallFrame.functionName()));
357             array->addItem(indexForData(scriptCallFrame.sourceURL()));
358             array->addItem(static_cast<int>(scriptCallFrame.lineNumber()));
359             array->addItem(static_cast<int>(scriptCallFrame.columnNumber()));
360             item = WTFMove(array);
361         },
362         [&] (const String& value) { item = InspectorValue::create(value); }
363     );
364
365     m_bufferUsed += item->memoryCost();
366     m_serializedDuplicateData->addItem(WTFMove(item));
367
368     m_indexedDuplicateData.append(data);
369     index = m_indexedDuplicateData.size() - 1;
370
371     ASSERT(index < std::numeric_limits<int>::max());
372     return static_cast<int>(index);
373 }
374
375 static RefPtr<Inspector::Protocol::Array<double>> buildArrayForAffineTransform(const AffineTransform& affineTransform)
376 {
377     RefPtr<Inspector::Protocol::Array<double>> array = Inspector::Protocol::Array<double>::create();
378     array->addItem(affineTransform.a());
379     array->addItem(affineTransform.b());
380     array->addItem(affineTransform.c());
381     array->addItem(affineTransform.d());
382     array->addItem(affineTransform.e());
383     array->addItem(affineTransform.f());
384     return array;
385 }
386
387 template <typename T>
388 static RefPtr<Inspector::Protocol::Array<InspectorValue>> buildArrayForVector(const Vector<T>& vector)
389 {
390     RefPtr<Inspector::Protocol::Array<InspectorValue>> array = Inspector::Protocol::Array<InspectorValue>::create();
391     for (auto& item : vector)
392         array->addItem(item);
393     return array;
394 }
395
396 RefPtr<Inspector::Protocol::Recording::InitialState> InspectorCanvas::buildInitialState()
397 {
398     RefPtr<Inspector::Protocol::Recording::InitialState> initialState = Inspector::Protocol::Recording::InitialState::create()
399         .release();
400
401     auto attributes = InspectorObject::create();
402     attributes->setInteger(ASCIILiteral("width"), canvas().width());
403     attributes->setInteger(ASCIILiteral("height"), canvas().height());
404
405     auto parameters = Inspector::Protocol::Array<InspectorValue>::create();
406
407     CanvasRenderingContext* canvasRenderingContext = canvas().renderingContext();
408     if (is<CanvasRenderingContext2D>(canvasRenderingContext)) {
409         const CanvasRenderingContext2D* context2d = downcast<CanvasRenderingContext2D>(canvasRenderingContext);
410         const CanvasRenderingContext2D::State& state = context2d->state();
411
412         attributes->setArray(ASCIILiteral("setTransform"), buildArrayForAffineTransform(state.transform));
413         attributes->setDouble(ASCIILiteral("globalAlpha"), context2d->globalAlpha());
414         attributes->setInteger(ASCIILiteral("globalCompositeOperation"), indexForData(context2d->globalCompositeOperation()));
415         attributes->setDouble(ASCIILiteral("lineWidth"), context2d->lineWidth());
416         attributes->setInteger(ASCIILiteral("lineCap"), indexForData(convertEnumerationToString(context2d->lineCap())));
417         attributes->setInteger(ASCIILiteral("lineJoin"), indexForData(convertEnumerationToString(context2d->lineJoin())));
418         attributes->setDouble(ASCIILiteral("miterLimit"), context2d->miterLimit());
419         attributes->setDouble(ASCIILiteral("shadowOffsetX"), context2d->shadowOffsetX());
420         attributes->setDouble(ASCIILiteral("shadowOffsetY"), context2d->shadowOffsetY());
421         attributes->setDouble(ASCIILiteral("shadowBlur"), context2d->shadowBlur());
422         attributes->setInteger(ASCIILiteral("shadowColor"), indexForData(context2d->shadowColor()));
423
424         // The parameter to `setLineDash` is itself an array, so we need to wrap the parameters
425         // list in an array to allow spreading.
426         auto setLineDash = Inspector::Protocol::Array<InspectorValue>::create();
427         setLineDash->addItem(buildArrayForVector(state.lineDash));
428         attributes->setArray(ASCIILiteral("setLineDash"), WTFMove(setLineDash));
429
430         attributes->setDouble(ASCIILiteral("lineDashOffset"), context2d->lineDashOffset());
431         attributes->setInteger(ASCIILiteral("font"), indexForData(context2d->font()));
432         attributes->setInteger(ASCIILiteral("textAlign"), indexForData(convertEnumerationToString(context2d->textAlign())));
433         attributes->setInteger(ASCIILiteral("textBaseline"), indexForData(convertEnumerationToString(context2d->textBaseline())));
434         attributes->setInteger(ASCIILiteral("direction"), indexForData(convertEnumerationToString(context2d->direction())));
435
436         int strokeStyleIndex;
437         if (CanvasGradient* canvasGradient = state.strokeStyle.canvasGradient())
438             strokeStyleIndex = indexForData(canvasGradient);
439         else if (CanvasPattern* canvasPattern = state.strokeStyle.canvasPattern())
440             strokeStyleIndex = indexForData(canvasPattern);
441         else
442             strokeStyleIndex = indexForData(state.strokeStyle.color());
443         attributes->setInteger(ASCIILiteral("strokeStyle"), strokeStyleIndex);
444
445         int fillStyleIndex;
446         if (CanvasGradient* canvasGradient = state.fillStyle.canvasGradient())
447             fillStyleIndex = indexForData(canvasGradient);
448         else if (CanvasPattern* canvasPattern = state.fillStyle.canvasPattern())
449             fillStyleIndex = indexForData(canvasPattern);
450         else
451             fillStyleIndex = indexForData(state.fillStyle.color());
452         attributes->setInteger(ASCIILiteral("fillStyle"), fillStyleIndex);
453
454         attributes->setBoolean(ASCIILiteral("imageSmoothingEnabled"), context2d->imageSmoothingEnabled());
455         attributes->setInteger(ASCIILiteral("imageSmoothingQuality"), indexForData(convertEnumerationToString(context2d->imageSmoothingQuality())));
456
457         auto setPath = Inspector::Protocol::Array<InspectorValue>::create();
458         setPath->addItem(indexForData(buildStringFromPath(context2d->getPath()->path())));
459         attributes->setArray(ASCIILiteral("setPath"), WTFMove(setPath));
460     }
461 #if ENABLE(WEBGL)
462     else if (is<WebGLRenderingContextBase>(canvasRenderingContext)) {
463         WebGLRenderingContextBase* contextWebGLBase = downcast<WebGLRenderingContextBase>(canvasRenderingContext);
464         if (std::optional<WebGLContextAttributes> attributes = contextWebGLBase->getContextAttributes()) {
465             RefPtr<InspectorObject> contextAttributes = InspectorObject::create();
466             contextAttributes->setBoolean(ASCIILiteral("alpha"), attributes->alpha);
467             contextAttributes->setBoolean(ASCIILiteral("depth"), attributes->depth);
468             contextAttributes->setBoolean(ASCIILiteral("stencil"), attributes->stencil);
469             contextAttributes->setBoolean(ASCIILiteral("antialias"), attributes->antialias);
470             contextAttributes->setBoolean(ASCIILiteral("premultipliedAlpha"), attributes->premultipliedAlpha);
471             contextAttributes->setBoolean(ASCIILiteral("preserveDrawingBuffer"), attributes->preserveDrawingBuffer);
472             contextAttributes->setBoolean(ASCIILiteral("failIfMajorPerformanceCaveat"), attributes->failIfMajorPerformanceCaveat);
473             parameters->addItem(WTFMove(contextAttributes));
474         }
475     }
476 #endif
477
478     initialState->setAttributes(WTFMove(attributes));
479
480     if (parameters->length())
481         initialState->setParameters(WTFMove(parameters));
482
483     initialState->setContent(getCanvasContentAsDataURL());
484
485     return initialState;
486 }
487
488 RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> InspectorCanvas::buildAction(const String& name, Vector<RecordCanvasActionVariant>&& parameters)
489 {
490     RefPtr<Inspector::Protocol::Array<InspectorValue>> action = Inspector::Protocol::Array<InspectorValue>::create();
491     action->addItem(indexForData(name));
492
493     RefPtr<Inspector::Protocol::Array<InspectorValue>> parametersData = Inspector::Protocol::Array<Inspector::InspectorValue>::create();
494     RefPtr<Inspector::Protocol::Array<int>> swizzleTypes = Inspector::Protocol::Array<int>::create();
495
496     auto addParameter = [&parametersData, &swizzleTypes] (auto value, RecordingSwizzleTypes swizzleType) {
497         parametersData->addItem(value);
498         swizzleTypes->addItem(static_cast<int>(swizzleType));
499     };
500
501     for (RecordCanvasActionVariant& item : parameters) {
502         WTF::switchOn(item,
503             [&] (CanvasDirection value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
504             [&] (CanvasFillRule value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
505             [&] (CanvasLineCap value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
506             [&] (CanvasLineJoin value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
507             [&] (CanvasTextAlign value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
508             [&] (CanvasTextBaseline value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
509             [&] (const DOMMatrix2DInit& value) {
510                 RefPtr<Inspector::Protocol::Array<double>> array = Inspector::Protocol::Array<double>::create();
511                 array->addItem(value.a.value_or(1));
512                 array->addItem(value.b.value_or(0));
513                 array->addItem(value.c.value_or(0));
514                 array->addItem(value.d.value_or(1));
515                 array->addItem(value.e.value_or(0));
516                 array->addItem(value.f.value_or(0));
517                 addParameter(WTFMove(array), RecordingSwizzleTypes::DOMMatrix);
518             },
519             [&] (const Element*) {
520                 // Elements are not serializable, so add a string as a placeholder since the actual
521                 // element cannot be reconstructed in the frontend.
522                 addParameter(indexForData("Element"), RecordingSwizzleTypes::None);
523             },
524             [&] (HTMLImageElement* value) { addParameter(indexForData(value), RecordingSwizzleTypes::Image); },
525             [&] (ImageData* value) { addParameter(indexForData(value), RecordingSwizzleTypes::ImageData); },
526             [&] (ImageSmoothingQuality value) { addParameter(indexForData(convertEnumerationToString(value)), RecordingSwizzleTypes::String); },
527             [&] (const Path2D* value) { addParameter(indexForData(buildStringFromPath(value->path())), RecordingSwizzleTypes::Path2D); },
528 #if ENABLE(WEBGL)
529             // FIXME: <https://webkit.org/b/176009> Web Inspector: send data for WebGL objects during a recording instead of a placeholder string
530             [&] (const WebGLBuffer*) { addParameter(0, RecordingSwizzleTypes::WebGLBuffer); },
531             [&] (const WebGLFramebuffer*) { addParameter(0, RecordingSwizzleTypes::WebGLFramebuffer); },
532             [&] (const WebGLProgram*) { addParameter(0, RecordingSwizzleTypes::WebGLProgram); },
533             [&] (const WebGLRenderbuffer*) { addParameter(0, RecordingSwizzleTypes::WebGLRenderbuffer); },
534             [&] (const WebGLShader*) { addParameter(0, RecordingSwizzleTypes::WebGLShader); },
535             [&] (const WebGLTexture*) { addParameter(0, RecordingSwizzleTypes::WebGLTexture); },
536             [&] (const WebGLUniformLocation*) { addParameter(0, RecordingSwizzleTypes::WebGLUniformLocation); },
537 #endif
538             [&] (const RefPtr<ArrayBuffer>&) { addParameter(0, RecordingSwizzleTypes::TypedArray); },
539             [&] (const RefPtr<ArrayBufferView>&) { addParameter(0, RecordingSwizzleTypes::TypedArray); },
540             [&] (const RefPtr<CanvasGradient>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::CanvasGradient); },
541             [&] (const RefPtr<CanvasPattern>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::CanvasPattern); },
542             [&] (const RefPtr<Float32Array>&) { addParameter(0, RecordingSwizzleTypes::TypedArray); },
543             [&] (RefPtr<HTMLCanvasElement>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::Image); },
544             [&] (const RefPtr<HTMLImageElement>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::Image); },
545 #if ENABLE(VIDEO)
546             [&] (RefPtr<HTMLVideoElement>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::Image); },
547 #endif
548             [&] (const RefPtr<ImageBitmap>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::ImageBitmap); },
549             [&] (const RefPtr<ImageData>& value) { addParameter(indexForData(value.get()), RecordingSwizzleTypes::ImageData); },
550             [&] (const RefPtr<Int32Array>&) { addParameter(0, RecordingSwizzleTypes::TypedArray); },
551             [&] (const Vector<float>& value) { addParameter(buildArrayForVector(value), RecordingSwizzleTypes::Array); },
552             [&] (const Vector<int>& value) { addParameter(buildArrayForVector(value), RecordingSwizzleTypes::Array); },
553             [&] (const String& value) { addParameter(indexForData(value), RecordingSwizzleTypes::String); },
554             [&] (double value) { addParameter(value, RecordingSwizzleTypes::Number); },
555             [&] (float value) { addParameter(value, RecordingSwizzleTypes::Number); },
556             [&] (int64_t value) { addParameter(static_cast<double>(value), RecordingSwizzleTypes::Number); },
557             [&] (uint32_t value) { addParameter(static_cast<double>(value), RecordingSwizzleTypes::Number); },
558             [&] (int32_t value) { addParameter(value, RecordingSwizzleTypes::Number); },
559             [&] (uint8_t value) { addParameter(static_cast<int>(value), RecordingSwizzleTypes::Number); },
560             [&] (bool value) { addParameter(value, RecordingSwizzleTypes::Boolean); }
561         );
562     }
563
564     action->addItem(WTFMove(parametersData));
565     action->addItem(WTFMove(swizzleTypes));
566
567     RefPtr<Inspector::Protocol::Array<double>> trace = Inspector::Protocol::Array<double>::create();
568     auto stackTrace = Inspector::createScriptCallStack(JSMainThreadExecState::currentState(), Inspector::ScriptCallStack::maxCallStackSizeToCapture);
569     for (size_t i = 0; i < stackTrace->size(); ++i)
570         trace->addItem(indexForData(stackTrace->at(i)));
571     action->addItem(WTFMove(trace));
572
573     return action;
574 }
575
576 RefPtr<Inspector::Protocol::Array<InspectorValue>> InspectorCanvas::buildArrayForCanvasGradient(const CanvasGradient& canvasGradient)
577 {
578     const Gradient& gradient = canvasGradient.gradient();
579     bool isRadial = gradient.isRadial();
580
581     String type = isRadial ? ASCIILiteral("radial-gradient") : ASCIILiteral("linear-gradient");
582
583     RefPtr<Inspector::Protocol::Array<float>> parameters = Inspector::Protocol::Array<float>::create();
584     parameters->addItem(gradient.p0().x());
585     parameters->addItem(gradient.p0().y());
586     if (isRadial)
587         parameters->addItem(gradient.startRadius());
588     parameters->addItem(gradient.p1().x());
589     parameters->addItem(gradient.p1().y());
590     if (isRadial)
591         parameters->addItem(gradient.endRadius());
592
593     RefPtr<Inspector::Protocol::Array<InspectorValue>> stops = Inspector::Protocol::Array<InspectorValue>::create();
594     for (const Gradient::ColorStop& colorStop : gradient.stops()) {
595         RefPtr<Inspector::Protocol::Array<InspectorValue>> stop = Inspector::Protocol::Array<InspectorValue>::create();
596         stop->addItem(colorStop.offset);
597         stop->addItem(indexForData(colorStop.color.cssText()));
598         stops->addItem(WTFMove(stop));
599     }
600
601     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> array = Inspector::Protocol::Array<Inspector::InspectorValue>::create();
602     array->addItem(indexForData(type));
603     array->addItem(WTFMove(parameters));
604     array->addItem(WTFMove(stops));
605     return array;
606 }
607
608 RefPtr<Inspector::Protocol::Array<InspectorValue>> InspectorCanvas::buildArrayForCanvasPattern(const CanvasPattern& canvasPattern)
609 {
610     Image& tileImage = canvasPattern.pattern().tileImage();
611     std::unique_ptr<ImageBuffer> imageBuffer = ImageBuffer::create(tileImage.size(), RenderingMode::Unaccelerated);
612     imageBuffer->context().drawImage(tileImage, FloatPoint(0, 0));
613
614     String repeat;
615     bool repeatX = canvasPattern.pattern().repeatX();
616     bool repeatY = canvasPattern.pattern().repeatY();
617     if (repeatX && repeatY)
618         repeat = ASCIILiteral("repeat");
619     else if (repeatX && !repeatY)
620         repeat = ASCIILiteral("repeat-x");
621     else if (!repeatX && repeatY)
622         repeat = ASCIILiteral("repeat-y");
623     else
624         repeat = ASCIILiteral("no-repeat");
625
626     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> array = Inspector::Protocol::Array<Inspector::InspectorValue>::create();
627     array->addItem(indexForData(imageBuffer->toDataURL("image/png")));
628     array->addItem(indexForData(repeat));
629     return array;
630 }
631
632 RefPtr<Inspector::Protocol::Array<InspectorValue>> InspectorCanvas::buildArrayForImageData(const ImageData& imageData)
633 {
634     RefPtr<Inspector::Protocol::Array<int>> data = Inspector::Protocol::Array<int>::create();
635     for (size_t i = 0; i < imageData.data()->length(); ++i)
636         data->addItem(imageData.data()->item(i));
637
638     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> array = Inspector::Protocol::Array<Inspector::InspectorValue>::create();
639     array->addItem(WTFMove(data));
640     array->addItem(imageData.width());
641     array->addItem(imageData.height());
642     return array;
643 }
644
645 RefPtr<Inspector::Protocol::Array<InspectorValue>> InspectorCanvas::buildArrayForImageBitmap(const ImageBitmap& imageBitmap)
646 {
647     // FIXME: Needs to include the data somehow.
648     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> array = Inspector::Protocol::Array<Inspector::InspectorValue>::create();
649     array->addItem(static_cast<int>(imageBitmap.width()));
650     array->addItem(static_cast<int>(imageBitmap.height()));
651     return array;
652 }
653
654 } // namespace WebCore
655