Web Inspector: drag/drop over the sidebar should load an imported file in Canvas...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Controllers / CanvasManager.js
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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 // FIXME: CanvasManager lacks advanced multi-target support. (Canvases per-target)
27
28 WI.CanvasManager = class CanvasManager extends WI.Object
29 {
30     constructor()
31     {
32         super();
33
34         this._enabled = false;
35         this._canvasIdentifierMap = new Map;
36         this._shaderProgramIdentifierMap = new Map;
37         this._importedRecordings = new Set;
38
39         WI.Frame.addEventListener(WI.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
40     }
41
42     // Target
43
44     initializeTarget(target)
45     {
46         if (!this._enabled)
47             return;
48
49         if (target.CanvasAgent) {
50             target.CanvasAgent.enable();
51
52             if (target.CanvasAgent.setRecordingAutoCaptureFrameCount && WI.settings.canvasRecordingAutoCaptureEnabled.value && WI.settings.canvasRecordingAutoCaptureFrameCount.value)
53                 target.CanvasAgent.setRecordingAutoCaptureFrameCount(WI.settings.canvasRecordingAutoCaptureFrameCount.value);
54         }
55     }
56
57     // Static
58
59     static supportsRecordingAutoCapture()
60     {
61         return InspectorBackend.domains.Canvas && InspectorBackend.domains.Canvas.setRecordingAutoCaptureFrameCount;
62     }
63
64     // Public
65
66     get importedRecordings() { return this._importedRecordings; }
67
68     get canvases()
69     {
70         return Array.from(this._canvasIdentifierMap.values());
71     }
72
73     get shaderPrograms()
74     {
75         return Array.from(this._shaderProgramIdentifierMap.values());
76     }
77
78     async processJSON({filename, json, error})
79     {
80         if (error) {
81             WI.Recording.synthesizeError(error);
82             return;
83         }
84
85         if (typeof json !== "object" || json === null) {
86             WI.Recording.synthesizeError(WI.UIString("invalid JSON"));
87             return;
88         }
89
90         let recording = WI.Recording.fromPayload(json);
91         if (!recording)
92             return;
93
94         let extensionStart = filename.lastIndexOf(".");
95         if (extensionStart !== -1)
96             filename = filename.substring(0, extensionStart);
97         recording.createDisplayName(filename);
98
99         this._importedRecordings.add(recording);
100
101         this.dispatchEventToListeners(WI.CanvasManager.Event.RecordingImported, {recording, initiatedByUser: true});
102     }
103
104     enable()
105     {
106         console.assert(!this._enabled);
107
108         this._enabled = true;
109
110         for (let target of WI.targets)
111             this.initializeTarget(target);
112     }
113
114     disable()
115     {
116         console.assert(this._enabled);
117
118         for (let target of WI.targets) {
119             if (target.CanvasAgent)
120                 target.CanvasAgent.disable();
121         }
122
123         this._canvasIdentifierMap.clear();
124         this._shaderProgramIdentifierMap.clear();
125         this._importedRecordings.clear();
126
127         this._enabled = false;
128     }
129
130     setRecordingAutoCaptureFrameCount(enabled, count)
131     {
132         console.assert(!isNaN(count) && count >= 0);
133
134         for (let target of WI.targets) {
135             if (target.CanvasAgent)
136                 target.CanvasAgent.setRecordingAutoCaptureFrameCount(enabled ? count : 0);
137         }
138
139         WI.settings.canvasRecordingAutoCaptureEnabled.value = enabled && count;
140         WI.settings.canvasRecordingAutoCaptureFrameCount.value = count;
141     }
142
143     canvasAdded(canvasPayload)
144     {
145         // Called from WI.CanvasObserver.
146
147         console.assert(!this._canvasIdentifierMap.has(canvasPayload.canvasId), `Canvas already exists with id ${canvasPayload.canvasId}.`);
148
149         let canvas = WI.Canvas.fromPayload(canvasPayload);
150         this._canvasIdentifierMap.set(canvas.identifier, canvas);
151
152         this.dispatchEventToListeners(WI.CanvasManager.Event.CanvasAdded, {canvas});
153     }
154
155     canvasRemoved(canvasIdentifier)
156     {
157         // Called from WI.CanvasObserver.
158
159         let canvas = this._canvasIdentifierMap.take(canvasIdentifier);
160         console.assert(canvas);
161         if (!canvas)
162             return;
163
164         this._removeCanvas(canvas);
165     }
166
167     canvasMemoryChanged(canvasIdentifier, memoryCost)
168     {
169         // Called from WI.CanvasObserver.
170
171         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
172         console.assert(canvas);
173         if (!canvas)
174             return;
175
176         canvas.memoryCost = memoryCost;
177     }
178
179     cssCanvasClientNodesChanged(canvasIdentifier)
180     {
181         // Called from WI.CanvasObserver.
182
183         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
184         console.assert(canvas);
185         if (!canvas)
186             return;
187
188         canvas.cssCanvasClientNodesChanged();
189     }
190
191     recordingStarted(canvasIdentifier, initiator)
192     {
193         // Called from WI.CanvasObserver.
194
195         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
196         console.assert(canvas);
197         if (!canvas)
198             return;
199
200         canvas.recordingStarted(initiator);
201     }
202
203     recordingProgress(canvasIdentifier, framesPayload, bufferUsed)
204     {
205         // Called from WI.CanvasObserver.
206
207         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
208         console.assert(canvas);
209         if (!canvas)
210             return;
211
212         canvas.recordingProgress(framesPayload, bufferUsed);
213     }
214
215     recordingFinished(canvasIdentifier, recordingPayload)
216     {
217         // Called from WI.CanvasObserver.
218
219         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
220         console.assert(canvas);
221         if (!canvas)
222             return;
223
224         canvas.recordingFinished(recordingPayload);
225     }
226
227     extensionEnabled(canvasIdentifier, extension)
228     {
229         // Called from WI.CanvasObserver.
230
231         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
232         console.assert(canvas);
233         if (!canvas)
234             return;
235
236         canvas.enableExtension(extension);
237     }
238
239     programCreated(canvasIdentifier, programIdentifier)
240     {
241         // Called from WI.CanvasObserver.
242
243         let canvas = this._canvasIdentifierMap.get(canvasIdentifier);
244         console.assert(canvas);
245         if (!canvas)
246             return;
247
248         console.assert(!this._shaderProgramIdentifierMap.has(programIdentifier), `ShaderProgram already exists with id ${programIdentifier}.`);
249
250         let program = new WI.ShaderProgram(programIdentifier, canvas);
251         this._shaderProgramIdentifierMap.set(program.identifier, program);
252
253         canvas.shaderProgramCollection.add(program);
254     }
255
256     programDeleted(programIdentifier)
257     {
258         // Called from WI.CanvasObserver.
259
260         let program = this._shaderProgramIdentifierMap.take(programIdentifier);
261         console.assert(program);
262         if (!program)
263             return;
264
265         program.canvas.shaderProgramCollection.remove(program);
266     }
267
268     // Private
269
270     _removeCanvas(canvas)
271     {
272         for (let program of canvas.shaderProgramCollection)
273             this._shaderProgramIdentifierMap.delete(program.identifier);
274
275         canvas.shaderProgramCollection.clear();
276
277         for (let recording of canvas.recordingCollection) {
278             recording.source = null;
279             recording.createDisplayName(recording.displayName);
280         }
281
282         this.dispatchEventToListeners(WI.CanvasManager.Event.CanvasRemoved, {canvas});
283     }
284
285     _mainResourceDidChange(event)
286     {
287         console.assert(event.target instanceof WI.Frame);
288         if (!event.target.isMainFrame())
289             return;
290
291         WI.Canvas.resetUniqueDisplayNameNumbers();
292
293         for (let canvas of this._canvasIdentifierMap.values())
294             this._removeCanvas(canvas);
295
296         this._shaderProgramIdentifierMap.clear();
297         this._canvasIdentifierMap.clear();
298     }
299 };
300
301 WI.CanvasManager.Event = {
302     CanvasAdded: "canvas-manager-canvas-was-added",
303     CanvasRemoved: "canvas-manager-canvas-was-removed",
304     RecordingImported: "canvas-manager-recording-imported",
305 };