OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "GrBlurUtils.h" | 8 #include "GrBlurUtils.h" |
9 #include "GrDrawContext.h" | 9 #include "GrDrawContext.h" |
10 #include "GrCaps.h" | 10 #include "GrCaps.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 } | 44 } |
45 drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), maskRect, in
verse); | 45 drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), maskRect, in
verse); |
46 return true; | 46 return true; |
47 } | 47 } |
48 | 48 |
49 static bool draw_with_mask_filter(GrDrawContext* drawContext, | 49 static bool draw_with_mask_filter(GrDrawContext* drawContext, |
50 GrTextureProvider* textureProvider, | 50 GrTextureProvider* textureProvider, |
51 const GrClip& clipData, | 51 const GrClip& clipData, |
52 const SkMatrix& viewMatrix, | 52 const SkMatrix& viewMatrix, |
53 const SkPath& devPath, | 53 const SkPath& devPath, |
54 SkMaskFilter* filter, | 54 const SkMaskFilter* filter, |
55 const SkIRect& clipBounds, | 55 const SkIRect& clipBounds, |
56 GrPaint* grp, | 56 GrPaint* grp, |
57 SkPaint::Style style) { | 57 SkPaint::Style style) { |
58 SkMask srcM, dstM; | 58 SkMask srcM, dstM; |
59 | 59 |
60 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, | 60 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
61 SkMask::kComputeBoundsAndRenderImage_CreateMode, sty
le)) { | 61 SkMask::kComputeBoundsAndRenderImage_CreateMode, sty
le)) { |
62 return false; | 62 return false; |
63 } | 63 } |
64 SkAutoMaskFreeImage autoSrc(srcM.fImage); | 64 SkAutoMaskFreeImage autoSrc(srcM.fImage); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 GrClip clip(clipRect); | 139 GrClip clip(clipRect); |
140 | 140 |
141 // Draw the mask into maskTexture with the path's integerized top-left at | 141 // Draw the mask into maskTexture with the path's integerized top-left at |
142 // the origin using tempPaint. | 142 // the origin using tempPaint. |
143 SkMatrix translate; | 143 SkMatrix translate; |
144 translate.setTranslate(-maskRect->fLeft, -maskRect->fTop); | 144 translate.setTranslate(-maskRect->fLeft, -maskRect->fTop); |
145 drawContext->drawPath(clip, tempPaint, translate, devPath, strokeInfo); | 145 drawContext->drawPath(clip, tempPaint, translate, devPath, strokeInfo); |
146 return mask; | 146 return mask; |
147 } | 147 } |
148 | 148 |
| 149 static void draw_path_with_mask_filter(GrContext* context, |
| 150 GrDrawContext* drawContext, |
| 151 GrRenderTarget* renderTarget, |
| 152 const GrClip& clip, |
| 153 GrPaint* paint, |
| 154 const SkMatrix& viewMatrix, |
| 155 const SkMaskFilter* maskFilter, |
| 156 const SkPathEffect* pathEffect, |
| 157 const GrStrokeInfo& origStrokeInfo, |
| 158 SkPath* pathPtr, |
| 159 bool pathIsMutable) { |
| 160 SkASSERT(maskFilter); |
| 161 |
| 162 SkIRect clipBounds; |
| 163 clip.getConservativeBounds(renderTarget, &clipBounds); |
| 164 SkTLazy<SkPath> tmpPath; |
| 165 GrStrokeInfo strokeInfo(origStrokeInfo); |
| 166 |
| 167 static const SkRect* cullRect = nullptr; // TODO: what is our bounds? |
| 168 |
| 169 if (!strokeInfo.isDashed() && pathEffect && pathEffect->filterPath(tmpPath.i
nit(), *pathPtr, |
| 170 &strokeIn
fo, cullRect)) { |
| 171 pathPtr = tmpPath.get(); |
| 172 pathPtr->setIsVolatile(true); |
| 173 pathIsMutable = true; |
| 174 } |
| 175 if (!strokeInfo.isHairlineStyle()) { |
| 176 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init(); |
| 177 if (strokeInfo.isDashed()) { |
| 178 if (pathEffect->filterPath(strokedPath, *pathPtr, &strokeInfo, cullR
ect)) { |
| 179 pathPtr = strokedPath; |
| 180 pathPtr->setIsVolatile(true); |
| 181 pathIsMutable = true; |
| 182 } |
| 183 strokeInfo.removeDash(); |
| 184 } |
| 185 if (strokeInfo.applyToPath(strokedPath, *pathPtr)) { |
| 186 pathPtr = strokedPath; |
| 187 pathPtr->setIsVolatile(true); |
| 188 pathIsMutable = true; |
| 189 strokeInfo.setFillStyle(); |
| 190 } |
| 191 } |
| 192 |
| 193 // avoid possibly allocating a new path in transform if we can |
| 194 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init(); |
| 195 if (!pathIsMutable) { |
| 196 devPathPtr->setIsVolatile(true); |
| 197 } |
| 198 |
| 199 // transform the path into device space |
| 200 pathPtr->transform(viewMatrix, devPathPtr); |
| 201 |
| 202 SkRect maskRect; |
| 203 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(devPathPtr->getBounds()), |
| 204 clipBounds, |
| 205 viewMatrix, |
| 206 &maskRect)) { |
| 207 SkIRect finalIRect; |
| 208 maskRect.roundOut(&finalIRect); |
| 209 if (clip_bounds_quick_reject(clipBounds, finalIRect)) { |
| 210 // clipped out |
| 211 return; |
| 212 } |
| 213 |
| 214 if (maskFilter->directFilterMaskGPU(context->textureProvider(), |
| 215 drawContext, |
| 216 paint, |
| 217 clip, |
| 218 viewMatrix, |
| 219 strokeInfo, |
| 220 *devPathPtr)) { |
| 221 // the mask filter was able to draw itself directly, so there's noth
ing |
| 222 // left to do. |
| 223 return; |
| 224 } |
| 225 |
| 226 SkAutoTUnref<GrTexture> mask(create_mask_GPU(context, |
| 227 &maskRect, |
| 228 *devPathPtr, |
| 229 strokeInfo, |
| 230 paint->isAntiAlias(), |
| 231 renderTarget->numColorSampl
es())); |
| 232 if (mask) { |
| 233 GrTexture* filtered; |
| 234 |
| 235 if (maskFilter->filterMaskGPU(mask, viewMatrix, maskRect, &filtered,
true)) { |
| 236 // filterMaskGPU gives us ownership of a ref to the result |
| 237 SkAutoTUnref<GrTexture> atu(filtered); |
| 238 if (draw_mask(drawContext, clip, viewMatrix, maskRect, paint, fi
ltered)) { |
| 239 // This path is completely drawn |
| 240 return; |
| 241 } |
| 242 } |
| 243 } |
| 244 } |
| 245 |
| 246 // draw the mask on the CPU - this is a fallthrough path in case the |
| 247 // GPU path fails |
| 248 SkPaint::Style style = strokeInfo.isHairlineStyle() ? SkPaint::kStroke_Style
: |
| 249 SkPaint::kFill_Style; |
| 250 draw_with_mask_filter(drawContext, context->textureProvider(), |
| 251 clip, viewMatrix, *devPathPtr, |
| 252 maskFilter, clipBounds, paint, style); |
| 253 } |
| 254 |
| 255 void GrBlurUtils::drawPathWithMaskFilter(GrContext* context, |
| 256 GrDrawContext* drawContext, |
| 257 GrRenderTarget* rt, |
| 258 const GrClip& clip, |
| 259 const SkPath& origPath, |
| 260 GrPaint* paint, |
| 261 const SkMatrix& viewMatrix, |
| 262 const SkMaskFilter* mf, |
| 263 const SkPathEffect* pe, |
| 264 const GrStrokeInfo& strokeInfo) { |
| 265 SkPath* path = const_cast<SkPath*>(&origPath); |
| 266 draw_path_with_mask_filter(context, drawContext, rt, clip, paint, viewMatrix
, mf, pe, |
| 267 strokeInfo, path, false); |
| 268 } |
| 269 |
149 void GrBlurUtils::drawPathWithMaskFilter(GrContext* context, | 270 void GrBlurUtils::drawPathWithMaskFilter(GrContext* context, |
150 GrDrawContext* drawContext, | 271 GrDrawContext* drawContext, |
151 GrRenderTarget* renderTarget, | 272 GrRenderTarget* renderTarget, |
152 const GrClip& clip, | 273 const GrClip& clip, |
153 const SkPath& origSrcPath, | 274 const SkPath& origSrcPath, |
154 const SkPaint& paint, | 275 const SkPaint& paint, |
155 const SkMatrix& origViewMatrix, | 276 const SkMatrix& origViewMatrix, |
156 const SkMatrix* prePathMatrix, | 277 const SkMatrix* prePathMatrix, |
157 const SkIRect& clipBounds, | 278 const SkIRect& clipBounds, |
158 bool pathIsMutable) { | 279 bool pathIsMutable) { |
159 SkASSERT(!pathIsMutable || origSrcPath.isVolatile()); | 280 SkASSERT(!pathIsMutable || origSrcPath.isVolatile()); |
160 | 281 |
161 GrStrokeInfo strokeInfo(paint); | 282 GrStrokeInfo strokeInfo(paint); |
162 | 283 |
163 // If we have a prematrix, apply it to the path, optimizing for the case | 284 // If we have a prematrix, apply it to the path, optimizing for the case |
164 // where the original path can in fact be modified in place (even though | 285 // where the original path can in fact be modified in place (even though |
165 // its parameter type is const). | 286 // its parameter type is const). |
166 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); | 287 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
167 SkTLazy<SkPath> tmpPath; | 288 SkTLazy<SkPath> tmpPath; |
168 SkTLazy<SkPath> effectPath; | 289 SkTLazy<SkPath> effectPath; |
169 SkPathEffect* pathEffect = paint.getPathEffect(); | 290 SkPathEffect* pathEffect = paint.getPathEffect(); |
170 | 291 |
171 SkMatrix viewMatrix = origViewMatrix; | 292 SkMatrix viewMatrix = origViewMatrix; |
172 | 293 |
173 if (prePathMatrix) { | 294 if (prePathMatrix) { |
174 // stroking, path effects, and blurs are supposed to be applied *after*
the prePathMatrix. | 295 // stroking, path effects, and blurs are supposed to be applied *after*
the prePathMatrix. |
175 // The pre-path-matrix also should not affect shading. | 296 // The pre-path-matrix also should not affect shading. |
176 if (nullptr == paint.getMaskFilter() && nullptr == pathEffect && nullptr
== paint.getShader() && | 297 if (!paint.getMaskFilter() && !pathEffect && !paint.getShader() && |
177 (strokeInfo.isFillStyle() || strokeInfo.isHairlineStyle())) { | 298 (strokeInfo.isFillStyle() || strokeInfo.isHairlineStyle())) { |
178 viewMatrix.preConcat(*prePathMatrix); | 299 viewMatrix.preConcat(*prePathMatrix); |
179 } else { | 300 } else { |
180 SkPath* result = pathPtr; | 301 SkPath* result = pathPtr; |
181 | 302 |
182 if (!pathIsMutable) { | 303 if (!pathIsMutable) { |
183 result = tmpPath.init(); | 304 result = tmpPath.init(); |
184 result->setIsVolatile(true); | 305 result->setIsVolatile(true); |
185 pathIsMutable = true; | 306 pathIsMutable = true; |
186 } | 307 } |
187 // should I push prePathMatrix on our MV stack temporarily, instead | 308 // should I push prePathMatrix on our MV stack temporarily, instead |
188 // of applying it here? See SkDraw.cpp | 309 // of applying it here? See SkDraw.cpp |
189 pathPtr->transform(*prePathMatrix, result); | 310 pathPtr->transform(*prePathMatrix, result); |
190 pathPtr = result; | 311 pathPtr = result; |
191 } | 312 } |
192 } | 313 } |
193 // at this point we're done with prePathMatrix | 314 // at this point we're done with prePathMatrix |
194 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) | 315 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
195 | 316 |
196 GrPaint grPaint; | 317 GrPaint grPaint; |
197 if (!SkPaintToGrPaint(context, paint, viewMatrix, &grPaint)) { | 318 if (!SkPaintToGrPaint(context, paint, viewMatrix, &grPaint)) { |
198 return; | 319 return; |
199 } | 320 } |
200 | 321 |
201 const SkRect* cullRect = nullptr; // TODO: what is our bounds? | 322 if (paint.getMaskFilter()) { |
202 if (!strokeInfo.isDashed() && pathEffect && pathEffect->filterPath(effectPat
h.init(), *pathPtr, | 323 draw_path_with_mask_filter(context, drawContext, renderTarget, clip, &gr
Paint, viewMatrix, |
203 &strokeIn
fo, cullRect)) { | 324 paint.getMaskFilter(), paint.getPathEffect(),
strokeInfo, |
204 pathPtr = effectPath.get(); | 325 pathPtr, pathIsMutable); |
205 pathIsMutable = true; | 326 } else { |
| 327 SkTLazy<SkPath> tmpPath2; |
| 328 if (!strokeInfo.isDashed() && pathEffect && |
| 329 pathEffect->filterPath(tmpPath2.init(), *pathPtr, &strokeInfo, nullp
tr)) { |
| 330 pathPtr = tmpPath2.get(); |
| 331 pathPtr->setIsVolatile(true); |
| 332 pathIsMutable = true; |
| 333 } |
| 334 drawContext->drawPath(clip, grPaint, viewMatrix, *pathPtr, strokeInfo); |
206 } | 335 } |
207 | |
208 if (paint.getMaskFilter()) { | |
209 if (!strokeInfo.isHairlineStyle()) { | |
210 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init(); | |
211 if (strokeInfo.isDashed()) { | |
212 if (pathEffect->filterPath(strokedPath, *pathPtr, &strokeInfo, c
ullRect)) { | |
213 pathPtr = strokedPath; | |
214 pathIsMutable = true; | |
215 } | |
216 strokeInfo.removeDash(); | |
217 } | |
218 if (strokeInfo.applyToPath(strokedPath, *pathPtr)) { | |
219 pathPtr = strokedPath; | |
220 pathIsMutable = true; | |
221 strokeInfo.setFillStyle(); | |
222 } | |
223 } | |
224 | |
225 // avoid possibly allocating a new path in transform if we can | |
226 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init(); | |
227 if (!pathIsMutable) { | |
228 devPathPtr->setIsVolatile(true); | |
229 } | |
230 | |
231 // transform the path into device space | |
232 pathPtr->transform(viewMatrix, devPathPtr); | |
233 | |
234 SkRect maskRect; | |
235 if (paint.getMaskFilter()->canFilterMaskGPU(SkRRect::MakeRect(devPathPtr
->getBounds()), | |
236 clipBounds, | |
237 viewMatrix, | |
238 &maskRect)) { | |
239 SkIRect finalIRect; | |
240 maskRect.roundOut(&finalIRect); | |
241 if (clip_bounds_quick_reject(clipBounds, finalIRect)) { | |
242 // clipped out | |
243 return; | |
244 } | |
245 | |
246 if (paint.getMaskFilter()->directFilterMaskGPU(context->textureProvi
der(), | |
247 drawContext, | |
248 &grPaint, | |
249 clip, | |
250 viewMatrix, | |
251 strokeInfo, | |
252 *devPathPtr)) { | |
253 // the mask filter was able to draw itself directly, so there's
nothing | |
254 // left to do. | |
255 return; | |
256 } | |
257 | |
258 SkAutoTUnref<GrTexture> mask(create_mask_GPU(context, | |
259 &maskRect, | |
260 *devPathPtr, | |
261 strokeInfo, | |
262 grPaint.isAntiAlias(), | |
263 renderTarget->numColorS
amples())); | |
264 if (mask) { | |
265 GrTexture* filtered; | |
266 | |
267 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskR
ect, | |
268 &filtered, true)) { | |
269 // filterMaskGPU gives us ownership of a ref to the result | |
270 SkAutoTUnref<GrTexture> atu(filtered); | |
271 if (draw_mask(drawContext, | |
272 clip, | |
273 viewMatrix, | |
274 maskRect, | |
275 &grPaint, | |
276 filtered)) { | |
277 // This path is completely drawn | |
278 return; | |
279 } | |
280 } | |
281 } | |
282 } | |
283 | |
284 // draw the mask on the CPU - this is a fallthrough path in case the | |
285 // GPU path fails | |
286 SkPaint::Style style = strokeInfo.isHairlineStyle() ? SkPaint::kStroke_S
tyle : | |
287 SkPaint::kFill_Sty
le; | |
288 draw_with_mask_filter(drawContext, context->textureProvider(), | |
289 clip, viewMatrix, *devPathPtr, | |
290 paint.getMaskFilter(), clipBounds, &grPaint, style
); | |
291 return; | |
292 } | |
293 | |
294 drawContext->drawPath(clip, grPaint, viewMatrix, *pathPtr, strokeInfo); | |
295 } | 336 } |
296 | 337 |
OLD | NEW |