Skip to content

Commit

Permalink
Expand the grid size of the Multiply test automatically when needed
Browse files Browse the repository at this point in the history
WebKit#52

Enlarge the grid size and reduce the tile size when a larger complexity is needed
to lower the frame rate.

Refactor the spiral iterator into a separate class. Keep calling its next() method
to move to the next cell. When its isDon() returns true enlarge the iterator grid
size and resize the already created tiles to fit in the new grid size.

Add a new class for the roundedRect tile called "Tile". This class can handle the
location, size and animation of the Tile.

Add a unit test for the spiral iterator.
  • Loading branch information
shallawa committed Apr 26, 2024
1 parent f1c7edb commit 6f38f7a
Show file tree
Hide file tree
Showing 6 changed files with 1,308 additions and 92 deletions.
26 changes: 26 additions & 0 deletions MotionMark/resources/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,41 @@ Point = Utilities.createClass(
return this.x;
},

// Used when the point object is used as a size object.
set width(w)
{
this.x = w;
},

// Used when the point object is used as a size object.
get height()
{
return this.y;
},

// Used when the point object is used as a size object.
set height(h)
{
this.y = h;
},

// Used when the point object is used as a size object.
get center()
{
return new Point(this.x / 2, this.y / 2);
},

// Used when the point object is used as a size object.
area: function() {
return this.x * this.y;
},

// Used when the point object is used as a size object.
expand(width, height) {
this.x += width;
this.y += height;
},

str: function()
{
return "x = " + this.x + ", y = " + this.y;
Expand Down Expand Up @@ -320,6 +343,9 @@ Point = Utilities.createClass(
}
});

// FIXME: Add a seprate class for Size.
let Size = Point;

Utilities.extendObject(Point, {
zero: new Point(0, 0),

Expand Down
100 changes: 100 additions & 0 deletions MotionMark/resources/spiral-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2015-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

class SpiralIterator {
gridSize;
current;
direction;
size;
count;

directions = {
top: 0,
left: 1,
bottom: 2,
right: 3
};

moves = [
new Size(0, -1), // top
new Size(-1, 0), // left
new Size(0, +1), // bottom
new Size(+1, 0) // right
];

constructor(gridSize) {
this.gridSize = gridSize;
this.current = Point.zero;
this.direction = this.directions.right;
this.size = new Size(1, 1);
this.count = 0;

}

isDone() {
return this.count >= this.gridSize.area();
}

next() {
++this.count;

if (this.isDone())
return;

let direction = this.direction;
let move = this.moves[direction];

if (Math.abs(this.current.x) == Math.abs(this.current.y)) {
// Turn left.
direction = (direction + 1) % 4;

if (this.current.x >= 0 && this.current.y >= 0) {
if (this.size.width < Math.min(this.gridSize.width, this.gridSize.height))
this.size.expand(2, 2);
else if (this.size.width < this.gridSize.width)
++this.size.width;

move = this.moves[this.directions.right];
} else
move = this.moves[direction];
}

if (this.count < this.size.area()) {
this.current = this.current.add(move);
this.direction = direction;
return;
}

// Make a U-turn.
this.direction = (this.direction + 1) % 4;

if (this.direction == this.directions.left || this.direction == this.directions.right)
this.current = this.current.add(this.moves[this.direction].multiply(this.size.width++));
else
this.current = this.current.add(this.moves[this.direction].multiply(this.size.height++));

this.direction = (this.direction + 1) % 4;
}
}
1 change: 1 addition & 0 deletions MotionMark/tests/core/multiply.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
</div>
<script src="../../resources/strings.js"></script>
<script src="../../resources/extensions.js"></script>
<script src="../../resources/spiral-iterator.js"></script>
<script src="../../resources/statistics.js"></script>
<script src="../resources/math.js"></script>
<script src="../resources/main.js"></script>
Expand Down
Loading

0 comments on commit 6f38f7a

Please sign in to comment.