Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / gfx / image / image_skia.cc
blob737d9f4db1cb09c706d896489fb29f038ba2589f
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/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"
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 } // namespace
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'.
66 class 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) {
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());
85 bool has_source() const { return source_.get() != NULL; }
87 std::vector<gfx::ImageSkiaRep>& image_reps() { return image_reps_; }
89 const gfx::Size& size() const { return size_; }
91 bool read_only() const { return read_only_; }
93 void DeleteSource() {
94 source_.reset();
97 void SetReadOnly() {
98 read_only_ = true;
101 void DetachFromThread() {
102 base::NonThreadSafe::DetachFromThread();
105 // Checks if the current thread can safely modify the storage.
106 bool CanModify() const {
107 return !read_only_ && CalledOnValidThread();
110 // Checks if the current thread can safely read the storage.
111 bool CanRead() const {
112 return (read_only_ && !source_.get()) || CalledOnValidThread();
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;
130 image_reps_.push_back(image);
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);
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;
165 float diff = std::abs(it->scale() - scale);
166 if (diff < smallest_diff && !it->is_null()) {
167 closest_iter = it;
168 smallest_diff = diff;
172 if (fetch_new_image && source_.get()) {
173 DCHECK(CalledOnValidThread()) <<
174 "An ImageSkia with the source must be accessed by the same thread.";
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;
191 if (ImageSkia::IsDSFScalingInImageSkiaEnabled() &&
192 scale != resource_scale) {
193 std::vector<ImageSkiaRep>::iterator iter = FindRepresentation(
194 resource_scale, fetch_new_image);
196 DCHECK(iter != image_reps_.end());
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()));
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;
214 } else {
215 image = source_->GetImageForScale(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 bool ImageSkia::IsDSFScalingInImageSkiaEnabled() {
321 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
322 return !command_line->HasSwitch(
323 switches::kDisableArbitraryScaleFactorInImageSkia);
326 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const {
327 ImageSkia* copy = new ImageSkia;
328 if (isNull())
329 return scoped_ptr<ImageSkia>(copy);
331 CHECK(CanRead());
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);
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);
345 bool ImageSkia::BackedBySameObjectAs(const gfx::ImageSkia& other) const {
346 return storage_.get() == other.storage_.get();
349 void ImageSkia::AddRepresentation(const ImageSkiaRep& image_rep) {
350 DCHECK(!image_rep.is_null());
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);
367 void ImageSkia::RemoveRepresentation(float scale) {
368 if (isNull())
369 return;
370 CHECK(CanModify());
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);
379 bool ImageSkia::HasRepresentation(float scale) const {
380 if (isNull())
381 return false;
382 CHECK(CanRead());
384 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, false);
385 return (it != storage_->image_reps().end() && it->scale() == scale);
388 const ImageSkiaRep& ImageSkia::GetRepresentation(float scale) const {
389 if (isNull())
390 return NullImageRep();
392 CHECK(CanRead());
394 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, true);
395 if (it == storage_->image_reps().end())
396 return NullImageRep();
398 return *it;
401 void ImageSkia::SetReadOnly() {
402 CHECK(storage_.get());
403 storage_->SetReadOnly();
404 DetachStorageFromThread();
407 void 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());
417 bool ImageSkia::IsThreadSafe() const {
418 return !storage_.get() || (storage_->read_only() && !storage_->has_source());
421 int ImageSkia::width() const {
422 return isNull() ? 0 : storage_->size().width();
425 gfx::Size ImageSkia::size() const {
426 return gfx::Size(width(), height());
429 int ImageSkia::height() const {
430 return isNull() ? 0 : storage_->size().height();
433 std::vector<ImageSkiaRep> ImageSkia::image_reps() const {
434 if (isNull())
435 return std::vector<ImageSkiaRep>();
437 CHECK(CanRead());
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);
449 return image_reps;
452 void 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);
464 void 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;
470 storage_ = new internal::ImageSkiaStorage(
471 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight()));
472 storage_->image_reps().push_back(image_rep);
475 SkBitmap& 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();
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
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();
494 bool ImageSkia::CanRead() const {
495 return !storage_.get() || storage_->CanRead();
498 bool ImageSkia::CanModify() const {
499 return !storage_.get() || storage_->CanModify();
502 void ImageSkia::DetachStorageFromThread() {
503 if (storage_.get())
504 storage_->DetachFromThread();
507 } // namespace gfx