| 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/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "cc/layers/layer.h" |
| 14 #include "content/common/browser_rendering_stats.h" | 15 #include "content/common/browser_rendering_stats.h" |
| 15 #include "content/common/gpu/gpu_rendering_stats.h" | 16 #include "content/common/gpu/gpu_rendering_stats.h" |
| 16 #include "content/public/renderer/render_thread.h" | 17 #include "content/public/renderer/render_thread.h" |
| 17 #include "content/renderer/gpu/render_widget_compositor.h" | 18 #include "content/renderer/gpu/render_widget_compositor.h" |
| 18 #include "content/renderer/render_view_impl.h" | 19 #include "content/renderer/render_view_impl.h" |
| 19 #include "content/renderer/skia_benchmarking_extension.h" | 20 #include "content/renderer/skia_benchmarking_extension.h" |
| 20 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebImageCache.h" | 22 #include "third_party/WebKit/public/web/WebImageCache.h" |
| 22 #include "third_party/WebKit/public/web/WebView.h" | 23 #include "third_party/WebKit/public/web/WebView.h" |
| 23 #include "third_party/WebKit/public/web/WebViewBenchmarkSupport.h" | |
| 24 #include "third_party/skia/include/core/SkData.h" | 24 #include "third_party/skia/include/core/SkData.h" |
| 25 #include "third_party/skia/include/core/SkGraphics.h" | 25 #include "third_party/skia/include/core/SkGraphics.h" |
| 26 #include "third_party/skia/include/core/SkPicture.h" | 26 #include "third_party/skia/include/core/SkPicture.h" |
| 27 #include "third_party/skia/include/core/SkPixelRef.h" | 27 #include "third_party/skia/include/core/SkPixelRef.h" |
| 28 #include "third_party/skia/include/core/SkStream.h" | 28 #include "third_party/skia/include/core/SkStream.h" |
| 29 #include "ui/gfx/codec/png_codec.h" | 29 #include "ui/gfx/codec/png_codec.h" |
| 30 #include "v8/include/v8.h" | 30 #include "v8/include/v8.h" |
| 31 #include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h" | 31 #include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h" |
| 32 | 32 |
| 33 using WebKit::WebCanvas; | 33 using WebKit::WebCanvas; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 52 } | 52 } |
| 53 std::vector<unsigned char> vector; | 53 std::vector<unsigned char> vector; |
| 54 if (gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &vector)) { | 54 if (gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &vector)) { |
| 55 return SkData::NewWithCopy(&vector.front() , vector.size()); | 55 return SkData::NewWithCopy(&vector.front() , vector.size()); |
| 56 } | 56 } |
| 57 return NULL; | 57 return NULL; |
| 58 } | 58 } |
| 59 | 59 |
| 60 namespace { | 60 namespace { |
| 61 | 61 |
| 62 class SkPictureRecorder : public WebViewBenchmarkSupport::PaintClient { | 62 class SkPictureSerializer { |
| 63 public: | 63 public: |
| 64 explicit SkPictureRecorder(const base::FilePath& dirpath) | 64 explicit SkPictureSerializer(const base::FilePath& dirpath) |
| 65 : dirpath_(dirpath), | 65 : dirpath_(dirpath), |
| 66 layer_id_(0) { | 66 layer_id_(0) { |
| 67 // Let skia register known effect subclasses. This basically enables | 67 // Let skia register known effect subclasses. This basically enables |
| 68 // reflection on those subclasses required for picture serialization. | 68 // reflection on those subclasses required for picture serialization. |
| 69 content::SkiaBenchmarkingExtension::InitSkGraphics(); | 69 content::SkiaBenchmarkingExtension::InitSkGraphics(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 virtual WebCanvas* willPaint(const WebSize& size) { | 72 // Recursively serializes the layer tree. |
| 73 return picture_.beginRecording(size.width, size.height); | 73 // Each layer in the tree is serialized into a separate skp file |
| 74 } | 74 // in the given directory. |
| 75 void Serialize(const cc::Layer* layer) { |
| 76 const cc::LayerList& children = layer->children(); |
| 77 for (size_t i = 0; i < children.size(); ++i) { |
| 78 Serialize(children[i].get()); |
| 79 } |
| 75 | 80 |
| 76 virtual void didPaint(WebCanvas* canvas) { | 81 skia::RefPtr<SkPicture> picture = layer->GetPicture(); |
| 77 DCHECK(canvas == picture_.getRecordingCanvas()); | 82 if (!picture) |
| 78 picture_.endRecording(); | 83 return; |
| 84 |
| 79 // Serialize picture to file. | 85 // Serialize picture to file. |
| 80 // TODO(alokp): Note that for this to work Chrome needs to be launched with | 86 // TODO(alokp): Note that for this to work Chrome needs to be launched with |
| 81 // --no-sandbox command-line flag. Get rid of this limitation. | 87 // --no-sandbox command-line flag. Get rid of this limitation. |
| 82 // CRBUG: 139640. | 88 // CRBUG: 139640. |
| 83 std::string filename = "layer_" + base::IntToString(layer_id_++) + ".skp"; | 89 std::string filename = "layer_" + base::IntToString(layer_id_++) + ".skp"; |
| 84 std::string filepath = dirpath_.AppendASCII(filename).MaybeAsASCII(); | 90 std::string filepath = dirpath_.AppendASCII(filename).MaybeAsASCII(); |
| 85 DCHECK(!filepath.empty()); | 91 DCHECK(!filepath.empty()); |
| 86 SkFILEWStream file(filepath.c_str()); | 92 SkFILEWStream file(filepath.c_str()); |
| 87 DCHECK(file.isValid()); | 93 DCHECK(file.isValid()); |
| 88 picture_.serialize(&file, &EncodeBitmapToData); | 94 picture->serialize(&file, &EncodeBitmapToData); |
| 89 } | 95 } |
| 90 | 96 |
| 91 private: | 97 private: |
| 92 base::FilePath dirpath_; | 98 base::FilePath dirpath_; |
| 93 int layer_id_; | 99 int layer_id_; |
| 94 SkPicture picture_; | |
| 95 }; | 100 }; |
| 96 | 101 |
| 97 class RenderingStatsEnumerator : public cc::RenderingStats::Enumerator { | 102 class RenderingStatsEnumerator : public cc::RenderingStats::Enumerator { |
| 98 public: | 103 public: |
| 99 RenderingStatsEnumerator(v8::Handle<v8::Object> stats_object) | 104 RenderingStatsEnumerator(v8::Handle<v8::Object> stats_object) |
| 100 : stats_object(stats_object) { } | 105 : stats_object(stats_object) { } |
| 101 | 106 |
| 102 virtual void AddInt64(const char* name, int64 value) OVERRIDE { | 107 virtual void AddInt64(const char* name, int64 value) OVERRIDE { |
| 103 stats_object->Set(v8::String::New(name), v8::Number::New(value)); | 108 stats_object->Set(v8::String::New(name), v8::Number::New(value)); |
| 104 } | 109 } |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 return; | 331 return; |
| 327 | 332 |
| 328 WebFrame* web_frame = WebFrame::frameForCurrentContext(); | 333 WebFrame* web_frame = WebFrame::frameForCurrentContext(); |
| 329 if (!web_frame) | 334 if (!web_frame) |
| 330 return; | 335 return; |
| 331 | 336 |
| 332 WebView* web_view = web_frame->view(); | 337 WebView* web_view = web_frame->view(); |
| 333 if (!web_view) | 338 if (!web_view) |
| 334 return; | 339 return; |
| 335 | 340 |
| 336 WebViewBenchmarkSupport* benchmark_support = web_view->benchmarkSupport(); | 341 RenderViewImpl* render_view_impl = RenderViewImpl::FromWebView(web_view); |
| 337 if (!benchmark_support) | 342 if (!render_view_impl) |
| 343 return; |
| 344 |
| 345 RenderWidgetCompositor* compositor = render_view_impl->compositor(); |
| 346 if (!compositor) |
| 347 return; |
| 348 |
| 349 const cc::Layer* root_layer = compositor->GetRootLayer(); |
| 350 if (!root_layer) |
| 338 return; | 351 return; |
| 339 | 352 |
| 340 base::FilePath dirpath( | 353 base::FilePath dirpath( |
| 341 base::FilePath::StringType(*dirname, *dirname + dirname.length())); | 354 base::FilePath::StringType(*dirname, *dirname + dirname.length())); |
| 342 if (!file_util::CreateDirectory(dirpath) || | 355 if (!file_util::CreateDirectory(dirpath) || |
| 343 !base::PathIsWritable(dirpath)) { | 356 !base::PathIsWritable(dirpath)) { |
| 344 std::string msg("Path is not writable: "); | 357 std::string msg("Path is not writable: "); |
| 345 msg.append(dirpath.MaybeAsASCII()); | 358 msg.append(dirpath.MaybeAsASCII()); |
| 346 v8::ThrowException(v8::Exception::Error( | 359 v8::ThrowException(v8::Exception::Error( |
| 347 v8::String::New(msg.c_str(), msg.length()))); | 360 v8::String::New(msg.c_str(), msg.length()))); |
| 348 return; | 361 return; |
| 349 } | 362 } |
| 350 | 363 |
| 351 SkPictureRecorder recorder(dirpath); | 364 SkPictureSerializer serializer(dirpath); |
| 352 benchmark_support->paint(&recorder, | 365 serializer.Serialize(root_layer); |
| 353 WebViewBenchmarkSupport::PaintModeEverything); | |
| 354 } | 366 } |
| 355 | 367 |
| 356 static void OnSmoothScrollCompleted( | 368 static void OnSmoothScrollCompleted( |
| 357 CallbackAndContext* callback_and_context) { | 369 CallbackAndContext* callback_and_context) { |
| 358 v8::HandleScope scope(callback_and_context->isolate()); | 370 v8::HandleScope scope(callback_and_context->isolate()); |
| 359 v8::Handle<v8::Context> context = callback_and_context->GetContext(); | 371 v8::Handle<v8::Context> context = callback_and_context->GetContext(); |
| 360 v8::Context::Scope context_scope(context); | 372 v8::Context::Scope context_scope(context); |
| 361 WebFrame* frame = WebFrame::frameForContext(context); | 373 WebFrame* frame = WebFrame::frameForContext(context); |
| 362 if (frame) { | 374 if (frame) { |
| 363 frame->callFunctionEvenIfScriptDisabled( | 375 frame->callFunctionEvenIfScriptDisabled( |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 const v8::FunctionCallbackInfo<v8::Value>& args) { | 528 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 517 WebImageCache::clear(); | 529 WebImageCache::clear(); |
| 518 } | 530 } |
| 519 }; | 531 }; |
| 520 | 532 |
| 521 v8::Extension* GpuBenchmarkingExtension::Get() { | 533 v8::Extension* GpuBenchmarkingExtension::Get() { |
| 522 return new GpuBenchmarkingWrapper(); | 534 return new GpuBenchmarkingWrapper(); |
| 523 } | 535 } |
| 524 | 536 |
| 525 } // namespace content | 537 } // namespace content |
| OLD | NEW |