Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/tab_contents/thumbnail_generator.cc

Issue 6246007: Generate thumbnails in the browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "chrome/browser/tab_contents/thumbnail_generator.h" 5 #include "chrome/browser/tab_contents/thumbnail_generator.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 9
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/scoped_ptr.h" 11 #include "base/scoped_ptr.h"
12 #include "base/time.h" 12 #include "base/time.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "chrome/browser/renderer_host/backing_store.h" 14 #include "chrome/browser/renderer_host/backing_store.h"
15 #include "chrome/browser/renderer_host/render_process_host.h" 15 #include "chrome/browser/renderer_host/render_process_host.h"
16 #include "chrome/browser/renderer_host/render_view_host.h" 16 #include "chrome/browser/renderer_host/render_view_host.h"
17 #include "chrome/browser/tab_contents/tab_contents.h" 17 #include "chrome/browser/tab_contents/tab_contents.h"
18 #include "chrome/common/notification_service.h" 18 #include "chrome/common/notification_service.h"
19 #include "chrome/common/property_bag.h" 19 #include "chrome/common/property_bag.h"
20 #include "gfx/color_utils.h"
20 #include "gfx/rect.h" 21 #include "gfx/rect.h"
21 #include "gfx/skbitmap_operations.h" 22 #include "gfx/skbitmap_operations.h"
23 #include "skia/ext/bitmap_platform_device.h"
24 #include "skia/ext/image_operations.h"
22 #include "skia/ext/platform_canvas.h" 25 #include "skia/ext/platform_canvas.h"
23 #include "third_party/skia/include/core/SkBitmap.h" 26 #include "third_party/skia/include/core/SkBitmap.h"
24 27
25 #if defined(OS_WIN) 28 #if defined(OS_WIN)
26 #include "app/win/win_util.h" 29 #include "app/win/win_util.h"
27 #endif 30 #endif
28 31
29 // Overview 32 // Overview
30 // -------- 33 // --------
31 // This class provides current thumbnails for tabs. The simplest operation is 34 // This class provides current thumbnails for tabs. The simplest operation is
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 WidgetThumbnail* wt = GetThumbnailAccessor()->GetProperty( 93 WidgetThumbnail* wt = GetThumbnailAccessor()->GetProperty(
91 host->property_bag()); 94 host->property_bag());
92 if (wt) 95 if (wt)
93 return wt; 96 return wt;
94 97
95 GetThumbnailAccessor()->SetProperty(host->property_bag(), 98 GetThumbnailAccessor()->SetProperty(host->property_bag(),
96 WidgetThumbnail()); 99 WidgetThumbnail());
97 return GetThumbnailAccessor()->GetProperty(host->property_bag()); 100 return GetThumbnailAccessor()->GetProperty(host->property_bag());
98 } 101 }
99 102
103 // Calculates how "boring" a thumbnail is. The boring score is the
104 // 0,1 ranged percentage of pixels that are the most common
105 // luma. Higher boring scores indicate that a higher percentage of a
106 // bitmap are all the same brightness.
107 static double CalculateBoringScore(SkBitmap* bitmap) {
108 int histogram[256] = {0};
109 color_utils::BuildLumaHistogram(bitmap, histogram);
110
111 int color_count = *std::max_element(histogram, histogram + 256);
112 int pixel_count = bitmap->width() * bitmap->height();
113 return static_cast<double>(color_count) / pixel_count;
114 }
115
100 // Creates a downsampled thumbnail for the given backing store. The returned 116 // Creates a downsampled thumbnail for the given backing store. The returned
101 // bitmap will be isNull if there was an error creating it. 117 // bitmap will be isNull if there was an error creating it.
102 SkBitmap GetBitmapForBackingStore(BackingStore* backing_store, 118 SkBitmap GetBitmapForBackingStore(
103 int desired_width, 119 BackingStore* backing_store,
104 int desired_height) { 120 int desired_width,
121 int desired_height,
122 int options,
123 ThumbnailGenerator::ThumbnailResult* thumbnail_result) {
105 base::TimeTicks begin_compute_thumbnail = base::TimeTicks::Now(); 124 base::TimeTicks begin_compute_thumbnail = base::TimeTicks::Now();
106 125
107 SkBitmap result; 126 SkBitmap result;
108 127
109 // Get the bitmap as a Skia object so we can resample it. This is a large 128 // Get the bitmap as a Skia object so we can resample it. This is a large
110 // allocation and we can tolerate failure here, so give up if the allocation 129 // allocation and we can tolerate failure here, so give up if the allocation
111 // fails. 130 // fails.
112 skia::PlatformCanvas temp_canvas; 131 skia::PlatformCanvas temp_canvas;
113 if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()), 132 if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()),
114 &temp_canvas)) 133 &temp_canvas))
115 return result; 134 return result;
116 const SkBitmap& bmp = temp_canvas.getTopPlatformDevice().accessBitmap(false); 135 const SkBitmap& bmp = temp_canvas.getTopPlatformDevice().accessBitmap(false);
117 136
118 // Need to resize it to the size we want, so downsample until it's 137 // Check if a clipped thumbnail is requested.
119 // close, and let the caller make it the exact size if desired. 138 if (options & ThumbnailGenerator::kClippedThumbnail) {
120 result = SkBitmapOperations::DownsampleByTwoUntilSize( 139 const SkRect dest_rect = { 0, 0,
121 bmp, desired_width, desired_height); 140 SkIntToScalar(desired_width),
141 SkIntToScalar(desired_height) };
142 const float dest_aspect = dest_rect.width() / dest_rect.height();
122 143
123 // This is a bit subtle. SkBitmaps are refcounted, but the magic 144 // Get the src rect so that we can preserve the aspect ratio while filling
124 // ones in PlatformCanvas can't be assigned to SkBitmap with proper 145 // the destination.
125 // refcounting. If the bitmap doesn't change, then the downsampler 146 SkIRect src_rect;
126 // will return the input bitmap, which will be the reference to the 147 if (bmp.width() < dest_rect.width() ||
127 // weird PlatformCanvas one insetad of a regular one. To get a 148 bmp.height() < dest_rect.height()) {
128 // regular refcounted bitmap, we need to copy it. 149 // Source image is smaller: we clip the part of source image within the
129 if (bmp.width() == result.width() && 150 // dest rect, and then stretch it to fill the dest rect. We don't respect
130 bmp.height() == result.height()) 151 // the aspect ratio in this case.
131 bmp.copyTo(&result, SkBitmap::kARGB_8888_Config); 152 src_rect.set(0, 0, static_cast<S16CPU>(dest_rect.width()),
153 static_cast<S16CPU>(dest_rect.height()));
154 if (thumbnail_result)
155 thumbnail_result->clip_result = ThumbnailGenerator::kSourceIsSmaller;
156 } else {
157 const float src_aspect =
158 static_cast<float>(bmp.width()) / bmp.height();
159 if (src_aspect > dest_aspect) {
160 // Wider than tall, clip horizontally: we center the smaller
161 // thumbnail in the wider screen.
162 S16CPU new_width = static_cast<S16CPU>(bmp.height() * dest_aspect);
163 S16CPU x_offset = (bmp.width() - new_width) / 2;
164 src_rect.set(x_offset, 0, new_width + x_offset, bmp.height());
165 if (thumbnail_result)
166 thumbnail_result->clip_result = ThumbnailGenerator::kWiderThanTall;
167 } else if (src_aspect < dest_aspect) {
168 src_rect.set(0, 0, bmp.width(),
169 static_cast<S16CPU>(bmp.width() / dest_aspect));
170 if (thumbnail_result)
171 thumbnail_result->clip_result = ThumbnailGenerator::kTallerThanWide;
172 } else {
173 src_rect.set(0, 0, bmp.width(), bmp.height());
174 if (thumbnail_result)
175 thumbnail_result->clip_result = ThumbnailGenerator::kNotClipped;
176 }
177 }
178
179 SkBitmap subset;
180 temp_canvas.getTopPlatformDevice().accessBitmap(false).
181 extractSubset(&subset, src_rect);
182
183 // Need to resize it to the size we want, so downsample until it's
184 // close, and let the caller make it the exact size if desired.
185 result = SkBitmapOperations::DownsampleByTwoUntilSize(
186 subset, desired_width, desired_height);
187 } else {
188 // Need to resize it to the size we want, so downsample until it's
189 // close, and let the caller make it the exact size if desired.
190 result = SkBitmapOperations::DownsampleByTwoUntilSize(
191 bmp, desired_width, desired_height);
192
193 // This is a bit subtle. SkBitmaps are refcounted, but the magic
194 // ones in PlatformCanvas can't be assigned to SkBitmap with proper
195 // refcounting. If the bitmap doesn't change, then the downsampler
196 // will return the input bitmap, which will be the reference to the
197 // weird PlatformCanvas one insetad of a regular one. To get a
198 // regular refcounted bitmap, we need to copy it.
199 if (bmp.width() == result.width() &&
200 bmp.height() == result.height())
201 bmp.copyTo(&result, SkBitmap::kARGB_8888_Config);
202 }
203
204 // Calculate the boring score if requested.
205 if (options & ThumbnailGenerator::kBoringScore && thumbnail_result) {
206 thumbnail_result->boring_score = CalculateBoringScore(&result);
207 }
132 208
133 HISTOGRAM_TIMES(kThumbnailHistogramName, 209 HISTOGRAM_TIMES(kThumbnailHistogramName,
134 base::TimeTicks::Now() - begin_compute_thumbnail); 210 base::TimeTicks::Now() - begin_compute_thumbnail);
135 return result; 211 return result;
136 } 212 }
137 213
138 } // namespace 214 } // namespace
139 215
140 struct ThumbnailGenerator::AsyncRequestInfo { 216 struct ThumbnailGenerator::AsyncRequestInfo {
141 scoped_ptr<ThumbnailReadyCallback> callback; 217 scoped_ptr<ThumbnailReadyCallback> callback;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 ThumbnailReadyCallback* callback, 292 ThumbnailReadyCallback* callback,
217 gfx::Size page_size, 293 gfx::Size page_size,
218 gfx::Size desired_size) { 294 gfx::Size desired_size) {
219 if (prefer_backing_store) { 295 if (prefer_backing_store) {
220 BackingStore* backing_store = renderer->GetBackingStore(false); 296 BackingStore* backing_store = renderer->GetBackingStore(false);
221 if (backing_store) { 297 if (backing_store) {
222 // We were able to find a non-null backing store for this renderer, so 298 // We were able to find a non-null backing store for this renderer, so
223 // we'll go with it. 299 // we'll go with it.
224 SkBitmap first_try = GetBitmapForBackingStore(backing_store, 300 SkBitmap first_try = GetBitmapForBackingStore(backing_store,
225 desired_size.width(), 301 desired_size.width(),
226 desired_size.height()); 302 desired_size.height(),
303 kNone,
304 NULL);
227 callback->Run(first_try); 305 callback->Run(first_try);
228 306
229 delete callback; 307 delete callback;
230 return; 308 return;
231 } 309 }
232 // Now, if the backing store didn't exist, we will still try and 310 // Now, if the backing store didn't exist, we will still try and
233 // render asynchronously. 311 // render asynchronously.
234 } 312 }
235 313
236 // We are going to render the thumbnail asynchronously now, so keep 314 // We are going to render the thumbnail asynchronously now, so keep
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 NOTREACHED() << "Callback already registered?"; 346 NOTREACHED() << "Callback already registered?";
269 return; 347 return;
270 } 348 }
271 349
272 renderer->PaintAtSize( 350 renderer->PaintAtSize(
273 renderer_dib_handle, sequence_num, page_size, desired_size); 351 renderer_dib_handle, sequence_num, page_size, desired_size);
274 } 352 }
275 353
276 SkBitmap ThumbnailGenerator::GetThumbnailForRenderer( 354 SkBitmap ThumbnailGenerator::GetThumbnailForRenderer(
277 RenderWidgetHost* renderer) const { 355 RenderWidgetHost* renderer) const {
356 return GetThumbnailForRendererWithOptions(renderer, kNone, NULL);
357 }
358
359 SkBitmap ThumbnailGenerator::GetThumbnailForRendererWithOptions(
360 RenderWidgetHost* renderer,
361 int options,
362 ThumbnailResult* result) const {
278 WidgetThumbnail* wt = GetDataForHost(renderer); 363 WidgetThumbnail* wt = GetDataForHost(renderer);
279 364
280 BackingStore* backing_store = renderer->GetBackingStore(false); 365 BackingStore* backing_store = renderer->GetBackingStore(false);
281 if (!backing_store) { 366 if (!backing_store) {
282 // When we have no backing store, there's no choice in what to use. We 367 // When we have no backing store, there's no choice in what to use. We
283 // have to return either the existing thumbnail or the empty one if there 368 // have to return either the existing thumbnail or the empty one if there
284 // isn't a saved one. 369 // isn't a saved one.
285 return wt->thumbnail; 370 return wt->thumbnail;
286 } 371 }
287 372
288 // Now that we have a backing store, we have a choice to use it to make 373 // Now that we have a backing store, we have a choice to use it to make
289 // a new thumbnail, or use a previously stashed one if we have it. 374 // a new thumbnail, or use a previously stashed one if we have it.
290 // 375 //
291 // Return the previously-computed one if we have it and it hasn't expired. 376 // Return the previously-computed one if we have it and it hasn't expired.
292 if (!wt->thumbnail.isNull() && 377 if (!wt->thumbnail.isNull() &&
293 (no_timeout_ || 378 (no_timeout_ ||
294 base::TimeTicks::Now() - 379 base::TimeTicks::Now() -
295 base::TimeDelta::FromMilliseconds(kVisibilitySlopMS) < wt->last_shown)) 380 base::TimeDelta::FromMilliseconds(kVisibilitySlopMS) < wt->last_shown))
296 return wt->thumbnail; 381 return wt->thumbnail;
297 382
298 // Save this thumbnail in case we need to use it again soon. It will be 383 // Save this thumbnail in case we need to use it again soon. It will be
299 // invalidated on the next paint. 384 // invalidated on the next paint.
300 wt->thumbnail = GetBitmapForBackingStore(backing_store, 385 wt->thumbnail = GetBitmapForBackingStore(backing_store,
301 kThumbnailWidth, 386 kThumbnailWidth,
302 kThumbnailHeight); 387 kThumbnailHeight,
388 options,
389 result);
303 return wt->thumbnail; 390 return wt->thumbnail;
304 } 391 }
305 392
306 void ThumbnailGenerator::WidgetWillDestroyBackingStore( 393 void ThumbnailGenerator::WidgetWillDestroyBackingStore(
307 RenderWidgetHost* widget, 394 RenderWidgetHost* widget,
308 BackingStore* backing_store) { 395 BackingStore* backing_store) {
309 // Since the backing store is going away, we need to save it as a thumbnail. 396 // Since the backing store is going away, we need to save it as a thumbnail.
310 WidgetThumbnail* wt = GetDataForHost(widget); 397 WidgetThumbnail* wt = GetDataForHost(widget);
311 398
312 // If there is already a thumbnail on the RWH that's visible, it means that 399 // If there is already a thumbnail on the RWH that's visible, it means that
313 // not enough time has elapsed since being shown, and we can ignore generating 400 // not enough time has elapsed since being shown, and we can ignore generating
314 // a new one. 401 // a new one.
315 if (!wt->thumbnail.isNull()) 402 if (!wt->thumbnail.isNull())
316 return; 403 return;
317 404
318 // Save a scaled-down image of the page in case we're asked for the thumbnail 405 // Save a scaled-down image of the page in case we're asked for the thumbnail
319 // when there is no RenderViewHost. If this fails, we don't want to overwrite 406 // when there is no RenderViewHost. If this fails, we don't want to overwrite
320 // an existing thumbnail. 407 // an existing thumbnail.
321 SkBitmap new_thumbnail = GetBitmapForBackingStore(backing_store, 408 SkBitmap new_thumbnail = GetBitmapForBackingStore(backing_store,
322 kThumbnailWidth, 409 kThumbnailWidth,
323 kThumbnailHeight); 410 kThumbnailHeight,
411 kNone,
412 NULL);
324 if (!new_thumbnail.isNull()) 413 if (!new_thumbnail.isNull())
325 wt->thumbnail = new_thumbnail; 414 wt->thumbnail = new_thumbnail;
326 } 415 }
327 416
328 void ThumbnailGenerator::WidgetDidUpdateBackingStore(RenderWidgetHost* widget) { 417 void ThumbnailGenerator::WidgetDidUpdateBackingStore(RenderWidgetHost* widget) {
329 // Notify interested parties that they might want to update their 418 // Notify interested parties that they might want to update their
330 // snapshots. 419 // snapshots.
331 NotificationService::current()->Notify( 420 NotificationService::current()->Notify(
332 NotificationType::THUMBNAIL_GENERATOR_SNAPSHOT_CHANGED, 421 NotificationType::THUMBNAIL_GENERATOR_SNAPSHOT_CHANGED,
333 Source<ThumbnailGenerator>(this), 422 Source<ThumbnailGenerator>(this),
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 &ThumbnailGenerator::ShownDelayHandler); 616 &ThumbnailGenerator::ShownDelayHandler);
528 } 617 }
529 } 618 }
530 619
531 void ThumbnailGenerator::EraseHostFromShownList(RenderWidgetHost* widget) { 620 void ThumbnailGenerator::EraseHostFromShownList(RenderWidgetHost* widget) {
532 std::vector<RenderWidgetHost*>::iterator found = 621 std::vector<RenderWidgetHost*>::iterator found =
533 std::find(shown_hosts_.begin(), shown_hosts_.end(), widget); 622 std::find(shown_hosts_.begin(), shown_hosts_.end(), widget);
534 if (found != shown_hosts_.end()) 623 if (found != shown_hosts_.end())
535 shown_hosts_.erase(found); 624 shown_hosts_.erase(found);
536 } 625 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698