Web Inspector: Canvas: show WebGPU shader pipelines
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ShaderProgramTreeElement.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 WI.ShaderProgramTreeElement = class ShaderProgramTreeElement extends WI.GeneralTreeElement
27 {
28     constructor(shaderProgram)
29     {
30         console.assert(shaderProgram instanceof WI.ShaderProgram);
31
32         const subtitle = null;
33         super("shader-program", shaderProgram.displayName, subtitle, shaderProgram);
34
35         // FIXME: add support for disabling/highlighting WebGPU shader pipelines.
36         let contextType = this.representedObject.canvas.contextType;
37         if (contextType === WI.Canvas.ContextType.WebGL || contextType === WI.Canvas.ContextType.WebGL2) {
38             this._disabledImageElement = document.createElement("img");
39             this._disabledImageElement.title = WI.UIString("Disable Program");
40             this._disabledImageElement.addEventListener("click", this._disabledImageElementClicked.bind(this));
41             this.status = this._disabledImageElement;
42         }
43     }
44
45     // Protected
46
47     onattach()
48     {
49         super.onattach();
50
51         // FIXME: add support for disabling/highlighting WebGPU shader pipelines.
52         let contextType = this.representedObject.canvas.contextType;
53         if (contextType === WI.Canvas.ContextType.WebGL || contextType === WI.Canvas.ContextType.WebGL2) {
54             this.representedObject.addEventListener(WI.ShaderProgram.Event.DisabledChanged, this._handleShaderProgramDisabledChanged, this);
55
56             this.element.addEventListener("mouseover", this._handleMouseOver.bind(this));
57             this.element.addEventListener("mouseout", this._handleMouseOut.bind(this));
58         }
59     }
60
61     ondetach()
62     {
63         // FIXME: add support for disabling/highlighting WebGPU shader pipelines.
64         let contextType = this.representedObject.canvas.contextType;
65         if (contextType === WI.Canvas.ContextType.WebGL || contextType === WI.Canvas.ContextType.WebGL2)
66             this.representedObject.removeEventListener(WI.ShaderProgram.Event.DisabledChanged, this._handleShaderProgramDisabledChanged, this);
67
68         super.ondetach();
69     }
70
71     canSelectOnMouseDown(event)
72     {
73         if (this._disabledImageElement && this._disabledImageElement.contains(event.target))
74             return false;
75         return super.canSelectOnMouseDown(event);
76     }
77
78     populateContextMenu(contextMenu, event)
79     {
80         // FIXME: add support for disabling/highlighting WebGPU shader pipelines.
81         let contextType = this.representedObject.canvas.contextType;
82         if (contextType === WI.Canvas.ContextType.WebGL || contextType === WI.Canvas.ContextType.WebGL2) {
83             let disabled = this.representedObject.disabled;
84             contextMenu.appendItem(disabled ? WI.UIString("Enable Program") : WI.UIString("Disable Program"), () => {
85                 this.representedObject.disabled = !disabled;
86             });
87
88             contextMenu.appendSeparator();
89         }
90
91         super.populateContextMenu(contextMenu, event);
92     }
93
94     // Private
95
96     _disabledImageElementClicked(event)
97     {
98         this.representedObject.disabled = !this.representedObject.disabled;
99     }
100
101     _handleShaderProgramDisabledChanged(event)
102     {
103         this._listItemNode.classList.toggle("disabled", !!this.representedObject.disabled);
104         this._disabledImageElement.title = this.representedObject.disabled ? WI.UIString("Enable Program") : WI.UIString("Disable Program");
105     }
106
107     _handleMouseOver(event)
108     {
109         this.representedObject.showHighlight();
110     }
111
112     _handleMouseOut(event)
113     {
114         this.representedObject.hideHighlight();
115     }
116 };