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.
4
5#include "ui/gfx/image/image_skia.h"
6
7#include <algorithm>
8#include <cmath>
9#include <limits>
10
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/size_conversions.h"
16#include "ui/gfx/image/image_skia_operations.h"
17#include "ui/gfx/image/image_skia_source.h"
18#include "ui/gfx/rect.h"
19#include "ui/gfx/size.h"
20#include "ui/gfx/skia_util.h"
21#include "ui/gfx/switches.h"
22
23namespace gfx {
24namespace {
25
26// static
27gfx::ImageSkiaRep& NullImageRep() {
28  CR_DEFINE_STATIC_LOCAL(ImageSkiaRep, null_image_rep, ());
29  return null_image_rep;
30}
31
32std::vector<float>* g_supported_scales = NULL;
33
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.
38const float kFallbackToSmallerScaleDiff = 0.20f;
39
40}  // namespace
41
42namespace internal {
43namespace {
44
45class Matcher {
46 public:
47  explicit Matcher(float scale) : scale_(scale) {
48  }
49
50  bool operator()(const ImageSkiaRep& rep) const {
51    return rep.scale() == scale_;
52  }
53
54 private:
55  float scale_;
56};
57
58}  // namespace
59
60// A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a
61// refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's
62// information. Having both |base::RefCountedThreadSafe| and
63// |base::NonThreadSafe| may sounds strange but necessary to turn
64// the 'thread-non-safe modifiable ImageSkiaStorage' into
65// the 'thread-safe read-only ImageSkiaStorage'.
66class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>,
67                         public base::NonThreadSafe {
68 public:
69  ImageSkiaStorage(ImageSkiaSource* source, const gfx::Size& size)
70      : source_(source),
71        size_(size),
72        read_only_(false) {
73  }
74
75  ImageSkiaStorage(ImageSkiaSource* source, float scale)
76      : source_(source),
77        read_only_(false) {
78    ImageSkia::ImageSkiaReps::iterator it = FindRepresentation(scale, true);
79    if (it == image_reps_.end() || it->is_null())
80      source_.reset();
81    else
82      size_.SetSize(it->GetWidth(), it->GetHeight());
83  }
84
85  bool has_source() const { return source_.get() != NULL; }
86
87  std::vector<gfx::ImageSkiaRep>& image_reps() { return image_reps_; }
88
89  const gfx::Size& size() const { return size_; }
90
91  bool read_only() const { return read_only_; }
92
93  void DeleteSource() {
94    source_.reset();
95  }
96
97  void SetReadOnly() {
98    read_only_ = true;
99  }
100
101  void DetachFromThread() {
102    base::NonThreadSafe::DetachFromThread();
103  }
104
105  // Checks if the current thread can safely modify the storage.
106  bool CanModify() const {
107    return !read_only_ && CalledOnValidThread();
108  }
109
110  // Checks if the current thread can safely read the storage.
111  bool CanRead() const {
112    return (read_only_ && !source_.get()) || CalledOnValidThread();
113  }
114
115  // Add a new representation. This checks if the scale of the added image
116  // is not 1.0f, and mark the existing rep as scaled to make
117  // the image high DPI aware.
118  void AddRepresentation(const ImageSkiaRep& image) {
119    if (image.scale() != 1.0f) {
120      for (ImageSkia::ImageSkiaReps::iterator it = image_reps_.begin();
121           it < image_reps_.end();
122           ++it) {
123        if (it->unscaled()) {
124          DCHECK_EQ(1.0f, it->scale());
125          it->SetScaled();
126          break;
127        }
128      }
129    }
130    image_reps_.push_back(image);
131  }
132
133  // Returns the iterator of the image rep whose density best matches
134  // |scale|. If the image for the |scale| doesn't exist in the storage and
135  // |storage| is set, it fetches new image by calling
136  // |ImageSkiaSource::GetImageForScale|. There are two modes to deal with
137  // arbitrary scale factors.
138  // 1: Invoke GetImageForScale with requested scale and if the source
139  //   returns the image with different scale (if the image doesn't exist in
140  //   resource, for example), it will fallback to closest image rep.
141  // 2: Invoke GetImageForScale with the closest known scale to the requested
142  //   one and rescale the image.
143  // Right now only Windows uses 2 and other platforms use 1 by default.
144  // TODO(mukai, oshima): abandon 1 code path and use 2 for every platforms.
145  std::vector<ImageSkiaRep>::iterator FindRepresentation(
146      float scale, bool fetch_new_image) const {
147    ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this);
148
149    ImageSkia::ImageSkiaReps::iterator closest_iter =
150        non_const->image_reps().end();
151    ImageSkia::ImageSkiaReps::iterator exact_iter =
152        non_const->image_reps().end();
153    float smallest_diff = std::numeric_limits<float>::max();
154    for (ImageSkia::ImageSkiaReps::iterator it =
155             non_const->image_reps().begin();
156         it < image_reps_.end(); ++it) {
157      if (it->scale() == scale) {
158        // found exact match
159        fetch_new_image = false;
160        if (it->is_null())
161          continue;
162        exact_iter = it;
163        break;
164      }
165      float diff = std::abs(it->scale() - scale);
166      if (diff < smallest_diff && !it->is_null()) {
167        closest_iter = it;
168        smallest_diff = diff;
169      }
170    }
171
172    if (fetch_new_image && source_.get()) {
173      DCHECK(CalledOnValidThread()) <<
174          "An ImageSkia with the source must be accessed by the same thread.";
175
176      ImageSkiaRep image;
177      float resource_scale = scale;
178      if (ImageSkia::IsDSFScalingInImageSkiaEnabled() && g_supported_scales) {
179        if (g_supported_scales->back() <= scale) {
180          resource_scale = g_supported_scales->back();
181        } else {
182          for (size_t i = 0; i < g_supported_scales->size(); ++i) {
183            if ((*g_supported_scales)[i] + kFallbackToSmallerScaleDiff >=
184                scale) {
185              resource_scale = (*g_supported_scales)[i];
186              break;
187            }
188          }
189        }
190      }
191      if (ImageSkia::IsDSFScalingInImageSkiaEnabled() &&
192          scale != resource_scale) {
193        std::vector<ImageSkiaRep>::iterator iter = FindRepresentation(
194            resource_scale, fetch_new_image);
195
196        DCHECK(iter != image_reps_.end());
197
198        if (!iter->unscaled()) {
199          SkBitmap scaled_image;
200          gfx::Size unscaled_size(iter->pixel_width(), iter->pixel_height());
201          gfx::Size scaled_size = ToCeiledSize(
202              gfx::ScaleSize(unscaled_size, scale / iter->scale()));
203
204          image = ImageSkiaRep(skia::ImageOperations::Resize(
205              iter->sk_bitmap(),
206              skia::ImageOperations::RESIZE_LANCZOS3,
207              scaled_size.width(),
208              scaled_size.height()), scale);
209          DCHECK_EQ(image.pixel_width(), scaled_size.width());
210          DCHECK_EQ(image.pixel_height(), scaled_size.height());
211        } else {
212          image = *iter;
213        }
214      } else {
215        image = source_->GetImageForScale(scale);
216      }
217
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);
223      }
224
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));
230      }
231
232      // image_reps_ must have the exact much now, so find again.
233      return FindRepresentation(scale, false);
234    }
235    return exact_iter != image_reps_.end() ? exact_iter : closest_iter;
236  }
237
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();
243  }
244
245  // Vector of bitmaps and their associated scale.
246  std::vector<gfx::ImageSkiaRep> image_reps_;
247
248  scoped_ptr<ImageSkiaSource> source_;
249
250  // Size of the image in DIP.
251  gfx::Size size_;
252
253  bool read_only_;
254
255  friend class base::RefCountedThreadSafe<ImageSkiaStorage>;
256};
257
258}  // internal
259
260ImageSkia::ImageSkia() : storage_(NULL) {
261}
262
263ImageSkia::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();
268}
269
270ImageSkia::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();
277}
278
279ImageSkia::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();
283}
284
285ImageSkia::ImageSkia(const ImageSkia& other) : storage_(other.storage_) {
286}
287
288ImageSkia& ImageSkia::operator=(const ImageSkia& other) {
289  storage_ = other.storage_;
290  return *this;
291}
292
293ImageSkia::~ImageSkia() {
294}
295
296// static
297void 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());
302}
303
304// static
305const std::vector<float>& ImageSkia::GetSupportedScales() {
306  DCHECK(g_supported_scales != NULL);
307  return *g_supported_scales;
308}
309
310// static
311float ImageSkia::GetMaxSupportedScale() {
312  return g_supported_scales->back();
313}
314
315// static
316ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) {
317  return ImageSkia(ImageSkiaRep(bitmap, 0.0f));
318}
319
320bool ImageSkia::IsDSFScalingInImageSkiaEnabled() {
321  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
322  return !command_line->HasSwitch(
323      switches::kDisableArbitraryScaleFactorInImageSkia);
324}
325
326scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const {
327  ImageSkia* copy = new ImageSkia;
328  if (isNull())
329    return scoped_ptr<ImageSkia>(copy);
330
331  CHECK(CanRead());
332
333  std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps();
334  for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin();
335       iter != reps.end(); ++iter) {
336    copy->AddRepresentation(*iter);
337  }
338  // The copy has its own storage. Detach the copy from the current
339  // thread so that other thread can use this.
340  if (!copy->isNull())
341    copy->storage_->DetachFromThread();
342  return scoped_ptr<ImageSkia>(copy);
343}
344
345bool ImageSkia::BackedBySameObjectAs(const gfx::ImageSkia& other) const {
346  return storage_.get() == other.storage_.get();
347}
348
349void ImageSkia::AddRepresentation(const ImageSkiaRep& image_rep) {
350  DCHECK(!image_rep.is_null());
351
352  // TODO(oshima): This method should be called |SetRepresentation|
353  // and replace the existing rep if there is already one with the
354  // same scale so that we can guarantee that a ImageSkia instance contains only
355  // one image rep per scale. This is not possible now as ImageLoader currently
356  // stores need this feature, but this needs to be fixed.
357  if (isNull()) {
358    Init(image_rep);
359  } else {
360    CHECK(CanModify());
361    // If someone is adding ImageSkia explicitly, check if we should
362    // make the image high DPI aware.
363    storage_->AddRepresentation(image_rep);
364  }
365}
366
367void ImageSkia::RemoveRepresentation(float scale) {
368  if (isNull())
369    return;
370  CHECK(CanModify());
371
372  ImageSkiaReps& image_reps = storage_->image_reps();
373  ImageSkiaReps::iterator it =
374      storage_->FindRepresentation(scale, false);
375  if (it != image_reps.end() && it->scale() == scale)
376    image_reps.erase(it);
377}
378
379bool ImageSkia::HasRepresentation(float scale) const {
380  if (isNull())
381    return false;
382  CHECK(CanRead());
383
384  ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, false);
385  return (it != storage_->image_reps().end() && it->scale() == scale);
386}
387
388const ImageSkiaRep& ImageSkia::GetRepresentation(float scale) const {
389  if (isNull())
390    return NullImageRep();
391
392  CHECK(CanRead());
393
394  ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, true);
395  if (it == storage_->image_reps().end())
396    return NullImageRep();
397
398  return *it;
399}
400
401void ImageSkia::SetReadOnly() {
402  CHECK(storage_.get());
403  storage_->SetReadOnly();
404  DetachStorageFromThread();
405}
406
407void ImageSkia::MakeThreadSafe() {
408  CHECK(storage_.get());
409  EnsureRepsForSupportedScales();
410  // Delete source as we no longer needs it.
411  if (storage_.get())
412    storage_->DeleteSource();
413  storage_->SetReadOnly();
414  CHECK(IsThreadSafe());
415}
416
417bool ImageSkia::IsThreadSafe() const {
418  return !storage_.get() || (storage_->read_only() && !storage_->has_source());
419}
420
421int ImageSkia::width() const {
422  return isNull() ? 0 : storage_->size().width();
423}
424
425gfx::Size ImageSkia::size() const {
426  return gfx::Size(width(), height());
427}
428
429int ImageSkia::height() const {
430  return isNull() ? 0 : storage_->size().height();
431}
432
433std::vector<ImageSkiaRep> ImageSkia::image_reps() const {
434  if (isNull())
435    return std::vector<ImageSkiaRep>();
436
437  CHECK(CanRead());
438
439  ImageSkiaReps internal_image_reps = storage_->image_reps();
440  // Create list of image reps to return, skipping null image reps which were
441  // added for caching purposes only.
442  ImageSkiaReps image_reps;
443  for (ImageSkiaReps::iterator it = internal_image_reps.begin();
444       it != internal_image_reps.end(); ++it) {
445    if (!it->is_null())
446      image_reps.push_back(*it);
447  }
448
449  return image_reps;
450}
451
452void ImageSkia::EnsureRepsForSupportedScales() const {
453  DCHECK(g_supported_scales != NULL);
454  // Don't check ReadOnly because the source may generate images
455  // even for read only ImageSkia. Concurrent access will be protected
456  // by |DCHECK(CalledOnValidThread())| in FindRepresentation.
457  if (storage_.get() && storage_->has_source()) {
458    for (std::vector<float>::const_iterator it = g_supported_scales->begin();
459         it != g_supported_scales->end(); ++it)
460      storage_->FindRepresentation(*it, true);
461  }
462}
463
464void ImageSkia::Init(const ImageSkiaRep& image_rep) {
465  // TODO(pkotwicz): The image should be null whenever image rep is null.
466  if (image_rep.sk_bitmap().empty()) {
467    storage_ = NULL;
468    return;
469  }
470  storage_ = new internal::ImageSkiaStorage(
471      NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight()));
472  storage_->image_reps().push_back(image_rep);
473}
474
475SkBitmap& ImageSkia::GetBitmap() const {
476  if (isNull()) {
477    // Callers expect a ImageSkiaRep even if it is |isNull()|.
478    // TODO(pkotwicz): Fix this.
479    return NullImageRep().mutable_sk_bitmap();
480  }
481
482  // TODO(oshima): This made a few tests flaky on Windows.
483  // Fix the root cause and re-enable this. crbug.com/145623.
484#if !defined(OS_WIN)
485  CHECK(CanRead());
486#endif
487
488  ImageSkiaReps::iterator it = storage_->FindRepresentation(1.0f, true);
489  if (it != storage_->image_reps().end())
490    return it->mutable_sk_bitmap();
491  return NullImageRep().mutable_sk_bitmap();
492}
493
494bool ImageSkia::CanRead() const {
495  return !storage_.get() || storage_->CanRead();
496}
497
498bool ImageSkia::CanModify() const {
499  return !storage_.get() || storage_->CanModify();
500}
501
502void ImageSkia::DetachStorageFromThread() {
503  if (storage_.get())
504    storage_->DetachFromThread();
505}
506
507}  // namespace gfx
508