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. This checks if the scale of the added image |
| 106 // is not 1.0f, and mark the existing rep as scaled to make |
| 107 // the image high DPI aware. |
| 108 void AddRepresentation(const ImageSkiaRep& image) { |
| 109 if (image.scale() != 1.0f) { |
| 110 for (ImageSkia::ImageSkiaReps::iterator it = image_reps_.begin(); |
| 111 it < image_reps_.end(); |
| 112 ++it) { |
| 113 if (it->unscaled()) { |
| 114 DCHECK_EQ(1.0f, it->scale()); |
| 115 it->SetScaled(); |
| 116 break; |
| 117 } |
| 118 } |
| 119 } |
| 120 image_reps_.push_back(image); |
| 121 } |
| 122 |
105 // Returns the iterator of the image rep whose density best matches | 123 // 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 | 124 // |scale|. If the image for the |scale| doesn't exist in the storage and |
107 // |storage| is set, it fetches new image by calling | 125 // |storage| is set, it fetches new image by calling |
108 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with | 126 // |ImageSkiaSource::GetImageForScale|. If the source returns the image with |
109 // different scale (if the image doesn't exist in resource, for example), it | 127 // different scale (if the image doesn't exist in resource, for example), it |
110 // will fallback to closest image rep. | 128 // will fallback to closest image rep. |
111 std::vector<ImageSkiaRep>::iterator FindRepresentation( | 129 std::vector<ImageSkiaRep>::iterator FindRepresentation( |
112 float scale, bool fetch_new_image) const { | 130 float scale, bool fetch_new_image) const { |
113 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); | 131 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); |
114 | 132 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 return *g_supported_scales; | 251 return *g_supported_scales; |
234 } | 252 } |
235 | 253 |
236 // static | 254 // static |
237 float ImageSkia::GetMaxSupportedScale() { | 255 float ImageSkia::GetMaxSupportedScale() { |
238 return g_supported_scales->back(); | 256 return g_supported_scales->back(); |
239 } | 257 } |
240 | 258 |
241 // static | 259 // static |
242 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { | 260 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { |
243 return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); | 261 return ImageSkia(ImageSkiaRep(bitmap, 0.0f)); |
244 } | 262 } |
245 | 263 |
246 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { | 264 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { |
247 ImageSkia* copy = new ImageSkia; | 265 ImageSkia* copy = new ImageSkia; |
248 if (isNull()) | 266 if (isNull()) |
249 return scoped_ptr<ImageSkia>(copy); | 267 return scoped_ptr<ImageSkia>(copy); |
250 | 268 |
251 CHECK(CanRead()); | 269 CHECK(CanRead()); |
252 | 270 |
253 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); | 271 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps(); |
(...skipping 17 matching lines...) Expand all Loading... |
271 | 289 |
272 // TODO(oshima): This method should be called |SetRepresentation| | 290 // TODO(oshima): This method should be called |SetRepresentation| |
273 // and replace the existing rep if there is already one with the | 291 // 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 | 292 // 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 | 293 // one image rep per scale. This is not possible now as ImageLoader currently |
276 // stores need this feature, but this needs to be fixed. | 294 // stores need this feature, but this needs to be fixed. |
277 if (isNull()) { | 295 if (isNull()) { |
278 Init(image_rep); | 296 Init(image_rep); |
279 } else { | 297 } else { |
280 CHECK(CanModify()); | 298 CHECK(CanModify()); |
281 storage_->image_reps().push_back(image_rep); | 299 // If someone is adding ImageSkia explicitly, check if we should |
| 300 // make the image high DPI aware. |
| 301 storage_->AddRepresentation(image_rep); |
282 } | 302 } |
283 } | 303 } |
284 | 304 |
285 void ImageSkia::RemoveRepresentation(float scale) { | 305 void ImageSkia::RemoveRepresentation(float scale) { |
286 if (isNull()) | 306 if (isNull()) |
287 return; | 307 return; |
288 CHECK(CanModify()); | 308 CHECK(CanModify()); |
289 | 309 |
290 ImageSkiaReps& image_reps = storage_->image_reps(); | 310 ImageSkiaReps& image_reps = storage_->image_reps(); |
291 ImageSkiaReps::iterator it = | 311 ImageSkiaReps::iterator it = |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 bool ImageSkia::CanModify() const { | 436 bool ImageSkia::CanModify() const { |
417 return !storage_.get() || storage_->CanModify(); | 437 return !storage_.get() || storage_->CanModify(); |
418 } | 438 } |
419 | 439 |
420 void ImageSkia::DetachStorageFromThread() { | 440 void ImageSkia::DetachStorageFromThread() { |
421 if (storage_.get()) | 441 if (storage_.get()) |
422 storage_->DetachFromThread(); | 442 storage_->DetachFromThread(); |
423 } | 443 } |
424 | 444 |
425 } // namespace gfx | 445 } // namespace gfx |
OLD | NEW |