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

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

Issue 1770723002: Add variants of the setters on SkPaint which take a sk_sp<effect> (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 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 | « include/core/SkPaint.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkPaint.h" 8 #include "SkPaint.h"
9 #include "SkAutoKern.h" 9 #include "SkAutoKern.h"
10 #include "SkChecksum.h" 10 #include "SkChecksum.h"
(...skipping 26 matching lines...) Expand all
37 37
38 static inline uint32_t set_clear_mask(uint32_t bits, bool cond, uint32_t mask) { 38 static inline uint32_t set_clear_mask(uint32_t bits, bool cond, uint32_t mask) {
39 return cond ? bits | mask : bits & ~mask; 39 return cond ? bits | mask : bits & ~mask;
40 } 40 }
41 41
42 // define this to get a printf for out-of-range parameter in setters 42 // define this to get a printf for out-of-range parameter in setters
43 // e.g. setTextSize(-1) 43 // e.g. setTextSize(-1)
44 //#define SK_REPORT_API_RANGE_CHECK 44 //#define SK_REPORT_API_RANGE_CHECK
45 45
46 SkPaint::SkPaint() { 46 SkPaint::SkPaint() {
47 fTypeface = nullptr;
48 fPathEffect = nullptr;
49 fShader = nullptr;
50 fXfermode = nullptr;
51 fMaskFilter = nullptr;
52 fColorFilter = nullptr;
53 fRasterizer = nullptr;
54 fLooper = nullptr;
55 fImageFilter = nullptr;
56
57 fTextSize = SkPaintDefaults_TextSize; 47 fTextSize = SkPaintDefaults_TextSize;
58 fTextScaleX = SK_Scalar1; 48 fTextScaleX = SK_Scalar1;
59 fTextSkewX = 0; 49 fTextSkewX = 0;
60 fColor = SK_ColorBLACK; 50 fColor = SK_ColorBLACK;
61 fWidth = 0; 51 fWidth = 0;
62 fMiterLimit = SkPaintDefaults_MiterLimit; 52 fMiterLimit = SkPaintDefaults_MiterLimit;
63 53
64 // Zero all bitfields, then set some non-zero defaults. 54 // Zero all bitfields, then set some non-zero defaults.
65 fBitfieldsUInt = 0; 55 fBitfieldsUInt = 0;
66 fBitfields.fFlags = SkPaintDefaults_Flags; 56 fBitfields.fFlags = SkPaintDefaults_Flags;
67 fBitfields.fCapType = kDefault_Cap; 57 fBitfields.fCapType = kDefault_Cap;
68 fBitfields.fJoinType = kDefault_Join; 58 fBitfields.fJoinType = kDefault_Join;
69 fBitfields.fTextAlign = kLeft_Align; 59 fBitfields.fTextAlign = kLeft_Align;
70 fBitfields.fStyle = kFill_Style; 60 fBitfields.fStyle = kFill_Style;
71 fBitfields.fTextEncoding = kUTF8_TextEncoding; 61 fBitfields.fTextEncoding = kUTF8_TextEncoding;
72 fBitfields.fHinting = SkPaintDefaults_Hinting; 62 fBitfields.fHinting = SkPaintDefaults_Hinting;
73 } 63 }
74 64
75 SkPaint::SkPaint(const SkPaint& src) { 65 SkPaint::SkPaint(const SkPaint& src)
76 #define COPY(field) field = src.field 66 #define COPY(field) field(src.field)
77 #define REF_COPY(field) field = SkSafeRef(src.field) 67 : COPY(fTypeface)
78 68 , COPY(fPathEffect)
79 REF_COPY(fTypeface); 69 , COPY(fShader)
80 REF_COPY(fPathEffect); 70 , COPY(fXfermode)
81 REF_COPY(fShader); 71 , COPY(fMaskFilter)
82 REF_COPY(fXfermode); 72 , COPY(fColorFilter)
83 REF_COPY(fMaskFilter); 73 , COPY(fRasterizer)
84 REF_COPY(fColorFilter); 74 , COPY(fLooper)
85 REF_COPY(fRasterizer); 75 , COPY(fImageFilter)
86 REF_COPY(fLooper); 76 , COPY(fTextSize)
87 REF_COPY(fImageFilter); 77 , COPY(fTextScaleX)
88 78 , COPY(fTextSkewX)
89 COPY(fTextSize); 79 , COPY(fColor)
90 COPY(fTextScaleX); 80 , COPY(fWidth)
91 COPY(fTextSkewX); 81 , COPY(fMiterLimit)
92 COPY(fColor); 82 , COPY(fBitfields)
93 COPY(fWidth);
94 COPY(fMiterLimit);
95 COPY(fBitfields);
96
97 #undef COPY 83 #undef COPY
98 #undef REF_COPY 84 {}
99 }
100 85
101 SkPaint::SkPaint(SkPaint&& src) { 86 SkPaint::SkPaint(SkPaint&& src) {
102 #define MOVE(field) field = std::move(src.field) 87 #define MOVE(field) field = std::move(src.field)
103 #define REF_MOVE(field) field = src.field; src.field = nullptr 88 MOVE(fTypeface);
104 89 MOVE(fPathEffect);
105 REF_MOVE(fTypeface); 90 MOVE(fShader);
106 REF_MOVE(fPathEffect); 91 MOVE(fXfermode);
107 REF_MOVE(fShader); 92 MOVE(fMaskFilter);
108 REF_MOVE(fXfermode); 93 MOVE(fColorFilter);
109 REF_MOVE(fMaskFilter); 94 MOVE(fRasterizer);
110 REF_MOVE(fColorFilter); 95 MOVE(fLooper);
111 REF_MOVE(fRasterizer); 96 MOVE(fImageFilter);
112 REF_MOVE(fLooper);
113 REF_MOVE(fImageFilter);
114
115 MOVE(fTextSize); 97 MOVE(fTextSize);
116 MOVE(fTextScaleX); 98 MOVE(fTextScaleX);
117 MOVE(fTextSkewX); 99 MOVE(fTextSkewX);
118 MOVE(fColor); 100 MOVE(fColor);
119 MOVE(fWidth); 101 MOVE(fWidth);
120 MOVE(fMiterLimit); 102 MOVE(fMiterLimit);
121 MOVE(fBitfields); 103 MOVE(fBitfields);
122
123 #undef MOVE 104 #undef MOVE
124 #undef REF_MOVE
125 } 105 }
126 106
127 SkPaint::~SkPaint() { 107 SkPaint::~SkPaint() {}
128 SkSafeUnref(fTypeface);
129 SkSafeUnref(fPathEffect);
130 SkSafeUnref(fShader);
131 SkSafeUnref(fXfermode);
132 SkSafeUnref(fMaskFilter);
133 SkSafeUnref(fColorFilter);
134 SkSafeUnref(fRasterizer);
135 SkSafeUnref(fLooper);
136 SkSafeUnref(fImageFilter);
137 }
138 108
139 SkPaint& SkPaint::operator=(const SkPaint& src) { 109 SkPaint& SkPaint::operator=(const SkPaint& src) {
140 if (this == &src) { 110 if (this == &src) {
141 return *this; 111 return *this;
142 } 112 }
143 113
144 #define COPY(field) field = src.field 114 #define ASSIGN(field) field = src.field
145 #define REF_COPY(field) SkSafeUnref(field); field = SkSafeRef(src.field) 115 ASSIGN(fTypeface);
146 116 ASSIGN(fPathEffect);
147 REF_COPY(fTypeface); 117 ASSIGN(fShader);
148 REF_COPY(fPathEffect); 118 ASSIGN(fXfermode);
149 REF_COPY(fShader); 119 ASSIGN(fMaskFilter);
150 REF_COPY(fXfermode); 120 ASSIGN(fColorFilter);
151 REF_COPY(fMaskFilter); 121 ASSIGN(fRasterizer);
152 REF_COPY(fColorFilter); 122 ASSIGN(fLooper);
153 REF_COPY(fRasterizer); 123 ASSIGN(fImageFilter);
154 REF_COPY(fLooper); 124 ASSIGN(fTextSize);
155 REF_COPY(fImageFilter); 125 ASSIGN(fTextScaleX);
156 126 ASSIGN(fTextSkewX);
157 COPY(fTextSize); 127 ASSIGN(fColor);
158 COPY(fTextScaleX); 128 ASSIGN(fWidth);
159 COPY(fTextSkewX); 129 ASSIGN(fMiterLimit);
160 COPY(fColor); 130 ASSIGN(fBitfields);
161 COPY(fWidth); 131 #undef ASSIGN
162 COPY(fMiterLimit);
163 COPY(fBitfields);
164 132
165 return *this; 133 return *this;
166
167 #undef COPY
168 #undef REF_COPY
169 } 134 }
170 135
171 SkPaint& SkPaint::operator=(SkPaint&& src) { 136 SkPaint& SkPaint::operator=(SkPaint&& src) {
172 if (this == &src) { 137 if (this == &src) {
173 return *this; 138 return *this;
174 } 139 }
175 140
176 #define MOVE(field) field = std::move(src.field) 141 #define MOVE(field) field = std::move(src.field)
177 #define REF_MOVE(field) SkSafeUnref(field); field = src.field; src.field = nullp tr 142 MOVE(fTypeface);
178 143 MOVE(fPathEffect);
179 REF_MOVE(fTypeface); 144 MOVE(fShader);
180 REF_MOVE(fPathEffect); 145 MOVE(fXfermode);
181 REF_MOVE(fShader); 146 MOVE(fMaskFilter);
182 REF_MOVE(fXfermode); 147 MOVE(fColorFilter);
183 REF_MOVE(fMaskFilter); 148 MOVE(fRasterizer);
184 REF_MOVE(fColorFilter); 149 MOVE(fLooper);
185 REF_MOVE(fRasterizer); 150 MOVE(fImageFilter);
186 REF_MOVE(fLooper);
187 REF_MOVE(fImageFilter);
188
189 MOVE(fTextSize); 151 MOVE(fTextSize);
190 MOVE(fTextScaleX); 152 MOVE(fTextScaleX);
191 MOVE(fTextSkewX); 153 MOVE(fTextSkewX);
192 MOVE(fColor); 154 MOVE(fColor);
193 MOVE(fWidth); 155 MOVE(fWidth);
194 MOVE(fMiterLimit); 156 MOVE(fMiterLimit);
195 MOVE(fBitfields); 157 MOVE(fBitfields);
158 #undef MOVE
196 159
197 return *this; 160 return *this;
198
199 #undef MOVE
200 #undef REF_MOVE
201 } 161 }
202 162
203 bool operator==(const SkPaint& a, const SkPaint& b) { 163 bool operator==(const SkPaint& a, const SkPaint& b) {
204 #define EQUAL(field) (a.field == b.field) 164 #define EQUAL(field) (a.field == b.field)
205 return EQUAL(fTypeface) 165 return EQUAL(fTypeface)
206 && EQUAL(fPathEffect) 166 && EQUAL(fPathEffect)
207 && EQUAL(fShader) 167 && EQUAL(fShader)
208 && EQUAL(fXfermode) 168 && EQUAL(fXfermode)
209 && EQUAL(fMaskFilter) 169 && EQUAL(fMaskFilter)
210 && EQUAL(fColorFilter) 170 && EQUAL(fColorFilter)
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 fBitfields.fTextEncoding = encoding; 345 fBitfields.fTextEncoding = encoding;
386 } else { 346 } else {
387 #ifdef SK_REPORT_API_RANGE_CHECK 347 #ifdef SK_REPORT_API_RANGE_CHECK
388 SkDebugf("SkPaint::setTextEncoding(%d) out of range\n", encoding); 348 SkDebugf("SkPaint::setTextEncoding(%d) out of range\n", encoding);
389 #endif 349 #endif
390 } 350 }
391 } 351 }
392 352
393 /////////////////////////////////////////////////////////////////////////////// 353 ///////////////////////////////////////////////////////////////////////////////
394 354
395 SkTypeface* SkPaint::setTypeface(SkTypeface* font) { 355 #define MOVE_FIELD(Field) void SkPaint::set##Field(sk_sp<Sk##Field> f) { f##Fiel d = std::move(f); }
396 SkRefCnt_SafeAssign(fTypeface, font); 356 MOVE_FIELD(Typeface)
397 return font; 357 MOVE_FIELD(Rasterizer)
398 } 358 MOVE_FIELD(ImageFilter)
359 MOVE_FIELD(Shader)
360 MOVE_FIELD(ColorFilter)
361 MOVE_FIELD(Xfermode)
362 MOVE_FIELD(PathEffect)
363 MOVE_FIELD(MaskFilter)
364 #undef MOVE_FIELD
365 void SkPaint::setLooper(sk_sp<SkDrawLooper> looper) { fLooper = std::move(looper ); }
399 366
400 SkRasterizer* SkPaint::setRasterizer(SkRasterizer* r) { 367 #define SET_PTR(Field) \
401 SkRefCnt_SafeAssign(fRasterizer, r); 368 Sk##Field* SkPaint::set##Field(Sk##Field* f) { \
402 return r; 369 this->f##Field.reset(SkSafeRef(f)); \
403 } 370 return f; \
371 }
372 SET_PTR(Typeface)
373 SET_PTR(Rasterizer)
374 SET_PTR(ImageFilter)
375 SET_PTR(Shader)
376 SET_PTR(ColorFilter)
377 SET_PTR(Xfermode)
378 SET_PTR(PathEffect)
379 SET_PTR(MaskFilter)
380 #undef SET_PTR
404 381
405 SkDrawLooper* SkPaint::setLooper(SkDrawLooper* looper) { 382 SkDrawLooper* SkPaint::setLooper(SkDrawLooper* looper) {
406 SkRefCnt_SafeAssign(fLooper, looper); 383 fLooper.reset(SkSafeRef(looper));
407 return looper; 384 return looper;
408 } 385 }
409 386
410 SkImageFilter* SkPaint::setImageFilter(SkImageFilter* imageFilter) { 387 SkXfermode* SkPaint::setXfermodeMode(SkXfermode::Mode mode) {
411 SkRefCnt_SafeAssign(fImageFilter, imageFilter); 388 fXfermode.reset(SkXfermode::Create(mode));
412 return imageFilter; 389 return fXfermode.get();
413 } 390 }
414 391
415 /////////////////////////////////////////////////////////////////////////////// 392 ///////////////////////////////////////////////////////////////////////////////
416 393
417 static SkScalar mag2(SkScalar x, SkScalar y) { 394 static SkScalar mag2(SkScalar x, SkScalar y) {
418 return x * x + y * y; 395 return x * x + y * y;
419 } 396 }
420 397
421 static bool tooBig(const SkMatrix& m, SkScalar ma2max) { 398 static bool tooBig(const SkMatrix& m, SkScalar ma2max) {
422 return mag2(m[SkMatrix::kMScaleX], m[SkMatrix::kMSkewY]) > ma2max 399 return mag2(m[SkMatrix::kMScaleX], m[SkMatrix::kMSkewY]) > ma2max
(...skipping 1296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1719 SkDescriptor* desc = ad.getDesc(); 1696 SkDescriptor* desc = ad.getDesc();
1720 1697
1721 write_out_descriptor(desc, rec, pe, &peBuffer, mf, &mfBuffer, ra, &raBuffer, descSize); 1698 write_out_descriptor(desc, rec, pe, &peBuffer, mf, &mfBuffer, ra, &raBuffer, descSize);
1722 1699
1723 SkASSERT(descSize == desc->getLength()); 1700 SkASSERT(descSize == desc->getLength());
1724 1701
1725 #ifdef TEST_DESC 1702 #ifdef TEST_DESC
1726 test_desc(rec, pe, &peBuffer, mf, &mfBuffer, ra, &raBuffer, desc, descSize); 1703 test_desc(rec, pe, &peBuffer, mf, &mfBuffer, ra, &raBuffer, desc, descSize);
1727 #endif 1704 #endif
1728 1705
1729 proc(fTypeface, desc, context); 1706 proc(fTypeface.get(), desc, context);
1730 } 1707 }
1731 1708
1732 SkGlyphCache* SkPaint::detachCache(const SkSurfaceProps* surfaceProps, 1709 SkGlyphCache* SkPaint::detachCache(const SkSurfaceProps* surfaceProps,
1733 FakeGamma fakeGamma, 1710 FakeGamma fakeGamma,
1734 const SkMatrix* deviceMatrix) const { 1711 const SkMatrix* deviceMatrix) const {
1735 SkGlyphCache* cache; 1712 SkGlyphCache* cache;
1736 this->descriptorProc(surfaceProps, fakeGamma, deviceMatrix, DetachDescProc, &cache); 1713 this->descriptorProc(surfaceProps, fakeGamma, deviceMatrix, DetachDescProc, &cache);
1737 return cache; 1714 return cache;
1738 } 1715 }
1739 1716
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
1976 this->setMaskFilter(nullptr); 1953 this->setMaskFilter(nullptr);
1977 this->setColorFilter(nullptr); 1954 this->setColorFilter(nullptr);
1978 this->setRasterizer(nullptr); 1955 this->setRasterizer(nullptr);
1979 this->setLooper(nullptr); 1956 this->setLooper(nullptr);
1980 this->setImageFilter(nullptr); 1957 this->setImageFilter(nullptr);
1981 } 1958 }
1982 } 1959 }
1983 1960
1984 /////////////////////////////////////////////////////////////////////////////// 1961 ///////////////////////////////////////////////////////////////////////////////
1985 1962
1986 SkShader* SkPaint::setShader(SkShader* shader) {
1987 SkRefCnt_SafeAssign(fShader, shader);
1988 return shader;
1989 }
1990
1991 SkColorFilter* SkPaint::setColorFilter(SkColorFilter* filter) {
1992 SkRefCnt_SafeAssign(fColorFilter, filter);
1993 return filter;
1994 }
1995
1996 SkXfermode* SkPaint::setXfermode(SkXfermode* mode) {
1997 SkRefCnt_SafeAssign(fXfermode, mode);
1998 return mode;
1999 }
2000
2001 SkXfermode* SkPaint::setXfermodeMode(SkXfermode::Mode mode) {
2002 SkSafeUnref(fXfermode);
2003 fXfermode = SkXfermode::Create(mode);
2004 return fXfermode;
2005 }
2006
2007 SkPathEffect* SkPaint::setPathEffect(SkPathEffect* effect) {
2008 SkRefCnt_SafeAssign(fPathEffect, effect);
2009 return effect;
2010 }
2011
2012 SkMaskFilter* SkPaint::setMaskFilter(SkMaskFilter* filter) {
2013 SkRefCnt_SafeAssign(fMaskFilter, filter);
2014 return filter;
2015 }
2016
2017 ///////////////////////////////////////////////////////////////////////////////
2018
2019 bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect , 1963 bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect ,
2020 SkScalar resScale) const { 1964 SkScalar resScale) const {
2021 SkStrokeRec rec(*this, resScale); 1965 SkStrokeRec rec(*this, resScale);
2022 1966
2023 const SkPath* srcPtr = &src; 1967 const SkPath* srcPtr = &src;
2024 SkPath tmpPath; 1968 SkPath tmpPath;
2025 1969
2026 if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect)) { 1970 if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect)) {
2027 srcPtr = &tmpPath; 1971 srcPtr = &tmpPath;
2028 } 1972 }
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
2386 // TODO: check if we should allow imagefilters to broadcast that they don't affect alpha 2330 // TODO: check if we should allow imagefilters to broadcast that they don't affect alpha
2387 // ala colorfilters 2331 // ala colorfilters
2388 return imf != nullptr; 2332 return imf != nullptr;
2389 } 2333 }
2390 2334
2391 bool SkPaint::nothingToDraw() const { 2335 bool SkPaint::nothingToDraw() const {
2392 if (fLooper) { 2336 if (fLooper) {
2393 return false; 2337 return false;
2394 } 2338 }
2395 SkXfermode::Mode mode; 2339 SkXfermode::Mode mode;
2396 if (SkXfermode::AsMode(fXfermode, &mode)) { 2340 if (SkXfermode::AsMode(fXfermode.get(), &mode)) {
2397 switch (mode) { 2341 switch (mode) {
2398 case SkXfermode::kSrcOver_Mode: 2342 case SkXfermode::kSrcOver_Mode:
2399 case SkXfermode::kSrcATop_Mode: 2343 case SkXfermode::kSrcATop_Mode:
2400 case SkXfermode::kDstOut_Mode: 2344 case SkXfermode::kDstOut_Mode:
2401 case SkXfermode::kDstOver_Mode: 2345 case SkXfermode::kDstOver_Mode:
2402 case SkXfermode::kPlus_Mode: 2346 case SkXfermode::kPlus_Mode:
2403 if (0 == this->getAlpha()) { 2347 if (0 == this->getAlpha()) {
2404 return !affects_alpha(fColorFilter) && !affects_alpha(fImage Filter); 2348 return !affects_alpha(fColorFilter.get()) && !affects_alpha( fImageFilter.get());
2405 } 2349 }
2406 break; 2350 break;
2407 case SkXfermode::kDst_Mode: 2351 case SkXfermode::kDst_Mode:
2408 return true; 2352 return true;
2409 default: 2353 default:
2410 break; 2354 break;
2411 } 2355 }
2412 } 2356 }
2413 return false; 2357 return false;
2414 } 2358 }
2415 2359
2416 uint32_t SkPaint::getHash() const { 2360 uint32_t SkPaint::getHash() const {
2417 // We're going to hash 10 pointers and 7 32-bit values, finishing up with fB itfields, 2361 // We're going to hash 10 pointers and 7 32-bit values, finishing up with fB itfields,
2418 // so fBitfields should be 10 pointers and 6 32-bit values from the start. 2362 // so fBitfields should be 10 pointers and 6 32-bit values from the start.
2419 static_assert(offsetof(SkPaint, fBitfields) == 9 * sizeof(void*) + 6 * sizeo f(uint32_t), 2363 static_assert(offsetof(SkPaint, fBitfields) == 9 * sizeof(void*) + 6 * sizeo f(uint32_t),
2420 "SkPaint_notPackedTightly"); 2364 "SkPaint_notPackedTightly");
2421 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(this), 2365 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(this),
2422 offsetof(SkPaint, fBitfields) + sizeof(fBitfields )); 2366 offsetof(SkPaint, fBitfields) + sizeof(fBitfields ));
2423 } 2367 }
OLDNEW
« no previous file with comments | « include/core/SkPaint.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698