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

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

Issue 670063004: Add SkMaskCache (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add style and quality to mask key 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
« no previous file with comments | « src/core/SkMaskCache.h ('k') | src/core/SkResourceCache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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, SkBlurStyle style, SkBlur Quality quality)
21 : fSigma(sigma)
22 , fRRect(rrect)
23 , fStyle(style)
24 , fQuality(quality) {
25 this->init(sizeof(fSigma) + sizeof(fRRect) + sizeof(fStyle) + sizeof(fQu ality));
26 }
27
28 SkScalar fSigma;
29 SkRRect fRRect;
reed1 2014/10/22 16:05:27 SkRRect has as its last field an enum "Type". This
robertphillips 2014/10/22 16:52:25 I think we should "fix" SkRRect. Additionally, som
qiankun 2014/10/22 17:19:54 What about only use rect and radii of SkRRect?
30 SkBlurStyle fStyle;
reed1 2014/10/22 16:05:26 Since ResourceKey is going to hash through the ent
qiankun 2014/10/22 17:19:54 Done.
31 SkBlurQuality fQuality;
32 };
33
34 struct RRectBlurRec : public SkResourceCache::Rec {
35 RRectBlurRec(RRectBlurKey key, const SkMask& mask, SkCachedData* data)
36 : fKey(key)
37 {
38 fValue.fMask = mask;
39 fValue.fData = data;
40 fValue.fData->attachToCacheAndRef();
41 }
42 ~RRectBlurRec() {
43 fValue.fData->detachFromCacheAndUnref();
44 }
45
46 RRectBlurKey fKey;
47 MaskValue fValue;
48
49 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
50 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue .fData->size(); }
51
52 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
53 const RRectBlurRec& rec = static_cast<const RRectBlurRec&>(baseRec);
54 MaskValue* result = (MaskValue*)contextData;
55
56 SkCachedData* tmpData = rec.fValue.fData;
57 tmpData->ref();
58 if (NULL == tmpData->data()) {
59 tmpData->unref();
60 return false;
61 }
62 *result = rec.fValue;
63 return true;
64 }
65 };
66
67 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRRect& rrect, SkBl urStyle style,
68 SkBlurQuality quality, SkMask* mask,
69 SkResourceCache* localCache) {
70 MaskValue result;
71 RRectBlurKey key(sigma, rrect, style, quality);
72 if (!CHECK_LOCAL(localCache, find, Find, key, RRectBlurRec::Visitor, &result )) {
73 return NULL;
74 }
75
76 *mask = result.fMask;
77 mask->fImage = (uint8_t*)(result.fData->data());
78 return result.fData;
79 }
80
81 void SkMaskCache::Add(SkScalar sigma, const SkRRect& rrect, SkBlurStyle style,
82 SkBlurQuality quality, const SkMask& mask, SkCachedData* d ata,
83 SkResourceCache* localCache) {
84 RRectBlurKey key(sigma, rrect, style, quality);
85 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(RRectBlurRec, (key, mask , data)));
86 }
87
88 //////////////////////////////////////////////////////////////////////////////// //////////
89
90 struct RectsBlurKey : public SkResourceCache::Key {
91 public:
92 RectsBlurKey(SkScalar sigma, int count, const SkRect rects[], SkBlurStyle st yle)
93 : fSigma(sigma)
94 , fRecCount(count)
95 , fStyle(style){
96 SkASSERT(1 == count || 2 == count);
97 fRects[0] = SkRect::MakeEmpty();
98 fRects[1] = SkRect::MakeEmpty();
99 for (int i = 0; i < count; i++) {
100 fRects[i] = rects[i];
101 }
102 this->init(sizeof(fSigma) + sizeof(fRecCount) + sizeof(fRects) + sizeof( fStyle));
103 }
104
105 SkScalar fSigma;
106 int fRecCount;
107 SkRect fRects[2];
108 SkBlurStyle fStyle;
109 };
110
111 struct RectsBlurRec : public SkResourceCache::Rec {
112 RectsBlurRec(RectsBlurKey key, const SkMask& mask, SkCachedData* data)
113 : fKey(key)
114 {
115 fValue.fMask = mask;
116 fValue.fData = data;
117 fValue.fData->attachToCacheAndRef();
118 }
119 ~RectsBlurRec() {
120 fValue.fData->detachFromCacheAndUnref();
121 }
122
123 RectsBlurKey fKey;
124 MaskValue fValue;
125
126 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
127 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue .fData->size(); }
128
129 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
130 const RectsBlurRec& rec = static_cast<const RectsBlurRec&>(baseRec);
131 MaskValue* result = (MaskValue*)contextData;
132
133 SkCachedData* tmpData = rec.fValue.fData;
134 tmpData->ref();
135 if (NULL == tmpData->data()) {
136 tmpData->unref();
137 return false;
138 }
139 *result = rec.fValue;
140 return true;
141 }
142 };
143
144 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRect rects[], int count,
145 SkBlurStyle style, SkMask* mask,
146 SkResourceCache* localCache) {
147 MaskValue result;
148 RectsBlurKey key(sigma, count, rects, style);
149 if (!CHECK_LOCAL(localCache, find, Find, key, RectsBlurRec::Visitor, &result )) {
150 return NULL;
151 }
152
153 *mask = result.fMask;
154 mask->fImage = (uint8_t*)(result.fData->data());
155 return result.fData;
156 }
157
158 void SkMaskCache::Add(SkScalar sigma, const SkRect rects[], int count, SkBlurSty le style,
159 const SkMask& mask, SkCachedData* data,
160 SkResourceCache* localCache) {
161 RectsBlurKey key(sigma, count, rects, style);
162 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(RectsBlurRec, (key, mask , data)));
163 }
OLDNEW
« no previous file with comments | « src/core/SkMaskCache.h ('k') | src/core/SkResourceCache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698