Loosen up heuristics for detecting account creation forms.
[chromium-blink-merge.git] / content / renderer / gpu / gpu_benchmarking_extension.cc
blobb7fd7c9e6ac9af37dfecc471eb88d2c6b944d532
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
3 // found in the LICENSE file.
5 #include "content/renderer/gpu/gpu_benchmarking_extension.h"
7 #include <string>
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/string_number_conversions.h"
13 #include "content/common/gpu/gpu_rendering_stats.h"
14 #include "content/public/renderer/render_thread.h"
15 #include "content/renderer/all_rendering_benchmarks.h"
16 #include "content/renderer/render_view_impl.h"
17 #include "content/renderer/rendering_benchmark.h"
18 #include "third_party/skia/include/core/SkGraphics.h"
19 #include "third_party/skia/include/core/SkPicture.h"
20 #include "third_party/skia/include/core/SkStream.h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebRenderingStats.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewBenchmarkSupport.h"
25 #include "v8/include/v8.h"
27 using WebKit::WebCanvas;
28 using WebKit::WebFrame;
29 using WebKit::WebPrivatePtr;
30 using WebKit::WebRenderingStats;
31 using WebKit::WebSize;
32 using WebKit::WebView;
33 using WebKit::WebViewBenchmarkSupport;
35 const char kGpuBenchmarkingExtensionName[] = "v8/GpuBenchmarking";
37 namespace {
39 // Always called on the main render thread.
40 // Does not need to be thread-safe.
41 void InitSkGraphics() {
42 static bool init = false;
43 if (!init) {
44 SkGraphics::Init();
45 init = true;
49 class SkPictureRecorder : public WebViewBenchmarkSupport::PaintClient {
50 public:
51 explicit SkPictureRecorder(const FilePath& dirpath)
52 : dirpath_(dirpath),
53 layer_id_(0) {
54 // Let skia register known effect subclasses. This basically enables
55 // reflection on those subclasses required for picture serialization.
56 InitSkGraphics();
59 virtual WebCanvas* willPaint(const WebSize& size) {
60 return picture_.beginRecording(size.width, size.height);
63 virtual void didPaint(WebCanvas* canvas) {
64 DCHECK(canvas == picture_.getRecordingCanvas());
65 picture_.endRecording();
66 // Serialize picture to file.
67 // TODO(alokp): Note that for this to work Chrome needs to be launched with
68 // --no-sandbox command-line flag. Get rid of this limitation.
69 // CRBUG: 139640.
70 std::string filename = "layer_" + base::IntToString(layer_id_++) + ".skp";
71 std::string filepath = dirpath_.AppendASCII(filename).MaybeAsASCII();
72 DCHECK(!filepath.empty());
73 SkFILEWStream file(filepath.c_str());
74 DCHECK(file.isValid());
75 picture_.serialize(&file);
78 private:
79 FilePath dirpath_;
80 int layer_id_;
81 SkPicture picture_;
84 } // namespace
86 namespace content {
88 class GpuBenchmarkingWrapper : public v8::Extension {
89 public:
90 GpuBenchmarkingWrapper() :
91 v8::Extension(kGpuBenchmarkingExtensionName,
92 "if (typeof(chrome) == 'undefined') {"
93 " chrome = {};"
94 "};"
95 "if (typeof(chrome.gpuBenchmarking) == 'undefined') {"
96 " chrome.gpuBenchmarking = {};"
97 "};"
98 "chrome.gpuBenchmarking.renderingStats = function() {"
99 " native function GetRenderingStats();"
100 " return GetRenderingStats();"
101 "};"
102 "chrome.gpuBenchmarking.printToSkPicture = function(dirname) {"
103 " native function PrintToSkPicture();"
104 " return PrintToSkPicture(dirname);"
105 "};"
106 "chrome.gpuBenchmarking.beginSmoothScrollDown = "
107 " function(scroll_far, opt_callback) {"
108 " scroll_far = scroll_far || false;"
109 " callback = opt_callback || function() { };"
110 " native function BeginSmoothScroll();"
111 " return BeginSmoothScroll(true, scroll_far, callback);"
112 "};"
113 "chrome.gpuBenchmarking.beginSmoothScrollUp = "
114 " function(scroll_far, opt_callback) {"
115 " scroll_far = scroll_far || false;"
116 " callback = opt_callback || function() { };"
117 " native function BeginSmoothScroll();"
118 " return BeginSmoothScroll(false, scroll_far, callback);"
119 "};"
120 "chrome.gpuBenchmarking.runRenderingBenchmarks = function(filter) {"
121 " native function RunRenderingBenchmarks();"
122 " return RunRenderingBenchmarks(filter);"
123 "};") {}
125 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
126 v8::Handle<v8::String> name) {
127 if (name->Equals(v8::String::New("GetRenderingStats")))
128 return v8::FunctionTemplate::New(GetRenderingStats);
129 if (name->Equals(v8::String::New("PrintToSkPicture")))
130 return v8::FunctionTemplate::New(PrintToSkPicture);
131 if (name->Equals(v8::String::New("BeginSmoothScroll")))
132 return v8::FunctionTemplate::New(BeginSmoothScroll);
133 if (name->Equals(v8::String::New("RunRenderingBenchmarks")))
134 return v8::FunctionTemplate::New(RunRenderingBenchmarks);
136 return v8::Handle<v8::FunctionTemplate>();
139 static v8::Handle<v8::Value> GetRenderingStats(const v8::Arguments& args) {
140 WebFrame* web_frame = WebFrame::frameForEnteredContext();
141 if (!web_frame)
142 return v8::Undefined();
144 WebView* web_view = web_frame->view();
145 if (!web_view)
146 return v8::Undefined();
148 RenderViewImpl* render_view_impl = RenderViewImpl::FromWebView(web_view);
149 if (!render_view_impl)
150 return v8::Undefined();
152 WebRenderingStats stats;
153 render_view_impl->GetRenderingStats(stats);
155 content::GpuRenderingStats gpu_stats;
156 render_view_impl->GetGpuRenderingStats(&gpu_stats);
158 v8::Handle<v8::Object> stats_object = v8::Object::New();
159 stats_object->Set(v8::String::New("numAnimationFrames"),
160 v8::Integer::New(stats.numAnimationFrames));
161 stats_object->Set(v8::String::New("numFramesSentToScreen"),
162 v8::Integer::New(stats.numFramesSentToScreen));
163 stats_object->Set(v8::String::New("droppedFrameCount"),
164 v8::Integer::New(stats.droppedFrameCount));
165 stats_object->Set(v8::String::New("totalPaintTimeInSeconds"),
166 v8::Number::New(stats.totalPaintTimeInSeconds));
167 stats_object->Set(v8::String::New("totalRasterizeTimeInSeconds"),
168 v8::Number::New(stats.totalRasterizeTimeInSeconds));
169 stats_object->Set(v8::String::New("globalTextureUploadCount"),
170 v8::Number::New(gpu_stats.global_texture_upload_count));
171 stats_object->Set(
172 v8::String::New("globalTotalTextureUploadTimeInSeconds"),
173 v8::Number::New(
174 gpu_stats.global_total_texture_upload_time.InSecondsF()));
175 stats_object->Set(v8::String::New("textureUploadCount"),
176 v8::Number::New(gpu_stats.texture_upload_count));
177 stats_object->Set(
178 v8::String::New("totalTextureUploadTimeInSeconds"),
179 v8::Number::New(gpu_stats.total_texture_upload_time.InSecondsF()));
180 stats_object->Set(
181 v8::String::New("globalTotalProcessingCommandsTimeInSeconds"),
182 v8::Number::New(
183 gpu_stats.global_total_processing_commands_time.InSecondsF()));
184 stats_object->Set(
185 v8::String::New("totalProcessingCommandsTimeInSeconds"),
186 v8::Number::New(
187 gpu_stats.total_processing_commands_time.InSecondsF()));
188 return stats_object;
191 static v8::Handle<v8::Value> PrintToSkPicture(const v8::Arguments& args) {
192 if (args.Length() != 1)
193 return v8::Undefined();
195 v8::String::AsciiValue dirname(args[0]);
196 if (dirname.length() == 0)
197 return v8::Undefined();
199 WebFrame* web_frame = WebFrame::frameForEnteredContext();
200 if (!web_frame)
201 return v8::Undefined();
203 WebView* web_view = web_frame->view();
204 if (!web_view)
205 return v8::Undefined();
207 WebViewBenchmarkSupport* benchmark_support = web_view->benchmarkSupport();
208 if (!benchmark_support)
209 return v8::Undefined();
211 FilePath dirpath(FilePath::StringType(*dirname,
212 *dirname + dirname.length()));
213 if (!file_util::CreateDirectory(dirpath) ||
214 !file_util::PathIsWritable(dirpath)) {
215 std::string msg("Path is not writable: ");
216 msg.append(dirpath.MaybeAsASCII());
217 return v8::ThrowException(v8::Exception::Error(
218 v8::String::New(msg.c_str(), msg.length())));
221 SkPictureRecorder recorder(dirpath);
222 benchmark_support->paint(&recorder,
223 WebViewBenchmarkSupport::PaintModeEverything);
224 return v8::Undefined();
227 static void OnSmoothScrollCompleted(v8::Persistent<v8::Function> callback,
228 v8::Persistent<v8::Context> context) {
229 v8::HandleScope scope;
230 v8::Context::Scope context_scope(context);
231 WebFrame* frame = WebFrame::frameForContext(context);
232 if (frame) {
233 frame->callFunctionEvenIfScriptDisabled(callback,
234 v8::Object::New(),
236 NULL);
238 callback.Dispose();
239 context.Dispose();
242 static v8::Handle<v8::Value> BeginSmoothScroll(const v8::Arguments& args) {
243 WebFrame* web_frame = WebFrame::frameForEnteredContext();
244 if (!web_frame)
245 return v8::Undefined();
247 WebView* web_view = web_frame->view();
248 if (!web_view)
249 return v8::Undefined();
251 RenderViewImpl* render_view_impl = RenderViewImpl::FromWebView(web_view);
252 if (!render_view_impl)
253 return v8::Undefined();
255 if (args.Length() != 3 ||
256 !args[0]->IsBoolean() ||
257 !args[1]->IsBoolean() ||
258 !args[2]->IsFunction())
259 return v8::False();
261 bool scroll_down = args[0]->BooleanValue();
262 bool scroll_far = args[1]->BooleanValue();
263 v8::Local<v8::Function> callback_local =
264 v8::Local<v8::Function>(v8::Function::Cast(*args[2]));
265 v8::Persistent<v8::Function> callback =
266 v8::Persistent<v8::Function>::New(callback_local);
267 v8::Persistent<v8::Context> context =
268 v8::Persistent<v8::Context>::New(web_frame->mainWorldScriptContext());
270 // TODO(nduca): If the render_view_impl is destroyed while the gesture is in
271 // progress, we will leak the callback and context. This needs to be fixed,
272 // somehow.
273 render_view_impl->BeginSmoothScroll(
274 scroll_down,
275 scroll_far,
276 base::Bind(&OnSmoothScrollCompleted,
277 callback,
278 context));
280 return v8::True();
283 static v8::Handle<v8::Value> RunRenderingBenchmarks(
284 const v8::Arguments& args) {
285 // For our name filter, the argument can be undefined or null to run
286 // all benchmarks, or a string for filtering by name.
287 if (!args.Length() ||
288 (!args[0]->IsString() &&
289 !(args[0]->IsNull() || args[0]->IsUndefined()))) {
290 return v8::Undefined();
293 std::string name_filter;
294 if (args[0]->IsNull() || args[0]->IsUndefined()) {
295 name_filter = "";
296 } else {
297 char filter[256];
298 args[0]->ToString()->WriteAscii(filter, 0, sizeof(filter)-1);
299 name_filter = std::string(filter);
302 WebFrame* web_frame = WebFrame::frameForEnteredContext();
303 if (!web_frame)
304 return v8::Undefined();
306 WebView* web_view = web_frame->view();
307 if (!web_view)
308 return v8::Undefined();
310 WebViewBenchmarkSupport* support = web_view->benchmarkSupport();
311 if (!support)
312 return v8::Undefined();
314 ScopedVector<RenderingBenchmark> benchmarks = AllRenderingBenchmarks();
316 v8::Handle<v8::Array> results = v8::Array::New(0);
317 ScopedVector<RenderingBenchmark>::const_iterator it;
318 for (it = benchmarks.begin(); it != benchmarks.end(); it++) {
319 RenderingBenchmark* benchmark = *it;
320 const std::string& name = benchmark->name();
321 if (name_filter != "" &&
322 std::string::npos == name.find(name_filter)) {
323 continue;
325 benchmark->SetUp(support);
326 double result = benchmark->Run(support);
327 benchmark->TearDown(support);
329 v8::Handle<v8::Object> result_object = v8::Object::New();
330 result_object->Set(v8::String::New("benchmark", 9),
331 v8::String::New(name.c_str(), -1));
332 result_object->Set(v8::String::New("result", 6), v8::Number::New(result));
333 results->Set(results->Length(), result_object);
336 return results;
340 v8::Extension* GpuBenchmarkingExtension::Get() {
341 return new GpuBenchmarkingWrapper();
344 } // namespace content