| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
| 9 #include "SkResourceCache.h" | 9 #include "SkResourceCache.h" |
| 10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 } | 41 } |
| 42 SkIPoint origin = bm.pixelRefOrigin(); | 42 SkIPoint origin = bm.pixelRefOrigin(); |
| 43 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); | 43 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 static unsigned gBitmapKeyNamespaceLabel; | 47 static unsigned gBitmapKeyNamespaceLabel; |
| 48 | 48 |
| 49 struct BitmapKey : public SkResourceCache::Key { | 49 struct BitmapKey : public SkResourceCache::Key { |
| 50 public: | 50 public: |
| 51 BitmapKey(uint32_t genID, SkScalar sx, SkScalar sy, const SkIRect& bounds) | 51 BitmapKey(uint32_t genID, int width, int height, const SkIRect& bounds) |
| 52 : fGenID(genID) | 52 : fGenID(genID) |
| 53 , fScaleX(sx) | 53 , fWidth(width) |
| 54 , fScaleY(sy) | 54 , fHeight(height) |
| 55 , fBounds(bounds) | 55 , fBounds(bounds) |
| 56 { | 56 { |
| 57 this->init(&gBitmapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitm
ap(genID), | 57 this->init(&gBitmapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitm
ap(genID), |
| 58 sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(f
Bounds)); | 58 sizeof(fGenID) + sizeof(fWidth) + sizeof(fHeight) + sizeof(fB
ounds)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 uint32_t fGenID; | 61 const uint32_t fGenID; |
| 62 SkScalar fScaleX; | 62 const int fWidth; |
| 63 SkScalar fScaleY; | 63 const int fHeight; |
| 64 SkIRect fBounds; | 64 const SkIRect fBounds; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 struct BitmapRec : public SkResourceCache::Rec { | 67 struct BitmapRec : public SkResourceCache::Rec { |
| 68 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds, | 68 BitmapRec(uint32_t genID, int width, int height, const SkIRect& bounds, |
| 69 const SkBitmap& result) | 69 const SkBitmap& result) |
| 70 : fKey(genID, scaleX, scaleY, bounds) | 70 : fKey(genID, width, height, bounds) |
| 71 , fBitmap(result) | 71 , fBitmap(result) |
| 72 {} | 72 {} |
| 73 | 73 |
| 74 const Key& getKey() const override { return fKey; } | 74 const Key& getKey() const override { return fKey; } |
| 75 size_t bytesUsed() const override { return sizeof(fKey) + fBitmap.getSize();
} | 75 size_t bytesUsed() const override { return sizeof(fKey) + fBitmap.getSize();
} |
| 76 | 76 |
| 77 const char* getCategory() const override { return "bitmap"; } | 77 const char* getCategory() const override { return "bitmap"; } |
| 78 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { | 78 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { |
| 79 return fBitmap.pixelRef()->diagnostic_only_getDiscardable(); | 79 return fBitmap.pixelRef()->diagnostic_only_getDiscardable(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 static bool Finder(const SkResourceCache::Rec& baseRec, void* contextBitmap)
{ | 82 static bool Finder(const SkResourceCache::Rec& baseRec, void* contextBitmap)
{ |
| 83 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); | 83 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); |
| 84 SkBitmap* result = (SkBitmap*)contextBitmap; | 84 SkBitmap* result = (SkBitmap*)contextBitmap; |
| 85 | 85 |
| 86 *result = rec.fBitmap; | 86 *result = rec.fBitmap; |
| 87 result->lockPixels(); | 87 result->lockPixels(); |
| 88 return SkToBool(result->getPixels()); | 88 return SkToBool(result->getPixels()); |
| 89 } | 89 } |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 BitmapKey fKey; | 92 BitmapKey fKey; |
| 93 SkBitmap fBitmap; | 93 SkBitmap fBitmap; |
| 94 }; | 94 }; |
| 95 } // namespace | 95 } // namespace |
| 96 | 96 |
| 97 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ | 97 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ |
| 98 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global
Name(__VA_ARGS__)) | 98 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global
Name(__VA_ARGS__)) |
| 99 | 99 |
| 100 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, SkBitmap* result, | 100 bool SkBitmapCache::FindWH(const SkBitmap& src, int width, int height, SkBitmap*
result, |
| 101 SkResourceCache* localCache) { | 101 SkResourceCache* localCache) { |
| 102 if (0 == invScaleX || 0 == invScaleY) { | 102 if (0 == width || 0 == height) { |
| 103 // degenerate, and the key we use for mipmaps | 103 // degenerate, and the key we use for mipmaps |
| 104 return false; | 104 return false; |
| 105 } | 105 } |
| 106 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 106 BitmapKey key(src.getGenerationID(), width, height, get_bounds_from_bitmap(s
rc)); |
| 107 | 107 |
| 108 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); | 108 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, | 111 void SkBitmapCache::AddWH(const SkBitmap& src, int width, int height, |
| 112 const SkBitmap& result, SkResourceCache* localCache) { | 112 const SkBitmap& result, SkResourceCache* localCache) { |
| 113 if (0 == invScaleX || 0 == invScaleY) { | 113 if (0 == width || 0 == height) { |
| 114 // degenerate, and the key we use for mipmaps | 114 // degenerate, and the key we use for mipmaps |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 SkASSERT(result.isImmutable()); | 117 SkASSERT(result.isImmutable()); |
| 118 BitmapRec* rec = new BitmapRec(src.getGenerationID(), invScaleX, invScaleY, | 118 BitmapRec* rec = new BitmapRec(src.getGenerationID(), width, height, |
| 119 get_bounds_from_bitmap(src), result); | 119 get_bounds_from_bitmap(src), result); |
| 120 CHECK_LOCAL(localCache, add, Add, rec); | 120 CHECK_LOCAL(localCache, add, Add, rec); |
| 121 src.pixelRef()->notifyAddedToCache(); | 121 src.pixelRef()->notifyAddedToCache(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result
, | 124 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result
, |
| 125 SkResourceCache* localCache) { | 125 SkResourceCache* localCache) { |
| 126 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); | 126 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); |
| 127 | 127 |
| 128 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); | 128 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 234 |
| 235 const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* l
ocalCache) { | 235 const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* l
ocalCache) { |
| 236 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache)); | 236 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache)); |
| 237 if (mipmap) { | 237 if (mipmap) { |
| 238 MipMapRec* rec = new MipMapRec(src, mipmap); | 238 MipMapRec* rec = new MipMapRec(src, mipmap); |
| 239 CHECK_LOCAL(localCache, add, Add, rec); | 239 CHECK_LOCAL(localCache, add, Add, rec); |
| 240 src.pixelRef()->notifyAddedToCache(); | 240 src.pixelRef()->notifyAddedToCache(); |
| 241 } | 241 } |
| 242 return mipmap; | 242 return mipmap; |
| 243 } | 243 } |
| OLD | NEW |