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

Side by Side Diff: chrome/browser/thumbnails/simple_thumbnail_crop.cc

Issue 11985003: Refactored-out the code of thumbnaling algorithm from thumbnail_tab_helper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added a unit test. Created 7 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
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
6
7 #include "base/metrics/histogram.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "skia/ext/platform_canvas.h"
10 #include "ui/gfx/color_utils.h"
11 #include "ui/gfx/screen.h"
12 #include "ui/gfx/scrollbar_size.h"
13 #include "ui/gfx/size_conversions.h"
14 #include "ui/gfx/skbitmap_operations.h"
15
16 namespace {
17 static const char kThumbnailHistogramName[] = "Thumbnail.ComputeMS";
18 }
19
20 namespace thumbnails {
21
22 SimpleThumbnailCrop::SimpleThumbnailCrop(const gfx::Size& target_size)
23 : target_size_(target_size) {
24 }
25
26
mazda 2013/01/17 18:08:43 nit: remove an extra line.
motek. 2013/01/17 18:39:42 Done.
27 SimpleThumbnailCrop::~SimpleThumbnailCrop() {
28 }
29
30 void SimpleThumbnailCrop::ProcessBitmap(ThumbnailingContext* context,
31 const SkBitmap& bitmap,
32 const ConsumerCallback& callback) {
33 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
34 if (bitmap.isNull() || bitmap.empty())
35 return;
36
37 SkBitmap thumbnail = CreateThumbnail(bitmap,
38 GetThumbnailSizeInPixel(),
39 &context->clip_result);
40
41 context->score.boring_score = CalculateBoringScore(thumbnail);
42 context->score.good_clipping =
43 (context->clip_result == kWiderThanTall ||
44 context->clip_result == kTallerThanWide ||
45 context->clip_result == kNotClipped);
46
47 callback.Run(context, thumbnail);
48 }
49
50 ClipResult SimpleThumbnailCrop::GetCanvasCopyInfo(
51 const gfx::Size& source_size,
52 ui::ScaleFactor scale_factor,
53 gfx::Rect* clipping_rect,
54 gfx::Size* target_size) const {
55 ClipResult clip_result = thumbnails::kNotClipped;
56 *clipping_rect = GetClippingRect(source_size, target_size_, &clip_result);
57 *target_size = GetCopySizeForThumbnail(scale_factor, target_size_);
58 return clip_result;
59 }
60
61 gfx::Rect SimpleThumbnailCrop::GetClippingRect(const gfx::Size& source_size,
62 const gfx::Size& desired_size,
63 ClipResult* clip_result) {
64 DCHECK(clip_result);
65
66 float desired_aspect =
67 static_cast<float>(desired_size.width()) / desired_size.height();
68
69 // Get the clipping rect so that we can preserve the aspect ratio while
70 // filling the destination.
71 gfx::Rect clipping_rect;
72 if (source_size.width() < desired_size.width() ||
73 source_size.height() < desired_size.height()) {
74 // Source image is smaller: we clip the part of source image within the
75 // dest rect, and then stretch it to fill the dest rect. We don't respect
76 // the aspect ratio in this case.
77 clipping_rect = gfx::Rect(desired_size);
78 *clip_result = thumbnails::kSourceIsSmaller;
79 } else {
80 float src_aspect =
81 static_cast<float>(source_size.width()) / source_size.height();
82 if (src_aspect > desired_aspect) {
83 // Wider than tall, clip horizontally: we center the smaller
84 // thumbnail in the wider screen.
85 int new_width = static_cast<int>(source_size.height() * desired_aspect);
86 int x_offset = (source_size.width() - new_width) / 2;
87 clipping_rect.SetRect(x_offset, 0, new_width, source_size.height());
88 *clip_result = (src_aspect >= ThumbnailScore::kTooWideAspectRatio) ?
89 thumbnails::kTooWiderThanTall :
90 thumbnails::kWiderThanTall;
91 } else if (src_aspect < desired_aspect) {
92 clipping_rect =
93 gfx::Rect(source_size.width(), source_size.width() / desired_aspect);
94 *clip_result = thumbnails::kTallerThanWide;
95 } else {
96 clipping_rect = gfx::Rect(source_size);
97 *clip_result = thumbnails::kNotClipped;
98 }
99 }
100 return clipping_rect;
101 }
102
103 // Returns the size used by RenderWidgetHost::CopyFromBackingStore.
104 //
105 // The size is calculated in such a way that the copied size in pixel becomes
106 // equal to (f * kThumbnailWidth, f * kThumbnailHeight), where f is the scale
107 // of ui::SCALE_FACTOR_200P. Since RenderWidgetHost::CopyFromBackingStore takes
108 // the size in DIP, we need to adjust the size based on |view|'s device scale
109 // factor in order to copy the pixels with the size above.
110 //
111 // The copied size was chosen for the following reasons.
112 //
113 // 1. When the scale factor of the primary monitor is ui::SCALE_FACTOR_200P, the
114 // generated thumbnail size is (f * kThumbnailWidth, f * kThumbnailHeight).
115 // In order to avoid degrading the image quality by magnification, the size
116 // of the copied pixels should be equal to or larger than this thumbnail size.
117 //
118 // 2. RenderWidgetHost::CopyFromBackingStore can be costly especially when
119 // it is necessary to read back the web contents image data from GPU. As the
120 // cost is roughly propotional to the number of the copied pixels, the size of
121 // the copied pixels should be as small as possible.
122 //
123 // When the scale factor of the primary monitor is ui::SCALE_FACTOR_100P,
124 // we still copy the pixels with the same size as ui::SCALE_FACTOR_200P because
125 // the resampling method used in RenderWidgetHost::CopyFromBackingStore is not
126 // good enough for the resampled image to be used directly for the thumbnail
127 // (http://crbug.com/141235). We assume this is not an issue in case of
128 // ui::SCALE_FACTOR_200P because the high resolution thumbnail on high density
129 // display alleviates the aliasing.
130 // TODO(mazda): Copy the pixels with the smaller size in the case of
131 // ui::SCALE_FACTOR_100P once the resampling method has been improved.
132 // static
133 gfx::Size SimpleThumbnailCrop::GetCopySizeForThumbnail(
134 ui::ScaleFactor scale_factor, const gfx::Size& thumbnail_size) {
135 gfx::Size copy_size(thumbnail_size);
136 switch (scale_factor) {
137 case ui::SCALE_FACTOR_100P:
138 copy_size = gfx::ToFlooredSize(gfx::ScaleSize(
139 copy_size, ui::GetScaleFactorScale(ui::SCALE_FACTOR_200P)));
140 break;
141 case ui::SCALE_FACTOR_200P:
142 // Use the size as-is.
143 break;
144 default:
145 DLOG(WARNING) << "Unsupported scale factor. Use the same copy size as "
146 << "ui::SCALE_FACTOR_100P";
147 copy_size = gfx::ToFlooredSize(gfx::ScaleSize(
148 copy_size, ui::GetScaleFactorScale(ui::SCALE_FACTOR_200P)));
149 break;
150 }
151 return copy_size;
152 }
153
154 // Returns the size of the thumbnail stored in the database in pixel.
155 gfx::Size SimpleThumbnailCrop::GetThumbnailSizeInPixel() const {
156 // Determine the resolution of the thumbnail based on the maximum scale
157 // factor.
158 // TODO(mazda|oshima): Update thumbnail when the max scale factor changes.
159 // crbug.com/159157.
160 float max_scale_factor =
161 ui::GetScaleFactorScale(ui::GetMaxScaleFactor());
162 return gfx::ToFlooredSize(gfx::ScaleSize(target_size_, max_scale_factor));
163 }
164
165 SkBitmap SimpleThumbnailCrop::GetClippedBitmap(const SkBitmap& bitmap,
166 int desired_width,
167 int desired_height,
168 ClipResult* clip_result) {
169 gfx::Rect clipping_rect =
170 GetClippingRect(gfx::Size(bitmap.width(), bitmap.height()),
171 gfx::Size(desired_width, desired_height),
172 clip_result);
173 SkIRect src_rect = { clipping_rect.x(), clipping_rect.y(),
174 clipping_rect.right(), clipping_rect.bottom() };
175 SkBitmap clipped_bitmap;
176 bitmap.extractSubset(&clipped_bitmap, src_rect);
177 return clipped_bitmap;
178 }
179
180
mazda 2013/01/17 18:08:43 nit: remove an extra line.
motek. 2013/01/17 18:39:42 Done.
181 // Creates a downsampled thumbnail from the given bitmap.
182 // store. The returned bitmap will be isNull if there was an error creating it.
183 SkBitmap SimpleThumbnailCrop::CreateThumbnail(const SkBitmap& bitmap,
184 const gfx::Size& desired_size,
185 ClipResult* clip_result) {
186 base::TimeTicks begin_compute_thumbnail = base::TimeTicks::Now();
187
188 SkBitmap clipped_bitmap;
189 if (*clip_result == thumbnails::kUnprocessed) {
190 // Clip the pixels that will commonly hold a scrollbar, which looks bad in
191 // thumbnails.
192 int scrollbar_size = gfx::scrollbar_size();
193 SkIRect scrollbarless_rect =
194 { 0, 0,
195 std::max(1, bitmap.width() - scrollbar_size),
196 std::max(1, bitmap.height() - scrollbar_size) };
197 SkBitmap bmp;
198 bitmap.extractSubset(&bmp, scrollbarless_rect);
199
200 clipped_bitmap = GetClippedBitmap(
201 bmp, desired_size.width(), desired_size.height(), clip_result);
202 } else {
203 clipped_bitmap = bitmap;
204 }
205
206 // Need to resize it to the size we want, so downsample until it's
207 // close, and let the caller make it the exact size if desired.
208 SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize(
209 clipped_bitmap, desired_size.width(), desired_size.height());
210 #if !defined(USE_AURA)
211 // This is a bit subtle. SkBitmaps are refcounted, but the magic
212 // ones in PlatformCanvas can't be assigned to SkBitmap with proper
213 // refcounting. If the bitmap doesn't change, then the downsampler
214 // will return the input bitmap, which will be the reference to the
215 // weird PlatformCanvas one insetad of a regular one. To get a
216 // regular refcounted bitmap, we need to copy it.
217 //
218 // On Aura, the PlatformCanvas is platform-independent and does not have
219 // any native platform resources that can't be refounted, so this issue does
220 // not occur.
221 //
222 // Note that GetClippedBitmap() does extractSubset() but it won't copy
223 // the pixels, hence we check result size == clipped_bitmap size here.
224 if (clipped_bitmap.width() == result.width() &&
225 clipped_bitmap.height() == result.height())
226 clipped_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
227 #endif
228
229 HISTOGRAM_TIMES(kThumbnailHistogramName,
230 base::TimeTicks::Now() - begin_compute_thumbnail);
231 return result;
232 }
233
234 double SimpleThumbnailCrop::CalculateBoringScore(const SkBitmap& bitmap) {
235 if (bitmap.isNull() || bitmap.empty())
236 return 1.0;
237 int histogram[256] = {0};
238 color_utils::BuildLumaHistogram(bitmap, histogram);
239
240 int color_count = *std::max_element(histogram, histogram + 256);
241 int pixel_count = bitmap.width() * bitmap.height();
242 return static_cast<double>(color_count) / pixel_count;
243 }
244
245 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698