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

Unified Diff: src/core/SkBitmapCache.cpp

Issue 483493003: expose generalized imagecache key (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 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
Index: src/core/SkBitmapCache.cpp
diff --git a/src/core/SkBitmapCache.cpp b/src/core/SkBitmapCache.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..96d3d59e1bd794c586e2ea0981be99f32a66a640
--- /dev/null
+++ b/src/core/SkBitmapCache.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmapCache.h"
+#include "SkRect.h"
+
+/**
+ This function finds the bounds of the bitmap *within its pixelRef*.
+ If the bitmap lacks a pixelRef, it will return an empty rect, since
+ that doesn't make sense. This may be a useful enough function that
+ it should be somewhere else (in SkBitmap?).
+ */
+static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) {
+ if (!(bm.pixelRef())) {
+ return SkIRect::MakeEmpty();
+ }
+ SkIPoint origin = bm.pixelRefOrigin();
+ return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height());
+}
+
+struct GenWHKey : public SkScaledImageCache::Key {
mtklein 2014/08/21 15:29:18 Might want to consider just CacheKey? GenWHKey is
reed1 2014/08/21 15:58:10 Done.
+public:
+ GenWHKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds)
+ : fGenID(genID)
+ , fScaleX(scaleX)
+ , fScaleY(scaleY)
+ , fBounds(bounds)
+ {
+ this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds));
+ }
+
+ uint32_t fGenID;
+ SkScalar fScaleX;
+ SkScalar fScaleY;
+ SkIRect fBounds;
+};
+
+//////////////////////////////////////////////////////////////////////////////////////////
+
+SkScaledImageCache::ID* SkBitmapCache::FindAndLock(const SkBitmap& src,
+ SkScalar invScaleX, SkScalar invScaleY,
+ SkBitmap* result) {
+ if (0 == invScaleX || 0 == invScaleY) {
+ // degenerate, and the key we use for mipmaps
+ return NULL;
+ }
+ GenWHKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
+ return SkScaledImageCache::FindAndLock(key, result);
+}
+
+SkScaledImageCache::ID* SkBitmapCache::AddAndLock(const SkBitmap& src,
+ SkScalar invScaleX, SkScalar invScaleY,
+ const SkBitmap& result) {
+ if (0 == invScaleX || 0 == invScaleY) {
+ // degenerate, and the key we use for mipmaps
+ return NULL;
+ }
+ GenWHKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
+ return SkScaledImageCache::AddAndLock(key, result);
+}
+
+////
+
+SkScaledImageCache::ID* SkBitmapCache::FindAndLock(uint32_t genID, int width, int height,
+ SkBitmap* result) {
+ GenWHKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height));
+ return SkScaledImageCache::FindAndLock(key, result);
+}
+
+SkScaledImageCache::ID* SkBitmapCache::AddAndLock(uint32_t genID, int width, int height,
+ const SkBitmap& result) {
+ GenWHKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height));
+ return SkScaledImageCache::AddAndLock(key, result);
+}
+
+////
+
+SkScaledImageCache::ID* SkMipMapCache::FindAndLock(const SkBitmap& src, SkMipMap const** result) {
+ GenWHKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from_bitmap(src));
+ return SkScaledImageCache::FindAndLock(key, result);
+}
+
+SkScaledImageCache::ID* SkMipMapCache::AddAndLock(const SkBitmap& src, const SkMipMap* result) {
+ GenWHKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from_bitmap(src));
+ return SkScaledImageCache::AddAndLock(key, result);
+}
+

Powered by Google App Engine
This is Rietveld 408576698