Unreviewed, rolling out r224863.
[WebKit-https.git] / Source / WebCore / inspector / InspectorCanvas.h
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 #pragma once
27
28 #include "CallTracerTypes.h"
29 #include <inspector/InspectorProtocolObjects.h>
30 #include <inspector/InspectorValues.h>
31 #include <inspector/ScriptCallFrame.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/Ref.h>
34 #include <wtf/RefPtr.h>
35 #include <wtf/Variant.h>
36 #include <wtf/Vector.h>
37 #include <wtf/text/WTFString.h>
38
39 namespace WebCore {
40
41 class CanvasGradient;
42 class CanvasPattern;
43 class HTMLCanvasElement;
44 class HTMLImageElement;
45 class HTMLVideoElement;
46 class ImageBitmap;
47 class ImageData;
48 class InstrumentingAgents;
49
50 typedef String ErrorString;
51
52 class InspectorCanvas final : public RefCounted<InspectorCanvas> {
53 public:
54     static Ref<InspectorCanvas> create(HTMLCanvasElement&, const String& cssCanvasName);
55
56     const String& identifier() { return m_identifier; }
57     HTMLCanvasElement& canvas() { return m_canvas; }
58     const String& cssCanvasName() { return m_cssCanvasName; }
59
60     void resetRecordingData();
61     bool hasRecordingData() const;
62     bool currentFrameHasData() const;
63     void recordAction(const String&, Vector<RecordCanvasActionVariant>&& = { });
64
65     RefPtr<Inspector::Protocol::Recording::InitialState>&& releaseInitialState();
66     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Recording::Frame>>&& releaseFrames();
67     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>>&& releaseData();
68
69     void finalizeFrame();
70     void markCurrentFrameIncomplete();
71
72     void setBufferLimit(long);
73     bool hasBufferSpace() const;
74     long bufferUsed() const { return m_bufferUsed; }
75
76     bool singleFrame() const { return m_singleFrame; }
77     void setSingleFrame(bool singleFrame) { m_singleFrame = singleFrame; }
78
79     Ref<Inspector::Protocol::Canvas::Canvas> buildObjectForCanvas(InstrumentingAgents&, bool captureBacktrace);
80
81     ~InspectorCanvas();
82
83 private:
84     InspectorCanvas(HTMLCanvasElement&, const String& cssCanvasName);
85     void appendActionSnapshotIfNeeded();
86     String getCanvasContentAsDataURL();
87
88     typedef Variant<
89         CanvasGradient*,
90         CanvasPattern*,
91         HTMLCanvasElement*,
92         HTMLImageElement*,
93 #if ENABLE(VIDEO)
94         HTMLVideoElement*,
95 #endif
96         ImageData*,
97         ImageBitmap*,
98         Inspector::ScriptCallFrame,
99         String
100     > DuplicateDataVariant;
101
102     int indexForData(DuplicateDataVariant);
103     RefPtr<Inspector::Protocol::Recording::InitialState> buildInitialState();
104     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> buildAction(const String&, Vector<RecordCanvasActionVariant>&& = { });
105     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> buildArrayForCanvasGradient(const CanvasGradient&);
106     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> buildArrayForCanvasPattern(const CanvasPattern&);
107     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> buildArrayForImageData(const ImageData&);
108     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> buildArrayForImageBitmap(const ImageBitmap&);
109
110     String m_identifier;
111     HTMLCanvasElement& m_canvas;
112     String m_cssCanvasName;
113
114     RefPtr<Inspector::Protocol::Recording::InitialState> m_initialState;
115     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Recording::Frame>> m_frames;
116     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> m_currentActions;
117     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> m_actionNeedingSnapshot;
118     RefPtr<Inspector::Protocol::Array<Inspector::InspectorValue>> m_serializedDuplicateData;
119     Vector<DuplicateDataVariant> m_indexedDuplicateData;
120     double m_currentFrameStartTime { NAN };
121     size_t m_bufferLimit { 100 * 1024 * 1024 };
122     size_t m_bufferUsed { 0 };
123     bool m_singleFrame { true };
124 };
125
126 } // namespace WebCore
127