OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/gpu/gpu_benchmarking_extension.h" | 5 #include "content/renderer/gpu/gpu_benchmarking_extension.h" |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "content/public/renderer/render_thread.h" | |
11 #include "content/renderer/all_rendering_benchmarks.h" | |
12 #include "content/renderer/rendering_benchmark.h" | |
13 #include "content/renderer/rendering_benchmark_results.h" | |
6 #include "content/renderer/render_view_impl.h" | 14 #include "content/renderer/render_view_impl.h" |
7 #include "third_party/WebKit/Source/Platform/chromium/public/WebRenderingStats.h " | 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebRenderingStats.h " |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSuppo rt.h" | |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
11 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
12 | 20 |
13 using WebKit::WebFrame; | 21 using WebKit::WebFrame; |
22 using WebKit::WebPrivatePtr; | |
23 using WebKit::WebViewBenchmarkSupport; | |
14 using WebKit::WebRenderingStats; | 24 using WebKit::WebRenderingStats; |
15 using WebKit::WebView; | 25 using WebKit::WebView; |
16 | 26 |
17 const char kGpuBenchmarkingExtensionName[] = "v8/GpuBenchmarking"; | 27 const char kGpuBenchmarkingExtensionName[] = "v8/GpuBenchmarking"; |
18 | 28 |
19 using WebKit::WebFrame; | 29 using WebKit::WebFrame; |
20 using WebKit::WebView; | 30 using WebKit::WebView; |
21 | 31 |
22 namespace content { | 32 namespace content { |
23 | 33 |
34 // Benchmark results object that populates a v8 array. | |
35 class V8BenchmarkResults : public content::RenderingBenchmarkResults { | |
36 public: | |
37 explicit V8BenchmarkResults(v8::Handle<v8::Array> results_array) | |
nduca
2012/07/20 20:30:09
why not have the array inside here and have an acc
dmurph
2012/07/20 20:52:33
The results object doesn't own the array though, t
dmurph
2012/07/20 21:11:41
After discussion we realized v8 handles destructio
| |
38 : results_array_(results_array) { } | |
39 virtual ~V8BenchmarkResults() {} | |
40 | |
41 void addResult(const std::string& benchmark_name, | |
42 const std::string& result_name, | |
43 const std::string& result_unit, | |
44 double result) { | |
45 v8::Handle<v8::Object> result_object = v8::Object::New(); | |
46 result_object->Set(v8::String::New("benchmarkName", 13), | |
47 v8::String::New(benchmark_name.c_str(), -1)); | |
48 result_object->Set(v8::String::New("resultName", 10), | |
49 v8::String::New(result_name.c_str(), -1)); | |
50 result_object->Set(v8::String::New("resultUnit", 10), | |
51 v8::String::New(result_unit.c_str(), -1)); | |
52 result_object->Set(v8::String::New("result", 6), v8::Number::New(result)); | |
53 | |
54 results_array_->Set(results_array_->Length(), result_object); | |
55 } | |
56 | |
57 private: | |
58 v8::Handle<v8::Array> results_array_; | |
59 }; | |
60 | |
24 class GpuBenchmarkingWrapper : public v8::Extension { | 61 class GpuBenchmarkingWrapper : public v8::Extension { |
25 public: | 62 public: |
26 GpuBenchmarkingWrapper() : | 63 GpuBenchmarkingWrapper() : |
27 v8::Extension(kGpuBenchmarkingExtensionName, | 64 v8::Extension(kGpuBenchmarkingExtensionName, |
28 "if (typeof(chrome) == 'undefined') {" | 65 "if (typeof(chrome) == 'undefined') {" |
29 " chrome = {};" | 66 " chrome = {};" |
30 "};" | 67 "};" |
31 "if (typeof(chrome.gpuBenchmarking) == 'undefined') {" | 68 "if (typeof(chrome.gpuBenchmarking) == 'undefined') {" |
32 " chrome.gpuBenchmarking = {};" | 69 " chrome.gpuBenchmarking = {};" |
33 "};" | 70 "};" |
34 "chrome.gpuBenchmarking.renderingStats = function() {" | 71 "chrome.gpuBenchmarking.renderingStats = function() {" |
35 " native function GetRenderingStats();" | 72 " native function GetRenderingStats();" |
36 " return GetRenderingStats();" | 73 " return GetRenderingStats();" |
37 "};" | 74 "};" |
38 "chrome.gpuBenchmarking.beginSmoothScrollDown = " | 75 "chrome.gpuBenchmarking.beginSmoothScrollDown = " |
39 " function(scroll_far) {" | 76 " function(scroll_far) {" |
40 " scroll_far = scroll_far || false;" | 77 " scroll_far = scroll_far || false;" |
41 " native function BeginSmoothScroll();" | 78 " native function BeginSmoothScroll();" |
42 " return BeginSmoothScroll(true, scroll_far);" | 79 " return BeginSmoothScroll(true, scroll_far);" |
43 "};" | 80 "};" |
44 "chrome.gpuBenchmarking.beginSmoothScrollUp = function(scroll_far) {" | 81 "chrome.gpuBenchmarking.beginSmoothScrollUp = function(scroll_far) {" |
45 " scroll_far = scroll_far || false;" | 82 " scroll_far = scroll_far || false;" |
46 " native function BeginSmoothScroll();" | 83 " native function BeginSmoothScroll();" |
47 " return BeginSmoothScroll(false, scroll_far);" | 84 " return BeginSmoothScroll(false, scroll_far);" |
85 "};" | |
86 "chrome.gpuBenchmarking.runRenderingBenchmarks = function(filter) {" | |
87 " native function RunRenderingBenchmarks();" | |
88 " return RunRenderingBenchmarks(filter);" | |
48 "};") {} | 89 "};") {} |
49 | 90 |
50 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 91 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
51 v8::Handle<v8::String> name) { | 92 v8::Handle<v8::String> name) { |
52 if (name->Equals(v8::String::New("GetRenderingStats"))) | 93 if (name->Equals(v8::String::New("GetRenderingStats"))) |
53 return v8::FunctionTemplate::New(GetRenderingStats); | 94 return v8::FunctionTemplate::New(GetRenderingStats); |
54 if (name->Equals(v8::String::New("BeginSmoothScroll"))) | 95 if (name->Equals(v8::String::New("BeginSmoothScroll"))) |
55 return v8::FunctionTemplate::New(BeginSmoothScroll); | 96 return v8::FunctionTemplate::New(BeginSmoothScroll); |
56 | 97 if (name->Equals(v8::String::New("RunRenderingBenchmarks"))) |
98 return v8::FunctionTemplate::New(RunRenderingBenchmarks); | |
nduca
2012/07/20 20:30:09
i think there's a blank line after the ifs
dmurph
2012/07/20 20:52:33
Done.
| |
57 return v8::Handle<v8::FunctionTemplate>(); | 99 return v8::Handle<v8::FunctionTemplate>(); |
58 } | 100 } |
59 | 101 |
60 static v8::Handle<v8::Value> GetRenderingStats(const v8::Arguments& args) { | 102 static v8::Handle<v8::Value> GetRenderingStats(const v8::Arguments& args) { |
61 WebFrame* web_frame = WebFrame::frameForEnteredContext(); | 103 WebFrame* web_frame = WebFrame::frameForEnteredContext(); |
62 if (!web_frame) | 104 if (!web_frame) |
63 return v8::Undefined(); | 105 return v8::Undefined(); |
64 | 106 |
65 WebView* web_view = web_frame->view(); | 107 WebView* web_view = web_frame->view(); |
66 if (!web_view) | 108 if (!web_view) |
(...skipping 29 matching lines...) Expand all Loading... | |
96 | 138 |
97 if (args.Length() != 2 || !args[0]->IsBoolean() || !args[1]->IsBoolean()) | 139 if (args.Length() != 2 || !args[0]->IsBoolean() || !args[1]->IsBoolean()) |
98 return v8::False(); | 140 return v8::False(); |
99 | 141 |
100 bool scroll_down = args[0]->BooleanValue(); | 142 bool scroll_down = args[0]->BooleanValue(); |
101 bool scroll_far = args[1]->BooleanValue(); | 143 bool scroll_far = args[1]->BooleanValue(); |
102 | 144 |
103 render_view_impl->BeginSmoothScroll(scroll_down, scroll_far); | 145 render_view_impl->BeginSmoothScroll(scroll_down, scroll_far); |
104 return v8::True(); | 146 return v8::True(); |
105 } | 147 } |
148 | |
149 static v8::Handle<v8::Value> RunRenderingBenchmarks( | |
150 const v8::Arguments& args) { | |
151 // for our name filter, the argument can be undefined or null to run | |
nduca
2012/07/20 20:30:09
// For
dmurph
2012/07/20 20:52:33
Done.
| |
152 // all benchmarks, or a string for filtering by name | |
153 if (!args.Length() || | |
154 (!args[0]->IsString() && | |
155 !(args[0]->IsNull() || args[0]->IsUndefined()))) | |
piman
2012/07/20 20:35:35
nit: indent (! should be aligned with the one abov
dmurph
2012/07/20 21:19:13
Done.
| |
156 return v8::Undefined(); | |
157 | |
158 WebFrame* web_frame = WebFrame::frameForEnteredContext(); | |
159 if (!web_frame) | |
160 return v8::Undefined(); | |
161 | |
162 WebView* web_view = web_frame->view(); | |
163 if (!web_view) | |
164 return v8::Undefined(); | |
165 | |
166 v8::Handle<v8::Array> resultsArray = v8::Array::New(0); | |
167 V8BenchmarkResults results(resultsArray); | |
nduca
2012/07/20 20:30:09
initialize the results array as late as you can. E
dmurph
2012/07/20 20:52:33
Done.
| |
168 std::string name_filter; | |
nduca
2012/07/20 20:30:09
how about moving the name_filter stuff up to where
dmurph
2012/07/20 20:52:33
Done.
| |
169 if (args[0]->IsNull() || args[0]->IsUndefined()) { | |
170 name_filter = ""; | |
171 } else { | |
172 char filter[256]; | |
173 args[0]->ToString()->WriteAscii(filter, 0, sizeof(filter)-1); | |
174 name_filter = std::string(filter); | |
175 } | |
176 | |
177 WebViewBenchmarkSupport* support = | |
178 web_view->benchmarkSupport(); | |
179 std::vector<RenderingBenchmark*> benchmarks = AllRenderingBenchmarks(); | |
180 | |
181 std::vector<RenderingBenchmark*>::const_iterator it; | |
182 for (it = benchmarks.begin(); it != benchmarks.end(); it++) { | |
183 RenderingBenchmark* benchmark = *it; | |
184 const std::string& name = benchmark->name(); | |
185 if (name_filter != "" && | |
186 std::string::npos == name.find(name_filter)) { | |
piman
2012/07/20 20:35:35
nit: indent (should be +4)
dmurph
2012/07/20 21:19:13
Done.
| |
187 continue; | |
188 } | |
189 benchmark->SetUp(support); | |
190 benchmark->Run(&results, support); | |
191 benchmark->TearDown(support); | |
192 } | |
193 | |
194 return resultsArray; | |
195 } | |
106 }; | 196 }; |
107 | 197 |
108 v8::Extension* GpuBenchmarkingExtension::Get() { | 198 v8::Extension* GpuBenchmarkingExtension::Get() { |
109 return new GpuBenchmarkingWrapper(); | 199 return new GpuBenchmarkingWrapper(); |
110 } | 200 } |
111 | 201 |
112 } // namespace content | 202 } // namespace content |
OLD | NEW |