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

Side by Side Diff: src/core/SkBitmapCache.cpp

Issue 507483002: retool image cache to be generic cache (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 3 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 unified diff | Download patch
OLDNEW
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 "SkMipMap.h"
9 #include "SkRect.h" 10 #include "SkRect.h"
10 11
11 /** 12 /**
12 This function finds the bounds of the bitmap *within its pixelRef*. 13 This function finds the bounds of the bitmap *within its pixelRef*.
13 If the bitmap lacks a pixelRef, it will return an empty rect, since 14 If the bitmap lacks a pixelRef, it will return an empty rect, since
14 that doesn't make sense. This may be a useful enough function that 15 that doesn't make sense. This may be a useful enough function that
15 it should be somewhere else (in SkBitmap?). 16 it should be somewhere else (in SkBitmap?).
16 */ 17 */
17 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { 18 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) {
18 if (!(bm.pixelRef())) { 19 if (!(bm.pixelRef())) {
(...skipping 15 matching lines...) Expand all
34 } 35 }
35 36
36 uint32_t fGenID; 37 uint32_t fGenID;
37 SkScalar fScaleX; 38 SkScalar fScaleX;
38 SkScalar fScaleY; 39 SkScalar fScaleY;
39 SkIRect fBounds; 40 SkIRect fBounds;
40 }; 41 };
41 42
42 //////////////////////////////////////////////////////////////////////////////// ////////// 43 //////////////////////////////////////////////////////////////////////////////// //////////
43 44
44 SkScaledImageCache::ID* SkBitmapCache::FindAndLock(const SkBitmap& src, 45 struct BitmapRec : public SkScaledImageCache::Rec {
46 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b ounds,
47 const SkBitmap& result)
48 : fKey(genID, scaleX, scaleY, bounds)
49 , fBitmap(result)
50 {}
51
52 BitmapKey fKey;
qiankun 2014/08/26 07:12:40 Can we use pointer type for fKey to avoid using vi
reed1 2014/08/26 14:04:02 If the virtual call overhead shows up as a problem
53 SkBitmap fBitmap;
54
55 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
56 virtual size_t bytesUsed() const SK_OVERRIDE { return fBitmap.getSize(); }
57 };
58
59 SkScaledImageCache::ID SkBitmapCache::FindAndLock(const SkBitmap& src,
45 SkScalar invScaleX, SkScalar invScaleY, 60 SkScalar invScaleX, SkScalar invScaleY,
46 SkBitmap* result) { 61 SkBitmap* result) {
47 if (0 == invScaleX || 0 == invScaleY) { 62 if (0 == invScaleX || 0 == invScaleY) {
48 // degenerate, and the key we use for mipmaps 63 // degenerate, and the key we use for mipmaps
49 return NULL; 64 return NULL;
50 } 65 }
66
51 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b itmap(src)); 67 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b itmap(src));
52 return SkScaledImageCache::FindAndLock(key, result); 68 const BitmapRec* rec = (BitmapRec*)SkScaledImageCache::FindAndLock(key);
69 if (rec) {
70 *result = rec->fBitmap;
71 return rec;
72 }
73 return NULL;
53 } 74 }
54 75
55 SkScaledImageCache::ID* SkBitmapCache::AddAndLock(const SkBitmap& src, 76 SkScaledImageCache::ID SkBitmapCache::AddAndLock(const SkBitmap& src,
56 SkScalar invScaleX, SkScalar i nvScaleY, 77 SkScalar invScaleX, SkScalar i nvScaleY,
57 const SkBitmap& result) { 78 const SkBitmap& result) {
58 if (0 == invScaleX || 0 == invScaleY) { 79 if (0 == invScaleX || 0 == invScaleY) {
59 // degenerate, and the key we use for mipmaps 80 // degenerate, and the key we use for mipmaps
60 return NULL; 81 return NULL;
61 } 82 }
62 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b itmap(src)); 83 return SkScaledImageCache::AddAndLock(SkNEW_ARGS(BitmapRec,
63 return SkScaledImageCache::AddAndLock(key, result); 84 (src.getGenerationID(), inv ScaleX, invScaleY,
85 get_bounds_from_bitmap(src ),
86 result)));
64 } 87 }
65 88
66 //// 89 SkScaledImageCache::ID SkBitmapCache::FindAndLock(uint32_t genID, int width, int height,
67
68 SkScaledImageCache::ID* SkBitmapCache::FindAndLock(uint32_t genID, int width, in t height,
69 SkBitmap* result) { 90 SkBitmap* result) {
70 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)) ; 91 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)) ;
71 return SkScaledImageCache::FindAndLock(key, result); 92 const BitmapRec* rec = (BitmapRec*)SkScaledImageCache::FindAndLock(key);
93 if (rec) {
94 *result = rec->fBitmap;
95 return rec;
96 }
97 return NULL;
72 } 98 }
73 99
74 SkScaledImageCache::ID* SkBitmapCache::AddAndLock(uint32_t genID, int width, int height, 100 SkScaledImageCache::ID SkBitmapCache::AddAndLock(uint32_t genID, int width, int height,
75 const SkBitmap& result) { 101 const SkBitmap& result) {
76 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)) ; 102 return SkScaledImageCache::AddAndLock(SkNEW_ARGS(BitmapRec,
77 return SkScaledImageCache::AddAndLock(key, result); 103 (genID, SK_Scalar1, SK_Scal ar1,
104 SkIRect::MakeWH(width, hei ght),
105 result)));
78 } 106 }
79 107
80 //// 108 //////////////////////////////////////////////////////////////////////////////// //////////
81 109
82 SkScaledImageCache::ID* SkMipMapCache::FindAndLock(const SkBitmap& src, const Sk MipMap** result) { 110 struct MipMapRec : public SkScaledImageCache::Rec {
83 BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from _bitmap(src)); 111 MipMapRec(const SkBitmap& src, const SkMipMap* result)
84 return SkScaledImageCache::FindAndLock(key, result); 112 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
113 , fMipMap(SkRef(result))
114 {}
115
116 virtual ~MipMapRec() {
117 fMipMap->unref();
118 }
119
120 BitmapKey fKey;
121 const SkMipMap* fMipMap;
122
123 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
124 virtual size_t bytesUsed() const SK_OVERRIDE { return fMipMap->getSize(); }
125 };
126
127
128 SkScaledImageCache::ID SkMipMapCache::FindAndLock(const SkBitmap& src, const SkM ipMap** result) {
129 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
130 const MipMapRec* rec = (MipMapRec*)SkScaledImageCache::FindAndLock(key);
131 if (rec) {
132 *result = rec->fMipMap;
133 return rec;
134 }
135 return NULL;
85 } 136 }
86 137
87 SkScaledImageCache::ID* SkMipMapCache::AddAndLock(const SkBitmap& src, const SkM ipMap* result) { 138 SkScaledImageCache::ID SkMipMapCache::AddAndLock(const SkBitmap& src, const SkMi pMap* result) {
88 BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from _bitmap(src)); 139 if (NULL == result) {
89 return SkScaledImageCache::AddAndLock(key, result); 140 return NULL;
141 }
142 return SkScaledImageCache::AddAndLock(SkNEW_ARGS(MipMapRec, (src, result)));
90 } 143 }
91 144
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698