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 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 // Checks if the current thread can safely modify the storage. | 95 // Checks if the current thread can safely modify the storage. |
| 96 bool CanModify() const { | 96 bool CanModify() const { |
| 97 return !read_only_ && CalledOnValidThread(); | 97 return !read_only_ && CalledOnValidThread(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Checks if the current thread can safely read the storage. | 100 // Checks if the current thread can safely read the storage. |
| 101 bool CanRead() const { | 101 bool CanRead() const { |
| 102 return (read_only_ && !source_.get()) || CalledOnValidThread(); | 102 return (read_only_ && !source_.get()) || CalledOnValidThread(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Add a new representation. If the storage contains unscaled | |
| 106 // image rep, change it to scaled as this now contains | |
| 107 // multiple reps. | |
| 108 void AddRepresentation(const ImageSkiaRep& image) { | |
| 109 for (ImageSkia::ImageSkiaReps::iterator it = image_reps_.begin(); | |
| 110 it < image_reps_.end(); | |
| 111 ++it) { | |
| 112 if (it->unscaled()) { | |
| 113 it->SetScaled(); | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 image_reps_.push_back(image); | |
| 118 } | |
| 119 | |
| 105 // Returns the iterator of the image rep whose density best matches | 120 // 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 | 121 // |scale|. If the image for the |scale| doesn't exist in the storage and |
| 107 // |storage| is set, it fetches new image by calling | 122 // |storage| is set, it fetches new image by calling |
| 108 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with | 123 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with |
| 109 // different scale (if the image doesn't exist in resource, for example), it | 124 // different scale (if the image doesn't exist in resource, for example), it |
| 110 // will fallback to closest image rep. | 125 // will fallback to closest image rep. |
| 111 std::vector<ImageSkiaRep>::iterator FindRepresentation( | 126 std::vector<ImageSkiaRep>::iterator FindRepresentation( |
| 112 float scale, bool fetch_new_image) const { | 127 float scale, bool fetch_new_image) const { |
| 113 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); | 128 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); |
| 114 | 129 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 return *g_supported_scales; | 248 return *g_supported_scales; |
| 234 } | 249 } |
| 235 | 250 |
| 236 // static | 251 // static |
| 237 float ImageSkia::GetMaxSupportedScale() { | 252 float ImageSkia::GetMaxSupportedScale() { |
| 238 return g_supported_scales->back(); | 253 return g_supported_scales->back(); |
| 239 } | 254 } |
| 240 | 255 |
| 241 // static | 256 // static |
| 242 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { | 257 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { |
| 243 return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); | 258 return ImageSkia(ImageSkiaRep(bitmap, 0.0f)); |
|
Jun Mukai
2014/05/09 17:57:39
Doesn't this cause any other unexpected results?
T
oshima
2014/05/09 20:53:33
Yes, this is exactly what I wanted to guarantee ho
| |
| 244 } | 259 } |
| 245 | 260 |
| 246 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { | 261 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { |
| 247 ImageSkia* copy = new ImageSkia; | 262 ImageSkia* copy = new ImageSkia; |
| 248 if (isNull()) | 263 if (isNull()) |
| 249 return scoped_ptr<ImageSkia>(copy); | 264 return scoped_ptr<ImageSkia>(copy); |
| 250 | 265 |
| 251 CHECK(CanRead()); | 266 CHECK(CanRead()); |
| 252 | 267 |
| 253 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); | 268 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 271 | 286 |
| 272 // TODO(oshima): This method should be called |SetRepresentation| | 287 // TODO(oshima): This method should be called |SetRepresentation| |
| 273 // and replace the existing rep if there is already one with the | 288 // and replace the existing rep if there is already one with the |
| 274 // same scale so that we can guarantee that a ImageSkia instance contains only | 289 // same scale so that we can guarantee that a ImageSkia instance contains only |
| 275 // one image rep per scale. This is not possible now as ImageLoader currently | 290 // one image rep per scale. This is not possible now as ImageLoader currently |
| 276 // stores need this feature, but this needs to be fixed. | 291 // stores need this feature, but this needs to be fixed. |
| 277 if (isNull()) { | 292 if (isNull()) { |
| 278 Init(image_rep); | 293 Init(image_rep); |
| 279 } else { | 294 } else { |
| 280 CHECK(CanModify()); | 295 CHECK(CanModify()); |
| 281 storage_->image_reps().push_back(image_rep); | 296 storage_->AddRepresentation(image_rep); |
|
Jun Mukai
2014/05/09 17:57:39
Why only this has to be replaced by AddRepresentat
oshima
2014/05/09 20:53:33
I added the comment to explain why.
| |
| 282 } | 297 } |
| 283 } | 298 } |
| 284 | 299 |
| 285 void ImageSkia::RemoveRepresentation(float scale) { | 300 void ImageSkia::RemoveRepresentation(float scale) { |
| 286 if (isNull()) | 301 if (isNull()) |
| 287 return; | 302 return; |
| 288 CHECK(CanModify()); | 303 CHECK(CanModify()); |
| 289 | 304 |
| 290 ImageSkiaReps& image_reps = storage_->image_reps(); | 305 ImageSkiaReps& image_reps = storage_->image_reps(); |
| 291 ImageSkiaReps::iterator it = | 306 ImageSkiaReps::iterator it = |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 416 bool ImageSkia::CanModify() const { | 431 bool ImageSkia::CanModify() const { |
| 417 return !storage_.get() || storage_->CanModify(); | 432 return !storage_.get() || storage_->CanModify(); |
| 418 } | 433 } |
| 419 | 434 |
| 420 void ImageSkia::DetachStorageFromThread() { | 435 void ImageSkia::DetachStorageFromThread() { |
| 421 if (storage_.get()) | 436 if (storage_.get()) |
| 422 storage_->DetachFromThread(); | 437 storage_->DetachFromThread(); |
| 423 } | 438 } |
| 424 | 439 |
| 425 } // namespace gfx | 440 } // namespace gfx |
| OLD | NEW |