OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/manifest/manifest_icon_downloader.h" | 5 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 104 } |
105 | 105 |
106 const SkBitmap& chosen = bitmaps[closest_index]; | 106 const SkBitmap& chosen = bitmaps[closest_index]; |
107 | 107 |
108 // Only scale if we need to scale down. For scaling up we will let the system | 108 // Only scale if we need to scale down. For scaling up we will let the system |
109 // handle that when it is required to display it. This saves space in the | 109 // handle that when it is required to display it. This saves space in the |
110 // webapp storage system as well. | 110 // webapp storage system as well. |
111 if (chosen.height() > ideal_icon_size_in_px || | 111 if (chosen.height() > ideal_icon_size_in_px || |
112 chosen.width() > ideal_icon_size_in_px) { | 112 chosen.width() > ideal_icon_size_in_px) { |
113 content::BrowserThread::PostTask( | 113 content::BrowserThread::PostTask( |
114 content::BrowserThread::IO, | 114 content::BrowserThread::IO, FROM_HERE, |
115 FROM_HERE, | 115 base::BindOnce(&ManifestIconDownloader::ScaleIcon, |
116 base::Bind(&ManifestIconDownloader::ScaleIcon, | 116 ideal_icon_size_in_px, chosen, callback)); |
117 ideal_icon_size_in_px, | |
118 chosen, | |
119 callback)); | |
120 return; | 117 return; |
121 } | 118 } |
122 | 119 |
123 callback.Run(chosen); | 120 callback.Run(chosen); |
124 } | 121 } |
125 | 122 |
126 void ManifestIconDownloader::ScaleIcon( | 123 void ManifestIconDownloader::ScaleIcon( |
127 int ideal_icon_size_in_px, | 124 int ideal_icon_size_in_px, |
128 const SkBitmap& bitmap, | 125 const SkBitmap& bitmap, |
129 const ManifestIconDownloader::IconFetchCallback& callback) { | 126 const ManifestIconDownloader::IconFetchCallback& callback) { |
130 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
131 | 128 |
132 const SkBitmap& scaled = skia::ImageOperations::Resize( | 129 const SkBitmap& scaled = skia::ImageOperations::Resize( |
133 bitmap, | 130 bitmap, |
134 skia::ImageOperations::RESIZE_BEST, | 131 skia::ImageOperations::RESIZE_BEST, |
135 ideal_icon_size_in_px, | 132 ideal_icon_size_in_px, |
136 ideal_icon_size_in_px); | 133 ideal_icon_size_in_px); |
137 | 134 |
138 content::BrowserThread::PostTask( | 135 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
139 content::BrowserThread::UI, | 136 base::BindOnce(callback, scaled)); |
140 FROM_HERE, | |
141 base::Bind(callback, scaled)); | |
142 } | 137 } |
143 | 138 |
144 int ManifestIconDownloader::FindClosestBitmapIndex( | 139 int ManifestIconDownloader::FindClosestBitmapIndex( |
145 int ideal_icon_size_in_px, | 140 int ideal_icon_size_in_px, |
146 int minimum_icon_size_in_px, | 141 int minimum_icon_size_in_px, |
147 const std::vector<SkBitmap>& bitmaps) { | 142 const std::vector<SkBitmap>& bitmaps) { |
148 int best_index = -1; | 143 int best_index = -1; |
149 int best_delta = std::numeric_limits<int>::min(); | 144 int best_delta = std::numeric_limits<int>::min(); |
150 const int max_negative_delta = | 145 const int max_negative_delta = |
151 minimum_icon_size_in_px - ideal_icon_size_in_px; | 146 minimum_icon_size_in_px - ideal_icon_size_in_px; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 float ratio = height / width; | 180 float ratio = height / width; |
186 float ratio_difference = fabs(ratio - 1); | 181 float ratio_difference = fabs(ratio - 1); |
187 if (ratio_difference < best_ratio_difference) { | 182 if (ratio_difference < best_ratio_difference) { |
188 best_index = i; | 183 best_index = i; |
189 best_ratio_difference = ratio_difference; | 184 best_ratio_difference = ratio_difference; |
190 } | 185 } |
191 } | 186 } |
192 | 187 |
193 return best_index; | 188 return best_index; |
194 } | 189 } |
OLD | NEW |