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

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: reset upstream 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 // Checks if the current thread can safely read the storage. 110 // Checks if the current thread can safely read the storage.
101 bool CanRead() const { 111 bool CanRead() const {
102 return (read_only_ && !source_.get()) || CalledOnValidThread(); 112 return (read_only_ && !source_.get()) || CalledOnValidThread();
103 } 113 }
104 114
105 // Returns the iterator of the image rep whose density best matches 115 // Returns the iterator of the image rep whose density best matches
106 // |scale|. If the image for the |scale| doesn't exist in the storage and 116 // |scale|. If the image for the |scale| doesn't exist in the storage and
107 // |storage| is set, it fetches new image by calling 117 // |storage| is set, it fetches new image by calling
108 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with 118 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with
109 // different scale (if the image doesn't exist in resource, for example), it 119 // different scale (if the image doesn't exist in resource, for example), it
110 // will fallback to closest image rep. 120 // will fallback to closest image rep.
oshima 2014/05/09 16:10:30 can you add description for new mode and TODO to c
Jun Mukai 2014/05/12 05:23:24 Done.
111 std::vector<ImageSkiaRep>::iterator FindRepresentation( 121 std::vector<ImageSkiaRep>::iterator FindRepresentation(
112 float scale, bool fetch_new_image) const { 122 float scale, bool fetch_new_image) const {
113 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); 123 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this);
114 124
115 ImageSkia::ImageSkiaReps::iterator closest_iter = 125 ImageSkia::ImageSkiaReps::iterator closest_iter =
116 non_const->image_reps().end(); 126 non_const->image_reps().end();
117 ImageSkia::ImageSkiaReps::iterator exact_iter = 127 ImageSkia::ImageSkiaReps::iterator exact_iter =
118 non_const->image_reps().end(); 128 non_const->image_reps().end();
119 float smallest_diff = std::numeric_limits<float>::max(); 129 float smallest_diff = std::numeric_limits<float>::max();
120 for (ImageSkia::ImageSkiaReps::iterator it = 130 for (ImageSkia::ImageSkiaReps::iterator it =
(...skipping 11 matching lines...) Expand all
132 if (diff < smallest_diff && !it->is_null()) { 142 if (diff < smallest_diff && !it->is_null()) {
133 closest_iter = it; 143 closest_iter = it;
134 smallest_diff = diff; 144 smallest_diff = diff;
135 } 145 }
136 } 146 }
137 147
138 if (fetch_new_image && source_.get()) { 148 if (fetch_new_image && source_.get()) {
139 DCHECK(CalledOnValidThread()) << 149 DCHECK(CalledOnValidThread()) <<
140 "An ImageSkia with the source must be accessed by the same thread."; 150 "An ImageSkia with the source must be accessed by the same thread.";
141 151
142 ImageSkiaRep image = source_->GetImageForScale(scale); 152 ImageSkiaRep image;
153 float resource_scale = scale;
154 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() && g_supported_scales) {
155 if (g_supported_scales->back() <= scale) {
156 resource_scale = g_supported_scales->back();
157 } else {
158 for (size_t i = 0; i < g_supported_scales->size(); ++i) {
159 if ((*g_supported_scales)[i] + kFallbackToSmallerScaleDiff >=
160 resource_scale) {
161 resource_scale = (*g_supported_scales)[i];
162 break;
163 }
164 }
165 }
166 }
167 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() &&
168 scale != resource_scale) {
169 std::vector<ImageSkiaRep>::iterator iter = FindRepresentation(
170 resource_scale, fetch_new_image);
171
172 DCHECK(iter != image_reps_.end());
173
174 if (!iter->unscaled()) {
175 SkBitmap scaled_image;
176 gfx::Size unscaled_size(iter->pixel_width(), iter->pixel_height());
177 gfx::Size scaled_size = ToCeiledSize(
178 gfx::ScaleSize(unscaled_size, scale / iter->scale()));
179
180 image = ImageSkiaRep(skia::ImageOperations::Resize(
181 iter->sk_bitmap(),
182 skia::ImageOperations::RESIZE_LANCZOS3,
183 scaled_size.width(),
184 scaled_size.height()), scale);
185 DCHECK_EQ(image.pixel_width(), scaled_size.width());
186 DCHECK_EQ(image.pixel_height(), scaled_size.height());
187 }
oshima 2014/05/09 16:10:30 This currently works without the following because
Jun Mukai 2014/05/12 05:23:24 Done.
188 } else {
189 image = source_->GetImageForScale(scale);
190 }
143 191
144 // If the source returned the new image, store it. 192 // If the source returned the new image, store it.
145 if (!image.is_null() && 193 if (!image.is_null() &&
146 std::find_if(image_reps_.begin(), image_reps_.end(), 194 std::find_if(image_reps_.begin(), image_reps_.end(),
147 Matcher(image.scale())) == image_reps_.end()) { 195 Matcher(image.scale())) == image_reps_.end()) {
148 non_const->image_reps().push_back(image); 196 non_const->image_reps().push_back(image);
149 } 197 }
150 198
151 // If the result image's scale isn't same as the expected scale, create 199 // If the result image's scale isn't same as the expected scale, create
152 // null ImageSkiaRep with the |scale| so that the next lookup will 200 // null ImageSkiaRep with the |scale| so that the next lookup will
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 // static 284 // static
237 float ImageSkia::GetMaxSupportedScale() { 285 float ImageSkia::GetMaxSupportedScale() {
238 return g_supported_scales->back(); 286 return g_supported_scales->back();
239 } 287 }
240 288
241 // static 289 // static
242 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { 290 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) {
243 return ImageSkia(ImageSkiaRep(bitmap, 0.0f)); 291 return ImageSkia(ImageSkiaRep(bitmap, 0.0f));
244 } 292 }
245 293
294 bool ImageSkia::IsDSFScalingInImageSkiaEnabled() {
295 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
296 #if defined(OS_WIN)
297 return !command_line->HasSwitch(
298 switches::kDisallowArbitraryScaleFactorInImageSkia);
299 #else
300 return command_line->HasSwitch(
301 switches::kAllowArbitraryScaleFactorInImageSkia);
302 #endif
303 }
304
246 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { 305 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const {
247 ImageSkia* copy = new ImageSkia; 306 ImageSkia* copy = new ImageSkia;
248 if (isNull()) 307 if (isNull())
249 return scoped_ptr<ImageSkia>(copy); 308 return scoped_ptr<ImageSkia>(copy);
250 309
251 CHECK(CanRead()); 310 CHECK(CanRead());
252 311
253 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); 312 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps();
254 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); 313 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin();
255 iter != reps.end(); ++iter) { 314 iter != reps.end(); ++iter) {
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 bool ImageSkia::CanModify() const { 475 bool ImageSkia::CanModify() const {
417 return !storage_.get() || storage_->CanModify(); 476 return !storage_.get() || storage_->CanModify();
418 } 477 }
419 478
420 void ImageSkia::DetachStorageFromThread() { 479 void ImageSkia::DetachStorageFromThread() {
421 if (storage_.get()) 480 if (storage_.get())
422 storage_->DetachFromThread(); 481 storage_->DetachFromThread();
423 } 482 }
424 483
425 } // namespace gfx 484 } // 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