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