blob: c0ce0e63693111dee607a720cba9bd2cf0ba29e4 [file] [log] [blame]
reed04617132014-08-21 09:46:49 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBitmapCache.h"
reed680fb9e2014-08-26 09:08:04 -07009#include "SkScaledImageCache.h"
10#include "SkMipMap.h"
reed04617132014-08-21 09:46:49 -070011#include "SkRect.h"
12
13/**
14 This function finds the bounds of the bitmap *within its pixelRef*.
15 If the bitmap lacks a pixelRef, it will return an empty rect, since
16 that doesn't make sense. This may be a useful enough function that
17 it should be somewhere else (in SkBitmap?).
18 */
19static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) {
20 if (!(bm.pixelRef())) {
21 return SkIRect::MakeEmpty();
22 }
23 SkIPoint origin = bm.pixelRefOrigin();
24 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height());
25}
26
27struct BitmapKey : public SkScaledImageCache::Key {
28public:
29 BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds)
30 : fGenID(genID)
31 , fScaleX(scaleX)
32 , fScaleY(scaleY)
33 , fBounds(bounds)
34 {
35 this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds));
36 }
qiankun.miao045bb7f2014-08-25 06:08:25 -070037
reed04617132014-08-21 09:46:49 -070038 uint32_t fGenID;
39 SkScalar fScaleX;
40 SkScalar fScaleY;
41 SkIRect fBounds;
42};
43
44//////////////////////////////////////////////////////////////////////////////////////////
45
reed680fb9e2014-08-26 09:08:04 -070046struct BitmapRec : public SkScaledImageCache::Rec {
47 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds,
48 const SkBitmap& result)
49 : fKey(genID, scaleX, scaleY, bounds)
50 , fBitmap(result)
51 {}
52
53 BitmapKey fKey;
54 SkBitmap fBitmap;
55
56 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
57 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap.getSize(); }
58};
59
60static bool find_and_return(const BitmapKey& key, SkBitmap* result) {
61 const BitmapRec* rec = (BitmapRec*)SkScaledImageCache::FindAndLock(key);
62 if (rec) {
63 *result = rec->fBitmap;
64 SkScaledImageCache::Unlock(rec);
65
66 result->lockPixels();
67 if (result->getPixels()) {
68 return true;
69 }
70 // todo: we should explicitly purge rec from the cache at this point, since
71 // it is effectively purged already (has no memory behind it)
72 result->reset();
73 // fall-through to false
74 }
75 return false;
76}
77
78bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
79 SkBitmap* result) {
reed04617132014-08-21 09:46:49 -070080 if (0 == invScaleX || 0 == invScaleY) {
81 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -070082 return false;
reed04617132014-08-21 09:46:49 -070083 }
84 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
reed680fb9e2014-08-26 09:08:04 -070085 return find_and_return(key, result);
reed04617132014-08-21 09:46:49 -070086}
87
reed680fb9e2014-08-26 09:08:04 -070088void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
89 const SkBitmap& result) {
reed04617132014-08-21 09:46:49 -070090 if (0 == invScaleX || 0 == invScaleY) {
91 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -070092 return;
reed04617132014-08-21 09:46:49 -070093 }
reed680fb9e2014-08-26 09:08:04 -070094 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec,
95 (src.getGenerationID(), invScaleX, invScaleY,
96 get_bounds_from_bitmap(src), result)));
reed04617132014-08-21 09:46:49 -070097}
98
reed680fb9e2014-08-26 09:08:04 -070099bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result) {
reed04617132014-08-21 09:46:49 -0700100 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height));
reed680fb9e2014-08-26 09:08:04 -0700101 return find_and_return(key, result);
reed04617132014-08-21 09:46:49 -0700102}
103
reed680fb9e2014-08-26 09:08:04 -0700104void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& result) {
105 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1,
106 SkIRect::MakeWH(width, height), result)));
reed04617132014-08-21 09:46:49 -0700107}
108
reed680fb9e2014-08-26 09:08:04 -0700109//////////////////////////////////////////////////////////////////////////////////////////
reed04617132014-08-21 09:46:49 -0700110
reed680fb9e2014-08-26 09:08:04 -0700111struct MipMapRec : public SkScaledImageCache::Rec {
112 MipMapRec(const SkBitmap& src, const SkMipMap* result)
113 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
114 , fMipMap(SkRef(result))
115 {}
116
117 virtual ~MipMapRec() {
118 fMipMap->unref();
119 }
120
121 BitmapKey fKey;
122 const SkMipMap* fMipMap;
123
124 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
125 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->getSize(); }
126};
127
128
129const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
130 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
131 const MipMapRec* rec = (MipMapRec*)SkScaledImageCache::FindAndLock(key);
132 const SkMipMap* result = NULL;
133 if (rec) {
134 result = SkRef(rec->fMipMap);
135 SkScaledImageCache::Unlock(rec);
136 }
137 return result;
reed04617132014-08-21 09:46:49 -0700138}
139
reed680fb9e2014-08-26 09:08:04 -0700140void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
141 if (result) {
142 SkScaledImageCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
143 }
reed04617132014-08-21 09:46:49 -0700144}
145