1 | /* |
2 | * Copyright 2011 Google Inc. |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #include "src/gpu/SkGpuDevice.h" |
9 | |
10 | #include "include/core/SkImageFilter.h" |
11 | #include "include/core/SkPathEffect.h" |
12 | #include "include/core/SkPicture.h" |
13 | #include "include/core/SkSurface.h" |
14 | #include "include/core/SkVertices.h" |
15 | #include "include/effects/SkRuntimeEffect.h" |
16 | #include "include/gpu/GrContext.h" |
17 | #include "include/private/SkShadowFlags.h" |
18 | #include "include/private/SkTo.h" |
19 | #include "src/core/SkCanvasPriv.h" |
20 | #include "src/core/SkClipStack.h" |
21 | #include "src/core/SkDraw.h" |
22 | #include "src/core/SkImageFilterCache.h" |
23 | #include "src/core/SkImageFilter_Base.h" |
24 | #include "src/core/SkLatticeIter.h" |
25 | #include "src/core/SkPictureData.h" |
26 | #include "src/core/SkRRectPriv.h" |
27 | #include "src/core/SkRasterClip.h" |
28 | #include "src/core/SkRecord.h" |
29 | #include "src/core/SkSpecialImage.h" |
30 | #include "src/core/SkStroke.h" |
31 | #include "src/core/SkTLazy.h" |
32 | #include "src/core/SkVerticesPriv.h" |
33 | #include "src/gpu/GrBitmapTextureMaker.h" |
34 | #include "src/gpu/GrBlurUtils.h" |
35 | #include "src/gpu/GrContextPriv.h" |
36 | #include "src/gpu/GrGpu.h" |
37 | #include "src/gpu/GrImageTextureMaker.h" |
38 | #include "src/gpu/GrRenderTargetContextPriv.h" |
39 | #include "src/gpu/GrStyle.h" |
40 | #include "src/gpu/GrSurfaceProxyPriv.h" |
41 | #include "src/gpu/GrTextureAdjuster.h" |
42 | #include "src/gpu/GrTracing.h" |
43 | #include "src/gpu/SkGr.h" |
44 | #include "src/gpu/geometry/GrShape.h" |
45 | #include "src/gpu/text/GrTextTarget.h" |
46 | #include "src/image/SkImage_Base.h" |
47 | #include "src/image/SkReadPixelsRec.h" |
48 | #include "src/image/SkSurface_Gpu.h" |
49 | #include "src/utils/SkUTF.h" |
50 | |
51 | #define ASSERT_SINGLE_OWNER \ |
52 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->priv().singleOwner());) |
53 | |
54 | |
55 | /////////////////////////////////////////////////////////////////////////////// |
56 | |
57 | /** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation |
58 | should fail. */ |
59 | bool SkGpuDevice::CheckAlphaTypeAndGetFlags( |
60 | const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) { |
61 | *flags = 0; |
62 | if (info) { |
63 | switch (info->alphaType()) { |
64 | case kPremul_SkAlphaType: |
65 | break; |
66 | case kOpaque_SkAlphaType: |
67 | *flags |= SkGpuDevice::kIsOpaque_Flag; |
68 | break; |
69 | default: // If it is unpremul or unknown don't try to render |
70 | return false; |
71 | } |
72 | } |
73 | if (kClear_InitContents == init) { |
74 | *flags |= kNeedClear_Flag; |
75 | } |
76 | return true; |
77 | } |
78 | |
79 | sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, |
80 | std::unique_ptr<GrRenderTargetContext> renderTargetContext, |
81 | InitContents init) { |
82 | if (!renderTargetContext || context->priv().abandoned()) { |
83 | return nullptr; |
84 | } |
85 | |
86 | SkColorType ct = GrColorTypeToSkColorType(renderTargetContext->colorInfo().colorType()); |
87 | |
88 | unsigned flags; |
89 | if (!context->colorTypeSupportedAsSurface(ct) || |
90 | !CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) { |
91 | return nullptr; |
92 | } |
93 | return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext), flags)); |
94 | } |
95 | |
96 | sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted, |
97 | const SkImageInfo& info, int sampleCount, |
98 | GrSurfaceOrigin origin, const SkSurfaceProps* props, |
99 | GrMipMapped mipMapped, InitContents init) { |
100 | unsigned flags; |
101 | if (!context->colorTypeSupportedAsSurface(info.colorType()) || |
102 | !CheckAlphaTypeAndGetFlags(&info, init, &flags)) { |
103 | return nullptr; |
104 | } |
105 | |
106 | auto renderTargetContext = |
107 | MakeRenderTargetContext(context, budgeted, info, sampleCount, origin, props, mipMapped); |
108 | if (!renderTargetContext) { |
109 | return nullptr; |
110 | } |
111 | |
112 | return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext), flags)); |
113 | } |
114 | |
115 | static SkImageInfo make_info(GrRenderTargetContext* context, bool opaque) { |
116 | SkColorType colorType = GrColorTypeToSkColorType(context->colorInfo().colorType()); |
117 | return SkImageInfo::Make(context->width(), context->height(), colorType, |
118 | opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType, |
119 | context->colorInfo().refColorSpace()); |
120 | } |
121 | |
122 | SkGpuDevice::SkGpuDevice(GrContext* context, |
123 | std::unique_ptr<GrRenderTargetContext> renderTargetContext, |
124 | unsigned flags) |
125 | : INHERITED(make_info(renderTargetContext.get(), SkToBool(flags & kIsOpaque_Flag)), |
126 | renderTargetContext->surfaceProps()) |
127 | , fContext(SkRef(context)) |
128 | , fRenderTargetContext(std::move(renderTargetContext)) { |
129 | if (flags & kNeedClear_Flag) { |
130 | this->clearAll(); |
131 | } |
132 | } |
133 | |
134 | std::unique_ptr<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext( |
135 | GrContext* context, |
136 | SkBudgeted budgeted, |
137 | const SkImageInfo& origInfo, |
138 | int sampleCount, |
139 | GrSurfaceOrigin origin, |
140 | const SkSurfaceProps* surfaceProps, |
141 | GrMipMapped mipMapped) { |
142 | if (!context) { |
143 | return nullptr; |
144 | } |
145 | |
146 | // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case |
147 | // they need to be exact. |
148 | return GrRenderTargetContext::Make( |
149 | context, SkColorTypeToGrColorType(origInfo.colorType()), origInfo.refColorSpace(), |
150 | SkBackingFit::kExact, origInfo.dimensions(), sampleCount, mipMapped, GrProtected::kNo, |
151 | origin, budgeted, surfaceProps); |
152 | } |
153 | |
154 | sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg, |
155 | int left, int top, |
156 | SkIPoint* offset, |
157 | const SkImageFilter* filter) { |
158 | SkASSERT(srcImg->isTextureBacked()); |
159 | SkASSERT(filter); |
160 | |
161 | SkMatrix matrix = this->localToDevice(); |
162 | matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
163 | const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top); |
164 | sk_sp<SkImageFilterCache> cache(this->getImageFilterCache()); |
165 | SkColorType colorType = GrColorTypeToSkColorType(fRenderTargetContext->colorInfo().colorType()); |
166 | if (colorType == kUnknown_SkColorType) { |
167 | colorType = kRGBA_8888_SkColorType; |
168 | } |
169 | SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), colorType, |
170 | fRenderTargetContext->colorInfo().colorSpace(), srcImg); |
171 | |
172 | return as_IFB(filter)->filterImage(ctx).imageAndOffset(offset); |
173 | } |
174 | |
175 | /////////////////////////////////////////////////////////////////////////////// |
176 | |
177 | bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) { |
178 | ASSERT_SINGLE_OWNER |
179 | |
180 | if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) { |
181 | return false; |
182 | } |
183 | |
184 | return fRenderTargetContext->readPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), {x, y}); |
185 | } |
186 | |
187 | bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) { |
188 | ASSERT_SINGLE_OWNER |
189 | |
190 | if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) { |
191 | return false; |
192 | } |
193 | |
194 | return fRenderTargetContext->writePixels(pm.info(), pm.addr(), pm.rowBytes(), {x, y}); |
195 | } |
196 | |
197 | bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) { |
198 | ASSERT_SINGLE_OWNER |
199 | return false; |
200 | } |
201 | |
202 | GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() { |
203 | ASSERT_SINGLE_OWNER |
204 | return fRenderTargetContext.get(); |
205 | } |
206 | |
207 | void SkGpuDevice::clearAll() { |
208 | ASSERT_SINGLE_OWNER |
209 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "clearAll" , fContext.get()); |
210 | |
211 | SkIRect rect = SkIRect::MakeWH(this->width(), this->height()); |
212 | fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT, |
213 | GrRenderTargetContext::CanClearFullscreen::kYes); |
214 | } |
215 | |
216 | void SkGpuDevice::replaceRenderTargetContext(std::unique_ptr<GrRenderTargetContext> rtc, |
217 | SkSurface::ContentChangeMode mode) { |
218 | SkASSERT(rtc->width() == this->width()); |
219 | SkASSERT(rtc->height() == this->height()); |
220 | SkASSERT(rtc->numSamples() == fRenderTargetContext->numSamples()); |
221 | SkASSERT(rtc->asSurfaceProxy()->priv().isExact()); |
222 | if (mode == SkSurface::kRetain_ContentChangeMode) { |
223 | if (this->context()->abandoned()) { |
224 | return; |
225 | } |
226 | |
227 | SkASSERT(fRenderTargetContext->asTextureProxy()); |
228 | SkAssertResult(rtc->blitTexture(fRenderTargetContext->readSurfaceView(), |
229 | SkIRect::MakeWH(this->width(), this->height()), |
230 | SkIPoint::Make(0,0))); |
231 | } |
232 | |
233 | fRenderTargetContext = std::move(rtc); |
234 | } |
235 | |
236 | void SkGpuDevice::replaceRenderTargetContext(SkSurface::ContentChangeMode mode) { |
237 | ASSERT_SINGLE_OWNER |
238 | |
239 | SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted(); |
240 | |
241 | // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a |
242 | // kExact-backed render target context. |
243 | auto newRTC = MakeRenderTargetContext(this->context(), |
244 | budgeted, |
245 | this->imageInfo(), |
246 | fRenderTargetContext->numSamples(), |
247 | fRenderTargetContext->origin(), |
248 | &this->surfaceProps(), |
249 | fRenderTargetContext->mipMapped()); |
250 | if (!newRTC) { |
251 | return; |
252 | } |
253 | this->replaceRenderTargetContext(std::move(newRTC), mode); |
254 | } |
255 | |
256 | /////////////////////////////////////////////////////////////////////////////// |
257 | |
258 | void SkGpuDevice::drawPaint(const SkPaint& paint) { |
259 | ASSERT_SINGLE_OWNER |
260 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawPaint" , fContext.get()); |
261 | |
262 | GrPaint grPaint; |
263 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
264 | this->localToDevice(), &grPaint)) { |
265 | return; |
266 | } |
267 | |
268 | fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->localToDevice()); |
269 | } |
270 | |
271 | static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) { |
272 | switch (mode) { |
273 | case SkCanvas::kPoints_PointMode: |
274 | return GrPrimitiveType::kPoints; |
275 | case SkCanvas::kLines_PointMode: |
276 | return GrPrimitiveType::kLines; |
277 | case SkCanvas::kPolygon_PointMode: |
278 | return GrPrimitiveType::kLineStrip; |
279 | } |
280 | SK_ABORT("Unexpected mode" ); |
281 | } |
282 | |
283 | void SkGpuDevice::drawPoints(SkCanvas::PointMode mode, |
284 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
285 | ASSERT_SINGLE_OWNER |
286 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawPoints" , fContext.get()); |
287 | SkScalar width = paint.getStrokeWidth(); |
288 | if (width < 0) { |
289 | return; |
290 | } |
291 | |
292 | if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) { |
293 | GrStyle style(paint, SkPaint::kStroke_Style); |
294 | GrPaint grPaint; |
295 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
296 | this->localToDevice(), &grPaint)) { |
297 | return; |
298 | } |
299 | SkPath path; |
300 | path.setIsVolatile(true); |
301 | path.moveTo(pts[0]); |
302 | path.lineTo(pts[1]); |
303 | fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
304 | this->localToDevice(), path, style); |
305 | return; |
306 | } |
307 | |
308 | SkScalar scales[2]; |
309 | bool isHairline = (0 == width) || |
310 | (1 == width && this->localToDevice().getMinMaxScales(scales) && |
311 | SkScalarNearlyEqual(scales[0], 1.f) && SkScalarNearlyEqual(scales[1], 1.f)); |
312 | // we only handle non-antialiased hairlines and paints without path effects or mask filters, |
313 | // else we let the SkDraw call our drawPath() |
314 | if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) { |
315 | SkRasterClip rc(this->devClipBounds()); |
316 | SkDraw draw; |
317 | draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0); |
318 | draw.fMatrix = &this->localToDevice(); |
319 | draw.fRC = &rc; |
320 | draw.drawPoints(mode, count, pts, paint, this); |
321 | return; |
322 | } |
323 | |
324 | GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode); |
325 | |
326 | const SkMatrix* viewMatrix = &this->localToDevice(); |
327 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
328 | // This offsetting in device space matches the expectations of the Android framework for non-AA |
329 | // points and lines. |
330 | SkMatrix tempMatrix; |
331 | if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) { |
332 | tempMatrix = *viewMatrix; |
333 | static const SkScalar kOffset = 0.063f; // Just greater than 1/16. |
334 | tempMatrix.postTranslate(kOffset, kOffset); |
335 | viewMatrix = &tempMatrix; |
336 | } |
337 | #endif |
338 | |
339 | GrPaint grPaint; |
340 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, *viewMatrix, |
341 | &grPaint)) { |
342 | return; |
343 | } |
344 | |
345 | static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode; |
346 | sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr, |
347 | nullptr); |
348 | |
349 | fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix, |
350 | std::move(vertices), &primitiveType); |
351 | } |
352 | |
353 | /////////////////////////////////////////////////////////////////////////////// |
354 | |
355 | void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) { |
356 | ASSERT_SINGLE_OWNER |
357 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawRect" , fContext.get()); |
358 | |
359 | GrStyle style(paint); |
360 | |
361 | // A couple reasons we might need to call drawPath. |
362 | if (paint.getMaskFilter() || paint.getPathEffect()) { |
363 | GrShape shape(rect, style); |
364 | |
365 | GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), |
366 | this->clip(), paint, this->localToDevice(), shape); |
367 | return; |
368 | } |
369 | |
370 | GrPaint grPaint; |
371 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
372 | this->localToDevice(), &grPaint)) { |
373 | return; |
374 | } |
375 | |
376 | fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
377 | this->localToDevice(), rect, &style); |
378 | } |
379 | |
380 | void SkGpuDevice::drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
381 | SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color, |
382 | SkBlendMode mode) { |
383 | ASSERT_SINGLE_OWNER |
384 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawEdgeAAQuad" , fContext.get()); |
385 | |
386 | SkPMColor4f dstColor = SkColor4fPrepForDst(color, fRenderTargetContext->colorInfo()).premul(); |
387 | |
388 | GrPaint grPaint; |
389 | grPaint.setColor4f(dstColor); |
390 | if (mode != SkBlendMode::kSrcOver) { |
391 | grPaint.setXPFactory(SkBlendMode_AsXPFactory(mode)); |
392 | } |
393 | |
394 | // This is exclusively meant for tiling operations, so keep AA enabled to handle MSAA seaming |
395 | GrQuadAAFlags grAA = SkToGrQuadAAFlags(aaFlags); |
396 | if (clip) { |
397 | // Use fillQuadWithEdgeAA |
398 | fRenderTargetContext->fillQuadWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA, |
399 | this->localToDevice(), clip, nullptr); |
400 | } else { |
401 | // Use fillRectWithEdgeAA to preserve mathematical properties of dst being rectangular |
402 | fRenderTargetContext->fillRectWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA, |
403 | this->localToDevice(), rect); |
404 | } |
405 | } |
406 | |
407 | /////////////////////////////////////////////////////////////////////////////// |
408 | |
409 | void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
410 | ASSERT_SINGLE_OWNER |
411 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawRRect" , fContext.get()); |
412 | |
413 | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
414 | if (mf) { |
415 | if (mf->hasFragmentProcessor()) { |
416 | mf = nullptr; // already handled in SkPaintToGrPaint |
417 | } |
418 | } |
419 | |
420 | GrStyle style(paint); |
421 | |
422 | if (mf || style.pathEffect()) { |
423 | // A path effect will presumably transform this rrect into something else. |
424 | GrShape shape(rrect, style); |
425 | |
426 | GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), |
427 | this->clip(), paint, this->localToDevice(), shape); |
428 | return; |
429 | } |
430 | |
431 | SkASSERT(!style.pathEffect()); |
432 | |
433 | GrPaint grPaint; |
434 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
435 | this->localToDevice(), &grPaint)) { |
436 | return; |
437 | } |
438 | |
439 | fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
440 | this->localToDevice(), rrect, style); |
441 | } |
442 | |
443 | |
444 | void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { |
445 | ASSERT_SINGLE_OWNER |
446 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawDRRect" , fContext.get()); |
447 | if (outer.isEmpty()) { |
448 | return; |
449 | } |
450 | |
451 | if (inner.isEmpty()) { |
452 | return this->drawRRect(outer, paint); |
453 | } |
454 | |
455 | SkStrokeRec stroke(paint); |
456 | |
457 | if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) { |
458 | GrPaint grPaint; |
459 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
460 | this->localToDevice(), &grPaint)) { |
461 | return; |
462 | } |
463 | |
464 | fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint), |
465 | GrAA(paint.isAntiAlias()), this->localToDevice(), |
466 | outer, inner); |
467 | return; |
468 | } |
469 | |
470 | SkPath path; |
471 | path.setIsVolatile(true); |
472 | path.addRRect(outer); |
473 | path.addRRect(inner); |
474 | path.setFillType(SkPathFillType::kEvenOdd); |
475 | |
476 | // TODO: We are losing the possible mutability of the path here but this should probably be |
477 | // fixed by upgrading GrShape to handle DRRects. |
478 | GrShape shape(path, paint); |
479 | |
480 | GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(), |
481 | paint, this->localToDevice(), shape); |
482 | } |
483 | |
484 | |
485 | ///////////////////////////////////////////////////////////////////////////// |
486 | |
487 | void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) { |
488 | if (paint.getMaskFilter()) { |
489 | SkPath path; |
490 | region.getBoundaryPath(&path); |
491 | path.setIsVolatile(true); |
492 | return this->drawPath(path, paint, true); |
493 | } |
494 | |
495 | GrPaint grPaint; |
496 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
497 | this->localToDevice(), &grPaint)) { |
498 | return; |
499 | } |
500 | |
501 | fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
502 | this->localToDevice(), region, GrStyle(paint)); |
503 | } |
504 | |
505 | void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) { |
506 | ASSERT_SINGLE_OWNER |
507 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawOval" , fContext.get()); |
508 | |
509 | if (paint.getMaskFilter()) { |
510 | // The RRect path can handle special case blurring |
511 | SkRRect rr = SkRRect::MakeOval(oval); |
512 | return this->drawRRect(rr, paint); |
513 | } |
514 | |
515 | GrPaint grPaint; |
516 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
517 | this->localToDevice(), &grPaint)) { |
518 | return; |
519 | } |
520 | |
521 | fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
522 | this->localToDevice(), oval, GrStyle(paint)); |
523 | } |
524 | |
525 | void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle, |
526 | SkScalar sweepAngle, bool useCenter, const SkPaint& paint) { |
527 | ASSERT_SINGLE_OWNER |
528 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawArc" , fContext.get()); |
529 | if (paint.getMaskFilter()) { |
530 | this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint); |
531 | return; |
532 | } |
533 | GrPaint grPaint; |
534 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
535 | this->localToDevice(), &grPaint)) { |
536 | return; |
537 | } |
538 | |
539 | fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
540 | this->localToDevice(), oval, startAngle, sweepAngle, useCenter, |
541 | GrStyle(paint)); |
542 | } |
543 | |
544 | #include "include/core/SkMaskFilter.h" |
545 | |
546 | /////////////////////////////////////////////////////////////////////////////// |
547 | void SkGpuDevice::drawStrokedLine(const SkPoint points[2], |
548 | const SkPaint& origPaint) { |
549 | ASSERT_SINGLE_OWNER |
550 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawStrokedLine" , fContext.get()); |
551 | // Adding support for round capping would require a |
552 | // GrRenderTargetContext::fillRRectWithLocalMatrix entry point |
553 | SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap()); |
554 | SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle()); |
555 | SkASSERT(!origPaint.getPathEffect()); |
556 | SkASSERT(!origPaint.getMaskFilter()); |
557 | |
558 | const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth(); |
559 | SkASSERT(halfWidth > 0); |
560 | |
561 | SkVector v = points[1] - points[0]; |
562 | |
563 | SkScalar length = SkPoint::Normalize(&v); |
564 | if (!length) { |
565 | v.fX = 1.0f; |
566 | v.fY = 0.0f; |
567 | } |
568 | |
569 | SkPaint newPaint(origPaint); |
570 | newPaint.setStyle(SkPaint::kFill_Style); |
571 | |
572 | SkScalar xtraLength = 0.0f; |
573 | if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) { |
574 | xtraLength = halfWidth; |
575 | } |
576 | |
577 | SkPoint mid = points[0] + points[1]; |
578 | mid.scale(0.5f); |
579 | |
580 | SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength, |
581 | mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength); |
582 | SkMatrix m; |
583 | m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY); |
584 | |
585 | SkMatrix local = m; |
586 | |
587 | m.postConcat(this->localToDevice()); |
588 | |
589 | GrPaint grPaint; |
590 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), newPaint, m, |
591 | &grPaint)) { |
592 | return; |
593 | } |
594 | |
595 | fRenderTargetContext->fillRectWithLocalMatrix( |
596 | this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local); |
597 | } |
598 | |
599 | void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) { |
600 | ASSERT_SINGLE_OWNER |
601 | if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) { |
602 | SkPoint points[2]; |
603 | if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 && |
604 | !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() && |
605 | this->localToDevice().preservesRightAngles() && origSrcPath.isLine(points)) { |
606 | // Path-based stroking looks better for thin rects |
607 | SkScalar strokeWidth = this->localToDevice().getMaxScale() * paint.getStrokeWidth(); |
608 | if (strokeWidth >= 1.0f) { |
609 | // Round capping support is currently disabled b.c. it would require a RRect |
610 | // GrDrawOp that takes a localMatrix. |
611 | this->drawStrokedLine(points, paint); |
612 | return; |
613 | } |
614 | } |
615 | } |
616 | |
617 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawPath" , fContext.get()); |
618 | if (!paint.getMaskFilter()) { |
619 | GrPaint grPaint; |
620 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, |
621 | this->localToDevice(), &grPaint)) { |
622 | return; |
623 | } |
624 | fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()), |
625 | this->localToDevice(), origSrcPath, GrStyle(paint)); |
626 | return; |
627 | } |
628 | |
629 | // TODO: losing possible mutability of 'origSrcPath' here |
630 | GrShape shape(origSrcPath, paint); |
631 | |
632 | GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(), |
633 | paint, this->localToDevice(), shape); |
634 | } |
635 | |
636 | void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint, |
637 | SkImage* clipImage, const SkMatrix& clipMatrix) { |
638 | SkASSERT(!paint.getMaskFilter()); |
639 | |
640 | ASSERT_SINGLE_OWNER |
641 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawSpecial" , fContext.get()); |
642 | |
643 | sk_sp<SkSpecialImage> result; |
644 | if (paint.getImageFilter()) { |
645 | SkIPoint offset = { 0, 0 }; |
646 | |
647 | result = this->filterTexture(special, left, top, &offset, paint.getImageFilter()); |
648 | if (!result) { |
649 | return; |
650 | } |
651 | |
652 | left += offset.fX; |
653 | top += offset.fY; |
654 | } else { |
655 | result = sk_ref_sp(special); |
656 | } |
657 | |
658 | SkASSERT(result->isTextureBacked()); |
659 | GrSurfaceProxyView view = result->view(this->context()); |
660 | if (!view) { |
661 | return; |
662 | } |
663 | |
664 | SkMatrix ctm = this->localToDevice(); |
665 | ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top)); |
666 | |
667 | SkPaint tmpUnfiltered(paint); |
668 | tmpUnfiltered.setImageFilter(nullptr); |
669 | |
670 | auto fp = GrTextureEffect::Make(std::move(view), special->alphaType()); |
671 | fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(), |
672 | fRenderTargetContext->colorInfo().colorSpace()); |
673 | if (GrColorTypeIsAlphaOnly(SkColorTypeToGrColorType(result->colorType()))) { |
674 | fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp)); |
675 | } else { |
676 | if (paint.getColor4f().isOpaque()) { |
677 | fp = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false); |
678 | } else { |
679 | fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp)); |
680 | } |
681 | } |
682 | |
683 | GrPaint grPaint; |
684 | if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorInfo(), |
685 | tmpUnfiltered, std::move(fp), &grPaint)) { |
686 | return; |
687 | } |
688 | |
689 | const SkIRect& subset = result->subset(); |
690 | SkRect dstRect = SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height())); |
691 | SkRect srcRect = SkRect::Make(subset); |
692 | if (clipImage) { |
693 | // Add the image as a simple texture effect applied to coverage. Accessing content outside |
694 | // of the clip image should behave as if it were a decal (i.e. zero coverage). However, to |
695 | // limit pixels touched and hardware checks, we draw the clip image geometry to get the |
696 | // decal effect. |
697 | auto filter = paint.getFilterQuality() > kNone_SkFilterQuality |
698 | ? GrSamplerState::Filter::kBilerp |
699 | : GrSamplerState::Filter::kNearest; |
700 | GrSurfaceProxyView clipView = as_IB(clipImage)->refView(this->context(), GrMipMapped::kNo); |
701 | // Fold clip matrix into ctm |
702 | ctm.preConcat(clipMatrix); |
703 | SkMatrix inverseClipMatrix; |
704 | |
705 | std::unique_ptr<GrFragmentProcessor> cfp; |
706 | if (clipView && ctm.invert(&inverseClipMatrix)) { |
707 | GrColorType srcColorType = SkColorTypeToGrColorType(clipImage->colorType()); |
708 | |
709 | cfp = GrTextureEffect::Make(std::move(clipView), clipImage->alphaType(), |
710 | inverseClipMatrix, filter); |
711 | if (srcColorType != GrColorType::kAlpha_8) { |
712 | cfp = GrFragmentProcessor::SwizzleOutput(std::move(cfp), GrSwizzle::AAAA()); |
713 | } |
714 | } |
715 | |
716 | if (cfp) { |
717 | // If the grPaint already has coverage, this adds an additional stage that multiples |
718 | // the image's alpha channel with the prior coverage. |
719 | grPaint.addCoverageFragmentProcessor(std::move(cfp)); |
720 | |
721 | // Undo the offset that was needed for shader coord transforms to get the transform for |
722 | // the actual drawn geometry. |
723 | ctm.postTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
724 | inverseClipMatrix.preTranslate(-SkIntToScalar(left), -SkIntToScalar(top)); |
725 | SkRect clipGeometry = SkRect::MakeWH(clipImage->width(), clipImage->height()); |
726 | if (!clipGeometry.contains(inverseClipMatrix.mapRect(dstRect))) { |
727 | // Draw the clip geometry since it is smaller, using dstRect as an extra scissor |
728 | SkClipStack clip(this->cs()); |
729 | clip.clipDevRect(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()), |
730 | SkClipOp::kIntersect); |
731 | SkMatrix local = SkMatrix::Concat(SkMatrix::MakeRectToRect( |
732 | dstRect, srcRect, SkMatrix::kFill_ScaleToFit), ctm); |
733 | fRenderTargetContext->fillRectWithLocalMatrix(GrClipStackClip(&clip), |
734 | std::move(grPaint), GrAA(paint.isAntiAlias()), ctm, clipGeometry, local); |
735 | return; |
736 | } |
737 | // Else fall through and draw the subset since that is contained in the clip geometry |
738 | } |
739 | // Else some issue configuring the coverage FP, so just draw without the clip mask image |
740 | } |
741 | // Draw directly in screen space, possibly with an extra coverage processor |
742 | fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint), |
743 | GrAA(paint.isAntiAlias()), SkMatrix::I(), dstRect, srcRect); |
744 | } |
745 | |
746 | sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) { |
747 | // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's |
748 | // semantics). Since this is cached we would have to bake the fit into the cache key though. |
749 | auto view = GrMakeCachedBitmapProxyView(fContext.get(), bitmap); |
750 | if (!view) { |
751 | return nullptr; |
752 | } |
753 | |
754 | const SkIRect rect = SkIRect::MakeSize(view.proxy()->dimensions()); |
755 | |
756 | // GrMakeCachedBitmapProxyView creates a tight copy of 'bitmap' so we don't have to subset |
757 | // the special image |
758 | return SkSpecialImage::MakeDeferredFromGpu(fContext.get(), |
759 | rect, |
760 | bitmap.getGenerationID(), |
761 | std::move(view), |
762 | SkColorTypeToGrColorType(bitmap.colorType()), |
763 | bitmap.refColorSpace(), |
764 | &this->surfaceProps()); |
765 | } |
766 | |
767 | sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) { |
768 | SkPixmap pm; |
769 | if (image->isTextureBacked()) { |
770 | const GrSurfaceProxyView* view = as_IB(image)->view(this->context()); |
771 | SkASSERT(view); |
772 | |
773 | return SkSpecialImage::MakeDeferredFromGpu(fContext.get(), |
774 | SkIRect::MakeWH(image->width(), image->height()), |
775 | image->uniqueID(), |
776 | *view, |
777 | SkColorTypeToGrColorType(image->colorType()), |
778 | image->refColorSpace(), |
779 | &this->surfaceProps()); |
780 | } else if (image->peekPixels(&pm)) { |
781 | SkBitmap bm; |
782 | |
783 | bm.installPixels(pm); |
784 | return this->makeSpecial(bm); |
785 | } else { |
786 | return nullptr; |
787 | } |
788 | } |
789 | |
790 | sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial(const SkIRect& subset, bool forceCopy) { |
791 | GrRenderTargetContext* rtc = this->accessRenderTargetContext(); |
792 | |
793 | // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image |
794 | // since it would require us to make a copy of the underlying VkImage which we don't have access |
795 | // to. Additionaly we can't stop and start the render pass that is used with the secondary |
796 | // command buffer. |
797 | if (rtc->wrapsVkSecondaryCB()) { |
798 | return nullptr; |
799 | } |
800 | |
801 | SkASSERT(rtc->asSurfaceProxy()); |
802 | |
803 | SkIRect finalSubset = subset; |
804 | GrSurfaceProxyView view = rtc->readSurfaceView(); |
805 | if (forceCopy || !view.asTextureProxy()) { |
806 | // When the device doesn't have a texture, or a copy is requested, we create a temporary |
807 | // texture that matches the device contents |
808 | view = GrSurfaceProxyView::Copy(fContext.get(), |
809 | std::move(view), |
810 | GrMipMapped::kNo, // Don't auto generate mips |
811 | subset, |
812 | SkBackingFit::kApprox, |
813 | SkBudgeted::kYes); // Always budgeted |
814 | if (!view) { |
815 | return nullptr; |
816 | } |
817 | // Since this copied only the requested subset, the special image wrapping the proxy no |
818 | // longer needs the original subset. |
819 | finalSubset = SkIRect::MakeSize(view.dimensions()); |
820 | } |
821 | |
822 | GrColorType ct = SkColorTypeToGrColorType(this->imageInfo().colorType()); |
823 | |
824 | return SkSpecialImage::MakeDeferredFromGpu(fContext.get(), |
825 | finalSubset, |
826 | kNeedNewImageUniqueID_SpecialImage, |
827 | std::move(view), |
828 | ct, |
829 | this->imageInfo().refColorSpace(), |
830 | &this->surfaceProps()); |
831 | } |
832 | |
833 | void SkGpuDevice::drawDevice(SkBaseDevice* device, |
834 | int left, int top, const SkPaint& paint) { |
835 | SkASSERT(!paint.getImageFilter()); |
836 | SkASSERT(!paint.getMaskFilter()); |
837 | |
838 | ASSERT_SINGLE_OWNER |
839 | // clear of the source device must occur before CHECK_SHOULD_DRAW |
840 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawDevice" , fContext.get()); |
841 | |
842 | // drawDevice is defined to be in device coords. |
843 | SkGpuDevice* dev = static_cast<SkGpuDevice*>(device); |
844 | sk_sp<SkSpecialImage> srcImg(dev->snapSpecial(SkIRect::MakeWH(dev->width(), dev->height()))); |
845 | if (!srcImg) { |
846 | return; |
847 | } |
848 | |
849 | this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I()); |
850 | } |
851 | |
852 | void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
853 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
854 | ASSERT_SINGLE_OWNER |
855 | GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone; |
856 | this->drawImageQuad(image, src, &dst, nullptr, GrAA(paint.isAntiAlias()), aaFlags, nullptr, |
857 | paint, constraint); |
858 | } |
859 | |
860 | // When drawing nine-patches or n-patches, cap the filter quality at kBilerp. |
861 | static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) { |
862 | if (paint.getFilterQuality() == kNone_SkFilterQuality) { |
863 | return GrSamplerState::Filter::kNearest; |
864 | } |
865 | |
866 | return GrSamplerState::Filter::kBilerp; |
867 | } |
868 | |
869 | void SkGpuDevice::drawImageNine(const SkImage* image, |
870 | const SkIRect& center, const SkRect& dst, const SkPaint& paint) { |
871 | ASSERT_SINGLE_OWNER |
872 | uint32_t pinnedUniqueID; |
873 | auto iter = std::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst); |
874 | if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) { |
875 | GrTextureAdjuster adjuster(this->context(), std::move(view), |
876 | image->imageInfo().colorInfo(), pinnedUniqueID); |
877 | this->drawProducerLattice(&adjuster, std::move(iter), dst, paint); |
878 | } else { |
879 | SkBitmap bm; |
880 | if (image->isLazyGenerated()) { |
881 | GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw); |
882 | this->drawProducerLattice(&maker, std::move(iter), dst, paint); |
883 | } else if (as_IB(image)->getROPixels(&bm)) { |
884 | GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw); |
885 | this->drawProducerLattice(&maker, std::move(iter), dst, paint); |
886 | } |
887 | } |
888 | } |
889 | |
890 | void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer, |
891 | std::unique_ptr<SkLatticeIter> iter, const SkRect& dst, |
892 | const SkPaint& origPaint) { |
893 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawProducerLattice" , fContext.get()); |
894 | SkTCopyOnFirstWrite<SkPaint> paint(&origPaint); |
895 | |
896 | if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) { |
897 | paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF)); |
898 | } |
899 | GrPaint grPaint; |
900 | if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorInfo(), |
901 | *paint, &grPaint)) { |
902 | return; |
903 | } |
904 | |
905 | auto dstColorSpace = fRenderTargetContext->colorInfo().colorSpace(); |
906 | const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint); |
907 | auto view = producer->view(filter); |
908 | if (!view) { |
909 | return; |
910 | } |
911 | auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(), |
912 | dstColorSpace, kPremul_SkAlphaType); |
913 | |
914 | fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->localToDevice(), |
915 | std::move(view), producer->alphaType(), std::move(csxf), |
916 | filter, std::move(iter), dst); |
917 | } |
918 | |
919 | void SkGpuDevice::drawImageLattice(const SkImage* image, |
920 | const SkCanvas::Lattice& lattice, const SkRect& dst, |
921 | const SkPaint& paint) { |
922 | ASSERT_SINGLE_OWNER |
923 | uint32_t pinnedUniqueID; |
924 | auto iter = std::make_unique<SkLatticeIter>(lattice, dst); |
925 | if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) { |
926 | GrTextureAdjuster adjuster(this->context(), std::move(view), |
927 | image->imageInfo().colorInfo(), pinnedUniqueID); |
928 | this->drawProducerLattice(&adjuster, std::move(iter), dst, paint); |
929 | } else { |
930 | SkBitmap bm; |
931 | if (image->isLazyGenerated()) { |
932 | GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw); |
933 | this->drawProducerLattice(&maker, std::move(iter), dst, paint); |
934 | } else if (as_IB(image)->getROPixels(&bm)) { |
935 | GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw); |
936 | this->drawProducerLattice(&maker, std::move(iter), dst, paint); |
937 | } |
938 | } |
939 | } |
940 | |
941 | static bool init_vertices_paint(GrContext* context, const GrColorInfo& colorInfo, |
942 | const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode, |
943 | bool hasTexs, bool hasColors, GrPaint* grPaint) { |
944 | if (hasTexs && skPaint.getShader()) { |
945 | if (hasColors) { |
946 | // When there are texs and colors the shader and colors are combined using bmode. |
947 | return SkPaintToGrPaintWithXfermode(context, colorInfo, skPaint, matrix, bmode, |
948 | grPaint); |
949 | } else { |
950 | // We have a shader, but no colors to blend it against. |
951 | return SkPaintToGrPaint(context, colorInfo, skPaint, matrix, grPaint); |
952 | } |
953 | } else { |
954 | if (hasColors) { |
955 | // We have colors, but either have no shader or no texture coords (which implies that |
956 | // we should ignore the shader). |
957 | return SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint); |
958 | } else { |
959 | // No colors and no shaders. Just draw with the paint color. |
960 | return SkPaintToGrPaintNoShader(context, colorInfo, skPaint, grPaint); |
961 | } |
962 | } |
963 | } |
964 | |
965 | void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) { |
966 | ASSERT_SINGLE_OWNER |
967 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawVertices" , fContext.get()); |
968 | SkASSERT(vertices); |
969 | |
970 | SkVerticesPriv info(vertices->priv()); |
971 | |
972 | const SkRuntimeEffect* effect = |
973 | paint.getShader() ? as_SB(paint.getShader())->asRuntimeEffect() : nullptr; |
974 | |
975 | // Pretend that we have tex coords when using custom per-vertex data. The shader is going to |
976 | // use those (rather than local coords), but our paint conversion remains the same. |
977 | GrPaint grPaint; |
978 | bool hasColors = info.hasColors(); |
979 | bool hasTexs = info.hasTexCoords() || info.hasCustomData(); |
980 | if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorInfo(), paint, |
981 | this->localToDevice(), mode, hasTexs, hasColors, &grPaint)) { |
982 | return; |
983 | } |
984 | SkM44 localToWorld(SkM44::kUninitialized_Constructor); |
985 | if (info.usesLocalToWorldMatrix()) { |
986 | localToWorld = this->localToWorld(); |
987 | } |
988 | fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->localToDevice(), |
989 | sk_ref_sp(const_cast<SkVertices*>(vertices)), nullptr, |
990 | effect, &localToWorld); |
991 | } |
992 | |
993 | /////////////////////////////////////////////////////////////////////////////// |
994 | |
995 | void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) { |
996 | |
997 | ASSERT_SINGLE_OWNER |
998 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawShadow" , fContext.get()); |
999 | |
1000 | if (!fRenderTargetContext->drawFastShadow(this->clip(), this->localToDevice(), path, rec)) { |
1001 | // failed to find an accelerated case |
1002 | this->INHERITED::drawShadow(path, rec); |
1003 | } |
1004 | } |
1005 | |
1006 | /////////////////////////////////////////////////////////////////////////////// |
1007 | |
1008 | void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[], |
1009 | const SkRect texRect[], const SkColor colors[], int count, |
1010 | SkBlendMode mode, const SkPaint& paint) { |
1011 | ASSERT_SINGLE_OWNER |
1012 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawAtlas" , fContext.get()); |
1013 | |
1014 | SkPaint p(paint); |
1015 | p.setShader(atlas->makeShader()); |
1016 | |
1017 | GrPaint grPaint; |
1018 | if (colors) { |
1019 | if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorInfo(), p, |
1020 | this->localToDevice(), (SkBlendMode)mode, &grPaint)) { |
1021 | return; |
1022 | } |
1023 | } else { |
1024 | if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), p, |
1025 | this->localToDevice(), &grPaint)) { |
1026 | return; |
1027 | } |
1028 | } |
1029 | |
1030 | fRenderTargetContext->drawAtlas( |
1031 | this->clip(), std::move(grPaint), this->localToDevice(), count, xform, texRect, colors); |
1032 | } |
1033 | |
1034 | /////////////////////////////////////////////////////////////////////////////// |
1035 | |
1036 | void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) { |
1037 | ASSERT_SINGLE_OWNER |
1038 | GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice" , "drawGlyphRunList" , fContext.get()); |
1039 | |
1040 | // Check for valid input |
1041 | if (!this->localToDevice().isFinite() || !glyphRunList.allFontsFinite()) { |
1042 | return; |
1043 | } |
1044 | |
1045 | fRenderTargetContext->drawGlyphRunList(this->clip(), this->localToDevice(), glyphRunList); |
1046 | } |
1047 | |
1048 | /////////////////////////////////////////////////////////////////////////////// |
1049 | |
1050 | void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) { |
1051 | GrBackendApi api = this->context()->backend(); |
1052 | if (GrBackendApi::kVulkan == api) { |
1053 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
1054 | const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm; |
1055 | std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw = |
1056 | drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(), |
1057 | this->imageInfo()); |
1058 | if (gpuDraw) { |
1059 | fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds()); |
1060 | return; |
1061 | } |
1062 | } |
1063 | this->INHERITED::drawDrawable(drawable, matrix, canvas); |
1064 | } |
1065 | |
1066 | |
1067 | /////////////////////////////////////////////////////////////////////////////// |
1068 | |
1069 | void SkGpuDevice::flush() { |
1070 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo()); |
1071 | } |
1072 | |
1073 | GrSemaphoresSubmitted SkGpuDevice::flush(SkSurface::BackendSurfaceAccess access, |
1074 | const GrFlushInfo& info) { |
1075 | ASSERT_SINGLE_OWNER |
1076 | |
1077 | return fRenderTargetContext->flush(access, info); |
1078 | } |
1079 | |
1080 | bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) { |
1081 | ASSERT_SINGLE_OWNER |
1082 | |
1083 | return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores); |
1084 | } |
1085 | |
1086 | /////////////////////////////////////////////////////////////////////////////// |
1087 | |
1088 | SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) { |
1089 | ASSERT_SINGLE_OWNER |
1090 | |
1091 | SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry); |
1092 | |
1093 | // layers are never drawn in repeat modes, so we can request an approx |
1094 | // match and ignore any padding. |
1095 | SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox |
1096 | : SkBackingFit::kExact; |
1097 | |
1098 | SkASSERT(cinfo.fInfo.colorType() != kRGBA_1010102_SkColorType); |
1099 | |
1100 | auto rtc = GrRenderTargetContext::MakeWithFallback( |
1101 | fContext.get(), SkColorTypeToGrColorType(cinfo.fInfo.colorType()), |
1102 | fRenderTargetContext->colorInfo().refColorSpace(), fit, cinfo.fInfo.dimensions(), |
1103 | fRenderTargetContext->numSamples(), GrMipMapped::kNo, |
1104 | fRenderTargetContext->asSurfaceProxy()->isProtected(), kBottomLeft_GrSurfaceOrigin, |
1105 | SkBudgeted::kYes, &props); |
1106 | if (!rtc) { |
1107 | return nullptr; |
1108 | } |
1109 | |
1110 | // Skia's convention is to only clear a device if it is non-opaque. |
1111 | InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents; |
1112 | |
1113 | return SkGpuDevice::Make(fContext.get(), std::move(rtc), init).release(); |
1114 | } |
1115 | |
1116 | sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
1117 | ASSERT_SINGLE_OWNER |
1118 | // TODO: Change the signature of newSurface to take a budgeted parameter. |
1119 | static const SkBudgeted kBudgeted = SkBudgeted::kNo; |
1120 | return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info, |
1121 | fRenderTargetContext->numSamples(), |
1122 | fRenderTargetContext->origin(), &props); |
1123 | } |
1124 | |
1125 | SkImageFilterCache* SkGpuDevice::getImageFilterCache() { |
1126 | ASSERT_SINGLE_OWNER |
1127 | // We always return a transient cache, so it is freed after each |
1128 | // filter traversal. |
1129 | return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize); |
1130 | } |
1131 | |
1132 | //////////////////////////////////////////////////////////////////////////////////// |
1133 | |
1134 | bool SkGpuDevice::android_utils_clipWithStencil() { |
1135 | SkRegion clipRegion; |
1136 | this->onAsRgnClip(&clipRegion); |
1137 | if (clipRegion.isEmpty()) { |
1138 | return false; |
1139 | } |
1140 | GrRenderTargetContext* rtc = this->accessRenderTargetContext(); |
1141 | if (!rtc) { |
1142 | return false; |
1143 | } |
1144 | GrPaint grPaint; |
1145 | grPaint.setXPFactory(GrDisableColorXPFactory::Get()); |
1146 | GrNoClip noClip; |
1147 | static constexpr GrUserStencilSettings kDrawToStencil( |
1148 | GrUserStencilSettings::StaticInit< |
1149 | 0x1, |
1150 | GrUserStencilTest::kAlways, |
1151 | 0x1, |
1152 | GrUserStencilOp::kReplace, |
1153 | GrUserStencilOp::kReplace, |
1154 | 0x1>() |
1155 | ); |
1156 | rtc->drawRegion(noClip, std::move(grPaint), GrAA::kNo, SkMatrix::I(), clipRegion, |
1157 | GrStyle::SimpleFill(), &kDrawToStencil); |
1158 | return true; |
1159 | } |
1160 | |
1161 | |