Chromium Code Reviews| Index: ui/gfx/paint_vector_icon.cc |
| diff --git a/ui/gfx/paint_vector_icon.cc b/ui/gfx/paint_vector_icon.cc |
| index eeca3f406925e366fdd0d661e6b154266e60bea8..5d567e52bdbb3bcfe7d784571492310c780df2da 100644 |
| --- a/ui/gfx/paint_vector_icon.cc |
| +++ b/ui/gfx/paint_vector_icon.cc |
| @@ -320,7 +320,9 @@ class VectorIconSource : public CanvasImageSource { |
| false), |
| id_(id), |
| color_(color), |
| - badge_id_(badge_id) {} |
| + badge_id_(badge_id), |
| + icon_(nullptr), |
| + badge_(nullptr) {} |
| VectorIconSource(const std::string& definition, |
| size_t dip_size, |
| @@ -331,16 +333,40 @@ class VectorIconSource : public CanvasImageSource { |
| id_(VectorIconId::VECTOR_ICON_NONE), |
| path_(PathFromSource(definition)), |
| color_(color), |
| - badge_id_(VectorIconId::VECTOR_ICON_NONE) {} |
| + badge_id_(VectorIconId::VECTOR_ICON_NONE), |
| + icon_(nullptr), |
| + badge_(nullptr) {} |
| + |
| + VectorIconSource(const VectorIcon* icon, |
| + size_t dip_size, |
| + SkColor color, |
| + const VectorIcon* badge_icon) |
| + : CanvasImageSource( |
| + gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), |
| + false), |
| + id_(VectorIconId::VECTOR_ICON_NONE), |
| + color_(color), |
| + badge_id_(VectorIconId::VECTOR_ICON_NONE), |
| + icon_(icon), |
| + badge_(badge_icon) {} |
| ~VectorIconSource() override {} |
| // CanvasImageSource: |
| bool HasRepresentationAtAllScales() const override { |
| + if (icon_) |
| + return icon_; |
| return id_ != VectorIconId::VECTOR_ICON_NONE; |
| } |
| void Draw(gfx::Canvas* canvas) override { |
| + if (icon_) { |
| + PaintVectorIcon(canvas, icon_, size_.width(), color_); |
| + if (badge_) |
| + PaintVectorIcon(canvas, badge_, size_.width(), color_); |
| + return; |
| + } |
| + |
| if (path_.empty()) { |
| PaintVectorIcon(canvas, id_, size_.width(), color_); |
| if (badge_id_ != VectorIconId::VECTOR_ICON_NONE) |
| @@ -356,6 +382,9 @@ class VectorIconSource : public CanvasImageSource { |
| const SkColor color_; |
| const VectorIconId badge_id_; |
| + const VectorIcon* icon_; |
| + const VectorIcon* badge_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(VectorIconSource); |
| }; |
| @@ -367,6 +396,58 @@ class VectorIconCache { |
| VectorIconCache() {} |
| ~VectorIconCache() {} |
| + ImageSkia GetOrCreateIcon(const VectorIcon* icon, |
| + size_t dip_size, |
| + SkColor color, |
| + const VectorIcon* badge_icon) { |
| + IconDescription description(icon, dip_size, color, badge_icon); |
| + auto iter = images_.find(description); |
| + if (iter != images_.end()) |
| + return iter->second; |
| + |
| + ImageSkia icon_image( |
| + new VectorIconSource(icon, dip_size, color, badge_icon), |
| + gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); |
| + images_.insert(std::make_pair(description, icon_image)); |
| + return icon_image; |
| + } |
| + |
| + private: |
| + struct IconDescription { |
| + IconDescription(const VectorIcon* icon, |
| + size_t dip_size, |
| + SkColor color, |
| + const VectorIcon* badge_icon) |
| + : icon(icon), |
| + dip_size(dip_size), |
| + color(color), |
| + badge_icon(badge_icon) {} |
| + |
| + bool operator<(const IconDescription& other) const { |
| + return std::tie(icon, dip_size, color, badge_icon) < |
| + std::tie(other.icon, other.dip_size, other.color, |
| + other.badge_icon); |
| + } |
| + |
| + const gfx::VectorIcon* icon; |
| + size_t dip_size; |
| + SkColor color; |
| + const gfx::VectorIcon* badge_icon; |
| + }; |
| + |
| + std::map<IconDescription, ImageSkia> images_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(VectorIconCache); |
| +}; |
| + |
| +static base::LazyInstance<VectorIconCache> g_icon_cache = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +class VectorIconCacheLegacy { |
| + public: |
| + VectorIconCacheLegacy() {} |
| + ~VectorIconCacheLegacy() {} |
| + |
| ImageSkia GetOrCreateIcon(VectorIconId id, |
| size_t dip_size, |
| SkColor color, |
| @@ -404,10 +485,10 @@ class VectorIconCache { |
| std::map<IconDescription, ImageSkia> images_; |
| - DISALLOW_COPY_AND_ASSIGN(VectorIconCache); |
| + DISALLOW_COPY_AND_ASSIGN(VectorIconCacheLegacy); |
| }; |
| -static base::LazyInstance<VectorIconCache> g_icon_cache = |
| +static base::LazyInstance<VectorIconCacheLegacy> g_icon_cache_legacy = |
| LAZY_INSTANCE_INITIALIZER; |
| } // namespace |
| @@ -423,6 +504,17 @@ void PaintVectorIcon(Canvas* canvas, |
| PaintPath(canvas, path, dip_size, color); |
| } |
| +void PaintVectorIcon(Canvas* canvas, |
| + const VectorIcon* icon, |
| + size_t dip_size, |
| + SkColor color) { |
| + DCHECK(icon); |
| + const PathElement* path = (canvas->image_scale() == 1.f && icon->path_1x) |
| + ? icon->path_1x |
| + : icon->path; |
| + PaintPath(canvas, path, dip_size, color); |
| +} |
| + |
| ImageSkia CreateVectorIcon(VectorIconId id, SkColor color) { |
| const PathElement* one_x_path = GetPathForVectorIconAt1xScale(id); |
| size_t size = one_x_path[0].type == CANVAS_DIMENSIONS ? one_x_path[1].arg |
| @@ -430,19 +522,41 @@ ImageSkia CreateVectorIcon(VectorIconId id, SkColor color) { |
| return CreateVectorIcon(id, size, color); |
| } |
| +ImageSkia CreateVectorIcon(const VectorIcon* icon, SkColor color) { |
| + const PathElement* one_x_path = icon->path_1x ? icon->path_1x : icon->path; |
| + size_t size = one_x_path[0].type == CANVAS_DIMENSIONS ? one_x_path[1].arg |
| + : kReferenceSizeDip; |
|
tdanderson
2016/08/19 21:23:35
Alternatively, we could do this work upfront and a
sadrul
2016/08/22 15:14:05
Yeah, that sounds like a good option. (may be a ba
tdanderson
2016/08/25 21:54:32
Will keep in mind as part of follow-up.
|
| + return CreateVectorIcon(icon, size, color); |
| +} |
| + |
| ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) { |
| return CreateVectorIconWithBadge(id, dip_size, color, |
| VectorIconId::VECTOR_ICON_NONE); |
| } |
| +ImageSkia CreateVectorIcon(const VectorIcon* icon, |
| + size_t dip_size, |
| + SkColor color) { |
| + return CreateVectorIconWithBadge(icon, dip_size, color, nullptr); |
| +} |
| + |
| ImageSkia CreateVectorIconWithBadge(VectorIconId id, |
| size_t dip_size, |
| SkColor color, |
| VectorIconId badge_id) { |
| return (id == VectorIconId::VECTOR_ICON_NONE) |
| ? gfx::ImageSkia() |
| - : g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color, |
| - badge_id); |
| + : g_icon_cache_legacy.Get().GetOrCreateIcon(id, dip_size, color, |
| + badge_id); |
| +} |
| + |
| +ImageSkia CreateVectorIconWithBadge(const VectorIcon* icon, |
| + size_t dip_size, |
| + SkColor color, |
| + const VectorIcon* badge_icon) { |
| + if (!icon) |
| + return gfx::ImageSkia(); |
| + return g_icon_cache.Get().GetOrCreateIcon(icon, dip_size, color, badge_icon); |
| } |
| ImageSkia CreateVectorIconFromSource(const std::string& source, |
|
tdanderson
2016/08/19 21:23:35
As a side note, vector_example (in views_examples)
|