Flutter Engine
The Flutter Engine
SkMaskCache.cpp
Go to the documentation of this file.
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
9
11#include "include/core/SkRect.h"
12#include "include/core/SkSize.h"
14#include "src/base/SkTLazy.h"
16#include "src/core/SkMask.h"
18
19#include <cstddef>
20#include <cstdint>
21
23enum SkBlurStyle : int;
24
25#define CHECK_LOCAL(localCache, localName, globalName, ...) \
26 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
27
28struct MaskValue {
31};
32
33namespace {
34static unsigned gRRectBlurKeyNamespaceLabel;
35
36struct RRectBlurKey : public SkResourceCache::Key {
37public:
38 RRectBlurKey(SkScalar sigma, const SkRRect& rrect, SkBlurStyle style)
39 : fSigma(sigma)
40 , fStyle(style)
41 , fRRect(rrect)
42 {
43 this->init(&gRRectBlurKeyNamespaceLabel, 0,
44 sizeof(fSigma) + sizeof(fStyle) + sizeof(fRRect));
45 }
46
47 SkScalar fSigma;
48 int32_t fStyle;
50};
51
52struct RRectBlurRec : public SkResourceCache::Rec {
53 RRectBlurRec(RRectBlurKey key, const SkMask& mask, SkCachedData* data)
54 : fKey(key), fValue({{nullptr, mask.fBounds, mask.fRowBytes, mask.fFormat}, data})
55 {
56 fValue.fData->attachToCacheAndRef();
57 }
58 ~RRectBlurRec() override {
59 fValue.fData->detachFromCacheAndUnref();
60 }
61
62 RRectBlurKey fKey;
63 MaskValue fValue;
64
65 const Key& getKey() const override { return fKey; }
66 size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
67 const char* getCategory() const override { return "rrect-blur"; }
70 }
71
72 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
73 const RRectBlurRec& rec = static_cast<const RRectBlurRec&>(baseRec);
75
76 SkCachedData* tmpData = rec.fValue.fData;
77 tmpData->ref();
78 if (nullptr == tmpData->data()) {
79 tmpData->unref();
80 return false;
81 }
82 result->init(rec.fValue);
83 return true;
84 }
85};
86} // namespace
87
89 const SkRRect& rrect, SkTLazy<SkMask>* mask,
90 SkResourceCache* localCache) {
92 RRectBlurKey key(sigma, rrect, style);
93 if (!CHECK_LOCAL(localCache, find, Find, key, RRectBlurRec::Visitor, &result)) {
94 return nullptr;
95 }
96
97 mask->init(static_cast<const uint8_t*>(result->fData->data()),
98 result->fMask.fBounds, result->fMask.fRowBytes, result->fMask.fFormat);
99 return result->fData;
100}
101
103 const SkRRect& rrect, const SkMask& mask, SkCachedData* data,
104 SkResourceCache* localCache) {
105 RRectBlurKey key(sigma, rrect, style);
106 return CHECK_LOCAL(localCache, add, Add, new RRectBlurRec(key, mask, data));
107}
108
109//////////////////////////////////////////////////////////////////////////////////////////
110
111namespace {
112static unsigned gRectsBlurKeyNamespaceLabel;
113
114struct RectsBlurKey : public SkResourceCache::Key {
115public:
116 RectsBlurKey(SkScalar sigma, SkBlurStyle style, const SkRect rects[], int count)
117 : fSigma(sigma)
118 , fStyle(style)
119 {
120 SkASSERT(1 == count || 2 == count);
121 SkIRect ir;
122 rects[0].roundOut(&ir);
123 fSizes[0] = SkSize{rects[0].width(), rects[0].height()};
124 if (2 == count) {
125 fSizes[1] = SkSize{rects[1].width(), rects[1].height()};
126 fSizes[2] = SkSize{rects[0].x() - rects[1].x(), rects[0].y() - rects[1].y()};
127 } else {
128 fSizes[1] = SkSize{0, 0};
129 fSizes[2] = SkSize{0, 0};
130 }
131 fSizes[3] = SkSize{rects[0].x() - ir.x(), rects[0].y() - ir.y()};
132
133 this->init(&gRectsBlurKeyNamespaceLabel, 0,
134 sizeof(fSigma) + sizeof(fStyle) + sizeof(fSizes));
135 }
136
137 SkScalar fSigma;
138 int32_t fStyle;
139 SkSize fSizes[4];
140};
141
142struct RectsBlurRec : public SkResourceCache::Rec {
143 RectsBlurRec(RectsBlurKey key, const SkMask& mask, SkCachedData* data)
144 : fKey(key), fValue({{nullptr, mask.fBounds, mask.fRowBytes, mask.fFormat}, data})
145 {
146 fValue.fData->attachToCacheAndRef();
147 }
148 ~RectsBlurRec() override {
150 }
151
152 RectsBlurKey fKey;
153 MaskValue fValue;
154
155 const Key& getKey() const override { return fKey; }
156 size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
157 const char* getCategory() const override { return "rects-blur"; }
159 return fValue.fData->diagnostic_only_getDiscardable();
160 }
161
162 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
163 const RectsBlurRec& rec = static_cast<const RectsBlurRec&>(baseRec);
164 SkTLazy<MaskValue>* result = static_cast<SkTLazy<MaskValue>*>(contextData);
165
166 SkCachedData* tmpData = rec.fValue.fData;
167 tmpData->ref();
168 if (nullptr == tmpData->data()) {
169 tmpData->unref();
170 return false;
171 }
172 result->init(rec.fValue);
173 return true;
174 }
175};
176} // namespace
177
179 const SkRect rects[], int count, SkTLazy<SkMask>* mask,
180 SkResourceCache* localCache) {
182 RectsBlurKey key(sigma, style, rects, count);
183 if (!CHECK_LOCAL(localCache, find, Find, key, RectsBlurRec::Visitor, &result)) {
184 return nullptr;
185 }
186
187 mask->init(static_cast<const uint8_t*>(result->fData->data()),
188 result->fMask.fBounds, result->fMask.fRowBytes, result->fMask.fFormat);
189 return result->fData;
190}
191
193 const SkRect rects[], int count, const SkMask& mask, SkCachedData* data,
194 SkResourceCache* localCache) {
195 RectsBlurKey key(sigma, style, rects, count);
196 return CHECK_LOCAL(localCache, add, Add, new RectsBlurRec(key, mask, data));
197}
SkStrokeRec::Style fStyle
SkRRect fRRect
int count
Definition: FontMgrTest.cpp:50
TArray< uint32_t > Key
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkBlurStyle
Definition: SkBlurTypes.h:11
#define CHECK_LOCAL(localCache, localName, globalName,...)
Definition: SkMaskCache.cpp:25
int find(T *array, int N, T item)
void detachFromCacheAndUnref() const
Definition: SkCachedData.h:112
void ref() const
Definition: SkCachedData.h:30
void attachToCacheAndRef() const
Definition: SkCachedData.h:106
size_t size() const
Definition: SkCachedData.h:25
void unref() const
Definition: SkCachedData.h:31
SkDiscardableMemory * diagnostic_only_getDiscardable() const
Definition: SkCachedData.h:37
const void * data() const
Definition: SkCachedData.h:26
static void Add(SkScalar sigma, SkBlurStyle style, const SkRRect &rrect, const SkMask &mask, SkCachedData *data, SkResourceCache *localCache=nullptr)
static SkCachedData * FindAndRef(SkScalar sigma, SkBlurStyle style, const SkRRect &rrect, SkTLazy< SkMask > *mask, SkResourceCache *localCache=nullptr)
Definition: SkMaskCache.cpp:88
void(* Visitor)(const Rec &, void *context)
T * init(Args &&... args)
Definition: SkTLazy.h:45
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result
SkRRect rrect
Definition: SkRecords.h:232
Visitor(Ts...) -> Visitor< Ts... >
SkCachedData * fData
Definition: SkMaskCache.cpp:30
SkMask fMask
Definition: SkMaskCache.cpp:29
Definition: SkRect.h:32
constexpr int32_t x() const
Definition: SkRect.h:141
constexpr int32_t y() const
Definition: SkRect.h:148
Definition: SkMask.h:25
const uint32_t fRowBytes
Definition: SkMask.h:43
const SkIRect fBounds
Definition: SkMask.h:42
const Format fFormat
Definition: SkMask.h:44
constexpr float x() const
Definition: SkRect.h:720
constexpr float y() const
Definition: SkRect.h:727
void roundOut(SkIRect *dst) const
Definition: SkRect.h:1241
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
void init(void *nameSpace, uint64_t sharedID, size_t dataSize)
virtual const char * getCategory() const =0
virtual SkDiscardableMemory * diagnostic_only_getDiscardable() const
virtual size_t bytesUsed() const =0
virtual const Key & getKey() const =0
Definition: SkSize.h:52
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63