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

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

Issue 246403013: Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 8 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/SkPictureShader.cpp ('k') | src/core/SkSmallAllocator.h » ('j') | 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 "SkBitmapProcShader.h" 8 #include "SkBitmapProcShader.h"
9 #include "SkReadBuffer.h" 9 #include "SkReadBuffer.h"
10 #include "SkMallocPixelRef.h" 10 #include "SkMallocPixelRef.h"
11 #include "SkPaint.h" 11 #include "SkPaint.h"
12 #include "SkPicture.h" 12 #include "SkPicture.h"
13 #include "SkPictureShader.h" 13 #include "SkPictureShader.h"
14 #include "SkScalar.h" 14 #include "SkScalar.h"
15 #include "SkShader.h" 15 #include "SkShader.h"
16 #include "SkWriteBuffer.h" 16 #include "SkWriteBuffer.h"
17 17
18 SkShader::SkShader() { 18 SkShader::SkShader() {
19 fLocalMatrix.reset(); 19 fLocalMatrix.reset();
20 SkDEBUGCODE(fInSetContext = false;)
21 } 20 }
22 21
23 SkShader::SkShader(SkReadBuffer& buffer) 22 SkShader::SkShader(SkReadBuffer& buffer)
24 : INHERITED(buffer) { 23 : INHERITED(buffer) {
25 if (buffer.readBool()) { 24 if (buffer.readBool()) {
26 buffer.readMatrix(&fLocalMatrix); 25 buffer.readMatrix(&fLocalMatrix);
27 } else { 26 } else {
28 fLocalMatrix.reset(); 27 fLocalMatrix.reset();
29 } 28 }
30
31 SkDEBUGCODE(fInSetContext = false;)
32 } 29 }
33 30
34 SkShader::~SkShader() { 31 SkShader::~SkShader() {
35 SkASSERT(!fInSetContext);
36 } 32 }
37 33
38 void SkShader::flatten(SkWriteBuffer& buffer) const { 34 void SkShader::flatten(SkWriteBuffer& buffer) const {
39 this->INHERITED::flatten(buffer); 35 this->INHERITED::flatten(buffer);
40 bool hasLocalM = this->hasLocalMatrix(); 36 bool hasLocalM = this->hasLocalMatrix();
41 buffer.writeBool(hasLocalM); 37 buffer.writeBool(hasLocalM);
42 if (hasLocalM) { 38 if (hasLocalM) {
43 buffer.writeMatrix(fLocalMatrix); 39 buffer.writeMatrix(fLocalMatrix);
44 } 40 }
45 } 41 }
46 42
47 bool SkShader::setContext(const SkBitmap& device, 43 bool SkShader::computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInvers e) const {
48 const SkPaint& paint,
49 const SkMatrix& matrix) {
50 SkASSERT(!this->setContextHasBeenCalled());
51
52 const SkMatrix* m = &matrix; 44 const SkMatrix* m = &matrix;
53 SkMatrix total; 45 SkMatrix total;
54 46
55 fPaintAlpha = paint.getAlpha();
56 if (this->hasLocalMatrix()) { 47 if (this->hasLocalMatrix()) {
57 total.setConcat(matrix, this->getLocalMatrix()); 48 total.setConcat(matrix, this->getLocalMatrix());
58 m = &total; 49 m = &total;
59 } 50 }
60 if (m->invert(&fTotalInverse)) { 51
61 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); 52 return m->invert(totalInverse);
62 SkDEBUGCODE(fInSetContext = true;)
63 return true;
64 }
65 return false;
66 } 53 }
67 54
68 void SkShader::endContext() { 55 bool SkShader::validContext(const SkBitmap& device,
69 SkASSERT(fInSetContext); 56 const SkPaint& paint,
70 SkDEBUGCODE(fInSetContext = false;) 57 const SkMatrix& matrix,
58 SkMatrix* totalInverse) const {
59 return this->computeTotalInverse(matrix, totalInverse);
71 } 60 }
72 61
73 SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) { 62 SkShader::Context::Context(const SkShader& shader, const SkBitmap& device,
63 const SkPaint& paint, const SkMatrix& matrix)
64 : fShader(shader)
65 {
66 SkASSERT(fShader.validContext(device, paint, matrix));
67
68 // Because the context parameters must be valid at this point, we know that the matrix is
69 // invertible.
70 SkAssertResult(fShader.computeTotalInverse(matrix, &fTotalInverse));
71 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
72
73 fPaintAlpha = paint.getAlpha();
74 }
75
76 SkShader::Context::~Context() {}
77
78 SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
74 return NULL; 79 return NULL;
75 } 80 }
76 81
77 #include "SkColorPriv.h" 82 #include "SkColorPriv.h"
78 83
79 void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) { 84 void SkShader::Context::shadeSpan16(int x, int y, uint16_t span16[], int count) {
80 SkASSERT(span16); 85 SkASSERT(span16);
81 SkASSERT(count > 0); 86 SkASSERT(count > 0);
82 SkASSERT(this->canCallShadeSpan16()); 87 SkASSERT(this->canCallShadeSpan16());
83 88
84 // basically, if we get here, the subclass screwed up 89 // basically, if we get here, the subclass screwed up
85 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented"); 90 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
86 } 91 }
87 92
88 #define kTempColorQuadCount 6 // balance between speed (larger) and saving sta ck-space 93 #define kTempColorQuadCount 6 // balance between speed (larger) and saving sta ck-space
89 #define kTempColorCount (kTempColorQuadCount << 2) 94 #define kTempColorCount (kTempColorQuadCount << 2)
90 95
91 #ifdef SK_CPU_BENDIAN 96 #ifdef SK_CPU_BENDIAN
92 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) 97 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
93 #else 98 #else
94 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) 99 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
95 #endif 100 #endif
96 101
97 void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { 102 void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
98 SkASSERT(count > 0); 103 SkASSERT(count > 0);
99 104
100 SkPMColor colors[kTempColorCount]; 105 SkPMColor colors[kTempColorCount];
101 106
102 while ((count -= kTempColorCount) >= 0) { 107 while ((count -= kTempColorCount) >= 0) {
103 this->shadeSpan(x, y, colors, kTempColorCount); 108 this->shadeSpan(x, y, colors, kTempColorCount);
104 x += kTempColorCount; 109 x += kTempColorCount;
105 110
106 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset (SK_A32_SHIFT); 111 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset (SK_A32_SHIFT);
107 int quads = kTempColorQuadCount; 112 int quads = kTempColorQuadCount;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 146
142 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset (SK_A32_SHIFT); 147 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset (SK_A32_SHIFT);
143 do { 148 do {
144 *alpha++ = *srcA; 149 *alpha++ = *srcA;
145 srcA += 4; 150 srcA += 4;
146 } while (--n != 0); 151 } while (--n != 0);
147 } while (count > 0); 152 } while (count > 0);
148 #endif 153 #endif
149 } 154 }
150 155
151 SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) { 156 SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMat rix& mat) {
152 MatrixClass mc = kLinear_MatrixClass; 157 MatrixClass mc = kLinear_MatrixClass;
153 158
154 if (mat.hasPerspective()) { 159 if (mat.hasPerspective()) {
155 if (mat.fixedStepInX(0, NULL, NULL)) { 160 if (mat.fixedStepInX(0, NULL, NULL)) {
156 mc = kFixedStepInX_MatrixClass; 161 mc = kFixedStepInX_MatrixClass;
157 } else { 162 } else {
158 mc = kPerspective_MatrixClass; 163 mc = kPerspective_MatrixClass;
159 } 164 }
160 } 165 }
161 return mc; 166 return mc;
162 } 167 }
163 168
164 ////////////////////////////////////////////////////////////////////////////// 169 //////////////////////////////////////////////////////////////////////////////
165 170
166 SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, 171 SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, TileMode*) const {
167 TileMode*) const {
168 return kNone_BitmapType; 172 return kNone_BitmapType;
169 } 173 }
170 174
171 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const { 175 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
172 return kNone_GradientType; 176 return kNone_GradientType;
173 } 177 }
174 178
175 GrEffectRef* SkShader::asNewEffect(GrContext*, const SkPaint&) const { 179 GrEffectRef* SkShader::asNewEffect(GrContext*, const SkPaint&) const {
176 return NULL; 180 return NULL;
177 } 181 }
(...skipping 14 matching lines...) Expand all
192 this->getLocalMatrix().toString(str); 196 this->getLocalMatrix().toString(str);
193 } 197 }
194 } 198 }
195 #endif 199 #endif
196 200
197 ////////////////////////////////////////////////////////////////////////////// 201 //////////////////////////////////////////////////////////////////////////////
198 202
199 #include "SkColorShader.h" 203 #include "SkColorShader.h"
200 #include "SkUtils.h" 204 #include "SkUtils.h"
201 205
202 SkColorShader::SkColorShader() { 206 SkColorShader::SkColorShader()
203 fFlags = 0; 207 : fColor()
204 fInheritColor = true; 208 , fInheritColor(true) {
205 } 209 }
206 210
207 SkColorShader::SkColorShader(SkColor c) { 211 SkColorShader::SkColorShader(SkColor c)
208 fFlags = 0; 212 : fColor(c)
209 fColor = c; 213 , fInheritColor(false) {
210 fInheritColor = false;
211 } 214 }
212 215
213 SkColorShader::~SkColorShader() {}
214
215 bool SkColorShader::isOpaque() const { 216 bool SkColorShader::isOpaque() const {
216 if (fInheritColor) { 217 if (fInheritColor) {
217 return true; // using paint's alpha 218 return true; // using paint's alpha
218 } 219 }
219 return SkColorGetA(fColor) == 255; 220 return SkColorGetA(fColor) == 255;
220 } 221 }
221 222
222 SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) { 223 SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) {
223 fFlags = 0; // computed in setContext
224
225 fInheritColor = b.readBool(); 224 fInheritColor = b.readBool();
226 if (fInheritColor) { 225 if (fInheritColor) {
227 return; 226 return;
228 } 227 }
229 fColor = b.readColor(); 228 fColor = b.readColor();
230 } 229 }
231 230
232 void SkColorShader::flatten(SkWriteBuffer& buffer) const { 231 void SkColorShader::flatten(SkWriteBuffer& buffer) const {
233 this->INHERITED::flatten(buffer); 232 this->INHERITED::flatten(buffer);
234 buffer.writeBool(fInheritColor); 233 buffer.writeBool(fInheritColor);
235 if (fInheritColor) { 234 if (fInheritColor) {
236 return; 235 return;
237 } 236 }
238 buffer.writeColor(fColor); 237 buffer.writeColor(fColor);
239 } 238 }
240 239
241 uint32_t SkColorShader::getFlags() { 240 uint32_t SkColorShader::ColorShaderContext::getFlags() const {
242 return fFlags; 241 return fFlags;
243 } 242 }
244 243
245 uint8_t SkColorShader::getSpan16Alpha() const { 244 uint8_t SkColorShader::ColorShaderContext::getSpan16Alpha() const {
246 return SkGetPackedA32(fPMColor); 245 return SkGetPackedA32(fPMColor);
247 } 246 }
248 247
249 bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint, 248 SkShader::Context* SkColorShader::createContext(const SkBitmap& device, const Sk Paint& paint,
250 const SkMatrix& matrix) { 249 const SkMatrix& matrix, void* st orage) const {
251 if (!this->INHERITED::setContext(device, paint, matrix)) { 250 if (!this->validContext(device, paint, matrix)) {
252 return false; 251 return NULL;
253 } 252 }
254 253
254 return SkNEW_PLACEMENT_ARGS(storage, ColorShaderContext, (*this, device, pai nt, matrix));
255 }
256
257 SkColorShader::ColorShaderContext::ColorShaderContext(const SkColorShader& shade r,
258 const SkBitmap& device,
259 const SkPaint& paint,
260 const SkMatrix& matrix)
261 : INHERITED(shader, device, paint, matrix)
262 {
255 unsigned a; 263 unsigned a;
256 264
257 if (fInheritColor) { 265 SkColor color;
258 fColor = paint.getColor(); 266 if (shader.fInheritColor) {
259 a = SkColorGetA(fColor); 267 color = paint.getColor();
268 a = SkColorGetA(color);
260 } else { 269 } else {
261 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha())); 270 color = shader.fColor;
271 a = SkAlphaMul(SkColorGetA(color), SkAlpha255To256(paint.getAlpha()));
262 } 272 }
263 273
264 unsigned r = SkColorGetR(fColor); 274 unsigned r = SkColorGetR(color);
265 unsigned g = SkColorGetG(fColor); 275 unsigned g = SkColorGetG(color);
266 unsigned b = SkColorGetB(fColor); 276 unsigned b = SkColorGetB(color);
267 277
268 // we want this before we apply any alpha 278 // we want this before we apply any alpha
269 fColor16 = SkPack888ToRGB16(r, g, b); 279 fColor16 = SkPack888ToRGB16(r, g, b);
270 280
271 if (a != 255) { 281 if (a != 255) {
272 r = SkMulDiv255Round(r, a); 282 r = SkMulDiv255Round(r, a);
273 g = SkMulDiv255Round(g, a); 283 g = SkMulDiv255Round(g, a);
274 b = SkMulDiv255Round(b, a); 284 b = SkMulDiv255Round(b, a);
275 } 285 }
276 fPMColor = SkPackARGB32(a, r, g, b); 286 fPMColor = SkPackARGB32(a, r, g, b);
277 287
278 fFlags = kConstInY32_Flag; 288 fFlags = kConstInY32_Flag;
279 if (255 == a) { 289 if (255 == a) {
280 fFlags |= kOpaqueAlpha_Flag; 290 fFlags |= kOpaqueAlpha_Flag;
281 if (paint.isDither() == false) { 291 if (paint.isDither() == false) {
282 fFlags |= kHasSpan16_Flag; 292 fFlags |= kHasSpan16_Flag;
283 } 293 }
284 } 294 }
285
286 return true;
287 } 295 }
288 296
289 void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) { 297 void SkColorShader::ColorShaderContext::shadeSpan(int x, int y, SkPMColor span[] , int count) {
290 sk_memset32(span, fPMColor, count); 298 sk_memset32(span, fPMColor, count);
291 } 299 }
292 300
293 void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) { 301 void SkColorShader::ColorShaderContext::shadeSpan16(int x, int y, uint16_t span[ ], int count) {
294 sk_memset16(span, fColor16, count); 302 sk_memset16(span, fColor16, count);
295 } 303 }
296 304
297 void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { 305 void SkColorShader::ColorShaderContext::shadeSpanAlpha(int x, int y, uint8_t alp ha[], int count) {
298 memset(alpha, SkGetPackedA32(fPMColor), count); 306 memset(alpha, SkGetPackedA32(fPMColor), count);
299 } 307 }
300 308
301 // if we had a asAColor method, that would be more efficient... 309 // if we had a asAColor method, that would be more efficient...
302 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix , 310 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix ,
303 TileMode modes[]) const { 311 TileMode modes[]) const {
304 return kNone_BitmapType; 312 return kNone_BitmapType;
305 } 313 }
306 314
307 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const { 315 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
(...skipping 19 matching lines...) Expand all
327 } 335 }
328 336
329 this->INHERITED::toString(str); 337 this->INHERITED::toString(str);
330 338
331 str->append(")"); 339 str->append(")");
332 } 340 }
333 #endif 341 #endif
334 342
335 /////////////////////////////////////////////////////////////////////////////// 343 ///////////////////////////////////////////////////////////////////////////////
336 344
345 #ifndef SK_IGNORE_TO_STRING
337 #include "SkEmptyShader.h" 346 #include "SkEmptyShader.h"
338 347
339 uint32_t SkEmptyShader::getFlags() { return 0; }
340 uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
341
342 bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
343 const SkMatrix&) { return false; }
344
345 void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
346 SkDEBUGFAIL("should never get called, since setContext() returned false");
347 }
348
349 void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
350 SkDEBUGFAIL("should never get called, since setContext() returned false");
351 }
352
353 void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
354 SkDEBUGFAIL("should never get called, since setContext() returned false");
355 }
356
357 #ifndef SK_IGNORE_TO_STRING
358 void SkEmptyShader::toString(SkString* str) const { 348 void SkEmptyShader::toString(SkString* str) const {
359 str->append("SkEmptyShader: ("); 349 str->append("SkEmptyShader: (");
360 350
361 this->INHERITED::toString(str); 351 this->INHERITED::toString(str);
362 352
363 str->append(")"); 353 str->append(")");
364 } 354 }
365 #endif 355 #endif
OLDNEW
« no previous file with comments | « src/core/SkPictureShader.cpp ('k') | src/core/SkSmallAllocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698