aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/wasm/shell.html
blob: f38429c755773498a9b65ca8a8cdf490b55bbebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Skia and WASM</title>
    <style>
      .emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
      textarea.emscripten { font-family: monospace; width: 80%; }
      div.emscripten { text-align: center; }
      div.emscripten_border { border: 1px solid black; }
      /* the canvas *must not* have any border or padding, or mouse coords will be wrong */
      canvas.emscripten { border: 0px none; background-color: black; }

      .spinner {
        height: 50px;
        width: 50px;
        margin: 0px auto;
        -webkit-animation: rotation .8s linear infinite;
        -moz-animation: rotation .8s linear infinite;
        -o-animation: rotation .8s linear infinite;
        animation: rotation 0.8s linear infinite;
        border-left: 10px solid rgb(0,150,240);
        border-right: 10px solid rgb(0,150,240);
        border-bottom: 10px solid rgb(0,150,240);
        border-top: 10px solid rgb(100,0,200);
        border-radius: 100%;
        background-color: rgb(200,100,250);
      }
      @-webkit-keyframes rotation {
        from {-webkit-transform: rotate(0deg);}
        to {-webkit-transform: rotate(360deg);}
      }
      @-moz-keyframes rotation {
        from {-moz-transform: rotate(0deg);}
        to {-moz-transform: rotate(360deg);}
      }
      @-o-keyframes rotation {
        from {-o-transform: rotate(0deg);}
        to {-o-transform: rotate(360deg);}
      }
      @keyframes rotation {
        from {transform: rotate(0deg);}
        to {transform: rotate(360deg);}
      }

    </style>
  </head>
  <body>
    <hr/>
    <figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
    <div class="emscripten" id="status">Downloading...</div>
    <div class="emscripten">
      <progress value="0" max="100" id="progress" hidden=1></progress>
    </div>
    <div class="emscripten_border">
      <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
    </div>
    <svg id=svg xmlns='http://www.w3.org/2000/svg' width=50 height=50 viewPort='0 0 50 50'>
    </svg>
    <hr/>
    <div class="emscripten">
      <input type="checkbox" id="resize">Resize canvas
      <input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer
      &nbsp;&nbsp;&nbsp;
      <input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked,
                                                                                document.getElementById('resize').checked)">
    </div>

    <hr/>
    <textarea class="emscripten" id="output" rows="8"></textarea>
    <hr>
    <script type='text/javascript'>
      function entryPoint() {
        // See https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#call-compiled-c-c-code-directly-from-javascript
        const FAST_TIMES = 100 + 11 /*warmups*/;
        const SLOW_TIMES = 10 + 11 /*warmups*/;

        var path;
        for (let i = 0; i< FAST_TIMES; i++){
          if (i === 10) {
            // let it "warm up"
            console.time('Path_with_calls'); // about 3.9s for 1M iterations
          }
          path = new Module.SkPath();
          path.moveTo(0,0);
          path.lineTo(40,0);
          path.lineTo(20,20);
          path.close();
          path.moveTo(20,0);
          path.lineTo(60,0);
          path.lineTo(40,20);
          path.close();
          if (i < FAST_TIMES - 1) {
            path.delete();
          }
        }
        console.timeEnd('Path_with_calls');

        var result;
        for(let i = 0; i < SLOW_TIMES; i++) {
          if (i === 10) {
            console.time('simplify');     // about 5.4s for 100k iterations
          }
          result = Module.SimplifyPath(path);
          if (i < SLOW_TIMES - 1) {
            result.delete();
          }
        }
        console.timeEnd('simplify');

        var p2d;
        for(let i = 0; i < FAST_TIMES; i++) {
          if (i === 10) {
            console.time('ToPath2D');     // about 6.3s for 1M iterations
          }
          p2d = Module.ToPath2D(result, new Path2D());
        }
        console.timeEnd('ToPath2D');

        let ctx = Module.canvas.getContext('2d')
        ctx.strokeStyle = 'red';
        ctx.stroke(p2d);

        var svgPtr;
        for(let i=0; i < FAST_TIMES; i++) {
          if (i === 10) {
            console.time('ToSVGString (simple)');   // about 7.1s for 1M iterations
          }
          svgPtr = Module.ToSVGString(result);
        }
        console.timeEnd('ToSVGString (simple)');
        Module.print('Got SVG Data: ' + svgPtr);

        var mountains;
        for(let i=0; i < FAST_TIMES; i++) {
          if (i === 10) {
            console.time('FromSVGString (simple)');
          }
          mountains = Module.FromSVGString(svgPtr);
          if (i < FAST_TIMES - 1) {
            mountains.delete();
          }
        }
        console.timeEnd('FromSVGString (simple)');

        path.delete();
        result.delete();
        mountains.delete();

        //==============================================================================

        function uint8TypedArray(arr) {
          const ta = new Uint8ClampedArray(arr.length);
          for (let i = 0; i < arr.length; i++){
            ta[i] = arr[i];
          }

          retVal = Module._malloc(ta.length * ta.BYTES_PER_ELEMENT);
          Module.HEAPU8.set(ta, retVal / ta.BYTES_PER_ELEMENT);
          return retVal;
        }

        function floatTypedArray(arr) {
          const ta = new Float32Array(arr.length);
          for (let i = 0; i < arr.length; i++){
            ta[i] = arr[i];
          }

          retVal = Module._malloc(ta.length * ta.BYTES_PER_ELEMENT);
          Module.HEAPF32.set(ta, retVal / ta.BYTES_PER_ELEMENT);
          return retVal;
        }
        var path4;
        for (let i = 0; i< FAST_TIMES; i++) {
          if (i === 10) {
            console.time('path_with_typed_array'); // about 3.6s for 1M iterations
          }
          let verbs = [];
          let args = [];

          verbs.push(Module.MOVE_VERB);
          args.push(100); args.push(0);
          verbs.push(Module.LINE_VERB);
          args.push(140); args.push(0);
          verbs.push(Module.LINE_VERB);
          args.push(120); args.push(20);
          verbs.push(Module.CLOSE_VERB);

          verbs.push(Module.MOVE_VERB);
          args.push(120); args.push(0);
          verbs.push(Module.LINE_VERB);
          args.push(160); args.push(0);
          verbs.push(Module.LINE_VERB);
          args.push(140); args.push(20);
          verbs.push(Module.CLOSE_VERB);

          let tVerbs = uint8TypedArray(verbs);
          let tArgs =  floatTypedArray(args);

          path4 = Module.SkPathFromVerbsArgsTyped(tVerbs, verbs.length,
                                                  tArgs, args.length);
          if (i < FAST_TIMES - 1) {
            path4.delete();
          }
          Module._free(tVerbs);
          Module._free(tArgs);
        }
        console.timeEnd('path_with_typed_array');
        //path4.dump();

        var v4, a4;
        for (let i = 0; i< FAST_TIMES; i++) {
          if (i === 10) {
            console.time('to_verbs_args_array'); // about 5.4s for 1M iterations
          }
          v4 = [];
          a4 = [];
          Module.SkPathToVerbsArgsArray(path4, v4, a4);
        }
        console.timeEnd('to_verbs_args_array');

        Module.print(`Got path with ${v4.length} verbs and ${a4.length} args`);
        Module.print(v4[0] + ' (0 for value is MoveTo, 1 for LineTo, etc)');
        Module.print(a4[0], a4[1] + ' (coordinates)');

        path4.delete();
        //==============================================================================

        let p1 = new Module.SkPath();
        p1.moveTo(0,60);
        p1.lineTo(40,60);
        p1.lineTo(20,80);
        p1.close();
        let p2 = new Module.SkPath();
        p2.moveTo(20,60);
        p2.lineTo(60,60);
        p2.lineTo(40,80);
        p2.close();

        var p3;
        for (let i = 0; i< SLOW_TIMES; i++) {
          if (i === 10) {
            console.time('ApplyPathOp');       // about 5.2s for 100k iterations
          }
          p3 = Module.ApplyPathOp(p1, p2, Module.PathOp.INTERSECT);
          if (i < SLOW_TIMES - 1) {
            p3.delete();
          }
        }
        console.timeEnd('ApplyPathOp');

        ctx = Module.canvas.getContext('2d');
        ctx.strokeStyle = 'green';
        // Alternative way to use Path2D (potentially for browsers that
        // don't support the Path2D?)
        ctx.beginPath();
        Module.ToPath2D(p3, ctx);
        ctx.stroke();

        var builder, p4;
        for (let i = 0; i< SLOW_TIMES; i++) {
          if (i === 10) {
            console.time('PathOpBuilder');       // about 12.3s for 100k iterations
          }
          builder = new Module.SkOpBuilder();
          builder.add(p1, Module.PathOp.UNION);
          builder.add(p2, Module.PathOp.UNION);
          builder.add(p3, Module.PathOp.DIFFERENCE);
          p4 = Module.ResolveBuilder(builder);
          if (i < SLOW_TIMES - 1) {
            p4.delete();
          }
          builder.delete();
        }
        console.timeEnd('PathOpBuilder');


        p2d = Module.ToPath2D(p4, new Path2D());

        ctx = Module.canvas.getContext('2d');
        ctx.fillStyle = 'purple';
        ctx.strokeStyle = 'white';
        ctx.fill(p2d);
        ctx.stroke(p2d);

        p1.delete();
        p2.delete();
        p3.delete();
        p4.delete();

//=====================================================================================

        function floatTypedArrayFrom2D(arr) {
          // expects 2d array where index 0 is verb and index 1-n are args
          let len = 0;
          for (cmd of arr) {
            len += cmd.length;
          }

          const ta = new Float32Array(len);
          let i = 0;
          for (cmd of arr) {
            for (c of cmd) {
              ta[i] = c;
              i++;
            }
          }

          retVal = Module._malloc(ta.length * ta.BYTES_PER_ELEMENT);
          Module.HEAPF32.set(ta, retVal / ta.BYTES_PER_ELEMENT);
          return [retVal, len];
        }

        function SkPathFromCmdTyped(cmdArr) {
          let [cmd, len] = floatTypedArrayFrom2D(cmdArr);
          let path = Module.SkPathFromCmdTyped(cmd, len);
          Module._free(cmd);
          return path;
        }

        var path5;
        for (let i = 0; i< FAST_TIMES; i++) {
          if (i === 10) {
            console.time('path_cmd_typed (simple)'); // about 4.6s for 1M iterations
          }
          let commands = [];

          commands.push([Module.MOVE_VERB, 100, 60]);
          commands.push([Module.LINE_VERB, 140, 60]);
          commands.push([Module.LINE_VERB, 120, 80]);
          commands.push([Module.CLOSE_VERB]);

          commands.push([Module.MOVE_VERB, 120, 60]);
          commands.push([Module.LINE_VERB, 160, 60]);
          commands.push([Module.LINE_VERB, 140, 80]);
          commands.push([Module.CLOSE_VERB]);

          path5 = SkPathFromCmdTyped(commands);

          if (i < FAST_TIMES - 1) {
            path5.delete();
          }
        }
        console.timeEnd('path_cmd_typed (simple)');

        p2d = Module.ToPath2D(path5, new Path2D());

        ctx = Module.canvas.getContext('2d');
        ctx.fillStyle = 'white';
        ctx.fill(p2d);

        var cmd5;
        for (let i = 0; i< FAST_TIMES; i++) {
          if (i === 10) {
            console.time('to_cmd_array'); // about 9.1s for 1M iterations
          }
          cmd5 = Module.SkPathToCmdArray(path5);
        }
        console.timeEnd('to_cmd_array');


        Module.print(`Got path with ${cmd5.length} commands`);
        Module.print(cmd5[0][0] + ' (0 for value is MoveTo, 1 for LineTo, etc)');
        Module.print(cmd5[0][1], cmd5[0][2] + ' (coordinates)');
        //console.log(cmd5);

        path5.delete();
        //==========================================================
        const svgPath = 'M6 18c0 .55.45 1 1 1h1v3.5c0 .83.67 1.5 1.5 1.5s1.5-.67 1.5-1.5V19h2v3.5c0 .83.67 1.5 1.5 1.5s1.5-.67 1.5-1.5V19h1c.55 0 1-.45 1-1V8H6v10zM3.5 8C2.67 8 2 8.67 2 9.5v7c0 .83.67 1.5 1.5 1.5S5 17.33 5 16.5v-7C5 8.67 4.33 8 3.5 8zm17 0c-.83 0-1.5.67-1.5 1.5v7c0 .83.67 1.5 1.5 1.5s1.5-.67 1.5-1.5v-7c0-.83-.67-1.5-1.5-1.5zm-4.97-5.84l1.3-1.3c.2-.2.2-.51 0-.71-.2-.2-.51-.2-.71 0l-1.48 1.48C13.85 1.23 12.95 1 12 1c-.96 0-1.86.23-2.66.63L7.85.15c-.2-.2-.51-.2-.71 0-.2.2-.2.51 0 .71l1.31 1.31C6.97 3.26 6 5.01 6 7h12c0-1.99-.97-3.75-2.47-4.84zM10 5H9V4h1v1zm5 0h-1V4h1v1z';

        var android;
        for(let i=0; i < SLOW_TIMES; i++) {
          if (i === 10) {
            console.time('FromSVGString (moderate)'); // about 6.8s for 100k iterations
          }
          android = Module.FromSVGString(svgPath);
          if (i < SLOW_TIMES - 1) {
            android.delete();
          }
        }
        console.timeEnd('FromSVGString (moderate)');

        result = Module.SimplifyPath(android);
        //result.dump();
        let androidSimple = Module.ToSVGString(result);

        let newPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
        newPath.setAttribute('fill', 'rgb(0,200,0)');
        newPath.setAttribute('d', androidSimple);
        document.getElementById('svg').appendChild(newPath);

        androidCMDs = Module.SkPathToCmdArray(android);

        android.delete();
        result.delete();

        var android2;
        for (let i = 0; i< FAST_TIMES; i++) {
          if (i === 10) {
            console.time('path_cmd_typed (moderate)'); // about 2.2s for 100k
          }

          android2 = SkPathFromCmdTyped(androidCMDs);

          if (i < FAST_TIMES - 1) {
            android2.delete();
          }
        }
        console.timeEnd('path_cmd_typed (moderate)');
        var android2Str;
        for(let i=0; i < SLOW_TIMES; i++) {
          if (i === 10) {
            console.time('ToSVGString (moderate)');
          }
          android2Str = Module.ToSVGString(android2);
        }
        console.timeEnd('ToSVGString (moderate)');

        newPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
        newPath.setAttribute('fill', 'rgb(0,0,200)');
        newPath.setAttribute('d', android2Str);
        newPath.setAttribute('transform', 'translate(25 0)');
        document.getElementById('svg').appendChild(newPath);

        android2.delete();

        // ===========================================================

        let float = Module.SkBits2Float(parseInt("0x3f2aaaab"));
        Module.print('SkBits2Float(0x3f2aaaab) = 0.666667', float);

        p1 = new Module.SkPath();
        p1.moveTo(0,60);
        p1.lineTo(40,60);
        p1.lineTo(20,80);
        p1.close();
        p2 = new Module.SkPath();
        p2.moveTo(20,60);
        p2.lineTo(60,60);
        p2.lineTo(40,80);
        p2.close();

        let rgnA = new Module.SkRegion();
        let rgnB = new Module.SkRegion();
        let openClip = new Module.SkRegion();
        let rgnOut = new Module.SkRegion();
        openClip.setRect(-16000, -16000, 16000, 16000);
        rgnA.setPath(p1, openClip);
        rgnB.setPath(p2, openClip);
        rgnOut.opRegionAB(rgnA, rgnB, Module.RegionOp.INTERSECT);
        let pathOut = Module.GetBoundaryPathFromRegion(rgnOut);

        p1.delete();
        p2.delete();
        rgnA.delete();
        rgnB.delete();
        openClip.delete();
        rgnOut.delete();
        pathOut.delete();
      }
    </script>
    <script type='text/javascript'>
      var statusElement = document.getElementById('status');
      var progressElement = document.getElementById('progress');
      var spinnerElement = document.getElementById('spinner');

      var Module = {
        preRun: [],
        postRun: [entryPoint],
        print: (function() {
          var element = document.getElementById('output');
          if (element) element.value = ''; // clear browser cache
          return function(text) {
            if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
            // These replacements are necessary if you render to raw HTML
            //text = text.replace(/&/g, "&amp;");
            //text = text.replace(/</g, "&lt;");
            //text = text.replace(/>/g, "&gt;");
            //text = text.replace('\n', '<br>', 'g');
            console.log(text);
            if (element) {
              element.value += text + "\n";
              element.scrollTop = element.scrollHeight; // focus on bottom
            }
          };
        })(),
        printErr: function(text) {
          if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
          if (0) { // XXX disabled for safety typeof dump == 'function') {
            dump(text + '\n'); // fast, straight to the real console
          } else {
            console.error(text);
          }
        },
        canvas: (function() {
          var canvas = document.getElementById('canvas');

          // As a default initial behavior, pop up an alert when webgl context is lost. To make your
          // application robust, you may want to override this behavior before shipping!
          // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
          canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);

          return canvas;
        })(),
        setStatus: function(text) {
          if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
          if (text === Module.setStatus.last.text) return;
          var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
          var now = Date.now();
          if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
          Module.setStatus.last.time = now;
          Module.setStatus.last.text = text;
          if (m) {
            text = m[1];
            progressElement.value = parseInt(m[2])*100;
            progressElement.max = parseInt(m[4])*100;
            progressElement.hidden = false;
            spinnerElement.hidden = false;
          } else {
            progressElement.value = null;
            progressElement.max = null;
            progressElement.hidden = true;
            if (!text) spinnerElement.hidden = true;
          }
          statusElement.innerHTML = text;
        },
        totalDependencies: 0,
        monitorRunDependencies: function(left) {
          this.totalDependencies = Math.max(this.totalDependencies, left);
          Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
        }
      };
      Module.setStatus('Downloading...');
      window.onerror = function() {
        Module.setStatus('Exception thrown, see JavaScript console');
        spinnerElement.style.display = 'none';
        Module.setStatus = function(text) {
          if (text) Module.printErr('[post-exception status] ' + text);
        };
      };
    </script>
    {{{ SCRIPT }}}
  </body>
</html>