<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[169287] trunk/PerformanceTests</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/169287">169287</a></dd>
<dt>Author</dt> <dd>ggaren@apple.com</dd>
<dt>Date</dt> <dd>2014-05-23 15:37:30 -0700 (Fri, 23 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Performance testing, diamond-square terrain generation + canvas
https://bugs.webkit.org/show_bug.cgi?id=133054

Reviewed by Ryosuke Niwa.

This test was written by Hunter Loftis. It originally appeared on his
blog @ &lt;http://www.playfuljs.com/realistic-terrain-in-130-lines/&gt;.

I did a bit of editing for format and benchmark suitability.

* Canvas/terrain.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkPerformanceTestsChangeLog">trunk/PerformanceTests/ChangeLog</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkPerformanceTestsCanvasterrainhtml">trunk/PerformanceTests/Canvas/terrain.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkPerformanceTestsCanvasterrainhtml"></a>
<div class="addfile"><h4>Added: trunk/PerformanceTests/Canvas/terrain.html (0 => 169287)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/PerformanceTests/Canvas/terrain.html                                (rev 0)
+++ trunk/PerformanceTests/Canvas/terrain.html        2014-05-23 22:37:30 UTC (rev 169287)
</span><span class="lines">@@ -0,0 +1,183 @@
</span><ins>+&lt;!-- BSD License
+
+Copyright (c) 2014, Hunter Loftis.

+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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; 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 THE COPYRIGHT HOLDER OR 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.
+ --&gt;
+
+&lt;html&gt;
+    &lt;head&gt;
+        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, maximum-scale=1.0&quot;&gt;
+        &lt;title&gt;Terrain Demo - PlayfulJS&lt;/title&gt;
+        &lt;script src=&quot;../resources/runner.js&quot;&gt;&lt;/script&gt;
+    &lt;/head&gt;
+    &lt;body&gt;
+        &lt;canvas id='display' style='background: #000'&gt;&lt;/canvas&gt;
+        
+        &lt;script&gt;
+        Math.random = (function() {
+           var seed = 49734321;
+           return function() {
+               /* The Jenkins hash function:
+                   &lt;http://en.wikipedia.org/wiki/Jenkins_hash_function&gt;
+                   &lt;http://burtleburtle.net/bob/hash/integer.html&gt;
+                */
+               seed = ((seed + 0x7ed55d16) + (seed &lt;&lt; 12)) &amp; 0xffffffff;
+               seed = ((seed ^ 0xc761c23c) ^ (seed &gt;&gt;&gt; 19)) &amp; 0xffffffff;
+               seed = ((seed + 0x165667b1) + (seed &lt;&lt; 5)) &amp; 0xffffffff;
+               seed = ((seed + 0xd3a2646c) ^ (seed &lt;&lt; 9)) &amp; 0xffffffff;
+               seed = ((seed + 0xfd7046c5) + (seed &lt;&lt; 3)) &amp; 0xffffffff;
+               seed = ((seed ^ 0xb55a4f09) ^ (seed &gt;&gt;&gt; 16)) &amp; 0xffffffff;
+               return (seed &amp; 0xfffffff) / 0x10000000;
+           };
+        })();
+       
+        function Terrain(detail) {
+           this.size = Math.pow(2, detail) + 1;
+           this.max = this.size - 1;
+           this.map = new Float32Array(this.size * this.size);
+        }
+        
+        Terrain.prototype.get = function(x, y) {
+            if (x &lt; 0 || x &gt; this.max || y &lt; 0 || y &gt; this.max)
+                return -1;
+            return this.map[x + this.size * y];
+        };
+        
+        Terrain.prototype.set = function(x, y, val) {
+            this.map[x + this.size * y] = val;
+        };
+        
+        Terrain.prototype.generate = function(roughness) {
+            var self = this;
+            
+            this.set(0, 0, self.max);
+            this.set(this.max, 0, self.max / 2);
+            this.set(this.max, this.max, 0);
+            this.set(0, this.max, self.max / 2);
+            
+            divide(this.max);
+            
+            function divide(size) {
+                var x, y, half = size / 2;
+                var scale = roughness * size;
+                if (half &lt; 1)
+                    return;
+                
+                for (y = half; y &lt; self.max; y += size) {
+                    for (x = half; x &lt; self.max; x += size) {
+                        square(x, y, half, Math.random() * scale * 2 - scale);
+                    }
+                }
+                for (y = 0; y &lt;= self.max; y += half) {
+                    for (x = (y + half) % size; x &lt;= self.max; x += size) {
+                        diamond(x, y, half, Math.random() * scale * 2 - scale);
+                    }
+                }
+                divide(size / 2);
+            }
+            
+            function average(values) {
+                var valid = values.filter(function(val) { return val !== -1; });
+                var total = valid.reduce(function(sum, val) { return sum + val; }, 0);
+                return total / valid.length;
+            }
+            
+            function square(x, y, size, offset) {
+                var ave = average([
+                    self.get(x - size, y - size),   // upper left
+                    self.get(x + size, y - size),   // upper right
+                    self.get(x + size, y + size),   // lower right
+                    self.get(x - size, y + size)    // lower left
+                ]);
+                self.set(x, y, ave + offset);
+            }
+            
+            function diamond(x, y, size, offset) {
+                var ave = average([
+                    self.get(x, y - size),      // top
+                    self.get(x + size, y),      // right
+                    self.get(x, y + size),      // bottom
+                    self.get(x - size, y)       // left
+                ]);
+                self.set(x, y, ave + offset);
+            }
+        };
+        
+        Terrain.prototype.draw = function(ctx, width, height) {
+            var self = this;
+            var waterVal = this.size * 0.3;
+            
+            for (var y = 0; y &lt; this.size; y++) {
+                for (var x = 0; x &lt; this.size; x++) {
+                    var val = this.get(x, y);
+                    var top = project(x, y, val);
+                    var bottom = project(x + 1, y, 0);
+                    var water = project(x, y, waterVal);
+                    var style = brightness(x, y, this.get(x + 1, y) - val);
+                    
+                    rect(top, bottom, style);
+                    rect(water, bottom, 'rgba(50, 150, 200, 0.15)');
+                }
+            }
+            
+            function rect(a, b, style) {
+                if (b.y &lt; a.y)
+                    return;
+                ctx.fillStyle = style;
+                ctx.fillRect(a.x, a.y, b.x - a.x, b.y - a.y);
+            }
+            
+            function brightness(x, y, slope) {
+                if (y === self.max || x === self.max)
+                    return '#000';
+                var b = ~~(slope * 50) + 128;
+                return ['rgba(', b, ',', b, ',', b, ',1)'].join('');
+            }
+            
+            function iso(x, y) {
+                return {
+                    x: 0.5 * (self.size + x - y),
+                    y: 0.5 * (x + y)
+                };
+            }
+            
+            function project(flatX, flatY, flatZ) {
+                var point = iso(flatX, flatY);
+                var x0 = width * 0.5;
+                var y0 = height * 0.2;
+                var z = self.size * 0.5 - flatZ + point.y * 0.75;
+                var x = (point.x - self.size * 0.5) * 6;
+                var y = (self.size - point.y) * 0.005 + 1;
+                
+                return {
+                    x: x0 + x / y,
+                    y: y0 + z / y
+                };
+            }
+        };
+        
+        var display = document.getElementById('display');
+        var ctx = display.getContext('2d');
+        var width = display.width = 1024;
+        var height = display.height = 768;
+        
+        PerfTestRunner.measureTime({
+            run: function() {
+                var terrain = new Terrain(9);
+                terrain.generate(0.5);
+                ctx.clearRect(0, 0, width, height);
+                terrain.draw(ctx, width, height);
+            }
+        });
+        &lt;/script&gt;
+    &lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkPerformanceTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/PerformanceTests/ChangeLog (169286 => 169287)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/PerformanceTests/ChangeLog        2014-05-23 22:37:19 UTC (rev 169286)
+++ trunk/PerformanceTests/ChangeLog        2014-05-23 22:37:30 UTC (rev 169287)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2014-05-23  Geoffrey Garen  &lt;ggaren@apple.com&gt;
+
+        Performance testing, diamond-square terrain generation + canvas
+        https://bugs.webkit.org/show_bug.cgi?id=133054
+
+        Reviewed by Ryosuke Niwa.
+
+        This test was written by Hunter Loftis. It originally appeared on his
+        blog @ &lt;http://www.playfuljs.com/realistic-terrain-in-130-lines/&gt;.
+
+        I did a bit of editing for format and benchmark suitability.
+
+        * Canvas/terrain.html: Added.
+
</ins><span class="cx"> 2014-05-13  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         DYEBench should run 20 iterations in browser
</span></span></pre>
</div>
</div>

</body>
</html>