| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a | 46 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a |
| 47 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's | 47 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's |
| 48 // information. | 48 // information. |
| 49 class ImageSkiaStorage : public base::RefCounted<ImageSkiaStorage> { | 49 class ImageSkiaStorage : public base::RefCounted<ImageSkiaStorage> { |
| 50 public: | 50 public: |
| 51 ImageSkiaStorage(ImageSkiaSource* source, const gfx::Size& size) | 51 ImageSkiaStorage(ImageSkiaSource* source, const gfx::Size& size) |
| 52 : source_(source), | 52 : source_(source), |
| 53 size_(size) { | 53 size_(size) { |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool has_source() const { return source_.get() != NULL; } |
| 57 |
| 56 std::vector<gfx::ImageSkiaRep>& image_reps() { return image_reps_; } | 58 std::vector<gfx::ImageSkiaRep>& image_reps() { return image_reps_; } |
| 57 | 59 |
| 58 const gfx::Size& size() const { return size_; } | 60 const gfx::Size& size() const { return size_; } |
| 59 | 61 |
| 60 // Returns the iterator of the image rep whose density best matches | 62 // Returns the iterator of the image rep whose density best matches |
| 61 // |scale_factor|. If the image for the |scale_factor| doesn't exist | 63 // |scale_factor|. If the image for the |scale_factor| doesn't exist |
| 62 // in the storage and |storage| is set, it fetches new image by calling | 64 // in the storage and |storage| is set, it fetches new image by calling |
| 63 // |ImageSkiaSource::GetImageForScale|. If the source returns the | 65 // |ImageSkiaSource::GetImageForScale|. If the source returns the |
| 64 // image with different scale factor (if the image doesn't exist in | 66 // image with different scale factor (if the image doesn't exist in |
| 65 // resource, for example), it will fallback to closest image rep. | 67 // resource, for example), it will fallback to closest image rep. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 if (isNull()) | 186 if (isNull()) |
| 185 return; | 187 return; |
| 186 | 188 |
| 187 ImageSkiaReps& image_reps = storage_->image_reps(); | 189 ImageSkiaReps& image_reps = storage_->image_reps(); |
| 188 ImageSkiaReps::iterator it = | 190 ImageSkiaReps::iterator it = |
| 189 storage_->FindRepresentation(scale_factor, false); | 191 storage_->FindRepresentation(scale_factor, false); |
| 190 if (it != image_reps.end() && it->scale_factor() == scale_factor) | 192 if (it != image_reps.end() && it->scale_factor() == scale_factor) |
| 191 image_reps.erase(it); | 193 image_reps.erase(it); |
| 192 } | 194 } |
| 193 | 195 |
| 194 bool ImageSkia::HasRepresentation(ui::ScaleFactor scale_factor) { | 196 bool ImageSkia::HasRepresentation(ui::ScaleFactor scale_factor) const { |
| 195 if (isNull()) | 197 if (isNull()) |
| 196 return false; | 198 return false; |
| 197 | 199 |
| 198 ImageSkiaReps::iterator it = | 200 ImageSkiaReps::iterator it = |
| 199 storage_->FindRepresentation(scale_factor, false); | 201 storage_->FindRepresentation(scale_factor, false); |
| 200 return (it != storage_->image_reps().end() && | 202 return (it != storage_->image_reps().end() && |
| 201 it->scale_factor() == scale_factor); | 203 it->scale_factor() == scale_factor); |
| 202 } | 204 } |
| 203 | 205 |
| 204 const ImageSkiaRep& ImageSkia::GetRepresentation( | 206 const ImageSkiaRep& ImageSkia::GetRepresentation( |
| 205 ui::ScaleFactor scale_factor) const { | 207 ui::ScaleFactor scale_factor) const { |
| 206 if (isNull()) | 208 if (isNull()) |
| 207 return NullImageRep(); | 209 return NullImageRep(); |
| 208 | 210 |
| 209 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale_factor, true); | 211 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale_factor, true); |
| 210 if (it == storage_->image_reps().end()) | 212 if (it == storage_->image_reps().end()) |
| 211 return NullImageRep(); | 213 return NullImageRep(); |
| 212 | 214 |
| 213 return *it; | 215 return *it; |
| 214 } | 216 } |
| 215 | 217 |
| 218 std::vector<ImageSkiaRep> ImageSkia::GetRepresentations() const { |
| 219 if (isNull()) |
| 220 return std::vector<ImageSkiaRep>(); |
| 221 |
| 222 if (!storage_->has_source()) |
| 223 return image_reps(); |
| 224 |
| 225 // Attempt to generate image reps for as many scale factors supported by |
| 226 // this platform as possible. |
| 227 // Do not build return array here because the mapping from scale factor to |
| 228 // image rep is one to many in some cases. |
| 229 std::vector<ui::ScaleFactor> supported_scale_factors = |
| 230 ui::GetSupportedScaleFactors(); |
| 231 for (size_t i = 0; i < supported_scale_factors.size(); ++i) { |
| 232 storage_->FindRepresentation(supported_scale_factors[i], true); |
| 233 } |
| 234 |
| 235 ImageSkiaReps internal_image_reps = storage_->image_reps(); |
| 236 // Create list of image reps to return, skipping null image reps which were |
| 237 // added for caching purposes only. |
| 238 ImageSkiaReps image_reps; |
| 239 for (ImageSkiaReps::iterator it = internal_image_reps.begin(); |
| 240 it != internal_image_reps.end(); ++it) { |
| 241 if (!it->is_null()) |
| 242 image_reps.push_back(*it); |
| 243 } |
| 244 |
| 245 return image_reps; |
| 246 } |
| 247 |
| 216 bool ImageSkia::empty() const { | 248 bool ImageSkia::empty() const { |
| 217 return isNull() || storage_->size().IsEmpty(); | 249 return isNull() || storage_->size().IsEmpty(); |
| 218 } | 250 } |
| 219 | 251 |
| 220 int ImageSkia::width() const { | 252 int ImageSkia::width() const { |
| 221 return isNull() ? 0 : storage_->size().width(); | 253 return isNull() ? 0 : storage_->size().width(); |
| 222 } | 254 } |
| 223 | 255 |
| 224 gfx::Size ImageSkia::size() const { | 256 gfx::Size ImageSkia::size() const { |
| 225 return gfx::Size(width(), height()); | 257 return gfx::Size(width(), height()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 return storage_->image_reps(); | 295 return storage_->image_reps(); |
| 264 } | 296 } |
| 265 | 297 |
| 266 const SkBitmap* ImageSkia::bitmap() const { | 298 const SkBitmap* ImageSkia::bitmap() const { |
| 267 if (isNull()) { | 299 if (isNull()) { |
| 268 // Callers expect a ImageSkiaRep even if it is |isNull()|. | 300 // Callers expect a ImageSkiaRep even if it is |isNull()|. |
| 269 // TODO(pkotwicz): Fix this. | 301 // TODO(pkotwicz): Fix this. |
| 270 return &NullImageRep().sk_bitmap(); | 302 return &NullImageRep().sk_bitmap(); |
| 271 } | 303 } |
| 272 | 304 |
| 273 return &storage_->image_reps()[0].sk_bitmap(); | 305 ImageSkiaReps::iterator it = |
| 306 storage_->FindRepresentation(ui::SCALE_FACTOR_100P, true); |
| 307 return &it->sk_bitmap(); |
| 274 } | 308 } |
| 275 | 309 |
| 276 void ImageSkia::Init(const ImageSkiaRep& image_rep) { | 310 void ImageSkia::Init(const ImageSkiaRep& image_rep) { |
| 277 // TODO(pkotwicz): The image should be null whenever image rep is null. | 311 // TODO(pkotwicz): The image should be null whenever image rep is null. |
| 278 if (image_rep.sk_bitmap().empty()) { | 312 if (image_rep.sk_bitmap().empty()) { |
| 279 storage_ = NULL; | 313 storage_ = NULL; |
| 280 return; | 314 return; |
| 281 } | 315 } |
| 282 storage_ = new internal::ImageSkiaStorage( | 316 storage_ = new internal::ImageSkiaStorage( |
| 283 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); | 317 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight())); |
| 284 storage_->image_reps().push_back(image_rep); | 318 storage_->image_reps().push_back(image_rep); |
| 285 } | 319 } |
| 286 | 320 |
| 287 } // namespace gfx | 321 } // namespace gfx |
| OLD | NEW |