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

Side by Side Diff: trunk/src/chrome/browser/favicon/favicon_util.cc

Issue 24262008: Revert 224473 "Remove dependency on ui::ScaleFactor from ui/gfx" (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 3 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) 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 "chrome/browser/favicon/favicon_util.h" 5 #include "chrome/browser/favicon/favicon_util.h"
6 6
7 #include "chrome/browser/history/select_favicon_frames.h" 7 #include "chrome/browser/history/select_favicon_frames.h"
8 #include "chrome/common/favicon/favicon_types.h" 8 #include "chrome/common/favicon/favicon_types.h"
9 #include "skia/ext/image_operations.h" 9 #include "skia/ext/image_operations.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 10 #include "third_party/skia/include/core/SkBitmap.h"
(...skipping 25 matching lines...) Expand all
36 if (favicon_size == 0) { 36 if (favicon_size == 0) {
37 int maximum_area = 0; 37 int maximum_area = 0;
38 scoped_refptr<base::RefCountedMemory> best_candidate; 38 scoped_refptr<base::RefCountedMemory> best_candidate;
39 for (size_t i = 0; i < png_data.size(); ++i) { 39 for (size_t i = 0; i < png_data.size(); ++i) {
40 int area = png_data[i].pixel_size.GetArea(); 40 int area = png_data[i].pixel_size.GetArea();
41 if (area > maximum_area) { 41 if (area > maximum_area) {
42 maximum_area = area; 42 maximum_area = area;
43 best_candidate = png_data[i].bitmap_data; 43 best_candidate = png_data[i].bitmap_data;
44 } 44 }
45 } 45 }
46 png_reps.push_back(gfx::ImagePNGRep(best_candidate, 1.0f)); 46 png_reps.push_back(gfx::ImagePNGRep(best_candidate,
47 ui::SCALE_FACTOR_100P));
47 return png_reps; 48 return png_reps;
48 } 49 }
49 50
50 // Cache the scale factor for each pixel size as |scale_factors| may contain 51 // Cache the scale factor for each pixel size as |scale_factors| may contain
51 // any of GetFaviconScaleFactors() which may include scale factors not 52 // any of GetFaviconScaleFactors() which may include scale factors not
52 // supported by the platform. (ui::GetSupportedScaleFactor() cannot be used.) 53 // supported by the platform. (ui::GetScaleFactorFromScale() cannot be used.)
53 std::map<int, ui::ScaleFactor> desired_pixel_sizes; 54 std::map<int, ui::ScaleFactor> desired_pixel_sizes;
54 for (size_t i = 0; i < scale_factors.size(); ++i) { 55 for (size_t i = 0; i < scale_factors.size(); ++i) {
55 int pixel_size = floor(favicon_size * 56 int pixel_size = floor(favicon_size *
56 ui::GetImageScale(scale_factors[i])); 57 ui::GetScaleFactorScale(scale_factors[i]));
57 desired_pixel_sizes[pixel_size] = scale_factors[i]; 58 desired_pixel_sizes[pixel_size] = scale_factors[i];
58 } 59 }
59 60
60 for (size_t i = 0; i < png_data.size(); ++i) { 61 for (size_t i = 0; i < png_data.size(); ++i) {
61 if (!png_data[i].is_valid()) 62 if (!png_data[i].is_valid())
62 continue; 63 continue;
63 64
64 const gfx::Size& pixel_size = png_data[i].pixel_size; 65 const gfx::Size& pixel_size = png_data[i].pixel_size;
65 if (pixel_size.width() != pixel_size.height()) 66 if (pixel_size.width() != pixel_size.height())
66 continue; 67 continue;
67 68
68 std::map<int, ui::ScaleFactor>::iterator it = desired_pixel_sizes.find( 69 std::map<int, ui::ScaleFactor>::iterator it = desired_pixel_sizes.find(
69 pixel_size.width()); 70 pixel_size.width());
70 if (it == desired_pixel_sizes.end()) 71 if (it == desired_pixel_sizes.end())
71 continue; 72 continue;
72 73
73 png_reps.push_back( 74 png_reps.push_back(gfx::ImagePNGRep(png_data[i].bitmap_data, it->second));
74 gfx::ImagePNGRep(png_data[i].bitmap_data,
75 ui::GetImageScale(it->second)));
76 } 75 }
77 76
78 return png_reps; 77 return png_reps;
79 } 78 }
80 79
81 // Returns a resampled bitmap of 80 // Returns a resampled bitmap of
82 // |desired_size_in_pixel| x |desired_size_in_pixel| by resampling the best 81 // |desired_size_in_pixel| x |desired_size_in_pixel| by resampling the best
83 // bitmap out of |input_bitmaps|. ResizeBitmapByDownsamplingIfPossible() is 82 // bitmap out of |input_bitmaps|. ResizeBitmapByDownsamplingIfPossible() is
84 // similar to SelectFaviconFrames() but it operates on bitmaps which have 83 // similar to SelectFaviconFrames() but it operates on bitmaps which have
85 // already been resampled via SelectFaviconFrames(). 84 // already been resampled via SelectFaviconFrames().
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 return skia::ImageOperations::Resize(best_bitmap, 130 return skia::ImageOperations::Resize(best_bitmap,
132 skia::ImageOperations::RESIZE_LANCZOS3, 131 skia::ImageOperations::RESIZE_LANCZOS3,
133 desired_size_in_pixel, 132 desired_size_in_pixel,
134 desired_size_in_pixel); 133 desired_size_in_pixel);
135 } 134 }
136 135
137 } // namespace 136 } // namespace
138 137
139 // static 138 // static
140 std::vector<ui::ScaleFactor> FaviconUtil::GetFaviconScaleFactors() { 139 std::vector<ui::ScaleFactor> FaviconUtil::GetFaviconScaleFactors() {
141 const float kScale1x = ui::GetImageScale(ui::SCALE_FACTOR_100P); 140 const float kScale1x = ui::GetScaleFactorScale(ui::SCALE_FACTOR_100P);
142 std::vector<ui::ScaleFactor> favicon_scale_factors = 141 std::vector<ui::ScaleFactor> favicon_scale_factors =
143 ui::GetSupportedScaleFactors(); 142 ui::GetSupportedScaleFactors();
144 143
145 // The scale factors returned from ui::GetSupportedScaleFactors() are sorted. 144 // The scale factors returned from ui::GetSupportedScaleFactors() are sorted.
146 // Insert the 1x scale factor such that GetFaviconScaleFactors() is sorted as 145 // Insert the 1x scale factor such that GetFaviconScaleFactors() is sorted as
147 // well. 146 // well.
148 size_t insert_index = favicon_scale_factors.size(); 147 size_t insert_index = favicon_scale_factors.size();
149 for (size_t i = 0; i < favicon_scale_factors.size(); ++i) { 148 for (size_t i = 0; i < favicon_scale_factors.size(); ++i) {
150 float scale = ui::GetImageScale(favicon_scale_factors[i]); 149 float scale = ui::GetScaleFactorScale(favicon_scale_factors[i]);
151 if (scale == kScale1x) { 150 if (scale == kScale1x) {
152 return favicon_scale_factors; 151 return favicon_scale_factors;
153 } else if (scale > kScale1x) { 152 } else if (scale > kScale1x) {
154 insert_index = i; 153 insert_index = i;
155 break; 154 break;
156 } 155 }
157 } 156 }
158 // TODO(ios): 100p should not be necessary on iOS retina devices. However 157 // TODO(ios): 100p should not be necessary on iOS retina devices. However
159 // the sync service only supports syncing 100p favicons. Until sync supports 158 // the sync service only supports syncing 100p favicons. Until sync supports
160 // other scales 100p is needed in the list of scale factors to retrieve and 159 // other scales 100p is needed in the list of scale factors to retrieve and
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the 194 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the
196 // largest favicon if |favicon_size| == 0. 195 // largest favicon if |favicon_size| == 0.
197 if (favicon_size == 0) 196 if (favicon_size == 0)
198 return gfx::Image(png_reps); 197 return gfx::Image(png_reps);
199 198
200 std::vector<ui::ScaleFactor> scale_factors_to_generate = scale_factors; 199 std::vector<ui::ScaleFactor> scale_factors_to_generate = scale_factors;
201 for (size_t i = 0; i < png_reps.size(); ++i) { 200 for (size_t i = 0; i < png_reps.size(); ++i) {
202 std::vector<ui::ScaleFactor>::iterator it = std::find( 201 std::vector<ui::ScaleFactor>::iterator it = std::find(
203 scale_factors_to_generate.begin(), 202 scale_factors_to_generate.begin(),
204 scale_factors_to_generate.end(), 203 scale_factors_to_generate.end(),
205 ui::GetSupportedScaleFactor(png_reps[i].scale)); 204 png_reps[i].scale_factor);
206 CHECK(it != scale_factors_to_generate.end()); 205 CHECK(it != scale_factors_to_generate.end());
207 scale_factors_to_generate.erase(it); 206 scale_factors_to_generate.erase(it);
208 } 207 }
209 208
210 if (scale_factors_to_generate.empty()) 209 if (scale_factors_to_generate.empty())
211 return gfx::Image(png_reps); 210 return gfx::Image(png_reps);
212 211
213 std::vector<SkBitmap> bitmaps; 212 std::vector<SkBitmap> bitmaps;
214 for (size_t i = 0; i < png_data.size(); ++i) { 213 for (size_t i = 0; i < png_data.size(); ++i) {
215 if (!png_data[i].is_valid()) 214 if (!png_data[i].is_valid())
216 continue; 215 continue;
217 216
218 SkBitmap bitmap; 217 SkBitmap bitmap;
219 if (gfx::PNGCodec::Decode(png_data[i].bitmap_data->front(), 218 if (gfx::PNGCodec::Decode(png_data[i].bitmap_data->front(),
220 png_data[i].bitmap_data->size(), 219 png_data[i].bitmap_data->size(),
221 &bitmap)) { 220 &bitmap)) {
222 bitmaps.push_back(bitmap); 221 bitmaps.push_back(bitmap);
223 } 222 }
224 } 223 }
225 224
226 if (bitmaps.empty()) 225 if (bitmaps.empty())
227 return gfx::Image(); 226 return gfx::Image();
228 227
229 gfx::ImageSkia resized_image_skia; 228 gfx::ImageSkia resized_image_skia;
230 for (size_t i = 0; i < scale_factors_to_generate.size(); ++i) { 229 for (size_t i = 0; i < scale_factors_to_generate.size(); ++i) {
231 ui::ScaleFactor scale_factor = scale_factors_to_generate[i]; 230 ui::ScaleFactor scale_factor = scale_factors_to_generate[i];
232 int desired_size_in_pixel = 231 int desired_size_in_pixel =
233 ceil(favicon_size * ui::GetImageScale(scale_factor)); 232 ceil(favicon_size * ui::GetScaleFactorScale(scale_factor));
234 SkBitmap bitmap = ResizeBitmapByDownsamplingIfPossible( 233 SkBitmap bitmap = ResizeBitmapByDownsamplingIfPossible(
235 bitmaps, desired_size_in_pixel); 234 bitmaps, desired_size_in_pixel);
236 resized_image_skia.AddRepresentation( 235 resized_image_skia.AddRepresentation(
237 gfx::ImageSkiaRep(bitmap, scale_factor)); 236 gfx::ImageSkiaRep(bitmap, scale_factor));
238 } 237 }
239 238
240 if (png_reps.empty()) 239 if (png_reps.empty())
241 return gfx::Image(resized_image_skia); 240 return gfx::Image(resized_image_skia);
242 241
243 std::vector<gfx::ImageSkiaRep> resized_image_skia_reps = 242 std::vector<gfx::ImageSkiaRep> resized_image_skia_reps =
244 resized_image_skia.image_reps(); 243 resized_image_skia.image_reps();
245 for (size_t i = 0; i < resized_image_skia_reps.size(); ++i) { 244 for (size_t i = 0; i < resized_image_skia_reps.size(); ++i) {
246 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); 245 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes());
247 if (gfx::PNGCodec::EncodeBGRASkBitmap( 246 if (gfx::PNGCodec::EncodeBGRASkBitmap(
248 resized_image_skia_reps[i].sk_bitmap(), false, &png_bytes->data())) { 247 resized_image_skia_reps[i].sk_bitmap(), false, &png_bytes->data())) {
249 png_reps.push_back(gfx::ImagePNGRep(png_bytes, 248 png_reps.push_back(gfx::ImagePNGRep(png_bytes,
250 resized_image_skia_reps[i].scale())); 249 resized_image_skia_reps[i].scale_factor()));
251 } 250 }
252 } 251 }
253 252
254 return gfx::Image(png_reps); 253 return gfx::Image(png_reps);
255 } 254 }
OLDNEW
« no previous file with comments | « trunk/src/chrome/browser/favicon/favicon_service.cc ('k') | trunk/src/chrome/browser/history/select_favicon_frames.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698