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/gpu/GrContext.h"
16 #include "include/private/SkImageInfoPriv.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/SkVertState.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/GrImageInfo.h"
38 #include "src/gpu/GrImageTextureMaker.h"
39 #include "src/gpu/GrRenderTargetContextPriv.h"
40 #include "src/gpu/GrStyle.h"
41 #include "src/gpu/GrSurfaceProxyPriv.h"
42 #include "src/gpu/GrTextureAdjuster.h"
43 #include "src/gpu/GrTracing.h"
44 #include "src/gpu/SkGr.h"
45 #include "src/gpu/effects/GrBicubicEffect.h"
46 #include "src/gpu/geometry/GrShape.h"
47 #include "src/gpu/text/GrTextTarget.h"
48 #include "src/image/SkImage_Base.h"
49 #include "src/image/SkReadPixelsRec.h"
50 #include "src/image/SkSurface_Gpu.h"
51 #include "src/utils/SkUTF.h"
52
53 #define ASSERT_SINGLE_OWNER \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->priv().singleOwner());)
55
56
57 ///////////////////////////////////////////////////////////////////////////////
58
59 /** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
60 should fail. */
CheckAlphaTypeAndGetFlags(const SkImageInfo * info,SkGpuDevice::InitContents init,unsigned * flags)61 bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
62 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
63 *flags = 0;
64 if (info) {
65 switch (info->alphaType()) {
66 case kPremul_SkAlphaType:
67 break;
68 case kOpaque_SkAlphaType:
69 *flags |= SkGpuDevice::kIsOpaque_Flag;
70 break;
71 default: // If it is unpremul or unknown don't try to render
72 return false;
73 }
74 }
75 if (kClear_InitContents == init) {
76 *flags |= kNeedClear_Flag;
77 }
78 return true;
79 }
80
Make(GrContext * context,std::unique_ptr<GrRenderTargetContext> renderTargetContext,InitContents init)81 sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
82 std::unique_ptr<GrRenderTargetContext> renderTargetContext,
83 InitContents init) {
84 if (!renderTargetContext || context->priv().abandoned()) {
85 return nullptr;
86 }
87
88 SkColorType ct = GrColorTypeToSkColorType(renderTargetContext->colorInfo().colorType());
89
90 unsigned flags;
91 if (!context->colorTypeSupportedAsSurface(ct) ||
92 !CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
93 return nullptr;
94 }
95 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext), flags));
96 }
97
Make(GrContext * context,SkBudgeted budgeted,const SkImageInfo & info,int sampleCount,GrSurfaceOrigin origin,const SkSurfaceProps * props,GrMipMapped mipMapped,InitContents init)98 sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
99 const SkImageInfo& info, int sampleCount,
100 GrSurfaceOrigin origin, const SkSurfaceProps* props,
101 GrMipMapped mipMapped, InitContents init) {
102 unsigned flags;
103 if (!context->colorTypeSupportedAsSurface(info.colorType()) ||
104 !CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
105 return nullptr;
106 }
107
108 auto renderTargetContext =
109 MakeRenderTargetContext(context, budgeted, info, sampleCount, origin, props, mipMapped);
110 if (!renderTargetContext) {
111 return nullptr;
112 }
113
114 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext), flags));
115 }
116
make_info(GrRenderTargetContext * context,bool opaque)117 static SkImageInfo make_info(GrRenderTargetContext* context, bool opaque) {
118 SkColorType colorType = GrColorTypeToSkColorType(context->colorInfo().colorType());
119 return SkImageInfo::Make(context->width(), context->height(), colorType,
120 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
121 context->colorInfo().refColorSpace());
122 }
123
SkGpuDevice(GrContext * context,std::unique_ptr<GrRenderTargetContext> renderTargetContext,unsigned flags)124 SkGpuDevice::SkGpuDevice(GrContext* context,
125 std::unique_ptr<GrRenderTargetContext> renderTargetContext,
126 unsigned flags)
127 : INHERITED(make_info(renderTargetContext.get(), SkToBool(flags & kIsOpaque_Flag)),
128 renderTargetContext->surfaceProps())
129 , fContext(SkRef(context))
130 , fRenderTargetContext(std::move(renderTargetContext)) {
131 if (flags & kNeedClear_Flag) {
132 this->clearAll();
133 }
134 }
135
MakeRenderTargetContext(GrContext * context,SkBudgeted budgeted,const SkImageInfo & origInfo,int sampleCount,GrSurfaceOrigin origin,const SkSurfaceProps * surfaceProps,GrMipMapped mipMapped)136 std::unique_ptr<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
137 GrContext* context,
138 SkBudgeted budgeted,
139 const SkImageInfo& origInfo,
140 int sampleCount,
141 GrSurfaceOrigin origin,
142 const SkSurfaceProps* surfaceProps,
143 GrMipMapped mipMapped) {
144 if (!context) {
145 return nullptr;
146 }
147
148 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
149 // they need to be exact.
150 return GrRenderTargetContext::Make(
151 context, SkColorTypeToGrColorType(origInfo.colorType()), origInfo.refColorSpace(),
152 SkBackingFit::kExact, origInfo.dimensions(), sampleCount, mipMapped, GrProtected::kNo,
153 origin, budgeted, surfaceProps);
154 }
155
filterTexture(SkSpecialImage * srcImg,int left,int top,SkIPoint * offset,const SkImageFilter * filter)156 sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
157 int left, int top,
158 SkIPoint* offset,
159 const SkImageFilter* filter) {
160 SkASSERT(srcImg->isTextureBacked());
161 SkASSERT(filter);
162
163 SkMatrix matrix = this->localToDevice();
164 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
165 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
166 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
167 SkColorType colorType = GrColorTypeToSkColorType(fRenderTargetContext->colorInfo().colorType());
168 if (colorType == kUnknown_SkColorType) {
169 colorType = kRGBA_8888_SkColorType;
170 }
171 SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), colorType,
172 fRenderTargetContext->colorInfo().colorSpace(), srcImg);
173
174 return as_IFB(filter)->filterImage(ctx).imageAndOffset(offset);
175 }
176
177 ///////////////////////////////////////////////////////////////////////////////
178
onReadPixels(const SkPixmap & pm,int x,int y)179 bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
180 ASSERT_SINGLE_OWNER
181
182 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
183 return false;
184 }
185
186 return fRenderTargetContext->readPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), {x, y});
187 }
188
onWritePixels(const SkPixmap & pm,int x,int y)189 bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
190 ASSERT_SINGLE_OWNER
191
192 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
193 return false;
194 }
195
196 return fRenderTargetContext->writePixels(pm.info(), pm.addr(), pm.rowBytes(), {x, y});
197 }
198
onAccessPixels(SkPixmap * pmap)199 bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
200 ASSERT_SINGLE_OWNER
201 return false;
202 }
203
accessRenderTargetContext()204 GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
205 ASSERT_SINGLE_OWNER
206 return fRenderTargetContext.get();
207 }
208
clearAll()209 void SkGpuDevice::clearAll() {
210 ASSERT_SINGLE_OWNER
211 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
212
213 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
214 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
215 GrRenderTargetContext::CanClearFullscreen::kYes);
216 }
217
replaceRenderTargetContext(std::unique_ptr<GrRenderTargetContext> rtc,SkSurface::ContentChangeMode mode)218 void SkGpuDevice::replaceRenderTargetContext(std::unique_ptr<GrRenderTargetContext> rtc,
219 SkSurface::ContentChangeMode mode) {
220 SkASSERT(rtc->width() == this->width());
221 SkASSERT(rtc->height() == this->height());
222 SkASSERT(rtc->numSamples() == fRenderTargetContext->numSamples());
223 SkASSERT(rtc->asSurfaceProxy()->priv().isExact());
224 if (mode == SkSurface::kRetain_ContentChangeMode) {
225 if (this->context()->abandoned()) {
226 return;
227 }
228
229 SkASSERT(fRenderTargetContext->asTextureProxy());
230 SkAssertResult(rtc->blitTexture(fRenderTargetContext->readSurfaceView(),
231 SkIRect::MakeWH(this->width(), this->height()),
232 SkIPoint::Make(0,0)));
233 }
234
235 fRenderTargetContext = std::move(rtc);
236 }
237
replaceRenderTargetContext(SkSurface::ContentChangeMode mode)238 void SkGpuDevice::replaceRenderTargetContext(SkSurface::ContentChangeMode mode) {
239 ASSERT_SINGLE_OWNER
240
241 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
242
243 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
244 // kExact-backed render target context.
245 auto newRTC = MakeRenderTargetContext(this->context(),
246 budgeted,
247 this->imageInfo(),
248 fRenderTargetContext->numSamples(),
249 fRenderTargetContext->origin(),
250 &this->surfaceProps(),
251 fRenderTargetContext->mipMapped());
252 if (!newRTC) {
253 return;
254 }
255 this->replaceRenderTargetContext(std::move(newRTC), mode);
256 }
257
258 ///////////////////////////////////////////////////////////////////////////////
259
drawPaint(const SkPaint & paint)260 void SkGpuDevice::drawPaint(const SkPaint& paint) {
261 ASSERT_SINGLE_OWNER
262 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
263
264 GrPaint grPaint;
265 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
266 this->localToDevice(), &grPaint)) {
267 return;
268 }
269
270 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->localToDevice());
271 }
272
point_mode_to_primitive_type(SkCanvas::PointMode mode)273 static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
274 switch (mode) {
275 case SkCanvas::kPoints_PointMode:
276 return GrPrimitiveType::kPoints;
277 case SkCanvas::kLines_PointMode:
278 return GrPrimitiveType::kLines;
279 case SkCanvas::kPolygon_PointMode:
280 return GrPrimitiveType::kLineStrip;
281 }
282 SK_ABORT("Unexpected mode");
283 }
284
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)285 void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
286 size_t count, const SkPoint pts[], const SkPaint& paint) {
287 ASSERT_SINGLE_OWNER
288 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
289 SkScalar width = paint.getStrokeWidth();
290 if (width < 0) {
291 return;
292 }
293
294 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
295 GrStyle style(paint, SkPaint::kStroke_Style);
296 GrPaint grPaint;
297 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
298 this->localToDevice(), &grPaint)) {
299 return;
300 }
301 SkPath path;
302 path.setIsVolatile(true);
303 path.moveTo(pts[0]);
304 path.lineTo(pts[1]);
305 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
306 this->localToDevice(), path, style);
307 return;
308 }
309
310 SkScalar scales[2];
311 bool isHairline = (0 == width) ||
312 (1 == width && this->localToDevice().getMinMaxScales(scales) &&
313 SkScalarNearlyEqual(scales[0], 1.f) && SkScalarNearlyEqual(scales[1], 1.f));
314 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
315 // else we let the SkDraw call our drawPath()
316 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
317 SkRasterClip rc(this->devClipBounds());
318 SkDraw draw;
319 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
320 draw.fMatrix = &this->localToDevice();
321 draw.fRC = &rc;
322 draw.drawPoints(mode, count, pts, paint, this);
323 return;
324 }
325
326 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
327
328 const SkMatrix* viewMatrix = &this->localToDevice();
329 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
330 // This offsetting in device space matches the expectations of the Android framework for non-AA
331 // points and lines.
332 SkMatrix tempMatrix;
333 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
334 tempMatrix = *viewMatrix;
335 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
336 tempMatrix.postTranslate(kOffset, kOffset);
337 viewMatrix = &tempMatrix;
338 }
339 #endif
340
341 GrPaint grPaint;
342 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, *viewMatrix,
343 &grPaint)) {
344 return;
345 }
346
347 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
348 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
349 nullptr);
350
351 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
352 std::move(vertices), nullptr, 0, &primitiveType);
353 }
354
355 ///////////////////////////////////////////////////////////////////////////////
356
drawRect(const SkRect & rect,const SkPaint & paint)357 void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
358 ASSERT_SINGLE_OWNER
359 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
360
361 GrStyle style(paint);
362
363 // A couple reasons we might need to call drawPath.
364 if (paint.getMaskFilter() || paint.getPathEffect()) {
365 GrShape shape(rect, style);
366
367 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
368 this->clip(), paint, this->localToDevice(), shape);
369 return;
370 }
371
372 GrPaint grPaint;
373 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
374 this->localToDevice(), &grPaint)) {
375 return;
376 }
377
378 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
379 this->localToDevice(), rect, &style);
380 }
381
drawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],SkCanvas::QuadAAFlags aaFlags,const SkColor4f & color,SkBlendMode mode)382 void SkGpuDevice::drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
383 SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color,
384 SkBlendMode mode) {
385 ASSERT_SINGLE_OWNER
386 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawEdgeAAQuad", fContext.get());
387
388 SkPMColor4f dstColor = SkColor4fPrepForDst(color, fRenderTargetContext->colorInfo()).premul();
389
390 GrPaint grPaint;
391 grPaint.setColor4f(dstColor);
392 if (mode != SkBlendMode::kSrcOver) {
393 grPaint.setXPFactory(SkBlendMode_AsXPFactory(mode));
394 }
395
396 // This is exclusively meant for tiling operations, so keep AA enabled to handle MSAA seaming
397 GrQuadAAFlags grAA = SkToGrQuadAAFlags(aaFlags);
398 if (clip) {
399 // Use fillQuadWithEdgeAA
400 fRenderTargetContext->fillQuadWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
401 this->localToDevice(), clip, nullptr);
402 } else {
403 // Use fillRectWithEdgeAA to preserve mathematical properties of dst being rectangular
404 fRenderTargetContext->fillRectWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
405 this->localToDevice(), rect);
406 }
407 }
408
409 ///////////////////////////////////////////////////////////////////////////////
410
drawRRect(const SkRRect & rrect,const SkPaint & paint)411 void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
412 ASSERT_SINGLE_OWNER
413 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
414
415 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
416 if (mf) {
417 if (mf->hasFragmentProcessor()) {
418 mf = nullptr; // already handled in SkPaintToGrPaint
419 }
420 }
421
422 GrStyle style(paint);
423
424 if (mf || style.pathEffect()) {
425 // A path effect will presumably transform this rrect into something else.
426 GrShape shape(rrect, style);
427
428 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
429 this->clip(), paint, this->localToDevice(), shape);
430 return;
431 }
432
433 SkASSERT(!style.pathEffect());
434
435 GrPaint grPaint;
436 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
437 this->localToDevice(), &grPaint)) {
438 return;
439 }
440
441 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
442 this->localToDevice(), rrect, style);
443 }
444
445
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)446 void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
447 ASSERT_SINGLE_OWNER
448 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
449 if (outer.isEmpty()) {
450 return;
451 }
452
453 if (inner.isEmpty()) {
454 return this->drawRRect(outer, paint);
455 }
456
457 SkStrokeRec stroke(paint);
458
459 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
460 GrPaint grPaint;
461 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
462 this->localToDevice(), &grPaint)) {
463 return;
464 }
465
466 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
467 GrAA(paint.isAntiAlias()), this->localToDevice(),
468 outer, inner);
469 return;
470 }
471
472 SkPath path;
473 path.setIsVolatile(true);
474 path.addRRect(outer);
475 path.addRRect(inner);
476 path.setFillType(SkPathFillType::kEvenOdd);
477
478 // TODO: We are losing the possible mutability of the path here but this should probably be
479 // fixed by upgrading GrShape to handle DRRects.
480 GrShape shape(path, paint);
481
482 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
483 paint, this->localToDevice(), shape);
484 }
485
486
487 /////////////////////////////////////////////////////////////////////////////
488
drawRegion(const SkRegion & region,const SkPaint & paint)489 void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
490 if (paint.getMaskFilter()) {
491 SkPath path;
492 region.getBoundaryPath(&path);
493 path.setIsVolatile(true);
494 return this->drawPath(path, paint, true);
495 }
496
497 GrPaint grPaint;
498 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
499 this->localToDevice(), &grPaint)) {
500 return;
501 }
502
503 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
504 this->localToDevice(), region, GrStyle(paint));
505 }
506
drawOval(const SkRect & oval,const SkPaint & paint)507 void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
508 ASSERT_SINGLE_OWNER
509 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
510
511 if (paint.getMaskFilter()) {
512 // The RRect path can handle special case blurring
513 SkRRect rr = SkRRect::MakeOval(oval);
514 return this->drawRRect(rr, paint);
515 }
516
517 GrPaint grPaint;
518 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
519 this->localToDevice(), &grPaint)) {
520 return;
521 }
522
523 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
524 this->localToDevice(), oval, GrStyle(paint));
525 }
526
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)527 void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
528 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
529 ASSERT_SINGLE_OWNER
530 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
531 if (paint.getMaskFilter()) {
532 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
533 return;
534 }
535 GrPaint grPaint;
536 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
537 this->localToDevice(), &grPaint)) {
538 return;
539 }
540
541 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
542 this->localToDevice(), oval, startAngle, sweepAngle, useCenter,
543 GrStyle(paint));
544 }
545
546 #include "include/core/SkMaskFilter.h"
547
548 ///////////////////////////////////////////////////////////////////////////////
drawStrokedLine(const SkPoint points[2],const SkPaint & origPaint)549 void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
550 const SkPaint& origPaint) {
551 ASSERT_SINGLE_OWNER
552 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
553 // Adding support for round capping would require a
554 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
555 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
556 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
557 SkASSERT(!origPaint.getPathEffect());
558 SkASSERT(!origPaint.getMaskFilter());
559
560 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
561 SkASSERT(halfWidth > 0);
562
563 SkVector v = points[1] - points[0];
564
565 SkScalar length = SkPoint::Normalize(&v);
566 if (!length) {
567 v.fX = 1.0f;
568 v.fY = 0.0f;
569 }
570
571 SkPaint newPaint(origPaint);
572 newPaint.setStyle(SkPaint::kFill_Style);
573
574 SkScalar xtraLength = 0.0f;
575 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
576 xtraLength = halfWidth;
577 }
578
579 SkPoint mid = points[0] + points[1];
580 mid.scale(0.5f);
581
582 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
583 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
584 SkMatrix m;
585 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
586
587 SkMatrix local = m;
588
589 m.postConcat(this->localToDevice());
590
591 GrPaint grPaint;
592 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), newPaint, m,
593 &grPaint)) {
594 return;
595 }
596
597 fRenderTargetContext->fillRectWithLocalMatrix(
598 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
599 }
600
drawPath(const SkPath & origSrcPath,const SkPaint & paint,bool pathIsMutable)601 void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
602 ASSERT_SINGLE_OWNER
603 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
604 SkPoint points[2];
605 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
606 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
607 this->localToDevice().preservesRightAngles() && origSrcPath.isLine(points)) {
608 // Path-based stroking looks better for thin rects
609 SkScalar strokeWidth = this->localToDevice().getMaxScale() * paint.getStrokeWidth();
610 if (strokeWidth >= 1.0f) {
611 // Round capping support is currently disabled b.c. it would require a RRect
612 // GrDrawOp that takes a localMatrix.
613 this->drawStrokedLine(points, paint);
614 return;
615 }
616 }
617 }
618
619 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
620 if (!paint.getMaskFilter()) {
621 GrPaint grPaint;
622 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
623 this->localToDevice(), &grPaint)) {
624 return;
625 }
626 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
627 this->localToDevice(), origSrcPath, GrStyle(paint));
628 return;
629 }
630
631 // TODO: losing possible mutability of 'origSrcPath' here
632 GrShape shape(origSrcPath, paint);
633
634 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
635 paint, this->localToDevice(), shape);
636 }
637
638 static const int kBmpSmallTileSize = 1 << 10;
639
get_tile_count(const SkIRect & srcRect,int tileSize)640 static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
641 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
642 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
643 return tilesX * tilesY;
644 }
645
determine_tile_size(const SkIRect & src,int maxTileSize)646 static int determine_tile_size(const SkIRect& src, int maxTileSize) {
647 if (maxTileSize <= kBmpSmallTileSize) {
648 return maxTileSize;
649 }
650
651 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
652 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
653
654 maxTileTotalTileSize *= maxTileSize * maxTileSize;
655 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
656
657 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
658 return kBmpSmallTileSize;
659 } else {
660 return maxTileSize;
661 }
662 }
663
664 // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
665 // pixels from the bitmap are necessary.
determine_clipped_src_rect(int width,int height,const GrClip & clip,const SkMatrix & viewMatrix,const SkMatrix & srcToDstRect,const SkISize & imageDimensions,const SkRect * srcRectPtr,SkIRect * clippedSrcIRect)666 static void determine_clipped_src_rect(int width, int height,
667 const GrClip& clip,
668 const SkMatrix& viewMatrix,
669 const SkMatrix& srcToDstRect,
670 const SkISize& imageDimensions,
671 const SkRect* srcRectPtr,
672 SkIRect* clippedSrcIRect) {
673 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
674 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
675 if (!inv.invert(&inv)) {
676 clippedSrcIRect->setEmpty();
677 return;
678 }
679 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
680 inv.mapRect(&clippedSrcRect);
681 if (srcRectPtr) {
682 if (!clippedSrcRect.intersect(*srcRectPtr)) {
683 clippedSrcIRect->setEmpty();
684 return;
685 }
686 }
687 clippedSrcRect.roundOut(clippedSrcIRect);
688 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
689 if (!clippedSrcIRect->intersect(bmpBounds)) {
690 clippedSrcIRect->setEmpty();
691 }
692 }
693
caps() const694 const GrCaps* SkGpuDevice::caps() const {
695 return fContext->priv().caps();
696 }
697
shouldTileImageID(uint32_t imageID,const SkIRect & imageRect,const SkMatrix & viewMatrix,const SkMatrix & srcToDstRect,GrSamplerState params,const SkRect * srcRectPtr,int maxTileSize,int * tileSize,SkIRect * clippedSubset) const698 bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
699 const SkIRect& imageRect,
700 const SkMatrix& viewMatrix,
701 const SkMatrix& srcToDstRect,
702 GrSamplerState params,
703 const SkRect* srcRectPtr,
704 int maxTileSize,
705 int* tileSize,
706 SkIRect* clippedSubset) const {
707 ASSERT_SINGLE_OWNER
708 // if it's larger than the max tile size, then we have no choice but tiling.
709 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
710 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
711 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
712 srcRectPtr, clippedSubset);
713 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
714 return true;
715 }
716
717 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
718 const size_t area = imageRect.width() * imageRect.height();
719 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
720 return false;
721 }
722
723 // At this point we know we could do the draw by uploading the entire bitmap
724 // as a texture. However, if the texture would be large compared to the
725 // cache size and we don't require most of it for this draw then tile to
726 // reduce the amount of upload and cache spill.
727
728 // assumption here is that sw bitmap size is a good proxy for its size as
729 // a texture
730 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
731 size_t cacheSize = fContext->getResourceCacheLimit();
732 if (bmpSize < cacheSize / 2) {
733 return false;
734 }
735
736 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
737 // tiling memory savings would be < 50%.
738 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
739 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
740 clippedSubset);
741 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
742 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
743 kBmpSmallTileSize * kBmpSmallTileSize *
744 sizeof(SkPMColor); // assume 32bit pixels;
745
746 return usedTileBytes * 2 < bmpSize;
747 }
748
shouldTileImage(const SkImage * image,const SkRect * srcRectPtr,SkCanvas::SrcRectConstraint constraint,SkFilterQuality quality,const SkMatrix & viewMatrix,const SkMatrix & srcToDstRect) const749 bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
750 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
751 const SkMatrix& viewMatrix,
752 const SkMatrix& srcToDstRect) const {
753 ASSERT_SINGLE_OWNER
754 // If image is explicitly texture backed then we shouldn't get here.
755 SkASSERT(!image->isTextureBacked());
756
757 GrSamplerState samplerState;
758 bool doBicubic;
759 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
760 image->width(), image->height(), quality, viewMatrix, srcToDstRect,
761 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
762
763 int tileFilterPad;
764 if (doBicubic) {
765 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
766 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
767 tileFilterPad = 0;
768 } else {
769 tileFilterPad = 1;
770 }
771 samplerState.setFilterMode(textureFilterMode);
772
773 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
774
775 // these are output, which we safely ignore, as we just want to know the predicate
776 int outTileSize;
777 SkIRect outClippedSrcRect;
778
779 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
780 samplerState, srcRectPtr, maxTileSize, &outTileSize,
781 &outClippedSrcRect);
782 }
783
784 // This method outsets 'iRect' by 'outset' all around and then clamps its extents to
785 // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
786 // of 'iRect' for all possible outsets/clamps.
clamped_outset_with_offset(SkIRect * iRect,int outset,SkPoint * offset,const SkIRect & clamp)787 static inline void clamped_outset_with_offset(SkIRect* iRect,
788 int outset,
789 SkPoint* offset,
790 const SkIRect& clamp) {
791 iRect->outset(outset, outset);
792
793 int leftClampDelta = clamp.fLeft - iRect->fLeft;
794 if (leftClampDelta > 0) {
795 offset->fX -= outset - leftClampDelta;
796 iRect->fLeft = clamp.fLeft;
797 } else {
798 offset->fX -= outset;
799 }
800
801 int topClampDelta = clamp.fTop - iRect->fTop;
802 if (topClampDelta > 0) {
803 offset->fY -= outset - topClampDelta;
804 iRect->fTop = clamp.fTop;
805 } else {
806 offset->fY -= outset;
807 }
808
809 if (iRect->fRight > clamp.fRight) {
810 iRect->fRight = clamp.fRight;
811 }
812 if (iRect->fBottom > clamp.fBottom) {
813 iRect->fBottom = clamp.fBottom;
814 }
815 }
816
817 // Break 'bitmap' into several tiles to draw it since it has already
818 // been determined to be too large to fit in VRAM
drawTiledBitmap(const SkBitmap & bitmap,const SkMatrix & viewMatrix,const SkMatrix & dstMatrix,const SkRect & srcRect,const SkIRect & clippedSrcIRect,GrSamplerState params,const SkPaint & origPaint,SkCanvas::SrcRectConstraint constraint,int tileSize,bool bicubic)819 void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
820 const SkMatrix& viewMatrix,
821 const SkMatrix& dstMatrix,
822 const SkRect& srcRect,
823 const SkIRect& clippedSrcIRect,
824 GrSamplerState params,
825 const SkPaint& origPaint,
826 SkCanvas::SrcRectConstraint constraint,
827 int tileSize,
828 bool bicubic) {
829 ASSERT_SINGLE_OWNER
830
831 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
832 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
833 LogDrawScaleFactor(viewMatrix, SkMatrix::I(), origPaint.getFilterQuality());
834
835 const SkPaint* paint = &origPaint;
836 SkPaint tempPaint;
837 if (origPaint.isAntiAlias() && fRenderTargetContext->numSamples() <= 1) {
838 // Drop antialiasing to avoid seams at tile boundaries.
839 tempPaint = origPaint;
840 tempPaint.setAntiAlias(false);
841 paint = &tempPaint;
842 }
843 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
844
845 int nx = bitmap.width() / tileSize;
846 int ny = bitmap.height() / tileSize;
847 for (int x = 0; x <= nx; x++) {
848 for (int y = 0; y <= ny; y++) {
849 SkRect tileR;
850 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
851 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
852
853 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
854 continue;
855 }
856
857 if (!tileR.intersect(srcRect)) {
858 continue;
859 }
860
861 SkIRect iTileR;
862 tileR.roundOut(&iTileR);
863 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
864 SkIntToScalar(iTileR.fTop));
865 SkRect rectToDraw = tileR;
866 dstMatrix.mapRect(&rectToDraw);
867 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
868 SkIRect iClampRect;
869
870 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
871 // In bleed mode we want to always expand the tile on all edges
872 // but stay within the bitmap bounds
873 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
874 } else {
875 // In texture-domain/clamp mode we only want to expand the
876 // tile on edges interior to "srcRect" (i.e., we want to
877 // not bleed across the original clamped edges)
878 srcRect.roundOut(&iClampRect);
879 }
880 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
881 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
882 }
883
884 SkBitmap tmpB;
885 if (bitmap.extractSubset(&tmpB, iTileR)) {
886 // now offset it to make it "local" to our tmp bitmap
887 tileR.offset(-offset.fX, -offset.fY);
888 // de-optimized this determination
889 bool needsTextureDomain = true;
890 this->drawBitmapTile(tmpB,
891 viewMatrix,
892 rectToDraw,
893 tileR,
894 params,
895 *paint,
896 constraint,
897 bicubic,
898 needsTextureDomain);
899 }
900 }
901 }
902 }
903
drawBitmapTile(const SkBitmap & bitmap,const SkMatrix & viewMatrix,const SkRect & dstRect,const SkRect & srcRect,GrSamplerState samplerState,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint,bool bicubic,bool needsTextureDomain)904 void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
905 const SkMatrix& viewMatrix,
906 const SkRect& dstRect,
907 const SkRect& srcRect,
908 GrSamplerState samplerState,
909 const SkPaint& paint,
910 SkCanvas::SrcRectConstraint constraint,
911 bool bicubic,
912 bool needsTextureDomain) {
913 // We should have already handled bitmaps larger than the max texture size.
914 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
915 bitmap.height() <= this->caps()->maxTextureSize());
916 // We should be respecting the max tile size by the time we get here.
917 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
918 bitmap.height() <= this->caps()->maxTileSize());
919 SkASSERT(!samplerState.isRepeated());
920
921 SkScalar scales[2] = {1.f, 1.f};
922 GrSurfaceProxyView view = GrRefCachedBitmapView(fContext.get(), bitmap, samplerState, scales);
923 if (!view) {
924 return;
925 }
926
927 // Compute a matrix that maps the rect we will draw to the src rect.
928 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
929 texMatrix.postScale(scales[0], scales[1]);
930
931 SkAlphaType srcAlphaType = bitmap.alphaType();
932
933 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
934 // the rest from the SkPaint.
935 std::unique_ptr<GrFragmentProcessor> fp;
936
937 const auto& caps = *this->caps();
938 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
939 if (bicubic) {
940 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
941 fp = GrBicubicEffect::MakeSubset(std::move(view), srcAlphaType, texMatrix,
942 samplerState.wrapModeX(), samplerState.wrapModeY(),
943 srcRect, kDir, caps);
944 } else {
945 fp = GrTextureEffect::MakeSubset(std::move(view), srcAlphaType, texMatrix,
946 samplerState, srcRect, caps);
947 }
948 } else if (bicubic) {
949 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
950 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
951 fp = GrBicubicEffect::Make(std::move(view), srcAlphaType, texMatrix,
952 samplerState.wrapModeX(), samplerState.wrapModeY(), kDir, caps);
953 } else {
954 fp = GrTextureEffect::Make(std::move(view), srcAlphaType, texMatrix, samplerState, caps);
955 }
956
957 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
958 fRenderTargetContext->colorInfo().colorSpace());
959 GrPaint grPaint;
960 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorInfo(), paint,
961 viewMatrix, std::move(fp),
962 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
963 return;
964 }
965
966 // Coverage-based AA would cause seams between tiles.
967 GrAA aa = GrAA(paint.isAntiAlias() && fRenderTargetContext->numSamples() > 1);
968 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
969 }
970
drawSprite(const SkBitmap & bitmap,int left,int top,const SkPaint & paint)971 void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
972 int left, int top, const SkPaint& paint) {
973 ASSERT_SINGLE_OWNER
974 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
975
976 if (fContext->priv().abandoned()) {
977 return;
978 }
979
980 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
981 if (!srcImg) {
982 return;
983 }
984
985 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
986 }
987
988
drawSpecial(SkSpecialImage * special,int left,int top,const SkPaint & paint,SkImage * clipImage,const SkMatrix & clipMatrix)989 void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
990 SkImage* clipImage, const SkMatrix& clipMatrix) {
991 ASSERT_SINGLE_OWNER
992 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
993
994 sk_sp<SkSpecialImage> result;
995 if (paint.getImageFilter()) {
996 SkIPoint offset = { 0, 0 };
997
998 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
999 if (!result) {
1000 return;
1001 }
1002
1003 left += offset.fX;
1004 top += offset.fY;
1005 } else {
1006 result = sk_ref_sp(special);
1007 }
1008
1009 SkASSERT(result->isTextureBacked());
1010 GrSurfaceProxyView view = result->view(this->context());
1011 if (!view) {
1012 return;
1013 }
1014
1015 SkMatrix ctm = this->localToDevice();
1016 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1017
1018 SkPaint tmpUnfiltered(paint);
1019 if (tmpUnfiltered.getMaskFilter()) {
1020 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
1021 }
1022
1023 tmpUnfiltered.setImageFilter(nullptr);
1024
1025 auto fp = GrTextureEffect::Make(std::move(view), special->alphaType());
1026 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
1027 fRenderTargetContext->colorInfo().colorSpace());
1028 if (GrColorTypeIsAlphaOnly(SkColorTypeToGrColorType(result->colorType()))) {
1029 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
1030 } else {
1031 if (paint.getColor4f().isOpaque()) {
1032 fp = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
1033 } else {
1034 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
1035 }
1036 }
1037
1038 GrPaint grPaint;
1039 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorInfo(),
1040 tmpUnfiltered, std::move(fp), &grPaint)) {
1041 return;
1042 }
1043
1044 const SkIRect& subset = result->subset();
1045 SkRect dstRect = SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()));
1046 SkRect srcRect = SkRect::Make(subset);
1047 if (clipImage) {
1048 // Add the image as a simple texture effect applied to coverage. Accessing content outside
1049 // of the clip image should behave as if it were a decal (i.e. zero coverage). However, to
1050 // limit pixels touched and hardware checks, we draw the clip image geometry to get the
1051 // decal effect.
1052 auto filter = paint.getFilterQuality() > kNone_SkFilterQuality
1053 ? GrSamplerState::Filter::kBilerp
1054 : GrSamplerState::Filter::kNearest;
1055 GrSurfaceProxyView clipView = as_IB(clipImage)->refView(this->context(), filter, nullptr);
1056 // Fold clip matrix into ctm
1057 ctm.preConcat(clipMatrix);
1058 SkMatrix inverseClipMatrix;
1059
1060 std::unique_ptr<GrFragmentProcessor> cfp;
1061 if (clipView && ctm.invert(&inverseClipMatrix)) {
1062 GrColorType srcColorType = SkColorTypeToGrColorType(clipImage->colorType());
1063
1064 cfp = GrTextureEffect::Make(std::move(clipView), clipImage->alphaType(),
1065 inverseClipMatrix, filter);
1066 if (srcColorType != GrColorType::kAlpha_8) {
1067 cfp = GrFragmentProcessor::SwizzleOutput(std::move(cfp), GrSwizzle::AAAA());
1068 }
1069 }
1070
1071 if (cfp) {
1072 // If the grPaint already has coverage, this adds an additional stage that multiples
1073 // the image's alpha channel with the prior coverage.
1074 grPaint.addCoverageFragmentProcessor(std::move(cfp));
1075
1076 // Undo the offset that was needed for shader coord transforms to get the transform for
1077 // the actual drawn geometry.
1078 ctm.postTranslate(SkIntToScalar(left), SkIntToScalar(top));
1079 inverseClipMatrix.preTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1080 SkRect clipGeometry = SkRect::MakeWH(clipImage->width(), clipImage->height());
1081 if (!clipGeometry.contains(inverseClipMatrix.mapRect(dstRect))) {
1082 // Draw the clip geometry since it is smaller, using dstRect as an extra scissor
1083 SkClipStack clip(this->cs());
1084 clip.clipDevRect(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()),
1085 SkClipOp::kIntersect);
1086 SkMatrix local = SkMatrix::Concat(SkMatrix::MakeRectToRect(
1087 dstRect, srcRect, SkMatrix::kFill_ScaleToFit), ctm);
1088 fRenderTargetContext->fillRectWithLocalMatrix(GrClipStackClip(&clip),
1089 std::move(grPaint), GrAA(paint.isAntiAlias()), ctm, clipGeometry, local);
1090 return;
1091 }
1092 // Else fall through and draw the subset since that is contained in the clip geometry
1093 }
1094 // Else some issue configuring the coverage FP, so just draw without the clip mask image
1095 }
1096 // Draw directly in screen space, possibly with an extra coverage processor
1097 fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint),
1098 GrAA(paint.isAntiAlias()), SkMatrix::I(), dstRect, srcRect);
1099 }
1100
drawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & origDst,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)1101 void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
1102 const SkRect* src, const SkRect& origDst,
1103 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
1104 ASSERT_SINGLE_OWNER
1105 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1106 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1107 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1108 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1109 const SkRect* dst = &origDst;
1110 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
1111 // Compute matrix from the two rectangles
1112 if (!src) {
1113 src = &bmpBounds;
1114 }
1115
1116 SkMatrix srcToDstMatrix;
1117 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1118 return;
1119 }
1120 SkRect tmpSrc, tmpDst;
1121 if (src != &bmpBounds) {
1122 if (!bmpBounds.contains(*src)) {
1123 tmpSrc = *src;
1124 if (!tmpSrc.intersect(bmpBounds)) {
1125 return; // nothing to draw
1126 }
1127 src = &tmpSrc;
1128 srcToDstMatrix.mapRect(&tmpDst, *src);
1129 dst = &tmpDst;
1130 }
1131 }
1132
1133 int maxTileSize = this->caps()->maxTileSize();
1134
1135 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1136 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1137 bool useCoverageAA = fRenderTargetContext->numSamples() <= 1 &&
1138 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1139 bitmap.height() <= maxTileSize;
1140
1141 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
1142
1143 if (!skipTileCheck) {
1144 int tileSize;
1145 SkIRect clippedSrcRect;
1146
1147 GrSamplerState sampleState;
1148 bool doBicubic;
1149 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
1150 bitmap.width(), bitmap.height(), paint.getFilterQuality(), this->localToDevice(),
1151 srcToDstMatrix, fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
1152
1153 int tileFilterPad;
1154
1155 if (doBicubic) {
1156 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1157 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
1158 tileFilterPad = 0;
1159 } else {
1160 tileFilterPad = 1;
1161 }
1162 sampleState.setFilterMode(textureFilterMode);
1163
1164 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
1165 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(),
1166 this->localToDevice(), srcToDstMatrix, sampleState, src,
1167 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1168 this->drawTiledBitmap(bitmap, this->localToDevice(), srcToDstMatrix, *src, clippedSrcRect,
1169 sampleState, paint, constraint, tileSize, doBicubic);
1170 return;
1171 }
1172 }
1173 GrBitmapTextureMaker maker(fContext.get(), bitmap, GrBitmapTextureMaker::Cached::kYes);
1174 this->drawTextureProducer(&maker, src, dst, constraint, this->localToDevice(), paint, true);
1175 }
1176
makeSpecial(const SkBitmap & bitmap)1177 sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
1178 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1179 // semantics). Since this is cached we would have to bake the fit into the cache key though.
1180 auto view = GrMakeCachedBitmapProxyView(fContext.get(), bitmap);
1181 if (!view) {
1182 return nullptr;
1183 }
1184
1185 const SkIRect rect = SkIRect::MakeSize(view.proxy()->dimensions());
1186
1187 // GrMakeCachedBitmapProxyView creates a tight copy of 'bitmap' so we don't have to subset
1188 // the special image
1189 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1190 rect,
1191 bitmap.getGenerationID(),
1192 std::move(view),
1193 SkColorTypeToGrColorType(bitmap.colorType()),
1194 bitmap.refColorSpace(),
1195 &this->surfaceProps());
1196 }
1197
makeSpecial(const SkImage * image)1198 sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
1199 SkPixmap pm;
1200 if (image->isTextureBacked()) {
1201 const GrSurfaceProxyView* view = as_IB(image)->view(this->context());
1202 SkASSERT(view);
1203
1204 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1205 SkIRect::MakeWH(image->width(), image->height()),
1206 image->uniqueID(),
1207 *view,
1208 SkColorTypeToGrColorType(image->colorType()),
1209 image->refColorSpace(),
1210 &this->surfaceProps());
1211 } else if (image->peekPixels(&pm)) {
1212 SkBitmap bm;
1213
1214 bm.installPixels(pm);
1215 return this->makeSpecial(bm);
1216 } else {
1217 return nullptr;
1218 }
1219 }
1220
snapSpecial(const SkIRect & subset,bool forceCopy)1221 sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial(const SkIRect& subset, bool forceCopy) {
1222 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
1223
1224 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1225 // since it would require us to make a copy of the underlying VkImage which we don't have access
1226 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1227 // command buffer.
1228 if (rtc->wrapsVkSecondaryCB()) {
1229 return nullptr;
1230 }
1231
1232 SkASSERT(rtc->asSurfaceProxy());
1233
1234 SkIRect finalSubset = subset;
1235 GrSurfaceProxyView view = rtc->readSurfaceView();
1236 if (forceCopy || !view.asTextureProxy()) {
1237 // When the device doesn't have a texture, or a copy is requested, we create a temporary
1238 // texture that matches the device contents
1239 view = GrSurfaceProxy::Copy(fContext.get(),
1240 rtc->asSurfaceProxy(),
1241 view.origin(),
1242 rtc->colorInfo().colorType(),
1243 GrMipMapped::kNo, // Don't auto generate mips
1244 subset,
1245 SkBackingFit::kApprox,
1246 SkBudgeted::kYes); // Always budgeted
1247 if (!view) {
1248 return nullptr;
1249 }
1250
1251 // Since this copied only the requested subset, the special image wrapping the proxy no
1252 // longer needs the original subset.
1253 finalSubset = SkIRect::MakeSize(view.proxy()->dimensions());
1254 }
1255
1256 GrColorType ct = SkColorTypeToGrColorType(this->imageInfo().colorType());
1257
1258 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1259 finalSubset,
1260 kNeedNewImageUniqueID_SpecialImage,
1261 std::move(view),
1262 ct,
1263 this->imageInfo().refColorSpace(),
1264 &this->surfaceProps());
1265 }
1266
drawDevice(SkBaseDevice * device,int left,int top,const SkPaint & paint)1267 void SkGpuDevice::drawDevice(SkBaseDevice* device,
1268 int left, int top, const SkPaint& paint) {
1269 SkASSERT(!paint.getImageFilter());
1270
1271 ASSERT_SINGLE_OWNER
1272 // clear of the source device must occur before CHECK_SHOULD_DRAW
1273 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
1274
1275 // drawDevice is defined to be in device coords.
1276 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1277 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial(SkIRect::MakeWH(dev->width(), dev->height())));
1278 if (!srcImg) {
1279 return;
1280 }
1281
1282 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
1283 }
1284
drawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)1285 void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1286 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
1287 ASSERT_SINGLE_OWNER
1288 GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
1289 this->drawImageQuad(image, src, &dst, nullptr, GrAA(paint.isAntiAlias()), aaFlags, nullptr,
1290 paint, constraint);
1291 }
1292
1293 // When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
compute_lattice_filter_mode(const SkPaint & paint)1294 static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1295 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1296 return GrSamplerState::Filter::kNearest;
1297 }
1298
1299 return GrSamplerState::Filter::kBilerp;
1300 }
1301
drawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint & paint)1302 void SkGpuDevice::drawImageNine(const SkImage* image,
1303 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
1304 ASSERT_SINGLE_OWNER
1305 uint32_t pinnedUniqueID;
1306 auto iter = std::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
1307 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
1308 GrTextureAdjuster adjuster(this->context(), std::move(view),
1309 image->imageInfo().colorInfo(), pinnedUniqueID);
1310 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
1311 } else {
1312 SkBitmap bm;
1313 if (image->isLazyGenerated()) {
1314 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1315 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
1316 } else if (as_IB(image)->getROPixels(&bm)) {
1317 GrBitmapTextureMaker maker(fContext.get(), bm, GrBitmapTextureMaker::Cached::kYes);
1318 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
1319 }
1320 }
1321 }
1322
drawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint & paint)1323 void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
1324 const SkRect& dst, const SkPaint& paint) {
1325 ASSERT_SINGLE_OWNER
1326 auto iter = std::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
1327 GrBitmapTextureMaker maker(fContext.get(), bitmap, GrBitmapTextureMaker::Cached::kYes);
1328 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
1329 }
1330
drawProducerLattice(GrTextureProducer * producer,std::unique_ptr<SkLatticeIter> iter,const SkRect & dst,const SkPaint & origPaint)1331 void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
1332 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1333 const SkPaint& origPaint) {
1334 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
1335 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
1336
1337 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1338 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1339 }
1340 GrPaint grPaint;
1341 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorInfo(),
1342 *paint, &grPaint)) {
1343 return;
1344 }
1345
1346 auto dstColorSpace = fRenderTargetContext->colorInfo().colorSpace();
1347 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
1348 auto view = producer->viewForParams(&filter, nullptr);
1349 if (!view) {
1350 return;
1351 }
1352 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1353 dstColorSpace, kPremul_SkAlphaType);
1354
1355 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->localToDevice(),
1356 std::move(view), producer->alphaType(), std::move(csxf),
1357 filter, std::move(iter), dst);
1358 }
1359
drawImageLattice(const SkImage * image,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint & paint)1360 void SkGpuDevice::drawImageLattice(const SkImage* image,
1361 const SkCanvas::Lattice& lattice, const SkRect& dst,
1362 const SkPaint& paint) {
1363 ASSERT_SINGLE_OWNER
1364 uint32_t pinnedUniqueID;
1365 auto iter = std::make_unique<SkLatticeIter>(lattice, dst);
1366 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
1367 GrTextureAdjuster adjuster(this->context(), std::move(view),
1368 image->imageInfo().colorInfo(), pinnedUniqueID);
1369 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
1370 } else {
1371 SkBitmap bm;
1372 if (image->isLazyGenerated()) {
1373 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1374 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
1375 } else if (as_IB(image)->getROPixels(&bm)) {
1376 GrBitmapTextureMaker maker(fContext.get(), bm, GrBitmapTextureMaker::Cached::kYes);
1377 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
1378 }
1379 }
1380 }
1381
drawBitmapLattice(const SkBitmap & bitmap,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint & paint)1382 void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
1383 const SkCanvas::Lattice& lattice, const SkRect& dst,
1384 const SkPaint& paint) {
1385 ASSERT_SINGLE_OWNER
1386 auto iter = std::make_unique<SkLatticeIter>(lattice, dst);
1387 GrBitmapTextureMaker maker(fContext.get(), bitmap, GrBitmapTextureMaker::Cached::kYes);
1388 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
1389 }
1390
init_vertices_paint(GrContext * context,const GrColorInfo & colorInfo,const SkPaint & skPaint,const SkMatrix & matrix,SkBlendMode bmode,bool hasTexs,bool hasColors,GrPaint * grPaint)1391 static bool init_vertices_paint(GrContext* context, const GrColorInfo& colorInfo,
1392 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
1393 bool hasTexs, bool hasColors, GrPaint* grPaint) {
1394 if (hasTexs && skPaint.getShader()) {
1395 if (hasColors) {
1396 // When there are texs and colors the shader and colors are combined using bmode.
1397 return SkPaintToGrPaintWithXfermode(context, colorInfo, skPaint, matrix, bmode,
1398 grPaint);
1399 } else {
1400 // We have a shader, but no colors to blend it against.
1401 return SkPaintToGrPaint(context, colorInfo, skPaint, matrix, grPaint);
1402 }
1403 } else {
1404 if (hasColors) {
1405 // We have colors, but either have no shader or no texture coords (which implies that
1406 // we should ignore the shader).
1407 return SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
1408 } else {
1409 // No colors and no shaders. Just draw with the paint color.
1410 return SkPaintToGrPaintNoShader(context, colorInfo, skPaint, grPaint);
1411 }
1412 }
1413 }
1414
wireframeVertices(SkVertices::VertexMode vmode,int vertexCount,const SkPoint vertices[],const SkVertices::Bone bones[],int boneCount,SkBlendMode bmode,const uint16_t indices[],int indexCount,const SkPaint & paint)1415 void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
1416 const SkPoint vertices[],
1417 const SkVertices::Bone bones[], int boneCount,
1418 SkBlendMode bmode,
1419 const uint16_t indices[], int indexCount,
1420 const SkPaint& paint) {
1421 ASSERT_SINGLE_OWNER
1422 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
1423
1424 SkPaint copy(paint);
1425 copy.setStyle(SkPaint::kStroke_Style);
1426 copy.setStrokeWidth(0);
1427
1428 GrPaint grPaint;
1429 // we ignore the shader since we have no texture coordinates.
1430 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorInfo(), copy,
1431 &grPaint)) {
1432 return;
1433 }
1434
1435 int triangleCount = 0;
1436 int n = (nullptr == indices) ? vertexCount : indexCount;
1437 switch (vmode) {
1438 case SkVertices::kTriangles_VertexMode:
1439 triangleCount = n / 3;
1440 break;
1441 case SkVertices::kTriangleStrip_VertexMode:
1442 triangleCount = n - 2;
1443 break;
1444 case SkVertices::kTriangleFan_VertexMode:
1445 SK_ABORT("Unexpected triangle fan.");
1446 break;
1447 }
1448
1449 VertState state(vertexCount, indices, indexCount);
1450 VertState::Proc vertProc = state.chooseProc(vmode);
1451
1452 //number of indices for lines per triangle with kLines
1453 indexCount = triangleCount * 6;
1454
1455 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1456 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1457 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1458
1459 uint16_t* lineIndices = builder.indices();
1460 int i = 0;
1461 while (vertProc(&state)) {
1462 lineIndices[i] = state.f0;
1463 lineIndices[i + 1] = state.f1;
1464 lineIndices[i + 2] = state.f1;
1465 lineIndices[i + 3] = state.f2;
1466 lineIndices[i + 4] = state.f2;
1467 lineIndices[i + 5] = state.f0;
1468 i += 6;
1469 }
1470
1471 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
1472 fRenderTargetContext->drawVertices(this->clip(),
1473 std::move(grPaint),
1474 this->localToDevice(),
1475 builder.detach(),
1476 bones,
1477 boneCount,
1478 &primitiveType);
1479 }
1480
drawVertices(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode mode,const SkPaint & paint)1481 void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1482 int boneCount, SkBlendMode mode, const SkPaint& paint) {
1483 ASSERT_SINGLE_OWNER
1484 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
1485
1486 SkASSERT(vertices);
1487 GrPaint grPaint;
1488 bool hasColors = vertices->hasColors();
1489 bool hasTexs = vertices->hasTexCoords();
1490 if ((!hasTexs || !paint.getShader()) && !hasColors) {
1491 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
1492 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1493 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1494 paint);
1495 return;
1496 }
1497 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorInfo(), paint,
1498 this->localToDevice(), mode, hasTexs, hasColors, &grPaint)) {
1499 return;
1500 }
1501 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->localToDevice(),
1502 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1503 bones, boneCount);
1504 }
1505
1506 ///////////////////////////////////////////////////////////////////////////////
1507
drawShadow(const SkPath & path,const SkDrawShadowRec & rec)1508 void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1509
1510 ASSERT_SINGLE_OWNER
1511 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1512
1513 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->localToDevice(), path, rec)) {
1514 // failed to find an accelerated case
1515 this->INHERITED::drawShadow(path, rec);
1516 }
1517 }
1518
1519 ///////////////////////////////////////////////////////////////////////////////
1520
drawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect texRect[],const SkColor colors[],int count,SkBlendMode mode,const SkPaint & paint)1521 void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
1522 const SkRect texRect[], const SkColor colors[], int count,
1523 SkBlendMode mode, const SkPaint& paint) {
1524 ASSERT_SINGLE_OWNER
1525 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawAtlas", fContext.get());
1526
1527 SkPaint p(paint);
1528 p.setShader(atlas->makeShader());
1529
1530 GrPaint grPaint;
1531 if (colors) {
1532 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorInfo(), p,
1533 this->localToDevice(), (SkBlendMode)mode, &grPaint)) {
1534 return;
1535 }
1536 } else {
1537 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), p,
1538 this->localToDevice(), &grPaint)) {
1539 return;
1540 }
1541 }
1542
1543 fRenderTargetContext->drawAtlas(
1544 this->clip(), std::move(grPaint), this->localToDevice(), count, xform, texRect, colors);
1545 }
1546
1547 ///////////////////////////////////////////////////////////////////////////////
1548
drawGlyphRunList(const SkGlyphRunList & glyphRunList)1549 void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
1550 ASSERT_SINGLE_OWNER
1551 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
1552
1553 // Check for valid input
1554 if (!this->localToDevice().isFinite() || !glyphRunList.allFontsFinite()) {
1555 return;
1556 }
1557
1558 fRenderTargetContext->drawGlyphRunList(this->clip(), this->localToDevice(), glyphRunList);
1559 }
1560
1561 ///////////////////////////////////////////////////////////////////////////////
1562
drawDrawable(SkDrawable * drawable,const SkMatrix * matrix,SkCanvas * canvas)1563 void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
1564 GrBackendApi api = this->context()->backend();
1565 if (GrBackendApi::kVulkan == api) {
1566 const SkMatrix& ctm = canvas->getTotalMatrix();
1567 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1568 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
1569 drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(),
1570 this->imageInfo());
1571 if (gpuDraw) {
1572 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1573 return;
1574 }
1575 }
1576 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1577 }
1578
1579
1580 ///////////////////////////////////////////////////////////////////////////////
1581
flush()1582 void SkGpuDevice::flush() {
1583 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo());
1584 }
1585
flush(SkSurface::BackendSurfaceAccess access,const GrFlushInfo & info)1586 GrSemaphoresSubmitted SkGpuDevice::flush(SkSurface::BackendSurfaceAccess access,
1587 const GrFlushInfo& info) {
1588 ASSERT_SINGLE_OWNER
1589
1590 return fRenderTargetContext->flush(access, info);
1591 }
1592
wait(int numSemaphores,const GrBackendSemaphore * waitSemaphores)1593 bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
1594 ASSERT_SINGLE_OWNER
1595
1596 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
1597 }
1598
1599 ///////////////////////////////////////////////////////////////////////////////
1600
onCreateDevice(const CreateInfo & cinfo,const SkPaint *)1601 SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
1602 ASSERT_SINGLE_OWNER
1603
1604 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
1605
1606 // layers are never drawn in repeat modes, so we can request an approx
1607 // match and ignore any padding.
1608 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1609 : SkBackingFit::kExact;
1610
1611 SkASSERT(cinfo.fInfo.colorType() != kRGBA_1010102_SkColorType);
1612
1613 auto rtc = GrRenderTargetContext::MakeWithFallback(
1614 fContext.get(), SkColorTypeToGrColorType(cinfo.fInfo.colorType()),
1615 fRenderTargetContext->colorInfo().refColorSpace(), fit, cinfo.fInfo.dimensions(),
1616 fRenderTargetContext->numSamples(), GrMipMapped::kNo,
1617 fRenderTargetContext->asSurfaceProxy()->isProtected(), kBottomLeft_GrSurfaceOrigin,
1618 SkBudgeted::kYes, &props);
1619 if (!rtc) {
1620 return nullptr;
1621 }
1622
1623 // Skia's convention is to only clear a device if it is non-opaque.
1624 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1625
1626 return SkGpuDevice::Make(fContext.get(), std::move(rtc), init).release();
1627 }
1628
makeSurface(const SkImageInfo & info,const SkSurfaceProps & props)1629 sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1630 ASSERT_SINGLE_OWNER
1631 // TODO: Change the signature of newSurface to take a budgeted parameter.
1632 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
1633 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
1634 fRenderTargetContext->numSamples(),
1635 fRenderTargetContext->origin(), &props);
1636 }
1637
getImageFilterCache()1638 SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
1639 ASSERT_SINGLE_OWNER
1640 // We always return a transient cache, so it is freed after each
1641 // filter traversal.
1642 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
1643 }
1644
1645 ////////////////////////////////////////////////////////////////////////////////////
1646
android_utils_clipWithStencil()1647 bool SkGpuDevice::android_utils_clipWithStencil() {
1648 SkRegion clipRegion;
1649 this->onAsRgnClip(&clipRegion);
1650 if (clipRegion.isEmpty()) {
1651 return false;
1652 }
1653 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
1654 if (!rtc) {
1655 return false;
1656 }
1657 GrPaint grPaint;
1658 grPaint.setXPFactory(GrDisableColorXPFactory::Get());
1659 GrNoClip noClip;
1660 static constexpr GrUserStencilSettings kDrawToStencil(
1661 GrUserStencilSettings::StaticInit<
1662 0x1,
1663 GrUserStencilTest::kAlways,
1664 0x1,
1665 GrUserStencilOp::kReplace,
1666 GrUserStencilOp::kReplace,
1667 0x1>()
1668 );
1669 rtc->drawRegion(noClip, std::move(grPaint), GrAA::kNo, SkMatrix::I(), clipRegion,
1670 GrStyle::SimpleFill(), &kDrawToStencil);
1671 return true;
1672 }
1673
1674