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

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

Issue 670063004: Add SkMaskCache (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 2 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
(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)
21 : fSigma(sigma)
22 , fRRect(rrect) {
23 this->init(sizeof(fSigma) + sizeof(fRRect));
24 }
25
26 SkScalar fSigma;
27 SkRRect fRRect;
28 };
29
30 struct RRectBlurRec : public SkResourceCache::Rec {
31 RRectBlurRec(SkScalar sigma, const SkRRect& rrect, const SkMask& mask, SkCac hedData* data)
32 : fKey(sigma, rrect)
33 {
34 fValue.fMask = mask;
35 fValue.fData = data;
36 fValue.fData->attachToCacheAndRef();
37 }
38 ~RRectBlurRec() {
39 fValue.fData->detachFromCacheAndUnref();
40 }
41
42 RRectBlurKey fKey;
43 MaskValue fValue;
44
45 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
46 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue .fData->size(); }
47
48 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
49 const RRectBlurRec& rec = static_cast<const RRectBlurRec&>(baseRec);
50 MaskValue* result = (MaskValue*)contextData;
51
52 SkCachedData* tmpData = rec.fValue.fData;
53 tmpData->ref();
54 if (NULL == tmpData->data()) {
55 tmpData->unref();
56 return false;
57 }
58 *result = rec.fValue;
59 return true;
60 }
61 };
62
63 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRRect& rrect,
64 SkMask* mask, SkResourceCache* localCache) {
65 MaskValue result;
66 RRectBlurKey key(sigma, rrect);
67 if (!CHECK_LOCAL(localCache, find, Find, key, RRectBlurRec::Visitor, &result )) {
68 return NULL;
69 }
70
71 *mask = result.fMask;
72 mask->fImage = (uint8_t*)(result.fData->data());
73 return result.fData;
74 }
75
76 void SkMaskCache::Add(SkScalar sigma, const SkRRect& rrect,
77 const SkMask& mask, SkCachedData* data,
78 SkResourceCache* localCache) {
79 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(RRectBlurRec, (sigma, rr ect, mask, data)));
80 }
81
82 //////////////////////////////////////////////////////////////////////////////// //////////
83
84 struct RectsBlurKey : public SkResourceCache::Key {
85 public:
86 RectsBlurKey(SkScalar sigma, int32_t count, const SkRect rects[])
87 : fSigma(sigma)
88 , fRecCount(count) {
89 SkASSERT(1 == count || 2 == count);
90 for (int i = 0; i < count; i++) {
91 fRects[i] = rects[i];
92 }
93 this->init(sizeof(fSigma) + sizeof(fRecCount) + sizeof(SkRect) * fRecCou nt);
94 }
95
96 SkScalar fSigma;
97 int32_t fRecCount;
98 SkRect fRects[2];
99 };
100
101 struct RectsBlurRec : public SkResourceCache::Rec {
102 RectsBlurRec(SkScalar sigma, unsigned count, const SkRect rects[],
103 const SkMask& mask, SkCachedData* data)
104 : fKey(sigma, count, rects)
105 {
106 fValue.fMask = mask;
107 fValue.fData = data;
108 fValue.fData->attachToCacheAndRef();
109 }
110 ~RectsBlurRec() {
111 fValue.fData->detachFromCacheAndUnref();
112 }
113
114 RectsBlurKey fKey;
115 MaskValue fValue;
116
117 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
118 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue .fData->size(); }
119
120 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
121 const RectsBlurRec& rec = static_cast<const RectsBlurRec&>(baseRec);
122 MaskValue* result = (MaskValue*)contextData;
123
124 SkCachedData* tmpData = rec.fValue.fData;
125 tmpData->ref();
126 if (NULL == tmpData->data()) {
127 tmpData->unref();
128 return false;
129 }
130 *result = rec.fValue;
131 return true;
132 }
133 };
134
135 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRect rects[], int3 2_t count,
136 SkMask* mask, SkResourceCache* localCache) {
137 MaskValue result;
138 RectsBlurKey key(sigma, count, rects);
139 if (!CHECK_LOCAL(localCache, find, Find, key, RectsBlurRec::Visitor, &result )) {
140 return NULL;
141 }
142
143 *mask = result.fMask;
144 mask->fImage = (uint8_t*)(result.fData->data());
145 return result.fData;
146 }
147
148 void SkMaskCache::Add(SkScalar sigma, const SkRect rects[], int count,
149 const SkMask& mask, SkCachedData* data,
150 SkResourceCache* localCache) {
151 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(RectsBlurRec,
152 (sigma, count, rects, ma sk, data)));
153 }
OLDNEW
« src/core/SkMaskCache.h ('K') | « src/core/SkMaskCache.h ('k') | src/core/SkResourceCache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698