| OLD | NEW |
| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 = |
| 170 non_const->FindRepresentation(resource_scale, fetch_new_image); |
| 171 |
| 172 DCHECK(iter != non_const->image_reps().end()); |
| 173 SkBitmap scaled_image; |
| 174 gfx::Size unscaled_size(iter->pixel_width(), iter->pixel_height()); |
| 175 gfx::Size scaled_size = ToCeiledSize( |
| 176 gfx::ScaleSize(unscaled_size, scale / iter->scale())); |
| 177 |
| 178 image = ImageSkiaRep(skia::ImageOperations::Resize( |
| 179 iter->sk_bitmap(), |
| 180 skia::ImageOperations::RESIZE_LANCZOS3, |
| 181 scaled_size.width(), |
| 182 scaled_size.height()), scale); |
| 183 DCHECK_EQ(image.pixel_width(), scaled_size.width()); |
| 184 DCHECK_EQ(image.pixel_height(), scaled_size.height()); |
| 185 } else { |
| 186 image = source_->GetImageForScale(scale); |
| 187 } |
| 143 | 188 |
| 144 // If the source returned the new image, store it. | 189 // If the source returned the new image, store it. |
| 145 if (!image.is_null() && | 190 if (!image.is_null() && |
| 146 std::find_if(image_reps_.begin(), image_reps_.end(), | 191 std::find_if(image_reps_.begin(), image_reps_.end(), |
| 147 Matcher(image.scale())) == image_reps_.end()) { | 192 Matcher(image.scale())) == image_reps_.end()) { |
| 148 non_const->image_reps().push_back(image); | 193 non_const->image_reps().push_back(image); |
| 149 } | 194 } |
| 150 | 195 |
| 151 // If the result image's scale isn't same as the expected scale, create | 196 // 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 | 197 // null ImageSkiaRep with the |scale| so that the next lookup will |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 // static | 281 // static |
| 237 float ImageSkia::GetMaxSupportedScale() { | 282 float ImageSkia::GetMaxSupportedScale() { |
| 238 return g_supported_scales->back(); | 283 return g_supported_scales->back(); |
| 239 } | 284 } |
| 240 | 285 |
| 241 // static | 286 // static |
| 242 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { | 287 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { |
| 243 return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); | 288 return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); |
| 244 } | 289 } |
| 245 | 290 |
| 291 bool ImageSkia::IsDSFScalingInImageSkiaEnabled() { |
| 292 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 293 #if defined(OS_WIN) |
| 294 return !command_line->HasSwitch( |
| 295 switches::kDisallowArbitraryScaleFactorInImageSkia); |
| 296 #else |
| 297 return command_line->HasSwitch( |
| 298 switches::kAllowArbitraryScaleFactorInImageSkia); |
| 299 #endif |
| 300 } |
| 301 |
| 246 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { | 302 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { |
| 247 ImageSkia* copy = new ImageSkia; | 303 ImageSkia* copy = new ImageSkia; |
| 248 if (isNull()) | 304 if (isNull()) |
| 249 return scoped_ptr<ImageSkia>(copy); | 305 return scoped_ptr<ImageSkia>(copy); |
| 250 | 306 |
| 251 CHECK(CanRead()); | 307 CHECK(CanRead()); |
| 252 | 308 |
| 253 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); | 309 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); |
| 254 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); | 310 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); |
| 255 iter != reps.end(); ++iter) { | 311 iter != reps.end(); ++iter) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 bool ImageSkia::CanModify() const { | 472 bool ImageSkia::CanModify() const { |
| 417 return !storage_.get() || storage_->CanModify(); | 473 return !storage_.get() || storage_->CanModify(); |
| 418 } | 474 } |
| 419 | 475 |
| 420 void ImageSkia::DetachStorageFromThread() { | 476 void ImageSkia::DetachStorageFromThread() { |
| 421 if (storage_.get()) | 477 if (storage_.get()) |
| 422 storage_->DetachFromThread(); | 478 storage_->DetachFromThread(); |
| 423 } | 479 } |
| 424 | 480 |
| 425 } // namespace gfx | 481 } // namespace gfx |
| OLD | NEW |