Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / thumbnails / simple_thumbnail_crop.cc
blobb426325c7c4e586ad5c465df10a9ca92bcb34c5e
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.
5 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
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/geometry/size_conversions.h"
12 #include "ui/gfx/image/image_skia.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/gfx/scrollbar_size.h"
15 #include "ui/gfx/skbitmap_operations.h"
17 namespace {
18 static const char kThumbnailHistogramName[] = "Thumbnail.ComputeMS";
21 namespace thumbnails {
23 SimpleThumbnailCrop::SimpleThumbnailCrop(const gfx::Size& target_size)
24 : target_size_(target_size) {
25 DCHECK(!target_size.IsEmpty());
28 ClipResult SimpleThumbnailCrop::GetCanvasCopyInfo(
29 const gfx::Size& source_size,
30 ui::ScaleFactor scale_factor,
31 gfx::Rect* clipping_rect,
32 gfx::Size* target_size) const {
33 DCHECK(!source_size.IsEmpty());
34 ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED;
35 *clipping_rect = GetClippingRect(source_size, target_size_, &clip_result);
36 *target_size = GetCopySizeForThumbnail(scale_factor, target_size_);
37 return clip_result;
40 void SimpleThumbnailCrop::ProcessBitmap(
41 scoped_refptr<ThumbnailingContext> context,
42 const ConsumerCallback& callback,
43 const SkBitmap& bitmap) {
44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
45 if (bitmap.isNull() || bitmap.empty())
46 return;
48 SkBitmap thumbnail = CreateThumbnail(
49 bitmap,
50 ComputeTargetSizeAtMaximumScale(target_size_),
51 &context->clip_result);
53 context->score.boring_score = color_utils::CalculateBoringScore(thumbnail);
54 context->score.good_clipping =
55 (context->clip_result == CLIP_RESULT_WIDER_THAN_TALL ||
56 context->clip_result == CLIP_RESULT_TALLER_THAN_WIDE ||
57 context->clip_result == CLIP_RESULT_NOT_CLIPPED);
59 callback.Run(*context.get(), thumbnail);
62 SkBitmap SimpleThumbnailCrop::GetClippedBitmap(const SkBitmap& bitmap,
63 int desired_width,
64 int desired_height,
65 ClipResult* clip_result) {
66 gfx::Rect clipping_rect =
67 GetClippingRect(gfx::Size(bitmap.width(), bitmap.height()),
68 gfx::Size(desired_width, desired_height),
69 clip_result);
70 SkIRect src_rect = { clipping_rect.x(), clipping_rect.y(),
71 clipping_rect.right(), clipping_rect.bottom() };
72 SkBitmap clipped_bitmap;
73 bitmap.extractSubset(&clipped_bitmap, src_rect);
74 return clipped_bitmap;
77 // Returns the size used by RenderWidgetHost::CopyFromBackingStore.
79 // The size is calculated in such a way that the copied size in pixel becomes
80 // equal to (f * kThumbnailWidth, f * kThumbnailHeight), where f is the scale
81 // of ui::SCALE_FACTOR_200P. Since RenderWidgetHost::CopyFromBackingStore takes
82 // the size in DIP, we need to adjust the size based on |view|'s device scale
83 // factor in order to copy the pixels with the size above.
85 // The copied size was chosen for the following reasons.
87 // 1. When the scale factor of the primary monitor is ui::SCALE_FACTOR_200P, the
88 // generated thumbnail size is (f * kThumbnailWidth, f * kThumbnailHeight).
89 // In order to avoid degrading the image quality by magnification, the size
90 // of the copied pixels should be equal to or larger than this thumbnail size.
92 // 2. RenderWidgetHost::CopyFromBackingStore can be costly especially when
93 // it is necessary to read back the web contents image data from GPU. As the
94 // cost is roughly propotional to the number of the copied pixels, the size of
95 // the copied pixels should be as small as possible.
97 // When the scale factor of the primary monitor is ui::SCALE_FACTOR_100P,
98 // we still copy the pixels with the same size as ui::SCALE_FACTOR_200P (2.0f)
99 // because the resampling method used in RenderWidgetHost::CopyFromBackingStore
100 // is not good enough for the resampled image to be used directly for the
101 // thumbnail (http://crbug.com/141235). We assume this is not an issue in case of
102 // ui::SCALE_FACTOR_200P because the high resolution thumbnail on high density
103 // display alleviates the aliasing.
104 // TODO(mazda): Copy the pixels with the smaller size in the case of
105 // ui::SCALE_FACTOR_100P once the resampling method has been improved.
106 // static
107 gfx::Size SimpleThumbnailCrop::GetCopySizeForThumbnail(
108 ui::ScaleFactor scale_factor,
109 const gfx::Size& thumbnail_size) {
110 gfx::Size copy_size(thumbnail_size);
111 switch (scale_factor) {
112 case ui::SCALE_FACTOR_100P:
113 copy_size = gfx::ToFlooredSize(gfx::ScaleSize(copy_size, 2.0f));
114 break;
115 case ui::SCALE_FACTOR_200P:
116 // Use the size as-is.
117 break;
118 default:
119 DLOG(WARNING) << "Unsupported scale factor. Use the same copy size as "
120 << "ui::SCALE_FACTOR_100P";
121 copy_size = gfx::ToFlooredSize(gfx::ScaleSize(
122 copy_size, gfx::ImageSkia::GetMaxSupportedScale()));
123 break;
125 return copy_size;
128 gfx::Rect SimpleThumbnailCrop::GetClippingRect(const gfx::Size& source_size,
129 const gfx::Size& desired_size,
130 ClipResult* clip_result) {
131 DCHECK(clip_result);
133 float desired_aspect =
134 static_cast<float>(desired_size.width()) / desired_size.height();
136 // Get the clipping rect so that we can preserve the aspect ratio while
137 // filling the destination.
138 gfx::Rect clipping_rect;
139 if (source_size.width() < desired_size.width() ||
140 source_size.height() < desired_size.height()) {
141 // Source image is smaller: we clip the part of source image within the
142 // dest rect, and then stretch it to fill the dest rect. We don't respect
143 // the aspect ratio in this case.
144 clipping_rect = gfx::Rect(desired_size);
145 *clip_result = thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER;
146 } else {
147 float src_aspect =
148 static_cast<float>(source_size.width()) / source_size.height();
149 if (src_aspect > desired_aspect) {
150 // Wider than tall, clip horizontally: we center the smaller
151 // thumbnail in the wider screen.
152 int new_width = static_cast<int>(source_size.height() * desired_aspect);
153 int x_offset = (source_size.width() - new_width) / 2;
154 clipping_rect.SetRect(x_offset, 0, new_width, source_size.height());
155 *clip_result = (src_aspect >= ThumbnailScore::kTooWideAspectRatio) ?
156 thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL :
157 thumbnails::CLIP_RESULT_WIDER_THAN_TALL;
158 } else if (src_aspect < desired_aspect) {
159 clipping_rect =
160 gfx::Rect(source_size.width(), source_size.width() / desired_aspect);
161 *clip_result = thumbnails::CLIP_RESULT_TALLER_THAN_WIDE;
162 } else {
163 clipping_rect = gfx::Rect(source_size);
164 *clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED;
167 return clipping_rect;
170 // static
171 gfx::Size SimpleThumbnailCrop::ComputeTargetSizeAtMaximumScale(
172 const gfx::Size& given_size) {
173 // TODO(mazda|oshima): Update thumbnail when the max scale factor changes.
174 // crbug.com/159157.
175 float max_scale_factor = gfx::ImageSkia::GetMaxSupportedScale();
176 return gfx::ToFlooredSize(gfx::ScaleSize(given_size, max_scale_factor));
179 SimpleThumbnailCrop::~SimpleThumbnailCrop() {
182 // Creates a downsampled thumbnail from the given bitmap.
183 // store. The returned bitmap will be isNull if there was an error creating it.
184 SkBitmap SimpleThumbnailCrop::CreateThumbnail(const SkBitmap& bitmap,
185 const gfx::Size& desired_size,
186 ClipResult* clip_result) {
187 base::TimeTicks begin_compute_thumbnail = base::TimeTicks::Now();
189 SkBitmap clipped_bitmap;
190 if (*clip_result == thumbnails::CLIP_RESULT_UNPROCESSED) {
191 // Clip the pixels that will commonly hold a scrollbar, which looks bad in
192 // thumbnails.
193 int scrollbar_size = gfx::scrollbar_size();
194 SkIRect scrollbarless_rect =
195 { 0, 0,
196 std::max(1, bitmap.width() - scrollbar_size),
197 std::max(1, bitmap.height() - scrollbar_size) };
198 SkBitmap bmp;
199 bitmap.extractSubset(&bmp, scrollbarless_rect);
201 clipped_bitmap = GetClippedBitmap(
202 bmp, desired_size.width(), desired_size.height(), clip_result);
203 } else {
204 clipped_bitmap = bitmap;
207 // Need to resize it to the size we want, so downsample until it's
208 // close, and let the caller make it the exact size if desired.
209 SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize(
210 clipped_bitmap, desired_size.width(), desired_size.height());
211 #if !defined(USE_AURA)
212 // This is a bit subtle. SkBitmaps are refcounted, but the magic
213 // ones in PlatformCanvas can't be assigned to SkBitmap with proper
214 // refcounting. If the bitmap doesn't change, then the downsampler
215 // will return the input bitmap, which will be the reference to the
216 // weird PlatformCanvas one insetad of a regular one. To get a
217 // regular refcounted bitmap, we need to copy it.
219 // On Aura, the PlatformCanvas is platform-independent and does not have
220 // any native platform resources that can't be refounted, so this issue does
221 // not occur.
223 // Note that GetClippedBitmap() does extractSubset() but it won't copy
224 // the pixels, hence we check result size == clipped_bitmap size here.
225 if (clipped_bitmap.width() == result.width() &&
226 clipped_bitmap.height() == result.height())
227 clipped_bitmap.copyTo(&result, kN32_SkColorType);
228 #endif
230 LOCAL_HISTOGRAM_TIMES(kThumbnailHistogramName,
231 base::TimeTicks::Now() - begin_compute_thumbnail);
232 return result;
235 } // namespace thumbnails