blob: a08c0e5600643678d9c4de79ae1944e5f5f21a45 [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"
Brian Salomon2a943df2018-05-04 13:43:19 -04009#include "../private/SkShadowFlags.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050010#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070011#include "GrBlurUtils.h"
Brian Osman1cb41712017-10-19 12:54:52 -040012#include "GrColorSpaceXform.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050014#include "GrContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080015#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050016#include "GrImageTextureMaker.h"
17#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070018#include "GrStyle.h"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040019#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050020#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050021#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070022#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080023#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070024#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070026#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070028#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050029#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050030#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070031#include "SkLatticeIter.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040032#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050033#include "SkMaskFilterBase.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000035#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070036#include "SkPictureData.h"
Mike Reed242135a2018-02-22 13:41:39 -050037#include "SkRRectPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050038#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050039#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080040#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070041#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000042#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000043#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080044#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000045#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000046#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000047#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050048#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050049#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080050#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080051#include "effects/GrSimpleTextureEffect.h"
52#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080053#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054
joshualittce894002016-01-11 13:29:31 -080055#define ASSERT_SINGLE_OWNER \
Robert Phillips0c4b7b12018-03-06 08:20:37 -050056SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->contextPriv().debugSingleOwner());)
joshualittce894002016-01-11 13:29:31 -080057
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059///////////////////////////////////////////////////////////////////////////////
60
bsalomon74f681d2015-06-23 14:38:48 -070061/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
62 should fail. */
63bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
64 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
65 *flags = 0;
66 if (info) {
67 switch (info->alphaType()) {
68 case kPremul_SkAlphaType:
69 break;
70 case kOpaque_SkAlphaType:
71 *flags |= SkGpuDevice::kIsOpaque_Flag;
72 break;
73 default: // If it is unpremul or unknown don't try to render
74 return false;
75 }
76 }
77 if (kClear_InitContents == init) {
78 *flags |= kNeedClear_Flag;
79 }
80 return true;
81}
82
Robert Phillips9fab7e92016-11-17 12:45:04 -050083sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
84 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070085 int width, int height,
86 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040087 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070088 return nullptr;
89 }
90 unsigned flags;
91 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
92 return nullptr;
93 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050094 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
95 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000096}
97
robertphillips24e91282016-04-29 06:46:36 -070098sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
99 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400100 GrSurfaceOrigin origin, const SkSurfaceProps* props,
101 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700102 unsigned flags;
103 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700105 }
106
Brian Osman11052242016-10-27 14:47:55 -0400107 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
108 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400109 origin, props,
110 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400111 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700113 }
114
Robert Phillips9fab7e92016-11-17 12:45:04 -0500115 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700116 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700117}
118
Brian Osman11052242016-10-27 14:47:55 -0400119static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700120 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400121 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700122 colorType = kUnknown_SkColorType;
123 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400124 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
125 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700126}
127
Robert Phillips9fab7e92016-11-17 12:45:04 -0500128SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
129 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400130 : INHERITED(make_info(renderTargetContext.get(), width, height,
131 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500132 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400133 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700134{
robertphillips1f3923e2016-07-21 07:17:54 -0700135 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700136 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
bsalomone63ffef2016-02-05 07:17:34 -0800138 if (flags & kNeedClear_Flag) {
139 this->clearAll();
140 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141}
142
Brian Osman11052242016-10-27 14:47:55 -0400143sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
144 GrContext* context,
145 SkBudgeted budgeted,
146 const SkImageInfo& origInfo,
147 int sampleCount,
148 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400149 const SkSurfaceProps* surfaceProps,
150 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000151 if (kUnknown_SkColorType == origInfo.colorType() ||
152 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700153 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000154 }
155
bsalomonafe30052015-01-16 07:32:33 -0800156 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800158 }
159
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400160 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->contextPriv().caps());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500161 if (kUnknown_GrPixelConfig == config) {
162 return nullptr;
163 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400164 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
165 // they need to be exact.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500166 return context->contextPriv().makeDeferredRenderTargetContext(
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400167 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700168 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500169 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400170 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800171}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000172
Mike Reeda1361362017-03-07 09:37:29 -0500173sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700174 int left, int top,
175 SkIPoint* offset,
176 const SkImageFilter* filter) {
177 SkASSERT(srcImg->isTextureBacked());
178 SkASSERT(filter);
179
Mike Reeda1361362017-03-07 09:37:29 -0500180 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700181 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500182 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500183 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Salomonf3569f02017-10-24 12:52:33 -0400184 SkImageFilter::OutputProperties outputProperties(
185 fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700186 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700187
188 return filter->filterImage(srcImg, ctx, offset);
189}
190
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191///////////////////////////////////////////////////////////////////////////////
192
Mike Reed353196f2017-07-21 11:01:18 -0400193bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800194 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000195
Mike Reed353196f2017-07-21 11:01:18 -0400196 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500197 return false;
198 }
199
Mike Reed353196f2017-07-21 11:01:18 -0400200 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500201 if (!rec.trim(this->width(), this->height())) {
202 return false;
203 }
204
205 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000206}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207
Mike Reed353196f2017-07-21 11:01:18 -0400208bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800209 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700210
Mike Reed353196f2017-07-21 11:01:18 -0400211 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500212 return false;
213 }
214
Mike Reed353196f2017-07-21 11:01:18 -0400215 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500216 if (!rec.trim(this->width(), this->height())) {
217 return false;
218 }
219
220 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000221}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000222
reed41e010c2015-06-09 12:16:53 -0700223bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800224 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700225 return false;
226}
227
Brian Osman11052242016-10-27 14:47:55 -0400228GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700229 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400230 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700231}
232
reed8eddfb52014-12-04 07:50:14 -0800233void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800234 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500235 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500236
reed8eddfb52014-12-04 07:50:14 -0800237 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Chris Dalton344e9032017-12-11 15:42:09 -0700238 fRenderTargetContext->clear(&rect, 0x0, GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800239}
240
Brian Osman11052242016-10-27 14:47:55 -0400241void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800242 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800243
Brian Osman693a5402016-10-27 15:13:22 -0400244 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800245
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400246 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
247 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400248 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400249 this->context(),
250 budgeted,
251 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400252 fRenderTargetContext->numColorSamples(),
253 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400254 &this->surfaceProps(),
255 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400256 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800257 return;
258 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400259 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800260
261 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400262 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800263 return;
264 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500265 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800266 }
267
Brian Osman693a5402016-10-27 15:13:22 -0400268 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800269}
270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271///////////////////////////////////////////////////////////////////////////////
272
Mike Reeda1361362017-03-07 09:37:29 -0500273void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800274 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500275 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276
277 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400278 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
279 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700280 return;
281 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
Brian Salomon0166cfd2017-03-13 17:58:25 -0400283 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284}
285
Brian Salomon1459ebd2017-12-19 16:12:44 -0500286static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
287 switch (mode) {
288 case SkCanvas::kPoints_PointMode:
289 return GrPrimitiveType::kPoints;
290 case SkCanvas::kLines_PointMode:
291 return GrPrimitiveType::kLines;
292 case SkCanvas::kPolygon_PointMode:
293 return GrPrimitiveType::kLineStrip;
294 }
295 SK_ABORT("Unexpected mode");
296 return GrPrimitiveType::kPoints;
297}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
Mike Reeda1361362017-03-07 09:37:29 -0500299void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800301 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500302 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303 SkScalar width = paint.getStrokeWidth();
304 if (width < 0) {
305 return;
306 }
307
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000308 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700309 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700310 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400311 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
312 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700313 return;
314 }
egdaniele61c4112014-06-12 10:24:21 -0700315 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700316 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700317 path.moveTo(pts[0]);
318 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700319 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
320 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700321 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000322 }
323
bsalomon6ade6dd2016-09-12 12:07:17 -0700324 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500325 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700326 SkScalarNearlyEqual(scales[0], 1.f) &&
327 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700328 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400330 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500331 SkRasterClip rc(this->devClipBounds());
332 SkDraw draw;
333 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
334 draw.fMatrix = &this->ctm();
335 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500336 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000337 return;
338 }
339
Brian Salomon1459ebd2017-12-19 16:12:44 -0500340 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700341
Mike Reeda1361362017-03-07 09:37:29 -0500342 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700343#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
344 // This offsetting in device space matches the expectations of the Android framework for non-AA
345 // points and lines.
346 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600347 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700348 tempMatrix = *viewMatrix;
349 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
350 tempMatrix.postTranslate(kOffset, kOffset);
351 viewMatrix = &tempMatrix;
352 }
353#endif
354
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400356 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
357 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700358 return;
359 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000360
Brian Osmanae0c50c2017-05-25 16:56:34 -0400361 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
362 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
363 nullptr);
364
365 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
366 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367}
368
369///////////////////////////////////////////////////////////////////////////////
370
Mike Reeda1361362017-03-07 09:37:29 -0500371void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800372 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500373 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
bsalomona7d85ba2016-07-06 11:54:59 -0700374 // A couple reasons we might need to call drawPath.
375 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000376 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700377 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500379 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400380 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500381 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382 return;
383 }
384
385 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400386 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
387 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700388 return;
389 }
Mike Klein744fb732014-06-23 15:13:26 -0400390
bsalomon6663acf2016-05-10 09:14:17 -0700391 GrStyle style(paint);
Chris Dalton3b51df12017-11-27 14:33:06 -0700392 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
393 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394}
395
396///////////////////////////////////////////////////////////////////////////////
397
Mike Reeda1361362017-03-07 09:37:29 -0500398void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800399 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500400 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000401 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400402 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
403 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700404 return;
405 }
Mike Klein744fb732014-06-23 15:13:26 -0400406
Mike Reed80747ef2018-01-23 15:29:32 -0500407 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000408 if (mf) {
409 if (mf->hasFragmentProcessor()) {
410 mf = nullptr; // already handled in SkPaintToGrPaint
411 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400412 }
413
bsalomon6663acf2016-05-10 09:14:17 -0700414 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400415 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000416 // try to hit the fast path for drawing filtered round rects
417
418 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500419 if (rrect.transform(this->ctm(), &devRRect)) {
Mike Reed242135a2018-02-22 13:41:39 -0500420 if (SkRRectPriv::AllCornersCircular(devRRect)) {
Brian Osmane191ce62017-09-14 15:43:50 -0400421 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(), this->ctm(), nullptr)) {
Robert Phillips26c90e02017-03-14 14:39:29 -0400422 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400423 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500424 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000425 return;
426 }
427 }
428
429 }
430 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000431 }
432
Robert Phillipsa29a9562016-10-20 09:40:55 -0400433 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800434 // The only mask filter the native rrect drawing code could've handle was taken
435 // care of above.
436 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000437 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700438 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700439 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500440 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400441 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500442 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443 return;
444 }
Mike Klein744fb732014-06-23 15:13:26 -0400445
bsalomon6663acf2016-05-10 09:14:17 -0700446 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800447
Chris Dalton3b51df12017-11-27 14:33:06 -0700448 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
449 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450}
451
robertphillipsd7706102016-02-25 09:28:08 -0800452
Mike Reeda1361362017-03-07 09:37:29 -0500453void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800454 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800455 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500456 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800457 if (outer.isEmpty()) {
458 return;
459 }
460
461 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500462 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800463 }
464
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000465 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000466
robertphillips0e7029e2015-11-30 05:45:06 -0800467 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800468 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400469 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
470 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700471 return;
472 }
robertphillips00095892016-02-29 13:50:40 -0800473
Brian Salomon0166cfd2017-03-13 17:58:25 -0400474 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700475 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800476 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000477 }
478
479 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700480 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000481 path.addRRect(outer);
482 path.addRRect(inner);
483 path.setFillType(SkPath::kEvenOdd_FillType);
484
Brian Salomon0166cfd2017-03-13 17:58:25 -0400485 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
486 path, paint, this->ctm(), nullptr, this->devClipBounds(),
487 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000488}
489
490
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000491/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000492
Mike Reeda1361362017-03-07 09:37:29 -0500493void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700494 if (paint.getMaskFilter()) {
495 SkPath path;
496 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500497 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700498 }
499
500 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400501 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
502 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700503 return;
504 }
505
Chris Dalton3b51df12017-11-27 14:33:06 -0700506 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
507 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700508}
509
Mike Reeda1361362017-03-07 09:37:29 -0500510void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800511 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500512 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800513
robertphillips514450c2015-11-24 05:36:02 -0800514 if (paint.getMaskFilter()) {
515 // The RRect path can handle special case blurring
516 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500517 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518 }
519
520 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400521 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
522 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700523 return;
524 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525
Chris Dalton3b51df12017-11-27 14:33:06 -0700526 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
527 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000528}
529
Mike Reeda1361362017-03-07 09:37:29 -0500530void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700531 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
532 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500533 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700534 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500535 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700536 return;
537 }
538 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400539 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
540 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700541 return;
542 }
543
Chris Dalton3b51df12017-11-27 14:33:06 -0700544 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500545 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500546 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700547}
548
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550
551///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700552void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700553 const SkPaint& origPaint) {
554 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500555 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400556 // Adding support for round capping would require a
557 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700558 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
559 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
560 SkASSERT(!origPaint.getPathEffect());
561 SkASSERT(!origPaint.getMaskFilter());
562
563 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
564 SkASSERT(halfWidth > 0);
565
566 SkVector v = points[1] - points[0];
567
568 SkScalar length = SkPoint::Normalize(&v);
569 if (!length) {
570 v.fX = 1.0f;
571 v.fY = 0.0f;
572 }
573
574 SkPaint newPaint(origPaint);
575 newPaint.setStyle(SkPaint::kFill_Style);
576
577 SkScalar xtraLength = 0.0f;
578 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
579 xtraLength = halfWidth;
580 }
581
582 SkPoint mid = points[0] + points[1];
583 mid.scale(0.5f);
584
585 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
586 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
587 SkMatrix m;
588 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
589
590 SkMatrix local = m;
591
Mike Reeda1361362017-03-07 09:37:29 -0500592 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700593
594 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400595 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
596 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700597 return;
598 }
599
Brian Salomon82f44312017-01-11 13:42:54 -0500600 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700601 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700602}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603
Mike Reeda1361362017-03-07 09:37:29 -0500604void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000605 const SkPaint& paint, const SkMatrix* prePathMatrix,
606 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800607 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800608 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700609 SkPoint points[2];
610 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
611 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500612 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700613 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500614 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700615 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500616 // Round capping support is currently disabled b.c. it would require a RRect
617 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500618 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700619 return;
620 }
621 }
robertphillipsff55b492015-11-24 07:56:59 -0800622 }
623
Hal Canary144caf52016-11-07 17:57:18 -0500624 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Brian Salomona3cf3652018-01-03 15:11:00 -0500625 if (!prePathMatrix && !paint.getMaskFilter()) {
626 GrPaint grPaint;
627 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
628 this->ctm(), &grPaint)) {
629 return;
630 }
631 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
632 this->ctm(), origSrcPath, GrStyle(paint));
633 return;
634 }
Brian Salomon0166cfd2017-03-13 17:58:25 -0400635 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
636 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500637 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638}
639
640static const int kBmpSmallTileSize = 1 << 10;
641
642static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
643 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
644 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
645 return tilesX * tilesY;
646}
647
reed85d91782015-09-10 14:33:38 -0700648static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 if (maxTileSize <= kBmpSmallTileSize) {
650 return maxTileSize;
651 }
652
653 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
654 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
655
656 maxTileTotalTileSize *= maxTileSize * maxTileSize;
657 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
658
659 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
660 return kBmpSmallTileSize;
661 } else {
662 return maxTileSize;
663 }
664}
665
666// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
667// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700668static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800669 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800670 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700671 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700672 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673 const SkRect* srcRectPtr,
674 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700675 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700676 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
677 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 clippedSrcIRect->setEmpty();
679 return;
680 }
681 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
682 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700683 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684 if (!clippedSrcRect.intersect(*srcRectPtr)) {
685 clippedSrcIRect->setEmpty();
686 return;
687 }
688 }
689 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700690 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691 if (!clippedSrcIRect->intersect(bmpBounds)) {
692 clippedSrcIRect->setEmpty();
693 }
694}
695
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400696bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
697 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700698 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700699 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400700 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700701 const SkRect* srcRectPtr,
702 int maxTileSize,
703 int* tileSize,
704 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800705 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700706 // if it's larger than the max tile size, then we have no choice but tiling.
707 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400708 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400709 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
710 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700711 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
712 return true;
713 }
714
bsalomon1a1d0b82015-10-16 07:49:42 -0700715 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700716 const size_t area = imageRect.width() * imageRect.height();
717 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
718 return false;
719 }
720
reed85d91782015-09-10 14:33:38 -0700721 // At this point we know we could do the draw by uploading the entire bitmap
722 // as a texture. However, if the texture would be large compared to the
723 // cache size and we don't require most of it for this draw then tile to
724 // reduce the amount of upload and cache spill.
725
726 // assumption here is that sw bitmap size is a good proxy for its size as
727 // a texture
728 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
729 size_t cacheSize;
730 fContext->getResourceCacheLimits(nullptr, &cacheSize);
731 if (bmpSize < cacheSize / 2) {
732 return false;
733 }
734
bsalomon1a1d0b82015-10-16 07:49:42 -0700735 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
736 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400737 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
738 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400739 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700740 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
741 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500742 kBmpSmallTileSize * kBmpSmallTileSize *
743 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700744
Brian Osmand05cdc32017-02-06 13:24:47 -0500745 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700746}
747
reed85d91782015-09-10 14:33:38 -0700748bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
749 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700750 const SkMatrix& viewMatrix,
751 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800752 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400753 // If image is explicitly texture backed then we shouldn't get here.
754 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400756 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700757 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500758 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
759 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
760 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700761
762 int tileFilterPad;
763 if (doBicubic) {
764 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400765 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700766 tileFilterPad = 0;
767 } else {
768 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400770 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400772 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773
reed85d91782015-09-10 14:33:38 -0700774 // these are output, which we safely ignore, as we just want to know the predicate
775 int outTileSize;
776 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777
bsalomone553b642016-08-17 09:02:09 -0700778 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400779 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700780 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781}
782
Mike Reeda1361362017-03-07 09:37:29 -0500783void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400784 SkScalar x,
785 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400787 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800788 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800789 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500790 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700791
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400792 int maxTileSize = this->caps()->maxTileSize();
bsalomonb1b01992015-11-18 10:56:08 -0800793
794 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
795 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400796 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
797 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800798 bitmap.height() <= maxTileSize;
799
800 bool skipTileCheck = drawAA || paint.getMaskFilter();
801
802 if (!skipTileCheck) {
803 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
804 int tileSize;
805 SkIRect clippedSrcRect;
806
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400807 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800808 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400809 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500810 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
811 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800812
813 int tileFilterPad;
814
815 if (doBicubic) {
816 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400817 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800818 tileFilterPad = 0;
819 } else {
820 tileFilterPad = 1;
821 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400822 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800823
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400824 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700825 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400826 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700827 &tileSize, &clippedSrcRect)) {
828 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400829 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
830 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800831 return;
832 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000833 }
Hal Canary144caf52016-11-07 17:57:18 -0500834 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800835 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -0400836 viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837}
838
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000839// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
841// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000842static inline void clamped_outset_with_offset(SkIRect* iRect,
843 int outset,
844 SkPoint* offset,
845 const SkIRect& clamp) {
846 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000848 int leftClampDelta = clamp.fLeft - iRect->fLeft;
849 if (leftClampDelta > 0) {
850 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851 iRect->fLeft = clamp.fLeft;
852 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000853 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000855
856 int topClampDelta = clamp.fTop - iRect->fTop;
857 if (topClampDelta > 0) {
858 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859 iRect->fTop = clamp.fTop;
860 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000861 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000862 }
863
864 if (iRect->fRight > clamp.fRight) {
865 iRect->fRight = clamp.fRight;
866 }
867 if (iRect->fBottom > clamp.fBottom) {
868 iRect->fBottom = clamp.fBottom;
869 }
870}
871
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872// Break 'bitmap' into several tiles to draw it since it has already
873// been determined to be too large to fit in VRAM
874void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800875 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700876 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 const SkRect& srcRect,
878 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400879 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800880 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700881 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000882 int tileSize,
883 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800884 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800885
ericrk983294f2016-04-18 09:14:00 -0700886 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800887 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700888 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800889
bsalomonc55271f2015-11-09 11:55:57 -0800890 const SkPaint* paint = &origPaint;
891 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400892 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800893 // Drop antialiasing to avoid seams at tile boundaries.
894 tempPaint = origPaint;
895 tempPaint.setAntiAlias(false);
896 paint = &tempPaint;
897 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
899
900 int nx = bitmap.width() / tileSize;
901 int ny = bitmap.height() / tileSize;
902 for (int x = 0; x <= nx; x++) {
903 for (int y = 0; y <= ny; y++) {
904 SkRect tileR;
905 tileR.set(SkIntToScalar(x * tileSize),
906 SkIntToScalar(y * tileSize),
907 SkIntToScalar((x + 1) * tileSize),
908 SkIntToScalar((y + 1) * tileSize));
909
910 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
911 continue;
912 }
913
914 if (!tileR.intersect(srcRect)) {
915 continue;
916 }
917
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918 SkIRect iTileR;
919 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700920 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
921 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500922 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700923 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400924 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 SkIRect iClampRect;
926
reeda5517e22015-07-14 10:54:12 -0700927 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 // In bleed mode we want to always expand the tile on all edges
929 // but stay within the bitmap bounds
930 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
931 } else {
932 // In texture-domain/clamp mode we only want to expand the
933 // tile on edges interior to "srcRect" (i.e., we want to
934 // not bleed across the original clamped edges)
935 srcRect.roundOut(&iClampRect);
936 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000937 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
938 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939 }
940
bsalomone553b642016-08-17 09:02:09 -0700941 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 if (bitmap.extractSubset(&tmpB, iTileR)) {
943 // now offset it to make it "local" to our tmp bitmap
944 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800945 // de-optimized this determination
946 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700947 this->drawBitmapTile(tmpB,
948 viewMatrix,
949 rectToDraw,
950 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500951 params,
bsalomone553b642016-08-17 09:02:09 -0700952 *paint,
953 constraint,
954 bicubic,
955 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 }
957 }
958 }
959}
960
bsalomone553b642016-08-17 09:02:09 -0700961void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
962 const SkMatrix& viewMatrix,
963 const SkRect& dstRect,
964 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400965 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700966 const SkPaint& paint,
967 SkCanvas::SrcRectConstraint constraint,
968 bool bicubic,
969 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800970 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400971 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
972 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700973 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400974 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
975 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400976 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977
Brian Salomon2a943df2018-05-04 13:43:19 -0400978 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400979 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400980 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400981 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 return;
983 }
bsalomone553b642016-08-17 09:02:09 -0700984
bsalomone553b642016-08-17 09:02:09 -0700985 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400986 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
987 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700988
989 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
990 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400991 std::unique_ptr<GrFragmentProcessor> fp;
Brian Osmanf06ead92017-10-30 13:47:41 -0400992 GrPixelConfig config = proxy->config();
joshualitt5f10b5c2015-07-09 10:24:35 -0700993
reeda5517e22015-07-14 10:54:12 -0700994 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700996 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500998 domain.fLeft = srcRect.fLeft + 0.5f;
999 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001001 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 }
1003 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001004 domain.fTop = srcRect.fTop + 0.5f;
1005 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001006 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001007 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001009 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001010 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001011 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001012 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001013 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001014 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001015 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001016 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1017 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001018 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001019 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001020 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001021 }
1022
Brian Osmanf06ead92017-10-30 13:47:41 -04001023 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001024 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001025 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001026 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1027 viewMatrix, std::move(fp),
1028 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001029 return;
1030 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001032 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001033 GrAA aa = GrAA(paint.isAntiAlias() &&
1034 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001035 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036}
1037
Mike Reeda1361362017-03-07 09:37:29 -05001038void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001040 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001041 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001043 if (fContext->contextPriv().abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001044 return;
1045 }
1046
Robert Phillipse14d3052017-02-15 13:18:21 -05001047 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1048 if (!srcImg) {
1049 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001050 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051
Florin Malita53f77bd2017-04-28 13:48:37 -04001052 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001053}
1054
1055
Florin Malita53f77bd2017-04-28 13:48:37 -04001056void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001057 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001058 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001059 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001060
Florin Malita53f77bd2017-04-28 13:48:37 -04001061 // TODO: clipImage support.
1062
robertphillips970587b2016-07-14 14:12:55 -07001063 SkIPoint offset = { 0, 0 };
1064
1065 sk_sp<SkSpecialImage> result;
1066 if (paint.getImageFilter()) {
Robert Phillips213ce182018-04-25 09:13:28 -04001067 result = this->filterTexture(special1, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001068 if (!result) {
1069 return;
1070 }
1071 } else {
1072 result = sk_ref_sp(special1);
1073 }
1074
1075 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001076 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001077 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001078 return;
1079 }
robertphillips970587b2016-07-14 14:12:55 -07001080
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001081 const GrPixelConfig config = proxy->config();
1082
robertphillips970587b2016-07-14 14:12:55 -07001083 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001084 if (tmpUnfiltered.getMaskFilter()) {
1085 SkMatrix ctm = this->ctm();
1086 ctm.postTranslate(-SkIntToScalar(left + offset.fX), -SkIntToScalar(top + offset.fY));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001087 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001088 }
1089
robertphillips970587b2016-07-14 14:12:55 -07001090 tmpUnfiltered.setImageFilter(nullptr);
1091
Brian Osman2240be92017-10-18 13:15:13 -04001092 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osmanf06ead92017-10-30 13:47:41 -04001093 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001094 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001095 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001096 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001097 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001098 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001099 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001100
1101 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001102 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1103 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001104 return;
1105 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106
robertphillips970587b2016-07-14 14:12:55 -07001107 const SkIRect& subset = result->subset();
1108
Brian Salomon82f44312017-01-11 13:42:54 -05001109 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001110 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001111 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001112 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001113 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001114 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1115 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001116 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117}
1118
Mike Reeda1361362017-03-07 09:37:29 -05001119void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001120 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001121 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001122 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001123 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1124 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1125 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1126 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1127 const SkRect* dst = &origDst;
1128 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001129 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001130 if (!src) {
1131 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001133
bsalomonb1b01992015-11-18 10:56:08 -08001134 SkMatrix srcToDstMatrix;
1135 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1136 return;
1137 }
1138 SkRect tmpSrc, tmpDst;
1139 if (src != &bmpBounds) {
1140 if (!bmpBounds.contains(*src)) {
1141 tmpSrc = *src;
1142 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 return; // nothing to draw
1144 }
bsalomonb1b01992015-11-18 10:56:08 -08001145 src = &tmpSrc;
1146 srcToDstMatrix.mapRect(&tmpDst, *src);
1147 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 }
1149 }
1150
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001151 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001152
bsalomonb1b01992015-11-18 10:56:08 -08001153 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1154 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001155 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1156 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1157 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001158
Brian Salomon7c8460e2017-05-12 11:36:10 -04001159 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001160
1161 if (!skipTileCheck) {
1162 int tileSize;
1163 SkIRect clippedSrcRect;
1164
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001165 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001166 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001167 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001168 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1169 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001170
1171 int tileFilterPad;
1172
1173 if (doBicubic) {
1174 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001175 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001176 tileFilterPad = 0;
1177 } else {
1178 tileFilterPad = 1;
1179 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001180 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001181
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001182 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001183 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001184 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1185 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001186 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001187 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001188 return;
1189 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001190 }
Hal Canary144caf52016-11-07 17:57:18 -05001191 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon34169692017-08-28 15:32:01 -04001192 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193}
1194
robertphillips6451a0c2016-07-18 08:31:31 -07001195sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001196 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1197 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001198 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1199 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001200 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001201 return nullptr;
1202 }
1203
Robert Phillipse14d3052017-02-15 13:18:21 -05001204 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001205
Robert Phillipse14d3052017-02-15 13:18:21 -05001206 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1207 // the special image
1208 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1209 rect,
1210 bitmap.getGenerationID(),
1211 std::move(proxy),
1212 bitmap.refColorSpace(),
1213 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001214}
1215
reede51c3562016-07-19 14:33:20 -07001216sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001217 SkPixmap pm;
1218 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001219 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001220
Robert Phillips6de99042017-01-31 11:31:39 -05001221 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1222 SkIRect::MakeWH(image->width(), image->height()),
1223 image->uniqueID(),
1224 std::move(proxy),
1225 as_IB(image)->onImageInfo().refColorSpace(),
1226 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001227 } else if (image->peekPixels(&pm)) {
1228 SkBitmap bm;
1229
1230 bm.installPixels(pm);
1231 return this->makeSpecial(bm);
1232 } else {
1233 return nullptr;
1234 }
1235}
1236
1237sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001238 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1239 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001240 // When the device doesn't have a texture, we create a temporary texture.
1241 // TODO: we should actually only copy the portion of the source needed to apply the image
1242 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001243 proxy = GrSurfaceProxy::Copy(fContext.get(),
1244 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001245 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001246 SkBudgeted::kYes);
1247 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001248 return nullptr;
1249 }
robertphillips1b5f9682016-07-15 08:01:12 -07001250 }
1251
1252 const SkImageInfo ii = this->imageInfo();
1253 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1254
Robert Phillipse2f7d182016-12-15 09:23:05 -05001255 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1256 srcRect,
1257 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001258 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001259 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001260 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001261}
1262
Mike Reeda1361362017-03-07 09:37:29 -05001263void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001264 int left, int top, const SkPaint& paint) {
1265 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001266
joshualittce894002016-01-11 13:29:31 -08001267 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001269 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001272 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001273 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001274 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001275 return;
1276 }
1277
Mike Reed2179b782018-02-07 11:59:57 -05001278 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279}
1280
Brian Salomon34169692017-08-28 15:32:01 -04001281void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001282 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001283 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001284 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001285 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001286 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001287 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1288 image->alphaType(), nullptr, nullptr,
1289 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001290 return;
Brian Salomon34169692017-08-28 15:32:01 -04001291 }
1292 SkBitmap bm;
1293 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1294 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1295 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001296 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001297 return;
reed85d91782015-09-10 14:33:38 -07001298 }
Brian Salomon34169692017-08-28 15:32:01 -04001299 this->drawBitmap(bm, x, y, paint);
1300 return;
1301 }
1302 if (image->isLazyGenerated()) {
1303 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1304 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1305 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1306 return;
1307 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001308 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001309 GrBitmapTextureMaker maker(fContext.get(), bm);
1310 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1311 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001312 }
1313}
1314
Brian Salomon34169692017-08-28 15:32:01 -04001315void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1316 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001317 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001318 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001319 if (!src || src->contains(image->bounds())) {
1320 constraint = SkCanvas::kFast_SrcRectConstraint;
1321 }
Robert Phillips3798c862017-03-27 11:08:16 -04001322 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001323 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1324 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001325 return;
1326 }
1327 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001328 SkMatrix srcToDstRect;
1329 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1330 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001331 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001332 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001333 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001334 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001335 return;
1336 }
Mike Reeda1361362017-03-07 09:37:29 -05001337 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001338 return;
1339 }
1340 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001341 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001342 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1343 this->ctm(), paint);
1344 return;
1345 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001346 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001347 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001348 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1349 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001350 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001351}
1352
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001353// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1354static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1355 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1356 return GrSamplerState::Filter::kNearest;
1357 }
1358
1359 return GrSamplerState::Filter::kBilerp;
1360}
1361
Mike Reeda1361362017-03-07 09:37:29 -05001362void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001363 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001364 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001365 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001366 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001367 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001368 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001369 image->alphaType(), pinnedUniqueID,
1370 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001371 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001372 } else {
1373 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001374 if (image->isLazyGenerated()) {
1375 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001376 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001377 } else if (as_IB(image)->getROPixels(&bm,
1378 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001379 GrBitmapTextureMaker maker(fContext.get(), bm);
1380 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001381 }
1382 }
1383}
1384
Mike Reeda1361362017-03-07 09:37:29 -05001385void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001386 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001387 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001388 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001389 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001390 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001391}
1392
Mike Reeda1361362017-03-07 09:37:29 -05001393void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001394 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1395 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001396 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001397 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001398
Brian Salomon2a943df2018-05-04 13:43:19 -04001399 bool useFallback = paint->getMaskFilter() || paint->isAntiAlias() ||
1400 GrFSAAType::kUnifiedMSAA == fRenderTargetContext->fsaaType();
1401 if (useFallback) {
1402 SkRect srcR, dstR;
1403 while (iter->next(&srcR, &dstR)) {
1404 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
1405 this->ctm(), *paint);
1406 }
Robert Phillips1c9686b2017-06-30 08:40:28 -04001407 return;
1408 }
Brian Salomon2a943df2018-05-04 13:43:19 -04001409
1410 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1411 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1412 }
msarett10e3d9b2016-08-18 15:46:03 -07001413 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001414 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1415 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001416 return;
1417 }
1418
Brian Salomon2a943df2018-05-04 13:43:19 -04001419 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1420 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
1421 sk_sp<SkColorSpace> proxyColorSpace;
1422 auto proxy =
1423 producer->refTextureProxyForParams(filter, dstColorSpace, &proxyColorSpace, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001424 if (!proxy) {
1425 return;
1426 }
Brian Salomon2a943df2018-05-04 13:43:19 -04001427 auto csxf = GrColorSpaceXform::Make(proxyColorSpace.get(), proxy->config(), dstColorSpace);
1428
Brian Salomon0166cfd2017-03-13 17:58:25 -04001429 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001430 std::move(proxy), std::move(csxf), filter,
1431 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001432}
1433
Mike Reeda1361362017-03-07 09:37:29 -05001434void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001435 const SkCanvas::Lattice& lattice, const SkRect& dst,
1436 const SkPaint& paint) {
1437 ASSERT_SINGLE_OWNER
1438 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001439 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001440 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001441 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001442 image->alphaType(), pinnedUniqueID,
1443 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001444 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001445 } else {
1446 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001447 if (image->isLazyGenerated()) {
1448 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001449 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001450 } else if (as_IB(image)->getROPixels(&bm,
1451 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001452 GrBitmapTextureMaker maker(fContext.get(), bm);
1453 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001454 }
1455 }
1456}
1457
Mike Reeda1361362017-03-07 09:37:29 -05001458void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001459 const SkCanvas::Lattice& lattice, const SkRect& dst,
1460 const SkPaint& paint) {
1461 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001462 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001463 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001464 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001465}
1466
Brian Salomonf3569f02017-10-24 12:52:33 -04001467static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1468 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001469 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001470 if (hasTexs && skPaint.getShader()) {
1471 if (hasColors) {
1472 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001473 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1474 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001475 } else {
1476 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001477 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001478 }
1479 } else {
1480 if (hasColors) {
1481 // We have colors, but either have no shader or no texture coords (which implies that
1482 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001483 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001484 } else {
1485 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001486 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001487 }
1488 }
1489}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001490
Mike Reed887cdf12017-04-03 11:11:09 -04001491void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001492 const SkPoint vertices[], SkBlendMode bmode,
1493 const uint16_t indices[], int indexCount,
1494 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001495 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001496 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001497
Mike Reed2f6b5a42017-03-19 15:04:17 -04001498 SkPaint copy(paint);
1499 copy.setStyle(SkPaint::kStroke_Style);
1500 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001501
Mike Reed2f6b5a42017-03-19 15:04:17 -04001502 GrPaint grPaint;
1503 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001504 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1505 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001506 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507 }
1508
Mike Reed2f6b5a42017-03-19 15:04:17 -04001509 int triangleCount = 0;
1510 int n = (nullptr == indices) ? vertexCount : indexCount;
1511 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001512 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001513 triangleCount = n / 3;
1514 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001515 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001516 triangleCount = n - 2;
1517 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001518 case SkVertices::kTriangleFan_VertexMode:
1519 SK_ABORT("Unexpected triangle fan.");
1520 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001521 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001522
Mike Reed2f6b5a42017-03-19 15:04:17 -04001523 VertState state(vertexCount, indices, indexCount);
1524 VertState::Proc vertProc = state.chooseProc(vmode);
1525
1526 //number of indices for lines per triangle with kLines
1527 indexCount = triangleCount * 6;
1528
Brian Osmanae0c50c2017-05-25 16:56:34 -04001529 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1530 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1531 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1532
1533 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001534 int i = 0;
1535 while (vertProc(&state)) {
1536 lineIndices[i] = state.f0;
1537 lineIndices[i + 1] = state.f1;
1538 lineIndices[i + 2] = state.f1;
1539 lineIndices[i + 3] = state.f2;
1540 lineIndices[i + 4] = state.f2;
1541 lineIndices[i + 5] = state.f0;
1542 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001543 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001544
Chris Dalton3809bab2017-06-13 10:55:06 -06001545 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001546 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001547 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001548 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001549 builder.detach(),
1550 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551}
1552
Brian Osman0b403f82017-05-26 10:39:51 -04001553void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001554 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001555 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001556
1557 SkASSERT(vertices);
1558 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001559 bool hasColors = vertices->hasColors();
1560 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001561 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001562 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001563 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1564 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001565 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001566 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001567 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1568 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001569 return;
1570 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001571 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001572 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001573}
1574
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001575///////////////////////////////////////////////////////////////////////////////
1576
Jim Van Verth3af1af92017-05-18 15:06:54 -04001577void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1578
1579 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001580 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1581
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001582 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001583 // failed to find an accelerated case
1584 this->INHERITED::drawShadow(path, rec);
1585 }
1586}
1587
1588///////////////////////////////////////////////////////////////////////////////
1589
Mike Reeda1361362017-03-07 09:37:29 -05001590void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001591 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001592 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001593 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001594 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001595 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001596 return;
1597 }
1598
Hal Canary144caf52016-11-07 17:57:18 -05001599 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001600
reedca109532015-06-25 16:25:25 -07001601 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001602 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001603
jvanverth31ff7622015-08-07 10:09:28 -07001604 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001605 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001606 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1607 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001608 return;
1609 }
1610 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001611 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1612 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001613 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001614 }
1615 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001616
1617 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001618 fRenderTargetContext->drawAtlas(
1619 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001620}
1621
1622///////////////////////////////////////////////////////////////////////////////
1623
Mike Reeda1361362017-03-07 09:37:29 -05001624void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001625 size_t byteLength, SkScalar x, SkScalar y,
1626 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001627 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001628 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001629 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001630
Brian Salomon0166cfd2017-03-13 17:58:25 -04001631 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1632 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633}
1634
Mike Reeda1361362017-03-07 09:37:29 -05001635void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001636 const SkScalar pos[], int scalarsPerPos,
1637 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001638 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001639 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001640 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001641
Brian Salomon0166cfd2017-03-13 17:58:25 -04001642 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1643 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001644 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001645}
1646
Mike Reeda1361362017-03-07 09:37:29 -05001647void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001648 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001649 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001650 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001651 SkDEBUGCODE(this->validate();)
1652
Brian Salomon0166cfd2017-03-13 17:58:25 -04001653 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1654 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001655}
1656
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657///////////////////////////////////////////////////////////////////////////////
1658
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001660 this->flushAndSignalSemaphores(0, nullptr);
1661}
1662
Greg Daniel51316782017-08-02 15:10:09 +00001663GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1664 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001665 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001666
Greg Danielc64ee462017-06-15 16:59:49 -04001667 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001668}
1669
Greg Danielc64ee462017-06-15 16:59:49 -04001670bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001671 ASSERT_SINGLE_OWNER
1672
Greg Danielc64ee462017-06-15 16:59:49 -04001673 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674}
1675
1676///////////////////////////////////////////////////////////////////////////////
1677
reed76033be2015-03-14 10:54:31 -07001678SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001679 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680
robertphillipsca6eafc2016-05-17 09:57:46 -07001681 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682
robertphillipsca6eafc2016-05-17 09:57:46 -07001683 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001684 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001685 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1686 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001687
Brian Osman10fc6fd2018-03-02 11:01:10 -05001688 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
1689 if (kRGBA_1010102_GrPixelConfig == config) {
1690 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1691 // channel in the layer.
1692 config = kRGBA_8888_GrPixelConfig;
1693 }
1694
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001695 sk_sp<GrRenderTargetContext> rtc(fContext->contextPriv().makeDeferredRenderTargetContext(
Brian Osman10fc6fd2018-03-02 11:01:10 -05001696 fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001697 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001698 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1699 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001700 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001701 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001703
1704 // Skia's convention is to only clear a device if it is non-opaque.
1705 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1706
Robert Phillips9fab7e92016-11-17 12:45:04 -05001707 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1708 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709}
1710
reede8f30622016-03-23 18:59:25 -07001711sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001712 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001713 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001714 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001715 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001716 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001717 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001718}
1719
senorblanco900c3672016-04-27 11:31:23 -07001720SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001721 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001722 // We always return a transient cache, so it is freed after each
1723 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001724 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001725}
reedf037e0b2014-10-30 11:34:15 -07001726