Index: ui/gfx/image/image_skia.cc |
diff --git a/ui/gfx/image/image_skia.cc b/ui/gfx/image/image_skia.cc |
index 1ae9874ad4d36b0b1c68f73c894adf5293518389..a283117db2415488f3259cb3986fa956c0031a47 100644 |
--- a/ui/gfx/image/image_skia.cc |
+++ b/ui/gfx/image/image_skia.cc |
@@ -26,6 +26,7 @@ gfx::ImageSkiaRep& NullImageRep() { |
return null_image_rep; |
} |
+std::vector<float>* g_supported_scales = NULL; |
} // namespace |
namespace internal { |
@@ -33,15 +34,15 @@ namespace { |
class Matcher { |
public: |
- explicit Matcher(ui::ScaleFactor scale_factor) : scale_factor_(scale_factor) { |
+ explicit Matcher(float scale) : scale_(scale) { |
} |
bool operator()(const ImageSkiaRep& rep) const { |
- return rep.scale_factor() == scale_factor_; |
+ return rep.scale() == scale_; |
} |
private: |
- ui::ScaleFactor scale_factor_; |
+ float scale_; |
}; |
} // namespace |
@@ -61,11 +62,10 @@ class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>, |
read_only_(false) { |
} |
- ImageSkiaStorage(ImageSkiaSource* source, ui::ScaleFactor scale_factor) |
+ ImageSkiaStorage(ImageSkiaSource* source, float scale) |
: source_(source), |
read_only_(false) { |
- ImageSkia::ImageSkiaReps::iterator it = |
- FindRepresentation(scale_factor, true); |
+ ImageSkia::ImageSkiaReps::iterator it = FindRepresentation(scale, true); |
if (it == image_reps_.end() || it->is_null()) |
source_.reset(); |
else |
@@ -103,16 +103,15 @@ class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>, |
} |
// Returns the iterator of the image rep whose density best matches |
- // |scale_factor|. If the image for the |scale_factor| doesn't exist |
- // in the storage and |storage| is set, it fetches new image by calling |
- // |ImageSkiaSource::GetImageForScale|. If the source returns the |
- // image with different scale factor (if the image doesn't exist in |
- // resource, for example), it will fallback to closest image rep. |
+ // |scale|. If the image for the |scale| doesn't exist in the storage and |
+ // |storage| is set, it fetches new image by calling |
+ // |ImageSkiaSource::GetImageForScale|. If the source returns the image with |
+ // different scale (if the image doesn't exist in resource, for example), it |
+ // will fallback to closest image rep. |
std::vector<ImageSkiaRep>::iterator FindRepresentation( |
- ui::ScaleFactor scale_factor, bool fetch_new_image) const { |
+ float scale, bool fetch_new_image) const { |
ImageSkiaStorage* non_const = const_cast<ImageSkiaStorage*>(this); |
- float scale = ui::GetScaleFactorScale(scale_factor); |
ImageSkia::ImageSkiaReps::iterator closest_iter = |
non_const->image_reps().end(); |
ImageSkia::ImageSkiaReps::iterator exact_iter = |
@@ -121,7 +120,7 @@ class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>, |
for (ImageSkia::ImageSkiaReps::iterator it = |
non_const->image_reps().begin(); |
it < image_reps_.end(); ++it) { |
- if (it->GetScale() == scale) { |
+ if (it->scale() == scale) { |
// found exact match |
fetch_new_image = false; |
if (it->is_null()) |
@@ -129,7 +128,7 @@ class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>, |
exact_iter = it; |
break; |
} |
- float diff = std::abs(it->GetScale() - scale); |
+ float diff = std::abs(it->scale() - scale); |
if (diff < smallest_diff && !it->is_null()) { |
closest_iter = it; |
smallest_diff = diff; |
@@ -140,25 +139,24 @@ class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>, |
DCHECK(CalledOnValidThread()) << |
"An ImageSkia with the source must be accessed by the same thread."; |
- ImageSkiaRep image = source_->GetImageForScale(scale_factor); |
+ ImageSkiaRep image = source_->GetImageForScale(scale); |
// If the source returned the new image, store it. |
if (!image.is_null() && |
std::find_if(image_reps_.begin(), image_reps_.end(), |
- Matcher(image.scale_factor())) == image_reps_.end()) { |
+ Matcher(image.scale())) == image_reps_.end()) { |
non_const->image_reps().push_back(image); |
} |
- // If the result image's scale factor isn't same as the expected |
- // scale factor, create null ImageSkiaRep with the |scale_factor| |
- // so that the next lookup will fallback to the closest scale. |
- if (image.is_null() || image.scale_factor() != scale_factor) { |
- non_const->image_reps().push_back( |
- ImageSkiaRep(SkBitmap(), scale_factor)); |
+ // If the result image's scale isn't same as the expected scale, create |
+ // null ImageSkiaRep with the |scale| so that the next lookup will |
+ // fallback to the closest scale. |
+ if (image.is_null() || image.scale() != scale) { |
+ non_const->image_reps().push_back(ImageSkiaRep(SkBitmap(), scale)); |
} |
// image_reps_ must have the exact much now, so find again. |
- return FindRepresentation(scale_factor, false); |
+ return FindRepresentation(scale, false); |
} |
return exact_iter != image_reps_.end() ? exact_iter : closest_iter; |
} |
@@ -170,7 +168,7 @@ class ImageSkiaStorage : public base::RefCountedThreadSafe<ImageSkiaStorage>, |
DetachFromThread(); |
} |
- // Vector of bitmaps and their associated scale factor. |
+ // Vector of bitmaps and their associated scale. |
std::vector<gfx::ImageSkiaRep> image_reps_; |
scoped_ptr<ImageSkiaSource> source_; |
@@ -195,8 +193,8 @@ ImageSkia::ImageSkia(ImageSkiaSource* source, const gfx::Size& size) |
DetachStorageFromThread(); |
} |
-ImageSkia::ImageSkia(ImageSkiaSource* source, ui::ScaleFactor scale_factor) |
- : storage_(new internal::ImageSkiaStorage(source, scale_factor)) { |
+ImageSkia::ImageSkia(ImageSkiaSource* source, float scale) |
+ : storage_(new internal::ImageSkiaStorage(source, scale)) { |
DCHECK(source); |
if (!storage_->has_source()) |
storage_ = NULL; |
@@ -222,8 +220,27 @@ ImageSkia::~ImageSkia() { |
} |
// static |
+void ImageSkia::SetSupportedScales(const std::vector<float>& supported_scales) { |
+ if (g_supported_scales != NULL) |
+ delete g_supported_scales; |
+ g_supported_scales = new std::vector<float>(supported_scales); |
+ std::sort(g_supported_scales->begin(), g_supported_scales->end()); |
+} |
+ |
+// static |
+const std::vector<float>& ImageSkia::GetSupportedScales() { |
+ DCHECK(g_supported_scales != NULL); |
+ return *g_supported_scales; |
+} |
+ |
+// static |
+float ImageSkia::GetMaxSupportedScale() { |
+ return g_supported_scales->back(); |
+} |
+ |
+// static |
ImageSkia ImageSkia::CreateFrom1xBitmap(const SkBitmap& bitmap) { |
- return ImageSkia(ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P)); |
+ return ImageSkia(ImageSkiaRep(bitmap, 1.0f)); |
} |
scoped_ptr<ImageSkia> ImageSkia::DeepCopy() const { |
@@ -254,10 +271,9 @@ void ImageSkia::AddRepresentation(const ImageSkiaRep& image_rep) { |
// TODO(oshima): This method should be called |SetRepresentation| |
// and replace the existing rep if there is already one with the |
- // same scale factor so that we can guarantee that a ImageSkia |
- // instance contians only one image rep per scale factor. This is |
- // not possible now as ImageLoader currently stores need |
- // this feature, but this needs to be fixed. |
+ // same scale so that we can guarantee that a ImageSkia instance contains only |
+ // one image rep per scale. This is not possible now as ImageLoader currently |
+ // stores need this feature, but this needs to be fixed. |
if (isNull()) { |
Init(image_rep); |
} else { |
@@ -266,37 +282,34 @@ void ImageSkia::AddRepresentation(const ImageSkiaRep& image_rep) { |
} |
} |
-void ImageSkia::RemoveRepresentation(ui::ScaleFactor scale_factor) { |
+void ImageSkia::RemoveRepresentation(float scale) { |
if (isNull()) |
return; |
CHECK(CanModify()); |
ImageSkiaReps& image_reps = storage_->image_reps(); |
ImageSkiaReps::iterator it = |
- storage_->FindRepresentation(scale_factor, false); |
- if (it != image_reps.end() && it->scale_factor() == scale_factor) |
+ storage_->FindRepresentation(scale, false); |
+ if (it != image_reps.end() && it->scale() == scale) |
image_reps.erase(it); |
} |
-bool ImageSkia::HasRepresentation(ui::ScaleFactor scale_factor) const { |
+bool ImageSkia::HasRepresentation(float scale) const { |
if (isNull()) |
return false; |
CHECK(CanRead()); |
- ImageSkiaReps::iterator it = |
- storage_->FindRepresentation(scale_factor, false); |
- return (it != storage_->image_reps().end() && |
- it->scale_factor() == scale_factor); |
+ ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, false); |
+ return (it != storage_->image_reps().end() && it->scale() == scale); |
} |
-const ImageSkiaRep& ImageSkia::GetRepresentation( |
- ui::ScaleFactor scale_factor) const { |
+const ImageSkiaRep& ImageSkia::GetRepresentation(float scale) const { |
if (isNull()) |
return NullImageRep(); |
CHECK(CanRead()); |
- ImageSkiaReps::iterator it = storage_->FindRepresentation(scale_factor, true); |
+ ImageSkiaReps::iterator it = storage_->FindRepresentation(scale, true); |
if (it == storage_->image_reps().end()) |
return NullImageRep(); |
@@ -311,7 +324,7 @@ void ImageSkia::SetReadOnly() { |
void ImageSkia::MakeThreadSafe() { |
CHECK(storage_.get()); |
- EnsureRepsForSupportedScaleFactors(); |
+ EnsureRepsForSupportedScales(); |
// Delete source as we no longer needs it. |
if (storage_.get()) |
storage_->DeleteSource(); |
@@ -354,15 +367,15 @@ std::vector<ImageSkiaRep> ImageSkia::image_reps() const { |
return image_reps; |
} |
-void ImageSkia::EnsureRepsForSupportedScaleFactors() const { |
+void ImageSkia::EnsureRepsForSupportedScales() const { |
+ DCHECK(g_supported_scales != NULL); |
// Don't check ReadOnly because the source may generate images |
// even for read only ImageSkia. Concurrent access will be protected |
// by |DCHECK(CalledOnValidThread())| in FindRepresentation. |
if (storage_.get() && storage_->has_source()) { |
- std::vector<ui::ScaleFactor> supported_scale_factors = |
- ui::GetSupportedScaleFactors(); |
- for (size_t i = 0; i < supported_scale_factors.size(); ++i) |
- storage_->FindRepresentation(supported_scale_factors[i], true); |
+ for (std::vector<float>::const_iterator it = g_supported_scales->begin(); |
+ it != g_supported_scales->end(); ++it) |
+ storage_->FindRepresentation(*it, true); |
} |
} |
@@ -390,8 +403,7 @@ SkBitmap& ImageSkia::GetBitmap() const { |
CHECK(CanRead()); |
#endif |
- ImageSkiaReps::iterator it = |
- storage_->FindRepresentation(ui::SCALE_FACTOR_100P, true); |
+ ImageSkiaReps::iterator it = storage_->FindRepresentation(1.0f, true); |
if (it != storage_->image_reps().end()) |
return it->mutable_sk_bitmap(); |
return NullImageRep().mutable_sk_bitmap(); |