Home | History | Annotate | Download | only in Canvas
      1 <!DOCTYPE html>
      2 <html>
      3 <body>
      4 <script src="../resources/runner.js"></script>
      5 <script src="resources/canvas_runner.js"></script>
      6 <script>
      7 var sourceCanvas2D = document.createElement("canvas");
      8 var sourceCtx2D = sourceCanvas2D.getContext("2d");
      9 var destCanvas2D = document.createElement("canvas");
     10 var destCtx2D = destCanvas2D.getContext("2d");
     11  
     12 function setSize(sourceWidth, sourceHeight, destWidth, destHeight) {
     13     sourceCanvas2D.width = sourceWidth;
     14     sourceCanvas2D.height = sourceHeight;
     15     destCanvas2D.width = destWidth;
     16     destCanvas2D.height = destHeight;
     17 }
     18 
     19 function rand(range) {
     20     return Math.floor(Math.random() * range);
     21 }
     22 
     23 function fillCanvas(ctx2d, canvas2d) {
     24     ctx2d.fillStyle = "rgba(" + rand(255) + "," + rand(255) + "," + rand(255)  + "," + rand(255) + ")";
     25     ctx2d.fillRect(0, 0, canvas2d.width, canvas2d.height);
     26 }
     27 
     28 function doRun() {
     29     fillCanvas(sourceCtx2D, sourceCanvas2D);
     30     // draw dynamic Canvas
     31     destCtx2D.drawImage(sourceCanvas2D, 0, 0);
     32 }
     33 
     34 function ensureComplete() {
     35     // Calling getImageData() is just to flush out the content when
     36     // accelerated 2D canvas is in use. The cost of reading 1x1 pixels is low.
     37     destCtx2D.getImageData(0, 0, 1, 1);
     38 }
     39 
     40 window.onload = function () {
     41     // It should use setMinimumAccelerated2dCanvasSize() to enable accelerated Canvas for a specified size
     42     // but this API is not available in JS or WebPage. Assume the threshold size is 256x257
     43     // and the dest canvas is HW accelerated Canvas when setting its size to 1024x1024.
     44     setSize(1024, 1024, 1024, 1024);
     45     fillCanvas(sourceCtx2D, sourceCanvas2D);
     46     CanvasRunner.start({
     47         description: "This bench test checks the speed on drawing dynamic 2d Canvas(1024x1024) to HW accelerated Canvas2D(1024x1024).",
     48         doRun: doRun,
     49         ensureComplete: ensureComplete});
     50 }
     51 
     52 </script>
     53 </body>
     54 </html>
     55