xref: /aosp_15_r20/external/skia/experimental/webgpu-bazel/example/index.html (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1<!DOCTYPE html>
2<html>
3<head>
4    <title>Skia+WebGPU compiled with Bazel</title>
5    <meta charset="utf-8" />
6    <meta http-equiv="X-UA-Compatible" content="IE=edge">
7    <meta name="viewport" content="width=device-width, initial-scale=1.0">
8    <script type="text/javascript" src="/build/hello-world.js"></script>
9    <style>
10        body {
11            display: flex;
12            flex-direction: column;
13            align-items: center;
14            width: 100%;
15            height: 100%;
16            margin: 0;
17            padding: 0;
18            background: #000;
19        }
20        p {
21            color: white;
22            font-family: sans-serif;
23        }
24        button {
25            margin: 20px;
26            color: white;
27            background-color: black;
28            border: none;
29            text-decoration: none;
30            font-size: 20px;
31            cursor: pointer;
32        }
33    </style>
34</head>
35<body>
36    <p id="log"></p>
37    <canvas id="webgpu-demo-canvas" width=600 height=600></canvas>
38    <div id="buttons">
39        <button id="solid-color">Solid Color</button>
40        <button id="gradient">Gradient</button>
41        <button id="runtime-effect">Runtime Effect</button>
42    </div>
43    <script type="text/javascript" charset="utf-8">
44      if (navigator.gpu) {
45        log("WebGPU detected")
46        WebGPUDemo();
47      } else {
48        log("No WebGPU support.")
49      }
50
51      function log(s) {
52        document.getElementById("log").innerText = s;
53      }
54
55      async function WebGPUDemo() {
56        const adapter = await navigator.gpu.requestAdapter();
57        if (!adapter) {
58          log("Could not load an adapter. For Chrome, try running with --enable-features=Vulkan --enable-unsafe-webgpu");
59          return;
60        }
61        const device = await adapter.requestDevice();
62        console.log(adapter, device);
63
64        const wk = await WebGPUKitInit({locateFile: (file) => "/build/"+file});
65        // https://github.com/emscripten-core/emscripten/issues/12750#issuecomment-725001907
66        wk.preinitializedWebGPUDevice = device;
67
68        const demo = new wk.Demo();
69        if (!demo.init("#webgpu-demo-canvas", 600, 600)) {
70            log("Failed to initialize Skia context");
71            return;
72        }
73
74        let demoKind = wk.DemoKind.RUNTIME_EFFECT;
75        demo.setKind(demoKind);
76        document.getElementById("runtime-effect").style["text-decoration"] = "underline";
77
78        document.getElementById("solid-color").onclick = function() {
79          demoKind = wk.DemoKind.SOLID_COLOR;
80          document.getElementById("solid-color").style["text-decoration"] = "underline";
81          document.getElementById("gradient").style["text-decoration"] = "none";
82          document.getElementById("runtime-effect").style["text-decoration"] = "none";
83        };
84        document.getElementById("gradient").onclick = function() {
85          demoKind = wk.DemoKind.GRADIENT;
86          document.getElementById("solid-color").style["text-decoration"] = "none";
87          document.getElementById("gradient").style["text-decoration"] = "underline";
88          document.getElementById("runtime-effect").style["text-decoration"] = "none";
89        };
90        document.getElementById("runtime-effect").onclick = function() {
91          demoKind = wk.DemoKind.RUNTIME_EFFECT;
92          document.getElementById("solid-color").style["text-decoration"] = "none";
93          document.getElementById("gradient").style["text-decoration"] = "none";
94          document.getElementById("runtime-effect").style["text-decoration"] = "underline";
95        };
96
97        let timestamp = 0;
98        async function frame(now) {
99          if (demoKind == wk.DemoKind.RUNTIME_EFFECT || timestamp == 0 || now - timestamp >= 1000) {
100              timestamp = now;
101              demo.setKind(demoKind);
102              await demo.draw(timestamp);
103          }
104          requestAnimationFrame(frame);
105        }
106        requestAnimationFrame(frame);
107      }
108    </script>
109</body>
110</html>
111