| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 smallest_diff = diff; | 212 smallest_diff = diff; |
| 213 } | 213 } |
| 214 } | 214 } |
| 215 | 215 |
| 216 if (fetch_new_image && source_.get()) { | 216 if (fetch_new_image && source_.get()) { |
| 217 DCHECK(CalledOnValidThread()) | 217 DCHECK(CalledOnValidThread()) |
| 218 << "An ImageSkia with the source must be accessed by the same thread."; | 218 << "An ImageSkia with the source must be accessed by the same thread."; |
| 219 | 219 |
| 220 ImageSkiaRep image; | 220 ImageSkiaRep image; |
| 221 float resource_scale = scale; | 221 float resource_scale = scale; |
| 222 if (!HasRepresentationAtAllScales() && g_supported_scales) { | 222 if (!HasRepresentationAtAllScales() && g_supported_scales) |
| 223 if (g_supported_scales->back() <= scale) { | 223 resource_scale = ImageSkia::MapToSupportedScale(scale); |
| 224 resource_scale = g_supported_scales->back(); | |
| 225 } else { | |
| 226 for (size_t i = 0; i < g_supported_scales->size(); ++i) { | |
| 227 if ((*g_supported_scales)[i] + kFallbackToSmallerScaleDiff >= scale) { | |
| 228 resource_scale = (*g_supported_scales)[i]; | |
| 229 break; | |
| 230 } | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 if (scale != resource_scale) { | 224 if (scale != resource_scale) { |
| 235 std::vector<ImageSkiaRep>::iterator iter = | 225 std::vector<ImageSkiaRep>::iterator iter = |
| 236 FindRepresentation(resource_scale, fetch_new_image); | 226 FindRepresentation(resource_scale, fetch_new_image); |
| 237 CHECK(iter != image_reps_.end()); | 227 CHECK(iter != image_reps_.end()); |
| 238 image = iter->unscaled() ? (*iter) : ScaleImageSkiaRep(*iter, scale); | 228 image = iter->unscaled() ? (*iter) : ScaleImageSkiaRep(*iter, scale); |
| 239 } else { | 229 } else { |
| 240 image = source_->GetImageForScale(scale); | 230 image = source_->GetImageForScale(scale); |
| 241 // Image may be missing for the specified scale in some cases, such like | 231 // Image may be missing for the specified scale in some cases, such like |
| 242 // looking up 2x resources but the 2x resource pack is missing. Fall back | 232 // looking up 2x resources but the 2x resource pack is missing. Fall back |
| 243 // to 1x and re-scale it. | 233 // to 1x and re-scale it. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 DCHECK(g_supported_scales != NULL); | 314 DCHECK(g_supported_scales != NULL); |
| 325 return *g_supported_scales; | 315 return *g_supported_scales; |
| 326 } | 316 } |
| 327 | 317 |
| 328 // static | 318 // static |
| 329 float ImageSkia::GetMaxSupportedScale() { | 319 float ImageSkia::GetMaxSupportedScale() { |
| 330 return g_supported_scales->back(); | 320 return g_supported_scales->back(); |
| 331 } | 321 } |
| 332 | 322 |
| 333 // static | 323 // static |
| 324 float ImageSkia::MapToSupportedScale(float scale) { |
| 325 for (float supported_scale : *g_supported_scales) { |
| 326 if (supported_scale + kFallbackToSmallerScaleDiff >= scale) |
| 327 return supported_scale; |
| 328 } |
| 329 return g_supported_scales->back(); |
| 330 } |
| 331 |
| 332 // static |
| 334 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { | 333 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { |
| 335 return ImageSkia(ImageSkiaRep(bitmap, 0.0f)); | 334 return ImageSkia(ImageSkiaRep(bitmap, 0.0f)); |
| 336 } | 335 } |
| 337 | 336 |
| 338 std::unique_ptr<ImageSkia> ImageSkia::DeepCopy() const { | 337 std::unique_ptr<ImageSkia> ImageSkia::DeepCopy() const { |
| 339 ImageSkia* copy = new ImageSkia; | 338 ImageSkia* copy = new ImageSkia; |
| 340 if (isNull()) | 339 if (isNull()) |
| 341 return base::WrapUnique(copy); | 340 return base::WrapUnique(copy); |
| 342 | 341 |
| 343 CHECK(CanRead()); | 342 CHECK(CanRead()); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 bool ImageSkia::CanModify() const { | 517 bool ImageSkia::CanModify() const { |
| 519 return !storage_.get() || storage_->CanModify(); | 518 return !storage_.get() || storage_->CanModify(); |
| 520 } | 519 } |
| 521 | 520 |
| 522 void ImageSkia::DetachStorageFromThread() { | 521 void ImageSkia::DetachStorageFromThread() { |
| 523 if (storage_.get()) | 522 if (storage_.get()) |
| 524 storage_->DetachFromThread(); | 523 storage_->DetachFromThread(); |
| 525 } | 524 } |
| 526 | 525 |
| 527 } // namespace gfx | 526 } // namespace gfx |
| OLD | NEW |