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/SkBitmapProcShader.cpp

Issue 207683004: 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: clean up Created 6 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
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "SkColorPriv.h" 8 #include "SkColorPriv.h"
9 #include "SkReadBuffer.h" 9 #include "SkReadBuffer.h"
10 #include "SkWriteBuffer.h" 10 #include "SkWriteBuffer.h"
(...skipping 16 matching lines...) Expand all
27 return true; 27 return true;
28 default: 28 default:
29 break; 29 break;
30 } 30 }
31 return false; 31 return false;
32 } 32 }
33 33
34 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, 34 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src,
35 TileMode tmx, TileMode tmy) { 35 TileMode tmx, TileMode tmy) {
36 fRawBitmap = src; 36 fRawBitmap = src;
37 fState.fTileModeX = (uint8_t)tmx; 37 fTileModeX = (uint8_t)tmx;
38 fState.fTileModeY = (uint8_t)tmy; 38 fTileModeY = (uint8_t)tmy;
39 fFlags = 0; // computed in setContext
40 } 39 }
41 40
42 SkBitmapProcShader::SkBitmapProcShader(SkReadBuffer& buffer) 41 SkBitmapProcShader::SkBitmapProcShader(SkReadBuffer& buffer)
43 : INHERITED(buffer) { 42 : INHERITED(buffer) {
44 buffer.readBitmap(&fRawBitmap); 43 buffer.readBitmap(&fRawBitmap);
45 fRawBitmap.setImmutable(); 44 fRawBitmap.setImmutable();
46 fState.fTileModeX = buffer.readUInt(); 45 fTileModeX = buffer.readUInt();
47 fState.fTileModeY = buffer.readUInt(); 46 fTileModeY = buffer.readUInt();
48 fFlags = 0; // computed in setContext
49 } 47 }
50 48
51 SkShader::BitmapType SkBitmapProcShader::asABitmap(SkBitmap* texture, 49 SkShader::BitmapType SkBitmapProcShader::asABitmap(SkBitmap* texture,
52 SkMatrix* texM, 50 SkMatrix* texM,
53 TileMode xy[]) const { 51 TileMode xy[]) const {
54 if (texture) { 52 if (texture) {
55 *texture = fRawBitmap; 53 *texture = fRawBitmap;
56 } 54 }
57 if (texM) { 55 if (texM) {
58 texM->reset(); 56 texM->reset();
59 } 57 }
60 if (xy) { 58 if (xy) {
61 xy[0] = (TileMode)fState.fTileModeX; 59 xy[0] = (TileMode)fTileModeX;
62 xy[1] = (TileMode)fState.fTileModeY; 60 xy[1] = (TileMode)fTileModeY;
63 } 61 }
64 return kDefault_BitmapType; 62 return kDefault_BitmapType;
65 } 63 }
66 64
67 void SkBitmapProcShader::flatten(SkWriteBuffer& buffer) const { 65 void SkBitmapProcShader::flatten(SkWriteBuffer& buffer) const {
68 this->INHERITED::flatten(buffer); 66 this->INHERITED::flatten(buffer);
69 67
70 buffer.writeBitmap(fRawBitmap); 68 buffer.writeBitmap(fRawBitmap);
71 buffer.writeUInt(fState.fTileModeX); 69 buffer.writeUInt(fTileModeX);
72 buffer.writeUInt(fState.fTileModeY); 70 buffer.writeUInt(fTileModeY);
73 } 71 }
74 72
75 static bool only_scale_and_translate(const SkMatrix& matrix) { 73 static bool only_scale_and_translate(const SkMatrix& matrix) {
76 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; 74 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
77 return (matrix.getType() & ~mask) == 0; 75 return (matrix.getType() & ~mask) == 0;
78 } 76 }
79 77
80 bool SkBitmapProcShader::isOpaque() const { 78 bool SkBitmapProcShader::isOpaque() const {
81 return fRawBitmap.isOpaque(); 79 return fRawBitmap.isOpaque();
82 } 80 }
83 81
84 static bool valid_for_drawing(const SkBitmap& bm) { 82 static bool valid_for_drawing(const SkBitmap& bm) {
85 if (0 == bm.width() || 0 == bm.height()) { 83 if (0 == bm.width() || 0 == bm.height()) {
86 return false; // nothing to draw 84 return false; // nothing to draw
87 } 85 }
88 if (NULL == bm.pixelRef()) { 86 if (NULL == bm.pixelRef()) {
89 return false; // no pixels to read 87 return false; // no pixels to read
90 } 88 }
91 if (kIndex_8_SkColorType == bm.colorType()) { 89 if (kIndex_8_SkColorType == bm.colorType()) {
92 // ugh, I have to lock-pixels to inspect the colortable 90 // ugh, I have to lock-pixels to inspect the colortable
93 SkAutoLockPixels alp(bm); 91 SkAutoLockPixels alp(bm);
94 if (!bm.getColorTable()) { 92 if (!bm.getColorTable()) {
95 return false; 93 return false;
96 } 94 }
97 } 95 }
98 return true; 96 return true;
99 } 97 }
100 98
101 bool SkBitmapProcShader::setContext(const SkBitmap& device, 99 bool SkBitmapProcShader::validContext(const SkBitmap& device,
102 const SkPaint& paint, 100 const SkPaint& paint,
103 const SkMatrix& matrix) { 101 const SkMatrix& matrix) const {
104 if (!fRawBitmap.getTexture() && !valid_for_drawing(fRawBitmap)) { 102 if (!fRawBitmap.getTexture() && !valid_for_drawing(fRawBitmap)) {
105 return false; 103 return false;
106 } 104 }
107 105
108 // do this first, so we have a correct inverse matrix 106 // Do this first, so we know the matrix can be inverted.
109 if (!this->INHERITED::setContext(device, paint, matrix)) { 107 if (!this->INHERITED::validContext(device, paint, matrix)) {
110 return false; 108 return false;
111 } 109 }
112 110
113 fState.fOrigBitmap = fRawBitmap; 111 // TODO(dominikg): Can we avoid re-computing the inverse?
114 if (!fState.chooseProcs(this->getTotalInverse(), paint)) { 112 SkMatrix inverse;
115 this->INHERITED::endContext(); 113 SkASSERT(matrix.invert(&inverse));
114
115 SkBitmapProcState state;
116 state.fTileModeX = fTileModeX;
117 state.fTileModeY = fTileModeY;
118 state.fOrigBitmap = fRawBitmap;
119 // TODO(dominikg): Could we have a more light-weight method instead of choos eProcs that only
scroggo 2014/03/24 21:24:46 Here's what I might do: 1. Instead of making crea
Dominik Grewe 2014/03/26 17:22:22 Yeah, that could easily lead to problems, couldn't
scroggo 2014/03/26 23:13:09 I don't think we'll subclass SkBitmapProcShader, a
Dominik Grewe 2014/03/27 14:27:20 Do you mean SkBitmapProcState rather than Shader?
scroggo 2014/03/27 18:05:33 You are correct; I did mean SkBitmapProcState. I d
Dominik Grewe 2014/03/28 18:16:31 Yes, that looks reasonable. The only caveat is tha
scroggo 2014/03/28 20:50:07 I also don't have much time to look at this until
Dominik Grewe 2014/04/01 10:49:55 Good idea. I'm now placing the SkBitmapProcState o
120 // validates the inputs?
121 if (!state.chooseProcs(inverse, paint)) {
116 return false; 122 return false;
117 } 123 }
118 124
125 return true;
126 }
127
128 SkShader::Context* SkBitmapProcShader::createContext(const SkBitmap& device, con st SkPaint& paint,
129 const SkMatrix& matrix, voi d* storage) const {
130 if (!this->validContext(device, paint, matrix)) {
131 return NULL;
132 }
133
134 return SkNEW_PLACEMENT_ARGS(storage, BitmapProcShaderContext, (*this, device , paint, matrix));
135 }
136
137 size_t SkBitmapProcShader::contextSize() const {
138 return sizeof(BitmapProcShaderContext);
139 }
140
141 SkBitmapProcShader::BitmapProcShaderContext::BitmapProcShaderContext(
142 const SkBitmapProcShader& shader, const SkBitmap& device,
143 const SkPaint& paint, const SkMatrix& matrix)
144 : INHERITED(shader, device, paint, matrix) {
145
146 fState.fTileModeX = shader.fTileModeX;
147 fState.fTileModeY = shader.fTileModeY;
148 fState.fOrigBitmap = shader.fRawBitmap;
149 SkASSERT(fState.chooseProcs(this->getTotalInverse(), paint));
150
119 const SkBitmap& bitmap = *fState.fBitmap; 151 const SkBitmap& bitmap = *fState.fBitmap;
120 bool bitmapIsOpaque = bitmap.isOpaque(); 152 bool bitmapIsOpaque = bitmap.isOpaque();
121 153
122 // update fFlags 154 // update fFlags
123 uint32_t flags = 0; 155 uint32_t flags = 0;
124 if (bitmapIsOpaque && (255 == this->getPaintAlpha())) { 156 if (bitmapIsOpaque && (255 == this->getPaintAlpha())) {
125 flags |= kOpaqueAlpha_Flag; 157 flags |= kOpaqueAlpha_Flag;
126 } 158 }
127 159
128 switch (bitmap.colorType()) { 160 switch (bitmap.colorType()) {
(...skipping 21 matching lines...) Expand all
150 // if we're only 1-pixel high, and we don't rotate, then we can claim this 182 // if we're only 1-pixel high, and we don't rotate, then we can claim this
151 if (1 == bitmap.height() && 183 if (1 == bitmap.height() &&
152 only_scale_and_translate(this->getTotalInverse())) { 184 only_scale_and_translate(this->getTotalInverse())) {
153 flags |= kConstInY32_Flag; 185 flags |= kConstInY32_Flag;
154 if (flags & kHasSpan16_Flag) { 186 if (flags & kHasSpan16_Flag) {
155 flags |= kConstInY16_Flag; 187 flags |= kConstInY16_Flag;
156 } 188 }
157 } 189 }
158 190
159 fFlags = flags; 191 fFlags = flags;
160 return true;
161 } 192 }
162 193
163 void SkBitmapProcShader::endContext() { 194 SkBitmapProcShader::BitmapProcShaderContext::~BitmapProcShaderContext() {
scroggo 2014/03/24 21:24:46 Any reason not to just remove this entirely?
Dominik Grewe 2014/03/26 17:22:22 Just for completeness. I've moved the empty body t
164 fState.endContext();
165 this->INHERITED::endContext();
166 } 195 }
167 196
168 #define BUF_MAX 128 197 #define BUF_MAX 128
169 198
170 #define TEST_BUFFER_OVERRITEx 199 #define TEST_BUFFER_OVERRITEx
171 200
172 #ifdef TEST_BUFFER_OVERRITE 201 #ifdef TEST_BUFFER_OVERRITE
173 #define TEST_BUFFER_EXTRA 32 202 #define TEST_BUFFER_EXTRA 32
174 #define TEST_PATTERN 0x88888888 203 #define TEST_PATTERN 0x88888888
175 #else 204 #else
176 #define TEST_BUFFER_EXTRA 0 205 #define TEST_BUFFER_EXTRA 0
177 #endif 206 #endif
178 207
179 void SkBitmapProcShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { 208 void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan(
209 int x, int y, SkPMColor dstC[], int count) {
scroggo 2014/03/24 21:24:46 Parameters that fit should be on the same line.
Dominik Grewe 2014/03/26 17:22:22 If I read the coding style correctly it seems that
scroggo 2014/03/26 23:13:09 I think it's more conventional, but you're right,
180 const SkBitmapProcState& state = fState; 210 const SkBitmapProcState& state = fState;
181 if (state.getShaderProc32()) { 211 if (state.getShaderProc32()) {
182 state.getShaderProc32()(state, x, y, dstC, count); 212 state.getShaderProc32()(state, x, y, dstC, count);
183 return; 213 return;
184 } 214 }
185 215
186 uint32_t buffer[BUF_MAX + TEST_BUFFER_EXTRA]; 216 uint32_t buffer[BUF_MAX + TEST_BUFFER_EXTRA];
187 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); 217 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc();
188 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32(); 218 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32();
189 int max = fState.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX); 219 int max = fState.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX);
(...skipping 23 matching lines...) Expand all
213 243
214 if ((count -= n) == 0) { 244 if ((count -= n) == 0) {
215 break; 245 break;
216 } 246 }
217 SkASSERT(count > 0); 247 SkASSERT(count > 0);
218 x += n; 248 x += n;
219 dstC += n; 249 dstC += n;
220 } 250 }
221 } 251 }
222 252
223 SkShader::ShadeProc SkBitmapProcShader::asAShadeProc(void** ctx) { 253 SkShader::Context::ShadeProc SkBitmapProcShader::BitmapProcShaderContext::asASha deProc(void** ctx) {
224 if (fState.getShaderProc32()) { 254 if (fState.getShaderProc32()) {
225 *ctx = &fState; 255 *ctx = &fState;
226 return (ShadeProc)fState.getShaderProc32(); 256 return (ShadeProc)fState.getShaderProc32();
227 } 257 }
228 return NULL; 258 return NULL;
229 } 259 }
230 260
231 void SkBitmapProcShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) { 261 void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan16(
262 int x, int y, uint16_t dstC[], int count) {
scroggo 2014/03/24 21:24:46 Same
232 const SkBitmapProcState& state = fState; 263 const SkBitmapProcState& state = fState;
233 if (state.getShaderProc16()) { 264 if (state.getShaderProc16()) {
234 state.getShaderProc16()(state, x, y, dstC, count); 265 state.getShaderProc16()(state, x, y, dstC, count);
235 return; 266 return;
236 } 267 }
237 268
238 uint32_t buffer[BUF_MAX]; 269 uint32_t buffer[BUF_MAX];
239 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); 270 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc();
240 SkBitmapProcState::SampleProc16 sproc = state.getSampleProc16(); 271 SkBitmapProcState::SampleProc16 sproc = state.getSampleProc16();
241 int max = fState.maxCountForBufferSize(sizeof(buffer)); 272 int max = fState.maxCountForBufferSize(sizeof(buffer));
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 366
336 #ifdef SK_DEVELOPER 367 #ifdef SK_DEVELOPER
337 void SkBitmapProcShader::toString(SkString* str) const { 368 void SkBitmapProcShader::toString(SkString* str) const {
338 static const char* gTileModeName[SkShader::kTileModeCount] = { 369 static const char* gTileModeName[SkShader::kTileModeCount] = {
339 "clamp", "repeat", "mirror" 370 "clamp", "repeat", "mirror"
340 }; 371 };
341 372
342 str->append("BitmapShader: ("); 373 str->append("BitmapShader: (");
343 374
344 str->appendf("(%s, %s)", 375 str->appendf("(%s, %s)",
345 gTileModeName[fState.fTileModeX], 376 gTileModeName[fTileModeX],
346 gTileModeName[fState.fTileModeY]); 377 gTileModeName[fTileModeY]);
347 378
348 str->append(" "); 379 str->append(" ");
349 fRawBitmap.toString(str); 380 fRawBitmap.toString(str);
350 381
351 this->INHERITED::toString(str); 382 this->INHERITED::toString(str);
352 383
353 str->append(")"); 384 str->append(")");
354 } 385 }
355 #endif 386 #endif
356 387
(...skipping 20 matching lines...) Expand all
377 SkMatrix matrix; 408 SkMatrix matrix;
378 matrix.setIDiv(fRawBitmap.width(), fRawBitmap.height()); 409 matrix.setIDiv(fRawBitmap.width(), fRawBitmap.height());
379 410
380 SkMatrix lmInverse; 411 SkMatrix lmInverse;
381 if (!this->getLocalMatrix().invert(&lmInverse)) { 412 if (!this->getLocalMatrix().invert(&lmInverse)) {
382 return NULL; 413 return NULL;
383 } 414 }
384 matrix.preConcat(lmInverse); 415 matrix.preConcat(lmInverse);
385 416
386 SkShader::TileMode tm[] = { 417 SkShader::TileMode tm[] = {
387 (TileMode)fState.fTileModeX, 418 (TileMode)fTileModeX,
388 (TileMode)fState.fTileModeY, 419 (TileMode)fTileModeY,
389 }; 420 };
390 421
391 // Must set wrap and filter on the sampler before requesting a texture. In t wo places below 422 // Must set wrap and filter on the sampler before requesting a texture. In t wo places below
392 // we check the matrix scale factors to determine how to interpret the filte r quality setting. 423 // we check the matrix scale factors to determine how to interpret the filte r quality setting.
393 // This completely ignores the complexity of the drawVertices case where exp licit local coords 424 // This completely ignores the complexity of the drawVertices case where exp licit local coords
394 // are provided by the caller. 425 // are provided by the caller.
395 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel(); 426 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
396 GrTextureParams::FilterMode textureFilterMode; 427 GrTextureParams::FilterMode textureFilterMode;
397 switch(paintFilterLevel) { 428 switch(paintFilterLevel) {
398 case SkPaint::kNone_FilterLevel: 429 case SkPaint::kNone_FilterLevel:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 GrEffectRef* effect = NULL; 475 GrEffectRef* effect = NULL;
445 if (paintFilterLevel == SkPaint::kHigh_FilterLevel) { 476 if (paintFilterLevel == SkPaint::kHigh_FilterLevel) {
446 effect = GrBicubicEffect::Create(texture, matrix, tm); 477 effect = GrBicubicEffect::Create(texture, matrix, tm);
447 } else { 478 } else {
448 effect = GrSimpleTextureEffect::Create(texture, matrix, params); 479 effect = GrSimpleTextureEffect::Create(texture, matrix, params);
449 } 480 }
450 GrUnlockAndUnrefCachedBitmapTexture(texture); 481 GrUnlockAndUnrefCachedBitmapTexture(texture);
451 return effect; 482 return effect;
452 } 483 }
453 #endif 484 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698