Chromium Code Reviews| 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 | |
| 30 } // namespace | 34 } // namespace |
| 31 | 35 |
| 32 namespace internal { | 36 namespace internal { |
| 33 namespace { | 37 namespace { |
| 34 | 38 |
| 35 class Matcher { | 39 class Matcher { |
| 36 public: | 40 public: |
| 37 explicit Matcher(float scale) : scale_(scale) { | 41 explicit Matcher(float scale) : scale_(scale) { |
| 38 } | 42 } |
| 39 | 43 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 if (diff < smallest_diff && !it->is_null()) { | 136 if (diff < smallest_diff && !it->is_null()) { |
| 133 closest_iter = it; | 137 closest_iter = it; |
| 134 smallest_diff = diff; | 138 smallest_diff = diff; |
| 135 } | 139 } |
| 136 } | 140 } |
| 137 | 141 |
| 138 if (fetch_new_image && source_.get()) { | 142 if (fetch_new_image && source_.get()) { |
| 139 DCHECK(CalledOnValidThread()) << | 143 DCHECK(CalledOnValidThread()) << |
| 140 "An ImageSkia with the source must be accessed by the same thread."; | 144 "An ImageSkia with the source must be accessed by the same thread."; |
| 141 | 145 |
| 142 ImageSkiaRep image = source_->GetImageForScale(scale); | 146 ImageSkiaRep image; |
| 147 float request_scale = scale; | |
|
oshima
2014/05/06 20:59:49
can you use "resource_scale" ?
Jun Mukai
2014/05/06 21:06:27
Done.
| |
| 148 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() && g_supported_scales) { | |
| 149 if (g_supported_scales->back() <= scale) { | |
| 150 request_scale = g_supported_scales->back(); | |
| 151 } else { | |
| 152 for (size_t i = 0; i < g_supported_scales->size(); ++i) { | |
| 153 if ((*g_supported_scales)[i] >= request_scale) { | |
| 154 request_scale = (*g_supported_scales)[i]; | |
|
oshima
2014/05/06 20:59:49
can you make it so that this picks 2.0 for 1.25 an
Jun Mukai
2014/05/06 21:06:27
I think this does so already, this for-loop attemp
Jun Mukai
2014/05/07 00:40:17
Fixed the code so that 1.25 falls back to 1.0. Add
| |
| 155 break; | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() && | |
| 161 scale != request_scale) { | |
| 162 std::vector<ImageSkiaRep>::iterator iter = | |
| 163 non_const->FindRepresentation(request_scale, fetch_new_image); | |
| 164 | |
| 165 DCHECK(iter != non_const->image_reps().end()); | |
| 166 SkBitmap scaled_image; | |
| 167 gfx::Size unscaled_size(iter->pixel_width(), iter->pixel_height()); | |
| 168 gfx::Size scaled_size = ToCeiledSize( | |
| 169 gfx::ScaleSize(unscaled_size, scale / iter->scale())); | |
| 170 | |
| 171 image = ImageSkiaRep(skia::ImageOperations::Resize( | |
| 172 iter->sk_bitmap(), | |
| 173 skia::ImageOperations::RESIZE_LANCZOS3, | |
| 174 scaled_size.width(), | |
| 175 scaled_size.height()), scale); | |
| 176 DCHECK_EQ(image.pixel_width(), scaled_size.width()); | |
| 177 DCHECK_EQ(image.pixel_height(), scaled_size.height()); | |
| 178 } else { | |
| 179 image = source_->GetImageForScale(scale); | |
| 180 } | |
| 143 | 181 |
| 144 // If the source returned the new image, store it. | 182 // If the source returned the new image, store it. |
| 145 if (!image.is_null() && | 183 if (!image.is_null() && |
| 146 std::find_if(image_reps_.begin(), image_reps_.end(), | 184 std::find_if(image_reps_.begin(), image_reps_.end(), |
| 147 Matcher(image.scale())) == image_reps_.end()) { | 185 Matcher(image.scale())) == image_reps_.end()) { |
| 148 non_const->image_reps().push_back(image); | 186 non_const->image_reps().push_back(image); |
| 149 } | 187 } |
| 150 | 188 |
| 151 // If the result image's scale isn't same as the expected scale, create | 189 // 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 | 190 // 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 | 274 // static |
| 237 float ImageSkia::GetMaxSupportedScale() { | 275 float ImageSkia::GetMaxSupportedScale() { |
| 238 return g_supported_scales->back(); | 276 return g_supported_scales->back(); |
| 239 } | 277 } |
| 240 | 278 |
| 241 // static | 279 // static |
| 242 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { | 280 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { |
| 243 return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); | 281 return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); |
| 244 } | 282 } |
| 245 | 283 |
| 284 bool ImageSkia::IsDSFScalingInImageSkiaEnabled() { | |
| 285 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 286 #if defined(OS_WIN) | |
| 287 return !command_line->HasSwitch( | |
| 288 switches::kDisallowArbitraryScaleFactorInImageSkia); | |
| 289 #else | |
| 290 return command_line->HasSwitch( | |
| 291 switches::kAllowArbitraryScaleFactorInImageSkia); | |
| 292 #endif | |
| 293 } | |
| 294 | |
| 246 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { | 295 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { |
| 247 ImageSkia* copy = new ImageSkia; | 296 ImageSkia* copy = new ImageSkia; |
| 248 if (isNull()) | 297 if (isNull()) |
| 249 return scoped_ptr<ImageSkia>(copy); | 298 return scoped_ptr<ImageSkia>(copy); |
| 250 | 299 |
| 251 CHECK(CanRead()); | 300 CHECK(CanRead()); |
| 252 | 301 |
| 253 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); | 302 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); |
| 254 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); | 303 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); |
| 255 iter != reps.end(); ++iter) { | 304 iter != reps.end(); ++iter) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 416 bool ImageSkia::CanModify() const { | 465 bool ImageSkia::CanModify() const { |
| 417 return !storage_.get() || storage_->CanModify(); | 466 return !storage_.get() || storage_->CanModify(); |
| 418 } | 467 } |
| 419 | 468 |
| 420 void ImageSkia::DetachStorageFromThread() { | 469 void ImageSkia::DetachStorageFromThread() { |
| 421 if (storage_.get()) | 470 if (storage_.get()) |
| 422 storage_->DetachFromThread(); | 471 storage_->DetachFromThread(); |
| 423 } | 472 } |
| 424 | 473 |
| 425 } // namespace gfx | 474 } // namespace gfx |
| OLD | NEW |