Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Unified Diff: ui/gfx/paint_vector_icon.cc

Issue 1266463002: Save vector icons in a cache (a la ResourceBundle). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: no base Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/paint_vector_icon.cc
diff --git a/ui/gfx/paint_vector_icon.cc b/ui/gfx/paint_vector_icon.cc
index 358f87260d785e147e60ad67b71685ca2ff8f332..fe17ab6c7c95e0c3145ff5fce4520e3b322c7991 100644
--- a/ui/gfx/paint_vector_icon.cc
+++ b/ui/gfx/paint_vector_icon.cc
@@ -4,6 +4,9 @@
#include "ui/gfx/paint_vector_icon.h"
+#include <map>
+
+#include "base/lazy_instance.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/vector_icon_types.h"
@@ -36,6 +39,53 @@ class VectorIconSource : public CanvasImageSource {
DISALLOW_COPY_AND_ASSIGN(VectorIconSource);
};
+// This class caches vector icons (as ImageSkia) so they don't have to be drawn
+// more than once. This also guarantees the backing data for the images returned
+// by CreateVectorIcon will persist in memory until program termination.
+class VectorIconCache {
+ public:
+ VectorIconCache() {}
+ ~VectorIconCache() {}
+
+ ImageSkia GetOrCreateIcon(VectorIconId id, size_t dip_size, SkColor color) {
+ IconDescription description(id, dip_size, color);
+ auto iter = images_.find(description);
+ if (iter != images_.end())
+ return iter->second;
+
+ ImageSkia icon(
+ new VectorIconSource(id, dip_size, color),
+ gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)));
+ images_.insert(std::make_pair(description, icon));
+ return icon;
+ }
+
+ private:
+ struct IconDescription {
+ IconDescription(VectorIconId id, size_t dip_size, SkColor color)
+ : id(id), dip_size(dip_size), color(color) {}
+
+ bool operator<(const IconDescription& other) const {
+ if (id != other.id)
+ return id < other.id;
+ if (dip_size != other.dip_size)
+ return dip_size < other.dip_size;
+ return color < other.color;
+ }
+
+ VectorIconId id;
+ size_t dip_size;
+ SkColor color;
+ };
+
+ std::map<IconDescription, ImageSkia> images_;
+
+ DISALLOW_COPY_AND_ASSIGN(VectorIconCache);
+};
+
+static base::LazyInstance<VectorIconCache> g_icon_cache =
+ LAZY_INSTANCE_INITIALIZER;
+
} // namespace
void PaintVectorIcon(Canvas* canvas,
@@ -147,9 +197,7 @@ void PaintVectorIcon(Canvas* canvas,
}
ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) {
- return ImageSkia(
- new VectorIconSource(id, dip_size, color),
- gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)));
+ return g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color);
}
} // namespace gfx
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698