summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-15 16:48:23 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-15 16:48:23 +0000
commit30507928052b10bd7a608c2aa3d7afa78494cef1 (patch)
tree894adb4e6f206b3f23ae668b1f3f909d7ffa9f62 /chrome/renderer
parentc7df381fcaea795f78863f5c1cc1b335fabcae48 (diff)
downloadchromium_src-30507928052b10bd7a608c2aa3d7afa78494cef1.zip
chromium_src-30507928052b10bd7a608c2aa3d7afa78494cef1.tar.gz
chromium_src-30507928052b10bd7a608c2aa3d7afa78494cef1.tar.bz2
One of my previous CL accidentaly reverted 2 changes to these
2 files. (not sure how it happened). Relanding the change. BUG=None TEST=None Review URL: http://codereview.chromium.org/543069 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36359 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/render_view.cc94
-rw-r--r--chrome/renderer/render_view.h9
2 files changed, 76 insertions, 27 deletions
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index a394aa7..b901a7e 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -17,10 +17,12 @@
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/field_trial.h"
+#include "base/histogram.h"
#include "base/process_util.h"
#include "base/singleton.h"
#include "base/string_piece.h"
#include "base/string_util.h"
+#include "base/time.h"
#include "build/build_config.h"
#include "chrome/common/bindings_policy.h"
#include "chrome/common/child_process_logging.h"
@@ -181,7 +183,6 @@ using WebKit::WebWorkerClient;
// define to write the time necessary for thumbnail/DOM text retrieval,
// respectively, into the system debug log
-// #define TIME_BITMAP_RETRIEVAL
// #define TIME_TEXT_RETRIEVAL
// maximum number of characters in the document to index, any text beyond this
@@ -242,6 +243,35 @@ static bool UrlMatchesPermissions(
return false;
}
+static bool PaintViewIntoCanvas(WebView* view,
+ skia::PlatformCanvas& canvas) {
+ view->layout();
+ const WebSize& size = view->size();
+
+ if (!canvas.initialize(size.width, size.height, true))
+ return false;
+
+ view->paint(webkit_glue::ToWebCanvas(&canvas),
+ WebRect(0, 0, size.width, size.height));
+ // TODO: Add a way to snapshot the whole page, not just the currently
+ // visible part.
+
+ return true;
+}
+
+// Calculates how "boring" a thumbnail is. The boring score is the
+// 0,1 ranged percentage of pixels that are the most common
+// luma. Higher boring scores indicate that a higher percentage of a
+// bitmap are all the same brightness.
+static double CalculateBoringScore(SkBitmap* bitmap) {
+ int histogram[256] = {0};
+ color_utils::BuildLumaHistogram(bitmap, histogram);
+
+ int color_count = *std::max_element(histogram, histogram + 256);
+ int pixel_count = bitmap->width() * bitmap->height();
+ return static_cast<double>(color_count) / pixel_count;
+}
+
///////////////////////////////////////////////////////////////////////////////
int32 RenderView::next_page_id_ = 1;
@@ -440,6 +470,7 @@ void RenderView::OnMessageReceived(const IPC::Message& message) {
IPC_BEGIN_MESSAGE_MAP(RenderView, message)
IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, SendThumbnail)
+ IPC_MESSAGE_HANDLER(ViewMsg_CaptureSnapshot, SendSnapshot)
IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages)
IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone)
IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate)
@@ -570,6 +601,24 @@ void RenderView::SendThumbnail() {
Send(new ViewHostMsg_Thumbnail(routing_id_, url, score, thumbnail));
}
+void RenderView::SendSnapshot() {
+ SkBitmap snapshot;
+ bool error = false;
+
+ WebFrame* main_frame = webview()->mainFrame();
+ if (!main_frame)
+ error = true;
+
+ if (!error && !CaptureSnapshot(webview(), &snapshot))
+ error = true;
+
+ DCHECK(error == snapshot.empty()) <<
+ "Snapshot should be empty on error, non-empty otherwise.";
+
+ // Send the snapshot to the browser process.
+ Send(new ViewHostMsg_Snapshot(routing_id_, snapshot));
+}
+
void RenderView::OnPrintPages() {
DCHECK(webview());
if (webview()) {
@@ -686,18 +735,13 @@ bool RenderView::CaptureThumbnail(WebView* view,
int h,
SkBitmap* thumbnail,
ThumbnailScore* score) {
-#ifdef TIME_BITMAP_RETRIEVAL
- double begin = time_util::GetHighResolutionTimeNow();
-#endif
-
- view->layout();
- const WebSize& size = view->size();
+ base::TimeTicks beginning_time = base::TimeTicks::Now();
skia::PlatformCanvas canvas;
- if (!canvas.initialize(size.width, size.height, true))
+
+ // Paint |view| into |canvas|.
+ if (!PaintViewIntoCanvas(view, canvas))
return false;
- view->paint(webkit_glue::ToWebCanvas(&canvas),
- WebRect(0, 0, size.width, size.height));
skia::BitmapPlatformDevice& device =
static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice());
@@ -745,22 +789,28 @@ bool RenderView::CaptureThumbnail(WebView* view,
score->boring_score = CalculateBoringScore(thumbnail);
-#ifdef TIME_BITMAP_RETRIEVAL
- double end = time_util::GetHighResolutionTimeNow();
- char buf[128];
- sprintf_s(buf, "thumbnail in %gms\n", (end - begin) * 1000);
- OutputDebugStringA(buf);
-#endif
+ HISTOGRAM_TIMES("Renderer4.Thumbnail",
+ base::TimeTicks::Now() - beginning_time);
return true;
}
-double RenderView::CalculateBoringScore(SkBitmap* bitmap) {
- int histogram[256] = {0};
- color_utils::BuildLumaHistogram(bitmap, histogram);
+bool RenderView::CaptureSnapshot(WebView* view, SkBitmap* snapshot) {
+ base::TimeTicks beginning_time = base::TimeTicks::Now();
- int color_count = *std::max_element(histogram, histogram + 256);
- int pixel_count = bitmap->width() * bitmap->height();
- return static_cast<double>(color_count) / pixel_count;
+ skia::PlatformCanvas canvas;
+ if (!PaintViewIntoCanvas(view, canvas))
+ return false;
+
+ skia::BitmapPlatformDevice& device =
+ static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice());
+
+ const SkBitmap& bitmap = device.accessBitmap(false);
+ if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
+ return false;
+
+ HISTOGRAM_TIMES("Renderer4.Snapshot",
+ base::TimeTicks::Now() - beginning_time);
+ return true;
}
void RenderView::OnNavigate(const ViewMsg_Navigate_Params& params) {
diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h
index 69f80ab..4d3a4e9 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -520,11 +520,9 @@ class RenderView : public RenderWidget,
SkBitmap* thumbnail,
ThumbnailScore* score);
- // Calculates how "boring" a thumbnail is. The boring score is the
- // 0,1 ranged percentage of pixels that are the most common
- // luma. Higher boring scores indicate that a higher percentage of a
- // bitmap are all the same brightness.
- double CalculateBoringScore(SkBitmap* bitmap);
+ // Capture a snapshot of a view. This is used to allow an extension
+ // to get a snapshot of a tab using chrome.tabs.captureVisibleTab().
+ bool CaptureSnapshot(WebKit::WebView* view, SkBitmap* snapshot);
bool RunJavaScriptMessage(int type,
const std::wstring& message,
@@ -538,6 +536,7 @@ class RenderView : public RenderWidget,
// RenderView IPC message handlers
void SendThumbnail();
+ void SendSnapshot();
void OnPrintPages();
void OnPrintingDone(int document_cookie, bool success);
void OnNavigate(const ViewMsg_Navigate_Params& params);