OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 "SkMaskCache.h" | |
9 | |
10 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ | |
11 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global Name(__VA_ARGS__)) | |
12 | |
13 struct MaskValue { | |
14 SkMask fMask; | |
15 SkCachedData* fData; | |
16 }; | |
17 | |
18 struct RRectBlurKey : public SkResourceCache::Key { | |
19 public: | |
20 RRectBlurKey(SkScalar sigma, const SkRRect& rrect, SkBlurStyle style, SkBlur Quality quality) | |
21 : fSigma(sigma) | |
22 , fRect(rrect.rect()) | |
23 , fStyle(style) | |
24 , fQuality(quality) { | |
25 fRadii[SkRRect::kUpperLeft_Corner] = rrect.radii(SkRRect::kUpperLeft_Cor ner); | |
26 fRadii[SkRRect::kUpperRight_Corner] = rrect.radii(SkRRect::kUpperRight_C orner); | |
27 fRadii[SkRRect::kLowerRight_Corner] = rrect.radii(SkRRect::kLowerRight_C orner); | |
28 fRadii[SkRRect::kLowerLeft_Corner] = rrect.radii(SkRRect::kLowerLeft_Cor ner); | |
29 | |
30 this->init(sizeof(fSigma) + sizeof(fRect) + sizeof(fRadii) + sizeof(fSty le) + sizeof(fQuality)); | |
f(malita)
2014/10/22 18:31:31
After https://skia.googlesource.com/skia/+/171e5b7
qiankun
2014/10/23 02:51:10
Done.
| |
31 } | |
32 | |
33 SkScalar fSigma; | |
34 SkRect fRect; | |
35 SkVector fRadii[4]; | |
36 int32_t fStyle; | |
37 int32_t fQuality; | |
38 }; | |
39 | |
40 struct RRectBlurRec : public SkResourceCache::Rec { | |
41 RRectBlurRec(RRectBlurKey key, const SkMask& mask, SkCachedData* data) | |
42 : fKey(key) | |
43 { | |
44 fValue.fMask = mask; | |
45 fValue.fData = data; | |
46 fValue.fData->attachToCacheAndRef(); | |
47 } | |
48 ~RRectBlurRec() { | |
49 fValue.fData->detachFromCacheAndUnref(); | |
50 } | |
51 | |
52 RRectBlurKey fKey; | |
53 MaskValue fValue; | |
54 | |
55 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | |
56 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue .fData->size(); } | |
57 | |
58 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) { | |
59 const RRectBlurRec& rec = static_cast<const RRectBlurRec&>(baseRec); | |
60 MaskValue* result = (MaskValue*)contextData; | |
61 | |
62 SkCachedData* tmpData = rec.fValue.fData; | |
63 tmpData->ref(); | |
64 if (NULL == tmpData->data()) { | |
65 tmpData->unref(); | |
66 return false; | |
67 } | |
68 *result = rec.fValue; | |
69 return true; | |
70 } | |
71 }; | |
72 | |
73 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRRect& rrect, SkBl urStyle style, | |
74 SkBlurQuality quality, SkMask* mask, | |
75 SkResourceCache* localCache) { | |
76 MaskValue result; | |
77 RRectBlurKey key(sigma, rrect, style, quality); | |
78 if (!CHECK_LOCAL(localCache, find, Find, key, RRectBlurRec::Visitor, &result )) { | |
79 return NULL; | |
80 } | |
81 | |
82 *mask = result.fMask; | |
83 mask->fImage = (uint8_t*)(result.fData->data()); | |
84 return result.fData; | |
85 } | |
86 | |
87 void SkMaskCache::Add(SkScalar sigma, const SkRRect& rrect, SkBlurStyle style, | |
88 SkBlurQuality quality, const SkMask& mask, SkCachedData* d ata, | |
89 SkResourceCache* localCache) { | |
90 RRectBlurKey key(sigma, rrect, style, quality); | |
91 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(RRectBlurRec, (key, mask , data))); | |
92 } | |
93 | |
94 //////////////////////////////////////////////////////////////////////////////// ////////// | |
95 | |
96 struct RectsBlurKey : public SkResourceCache::Key { | |
97 public: | |
98 RectsBlurKey(SkScalar sigma, int count, const SkRect rects[], SkBlurStyle st yle) | |
99 : fSigma(sigma) | |
100 , fRecCount(count) | |
101 , fStyle(style){ | |
102 SkASSERT(1 == count || 2 == count); | |
103 fRects[0] = SkRect::MakeEmpty(); | |
104 fRects[1] = SkRect::MakeEmpty(); | |
105 for (int i = 0; i < count; i++) { | |
106 fRects[i] = rects[i]; | |
107 } | |
108 this->init(sizeof(fSigma) + sizeof(fRecCount) + sizeof(fRects) + sizeof( fStyle)); | |
f(malita)
2014/10/22 18:31:31
Same.
qiankun
2014/10/23 02:51:10
Done.
| |
109 } | |
110 | |
111 SkScalar fSigma; | |
112 int fRecCount; | |
113 SkRect fRects[2]; | |
114 int32_t fStyle; | |
115 }; | |
116 | |
117 struct RectsBlurRec : public SkResourceCache::Rec { | |
118 RectsBlurRec(RectsBlurKey key, const SkMask& mask, SkCachedData* data) | |
119 : fKey(key) | |
120 { | |
121 fValue.fMask = mask; | |
122 fValue.fData = data; | |
123 fValue.fData->attachToCacheAndRef(); | |
124 } | |
125 ~RectsBlurRec() { | |
126 fValue.fData->detachFromCacheAndUnref(); | |
127 } | |
128 | |
129 RectsBlurKey fKey; | |
130 MaskValue fValue; | |
131 | |
132 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | |
133 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue .fData->size(); } | |
134 | |
135 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) { | |
136 const RectsBlurRec& rec = static_cast<const RectsBlurRec&>(baseRec); | |
137 MaskValue* result = (MaskValue*)contextData; | |
138 | |
139 SkCachedData* tmpData = rec.fValue.fData; | |
140 tmpData->ref(); | |
141 if (NULL == tmpData->data()) { | |
142 tmpData->unref(); | |
143 return false; | |
144 } | |
145 *result = rec.fValue; | |
146 return true; | |
147 } | |
148 }; | |
149 | |
150 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRect rects[], int count, | |
151 SkBlurStyle style, SkMask* mask, | |
152 SkResourceCache* localCache) { | |
153 MaskValue result; | |
154 RectsBlurKey key(sigma, count, rects, style); | |
155 if (!CHECK_LOCAL(localCache, find, Find, key, RectsBlurRec::Visitor, &result )) { | |
156 return NULL; | |
157 } | |
158 | |
159 *mask = result.fMask; | |
160 mask->fImage = (uint8_t*)(result.fData->data()); | |
161 return result.fData; | |
162 } | |
163 | |
164 void SkMaskCache::Add(SkScalar sigma, const SkRect rects[], int count, SkBlurSty le style, | |
165 const SkMask& mask, SkCachedData* data, | |
166 SkResourceCache* localCache) { | |
167 RectsBlurKey key(sigma, count, rects, style); | |
168 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(RectsBlurRec, (key, mask , data))); | |
169 } | |
OLD | NEW |