Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / image / image_skia.cc
blob7158f242c6187d2ae642d366012eb05033e01043
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
3 // found in the LICENSE file.
5 #include "ui/gfx/image/image_skia.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "ui/gfx/geometry/size_conversions.h"
18 #include "ui/gfx/image/image_skia_operations.h"
19 #include "ui/gfx/image/image_skia_source.h"
20 #include "ui/gfx/skia_util.h"
21 #include "ui/gfx/switches.h"
23 namespace gfx {
24 namespace {
26 // static
27 gfx::ImageSkiaRep& NullImageRep() {
28 CR_DEFINE_STATIC_LOCAL(ImageSkiaRep, null_image_rep, ());
29 return null_image_rep;
32 std::vector<float>* g_supported_scales = NULL;
34 // The difference to fall back to the smaller scale factor rather than the
35 // larger one. For example, assume 1.20 is requested but only 1.0 and 2.0 are
36 // supported. In that case, not fall back to 2.0 but 1.0, and then expand
37 // the image to 1.25.
38 const float kFallbackToSmallerScaleDiff = 0.20f;
40 } // namespace
42 namespace internal {
43 namespace {
45 class Matcher {
46 public:
47 explicit Matcher(float scale) : scale_(scale) {
50 bool operator()(const ImageSkiaRep& rep) const {
51 return rep.scale() == scale_;
54 private:
55 float scale_;
58 ImageSkiaRep ScaleImageSkiaRep(const ImageSkiaRep& rep, float target_scale) {
59 if (rep.is_null() || rep.scale() == target_scale)
60 return rep;
62 gfx::Size scaled_size = ToCeiledSize(
63 gfx::ScaleSize(rep.pixel_size(), target_scale / rep.scale()));
64 return ImageSkiaRep(skia::ImageOperations::Resize(
65 rep.sk_bitmap(),
66 skia::ImageOperations::RESIZE_LANCZOS3,
67 scaled_size.width(),
68 scaled_size.height()), target_scale);
71 } // namespace
73 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a
74 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's
75 // information. Having both |base::RefCountedThreadSafe| and
76 // |base::NonThreadSafe| may sounds strange but necessary to turn
77 // the 'thread-non-safe modifiable ImageSkiaStorage' into
78 // the 'thread-safe read-only ImageSkiaStorage'.
79 class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>,
80 public base::NonThreadSafe {
81 public:
82 ImageSkiaStorage(ImageSkiaSource* source, const gfx::Size& size)
83 : source_(source),
84 size_(size),
85 read_only_(false) {
88 ImageSkiaStorage(ImageSkiaSource* source, float scale)
89 : source_(source),
90 read_only_(false) {
91 ImageSkia::ImageSkiaReps::iterator it = FindRepresentation(scale, true);
92 if (it == image_reps_.end() || it->is_null())
93 source_.reset();
94 else
95 size_.SetSize(it->GetWidth(), it->GetHeight());
98 bool has_source() const { return source_.get() != NULL; }
100 std::vector<gfx::ImageSkiaRep>& image_reps() { return image_reps_; }
102 const gfx::Size& size() const { return size_; }
104 bool read_only() const { return read_only_; }
106 void DeleteSource() {
107 source_.reset();
110 void SetReadOnly() {
111 read_only_ = true;
114 void DetachFromThread() {
115 base::NonThreadSafe::DetachFromThread();
118 // Checks if the current thread can safely modify the storage.
119 bool CanModify() const {
120 return !read_only_ && CalledOnValidThread();
123 // Checks if the current thread can safely read the storage.
124 bool CanRead() const {
125 return (read_only_ && !source_.get()) || CalledOnValidThread();
128 // Add a new representation. This checks if the scale of the added image
129 // is not 1.0f, and mark the existing rep as scaled to make
130 // the image high DPI aware.
131 void AddRepresentation(const ImageSkiaRep& image) {
132 if (image.scale() != 1.0f) {
133 for (ImageSkia::ImageSkiaReps::iterator it = image_reps_.begin();
134 it < image_reps_.end();
135 ++it) {
136 if (it->unscaled()) {
137 DCHECK_EQ(1.0f, it->scale());
138 it->SetScaled();
139 break;
143 image_reps_.push_back(image);
146 // Returns the iterator of the image rep whose density best matches
147 // |scale|. If the image for the |scale| doesn't exist in the storage and
148 // |storage| is set, it fetches new image by calling
149 // |ImageSkiaSource::GetImageForScale|. There are two modes to deal with
150 // arbitrary scale factors.
151 // 1: Invoke GetImageForScale with requested scale and if the source
152 // returns the image with different scale (if the image doesn't exist in
153 // resource, for example), it will fallback to closest image rep.
154 // 2: Invoke GetImageForScale with the closest known scale to the requested
155 // one and rescale the image.
156 // Right now only Windows uses 2 and other platforms use 1 by default.
157 // TODO(mukai, oshima): abandon 1 code path and use 2 for every platforms.
158 std::vector<ImageSkiaRep>::iterator FindRepresentation(
159 float scale, bool fetch_new_image) const {
160 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this);
162 ImageSkia::ImageSkiaReps::iterator closest_iter =
163 non_const->image_reps().end();
164 ImageSkia::ImageSkiaReps::iterator exact_iter =
165 non_const->image_reps().end();
166 float smallest_diff = std::numeric_limits<float>::max();
167 for (ImageSkia::ImageSkiaReps::iterator it =
168 non_const->image_reps().begin();
169 it < image_reps_.end(); ++it) {
170 if (it->scale() == scale) {
171 // found exact match
172 fetch_new_image = false;
173 if (it->is_null())
174 continue;
175 exact_iter = it;
176 break;
178 float diff = std::abs(it->scale() - scale);
179 if (diff < smallest_diff && !it->is_null()) {
180 closest_iter = it;
181 smallest_diff = diff;
185 if (fetch_new_image && source_.get()) {
186 DCHECK(CalledOnValidThread()) <<
187 "An ImageSkia with the source must be accessed by the same thread.";
189 ImageSkiaRep image;
190 float resource_scale = scale;
191 if (g_supported_scales) {
192 if (g_supported_scales->back() <= scale) {
193 resource_scale = g_supported_scales->back();
194 } else {
195 for (size_t i = 0; i < g_supported_scales->size(); ++i) {
196 if ((*g_supported_scales)[i] + kFallbackToSmallerScaleDiff >=
197 scale) {
198 resource_scale = (*g_supported_scales)[i];
199 break;
204 if (scale != resource_scale) {
205 std::vector<ImageSkiaRep>::iterator iter = FindRepresentation(
206 resource_scale, fetch_new_image);
207 DCHECK(iter != image_reps_.end());
208 image = iter->unscaled() ? (*iter) : ScaleImageSkiaRep(*iter, scale);
209 } else {
210 image = source_->GetImageForScale(scale);
211 // Image may be missing for the specified scale in some cases, such like
212 // looking up 2x resources but the 2x resource pack is missing. Falls
213 // back to 1x and re-scale it.
214 if (image.is_null() && scale != 1.0f)
215 image = ScaleImageSkiaRep(source_->GetImageForScale(1.0f), scale);
218 // If the source returned the new image, store it.
219 if (!image.is_null() &&
220 std::find_if(image_reps_.begin(), image_reps_.end(),
221 Matcher(image.scale())) == image_reps_.end()) {
222 non_const->image_reps().push_back(image);
225 // If the result image's scale isn't same as the expected scale, create
226 // null ImageSkiaRep with the |scale| so that the next lookup will
227 // fallback to the closest scale.
228 if (image.is_null() || image.scale() != scale) {
229 non_const->image_reps().push_back(ImageSkiaRep(SkBitmap(), scale));
232 // image_reps_ must have the exact much now, so find again.
233 return FindRepresentation(scale, false);
235 return exact_iter != image_reps_.end() ? exact_iter : closest_iter;
238 private:
239 virtual ~ImageSkiaStorage() {
240 // We only care if the storage is modified by the same thread.
241 // Don't blow up even if someone else deleted the ImageSkia.
242 DetachFromThread();
245 // Vector of bitmaps and their associated scale.
246 std::vector<gfx::ImageSkiaRep> image_reps_;
248 scoped_ptr<ImageSkiaSource> source_;
250 // Size of the image in DIP.
251 gfx::Size size_;
253 bool read_only_;
255 friend class base::RefCountedThreadSafe<ImageSkiaStorage>;
258 } // internal
260 ImageSkia::ImageSkia() : storage_(NULL) {
263 ImageSkia::ImageSkia(ImageSkiaSource* source, const gfx::Size& size)
264 : storage_(new internal::ImageSkiaStorage(source, size)) {
265 DCHECK(source);
266 // No other thread has reference to this, so it's safe to detach the thread.
267 DetachStorageFromThread();
270 ImageSkia::ImageSkia(ImageSkiaSource* source, float scale)
271 : storage_(new internal::ImageSkiaStorage(source, scale)) {
272 DCHECK(source);
273 if (!storage_->has_source())
274 storage_ = NULL;
275 // No other thread has reference to this, so it's safe to detach the thread.
276 DetachStorageFromThread();
279 ImageSkia::ImageSkia(const ImageSkiaRep& image_rep) {
280 Init(image_rep);
281 // No other thread has reference to this, so it's safe to detach the thread.
282 DetachStorageFromThread();
285 ImageSkia::ImageSkia(const ImageSkia& other) : storage_(other.storage_) {
288 ImageSkia& ImageSkia::operator=(const ImageSkia& other) {
289 storage_ = other.storage_;
290 return *this;
293 ImageSkia::~ImageSkia() {
296 // static
297 void ImageSkia::SetSupportedScales(const std::vector<float>& supported_scales) {
298 if (g_supported_scales != NULL)
299 delete g_supported_scales;
300 g_supported_scales = new std::vector<float>(supported_scales);
301 std::sort(g_supported_scales->begin(), g_supported_scales->end());
304 // static
305 const std::vector<float>& ImageSkia::GetSupportedScales() {
306 DCHECK(g_supported_scales != NULL);
307 return *g_supported_scales;
310 // static
311 float ImageSkia::GetMaxSupportedScale() {
312 return g_supported_scales->back();
315 // static
316 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) {
317 return ImageSkia(ImageSkiaRep(bitmap, 0.0f));
320 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const {
321 ImageSkia* copy = new ImageSkia;
322 if (isNull())
323 return make_scoped_ptr(copy);
325 CHECK(CanRead());
327 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps();
328 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin();
329 iter != reps.end(); ++iter) {
330 copy->AddRepresentation(*iter);
332 // The copy has its own storage. Detach the copy from the current
333 // thread so that other thread can use this.
334 if (!copy->isNull())
335 copy->storage_->DetachFromThread();
336 return make_scoped_ptr(copy);
339 bool ImageSkia::BackedBySameObjectAs(const gfx::ImageSkia& other) const {
340 return storage_.get() == other.storage_.get();
343 void ImageSkia::AddRepresentation(const ImageSkiaRep& image_rep) {
344 DCHECK(!image_rep.is_null());
346 // TODO(oshima): This method should be called |SetRepresentation|
347 // and replace the existing rep if there is already one with the
348 // same scale so that we can guarantee that a ImageSkia instance contains only
349 // one image rep per scale. This is not possible now as ImageLoader currently
350 // stores need this feature, but this needs to be fixed.
351 if (isNull()) {
352 Init(image_rep);
353 } else {
354 CHECK(CanModify());
355 // If someone is adding ImageSkia explicitly, check if we should
356 // make the image high DPI aware.
357 storage_->AddRepresentation(image_rep);
361 void ImageSkia::RemoveRepresentation(float scale) {
362 if (isNull())
363 return;
364 CHECK(CanModify());
366 ImageSkiaReps& image_reps = storage_->image_reps();
367 ImageSkiaReps::iterator it =
368 storage_->FindRepresentation(scale, false);
369 if (it != image_reps.end() && it->scale() == scale)
370 image_reps.erase(it);
373 bool ImageSkia::HasRepresentation(float scale) const {
374 if (isNull())
375 return false;
376 CHECK(CanRead());
378 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, false);
379 return (it != storage_->image_reps().end() && it->scale() == scale);
382 const ImageSkiaRep& ImageSkia::GetRepresentation(float scale) const {
383 if (isNull())
384 return NullImageRep();
386 CHECK(CanRead());
388 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, true);
389 if (it == storage_->image_reps().end())
390 return NullImageRep();
392 return *it;
395 void ImageSkia::SetReadOnly() {
396 CHECK(storage_.get());
397 storage_->SetReadOnly();
398 DetachStorageFromThread();
401 void ImageSkia::MakeThreadSafe() {
402 CHECK(storage_.get());
403 EnsureRepsForSupportedScales();
404 // Delete source as we no longer needs it.
405 if (storage_.get())
406 storage_->DeleteSource();
407 storage_->SetReadOnly();
408 CHECK(IsThreadSafe());
411 bool ImageSkia::IsThreadSafe() const {
412 return !storage_.get() || (storage_->read_only() && !storage_->has_source());
415 int ImageSkia::width() const {
416 return isNull() ? 0 : storage_->size().width();
419 gfx::Size ImageSkia::size() const {
420 return gfx::Size(width(), height());
423 int ImageSkia::height() const {
424 return isNull() ? 0 : storage_->size().height();
427 std::vector<ImageSkiaRep> ImageSkia::image_reps() const {
428 if (isNull())
429 return std::vector<ImageSkiaRep>();
431 CHECK(CanRead());
433 ImageSkiaReps internal_image_reps = storage_->image_reps();
434 // Create list of image reps to return, skipping null image reps which were
435 // added for caching purposes only.
436 ImageSkiaReps image_reps;
437 for (ImageSkiaReps::iterator it = internal_image_reps.begin();
438 it != internal_image_reps.end(); ++it) {
439 if (!it->is_null())
440 image_reps.push_back(*it);
443 return image_reps;
446 void ImageSkia::EnsureRepsForSupportedScales() const {
447 DCHECK(g_supported_scales != NULL);
448 // Don't check ReadOnly because the source may generate images
449 // even for read only ImageSkia. Concurrent access will be protected
450 // by |DCHECK(CalledOnValidThread())| in FindRepresentation.
451 if (storage_.get() && storage_->has_source()) {
452 for (std::vector<float>::const_iterator it = g_supported_scales->begin();
453 it != g_supported_scales->end(); ++it)
454 storage_->FindRepresentation(*it, true);
458 void ImageSkia::Init(const ImageSkiaRep& image_rep) {
459 // TODO(pkotwicz): The image should be null whenever image rep is null.
460 if (image_rep.sk_bitmap().empty()) {
461 storage_ = NULL;
462 return;
464 storage_ = new internal::ImageSkiaStorage(
465 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight()));
466 storage_->image_reps().push_back(image_rep);
469 const SkBitmap& ImageSkia::GetBitmap() const {
470 if (isNull()) {
471 // Callers expect a ImageSkiaRep even if it is |isNull()|.
472 // TODO(pkotwicz): Fix this.
473 return NullImageRep().sk_bitmap();
476 // TODO(oshima): This made a few tests flaky on Windows.
477 // Fix the root cause and re-enable this. crbug.com/145623.
478 #if !defined(OS_WIN)
479 CHECK(CanRead());
480 #endif
482 ImageSkiaReps::iterator it = storage_->FindRepresentation(1.0f, true);
483 if (it != storage_->image_reps().end())
484 return it->sk_bitmap();
485 return NullImageRep().sk_bitmap();
488 bool ImageSkia::CanRead() const {
489 return !storage_.get() || storage_->CanRead();
492 bool ImageSkia::CanModify() const {
493 return !storage_.get() || storage_->CanModify();
496 void ImageSkia::DetachStorageFromThread() {
497 if (storage_.get())
498 storage_->DetachFromThread();
501 } // namespace gfx