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 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" |
9 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/string_number_conversions.h" |
10 #include "content/public/renderer/render_thread.h" | 13 #include "content/public/renderer/render_thread.h" |
11 #include "content/renderer/all_rendering_benchmarks.h" | 14 #include "content/renderer/all_rendering_benchmarks.h" |
| 15 #include "content/renderer/render_view_impl.h" |
12 #include "content/renderer/rendering_benchmark.h" | 16 #include "content/renderer/rendering_benchmark.h" |
13 #include "content/renderer/rendering_benchmark_results.h" | 17 #include "content/renderer/rendering_benchmark_results.h" |
14 #include "content/renderer/render_view_impl.h" | 18 #include "third_party/skia/include/core/SkPicture.h" |
| 19 #include "third_party/skia/include/core/SkStream.h" |
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebRenderingStats.h
" | 20 #include "third_party/WebKit/Source/Platform/chromium/public/WebRenderingStats.h
" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSuppo
rt.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSuppo
rt.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
19 #include "v8/include/v8.h" | 24 #include "v8/include/v8.h" |
20 | 25 |
| 26 using WebKit::WebCanvas; |
21 using WebKit::WebFrame; | 27 using WebKit::WebFrame; |
22 using WebKit::WebPrivatePtr; | 28 using WebKit::WebPrivatePtr; |
| 29 using WebKit::WebRenderingStats; |
| 30 using WebKit::WebSize; |
| 31 using WebKit::WebView; |
23 using WebKit::WebViewBenchmarkSupport; | 32 using WebKit::WebViewBenchmarkSupport; |
24 using WebKit::WebRenderingStats; | |
25 using WebKit::WebView; | |
26 | 33 |
27 const char kGpuBenchmarkingExtensionName[] = "v8/GpuBenchmarking"; | 34 const char kGpuBenchmarkingExtensionName[] = "v8/GpuBenchmarking"; |
28 | 35 |
29 using WebKit::WebFrame; | 36 namespace { |
30 using WebKit::WebView; | 37 |
| 38 class SkPictureRecorder : public WebViewBenchmarkSupport::PaintClient { |
| 39 public: |
| 40 explicit SkPictureRecorder(const FilePath& dirpath) |
| 41 : dirpath_(dirpath), |
| 42 layer_id_(0) { |
| 43 } |
| 44 |
| 45 virtual WebCanvas* willPaint(const WebSize& size) { |
| 46 return picture_.beginRecording(size.width, size.height); |
| 47 } |
| 48 |
| 49 virtual void didPaint(WebCanvas* canvas) { |
| 50 DCHECK(canvas == picture_.getRecordingCanvas()); |
| 51 picture_.endRecording(); |
| 52 // Serialize picture to file. |
| 53 // TODO(alokp): Note that for this to work Chrome needs to be launched with |
| 54 // --no-sandbox command-line flag. Get rid of this limitation. |
| 55 // CRBUG: 139640. |
| 56 std::string filename = "layer_" + base::IntToString(layer_id_++) + ".skp"; |
| 57 std::string filepath = dirpath_.AppendASCII(filename).MaybeAsASCII(); |
| 58 DCHECK(!filepath.empty()); |
| 59 SkFILEWStream file(filepath.c_str()); |
| 60 DCHECK(file.isValid()); |
| 61 picture_.serialize(&file); |
| 62 } |
| 63 |
| 64 private: |
| 65 FilePath dirpath_; |
| 66 int layer_id_; |
| 67 SkPicture picture_; |
| 68 }; |
| 69 |
| 70 } // namespace |
31 | 71 |
32 namespace content { | 72 namespace content { |
33 | 73 |
34 // Benchmark results object that populates a v8 array. | 74 // Benchmark results object that populates a v8 array. |
35 class V8BenchmarkResults : public content::RenderingBenchmarkResults { | 75 class V8BenchmarkResults : public content::RenderingBenchmarkResults { |
36 public: | 76 public: |
37 explicit V8BenchmarkResults() | 77 explicit V8BenchmarkResults() |
38 : results_array_(v8::Array::New(0)) { } | 78 : results_array_(v8::Array::New(0)) { } |
39 virtual ~V8BenchmarkResults() {} | 79 virtual ~V8BenchmarkResults() {} |
40 | 80 |
(...skipping 28 matching lines...) Expand all Loading... |
69 "if (typeof(chrome) == 'undefined') {" | 109 "if (typeof(chrome) == 'undefined') {" |
70 " chrome = {};" | 110 " chrome = {};" |
71 "};" | 111 "};" |
72 "if (typeof(chrome.gpuBenchmarking) == 'undefined') {" | 112 "if (typeof(chrome.gpuBenchmarking) == 'undefined') {" |
73 " chrome.gpuBenchmarking = {};" | 113 " chrome.gpuBenchmarking = {};" |
74 "};" | 114 "};" |
75 "chrome.gpuBenchmarking.renderingStats = function() {" | 115 "chrome.gpuBenchmarking.renderingStats = function() {" |
76 " native function GetRenderingStats();" | 116 " native function GetRenderingStats();" |
77 " return GetRenderingStats();" | 117 " return GetRenderingStats();" |
78 "};" | 118 "};" |
| 119 "chrome.gpuBenchmarking.printToSkPicture = function(dirname) {" |
| 120 " native function PrintToSkPicture();" |
| 121 " return PrintToSkPicture(dirname);" |
| 122 "};" |
79 "chrome.gpuBenchmarking.beginSmoothScrollDown = " | 123 "chrome.gpuBenchmarking.beginSmoothScrollDown = " |
80 " function(scroll_far) {" | 124 " function(scroll_far) {" |
81 " scroll_far = scroll_far || false;" | 125 " scroll_far = scroll_far || false;" |
82 " native function BeginSmoothScroll();" | 126 " native function BeginSmoothScroll();" |
83 " return BeginSmoothScroll(true, scroll_far);" | 127 " return BeginSmoothScroll(true, scroll_far);" |
84 "};" | 128 "};" |
85 "chrome.gpuBenchmarking.beginSmoothScrollUp = function(scroll_far) {" | 129 "chrome.gpuBenchmarking.beginSmoothScrollUp = function(scroll_far) {" |
86 " scroll_far = scroll_far || false;" | 130 " scroll_far = scroll_far || false;" |
87 " native function BeginSmoothScroll();" | 131 " native function BeginSmoothScroll();" |
88 " return BeginSmoothScroll(false, scroll_far);" | 132 " return BeginSmoothScroll(false, scroll_far);" |
89 "};" | 133 "};" |
90 "chrome.gpuBenchmarking.runRenderingBenchmarks = function(filter) {" | 134 "chrome.gpuBenchmarking.runRenderingBenchmarks = function(filter) {" |
91 " native function RunRenderingBenchmarks();" | 135 " native function RunRenderingBenchmarks();" |
92 " return RunRenderingBenchmarks(filter);" | 136 " return RunRenderingBenchmarks(filter);" |
93 "};") {} | 137 "};") {} |
94 | 138 |
95 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 139 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
96 v8::Handle<v8::String> name) { | 140 v8::Handle<v8::String> name) { |
97 if (name->Equals(v8::String::New("GetRenderingStats"))) | 141 if (name->Equals(v8::String::New("GetRenderingStats"))) |
98 return v8::FunctionTemplate::New(GetRenderingStats); | 142 return v8::FunctionTemplate::New(GetRenderingStats); |
| 143 if (name->Equals(v8::String::New("PrintToSkPicture"))) |
| 144 return v8::FunctionTemplate::New(PrintToSkPicture); |
99 if (name->Equals(v8::String::New("BeginSmoothScroll"))) | 145 if (name->Equals(v8::String::New("BeginSmoothScroll"))) |
100 return v8::FunctionTemplate::New(BeginSmoothScroll); | 146 return v8::FunctionTemplate::New(BeginSmoothScroll); |
101 if (name->Equals(v8::String::New("RunRenderingBenchmarks"))) | 147 if (name->Equals(v8::String::New("RunRenderingBenchmarks"))) |
102 return v8::FunctionTemplate::New(RunRenderingBenchmarks); | 148 return v8::FunctionTemplate::New(RunRenderingBenchmarks); |
103 | 149 |
104 return v8::Handle<v8::FunctionTemplate>(); | 150 return v8::Handle<v8::FunctionTemplate>(); |
105 } | 151 } |
106 | 152 |
107 static v8::Handle<v8::Value> GetRenderingStats(const v8::Arguments& args) { | 153 static v8::Handle<v8::Value> GetRenderingStats(const v8::Arguments& args) { |
108 WebFrame* web_frame = WebFrame::frameForEnteredContext(); | 154 WebFrame* web_frame = WebFrame::frameForEnteredContext(); |
(...skipping 24 matching lines...) Expand all Loading... |
133 stats_object->Set(v8::String::New("totalPaintTimeInSeconds"), | 179 stats_object->Set(v8::String::New("totalPaintTimeInSeconds"), |
134 v8::Number::New(stats.totalPaintTimeInSeconds), | 180 v8::Number::New(stats.totalPaintTimeInSeconds), |
135 v8::ReadOnly); | 181 v8::ReadOnly); |
136 if (stats.totalRasterizeTimeInSeconds) | 182 if (stats.totalRasterizeTimeInSeconds) |
137 stats_object->Set(v8::String::New("totalRasterizeTimeInSeconds"), | 183 stats_object->Set(v8::String::New("totalRasterizeTimeInSeconds"), |
138 v8::Number::New(stats.totalRasterizeTimeInSeconds), | 184 v8::Number::New(stats.totalRasterizeTimeInSeconds), |
139 v8::ReadOnly); | 185 v8::ReadOnly); |
140 return stats_object; | 186 return stats_object; |
141 } | 187 } |
142 | 188 |
| 189 static v8::Handle<v8::Value> PrintToSkPicture(const v8::Arguments& args) { |
| 190 if (args.Length() != 1) |
| 191 return v8::Undefined(); |
| 192 |
| 193 v8::String::AsciiValue dirname(args[0]); |
| 194 if (dirname.length() == 0) |
| 195 return v8::Undefined(); |
| 196 |
| 197 WebFrame* web_frame = WebFrame::frameForEnteredContext(); |
| 198 if (!web_frame) |
| 199 return v8::Undefined(); |
| 200 |
| 201 WebView* web_view = web_frame->view(); |
| 202 if (!web_view) |
| 203 return v8::Undefined(); |
| 204 |
| 205 WebViewBenchmarkSupport* benchmark_support = web_view->benchmarkSupport(); |
| 206 if (!benchmark_support) |
| 207 return v8::Undefined(); |
| 208 |
| 209 FilePath dirpath; |
| 210 dirpath = dirpath.AppendASCII(*dirname); |
| 211 if (!file_util::CreateDirectory(dirpath) || |
| 212 !file_util::PathIsWritable(dirpath)) { |
| 213 std::string msg("Path is not writable: "); |
| 214 msg.append(dirpath.MaybeAsASCII()); |
| 215 return v8::ThrowException(v8::Exception::Error( |
| 216 v8::String::New(msg.c_str(), msg.length()))); |
| 217 } |
| 218 |
| 219 SkPictureRecorder recorder(dirpath); |
| 220 benchmark_support->paint(&recorder, |
| 221 WebViewBenchmarkSupport::PaintModeEverything); |
| 222 return v8::Undefined(); |
| 223 } |
| 224 |
143 static v8::Handle<v8::Value> BeginSmoothScroll(const v8::Arguments& args) { | 225 static v8::Handle<v8::Value> BeginSmoothScroll(const v8::Arguments& args) { |
144 WebFrame* web_frame = WebFrame::frameForEnteredContext(); | 226 WebFrame* web_frame = WebFrame::frameForEnteredContext(); |
145 if (!web_frame) | 227 if (!web_frame) |
146 return v8::Undefined(); | 228 return v8::Undefined(); |
147 | 229 |
148 WebView* web_view = web_frame->view(); | 230 WebView* web_view = web_frame->view(); |
149 if (!web_view) | 231 if (!web_view) |
150 return v8::Undefined(); | 232 return v8::Undefined(); |
151 | 233 |
152 RenderViewImpl* render_view_impl = RenderViewImpl::FromWebView(web_view); | 234 RenderViewImpl* render_view_impl = RenderViewImpl::FromWebView(web_view); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 294 |
213 return results.results_array(); | 295 return results.results_array(); |
214 } | 296 } |
215 }; | 297 }; |
216 | 298 |
217 v8::Extension* GpuBenchmarkingExtension::Get() { | 299 v8::Extension* GpuBenchmarkingExtension::Get() { |
218 return new GpuBenchmarkingWrapper(); | 300 return new GpuBenchmarkingWrapper(); |
219 } | 301 } |
220 | 302 |
221 } // namespace content | 303 } // namespace content |
OLD | NEW |