Refactor tune() to not return the complexity of the scene.
[WebKit-https.git] / PerformanceTests / Animometer / tests / simple / resources / tiled-canvas-image.js
1 (function() {
2
3 CanvasImageTile = Utilities.createClass(
4     function(stage, source)
5     {
6         this._context = stage.context;
7         this._size = stage.tileSize;
8         this.source = source;
9     }, {
10
11     getImageData: function()
12     {
13         this._imagedata = this._context.getImageData(this.source.x, this.source.y, this._size.width, this._size.height);
14     },
15
16     putImageData: function(destination)
17     {
18         this._context.putImageData(this._imagedata, destination.x, destination.y);
19     }
20 });
21
22 TiledCanvasImageStage = Utilities.createSubclass(Stage,
23     function(element, options)
24     {
25         Stage.call(this);
26     }, {
27
28     initialize: function(benchmark, options)
29     {
30         Stage.prototype.initialize.call(this, benchmark, options);
31         this.context = this.element.getContext("2d");
32         this._setupTiles();
33     },
34
35     _setupTiles: function()
36     {
37         const maxTilesPerRow = 50;
38         const maxTilesPerCol = 50;
39
40         this.tileSize = this.size.multiply(new Point(1 / maxTilesPerRow, 1 / maxTilesPerCol));
41
42         this._tiles = new Array(maxTilesPerRow * maxTilesPerCol);
43
44         var source = Point.zero();
45         for (var index = 0; index < this._tiles.length; ++index) {
46             this._tiles[index] = new CanvasImageTile(this, source);
47             source = this._nextTilePosition(source);
48         }
49
50         this._ctiles = 0;
51     },
52
53     _nextTilePosition: function(destination)
54     {
55         var next = destination.add(this.tileSize);
56
57         if (next.x >= this._size.width)
58             return new Point(0, next.y >= this._size.height ? 0 : next.y);
59
60         return new Point(next.x, destination.y);
61     },
62
63     tune: function(count)
64     {
65         this._ctiles += count;
66
67         this._ctiles = Math.max(this._ctiles, 0);
68         this._ctiles = Math.min(this._ctiles, this._tiles.length);
69     },
70
71     _drawBackground: function()
72     {
73         var size = this._benchmark._stage.size;
74         var gradient = this.context.createLinearGradient(0, 0, size.width, 0);
75         gradient.addColorStop(0, "red");
76         gradient.addColorStop(1, "white");
77         this.context.save();
78             this.context.fillStyle = gradient;
79             this.context.fillRect(0, 0, size.width, size.height);
80         this.context.restore();
81     },
82
83     animate: function(timeDelta)
84     {
85         this._drawBackground();
86
87         if (!this._ctiles)
88             return;
89
90         this._tiles.shuffle();
91
92         var destinations = new Array(this._ctiles);
93         for (var index = 0; index < this._ctiles; ++index) {
94             this._tiles[index].getImageData();
95             destinations[index] = this._tiles[index].source;
96         }
97
98         destinations.shuffle();
99
100         for (var index = 0; index < this._ctiles; ++index)
101             this._tiles[index].putImageData(destinations[index]);
102     },
103
104     complexity: function()
105     {
106         return this._ctiles;
107     }
108 });
109
110 TiledCanvasImageBenchmark = Utilities.createSubclass(Benchmark,
111     function(options)
112     {
113         Benchmark.call(this, new TiledCanvasImageStage(), options);
114     }
115 );
116
117 window.benchmarkClass = TiledCanvasImageBenchmark;
118
119 })();