OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
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 "SkPictureShader.h" | 8 #include "SkPictureShader.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 this->INHERITED::flatten(buffer); | 48 this->INHERITED::flatten(buffer); |
49 | 49 |
50 buffer.write32(fTmx); | 50 buffer.write32(fTmx); |
51 buffer.write32(fTmy); | 51 buffer.write32(fTmy); |
52 buffer.writeBool(NULL != fPicture); | 52 buffer.writeBool(NULL != fPicture); |
53 if (fPicture) { | 53 if (fPicture) { |
54 fPicture->flatten(buffer); | 54 fPicture->flatten(buffer); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 bool SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { | 58 SkShader* SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { |
59 if (!fPicture || (0 == fPicture->width() && 0 == fPicture->height())) { | 59 if (!fPicture || (0 == fPicture->width() && 0 == fPicture->height())) { |
60 return false; | 60 return NULL; |
61 } | 61 } |
62 | 62 |
63 SkMatrix m; | 63 SkMatrix m; |
64 if (this->hasLocalMatrix()) { | 64 if (this->hasLocalMatrix()) { |
65 m.setConcat(matrix, this->getLocalMatrix()); | 65 m.setConcat(matrix, this->getLocalMatrix()); |
66 } else { | 66 } else { |
67 m = matrix; | 67 m = matrix; |
68 } | 68 } |
69 | 69 |
70 // Use a rotation-invariant scale | 70 // Use a rotation-invariant scale |
71 SkPoint scale; | 71 SkPoint scale; |
72 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { | 72 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { |
73 // Decomposition failed, use an approximation. | 73 // Decomposition failed, use an approximation. |
74 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.
getSkewX()), | 74 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.
getSkewX()), |
75 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.
getSkewY())); | 75 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.
getSkewY())); |
76 } | 76 } |
77 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() *
fPicture->height()); | 77 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() *
fPicture->height()); |
78 | 78 |
79 SkISize tileSize = scaledSize.toRound(); | 79 SkISize tileSize = scaledSize.toRound(); |
80 if (tileSize.isEmpty()) { | 80 if (tileSize.isEmpty()) { |
81 return false; | 81 return NULL; |
82 } | 82 } |
83 | 83 |
84 // The actual scale, compensating for rounding. | 84 // The actual scale, compensating for rounding. |
85 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->
width(), | 85 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->
width(), |
86 SkIntToScalar(tileSize.height()) / fPicture-
>height()); | 86 SkIntToScalar(tileSize.height()) / fPicture-
>height()); |
87 | 87 |
88 if (!fCachedShader || tileScale != fCachedTileScale) { | 88 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); |
| 89 |
| 90 if (!fCachedBitmapShader || tileScale != fCachedTileScale || |
| 91 this->getLocalMatrix() != fCachedLocalMatrix) { |
89 SkBitmap bm; | 92 SkBitmap bm; |
90 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { | 93 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
91 return false; | 94 return NULL; |
92 } | 95 } |
93 bm.eraseColor(SK_ColorTRANSPARENT); | 96 bm.eraseColor(SK_ColorTRANSPARENT); |
94 | 97 |
95 SkCanvas canvas(bm); | 98 SkCanvas canvas(bm); |
96 canvas.scale(tileScale.width(), tileScale.height()); | 99 canvas.scale(tileScale.width(), tileScale.height()); |
97 canvas.drawPicture(*fPicture); | 100 canvas.drawPicture(*fPicture); |
98 | 101 |
99 fCachedShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); | 102 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); |
100 fCachedTileScale = tileScale; | 103 fCachedTileScale = tileScale; |
| 104 fCachedLocalMatrix = this->getLocalMatrix(); |
| 105 |
| 106 SkMatrix shaderMatrix = this->getLocalMatrix(); |
| 107 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
| 108 fCachedBitmapShader->setLocalMatrix(shaderMatrix); |
101 } | 109 } |
102 | 110 |
103 SkMatrix shaderMatrix = this->getLocalMatrix(); | 111 // Increment the ref counter inside the mutex to ensure the returned pointer
is still valid. |
104 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); | 112 // Otherwise, the pointer may have been overwritten on a different thread be
fore the object's |
105 fCachedShader->setLocalMatrix(shaderMatrix); | 113 // ref count was incremented. |
106 | 114 fCachedBitmapShader.get()->ref(); |
107 return true; | 115 return fCachedBitmapShader; |
108 } | 116 } |
109 | 117 |
110 bool SkPictureShader::setContext(const SkBitmap& device, | 118 SkShader* SkPictureShader::validInternal(const SkBitmap& device, const SkPaint&
paint, |
111 const SkPaint& paint, | 119 const SkMatrix& matrix, SkMatrix* total
Inverse) const { |
112 const SkMatrix& matrix) { | 120 if (!this->INHERITED::validContext(device, paint, matrix, totalInverse)) { |
113 if (!this->buildBitmapShader(matrix)) { | 121 return NULL; |
114 return false; | |
115 } | 122 } |
116 | 123 |
117 if (!this->INHERITED::setContext(device, paint, matrix)) { | 124 SkShader* bitmapShader = this->buildBitmapShader(matrix); |
118 return false; | 125 if (!bitmapShader) { |
| 126 return NULL; |
119 } | 127 } |
120 | 128 |
121 SkASSERT(fCachedShader); | 129 if (!bitmapShader->validContext(device, paint, matrix)) { |
122 if (!fCachedShader->setContext(device, paint, matrix)) { | 130 bitmapShader->unref(); |
123 this->INHERITED::endContext(); | 131 return NULL; |
124 return false; | |
125 } | 132 } |
126 | 133 |
127 return true; | 134 return bitmapShader; |
128 } | 135 } |
129 | 136 |
130 void SkPictureShader::endContext() { | 137 bool SkPictureShader::validContext(const SkBitmap& device, const SkPaint& paint, |
131 SkASSERT(fCachedShader); | 138 const SkMatrix& matrix, SkMatrix* totalInvers
e) const { |
132 fCachedShader->endContext(); | 139 SkAutoTUnref<SkShader> shader(this->validInternal(device, paint, matrix, tot
alInverse)); |
133 | 140 return shader != NULL; |
134 this->INHERITED::endContext(); | |
135 } | 141 } |
136 | 142 |
137 uint32_t SkPictureShader::getFlags() { | 143 SkShader::Context* SkPictureShader::createContext(const SkBitmap& device, const
SkPaint& paint, |
138 if (NULL != fCachedShader) { | 144 const SkMatrix& matrix, void*
storage) const { |
139 return fCachedShader->getFlags(); | 145 SkShader* bitmapShader = this->validInternal(device, paint, matrix, NULL); |
| 146 if (!bitmapShader) { |
| 147 return NULL; |
140 } | 148 } |
141 return 0; | 149 |
| 150 return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, |
| 151 (*this, device, paint, matrix, bitmapShader)); |
142 } | 152 } |
143 | 153 |
144 SkShader::ShadeProc SkPictureShader::asAShadeProc(void** ctx) { | 154 size_t SkPictureShader::contextSize() const { |
145 if (fCachedShader) { | 155 return sizeof(PictureShaderContext); |
146 return fCachedShader->asAShadeProc(ctx); | |
147 } | |
148 return NULL; | |
149 } | 156 } |
150 | 157 |
151 void SkPictureShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { | 158 SkPictureShader::PictureShaderContext::PictureShaderContext( |
152 SkASSERT(fCachedShader); | 159 const SkPictureShader& shader, const SkBitmap& device, |
153 fCachedShader->shadeSpan(x, y, dstC, count); | 160 const SkPaint& paint, const SkMatrix& matrix, SkShader* bitmapShader) |
| 161 : INHERITED(shader, device, paint, matrix) |
| 162 , fBitmapShader(bitmapShader) |
| 163 { |
| 164 SkASSERT(fBitmapShader); |
| 165 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize()); |
| 166 fBitmapShaderContext = fBitmapShader->createContext( |
| 167 device, paint, matrix, fBitmapShaderContextStorage); |
| 168 SkASSERT(fBitmapShaderContext); |
154 } | 169 } |
155 | 170 |
156 void SkPictureShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) { | 171 SkPictureShader::PictureShaderContext::~PictureShaderContext() { |
157 SkASSERT(fCachedShader); | 172 fBitmapShaderContext->SkShader::Context::~Context(); |
158 fCachedShader->shadeSpan16(x, y, dstC, count); | 173 sk_free(fBitmapShaderContextStorage); |
| 174 } |
| 175 |
| 176 uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
| 177 return fBitmapShaderContext->getFlags(); |
| 178 } |
| 179 |
| 180 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc
(void** ctx) { |
| 181 return fBitmapShaderContext->asAShadeProc(ctx); |
| 182 } |
| 183 |
| 184 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor ds
tC[], int count) { |
| 185 SkASSERT(fBitmapShaderContext); |
| 186 fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
| 187 } |
| 188 |
| 189 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t d
stC[], int count) { |
| 190 SkASSERT(fBitmapShaderContext); |
| 191 fBitmapShaderContext->shadeSpan16(x, y, dstC, count); |
159 } | 192 } |
160 | 193 |
161 #ifndef SK_IGNORE_TO_STRING | 194 #ifndef SK_IGNORE_TO_STRING |
162 void SkPictureShader::toString(SkString* str) const { | 195 void SkPictureShader::toString(SkString* str) const { |
163 static const char* gTileModeName[SkShader::kTileModeCount] = { | 196 static const char* gTileModeName[SkShader::kTileModeCount] = { |
164 "clamp", "repeat", "mirror" | 197 "clamp", "repeat", "mirror" |
165 }; | 198 }; |
166 | 199 |
167 str->appendf("PictureShader: [%d:%d] ", | 200 str->appendf("PictureShader: [%d:%d] ", |
168 fPicture ? fPicture->width() : 0, | 201 fPicture ? fPicture->width() : 0, |
169 fPicture ? fPicture->height() : 0); | 202 fPicture ? fPicture->height() : 0); |
170 | 203 |
171 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); | 204 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
172 | 205 |
173 this->INHERITED::toString(str); | 206 this->INHERITED::toString(str); |
174 } | 207 } |
175 #endif | 208 #endif |
176 | 209 |
177 #if SK_SUPPORT_GPU | 210 #if SK_SUPPORT_GPU |
178 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai
nt) const { | 211 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai
nt) const { |
179 if (!this->buildBitmapShader(context->getMatrix())) { | 212 SkAutoTUnref<SkShader> bitmapShader(this->buildBitmapShader(context->getMatr
ix())); |
| 213 if (!bitmapShader) { |
180 return NULL; | 214 return NULL; |
181 } | 215 } |
182 SkASSERT(fCachedShader); | 216 return bitmapShader->asNewEffect(context, paint); |
183 return fCachedShader->asNewEffect(context, paint); | |
184 } | 217 } |
185 #endif | 218 #endif |
OLD | NEW |