Chromium Code Reviews| 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 #include "SkResourceCache.h" | |
| 10 | |
| 11 struct MaskValue { | |
| 12 SkMask fMask; | |
| 13 SkCachedData* fData; | |
| 14 }; | |
| 15 | |
| 16 struct RRectBlurKey : public SkResourceCache::Key { | |
| 17 public: | |
| 18 RRectBlurKey(SkScalar sigma, const SkRRect& rrect) | |
| 19 : fSigma(sigma) | |
| 20 , fRRect(rrect) { | |
| 21 this->init(sizeof(fSigma) + sizeof(fRRect)); | |
| 22 } | |
| 23 | |
| 24 SkScalar fSigma; | |
| 25 SkRRect fRRect; | |
| 26 }; | |
| 27 | |
| 28 struct RRectBlurRec : public SkResourceCache::Rec { | |
| 29 RRectBlurRec(SkScalar sigma, const SkRRect& rrect, const SkMask& mask, SkCac hedData* data) | |
| 30 : fKey(sigma, rrect) | |
| 31 { | |
| 32 fValue.fMask = mask; | |
| 33 fValue.fData = data; | |
| 34 fValue.fData->attachToCacheAndRef(); | |
| 35 } | |
| 36 ~RRectBlurRec() { | |
| 37 fValue.fData->detachFromCacheAndUnref(); | |
| 38 } | |
| 39 | |
| 40 RRectBlurKey fKey; | |
| 41 MaskValue fValue; | |
| 42 | |
| 43 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | |
| 44 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof( fValue); } | |
| 45 | |
| 46 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) { | |
| 47 const RRectBlurRec& rec = static_cast<const RRectBlurRec&>(baseRec); | |
| 48 MaskValue* result = (MaskValue*)contextData; | |
| 49 | |
| 50 SkCachedData* tmpData = rec.fValue.fData; | |
| 51 tmpData->ref(); | |
| 52 if (tmpData->data() == NULL) { | |
| 53 tmpData->unref(); | |
| 54 return false; | |
| 55 } | |
| 56 *result = rec.fValue; | |
| 57 return true; | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, const SkRRect& rrect, SkMa sk* mask) { | |
| 62 MaskValue result; | |
| 63 RRectBlurKey key(sigma, rrect); | |
| 64 if (SkResourceCache::Find(key, RRectBlurRec::Visitor, &result)) { | |
| 65 *mask = result.fMask; | |
| 66 mask->fImage = (uint8_t*)(result.fData->data()); | |
| 67 | |
| 68 if (mask->fImage) { | |
|
reed1
2014/10/21 21:29:49
What is this test doing? Have we not already check
| |
| 69 return result.fData; | |
| 70 } | |
| 71 } | |
| 72 return NULL; | |
| 73 } | |
| 74 | |
| 75 void SkMaskCache::Add(SkScalar sigma, const SkRRect& rrect, | |
| 76 const SkMask& mask, SkCachedData* data) { | |
| 77 return SkResourceCache::Add(SkNEW_ARGS(RRectBlurRec, (sigma, rrect, mask, da ta))); | |
| 78 } | |
| 79 | |
| 80 //////////////////////////////////////////////////////////////////////////////// ////////// | |
| 81 | |
| 82 struct RectsBlurKey : public SkResourceCache::Key { | |
| 83 public: | |
| 84 RectsBlurKey(SkScalar sigma, int32_t count, const SkRect rects[]) | |
| 85 : fSigma(sigma) | |
| 86 , fRecCount(count) { | |
| 87 for (int i = 0; i < count; i++) { | |
|
reed1
2014/10/21 21:29:49
will we crash if count > 2? Should we assert (or c
| |
| 88 fRects[i] = rects[i]; | |
| 89 } | |
| 90 this->init(sizeof(fSigma) + sizeof(fRecCount) + sizeof(SkRect) * fRecCou nt); | |
| 91 } | |
| 92 | |
| 93 SkScalar fSigma; | |
| 94 int32_t fRecCount; | |
| 95 SkRect fRects[2]; | |
| 96 }; | |
| 97 | |
| 98 struct RectsBlurRec : public SkResourceCache::Rec { | |
| 99 RectsBlurRec(SkScalar sigma, unsigned count, const SkRect rects[], | |
| 100 const SkMask& mask, SkCachedData* data) | |
| 101 : fKey(sigma, count, rects) | |
| 102 { | |
| 103 fValue.fMask = mask; | |
| 104 fValue.fData = data; | |
| 105 fValue.fData->attachToCacheAndRef(); | |
| 106 } | |
| 107 ~RectsBlurRec() { | |
| 108 fValue.fData->detachFromCacheAndUnref(); | |
| 109 } | |
| 110 | |
| 111 RectsBlurKey fKey; | |
| 112 MaskValue fValue; | |
| 113 | |
| 114 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | |
| 115 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof( fValue); } | |
| 116 | |
| 117 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) { | |
| 118 const RectsBlurRec& rec = static_cast<const RectsBlurRec&>(baseRec); | |
| 119 MaskValue* result = (MaskValue*)contextData; | |
| 120 | |
| 121 SkCachedData* tmpData = rec.fValue.fData; | |
| 122 tmpData->ref(); | |
| 123 if (tmpData->data() == NULL) { | |
| 124 tmpData->unref(); | |
| 125 return false; | |
| 126 } | |
| 127 *result = rec.fValue; | |
| 128 return true; | |
| 129 } | |
| 130 }; | |
| 131 | |
| 132 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, int32_t count, | |
| 133 const SkRect rects[], SkMask* mask) { | |
| 134 MaskValue result; | |
| 135 RectsBlurKey key(sigma, count, rects); | |
| 136 if (SkResourceCache::Find(key, RectsBlurRec::Visitor, &result)) { | |
| 137 *mask = result.fMask; | |
| 138 mask->fImage = (uint8_t*)(result.fData->data()); | |
| 139 | |
| 140 if (mask->fImage) { | |
|
reed1
2014/10/21 21:29:49
ditto about this test?
| |
| 141 return result.fData; | |
| 142 } | |
| 143 } | |
| 144 return NULL; | |
| 145 } | |
| 146 | |
| 147 void SkMaskCache::Add(SkScalar sigma, int32_t count, const SkRect rects[], | |
| 148 const SkMask& mask, SkCachedData* data) { | |
| 149 return SkResourceCache::Add(SkNEW_ARGS(RectsBlurRec, (sigma, count, rects, m ask, data))); | |
| 150 } | |
| OLD | NEW |