Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: samples/o3djs/performance.js

Issue 147258: Add a utility to adjust rendering quality based on rendering performance. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/o3d/
Patch Set: Fixed the last few comments and added js_list.scons. Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/o3djs/js_list.scons ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2009, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32
33 /**
34 * @fileoverview This file contains a utility that helps adjust rendering
35 * quality [or any other setting, really] based on rendering performance.
36 *
37 */
38
39 o3djs.provide('o3djs.performance');
40
41 /**
42 * Creates a utility that monitors performance [in terms of FPS] and helps to
43 * adjust the rendered scene accordingly.
44 * @param {number} targetFPSMin the minimum acceptable frame rate; if we're
45 * under this, try to decrease quality to improve performance.
46 * @param {number} targetFPSMax if we're over this, try to increase quality.
47 * @param {!function(): void} increaseQuality a function to increase
48 * quality because we're rendering at high-enough FPS to afford it.
49 * @param {!function(): void} decreaseQuality a function to decrease
50 * quality to try to raise our rendering speed.
51 * @param {!o3djs.performance.PerformanceMonitor.Options} opt_options Options.
52 * @return {!o3djs.performance.PerformanceMonitor} The created
53 * PerformanceMonitor.
54 */
55 o3djs.performance.createPerformanceMonitor = function(
56 targetFPSMin, targetFPSMax, increaseQuality, decreaseQuality, opt_options) {
57 return new o3djs.performance.PerformanceMonitor(targetFPSMin, targetFPSMax,
58 increaseQuality, decreaseQuality, opt_options);
59 }
60
61 /**
62 * A class that monitors performance [in terms of FPS] and helps to adjust the
63 * rendered scene accordingly.
64 * @constructor
65 * @param {number} targetFPSMin the minimum acceptable frame rate; if we're
66 * under this, try to decrease quality to improve performance.
67 * @param {number} targetFPSMax if we're over this, try to increase quality.
68 * @param {function(): void} increaseQuality a function to increase
69 * quality/lower FPS.
70 * @param {function(): void} decreaseQuality a function to decrease
71 * quality/raise FPS.
72 * @param {!o3djs.performance.PerformanceMonitor.Options} opt_options Options.
73 */
74 o3djs.performance.PerformanceMonitor = function(
75 targetFPSMin, targetFPSMax, increaseQuality, decreaseQuality, opt_options) {
76 opt_options = opt_options || {};
77
78 /**
79 * A function to increase quality/lower FPS.
80 * @type {function(): void}
81 */
82 this.increaseQuality = increaseQuality;
83
84 /**
85 * A function to decrease quality/raise FPS.
86 * @type {function(): void}
87 */
88 this.decreaseQuality = decreaseQuality;
89
90 /**
91 * The mean time taken per frame so far, in seconds. This is only valid once
92 * we've collected at least minSamples samples.
93 * @type {number}
94 */
95 this.meanFrameTime = 0;
96
97 /**
98 * The number of samples we've collected so far, when that number is less than
99 * or equal to this.damping. After that point, we no longer update
100 * this.sampleCount, so it will clip at this.damping.
101 *
102 * @type {number}
103 */
104 this.sampleCount = 0;
105
106 /**
107 * The minimum number of samples to collect before trying to adjust quality.
108 *
109 * @type {number}
110 */
111 this.minSamples = opt_options.opt_minSamples || 60;
112
113 /**
114 * A number that controls the rate at which the effects of any given sample
115 * fade away. Higher is slower, but also means that each individual sample
116 * counts for less at its most-influential. Damping defaults to 120; anywhere
117 * between 60 and 600 are probably reasonable values, depending on your needs,
118 * but the number must be no less than minSamples.
119 *
120 * @type {number}
121 */
122 this.damping = opt_options.opt_damping || 120;
123
124 /**
125 * The minimum number of samples to take in between adjustments, to cut down
126 * on overshoot. It defaults to 2 * minSamples.
127 *
128 * @type {number}
129 */
130 this.delayCycles = opt_options.opt_delayCycles || 2 * this.minSamples;
131
132 this.targetFrameTimeMax_ = 1 / targetFPSMin;
133 this.targetFrameTimeMin_ = 1 / targetFPSMax;
134 this.scaleInput_ = 1 / this.minSamples;
135 this.scaleMean_ = 1;
136 this.delayCyclesLeft_ = 0;
137 if (this.damping < this.minSamples) {
138 throw Error('Damping must be at least minSamples.');
139 }
140 }
141
142 /**
143 * Options for the PerformanceMonitor.
144 *
145 * opt_minSamples is the minimum number of samples to take before making any
146 * performance adjustments.
147 * opt_damping is a number that controls the rate at which the effects of any
148 * given sample fade away. Higher is slower, but also means that each
149 * individual sample counts for less at its most-influential. Damping
150 * defaults to 120; anywhere between 60 and 600 are probably reasonable values,
151 * depending on your needs, but the number must be no less than minSamples.
152 * opt_delayCycles is the minimum number of samples to take in between
153 * adjustments, to cut down on overshoot. It defaults to 2 * opt_minSamples.
154 *
155 * @type {{
156 * opt_minSamples: number,
157 * opt_damping: number,
158 * opt_delayCycles, number
159 * }}
160 */
161 o3djs.performance.PerformanceMonitor.Options = goog.typedef;
162
163 /**
164 * Call this once per frame with the elapsed time since the last call, and it
165 * will attempt to adjust your rendering quality as needed.
166 *
167 * @param {number} seconds the elapsed time since the last frame was rendered,
168 * in seconds.
169 */
170 o3djs.performance.PerformanceMonitor.prototype.onRender = function(seconds) {
171 var test = true;
172 if (this.sampleCount < this.damping) {
173 if (this.sampleCount >= this.minSamples) {
174 this.scaleInput_ = 1 / (this.sampleCount + 1);
175 this.scaleMean_ = this.sampleCount * this.scaleInput_;
176 } else {
177 test = false;
178 }
179 this.sampleCount += 1;
180 }
181 this.meanFrameTime = this.meanFrameTime * this.scaleMean_ +
182 seconds * this.scaleInput_;
183 if (this.delayCyclesLeft_ > 0) {
184 this.delayCyclesLeft_ -= 1;
185 } else if (test) {
186 if (this.meanFrameTime < this.targetFrameTimeMin_) {
187 this.increaseQuality();
188 this.delayCyclesLeft_ = this.delayCycles;
189 } else if (this.meanFrameTime > this.targetFrameTimeMax_) {
190 this.decreaseQuality();
191 this.delayCyclesLeft_ = this.delayCycles;
192 }
193 }
194 }
195
OLDNEW
« no previous file with comments | « samples/o3djs/js_list.scons ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698