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

Side by Side Diff: ui/gfx/image/image_skia.cc

Issue 263253003: Allows arbitrary scale factor in ImageSkia. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 6 years, 7 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
« no previous file with comments | « ui/gfx/image/image_skia.h ('k') | ui/gfx/image/image_skia_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "ui/gfx/image/image_skia.h" 5 #include "ui/gfx/image/image_skia.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 10
11 #include "base/command_line.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/non_thread_safe.h" 14 #include "base/threading/non_thread_safe.h"
15 #include "ui/gfx/geometry/size_conversions.h"
14 #include "ui/gfx/image/image_skia_operations.h" 16 #include "ui/gfx/image/image_skia_operations.h"
15 #include "ui/gfx/image/image_skia_source.h" 17 #include "ui/gfx/image/image_skia_source.h"
16 #include "ui/gfx/rect.h" 18 #include "ui/gfx/rect.h"
17 #include "ui/gfx/size.h" 19 #include "ui/gfx/size.h"
18 #include "ui/gfx/skia_util.h" 20 #include "ui/gfx/skia_util.h"
21 #include "ui/gfx/switches.h"
19 22
20 namespace gfx { 23 namespace gfx {
21 namespace { 24 namespace {
22 25
23 // static 26 // static
24 gfx::ImageSkiaRep& NullImageRep() { 27 gfx::ImageSkiaRep& NullImageRep() {
25 CR_DEFINE_STATIC_LOCAL(ImageSkiaRep, null_image_rep, ()); 28 CR_DEFINE_STATIC_LOCAL(ImageSkiaRep, null_image_rep, ());
26 return null_image_rep; 29 return null_image_rep;
27 } 30 }
28 31
29 std::vector<float>* g_supported_scales = NULL; 32 std::vector<float>* g_supported_scales = NULL;
33
34 // The difference to fall back to the smaller scale factor rather than the
35 // larger one. For example, assume 1.25 is requested but only 1.0 and 2.0 are
36 // supported. In that case, not fall back to 2.0 but 1.0, and then expand
37 // the image to 1.25.
38 const float kFallbackToSmallerScaleDiff = 0.25f;
39
30 } // namespace 40 } // namespace
31 41
32 namespace internal { 42 namespace internal {
33 namespace { 43 namespace {
34 44
35 class Matcher { 45 class Matcher {
36 public: 46 public:
37 explicit Matcher(float scale) : scale_(scale) { 47 explicit Matcher(float scale) : scale_(scale) {
38 } 48 }
39 49
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 break; 126 break;
117 } 127 }
118 } 128 }
119 } 129 }
120 image_reps_.push_back(image); 130 image_reps_.push_back(image);
121 } 131 }
122 132
123 // Returns the iterator of the image rep whose density best matches 133 // Returns the iterator of the image rep whose density best matches
124 // |scale|. If the image for the |scale| doesn't exist in the storage and 134 // |scale|. If the image for the |scale| doesn't exist in the storage and
125 // |storage| is set, it fetches new image by calling 135 // |storage| is set, it fetches new image by calling
126 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with 136 // |ImageSkiaSource::GetImageForScale|. There are two modes to deal with
127 // different scale (if the image doesn't exist in resource, for example), it 137 // arbitrary scale factors.
128 // will fallback to closest image rep. 138 // 1: Invoke GetImageForScale with requested scale and if the source
139 // returns the image with different scale (if the image doesn't exist in
140 // resource, for example), it will fallback to closest image rep.
141 // 2: Invoke GetImageForScale with the closest known scale to the requested
142 // one and rescale the image.
143 // Right now only Windows uses 2 and other platforms use 1 by default.
144 // TODO(mukai, oshima): abandon 1 code path and use 2 for every platforms.
129 std::vector<ImageSkiaRep>::iterator FindRepresentation( 145 std::vector<ImageSkiaRep>::iterator FindRepresentation(
130 float scale, bool fetch_new_image) const { 146 float scale, bool fetch_new_image) const {
131 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); 147 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this);
132 148
133 ImageSkia::ImageSkiaReps::iterator closest_iter = 149 ImageSkia::ImageSkiaReps::iterator closest_iter =
134 non_const->image_reps().end(); 150 non_const->image_reps().end();
135 ImageSkia::ImageSkiaReps::iterator exact_iter = 151 ImageSkia::ImageSkiaReps::iterator exact_iter =
136 non_const->image_reps().end(); 152 non_const->image_reps().end();
137 float smallest_diff = std::numeric_limits<float>::max(); 153 float smallest_diff = std::numeric_limits<float>::max();
138 for (ImageSkia::ImageSkiaReps::iterator it = 154 for (ImageSkia::ImageSkiaReps::iterator it =
(...skipping 11 matching lines...) Expand all
150 if (diff < smallest_diff && !it->is_null()) { 166 if (diff < smallest_diff && !it->is_null()) {
151 closest_iter = it; 167 closest_iter = it;
152 smallest_diff = diff; 168 smallest_diff = diff;
153 } 169 }
154 } 170 }
155 171
156 if (fetch_new_image && source_.get()) { 172 if (fetch_new_image && source_.get()) {
157 DCHECK(CalledOnValidThread()) << 173 DCHECK(CalledOnValidThread()) <<
158 "An ImageSkia with the source must be accessed by the same thread."; 174 "An ImageSkia with the source must be accessed by the same thread.";
159 175
160 ImageSkiaRep image = source_->GetImageForScale(scale); 176 ImageSkiaRep image;
177 float resource_scale = scale;
178 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() && g_supported_scales) {
179 if (g_supported_scales->back() <= scale) {
180 resource_scale = g_supported_scales->back();
181 } else {
182 for (size_t i = 0; i < g_supported_scales->size(); ++i) {
183 if ((*g_supported_scales)[i] + kFallbackToSmallerScaleDiff >=
184 scale) {
185 resource_scale = (*g_supported_scales)[i];
186 break;
187 }
188 }
189 }
190 }
191 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() &&
192 scale != resource_scale) {
193 std::vector<ImageSkiaRep>::iterator iter = FindRepresentation(
194 resource_scale, fetch_new_image);
195
196 DCHECK(iter != image_reps_.end());
197
198 if (!iter->unscaled()) {
199 SkBitmap scaled_image;
200 gfx::Size unscaled_size(iter->pixel_width(), iter->pixel_height());
201 gfx::Size scaled_size = ToCeiledSize(
202 gfx::ScaleSize(unscaled_size, scale / iter->scale()));
203
204 image = ImageSkiaRep(skia::ImageOperations::Resize(
205 iter->sk_bitmap(),
206 skia::ImageOperations::RESIZE_LANCZOS3,
207 scaled_size.width(),
208 scaled_size.height()), scale);
209 DCHECK_EQ(image.pixel_width(), scaled_size.width());
210 DCHECK_EQ(image.pixel_height(), scaled_size.height());
211 } else {
212 image = *iter;
213 }
214 } else {
215 image = source_->GetImageForScale(scale);
216 }
161 217
162 // If the source returned the new image, store it. 218 // If the source returned the new image, store it.
163 if (!image.is_null() && 219 if (!image.is_null() &&
164 std::find_if(image_reps_.begin(), image_reps_.end(), 220 std::find_if(image_reps_.begin(), image_reps_.end(),
165 Matcher(image.scale())) == image_reps_.end()) { 221 Matcher(image.scale())) == image_reps_.end()) {
166 non_const->image_reps().push_back(image); 222 non_const->image_reps().push_back(image);
167 } 223 }
168 224
169 // If the result image's scale isn't same as the expected scale, create 225 // If the result image's scale isn't same as the expected scale, create
170 // null ImageSkiaRep with the |scale| so that the next lookup will 226 // null ImageSkiaRep with the |scale| so that the next lookup will
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // static 310 // static
255 float ImageSkia::GetMaxSupportedScale() { 311 float ImageSkia::GetMaxSupportedScale() {
256 return g_supported_scales->back(); 312 return g_supported_scales->back();
257 } 313 }
258 314
259 // static 315 // static
260 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { 316 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) {
261 return ImageSkia(ImageSkiaRep(bitmap, 0.0f)); 317 return ImageSkia(ImageSkiaRep(bitmap, 0.0f));
262 } 318 }
263 319
320 bool ImageSkia::IsDSFScalingInImageSkiaEnabled() {
321 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
322 #if defined(OS_WIN)
323 return !command_line->HasSwitch(
324 switches::kDisallowArbitraryScaleFactorInImageSkia);
325 #else
326 return command_line->HasSwitch(
327 switches::kAllowArbitraryScaleFactorInImageSkia);
328 #endif
329 }
330
264 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { 331 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const {
265 ImageSkia* copy = new ImageSkia; 332 ImageSkia* copy = new ImageSkia;
266 if (isNull()) 333 if (isNull())
267 return scoped_ptr<ImageSkia>(copy); 334 return scoped_ptr<ImageSkia>(copy);
268 335
269 CHECK(CanRead()); 336 CHECK(CanRead());
270 337
271 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); 338 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps();
272 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); 339 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin();
273 iter != reps.end(); ++iter) { 340 iter != reps.end(); ++iter) {
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 bool ImageSkia::CanModify() const { 503 bool ImageSkia::CanModify() const {
437 return !storage_.get() || storage_->CanModify(); 504 return !storage_.get() || storage_->CanModify();
438 } 505 }
439 506
440 void ImageSkia::DetachStorageFromThread() { 507 void ImageSkia::DetachStorageFromThread() {
441 if (storage_.get()) 508 if (storage_.get())
442 storage_->DetachFromThread(); 509 storage_->DetachFromThread();
443 } 510 }
444 511
445 } // namespace gfx 512 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/image/image_skia.h ('k') | ui/gfx/image/image_skia_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698