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"
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"
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
38 const float kFallbackToSmallerScaleDiff
= 0.20f
;
47 explicit Matcher(float scale
) : scale_(scale
) {
50 bool operator()(const ImageSkiaRep
& rep
) const {
51 return rep
.scale() == scale_
;
58 ImageSkiaRep
ScaleImageSkiaRep(const ImageSkiaRep
& rep
, float target_scale
) {
59 DCHECK_NE(rep
.scale(), target_scale
);
63 gfx::Size scaled_size
= ToCeiledSize(
64 gfx::ScaleSize(rep
.pixel_size(), target_scale
/ rep
.scale()));
65 return ImageSkiaRep(skia::ImageOperations::Resize(
67 skia::ImageOperations::RESIZE_LANCZOS3
,
69 scaled_size
.height()), target_scale
);
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
{
83 ImageSkiaStorage(ImageSkiaSource
* source
, const gfx::Size
& size
)
89 ImageSkiaStorage(ImageSkiaSource
* source
, float scale
)
92 ImageSkia::ImageSkiaReps::iterator it
= FindRepresentation(scale
, true);
93 if (it
== image_reps_
.end() || it
->is_null())
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() {
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();
137 if (it
->unscaled()) {
138 DCHECK_EQ(1.0f
, it
->scale());
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
) {
173 fetch_new_image
= false;
179 float diff
= std::abs(it
->scale() - scale
);
180 if (diff
< smallest_diff
&& !it
->is_null()) {
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.";
191 float resource_scale
= scale
;
192 if (g_supported_scales
) {
193 if (g_supported_scales
->back() <= scale
) {
194 resource_scale
= g_supported_scales
->back();
196 for (size_t i
= 0; i
< g_supported_scales
->size(); ++i
) {
197 if ((*g_supported_scales
)[i
] + kFallbackToSmallerScaleDiff
>=
199 resource_scale
= (*g_supported_scales
)[i
];
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
);
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
;
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.
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.
256 friend class base::RefCountedThreadSafe
<ImageSkiaStorage
>;
261 ImageSkia::ImageSkia() : storage_(NULL
) {
264 ImageSkia::ImageSkia(ImageSkiaSource
* source
, const gfx::Size
& size
)
265 : storage_(new internal::ImageSkiaStorage(source
, size
)) {
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
)) {
274 if (!storage_
->has_source())
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
) {
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_
;
294 ImageSkia::~ImageSkia() {
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());
306 const std::vector
<float>& ImageSkia::GetSupportedScales() {
307 DCHECK(g_supported_scales
!= NULL
);
308 return *g_supported_scales
;
312 float ImageSkia::GetMaxSupportedScale() {
313 return g_supported_scales
->back();
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
;
324 return scoped_ptr
<ImageSkia
>(copy
);
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.
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.
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
) {
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 {
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 {
385 return NullImageRep();
389 ImageSkiaReps::iterator it
= storage_
->FindRepresentation(scale
, true);
390 if (it
== storage_
->image_reps().end())
391 return NullImageRep();
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.
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 {
430 return std::vector
<ImageSkiaRep
>();
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
) {
441 image_reps
.push_back(*it
);
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()) {
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 {
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.
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() {
499 storage_
->DetachFromThread();