blob: a665517d4fbd81e801b34d570bb84546e5e61970 [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
8#include "SkGpuDevice.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Brian Salomon2a943df2018-05-04 13:43:19 -040010#include "../private/SkShadowFlags.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050011#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070012#include "GrBlurUtils.h"
Brian Osman1cb41712017-10-19 12:54:52 -040013#include "GrColorSpaceXform.h"
kkinnunenabcfab42015-02-22 22:53:44 -080014#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080016#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
18#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070019#include "GrStyle.h"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040020#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050021#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050022#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070023#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080024#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070025#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000026#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070027#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070029#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050030#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050031#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070032#include "SkLatticeIter.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040033#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050034#include "SkMaskFilterBase.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000036#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070037#include "SkPictureData.h"
Mike Reed242135a2018-02-22 13:41:39 -050038#include "SkRRectPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050039#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050040#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070042#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000044#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000046#include "SkTLazy.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040047#include "SkTo.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000048#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000049#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050050#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050051#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080052#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080053#include "effects/GrSimpleTextureEffect.h"
54#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080055#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056
joshualittce894002016-01-11 13:29:31 -080057#define ASSERT_SINGLE_OWNER \
Robert Phillips0c4b7b12018-03-06 08:20:37 -050058SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->contextPriv().debugSingleOwner());)
joshualittce894002016-01-11 13:29:31 -080059
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061///////////////////////////////////////////////////////////////////////////////
62
bsalomon74f681d2015-06-23 14:38:48 -070063/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
64 should fail. */
65bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
66 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
67 *flags = 0;
68 if (info) {
69 switch (info->alphaType()) {
70 case kPremul_SkAlphaType:
71 break;
72 case kOpaque_SkAlphaType:
73 *flags |= SkGpuDevice::kIsOpaque_Flag;
74 break;
75 default: // If it is unpremul or unknown don't try to render
76 return false;
77 }
78 }
79 if (kClear_InitContents == init) {
80 *flags |= kNeedClear_Flag;
81 }
82 return true;
83}
84
Robert Phillips9fab7e92016-11-17 12:45:04 -050085sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
86 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070087 int width, int height,
88 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040089 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070090 return nullptr;
91 }
92 unsigned flags;
93 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
94 return nullptr;
95 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050096 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
97 width, height, 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;
105 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700106 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700107 }
108
Brian Osman11052242016-10-27 14:47:55 -0400109 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
110 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400111 origin, props,
112 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400113 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700114 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700115 }
116
Robert Phillips9fab7e92016-11-17 12:45:04 -0500117 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700118 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700119}
120
Brian Osman11052242016-10-27 14:47:55 -0400121static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700122 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400123 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700124 colorType = kUnknown_SkColorType;
125 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400126 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
127 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700128}
129
Robert Phillips9fab7e92016-11-17 12:45:04 -0500130SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
131 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400132 : INHERITED(make_info(renderTargetContext.get(), width, height,
133 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500134 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400135 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700136{
robertphillips1f3923e2016-07-21 07:17:54 -0700137 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700138 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
bsalomone63ffef2016-02-05 07:17:34 -0800140 if (flags & kNeedClear_Flag) {
141 this->clearAll();
142 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143}
144
Brian Osman11052242016-10-27 14:47:55 -0400145sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
146 GrContext* context,
147 SkBudgeted budgeted,
148 const SkImageInfo& origInfo,
149 int sampleCount,
150 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400151 const SkSurfaceProps* surfaceProps,
152 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000153 if (kUnknown_SkColorType == origInfo.colorType() ||
154 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000156 }
157
bsalomonafe30052015-01-16 07:32:33 -0800158 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800160 }
161
Brian Osman2b23c4b2018-06-01 12:25:08 -0400162 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500163 if (kUnknown_GrPixelConfig == config) {
164 return nullptr;
165 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400166 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
167 // they need to be exact.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500168 return context->contextPriv().makeDeferredRenderTargetContext(
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400169 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700170 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500171 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400172 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800173}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000174
Mike Reeda1361362017-03-07 09:37:29 -0500175sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700176 int left, int top,
177 SkIPoint* offset,
178 const SkImageFilter* filter) {
179 SkASSERT(srcImg->isTextureBacked());
180 SkASSERT(filter);
181
Mike Reeda1361362017-03-07 09:37:29 -0500182 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700183 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500184 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500185 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Salomonf3569f02017-10-24 12:52:33 -0400186 SkImageFilter::OutputProperties outputProperties(
187 fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700188 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700189
190 return filter->filterImage(srcImg, ctx, offset);
191}
192
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000193///////////////////////////////////////////////////////////////////////////////
194
Mike Reed353196f2017-07-21 11:01:18 -0400195bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800196 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000197
Mike Reed353196f2017-07-21 11:01:18 -0400198 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500199 return false;
200 }
201
Mike Reed353196f2017-07-21 11:01:18 -0400202 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500203 if (!rec.trim(this->width(), this->height())) {
204 return false;
205 }
206
207 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000208}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000209
Mike Reed353196f2017-07-21 11:01:18 -0400210bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800211 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700212
Mike Reed353196f2017-07-21 11:01:18 -0400213 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500214 return false;
215 }
216
Mike Reed353196f2017-07-21 11:01:18 -0400217 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500218 if (!rec.trim(this->width(), this->height())) {
219 return false;
220 }
221
222 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000223}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000224
reed41e010c2015-06-09 12:16:53 -0700225bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800226 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700227 return false;
228}
229
Brian Osman11052242016-10-27 14:47:55 -0400230GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700231 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400232 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700233}
234
reed8eddfb52014-12-04 07:50:14 -0800235void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800236 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500237 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500238
reed8eddfb52014-12-04 07:50:14 -0800239 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Chris Dalton344e9032017-12-11 15:42:09 -0700240 fRenderTargetContext->clear(&rect, 0x0, GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800241}
242
Brian Osman11052242016-10-27 14:47:55 -0400243void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800244 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800245
Brian Osman693a5402016-10-27 15:13:22 -0400246 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800247
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400248 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
249 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400250 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400251 this->context(),
252 budgeted,
253 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400254 fRenderTargetContext->numColorSamples(),
255 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400256 &this->surfaceProps(),
257 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400258 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800259 return;
260 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400261 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800262
263 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400264 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800265 return;
266 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500267 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800268 }
269
Brian Osman693a5402016-10-27 15:13:22 -0400270 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800271}
272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273///////////////////////////////////////////////////////////////////////////////
274
Mike Reeda1361362017-03-07 09:37:29 -0500275void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800276 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500277 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278
279 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400280 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
281 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700282 return;
283 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284
Brian Salomon0166cfd2017-03-13 17:58:25 -0400285 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286}
287
Brian Salomon1459ebd2017-12-19 16:12:44 -0500288static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
289 switch (mode) {
290 case SkCanvas::kPoints_PointMode:
291 return GrPrimitiveType::kPoints;
292 case SkCanvas::kLines_PointMode:
293 return GrPrimitiveType::kLines;
294 case SkCanvas::kPolygon_PointMode:
295 return GrPrimitiveType::kLineStrip;
296 }
297 SK_ABORT("Unexpected mode");
298 return GrPrimitiveType::kPoints;
299}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300
Mike Reeda1361362017-03-07 09:37:29 -0500301void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800303 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500304 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305 SkScalar width = paint.getStrokeWidth();
306 if (width < 0) {
307 return;
308 }
309
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000310 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700311 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700312 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400313 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
314 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700315 return;
316 }
egdaniele61c4112014-06-12 10:24:21 -0700317 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700318 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700319 path.moveTo(pts[0]);
320 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700321 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
322 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700323 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000324 }
325
bsalomon6ade6dd2016-09-12 12:07:17 -0700326 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500327 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700328 SkScalarNearlyEqual(scales[0], 1.f) &&
329 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700330 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400332 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500333 SkRasterClip rc(this->devClipBounds());
334 SkDraw draw;
335 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
336 draw.fMatrix = &this->ctm();
337 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500338 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000339 return;
340 }
341
Brian Salomon1459ebd2017-12-19 16:12:44 -0500342 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700343
Mike Reeda1361362017-03-07 09:37:29 -0500344 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700345#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
346 // This offsetting in device space matches the expectations of the Android framework for non-AA
347 // points and lines.
348 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600349 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700350 tempMatrix = *viewMatrix;
351 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
352 tempMatrix.postTranslate(kOffset, kOffset);
353 viewMatrix = &tempMatrix;
354 }
355#endif
356
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400358 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
359 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700360 return;
361 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
Brian Osmanae0c50c2017-05-25 16:56:34 -0400363 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
364 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
365 nullptr);
366
367 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
368 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369}
370
371///////////////////////////////////////////////////////////////////////////////
372
Mike Reeda1361362017-03-07 09:37:29 -0500373void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800374 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500375 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
bsalomona7d85ba2016-07-06 11:54:59 -0700376 // A couple reasons we might need to call drawPath.
377 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700379 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500381 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400382 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500383 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384 return;
385 }
386
387 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400388 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
389 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700390 return;
391 }
Mike Klein744fb732014-06-23 15:13:26 -0400392
bsalomon6663acf2016-05-10 09:14:17 -0700393 GrStyle style(paint);
Chris Dalton3b51df12017-11-27 14:33:06 -0700394 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
395 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000396}
397
398///////////////////////////////////////////////////////////////////////////////
399
Mike Reeda1361362017-03-07 09:37:29 -0500400void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800401 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500402 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000403 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400404 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
405 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700406 return;
407 }
Mike Klein744fb732014-06-23 15:13:26 -0400408
Mike Reed80747ef2018-01-23 15:29:32 -0500409 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000410 if (mf) {
411 if (mf->hasFragmentProcessor()) {
412 mf = nullptr; // already handled in SkPaintToGrPaint
413 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400414 }
415
bsalomon6663acf2016-05-10 09:14:17 -0700416 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400417 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000418 // try to hit the fast path for drawing filtered round rects
419
420 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500421 if (rrect.transform(this->ctm(), &devRRect)) {
Mike Reed242135a2018-02-22 13:41:39 -0500422 if (SkRRectPriv::AllCornersCircular(devRRect)) {
Brian Osmane191ce62017-09-14 15:43:50 -0400423 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(), this->ctm(), nullptr)) {
Robert Phillips26c90e02017-03-14 14:39:29 -0400424 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400425 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500426 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000427 return;
428 }
429 }
430
431 }
432 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000433 }
434
Robert Phillipsa29a9562016-10-20 09:40:55 -0400435 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800436 // The only mask filter the native rrect drawing code could've handle was taken
437 // care of above.
438 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000439 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700440 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700441 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500442 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400443 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500444 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 return;
446 }
Mike Klein744fb732014-06-23 15:13:26 -0400447
bsalomon6663acf2016-05-10 09:14:17 -0700448 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800449
Chris Dalton3b51df12017-11-27 14:33:06 -0700450 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
451 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000452}
453
robertphillipsd7706102016-02-25 09:28:08 -0800454
Mike Reeda1361362017-03-07 09:37:29 -0500455void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800456 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800457 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500458 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800459 if (outer.isEmpty()) {
460 return;
461 }
462
463 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500464 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800465 }
466
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000467 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000468
robertphillips0e7029e2015-11-30 05:45:06 -0800469 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800470 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400471 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
472 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700473 return;
474 }
robertphillips00095892016-02-29 13:50:40 -0800475
Brian Salomon0166cfd2017-03-13 17:58:25 -0400476 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700477 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800478 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000479 }
480
481 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700482 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000483 path.addRRect(outer);
484 path.addRRect(inner);
485 path.setFillType(SkPath::kEvenOdd_FillType);
486
Brian Salomon0166cfd2017-03-13 17:58:25 -0400487 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
488 path, paint, this->ctm(), nullptr, this->devClipBounds(),
489 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000490}
491
492
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000493/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494
Mike Reeda1361362017-03-07 09:37:29 -0500495void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700496 if (paint.getMaskFilter()) {
497 SkPath path;
498 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500499 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700500 }
501
502 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400503 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
504 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700505 return;
506 }
507
Chris Dalton3b51df12017-11-27 14:33:06 -0700508 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
509 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700510}
511
Mike Reeda1361362017-03-07 09:37:29 -0500512void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800513 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500514 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800515
robertphillips514450c2015-11-24 05:36:02 -0800516 if (paint.getMaskFilter()) {
517 // The RRect path can handle special case blurring
518 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500519 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520 }
521
522 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400523 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
524 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700525 return;
526 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527
Chris Dalton3b51df12017-11-27 14:33:06 -0700528 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
529 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000530}
531
Mike Reeda1361362017-03-07 09:37:29 -0500532void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700533 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
534 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500535 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700536 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500537 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700538 return;
539 }
540 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400541 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
542 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700543 return;
544 }
545
Chris Dalton3b51df12017-11-27 14:33:06 -0700546 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500547 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500548 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700549}
550
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552
553///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700554void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700555 const SkPaint& origPaint) {
556 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500557 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400558 // Adding support for round capping would require a
559 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700560 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
561 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
562 SkASSERT(!origPaint.getPathEffect());
563 SkASSERT(!origPaint.getMaskFilter());
564
565 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
566 SkASSERT(halfWidth > 0);
567
568 SkVector v = points[1] - points[0];
569
570 SkScalar length = SkPoint::Normalize(&v);
571 if (!length) {
572 v.fX = 1.0f;
573 v.fY = 0.0f;
574 }
575
576 SkPaint newPaint(origPaint);
577 newPaint.setStyle(SkPaint::kFill_Style);
578
579 SkScalar xtraLength = 0.0f;
580 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
581 xtraLength = halfWidth;
582 }
583
584 SkPoint mid = points[0] + points[1];
585 mid.scale(0.5f);
586
587 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
588 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
589 SkMatrix m;
590 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
591
592 SkMatrix local = m;
593
Mike Reeda1361362017-03-07 09:37:29 -0500594 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700595
596 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400597 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
598 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700599 return;
600 }
601
Brian Salomon82f44312017-01-11 13:42:54 -0500602 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700603 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700604}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000605
Mike Reeda1361362017-03-07 09:37:29 -0500606void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607 const SkPaint& paint, const SkMatrix* prePathMatrix,
608 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800609 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800610 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700611 SkPoint points[2];
612 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
613 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500614 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700615 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500616 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700617 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500618 // Round capping support is currently disabled b.c. it would require a RRect
619 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500620 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700621 return;
622 }
623 }
robertphillipsff55b492015-11-24 07:56:59 -0800624 }
625
Hal Canary144caf52016-11-07 17:57:18 -0500626 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Brian Salomona3cf3652018-01-03 15:11:00 -0500627 if (!prePathMatrix && !paint.getMaskFilter()) {
628 GrPaint grPaint;
629 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
630 this->ctm(), &grPaint)) {
631 return;
632 }
633 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
634 this->ctm(), origSrcPath, GrStyle(paint));
635 return;
636 }
Brian Salomon0166cfd2017-03-13 17:58:25 -0400637 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
638 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500639 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640}
641
642static const int kBmpSmallTileSize = 1 << 10;
643
644static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
645 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
646 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
647 return tilesX * tilesY;
648}
649
reed85d91782015-09-10 14:33:38 -0700650static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651 if (maxTileSize <= kBmpSmallTileSize) {
652 return maxTileSize;
653 }
654
655 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
656 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
657
658 maxTileTotalTileSize *= maxTileSize * maxTileSize;
659 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
660
661 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
662 return kBmpSmallTileSize;
663 } else {
664 return maxTileSize;
665 }
666}
667
668// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
669// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700670static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800671 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800672 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700673 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700674 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 const SkRect* srcRectPtr,
676 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700677 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700678 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
679 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000680 clippedSrcIRect->setEmpty();
681 return;
682 }
683 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
684 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700685 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686 if (!clippedSrcRect.intersect(*srcRectPtr)) {
687 clippedSrcIRect->setEmpty();
688 return;
689 }
690 }
691 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700692 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 if (!clippedSrcIRect->intersect(bmpBounds)) {
694 clippedSrcIRect->setEmpty();
695 }
696}
697
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400698bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
699 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700700 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700701 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400702 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700703 const SkRect* srcRectPtr,
704 int maxTileSize,
705 int* tileSize,
706 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800707 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700708 // if it's larger than the max tile size, then we have no choice but tiling.
709 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400710 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400711 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
712 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700713 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
714 return true;
715 }
716
bsalomon1a1d0b82015-10-16 07:49:42 -0700717 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700718 const size_t area = imageRect.width() * imageRect.height();
719 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
720 return false;
721 }
722
reed85d91782015-09-10 14:33:38 -0700723 // At this point we know we could do the draw by uploading the entire bitmap
724 // as a texture. However, if the texture would be large compared to the
725 // cache size and we don't require most of it for this draw then tile to
726 // reduce the amount of upload and cache spill.
727
728 // assumption here is that sw bitmap size is a good proxy for its size as
729 // a texture
730 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
731 size_t cacheSize;
732 fContext->getResourceCacheLimits(nullptr, &cacheSize);
733 if (bmpSize < cacheSize / 2) {
734 return false;
735 }
736
bsalomon1a1d0b82015-10-16 07:49:42 -0700737 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
738 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400739 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
740 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400741 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700742 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
743 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500744 kBmpSmallTileSize * kBmpSmallTileSize *
745 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700746
Brian Osmand05cdc32017-02-06 13:24:47 -0500747 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700748}
749
reed85d91782015-09-10 14:33:38 -0700750bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
751 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700752 const SkMatrix& viewMatrix,
753 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800754 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400755 // If image is explicitly texture backed then we shouldn't get here.
756 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400758 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700759 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500760 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
761 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
762 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700763
764 int tileFilterPad;
765 if (doBicubic) {
766 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400767 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700768 tileFilterPad = 0;
769 } else {
770 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400772 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400774 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775
reed85d91782015-09-10 14:33:38 -0700776 // these are output, which we safely ignore, as we just want to know the predicate
777 int outTileSize;
778 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779
bsalomone553b642016-08-17 09:02:09 -0700780 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400781 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700782 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783}
784
Mike Reeda1361362017-03-07 09:37:29 -0500785void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400786 SkScalar x,
787 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400789 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800790 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800791 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500792 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700793
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400794 int maxTileSize = this->caps()->maxTileSize();
bsalomonb1b01992015-11-18 10:56:08 -0800795
796 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
797 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400798 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
799 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800800 bitmap.height() <= maxTileSize;
801
802 bool skipTileCheck = drawAA || paint.getMaskFilter();
803
804 if (!skipTileCheck) {
805 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
806 int tileSize;
807 SkIRect clippedSrcRect;
808
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400809 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800810 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400811 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500812 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
813 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800814
815 int tileFilterPad;
816
817 if (doBicubic) {
818 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400819 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800820 tileFilterPad = 0;
821 } else {
822 tileFilterPad = 1;
823 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400824 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800825
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400826 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700827 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400828 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700829 &tileSize, &clippedSrcRect)) {
830 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400831 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
832 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800833 return;
834 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000835 }
Hal Canary144caf52016-11-07 17:57:18 -0500836 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0cdfc322018-05-14 16:17:43 -0400837 this->drawTextureMaker(&maker, bitmap.width(), bitmap.height(), nullptr, nullptr,
838 SkCanvas::kStrict_SrcRectConstraint, viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839}
840
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000841// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
843// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000844static inline void clamped_outset_with_offset(SkIRect* iRect,
845 int outset,
846 SkPoint* offset,
847 const SkIRect& clamp) {
848 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000850 int leftClampDelta = clamp.fLeft - iRect->fLeft;
851 if (leftClampDelta > 0) {
852 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853 iRect->fLeft = clamp.fLeft;
854 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000855 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000857
858 int topClampDelta = clamp.fTop - iRect->fTop;
859 if (topClampDelta > 0) {
860 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 iRect->fTop = clamp.fTop;
862 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000863 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000864 }
865
866 if (iRect->fRight > clamp.fRight) {
867 iRect->fRight = clamp.fRight;
868 }
869 if (iRect->fBottom > clamp.fBottom) {
870 iRect->fBottom = clamp.fBottom;
871 }
872}
873
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874// Break 'bitmap' into several tiles to draw it since it has already
875// been determined to be too large to fit in VRAM
876void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800877 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700878 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000879 const SkRect& srcRect,
880 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400881 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800882 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700883 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000884 int tileSize,
885 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800886 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800887
ericrk983294f2016-04-18 09:14:00 -0700888 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800889 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700890 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800891
bsalomonc55271f2015-11-09 11:55:57 -0800892 const SkPaint* paint = &origPaint;
893 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400894 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800895 // Drop antialiasing to avoid seams at tile boundaries.
896 tempPaint = origPaint;
897 tempPaint.setAntiAlias(false);
898 paint = &tempPaint;
899 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000900 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
901
902 int nx = bitmap.width() / tileSize;
903 int ny = bitmap.height() / tileSize;
904 for (int x = 0; x <= nx; x++) {
905 for (int y = 0; y <= ny; y++) {
906 SkRect tileR;
907 tileR.set(SkIntToScalar(x * tileSize),
908 SkIntToScalar(y * tileSize),
909 SkIntToScalar((x + 1) * tileSize),
910 SkIntToScalar((y + 1) * tileSize));
911
912 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
913 continue;
914 }
915
916 if (!tileR.intersect(srcRect)) {
917 continue;
918 }
919
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 SkIRect iTileR;
921 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700922 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
923 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500924 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700925 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400926 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 SkIRect iClampRect;
928
reeda5517e22015-07-14 10:54:12 -0700929 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 // In bleed mode we want to always expand the tile on all edges
931 // but stay within the bitmap bounds
932 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
933 } else {
934 // In texture-domain/clamp mode we only want to expand the
935 // tile on edges interior to "srcRect" (i.e., we want to
936 // not bleed across the original clamped edges)
937 srcRect.roundOut(&iClampRect);
938 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000939 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
940 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 }
942
bsalomone553b642016-08-17 09:02:09 -0700943 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 if (bitmap.extractSubset(&tmpB, iTileR)) {
945 // now offset it to make it "local" to our tmp bitmap
946 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800947 // de-optimized this determination
948 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700949 this->drawBitmapTile(tmpB,
950 viewMatrix,
951 rectToDraw,
952 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500953 params,
bsalomone553b642016-08-17 09:02:09 -0700954 *paint,
955 constraint,
956 bicubic,
957 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 }
959 }
960 }
961}
962
bsalomone553b642016-08-17 09:02:09 -0700963void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
964 const SkMatrix& viewMatrix,
965 const SkRect& dstRect,
966 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400967 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700968 const SkPaint& paint,
969 SkCanvas::SrcRectConstraint constraint,
970 bool bicubic,
971 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800972 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400973 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
974 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700975 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400976 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
977 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400978 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979
Brian Salomon2a943df2018-05-04 13:43:19 -0400980 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400981 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400982 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400983 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 return;
985 }
bsalomone553b642016-08-17 09:02:09 -0700986
bsalomone553b642016-08-17 09:02:09 -0700987 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400988 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
989 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700990
991 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
992 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400993 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700994
reeda5517e22015-07-14 10:54:12 -0700995 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700997 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500999 domain.fLeft = srcRect.fLeft + 0.5f;
1000 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001002 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 }
1004 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001005 domain.fTop = srcRect.fTop + 0.5f;
1006 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001007 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001008 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001010 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001011 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001012 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001013 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001014 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001015 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001016 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001017 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1018 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001019 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001021 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022 }
1023
Brian Osman653f34d2018-06-14 11:44:02 -04001024 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001025 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001026 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001027 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1028 viewMatrix, std::move(fp),
1029 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001030 return;
1031 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001033 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001034 GrAA aa = GrAA(paint.isAntiAlias() &&
1035 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001036 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037}
1038
Mike Reeda1361362017-03-07 09:37:29 -05001039void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001041 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001042 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001043
Khushalc421ca12018-06-26 14:38:34 -07001044 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 return;
1046 }
1047
Robert Phillipse14d3052017-02-15 13:18:21 -05001048 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1049 if (!srcImg) {
1050 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001051 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052
Florin Malita53f77bd2017-04-28 13:48:37 -04001053 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001054}
1055
1056
Florin Malita53f77bd2017-04-28 13:48:37 -04001057void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001058 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001059 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001060 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001061
Florin Malita53f77bd2017-04-28 13:48:37 -04001062 // TODO: clipImage support.
1063
robertphillips970587b2016-07-14 14:12:55 -07001064 SkIPoint offset = { 0, 0 };
1065
1066 sk_sp<SkSpecialImage> result;
1067 if (paint.getImageFilter()) {
Robert Phillips213ce182018-04-25 09:13:28 -04001068 result = this->filterTexture(special1, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001069 if (!result) {
1070 return;
1071 }
1072 } else {
1073 result = sk_ref_sp(special1);
1074 }
1075
1076 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001077 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001078 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001079 return;
1080 }
robertphillips970587b2016-07-14 14:12:55 -07001081
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001082 const GrPixelConfig config = proxy->config();
1083
robertphillips970587b2016-07-14 14:12:55 -07001084 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001085 if (tmpUnfiltered.getMaskFilter()) {
1086 SkMatrix ctm = this->ctm();
1087 ctm.postTranslate(-SkIntToScalar(left + offset.fX), -SkIntToScalar(top + offset.fY));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001088 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001089 }
1090
robertphillips970587b2016-07-14 14:12:55 -07001091 tmpUnfiltered.setImageFilter(nullptr);
1092
Brian Osman2240be92017-10-18 13:15:13 -04001093 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman653f34d2018-06-14 11:44:02 -04001094 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001095 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001096 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001097 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001098 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001099 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001100 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001101
1102 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001103 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1104 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001105 return;
1106 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107
robertphillips970587b2016-07-14 14:12:55 -07001108 const SkIRect& subset = result->subset();
1109
Brian Salomon82f44312017-01-11 13:42:54 -05001110 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001111 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001112 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001113 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001114 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001115 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1116 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001117 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118}
1119
Mike Reeda1361362017-03-07 09:37:29 -05001120void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001121 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001122 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001123 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001124 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1125 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1126 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1127 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1128 const SkRect* dst = &origDst;
1129 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001131 if (!src) {
1132 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001134
bsalomonb1b01992015-11-18 10:56:08 -08001135 SkMatrix srcToDstMatrix;
1136 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1137 return;
1138 }
1139 SkRect tmpSrc, tmpDst;
1140 if (src != &bmpBounds) {
1141 if (!bmpBounds.contains(*src)) {
1142 tmpSrc = *src;
1143 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144 return; // nothing to draw
1145 }
bsalomonb1b01992015-11-18 10:56:08 -08001146 src = &tmpSrc;
1147 srcToDstMatrix.mapRect(&tmpDst, *src);
1148 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149 }
1150 }
1151
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001152 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001153
bsalomonb1b01992015-11-18 10:56:08 -08001154 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1155 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001156 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1157 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1158 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001159
Brian Salomon7c8460e2017-05-12 11:36:10 -04001160 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001161
1162 if (!skipTileCheck) {
1163 int tileSize;
1164 SkIRect clippedSrcRect;
1165
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001166 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001167 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001168 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001169 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1170 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001171
1172 int tileFilterPad;
1173
1174 if (doBicubic) {
1175 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001176 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001177 tileFilterPad = 0;
1178 } else {
1179 tileFilterPad = 1;
1180 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001181 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001182
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001183 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001184 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001185 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1186 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001187 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001188 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001189 return;
1190 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001191 }
Hal Canary144caf52016-11-07 17:57:18 -05001192 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0cdfc322018-05-14 16:17:43 -04001193 this->drawTextureMaker(&maker, bitmap.width(), bitmap.height(), src, dst, constraint,
1194 this->ctm(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195}
1196
robertphillips6451a0c2016-07-18 08:31:31 -07001197sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001198 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1199 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001200 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1201 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001202 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001203 return nullptr;
1204 }
1205
Robert Phillipse14d3052017-02-15 13:18:21 -05001206 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001207
Robert Phillipse14d3052017-02-15 13:18:21 -05001208 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1209 // the special image
1210 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1211 rect,
1212 bitmap.getGenerationID(),
1213 std::move(proxy),
1214 bitmap.refColorSpace(),
1215 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001216}
1217
reede51c3562016-07-19 14:33:20 -07001218sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001219 SkPixmap pm;
1220 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001221 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001222
Robert Phillips6de99042017-01-31 11:31:39 -05001223 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1224 SkIRect::MakeWH(image->width(), image->height()),
1225 image->uniqueID(),
1226 std::move(proxy),
1227 as_IB(image)->onImageInfo().refColorSpace(),
1228 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001229 } else if (image->peekPixels(&pm)) {
1230 SkBitmap bm;
1231
1232 bm.installPixels(pm);
1233 return this->makeSpecial(bm);
1234 } else {
1235 return nullptr;
1236 }
1237}
1238
1239sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001240 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1241 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001242 // When the device doesn't have a texture, we create a temporary texture.
1243 // TODO: we should actually only copy the portion of the source needed to apply the image
1244 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001245 proxy = GrSurfaceProxy::Copy(fContext.get(),
1246 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001247 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001248 SkBudgeted::kYes);
1249 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001250 return nullptr;
1251 }
robertphillips1b5f9682016-07-15 08:01:12 -07001252 }
1253
1254 const SkImageInfo ii = this->imageInfo();
1255 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1256
Robert Phillipse2f7d182016-12-15 09:23:05 -05001257 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1258 srcRect,
1259 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001260 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001261 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001262 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001263}
1264
Mike Reeda1361362017-03-07 09:37:29 -05001265void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001266 int left, int top, const SkPaint& paint) {
1267 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001268
joshualittce894002016-01-11 13:29:31 -08001269 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001270 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001271 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001274 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001275 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001276 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 return;
1278 }
1279
Mike Reed2179b782018-02-07 11:59:57 -05001280 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281}
1282
Brian Salomon34169692017-08-28 15:32:01 -04001283void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001284 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001285 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001286 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001287 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001288 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001289 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1290 image->alphaType(), nullptr, nullptr,
1291 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001292 return;
Brian Salomon34169692017-08-28 15:32:01 -04001293 }
1294 SkBitmap bm;
1295 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1296 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1297 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001298 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001299 return;
reed85d91782015-09-10 14:33:38 -07001300 }
Brian Salomon34169692017-08-28 15:32:01 -04001301 this->drawBitmap(bm, x, y, paint);
1302 return;
1303 }
1304 if (image->isLazyGenerated()) {
1305 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1306 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1307 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1308 return;
1309 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001310 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001311 GrBitmapTextureMaker maker(fContext.get(), bm);
1312 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1313 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001314 }
1315}
1316
Brian Salomon34169692017-08-28 15:32:01 -04001317void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1318 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001319 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001320 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001321 if (!src || src->contains(image->bounds())) {
1322 constraint = SkCanvas::kFast_SrcRectConstraint;
1323 }
Robert Phillips3798c862017-03-27 11:08:16 -04001324 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001325 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1326 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001327 return;
1328 }
1329 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001330 SkMatrix srcToDstRect;
1331 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1332 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001333 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001334 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001335 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001336 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001337 return;
1338 }
Mike Reeda1361362017-03-07 09:37:29 -05001339 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001340 return;
1341 }
1342 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001343 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001344 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1345 this->ctm(), paint);
1346 return;
1347 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001348 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001349 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001350 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1351 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001352 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001353}
1354
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001355// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1356static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1357 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1358 return GrSamplerState::Filter::kNearest;
1359 }
1360
1361 return GrSamplerState::Filter::kBilerp;
1362}
1363
Mike Reeda1361362017-03-07 09:37:29 -05001364void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001365 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001366 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001367 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001368 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001369 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001370 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001371 image->alphaType(), pinnedUniqueID,
1372 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001373 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001374 } else {
1375 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001376 if (image->isLazyGenerated()) {
1377 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001378 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001379 } else if (as_IB(image)->getROPixels(&bm,
1380 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001381 GrBitmapTextureMaker maker(fContext.get(), bm);
1382 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001383 }
1384 }
1385}
1386
Mike Reeda1361362017-03-07 09:37:29 -05001387void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001388 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001389 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001390 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001391 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001392 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001393}
1394
Mike Reeda1361362017-03-07 09:37:29 -05001395void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001396 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1397 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001398 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001399 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001400
Brian Salomon2a943df2018-05-04 13:43:19 -04001401 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1402 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1403 }
msarett10e3d9b2016-08-18 15:46:03 -07001404 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001405 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1406 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001407 return;
1408 }
1409
Brian Salomon2a943df2018-05-04 13:43:19 -04001410 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1411 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
1412 sk_sp<SkColorSpace> proxyColorSpace;
1413 auto proxy =
1414 producer->refTextureProxyForParams(filter, dstColorSpace, &proxyColorSpace, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001415 if (!proxy) {
1416 return;
1417 }
Brian Osman653f34d2018-06-14 11:44:02 -04001418 auto csxf = GrColorSpaceXform::Make(proxyColorSpace.get(), dstColorSpace);
Brian Salomon2a943df2018-05-04 13:43:19 -04001419
Brian Salomon0166cfd2017-03-13 17:58:25 -04001420 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001421 std::move(proxy), std::move(csxf), filter,
1422 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001423}
1424
Mike Reeda1361362017-03-07 09:37:29 -05001425void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001426 const SkCanvas::Lattice& lattice, const SkRect& dst,
1427 const SkPaint& paint) {
1428 ASSERT_SINGLE_OWNER
1429 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001430 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001431 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001432 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001433 image->alphaType(), pinnedUniqueID,
1434 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001435 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001436 } else {
1437 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001438 if (image->isLazyGenerated()) {
1439 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001440 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001441 } else if (as_IB(image)->getROPixels(&bm,
1442 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001443 GrBitmapTextureMaker maker(fContext.get(), bm);
1444 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001445 }
1446 }
1447}
1448
Mike Reeda1361362017-03-07 09:37:29 -05001449void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001450 const SkCanvas::Lattice& lattice, const SkRect& dst,
1451 const SkPaint& paint) {
1452 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001453 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001454 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001455 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001456}
1457
Brian Salomonf3569f02017-10-24 12:52:33 -04001458static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1459 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001460 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001461 if (hasTexs && skPaint.getShader()) {
1462 if (hasColors) {
1463 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001464 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1465 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001466 } else {
1467 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001468 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001469 }
1470 } else {
1471 if (hasColors) {
1472 // We have colors, but either have no shader or no texture coords (which implies that
1473 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001474 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001475 } else {
1476 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001477 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001478 }
1479 }
1480}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001481
Mike Reed887cdf12017-04-03 11:11:09 -04001482void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001483 const SkPoint vertices[], SkBlendMode bmode,
1484 const uint16_t indices[], int indexCount,
1485 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001486 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001487 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001488
Mike Reed2f6b5a42017-03-19 15:04:17 -04001489 SkPaint copy(paint);
1490 copy.setStyle(SkPaint::kStroke_Style);
1491 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001492
Mike Reed2f6b5a42017-03-19 15:04:17 -04001493 GrPaint grPaint;
1494 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001495 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1496 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001497 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001498 }
1499
Mike Reed2f6b5a42017-03-19 15:04:17 -04001500 int triangleCount = 0;
1501 int n = (nullptr == indices) ? vertexCount : indexCount;
1502 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001503 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001504 triangleCount = n / 3;
1505 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001506 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001507 triangleCount = n - 2;
1508 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001509 case SkVertices::kTriangleFan_VertexMode:
1510 SK_ABORT("Unexpected triangle fan.");
1511 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001512 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513
Mike Reed2f6b5a42017-03-19 15:04:17 -04001514 VertState state(vertexCount, indices, indexCount);
1515 VertState::Proc vertProc = state.chooseProc(vmode);
1516
1517 //number of indices for lines per triangle with kLines
1518 indexCount = triangleCount * 6;
1519
Brian Osmanae0c50c2017-05-25 16:56:34 -04001520 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1521 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1522 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1523
1524 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001525 int i = 0;
1526 while (vertProc(&state)) {
1527 lineIndices[i] = state.f0;
1528 lineIndices[i + 1] = state.f1;
1529 lineIndices[i + 2] = state.f1;
1530 lineIndices[i + 3] = state.f2;
1531 lineIndices[i + 4] = state.f2;
1532 lineIndices[i + 5] = state.f0;
1533 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001534 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001535
Chris Dalton3809bab2017-06-13 10:55:06 -06001536 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001537 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001538 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001539 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001540 builder.detach(),
1541 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542}
1543
Ruiqi Maof5101492018-06-29 14:32:21 -04001544void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkMatrix* bones, int boneCount,
1545 SkBlendMode mode, const SkPaint& paint) {
1546 // TODO: GPU ANIMATION
Brian Salomon199fb872017-02-06 09:41:10 -05001547 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001548 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001549
1550 SkASSERT(vertices);
1551 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001552 bool hasColors = vertices->hasColors();
1553 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001554 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001555 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001556 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1557 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001558 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001559 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001560 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1561 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001562 return;
1563 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001564 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001565 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001566}
1567
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001568///////////////////////////////////////////////////////////////////////////////
1569
Jim Van Verth3af1af92017-05-18 15:06:54 -04001570void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1571
1572 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001573 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1574
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001575 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001576 // failed to find an accelerated case
1577 this->INHERITED::drawShadow(path, rec);
1578 }
1579}
1580
1581///////////////////////////////////////////////////////////////////////////////
1582
Mike Reeda1361362017-03-07 09:37:29 -05001583void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001584 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001585 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001586 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001587 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001588 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001589 return;
1590 }
1591
Hal Canary144caf52016-11-07 17:57:18 -05001592 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001593
reedca109532015-06-25 16:25:25 -07001594 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001595 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001596
jvanverth31ff7622015-08-07 10:09:28 -07001597 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001598 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001599 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1600 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001601 return;
1602 }
1603 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001604 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1605 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001606 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001607 }
1608 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001609
1610 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001611 fRenderTargetContext->drawAtlas(
1612 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001613}
1614
1615///////////////////////////////////////////////////////////////////////////////
1616
Mike Reeda1361362017-03-07 09:37:29 -05001617void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001618 const SkScalar pos[], int scalarsPerPos,
1619 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001620 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001621 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001622 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001623
Brian Salomon0166cfd2017-03-13 17:58:25 -04001624 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1625 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001626 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001627}
1628
Robert Phillips38580452018-06-28 12:00:35 +00001629void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1630 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001631 ASSERT_SINGLE_OWNER
Robert Phillips38580452018-06-28 12:00:35 +00001632 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001633 SkDEBUGCODE(this->validate();)
1634
Robert Phillips38580452018-06-28 12:00:35 +00001635 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1636 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001637}
1638
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639///////////////////////////////////////////////////////////////////////////////
1640
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001642 this->flushAndSignalSemaphores(0, nullptr);
1643}
1644
Greg Daniel51316782017-08-02 15:10:09 +00001645GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1646 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001647 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001648
Greg Danielc64ee462017-06-15 16:59:49 -04001649 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001650}
1651
Greg Danielc64ee462017-06-15 16:59:49 -04001652bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001653 ASSERT_SINGLE_OWNER
1654
Greg Danielc64ee462017-06-15 16:59:49 -04001655 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656}
1657
1658///////////////////////////////////////////////////////////////////////////////
1659
reed76033be2015-03-14 10:54:31 -07001660SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001661 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662
robertphillipsca6eafc2016-05-17 09:57:46 -07001663 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001664
robertphillipsca6eafc2016-05-17 09:57:46 -07001665 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001666 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001667 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1668 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001669
Brian Osman10fc6fd2018-03-02 11:01:10 -05001670 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
1671 if (kRGBA_1010102_GrPixelConfig == config) {
1672 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1673 // channel in the layer.
1674 config = kRGBA_8888_GrPixelConfig;
1675 }
1676
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001677 sk_sp<GrRenderTargetContext> rtc(fContext->contextPriv().makeDeferredRenderTargetContext(
Brian Osman10fc6fd2018-03-02 11:01:10 -05001678 fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001679 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001680 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1681 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001682 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001683 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001684 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001685
1686 // Skia's convention is to only clear a device if it is non-opaque.
1687 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1688
Robert Phillips9fab7e92016-11-17 12:45:04 -05001689 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1690 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691}
1692
reede8f30622016-03-23 18:59:25 -07001693sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001694 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001695 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001696 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001697 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001698 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001699 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001700}
1701
senorblanco900c3672016-04-27 11:31:23 -07001702SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001703 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001704 // We always return a transient cache, so it is freed after each
1705 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001706 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001707}
reedf037e0b2014-10-30 11:34:15 -07001708