blob: abd0237bb7cb52d793c0ad73ee4f6cf1d1b03751 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/SkGpuDevice.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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"
Michael Ludwig8ee6cf32019-08-02 09:57:04 -040023#include "src/core/SkImageFilter_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkLatticeIter.h"
25#include "src/core/SkMakeUnique.h"
26#include "src/core/SkPictureData.h"
27#include "src/core/SkRRectPriv.h"
28#include "src/core/SkRasterClip.h"
29#include "src/core/SkRecord.h"
30#include "src/core/SkSpecialImage.h"
31#include "src/core/SkStroke.h"
32#include "src/core/SkTLazy.h"
33#include "src/core/SkVertState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrBitmapTextureMaker.h"
35#include "src/gpu/GrBlurUtils.h"
36#include "src/gpu/GrContextPriv.h"
37#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040038#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrImageTextureMaker.h"
40#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrStyle.h"
42#include "src/gpu/GrSurfaceProxyPriv.h"
43#include "src/gpu/GrTextureAdjuster.h"
44#include "src/gpu/GrTracing.h"
45#include "src/gpu/SkGr.h"
46#include "src/gpu/effects/GrBicubicEffect.h"
47#include "src/gpu/effects/GrTextureDomain.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040048#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/text/GrTextTarget.h"
50#include "src/image/SkImage_Base.h"
51#include "src/image/SkReadPixelsRec.h"
52#include "src/image/SkSurface_Gpu.h"
53#include "src/utils/SkUTF.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054
joshualittce894002016-01-11 13:29:31 -080055#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050056SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->priv().singleOwner());)
joshualittce894002016-01-11 13:29:31 -080057
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059///////////////////////////////////////////////////////////////////////////////
60
bsalomon74f681d2015-06-23 14:38:48 -070061/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
62 should fail. */
63bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
64 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
65 *flags = 0;
66 if (info) {
67 switch (info->alphaType()) {
68 case kPremul_SkAlphaType:
69 break;
70 case kOpaque_SkAlphaType:
71 *flags |= SkGpuDevice::kIsOpaque_Flag;
72 break;
73 default: // If it is unpremul or unknown don't try to render
74 return false;
75 }
76 }
77 if (kClear_InitContents == init) {
78 *flags |= kNeedClear_Flag;
79 }
80 return true;
81}
82
Robert Phillips9fab7e92016-11-17 12:45:04 -050083sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
Brian Salomonbf6b9792019-08-21 09:38:10 -040084 std::unique_ptr<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070085 InitContents init) {
Robert Phillips920d4882019-03-04 15:16:44 -050086 if (!renderTargetContext || context->priv().abandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070087 return nullptr;
88 }
Robert Phillipsd470e1b2019-09-04 15:05:35 -040089
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040090 SkColorType ct = GrColorTypeToSkColorType(renderTargetContext->colorInfo().colorType());
Robert Phillipsd470e1b2019-09-04 15:05:35 -040091
robertphillipsca6eafc2016-05-17 09:57:46 -070092 unsigned flags;
Robert Phillipsd470e1b2019-09-04 15:05:35 -040093 if (!context->colorTypeSupportedAsSurface(ct) ||
94 !CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
robertphillipsca6eafc2016-05-17 09:57:46 -070095 return nullptr;
96 }
Brian Salomonbf6b9792019-08-21 09:38:10 -040097 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000098}
99
robertphillips24e91282016-04-29 06:46:36 -0700100sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
101 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400102 GrSurfaceOrigin origin, const SkSurfaceProps* props,
103 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700104 unsigned flags;
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400105 if (!context->colorTypeSupportedAsSurface(info.colorType()) ||
106 !CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700108 }
109
Brian Salomonbf6b9792019-08-21 09:38:10 -0400110 auto renderTargetContext =
111 MakeRenderTargetContext(context, budgeted, info, sampleCount, origin, props, mipMapped);
Brian Osman11052242016-10-27 14:47:55 -0400112 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700114 }
115
Brian Salomonbf6b9792019-08-21 09:38:10 -0400116 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700117}
118
Brian Salomonbf6b9792019-08-21 09:38:10 -0400119static SkImageInfo make_info(GrRenderTargetContext* context, bool opaque) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400120 SkColorType colorType = GrColorTypeToSkColorType(context->colorInfo().colorType());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400121 return SkImageInfo::Make(context->width(), context->height(), colorType,
122 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400123 context->colorInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700124}
125
Brian Salomonbf6b9792019-08-21 09:38:10 -0400126SkGpuDevice::SkGpuDevice(GrContext* context,
127 std::unique_ptr<GrRenderTargetContext> renderTargetContext,
128 unsigned flags)
129 : INHERITED(make_info(renderTargetContext.get(), SkToBool(flags & kIsOpaque_Flag)),
130 renderTargetContext->surfaceProps())
131 , fContext(SkRef(context))
132 , fRenderTargetContext(std::move(renderTargetContext)) {
bsalomone63ffef2016-02-05 07:17:34 -0800133 if (flags & kNeedClear_Flag) {
134 this->clearAll();
135 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000136}
137
Brian Salomonbf6b9792019-08-21 09:38:10 -0400138std::unique_ptr<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
139 GrContext* context,
140 SkBudgeted budgeted,
141 const SkImageInfo& origInfo,
142 int sampleCount,
143 GrSurfaceOrigin origin,
144 const SkSurfaceProps* surfaceProps,
145 GrMipMapped mipMapped) {
bsalomonafe30052015-01-16 07:32:33 -0800146 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700147 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800148 }
149
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400150 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
151 // they need to be exact.
Robert Phillips9da87e02019-02-04 13:26:26 -0500152 return context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400153 SkBackingFit::kExact, origInfo.width(), origInfo.height(),
Brian Salomond6287472019-06-24 15:50:07 -0400154 SkColorTypeToGrColorType(origInfo.colorType()), origInfo.refColorSpace(), sampleCount,
155 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800156}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000157
Mike Reeda1361362017-03-07 09:37:29 -0500158sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700159 int left, int top,
160 SkIPoint* offset,
161 const SkImageFilter* filter) {
162 SkASSERT(srcImg->isTextureBacked());
163 SkASSERT(filter);
164
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400165 SkMatrix matrix = this->localToDevice();
robertphillips970587b2016-07-14 14:12:55 -0700166 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500167 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500168 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400169 SkColorType colorType = GrColorTypeToSkColorType(fRenderTargetContext->colorInfo().colorType());
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400170 if (colorType == kUnknown_SkColorType) {
171 colorType = kRGBA_8888_SkColorType;
Brian Osmana50205f2018-07-06 13:57:01 -0400172 }
Michael Ludwig03f9ca32019-08-14 14:35:15 -0400173 SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), colorType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400174 fRenderTargetContext->colorInfo().colorSpace(), srcImg);
robertphillips970587b2016-07-14 14:12:55 -0700175
Michael Ludwigea071232019-08-26 10:52:15 -0400176 return as_IFB(filter)->filterImage(ctx).imageAndOffset(offset);
robertphillips970587b2016-07-14 14:12:55 -0700177}
178
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000179///////////////////////////////////////////////////////////////////////////////
180
Mike Reed353196f2017-07-21 11:01:18 -0400181bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800182 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000183
Mike Reed353196f2017-07-21 11:01:18 -0400184 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500185 return false;
186 }
187
Brian Salomon1d435302019-07-01 13:05:28 -0400188 return fRenderTargetContext->readPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), {x, y});
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000189}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000190
Mike Reed353196f2017-07-21 11:01:18 -0400191bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800192 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700193
Mike Reed353196f2017-07-21 11:01:18 -0400194 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500195 return false;
196 }
197
Brian Salomon1d435302019-07-01 13:05:28 -0400198 return fRenderTargetContext->writePixels(pm.info(), pm.addr(), pm.rowBytes(), {x, y});
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000199}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200
reed41e010c2015-06-09 12:16:53 -0700201bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800202 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700203 return false;
204}
205
Brian Osman11052242016-10-27 14:47:55 -0400206GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700207 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400208 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700209}
210
reed8eddfb52014-12-04 07:50:14 -0800211void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800212 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500213 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500214
reed8eddfb52014-12-04 07:50:14 -0800215 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman9a9baae2018-11-05 15:06:26 -0500216 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
217 GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800218}
219
Brian Salomonbf6b9792019-08-21 09:38:10 -0400220void SkGpuDevice::replaceRenderTargetContext(std::unique_ptr<GrRenderTargetContext> rtc,
Brian Salomonaad83152019-05-24 10:16:35 -0400221 bool shouldRetainContent) {
222 SkASSERT(rtc->width() == this->width());
223 SkASSERT(rtc->height() == this->height());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600224 SkASSERT(rtc->numSamples() == fRenderTargetContext->numSamples());
Brian Salomonaad83152019-05-24 10:16:35 -0400225 SkASSERT(rtc->asSurfaceProxy()->priv().isExact());
226 if (shouldRetainContent) {
227 if (this->context()->abandoned()) {
228 return;
229 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400230
231 SkASSERT(fRenderTargetContext->asTextureProxy());
232 SkAssertResult(rtc->blitTexture(fRenderTargetContext->asTextureProxy(),
Brian Salomon078e8fa2019-11-22 04:10:18 +0000233 fRenderTargetContext->colorInfo().colorType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400234 SkIRect::MakeWH(this->width(), this->height()),
235 SkIPoint::Make(0,0)));
Brian Salomonaad83152019-05-24 10:16:35 -0400236 }
237
238 fRenderTargetContext = std::move(rtc);
239}
240
Brian Osman11052242016-10-27 14:47:55 -0400241void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800242 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800243
Brian Osman693a5402016-10-27 15:13:22 -0400244 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800245
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400246 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
247 // kExact-backed render target context.
Brian Salomonbf6b9792019-08-21 09:38:10 -0400248 auto newRTC = MakeRenderTargetContext(this->context(),
249 budgeted,
250 this->imageInfo(),
251 fRenderTargetContext->numSamples(),
252 fRenderTargetContext->origin(),
253 &this->surfaceProps(),
254 fRenderTargetContext->mipMapped());
Brian Osman693a5402016-10-27 15:13:22 -0400255 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800256 return;
257 }
Brian Salomonaad83152019-05-24 10:16:35 -0400258 this->replaceRenderTargetContext(std::move(newRTC), shouldRetainContent);
kkinnunenabcfab42015-02-22 22:53:44 -0800259}
260
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000261///////////////////////////////////////////////////////////////////////////////
262
Mike Reeda1361362017-03-07 09:37:29 -0500263void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800264 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500265 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000266
267 GrPaint grPaint;
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400268 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
269 this->localToDevice(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700270 return;
271 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400273 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->localToDevice());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274}
275
Brian Salomon1459ebd2017-12-19 16:12:44 -0500276static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
277 switch (mode) {
278 case SkCanvas::kPoints_PointMode:
279 return GrPrimitiveType::kPoints;
280 case SkCanvas::kLines_PointMode:
281 return GrPrimitiveType::kLines;
282 case SkCanvas::kPolygon_PointMode:
283 return GrPrimitiveType::kLineStrip;
284 }
285 SK_ABORT("Unexpected mode");
Brian Salomon1459ebd2017-12-19 16:12:44 -0500286}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287
Mike Reeda1361362017-03-07 09:37:29 -0500288void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800290 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500291 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292 SkScalar width = paint.getStrokeWidth();
293 if (width < 0) {
294 return;
295 }
296
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000297 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700298 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700299 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400300 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400301 this->localToDevice(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700302 return;
303 }
egdaniele61c4112014-06-12 10:24:21 -0700304 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700305 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700306 path.moveTo(pts[0]);
307 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700308 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400309 this->localToDevice(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700310 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000311 }
312
bsalomon6ade6dd2016-09-12 12:07:17 -0700313 SkScalar scales[2];
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400314 bool isHairline = (0 == width) ||
315 (1 == width && this->localToDevice().getMinMaxScales(scales) &&
316 SkScalarNearlyEqual(scales[0], 1.f) && SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700317 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400319 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500320 SkRasterClip rc(this->devClipBounds());
321 SkDraw draw;
322 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400323 draw.fMatrix = &this->localToDevice();
Mike Reeda1361362017-03-07 09:37:29 -0500324 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500325 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326 return;
327 }
328
Brian Salomon1459ebd2017-12-19 16:12:44 -0500329 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700330
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400331 const SkMatrix* viewMatrix = &this->localToDevice();
bsalomon6ade6dd2016-09-12 12:07:17 -0700332#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
333 // This offsetting in device space matches the expectations of the Android framework for non-AA
334 // points and lines.
335 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600336 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700337 tempMatrix = *viewMatrix;
338 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
339 tempMatrix.postTranslate(kOffset, kOffset);
340 viewMatrix = &tempMatrix;
341 }
342#endif
343
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000344 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400345 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint, *viewMatrix,
346 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700347 return;
348 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000349
Brian Osmanae0c50c2017-05-25 16:56:34 -0400350 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
351 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
352 nullptr);
353
354 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400355 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356}
357
358///////////////////////////////////////////////////////////////////////////////
359
Mike Reeda1361362017-03-07 09:37:29 -0500360void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800361 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500362 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400363
364 GrStyle style(paint);
365
bsalomona7d85ba2016-07-06 11:54:59 -0700366 // A couple reasons we might need to call drawPath.
367 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400368 GrShape shape(rect, style);
369
370 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400371 this->clip(), paint, this->localToDevice(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000372 return;
373 }
374
375 GrPaint grPaint;
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400376 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
377 this->localToDevice(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700378 return;
379 }
Mike Klein744fb732014-06-23 15:13:26 -0400380
Chris Dalton3b51df12017-11-27 14:33:06 -0700381 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400382 this->localToDevice(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383}
384
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400385void SkGpuDevice::drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
Michael Ludwiga595f862019-08-27 15:25:49 -0400386 SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color,
387 SkBlendMode mode) {
Michael Ludwig75451902019-01-23 11:14:29 -0500388 ASSERT_SINGLE_OWNER
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400389 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawEdgeAAQuad", fContext.get());
Michael Ludwig75451902019-01-23 11:14:29 -0500390
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400391 SkPMColor4f dstColor = SkColor4fPrepForDst(color, fRenderTargetContext->colorInfo()).premul();
Michael Ludwig75451902019-01-23 11:14:29 -0500392
393 GrPaint grPaint;
394 grPaint.setColor4f(dstColor);
395 if (mode != SkBlendMode::kSrcOver) {
396 grPaint.setXPFactory(SkBlendMode_AsXPFactory(mode));
397 }
398
Michael Ludwig136f45a2019-02-19 11:44:41 -0500399 // This is exclusively meant for tiling operations, so keep AA enabled to handle MSAA seaming
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500400 GrQuadAAFlags grAA = SkToGrQuadAAFlags(aaFlags);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400401 if (clip) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500402 // Use fillQuadWithEdgeAA
403 fRenderTargetContext->fillQuadWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400404 this->localToDevice(), clip, nullptr);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500405 } else {
406 // Use fillRectWithEdgeAA to preserve mathematical properties of dst being rectangular
407 fRenderTargetContext->fillRectWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400408 this->localToDevice(), rect);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500409 }
Michael Ludwig75451902019-01-23 11:14:29 -0500410}
411
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412///////////////////////////////////////////////////////////////////////////////
413
Mike Reeda1361362017-03-07 09:37:29 -0500414void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800415 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500416 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400417
Mike Reed80747ef2018-01-23 15:29:32 -0500418 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000419 if (mf) {
420 if (mf->hasFragmentProcessor()) {
421 mf = nullptr; // already handled in SkPaintToGrPaint
422 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400423 }
424
bsalomon6663acf2016-05-10 09:14:17 -0700425 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000426
Robert Phillipsa29a9562016-10-20 09:40:55 -0400427 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800428 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400429 GrShape shape(rrect, style);
430
Robert Phillips27927a52018-08-20 13:18:12 -0400431 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400432 this->clip(), paint, this->localToDevice(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000433 return;
434 }
Mike Klein744fb732014-06-23 15:13:26 -0400435
bsalomon6663acf2016-05-10 09:14:17 -0700436 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800437
Robert Phillipsa522d662018-08-23 13:50:16 -0400438 GrPaint grPaint;
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400439 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
440 this->localToDevice(), &grPaint)) {
Robert Phillipsa522d662018-08-23 13:50:16 -0400441 return;
442 }
443
Chris Dalton3b51df12017-11-27 14:33:06 -0700444 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400445 this->localToDevice(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446}
447
robertphillipsd7706102016-02-25 09:28:08 -0800448
Robert Phillips20390c32018-08-17 11:01:03 -0400449void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800450 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500451 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800452 if (outer.isEmpty()) {
453 return;
454 }
455
456 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500457 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800458 }
459
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000460 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000461
robertphillips0e7029e2015-11-30 05:45:06 -0800462 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800463 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400464 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400465 this->localToDevice(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700466 return;
467 }
robertphillips00095892016-02-29 13:50:40 -0800468
Brian Salomon0166cfd2017-03-13 17:58:25 -0400469 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400470 GrAA(paint.isAntiAlias()), this->localToDevice(),
471 outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800472 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000473 }
474
475 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700476 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000477 path.addRRect(outer);
478 path.addRRect(inner);
479 path.setFillType(SkPath::kEvenOdd_FillType);
480
Robert Phillips27927a52018-08-20 13:18:12 -0400481 // TODO: We are losing the possible mutability of the path here but this should probably be
482 // fixed by upgrading GrShape to handle DRRects.
483 GrShape shape(path, paint);
484
485 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400486 paint, this->localToDevice(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000487}
488
489
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000490/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491
Mike Reeda1361362017-03-07 09:37:29 -0500492void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700493 if (paint.getMaskFilter()) {
494 SkPath path;
495 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400496 path.setIsVolatile(true);
497 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700498 }
499
500 GrPaint grPaint;
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400501 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
502 this->localToDevice(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700503 return;
504 }
505
Chris Dalton3b51df12017-11-27 14:33:06 -0700506 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400507 this->localToDevice(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700508}
509
Mike Reeda1361362017-03-07 09:37:29 -0500510void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800511 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500512 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800513
robertphillips514450c2015-11-24 05:36:02 -0800514 if (paint.getMaskFilter()) {
515 // The RRect path can handle special case blurring
516 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500517 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518 }
519
520 GrPaint grPaint;
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400521 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
522 this->localToDevice(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700523 return;
524 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525
Chris Dalton3b51df12017-11-27 14:33:06 -0700526 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400527 this->localToDevice(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000528}
529
Mike Reeda1361362017-03-07 09:37:29 -0500530void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700531 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
532 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500533 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700534 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500535 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700536 return;
537 }
538 GrPaint grPaint;
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400539 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
540 this->localToDevice(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700541 return;
542 }
543
Chris Dalton3b51df12017-11-27 14:33:06 -0700544 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400545 this->localToDevice(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500546 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700547}
548
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500549#include "include/core/SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550
551///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700552void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700553 const SkPaint& origPaint) {
554 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500555 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400556 // Adding support for round capping would require a
557 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700558 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
559 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
560 SkASSERT(!origPaint.getPathEffect());
561 SkASSERT(!origPaint.getMaskFilter());
562
563 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
564 SkASSERT(halfWidth > 0);
565
566 SkVector v = points[1] - points[0];
567
568 SkScalar length = SkPoint::Normalize(&v);
569 if (!length) {
570 v.fX = 1.0f;
571 v.fY = 0.0f;
572 }
573
574 SkPaint newPaint(origPaint);
575 newPaint.setStyle(SkPaint::kFill_Style);
576
577 SkScalar xtraLength = 0.0f;
578 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
579 xtraLength = halfWidth;
580 }
581
582 SkPoint mid = points[0] + points[1];
583 mid.scale(0.5f);
584
585 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
586 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
587 SkMatrix m;
588 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
589
590 SkMatrix local = m;
591
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400592 m.postConcat(this->localToDevice());
robertphillips0851d2d2016-06-02 05:21:34 -0700593
594 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400595 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), newPaint, m,
Brian Salomonf3569f02017-10-24 12:52:33 -0400596 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700597 return;
598 }
599
Brian Salomon82f44312017-01-11 13:42:54 -0500600 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700601 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700602}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603
Robert Phillips20390c32018-08-17 11:01:03 -0400604void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800605 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400606 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700607 SkPoint points[2];
608 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
609 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400610 this->localToDevice().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700611 // Path-based stroking looks better for thin rects
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400612 SkScalar strokeWidth = this->localToDevice().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700613 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500614 // Round capping support is currently disabled b.c. it would require a RRect
615 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500616 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700617 return;
618 }
619 }
robertphillipsff55b492015-11-24 07:56:59 -0800620 }
621
Hal Canary144caf52016-11-07 17:57:18 -0500622 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400623 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500624 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400625 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), paint,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400626 this->localToDevice(), &grPaint)) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500627 return;
628 }
629 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400630 this->localToDevice(), origSrcPath, GrStyle(paint));
Brian Salomona3cf3652018-01-03 15:11:00 -0500631 return;
632 }
Robert Phillips27927a52018-08-20 13:18:12 -0400633
634 // TODO: losing possible mutability of 'origSrcPath' here
635 GrShape shape(origSrcPath, paint);
636
637 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400638 paint, this->localToDevice(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000639}
640
641static const int kBmpSmallTileSize = 1 << 10;
642
643static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
644 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
645 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
646 return tilesX * tilesY;
647}
648
reed85d91782015-09-10 14:33:38 -0700649static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000650 if (maxTileSize <= kBmpSmallTileSize) {
651 return maxTileSize;
652 }
653
654 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
655 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
656
657 maxTileTotalTileSize *= maxTileSize * maxTileSize;
658 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
659
660 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
661 return kBmpSmallTileSize;
662 } else {
663 return maxTileSize;
664 }
665}
666
667// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
668// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700669static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800670 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800671 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700672 const SkMatrix& srcToDstRect,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400673 const SkISize& imageDimensions,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 const SkRect* srcRectPtr,
675 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700676 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700677 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
678 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000679 clippedSrcIRect->setEmpty();
680 return;
681 }
682 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
683 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700684 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000685 if (!clippedSrcRect.intersect(*srcRectPtr)) {
686 clippedSrcIRect->setEmpty();
687 return;
688 }
689 }
690 clippedSrcRect.roundOut(clippedSrcIRect);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400691 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000692 if (!clippedSrcIRect->intersect(bmpBounds)) {
693 clippedSrcIRect->setEmpty();
694 }
695}
696
Robert Phillips920d4882019-03-04 15:16:44 -0500697const GrCaps* SkGpuDevice::caps() const {
698 return fContext->priv().caps();
699}
700
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400701bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
702 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700703 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700704 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400705 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700706 const SkRect* srcRectPtr,
707 int maxTileSize,
708 int* tileSize,
709 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800710 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700711 // if it's larger than the max tile size, then we have no choice but tiling.
712 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400713 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400714 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
715 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700716 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
717 return true;
718 }
719
bsalomon1a1d0b82015-10-16 07:49:42 -0700720 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700721 const size_t area = imageRect.width() * imageRect.height();
722 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
723 return false;
724 }
725
reed85d91782015-09-10 14:33:38 -0700726 // At this point we know we could do the draw by uploading the entire bitmap
727 // as a texture. However, if the texture would be large compared to the
728 // cache size and we don't require most of it for this draw then tile to
729 // reduce the amount of upload and cache spill.
730
731 // assumption here is that sw bitmap size is a good proxy for its size as
732 // a texture
733 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Robert Phillipscf39f372019-09-03 10:29:20 -0400734 size_t cacheSize = fContext->getResourceCacheLimit();
reed85d91782015-09-10 14:33:38 -0700735 if (bmpSize < cacheSize / 2) {
736 return false;
737 }
738
bsalomon1a1d0b82015-10-16 07:49:42 -0700739 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
740 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400741 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
742 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400743 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700744 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
745 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500746 kBmpSmallTileSize * kBmpSmallTileSize *
747 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700748
Brian Osmand05cdc32017-02-06 13:24:47 -0500749 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700750}
751
reed85d91782015-09-10 14:33:38 -0700752bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
753 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700754 const SkMatrix& viewMatrix,
755 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800756 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400757 // If image is explicitly texture backed then we shouldn't get here.
758 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400760 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700761 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500762 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600763 image->width(), image->height(), quality, viewMatrix, srcToDstRect,
Robert Phillips9da87e02019-02-04 13:26:26 -0500764 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700765
766 int tileFilterPad;
767 if (doBicubic) {
768 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400769 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700770 tileFilterPad = 0;
771 } else {
772 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400774 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400776 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777
reed85d91782015-09-10 14:33:38 -0700778 // these are output, which we safely ignore, as we just want to know the predicate
779 int outTileSize;
780 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781
bsalomone553b642016-08-17 09:02:09 -0700782 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400783 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700784 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785}
786
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000787// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
789// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000790static inline void clamped_outset_with_offset(SkIRect* iRect,
791 int outset,
792 SkPoint* offset,
793 const SkIRect& clamp) {
794 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000796 int leftClampDelta = clamp.fLeft - iRect->fLeft;
797 if (leftClampDelta > 0) {
798 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000799 iRect->fLeft = clamp.fLeft;
800 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000801 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000803
804 int topClampDelta = clamp.fTop - iRect->fTop;
805 if (topClampDelta > 0) {
806 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 iRect->fTop = clamp.fTop;
808 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000809 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810 }
811
812 if (iRect->fRight > clamp.fRight) {
813 iRect->fRight = clamp.fRight;
814 }
815 if (iRect->fBottom > clamp.fBottom) {
816 iRect->fBottom = clamp.fBottom;
817 }
818}
819
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820// Break 'bitmap' into several tiles to draw it since it has already
821// been determined to be too large to fit in VRAM
822void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800823 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700824 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825 const SkRect& srcRect,
826 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400827 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800828 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700829 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000830 int tileSize,
831 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800832 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800833
ericrk983294f2016-04-18 09:14:00 -0700834 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800835 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
Michael Ludwig3e2cc062019-02-19 12:11:40 -0500836 LogDrawScaleFactor(viewMatrix, SkMatrix::I(), origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800837
bsalomonc55271f2015-11-09 11:55:57 -0800838 const SkPaint* paint = &origPaint;
839 SkPaint tempPaint;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600840 if (origPaint.isAntiAlias() && fRenderTargetContext->numSamples() <= 1) {
bsalomonc55271f2015-11-09 11:55:57 -0800841 // Drop antialiasing to avoid seams at tile boundaries.
842 tempPaint = origPaint;
843 tempPaint.setAntiAlias(false);
844 paint = &tempPaint;
845 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000846 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
847
848 int nx = bitmap.width() / tileSize;
849 int ny = bitmap.height() / tileSize;
850 for (int x = 0; x <= nx; x++) {
851 for (int y = 0; y <= ny; y++) {
852 SkRect tileR;
Mike Reed92b33352019-08-24 19:39:13 -0400853 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
854 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855
856 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
857 continue;
858 }
859
860 if (!tileR.intersect(srcRect)) {
861 continue;
862 }
863
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000864 SkIRect iTileR;
865 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700866 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
867 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500868 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700869 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400870 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871 SkIRect iClampRect;
872
reeda5517e22015-07-14 10:54:12 -0700873 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874 // In bleed mode we want to always expand the tile on all edges
875 // but stay within the bitmap bounds
876 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
877 } else {
878 // In texture-domain/clamp mode we only want to expand the
879 // tile on edges interior to "srcRect" (i.e., we want to
880 // not bleed across the original clamped edges)
881 srcRect.roundOut(&iClampRect);
882 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000883 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
884 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885 }
886
bsalomone553b642016-08-17 09:02:09 -0700887 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888 if (bitmap.extractSubset(&tmpB, iTileR)) {
889 // now offset it to make it "local" to our tmp bitmap
890 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800891 // de-optimized this determination
892 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700893 this->drawBitmapTile(tmpB,
894 viewMatrix,
895 rectToDraw,
896 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500897 params,
bsalomone553b642016-08-17 09:02:09 -0700898 *paint,
899 constraint,
900 bicubic,
901 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000902 }
903 }
904 }
905}
906
bsalomone553b642016-08-17 09:02:09 -0700907void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
908 const SkMatrix& viewMatrix,
909 const SkRect& dstRect,
910 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400911 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700912 const SkPaint& paint,
913 SkCanvas::SrcRectConstraint constraint,
914 bool bicubic,
915 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800916 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400917 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
918 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700919 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400920 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
921 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400922 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923
Brian Salomon2a943df2018-05-04 13:43:19 -0400924 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400925 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400926 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400927 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 return;
929 }
bsalomone553b642016-08-17 09:02:09 -0700930
bsalomone553b642016-08-17 09:02:09 -0700931 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400932 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
933 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700934
Brian Salomon078e8fa2019-11-22 04:10:18 +0000935 GrColorType srcColorType = SkColorTypeToGrColorType(bitmap.colorType());
Greg Danielc594e622019-10-15 14:01:49 -0400936
joshualitt5f10b5c2015-07-09 10:24:35 -0700937 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
938 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400939 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700940
reeda5517e22015-07-14 10:54:12 -0700941 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700943 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500945 domain.fLeft = srcRect.fLeft + 0.5f;
946 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500948 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 }
950 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500951 domain.fTop = srcRect.fTop + 0.5f;
952 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500954 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000956 if (bicubic) {
Brian Salomona86fc7a2019-05-28 20:42:58 -0400957 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomon078e8fa2019-11-22 04:10:18 +0000958 fp = GrBicubicEffect::Make(std::move(proxy), srcColorType, texMatrix, domain, kDir,
959 bitmap.alphaType());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000960 } else {
Brian Salomon078e8fa2019-11-22 04:10:18 +0000961 fp = GrTextureDomainEffect::Make(std::move(proxy), srcColorType, texMatrix, domain,
Michael Ludwig170de012019-11-15 21:55:18 +0000962 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000963 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000964 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400965 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
966 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Salomona86fc7a2019-05-28 20:42:58 -0400967 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomon078e8fa2019-11-22 04:10:18 +0000968 fp = GrBicubicEffect::Make(std::move(proxy), srcColorType, texMatrix, wrapMode, kDir,
969 bitmap.alphaType());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970 } else {
Brian Salomon078e8fa2019-11-22 04:10:18 +0000971 fp = GrSimpleTextureEffect::Make(std::move(proxy), srcColorType, texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 }
973
Brian Osman21fc5ce2018-08-27 20:36:19 +0000974 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400975 fRenderTargetContext->colorInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -0800976 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400977 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorInfo(), paint,
Brian Salomonf3569f02017-10-24 12:52:33 -0400978 viewMatrix, std::move(fp),
979 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700980 return;
981 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500983 // Coverage-based AA would cause seams between tiles.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600984 GrAA aa = GrAA(paint.isAntiAlias() && fRenderTargetContext->numSamples() > 1);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400985 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000986}
987
Mike Reeda1361362017-03-07 09:37:29 -0500988void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000989 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800990 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500991 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992
Robert Phillips920d4882019-03-04 15:16:44 -0500993 if (fContext->priv().abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 return;
995 }
996
Robert Phillipse14d3052017-02-15 13:18:21 -0500997 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
998 if (!srcImg) {
999 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001000 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001
Florin Malita53f77bd2017-04-28 13:48:37 -04001002 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001003}
1004
1005
Robert Phillipsc100d482018-07-10 10:11:01 -04001006void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001007 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001008 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001009 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001010
robertphillips970587b2016-07-14 14:12:55 -07001011 sk_sp<SkSpecialImage> result;
1012 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001013 SkIPoint offset = { 0, 0 };
1014
1015 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001016 if (!result) {
1017 return;
1018 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001019
1020 left += offset.fX;
1021 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001022 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001023 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001024 }
1025
1026 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001027 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001028 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001029 return;
1030 }
robertphillips970587b2016-07-14 14:12:55 -07001031
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001032 SkMatrix ctm = this->localToDevice();
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001033 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1034
robertphillips970587b2016-07-14 14:12:55 -07001035 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001036 if (tmpUnfiltered.getMaskFilter()) {
Florin Malitac6c5ead2018-04-11 15:33:40 -04001037 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001038 }
1039
robertphillips970587b2016-07-14 14:12:55 -07001040 tmpUnfiltered.setImageFilter(nullptr);
1041
Brian Salomon078e8fa2019-11-22 04:10:18 +00001042 GrColorType srcColorType = SkColorTypeToGrColorType(result->colorType());
1043 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), srcColorType, SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001044 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001045 fRenderTargetContext->colorInfo().colorSpace());
Greg Daniela4828a12019-10-11 13:51:02 -04001046 if (GrColorTypeIsAlphaOnly(SkColorTypeToGrColorType(result->colorType()))) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001047 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001048 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -04001049 if (paint.getColor4f().isOpaque()) {
1050 fp = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
1051 } else {
1052 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
1053 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001054 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001055
1056 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001057 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorInfo(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001058 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001059 return;
1060 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061
robertphillips970587b2016-07-14 14:12:55 -07001062 const SkIRect& subset = result->subset();
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001063 SkRect dstRect = SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()));
1064 SkRect srcRect = SkRect::Make(subset);
1065 if (clipImage) {
1066 // Add the image as a simple texture effect applied to coverage. Accessing content outside
1067 // of the clip image should behave as if it were a decal (i.e. zero coverage). However, to
1068 // limit pixels touched and hardware checks, we draw the clip image geometry to get the
1069 // decal effect.
1070 GrSamplerState sampler = paint.getFilterQuality() > kNone_SkFilterQuality ?
1071 GrSamplerState::ClampBilerp() : GrSamplerState::ClampNearest();
1072 sk_sp<GrTextureProxy> clipProxy = as_IB(clipImage)->asTextureProxyRef(this->context(),
1073 sampler, nullptr);
1074 // Fold clip matrix into ctm
1075 ctm.preConcat(clipMatrix);
1076 SkMatrix inverseClipMatrix;
robertphillips970587b2016-07-14 14:12:55 -07001077
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001078 std::unique_ptr<GrFragmentProcessor> cfp;
1079 if (clipProxy && ctm.invert(&inverseClipMatrix)) {
Greg Danielc594e622019-10-15 14:01:49 -04001080 GrColorType srcColorType = SkColorTypeToGrColorType(clipImage->colorType());
Brian Salomon078e8fa2019-11-22 04:10:18 +00001081 cfp = GrSimpleTextureEffect::Make(std::move(clipProxy), srcColorType, inverseClipMatrix,
1082 sampler);
Greg Danielc594e622019-10-15 14:01:49 -04001083 if (srcColorType != GrColorType::kAlpha_8) {
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001084 cfp = GrFragmentProcessor::SwizzleOutput(std::move(cfp), GrSwizzle::AAAA());
1085 }
1086 }
1087
1088 if (cfp) {
1089 // If the grPaint already has coverage, this adds an additional stage that multiples
1090 // the image's alpha channel with the prior coverage.
1091 grPaint.addCoverageFragmentProcessor(std::move(cfp));
1092
1093 // Undo the offset that was needed for shader coord transforms to get the transform for
1094 // the actual drawn geometry.
1095 ctm.postTranslate(SkIntToScalar(left), SkIntToScalar(top));
1096 inverseClipMatrix.preTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1097 SkRect clipGeometry = SkRect::MakeWH(clipImage->width(), clipImage->height());
1098 if (!clipGeometry.contains(inverseClipMatrix.mapRect(dstRect))) {
1099 // Draw the clip geometry since it is smaller, using dstRect as an extra scissor
1100 SkClipStack clip(this->cs());
1101 clip.clipDevRect(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()),
1102 SkClipOp::kIntersect);
1103 SkMatrix local = SkMatrix::Concat(SkMatrix::MakeRectToRect(
1104 dstRect, srcRect, SkMatrix::kFill_ScaleToFit), ctm);
1105 fRenderTargetContext->fillRectWithLocalMatrix(GrClipStackClip(&clip),
1106 std::move(grPaint), GrAA(paint.isAntiAlias()), ctm, clipGeometry, local);
1107 return;
1108 }
1109 // Else fall through and draw the subset since that is contained in the clip geometry
1110 }
1111 // Else some issue configuring the coverage FP, so just draw without the clip mask image
1112 }
1113 // Draw directly in screen space, possibly with an extra coverage processor
1114 fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint),
1115 GrAA(paint.isAntiAlias()), SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116}
1117
Mike Reeda1361362017-03-07 09:37:29 -05001118void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001119 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001120 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001121 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001122 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1123 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1124 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1125 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1126 const SkRect* dst = &origDst;
1127 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001128 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001129 if (!src) {
1130 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132
bsalomonb1b01992015-11-18 10:56:08 -08001133 SkMatrix srcToDstMatrix;
1134 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1135 return;
1136 }
1137 SkRect tmpSrc, tmpDst;
1138 if (src != &bmpBounds) {
1139 if (!bmpBounds.contains(*src)) {
1140 tmpSrc = *src;
1141 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142 return; // nothing to draw
1143 }
bsalomonb1b01992015-11-18 10:56:08 -08001144 src = &tmpSrc;
1145 srcToDstMatrix.mapRect(&tmpDst, *src);
1146 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 }
1148 }
1149
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001150 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151
bsalomonb1b01992015-11-18 10:56:08 -08001152 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1153 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Chris Dalton6ce447a2019-06-23 18:07:38 -06001154 bool useCoverageAA = fRenderTargetContext->numSamples() <= 1 &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001155 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1156 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001157
Brian Salomon7c8460e2017-05-12 11:36:10 -04001158 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001159
1160 if (!skipTileCheck) {
1161 int tileSize;
1162 SkIRect clippedSrcRect;
1163
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001164 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001165 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001166 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001167 bitmap.width(), bitmap.height(), paint.getFilterQuality(), this->localToDevice(),
Chris Dalton309c6c02019-08-13 10:32:47 -06001168 srcToDstMatrix, fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001169
1170 int tileFilterPad;
1171
1172 if (doBicubic) {
1173 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001174 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001175 tileFilterPad = 0;
1176 } else {
1177 tileFilterPad = 1;
1178 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001179 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001180
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001181 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001182 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(),
1183 this->localToDevice(), srcToDstMatrix, sampleState, src,
1184 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1185 this->drawTiledBitmap(bitmap, this->localToDevice(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001186 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001187 return;
1188 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001189 }
Hal Canary144caf52016-11-07 17:57:18 -05001190 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001191 this->drawTextureProducer(&maker, src, dst, constraint, this->localToDevice(), paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192}
1193
robertphillips6451a0c2016-07-18 08:31:31 -07001194sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001195 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1196 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips9da87e02019-02-04 13:26:26 -05001197 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->priv().proxyProvider(),
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001198 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001199 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001200 return nullptr;
1201 }
1202
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001203 const SkIRect rect = SkIRect::MakeSize(proxy->dimensions());
robertphillips6451a0c2016-07-18 08:31:31 -07001204
Robert Phillipse14d3052017-02-15 13:18:21 -05001205 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1206 // the special image
1207 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1208 rect,
1209 bitmap.getGenerationID(),
1210 std::move(proxy),
Robert Phillips40b05c32019-09-20 12:40:55 -04001211 SkColorTypeToGrColorType(bitmap.colorType()),
Robert Phillipse14d3052017-02-15 13:18:21 -05001212 bitmap.refColorSpace(),
1213 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001214}
1215
reede51c3562016-07-19 14:33:20 -07001216sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001217 SkPixmap pm;
1218 if (image->isTextureBacked()) {
Robert Phillips6603a172019-03-05 12:35:44 -05001219 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(this->context());
robertphillips6451a0c2016-07-18 08:31:31 -07001220
Robert Phillips6de99042017-01-31 11:31:39 -05001221 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1222 SkIRect::MakeWH(image->width(), image->height()),
1223 image->uniqueID(),
1224 std::move(proxy),
Robert Phillips40b05c32019-09-20 12:40:55 -04001225 SkColorTypeToGrColorType(image->colorType()),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001226 image->refColorSpace(),
Robert Phillips6de99042017-01-31 11:31:39 -05001227 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001228 } else if (image->peekPixels(&pm)) {
1229 SkBitmap bm;
1230
1231 bm.installPixels(pm);
1232 return this->makeSpecial(bm);
1233 } else {
1234 return nullptr;
1235 }
1236}
1237
Michael Ludwigac352122019-08-28 21:03:05 +00001238sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial(const SkIRect& subset, bool forceCopy) {
Mike Reed148b7fd2018-12-18 17:38:18 -05001239 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
Greg Danielbe7fc462019-01-03 16:40:42 -05001240
1241 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1242 // since it would require us to make a copy of the underlying VkImage which we don't have access
1243 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1244 // command buffer.
1245 if (rtc->wrapsVkSecondaryCB()) {
Mike Reed148b7fd2018-12-18 17:38:18 -05001246 return nullptr;
1247 }
1248
Greg Danielbe7fc462019-01-03 16:40:42 -05001249 SkASSERT(rtc->asSurfaceProxy());
Mike Reed148b7fd2018-12-18 17:38:18 -05001250
Michael Ludwigac352122019-08-28 21:03:05 +00001251 SkIRect finalSubset = subset;
1252 sk_sp<GrTextureProxy> proxy(rtc->asTextureProxyRef());
1253 if (forceCopy || !proxy) {
1254 // When the device doesn't have a texture, or a copy is requested, we create a temporary
1255 // texture that matches the device contents
1256 proxy = GrSurfaceProxy::Copy(fContext.get(),
1257 rtc->asSurfaceProxy(),
Brian Salomon078e8fa2019-11-22 04:10:18 +00001258 rtc->colorInfo().colorType(),
Michael Ludwigac352122019-08-28 21:03:05 +00001259 GrMipMapped::kNo, // Don't auto generate mips
1260 subset,
1261 SkBackingFit::kApprox,
1262 SkBudgeted::kYes); // Always budgeted
1263 if (!proxy) {
1264 return nullptr;
1265 }
1266
1267 // Since this copied only the requested subset, the special image wrapping the proxy no
1268 // longer needs the original subset.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001269 finalSubset = SkIRect::MakeSize(proxy->dimensions());
Mike Reed148b7fd2018-12-18 17:38:18 -05001270 }
1271
Robert Phillips40b05c32019-09-20 12:40:55 -04001272 GrColorType ct = SkColorTypeToGrColorType(this->imageInfo().colorType());
1273
Mike Reed148b7fd2018-12-18 17:38:18 -05001274 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
Michael Ludwigac352122019-08-28 21:03:05 +00001275 finalSubset,
Mike Reed148b7fd2018-12-18 17:38:18 -05001276 kNeedNewImageUniqueID_SpecialImage,
Michael Ludwigac352122019-08-28 21:03:05 +00001277 std::move(proxy),
Robert Phillips40b05c32019-09-20 12:40:55 -04001278 ct,
Mike Reed148b7fd2018-12-18 17:38:18 -05001279 this->imageInfo().refColorSpace(),
1280 &this->surfaceProps());
1281}
1282
Mike Reeda1361362017-03-07 09:37:29 -05001283void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001284 int left, int top, const SkPaint& paint) {
1285 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001286
joshualittce894002016-01-11 13:29:31 -08001287 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001289 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001290
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001292 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
Michael Ludwigac352122019-08-28 21:03:05 +00001293 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial(SkIRect::MakeWH(dev->width(), dev->height())));
robertphillips1b5f9682016-07-15 08:01:12 -07001294 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295 return;
1296 }
1297
Mike Reed2179b782018-02-07 11:59:57 -05001298 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299}
1300
Brian Salomon34169692017-08-28 15:32:01 -04001301void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1302 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001303 ASSERT_SINGLE_OWNER
Michael Ludwig1433cfd2019-02-27 17:12:30 -05001304 GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -05001305 this->drawImageQuad(image, src, &dst, nullptr, GrAA(paint.isAntiAlias()), aaFlags, nullptr,
1306 paint, constraint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001307}
1308
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001309// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1310static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1311 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1312 return GrSamplerState::Filter::kNearest;
1313 }
1314
1315 return GrSamplerState::Filter::kBilerp;
1316}
1317
Mike Reeda1361362017-03-07 09:37:29 -05001318void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001319 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001320 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001321 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001322 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips6603a172019-03-05 12:35:44 -05001323 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
1324 &pinnedUniqueID)) {
Brian Salomond6287472019-06-24 15:50:07 -04001325 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Brian Salomon1a217eb2019-10-24 10:50:36 -04001326 image->imageInfo().colorInfo(), pinnedUniqueID);
Brian Salomon2a943df2018-05-04 13:43:19 -04001327 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001328 } else {
1329 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001330 if (image->isLazyGenerated()) {
1331 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001332 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001333 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001334 GrBitmapTextureMaker maker(fContext.get(), bm);
1335 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001336 }
1337 }
1338}
1339
Mike Reeda1361362017-03-07 09:37:29 -05001340void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001341 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001342 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001343 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001344 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001345 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001346}
1347
Mike Reeda1361362017-03-07 09:37:29 -05001348void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001349 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1350 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001351 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001352 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001353
Brian Salomon2a943df2018-05-04 13:43:19 -04001354 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1355 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1356 }
msarett10e3d9b2016-08-18 15:46:03 -07001357 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001358 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorInfo(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001359 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001360 return;
1361 }
1362
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001363 auto dstColorSpace = fRenderTargetContext->colorInfo().colorSpace();
Brian Salomon2a943df2018-05-04 13:43:19 -04001364 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Michael Ludwigddeed372019-02-20 16:50:10 -05001365 auto proxy = producer->refTextureProxyForParams(&filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001366 if (!proxy) {
1367 return;
1368 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001369 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1370 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001371
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001372 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->localToDevice(),
Greg Danielc594e622019-10-15 14:01:49 -04001373 std::move(proxy), producer->colorType(), std::move(csxf),
1374 filter, std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001375}
1376
Mike Reeda1361362017-03-07 09:37:29 -05001377void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001378 const SkCanvas::Lattice& lattice, const SkRect& dst,
1379 const SkPaint& paint) {
1380 ASSERT_SINGLE_OWNER
1381 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001382 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips6603a172019-03-05 12:35:44 -05001383 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
1384 &pinnedUniqueID)) {
Brian Salomond6287472019-06-24 15:50:07 -04001385 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Brian Salomon1a217eb2019-10-24 10:50:36 -04001386 image->imageInfo().colorInfo(), pinnedUniqueID);
Brian Salomon2a943df2018-05-04 13:43:19 -04001387 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001388 } else {
1389 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001390 if (image->isLazyGenerated()) {
1391 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001392 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001393 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001394 GrBitmapTextureMaker maker(fContext.get(), bm);
1395 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001396 }
1397 }
1398}
1399
Mike Reeda1361362017-03-07 09:37:29 -05001400void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001401 const SkCanvas::Lattice& lattice, const SkRect& dst,
1402 const SkPaint& paint) {
1403 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001404 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001405 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001406 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001407}
1408
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001409static bool init_vertices_paint(GrContext* context, const GrColorInfo& colorInfo,
Brian Salomonf3569f02017-10-24 12:52:33 -04001410 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001411 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001412 if (hasTexs && skPaint.getShader()) {
1413 if (hasColors) {
1414 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001415 return SkPaintToGrPaintWithXfermode(context, colorInfo, skPaint, matrix, bmode,
Brian Salomonf3569f02017-10-24 12:52:33 -04001416 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001417 } else {
1418 // We have a shader, but no colors to blend it against.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001419 return SkPaintToGrPaint(context, colorInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001420 }
1421 } else {
1422 if (hasColors) {
1423 // We have colors, but either have no shader or no texture coords (which implies that
1424 // we should ignore the shader).
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001425 return SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001426 } else {
1427 // No colors and no shaders. Just draw with the paint color.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001428 return SkPaintToGrPaintNoShader(context, colorInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001429 }
1430 }
1431}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432
Mike Reed887cdf12017-04-03 11:11:09 -04001433void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001434 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001435 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001436 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001437 const uint16_t indices[], int indexCount,
1438 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001439 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001440 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001441
Mike Reed2f6b5a42017-03-19 15:04:17 -04001442 SkPaint copy(paint);
1443 copy.setStyle(SkPaint::kStroke_Style);
1444 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001445
Mike Reed2f6b5a42017-03-19 15:04:17 -04001446 GrPaint grPaint;
1447 // we ignore the shader since we have no texture coordinates.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001448 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorInfo(), copy,
Brian Salomonf3569f02017-10-24 12:52:33 -04001449 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001450 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451 }
1452
Mike Reed2f6b5a42017-03-19 15:04:17 -04001453 int triangleCount = 0;
1454 int n = (nullptr == indices) ? vertexCount : indexCount;
1455 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001456 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001457 triangleCount = n / 3;
1458 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001459 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001460 triangleCount = n - 2;
1461 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001462 case SkVertices::kTriangleFan_VertexMode:
1463 SK_ABORT("Unexpected triangle fan.");
1464 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001465 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466
Mike Reed2f6b5a42017-03-19 15:04:17 -04001467 VertState state(vertexCount, indices, indexCount);
1468 VertState::Proc vertProc = state.chooseProc(vmode);
1469
1470 //number of indices for lines per triangle with kLines
1471 indexCount = triangleCount * 6;
1472
Brian Osmanae0c50c2017-05-25 16:56:34 -04001473 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1474 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1475 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1476
1477 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001478 int i = 0;
1479 while (vertProc(&state)) {
1480 lineIndices[i] = state.f0;
1481 lineIndices[i + 1] = state.f1;
1482 lineIndices[i + 2] = state.f1;
1483 lineIndices[i + 3] = state.f2;
1484 lineIndices[i + 4] = state.f2;
1485 lineIndices[i + 5] = state.f0;
1486 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001487 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001488
Chris Dalton3809bab2017-06-13 10:55:06 -06001489 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001490 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001491 std::move(grPaint),
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001492 this->localToDevice(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001493 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001494 bones,
1495 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001496 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497}
1498
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001499void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1500 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001501 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001502 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001503
1504 SkASSERT(vertices);
1505 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001506 bool hasColors = vertices->hasColors();
1507 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001508 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001509 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001510 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001511 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1512 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001513 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001514 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001515 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorInfo(), paint,
1516 this->localToDevice(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001517 return;
1518 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001519 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->localToDevice(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001520 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1521 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001522}
1523
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524///////////////////////////////////////////////////////////////////////////////
1525
Jim Van Verth3af1af92017-05-18 15:06:54 -04001526void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1527
1528 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001529 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1530
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001531 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->localToDevice(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001532 // failed to find an accelerated case
1533 this->INHERITED::drawShadow(path, rec);
1534 }
1535}
1536
1537///////////////////////////////////////////////////////////////////////////////
1538
Brian Osman4d92b892019-03-24 00:53:23 +00001539void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
1540 const SkRect texRect[], const SkColor colors[], int count,
1541 SkBlendMode mode, const SkPaint& paint) {
1542 ASSERT_SINGLE_OWNER
Brian Osmanddba4e62019-03-25 09:52:17 -04001543 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawAtlas", fContext.get());
Brian Osman4d92b892019-03-24 00:53:23 +00001544
1545 SkPaint p(paint);
1546 p.setShader(atlas->makeShader());
1547
1548 GrPaint grPaint;
1549 if (colors) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001550 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorInfo(), p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001551 this->localToDevice(), (SkBlendMode)mode, &grPaint)) {
Brian Osman4d92b892019-03-24 00:53:23 +00001552 return;
1553 }
1554 } else {
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001555 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorInfo(), p,
1556 this->localToDevice(), &grPaint)) {
Brian Osman4d92b892019-03-24 00:53:23 +00001557 return;
1558 }
1559 }
1560
1561 fRenderTargetContext->drawAtlas(
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001562 this->clip(), std::move(grPaint), this->localToDevice(), count, xform, texRect, colors);
Brian Osman4d92b892019-03-24 00:53:23 +00001563}
1564
1565///////////////////////////////////////////////////////////////////////////////
1566
Herb Derbyb935cf82018-07-26 16:54:18 -04001567void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001568 ASSERT_SINGLE_OWNER
1569 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
Herb Derbyb983e6b2018-07-13 13:26:29 -04001570
Jim Van Verth87a30112018-09-24 16:13:58 -04001571 // Check for valid input
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001572 if (!this->localToDevice().isFinite() || !glyphRunList.allFontsFinite()) {
Jim Van Verth87a30112018-09-24 16:13:58 -04001573 return;
1574 }
1575
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001576 fRenderTargetContext->drawGlyphRunList(this->clip(), this->localToDevice(), glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001577}
1578
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579///////////////////////////////////////////////////////////////////////////////
1580
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001581void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
Robert Phillips4217ea72019-01-30 13:08:28 -05001582 GrBackendApi api = this->context()->backend();
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001583 if (GrBackendApi::kVulkan == api) {
1584 const SkMatrix& ctm = canvas->getTotalMatrix();
1585 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1586 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -05001587 drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(),
1588 this->imageInfo());
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001589 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001590 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1591 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001592 }
1593 }
1594 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1595}
1596
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001597
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001598///////////////////////////////////////////////////////////////////////////////
1599
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600void SkGpuDevice::flush() {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001601 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo());
Greg Daniela5cb7812017-06-16 09:45:32 -04001602}
1603
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001604GrSemaphoresSubmitted SkGpuDevice::flush(SkSurface::BackendSurfaceAccess access,
1605 const GrFlushInfo& info) {
joshualittce894002016-01-11 13:29:31 -08001606 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001607
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001608 return fRenderTargetContext->flush(access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001609}
1610
Greg Danielc64ee462017-06-15 16:59:49 -04001611bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001612 ASSERT_SINGLE_OWNER
1613
Greg Danielc64ee462017-06-15 16:59:49 -04001614 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615}
1616
1617///////////////////////////////////////////////////////////////////////////////
1618
reed76033be2015-03-14 10:54:31 -07001619SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001620 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001621
robertphillipsca6eafc2016-05-17 09:57:46 -07001622 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623
robertphillipsca6eafc2016-05-17 09:57:46 -07001624 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001625 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001626 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1627 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001628
Brian Osmand7a59752019-09-20 11:16:58 -04001629 SkASSERT(cinfo.fInfo.colorType() != kRGBA_1010102_SkColorType);
Brian Osman10fc6fd2018-03-02 11:01:10 -05001630
Brian Osmanc2486592019-09-20 13:32:51 -04001631 auto rtc = fContext->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomond6287472019-06-24 15:50:07 -04001632 fit,
1633 cinfo.fInfo.width(),
1634 cinfo.fInfo.height(),
Brian Osmand7a59752019-09-20 11:16:58 -04001635 SkColorTypeToGrColorType(cinfo.fInfo.colorType()),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001636 fRenderTargetContext->colorInfo().refColorSpace(),
Brian Salomond6287472019-06-24 15:50:07 -04001637 fRenderTargetContext->numSamples(),
1638 GrMipMapped::kNo,
1639 kBottomLeft_GrSurfaceOrigin,
1640 &props,
1641 SkBudgeted::kYes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001642 fRenderTargetContext->asSurfaceProxy()->isProtected() ? GrProtected::kYes
Brian Salomonbf6b9792019-08-21 09:38:10 -04001643 : GrProtected::kNo);
Brian Osman11052242016-10-27 14:47:55 -04001644 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001645 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001647
1648 // Skia's convention is to only clear a device if it is non-opaque.
1649 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1650
Brian Salomonbf6b9792019-08-21 09:38:10 -04001651 return SkGpuDevice::Make(fContext.get(), std::move(rtc), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652}
1653
reede8f30622016-03-23 18:59:25 -07001654sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001655 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001656 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001657 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001658 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001659 fRenderTargetContext->numSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001660 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001661}
1662
senorblanco900c3672016-04-27 11:31:23 -07001663SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001664 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001665 // We always return a transient cache, so it is freed after each
1666 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001667 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001668}
reedf037e0b2014-10-30 11:34:15 -07001669