mojo: Fix map of booleans for python bindings.
[chromium-blink-merge.git] / ui / gfx / image / image_skia.cc
blobbba6c47641f9f08da231f5fb28f97022a6ca6ad2
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 ImageSkiaRep ScaleImageSkiaRep(const ImageSkiaRep& rep, float target_scale) {
59 DCHECK_NE(rep.scale(), target_scale);
60 if (rep.is_null())
61 return rep;
63 gfx::Size scaled_size = ToCeiledSize(
64 gfx::ScaleSize(rep.pixel_size(), target_scale / rep.scale()));
65 return ImageSkiaRep(skia::ImageOperations::Resize(
66 rep.sk_bitmap(),
67 skia::ImageOperations::RESIZE_LANCZOS3,
68 scaled_size.width(),
69 scaled_size.height()), target_scale);
72 } // namespace
74 // A helper class such that ImageSkia can be cheaply copied. ImageSkia holds a
75 // refptr instance of ImageSkiaStorage, which in turn holds all of ImageSkia's
76 // information. Having both |base::RefCountedThreadSafe| and
77 // |base::NonThreadSafe| may sounds strange but necessary to turn
78 // the 'thread-non-safe modifiable ImageSkiaStorage' into
79 // the 'thread-safe read-only ImageSkiaStorage'.
80 class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>,
81 public base::NonThreadSafe {
82 public:
83 ImageSkiaStorage(ImageSkiaSource* source, const gfx::Size& size)
84 : source_(source),
85 size_(size),
86 read_only_(false) {
89 ImageSkiaStorage(ImageSkiaSource* source, float scale)
90 : source_(source),
91 read_only_(false) {
92 ImageSkia::ImageSkiaReps::iterator it = FindRepresentation(scale, true);
93 if (it == image_reps_.end() || it->is_null())
94 source_.reset();
95 else
96 size_.SetSize(it->GetWidth(), it->GetHeight());
99 bool has_source() const { return source_.get() != NULL; }
101 std::vector<gfx::ImageSkiaRep>& image_reps() { return image_reps_; }
103 const gfx::Size& size() const { return size_; }
105 bool read_only() const { return read_only_; }
107 void DeleteSource() {
108 source_.reset();
111 void SetReadOnly() {
112 read_only_ = true;
115 void DetachFromThread() {
116 base::NonThreadSafe::DetachFromThread();
119 // Checks if the current thread can safely modify the storage.
120 bool CanModify() const {
121 return !read_only_ && CalledOnValidThread();
124 // Checks if the current thread can safely read the storage.
125 bool CanRead() const {
126 return (read_only_ && !source_.get()) || CalledOnValidThread();
129 // Add a new representation. This checks if the scale of the added image
130 // is not 1.0f, and mark the existing rep as scaled to make
131 // the image high DPI aware.
132 void AddRepresentation(const ImageSkiaRep& image) {
133 if (image.scale() != 1.0f) {
134 for (ImageSkia::ImageSkiaReps::iterator it = image_reps_.begin();
135 it < image_reps_.end();
136 ++it) {
137 if (it->unscaled()) {
138 DCHECK_EQ(1.0f, it->scale());
139 it->SetScaled();
140 break;
144 image_reps_.push_back(image);
147 // Returns the iterator of the image rep whose density best matches
148 // |scale|. If the image for the |scale| doesn't exist in the storage and
149 // |storage| is set, it fetches new image by calling
150 // |ImageSkiaSource::GetImageForScale|. There are two modes to deal with
151 // arbitrary scale factors.
152 // 1: Invoke GetImageForScale with requested scale and if the source
153 // returns the image with different scale (if the image doesn't exist in
154 // resource, for example), it will fallback to closest image rep.
155 // 2: Invoke GetImageForScale with the closest known scale to the requested
156 // one and rescale the image.
157 // Right now only Windows uses 2 and other platforms use 1 by default.
158 // TODO(mukai, oshima): abandon 1 code path and use 2 for every platforms.
159 std::vector<ImageSkiaRep>::iterator FindRepresentation(
160 float scale, bool fetch_new_image) const {
161 ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this);
163 ImageSkia::ImageSkiaReps::iterator closest_iter =
164 non_const->image_reps().end();
165 ImageSkia::ImageSkiaReps::iterator exact_iter =
166 non_const->image_reps().end();
167 float smallest_diff = std::numeric_limits<float>::max();
168 for (ImageSkia::ImageSkiaReps::iterator it =
169 non_const->image_reps().begin();
170 it < image_reps_.end(); ++it) {
171 if (it->scale() == scale) {
172 // found exact match
173 fetch_new_image = false;
174 if (it->is_null())
175 continue;
176 exact_iter = it;
177 break;
179 float diff = std::abs(it->scale() - scale);
180 if (diff < smallest_diff && !it->is_null()) {
181 closest_iter = it;
182 smallest_diff = diff;
186 if (fetch_new_image && source_.get()) {
187 DCHECK(CalledOnValidThread()) <<
188 "An ImageSkia with the source must be accessed by the same thread.";
190 ImageSkiaRep image;
191 float resource_scale = scale;
192 if (g_supported_scales) {
193 if (g_supported_scales->back() <= scale) {
194 resource_scale = g_supported_scales->back();
195 } else {
196 for (size_t i = 0; i < g_supported_scales->size(); ++i) {
197 if ((*g_supported_scales)[i] + kFallbackToSmallerScaleDiff >=
198 scale) {
199 resource_scale = (*g_supported_scales)[i];
200 break;
205 if (scale != resource_scale) {
206 std::vector<ImageSkiaRep>::iterator iter = FindRepresentation(
207 resource_scale, fetch_new_image);
208 DCHECK(iter != image_reps_.end());
209 image = iter->unscaled() ? (*iter) : ScaleImageSkiaRep(*iter, scale);
210 } else {
211 image = source_->GetImageForScale(scale);
212 // Image may be missing for the specified scale in some cases, such like
213 // looking up 2x resources but the 2x resource pack is missing. Falls
214 // back to 1x and re-scale it.
215 if (image.is_null() && scale != 1.0f)
216 image = ScaleImageSkiaRep(source_->GetImageForScale(1.0f), scale);
219 // If the source returned the new image, store it.
220 if (!image.is_null() &&
221 std::find_if(image_reps_.begin(), image_reps_.end(),
222 Matcher(image.scale())) == image_reps_.end()) {
223 non_const->image_reps().push_back(image);
226 // If the result image's scale isn't same as the expected scale, create
227 // null ImageSkiaRep with the |scale| so that the next lookup will
228 // fallback to the closest scale.
229 if (image.is_null() || image.scale() != scale) {
230 non_const->image_reps().push_back(ImageSkiaRep(SkBitmap(), scale));
233 // image_reps_ must have the exact much now, so find again.
234 return FindRepresentation(scale, false);
236 return exact_iter != image_reps_.end() ? exact_iter : closest_iter;
239 private:
240 virtual ~ImageSkiaStorage() {
241 // We only care if the storage is modified by the same thread.
242 // Don't blow up even if someone else deleted the ImageSkia.
243 DetachFromThread();
246 // Vector of bitmaps and their associated scale.
247 std::vector<gfx::ImageSkiaRep> image_reps_;
249 scoped_ptr<ImageSkiaSource> source_;
251 // Size of the image in DIP.
252 gfx::Size size_;
254 bool read_only_;
256 friend class base::RefCountedThreadSafe<ImageSkiaStorage>;
259 } // internal
261 ImageSkia::ImageSkia() : storage_(NULL) {
264 ImageSkia::ImageSkia(ImageSkiaSource* source, const gfx::Size& size)
265 : storage_(new internal::ImageSkiaStorage(source, size)) {
266 DCHECK(source);
267 // No other thread has reference to this, so it's safe to detach the thread.
268 DetachStorageFromThread();
271 ImageSkia::ImageSkia(ImageSkiaSource* source, float scale)
272 : storage_(new internal::ImageSkiaStorage(source, scale)) {
273 DCHECK(source);
274 if (!storage_->has_source())
275 storage_ = NULL;
276 // No other thread has reference to this, so it's safe to detach the thread.
277 DetachStorageFromThread();
280 ImageSkia::ImageSkia(const ImageSkiaRep& image_rep) {
281 Init(image_rep);
282 // No other thread has reference to this, so it's safe to detach the thread.
283 DetachStorageFromThread();
286 ImageSkia::ImageSkia(const ImageSkia& other) : storage_(other.storage_) {
289 ImageSkia& ImageSkia::operator=(const ImageSkia& other) {
290 storage_ = other.storage_;
291 return *this;
294 ImageSkia::~ImageSkia() {
297 // static
298 void ImageSkia::SetSupportedScales(const std::vector<float>& supported_scales) {
299 if (g_supported_scales != NULL)
300 delete g_supported_scales;
301 g_supported_scales = new std::vector<float>(supported_scales);
302 std::sort(g_supported_scales->begin(), g_supported_scales->end());
305 // static
306 const std::vector<float>& ImageSkia::GetSupportedScales() {
307 DCHECK(g_supported_scales != NULL);
308 return *g_supported_scales;
311 // static
312 float ImageSkia::GetMaxSupportedScale() {
313 return g_supported_scales->back();
316 // static
317 ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) {
318 return ImageSkia(ImageSkiaRep(bitmap, 0.0f));
321 scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const {
322 ImageSkia* copy = new ImageSkia;
323 if (isNull())
324 return scoped_ptr<ImageSkia>(copy);
326 CHECK(CanRead());
328 std::vector<gfx::ImageSkiaRep>& reps = storage_->image_reps();
329 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin();
330 iter != reps.end(); ++iter) {
331 copy->AddRepresentation(*iter);
333 // The copy has its own storage. Detach the copy from the current
334 // thread so that other thread can use this.
335 if (!copy->isNull())
336 copy->storage_->DetachFromThread();
337 return scoped_ptr<ImageSkia>(copy);
340 bool ImageSkia::BackedBySameObjectAs(const gfx::ImageSkia& other) const {
341 return storage_.get() == other.storage_.get();
344 void ImageSkia::AddRepresentation(const ImageSkiaRep& image_rep) {
345 DCHECK(!image_rep.is_null());
347 // TODO(oshima): This method should be called |SetRepresentation|
348 // and replace the existing rep if there is already one with the
349 // same scale so that we can guarantee that a ImageSkia instance contains only
350 // one image rep per scale. This is not possible now as ImageLoader currently
351 // stores need this feature, but this needs to be fixed.
352 if (isNull()) {
353 Init(image_rep);
354 } else {
355 CHECK(CanModify());
356 // If someone is adding ImageSkia explicitly, check if we should
357 // make the image high DPI aware.
358 storage_->AddRepresentation(image_rep);
362 void ImageSkia::RemoveRepresentation(float scale) {
363 if (isNull())
364 return;
365 CHECK(CanModify());
367 ImageSkiaReps& image_reps = storage_->image_reps();
368 ImageSkiaReps::iterator it =
369 storage_->FindRepresentation(scale, false);
370 if (it != image_reps.end() && it->scale() == scale)
371 image_reps.erase(it);
374 bool ImageSkia::HasRepresentation(float scale) const {
375 if (isNull())
376 return false;
377 CHECK(CanRead());
379 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, false);
380 return (it != storage_->image_reps().end() && it->scale() == scale);
383 const ImageSkiaRep& ImageSkia::GetRepresentation(float scale) const {
384 if (isNull())
385 return NullImageRep();
387 CHECK(CanRead());
389 ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, true);
390 if (it == storage_->image_reps().end())
391 return NullImageRep();
393 return *it;
396 void ImageSkia::SetReadOnly() {
397 CHECK(storage_.get());
398 storage_->SetReadOnly();
399 DetachStorageFromThread();
402 void ImageSkia::MakeThreadSafe() {
403 CHECK(storage_.get());
404 EnsureRepsForSupportedScales();
405 // Delete source as we no longer needs it.
406 if (storage_.get())
407 storage_->DeleteSource();
408 storage_->SetReadOnly();
409 CHECK(IsThreadSafe());
412 bool ImageSkia::IsThreadSafe() const {
413 return !storage_.get() || (storage_->read_only() && !storage_->has_source());
416 int ImageSkia::width() const {
417 return isNull() ? 0 : storage_->size().width();
420 gfx::Size ImageSkia::size() const {
421 return gfx::Size(width(), height());
424 int ImageSkia::height() const {
425 return isNull() ? 0 : storage_->size().height();
428 std::vector<ImageSkiaRep> ImageSkia::image_reps() const {
429 if (isNull())
430 return std::vector<ImageSkiaRep>();
432 CHECK(CanRead());
434 ImageSkiaReps internal_image_reps = storage_->image_reps();
435 // Create list of image reps to return, skipping null image reps which were
436 // added for caching purposes only.
437 ImageSkiaReps image_reps;
438 for (ImageSkiaReps::iterator it = internal_image_reps.begin();
439 it != internal_image_reps.end(); ++it) {
440 if (!it->is_null())
441 image_reps.push_back(*it);
444 return image_reps;
447 void ImageSkia::EnsureRepsForSupportedScales() const {
448 DCHECK(g_supported_scales != NULL);
449 // Don't check ReadOnly because the source may generate images
450 // even for read only ImageSkia. Concurrent access will be protected
451 // by |DCHECK(CalledOnValidThread())| in FindRepresentation.
452 if (storage_.get() && storage_->has_source()) {
453 for (std::vector<float>::const_iterator it = g_supported_scales->begin();
454 it != g_supported_scales->end(); ++it)
455 storage_->FindRepresentation(*it, true);
459 void ImageSkia::Init(const ImageSkiaRep& image_rep) {
460 // TODO(pkotwicz): The image should be null whenever image rep is null.
461 if (image_rep.sk_bitmap().empty()) {
462 storage_ = NULL;
463 return;
465 storage_ = new internal::ImageSkiaStorage(
466 NULL, gfx::Size(image_rep.GetWidth(), image_rep.GetHeight()));
467 storage_->image_reps().push_back(image_rep);
470 SkBitmap& ImageSkia::GetBitmap() const {
471 if (isNull()) {
472 // Callers expect a ImageSkiaRep even if it is |isNull()|.
473 // TODO(pkotwicz): Fix this.
474 return NullImageRep().mutable_sk_bitmap();
477 // TODO(oshima): This made a few tests flaky on Windows.
478 // Fix the root cause and re-enable this. crbug.com/145623.
479 #if !defined(OS_WIN)
480 CHECK(CanRead());
481 #endif
483 ImageSkiaReps::iterator it = storage_->FindRepresentation(1.0f, true);
484 if (it != storage_->image_reps().end())
485 return it->mutable_sk_bitmap();
486 return NullImageRep().mutable_sk_bitmap();
489 bool ImageSkia::CanRead() const {
490 return !storage_.get() || storage_->CanRead();
493 bool ImageSkia::CanModify() const {
494 return !storage_.get() || storage_->CanModify();
497 void ImageSkia::DetachStorageFromThread() {
498 if (storage_.get())
499 storage_->DetachFromThread();
502 } // namespace gfx