blob: 151ef28c17d69da169a63d82c13199162384569b [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Brian Salomon2a943df2018-05-04 13:43:19 -040010#include "../private/SkShadowFlags.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050011#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070012#include "GrBlurUtils.h"
Brian Osman1cb41712017-10-19 12:54:52 -040013#include "GrColorSpaceXform.h"
kkinnunenabcfab42015-02-22 22:53:44 -080014#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080016#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
18#include "GrRenderTargetContextPriv.h"
Robert Phillips27927a52018-08-20 13:18:12 -040019#include "GrShape.h"
bsalomon6663acf2016-05-10 09:14:17 -070020#include "GrStyle.h"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040021#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050022#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050023#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070024#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080025#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070026#include "SkDraw.h"
bsalomonf276ac52015-10-09 13:36:42 -070027#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070029#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050030#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050031#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070032#include "SkLatticeIter.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040033#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050034#include "SkMaskFilterBase.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000036#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070037#include "SkPictureData.h"
Mike Reed242135a2018-02-22 13:41:39 -050038#include "SkRRectPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050039#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050040#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070042#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000044#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000046#include "SkTLazy.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040047#include "SkTo.h"
Hal Canaryea60b952018-08-21 11:45:46 -040048#include "SkUTF.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000049#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050050#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050051#include "SkWritePixelsRec.h"
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040052#include "SkYUVAIndex.h"
kkinnunenabcfab42015-02-22 22:53:44 -080053#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080054#include "effects/GrSimpleTextureEffect.h"
55#include "effects/GrTextureDomain.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040056#include "text/GrTextTarget.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057
joshualittce894002016-01-11 13:29:31 -080058#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050059SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->priv().singleOwner());)
joshualittce894002016-01-11 13:29:31 -080060
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062///////////////////////////////////////////////////////////////////////////////
63
bsalomon74f681d2015-06-23 14:38:48 -070064/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
65 should fail. */
66bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
67 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
68 *flags = 0;
69 if (info) {
70 switch (info->alphaType()) {
71 case kPremul_SkAlphaType:
72 break;
73 case kOpaque_SkAlphaType:
74 *flags |= SkGpuDevice::kIsOpaque_Flag;
75 break;
76 default: // If it is unpremul or unknown don't try to render
77 return false;
78 }
79 }
80 if (kClear_InitContents == init) {
81 *flags |= kNeedClear_Flag;
82 }
83 return true;
84}
85
Robert Phillips9fab7e92016-11-17 12:45:04 -050086sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
87 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070088 int width, int height,
89 InitContents init) {
Robert Phillipsa9162df2019-02-11 14:12:03 -050090 if (!renderTargetContext || context->abandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070091 return nullptr;
92 }
93 unsigned flags;
94 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
95 return nullptr;
96 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050097 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
98 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000099}
100
robertphillips24e91282016-04-29 06:46:36 -0700101sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
102 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400103 GrSurfaceOrigin origin, const SkSurfaceProps* props,
104 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700105 unsigned flags;
106 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700108 }
109
Brian Osman11052242016-10-27 14:47:55 -0400110 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
111 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400112 origin, props,
113 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400114 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700115 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700116 }
117
Robert Phillips9fab7e92016-11-17 12:45:04 -0500118 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700119 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700120}
121
Brian Osman11052242016-10-27 14:47:55 -0400122static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700123 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400124 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700125 colorType = kUnknown_SkColorType;
126 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400127 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
128 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700129}
130
Robert Phillips9fab7e92016-11-17 12:45:04 -0500131SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
132 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400133 : INHERITED(make_info(renderTargetContext.get(), width, height,
134 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500135 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400136 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700137{
robertphillips1f3923e2016-07-21 07:17:54 -0700138 fSize.set(width, height);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
bsalomone63ffef2016-02-05 07:17:34 -0800140 if (flags & kNeedClear_Flag) {
141 this->clearAll();
142 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143}
144
Brian Osman11052242016-10-27 14:47:55 -0400145sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
146 GrContext* context,
147 SkBudgeted budgeted,
148 const SkImageInfo& origInfo,
149 int sampleCount,
150 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400151 const SkSurfaceProps* surfaceProps,
152 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000153 if (kUnknown_SkColorType == origInfo.colorType() ||
154 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000156 }
157
bsalomonafe30052015-01-16 07:32:33 -0800158 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800160 }
161
Brian Osman2b23c4b2018-06-01 12:25:08 -0400162 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500163 if (kUnknown_GrPixelConfig == config) {
164 return nullptr;
165 }
Greg Daniel4065d452018-11-16 15:43:41 -0500166 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500167 context->priv().caps()->getBackendFormatFromColorType(origInfo.colorType());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400168 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
169 // they need to be exact.
Robert Phillips9da87e02019-02-04 13:26:26 -0500170 return context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500171 format, SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700172 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500173 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400174 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800175}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000176
Mike Reeda1361362017-03-07 09:37:29 -0500177sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700178 int left, int top,
179 SkIPoint* offset,
180 const SkImageFilter* filter) {
181 SkASSERT(srcImg->isTextureBacked());
182 SkASSERT(filter);
183
Mike Reeda1361362017-03-07 09:37:29 -0500184 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700185 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500186 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500187 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -0400188 SkColorType colorType;
189 if (!GrPixelConfigToColorType(fRenderTargetContext->colorSpaceInfo().config(), &colorType)) {
190 colorType = kN32_SkColorType;
191 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400192 SkImageFilter::OutputProperties outputProperties(
Brian Osmana50205f2018-07-06 13:57:01 -0400193 colorType, fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700194 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700195
196 return filter->filterImage(srcImg, ctx, offset);
197}
198
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199///////////////////////////////////////////////////////////////////////////////
200
Mike Reed353196f2017-07-21 11:01:18 -0400201bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800202 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000203
Mike Reed353196f2017-07-21 11:01:18 -0400204 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500205 return false;
206 }
207
Mike Reed353196f2017-07-21 11:01:18 -0400208 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500209 if (!rec.trim(this->width(), this->height())) {
210 return false;
211 }
212
213 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000214}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000215
Mike Reed353196f2017-07-21 11:01:18 -0400216bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800217 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700218
Mike Reed353196f2017-07-21 11:01:18 -0400219 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500220 return false;
221 }
222
Mike Reed353196f2017-07-21 11:01:18 -0400223 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500224 if (!rec.trim(this->width(), this->height())) {
225 return false;
226 }
227
228 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000229}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000230
reed41e010c2015-06-09 12:16:53 -0700231bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800232 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700233 return false;
234}
235
Brian Osman11052242016-10-27 14:47:55 -0400236GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700237 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400238 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700239}
240
reed8eddfb52014-12-04 07:50:14 -0800241void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800242 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500243 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500244
reed8eddfb52014-12-04 07:50:14 -0800245 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman9a9baae2018-11-05 15:06:26 -0500246 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
247 GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800248}
249
Brian Osman11052242016-10-27 14:47:55 -0400250void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800251 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800252
Brian Osman693a5402016-10-27 15:13:22 -0400253 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800254
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400255 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
256 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400257 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400258 this->context(),
259 budgeted,
260 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400261 fRenderTargetContext->numColorSamples(),
262 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400263 &this->surfaceProps(),
264 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400265 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800266 return;
267 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400268 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800269
270 if (shouldRetainContent) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500271 if (this->context()->abandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800272 return;
273 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500274 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800275 }
276
Brian Osman693a5402016-10-27 15:13:22 -0400277 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800278}
279
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000280///////////////////////////////////////////////////////////////////////////////
281
Mike Reeda1361362017-03-07 09:37:29 -0500282void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800283 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500284 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000285
286 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400287 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
288 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700289 return;
290 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000291
Brian Salomon0166cfd2017-03-13 17:58:25 -0400292 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000293}
294
Brian Salomon1459ebd2017-12-19 16:12:44 -0500295static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
296 switch (mode) {
297 case SkCanvas::kPoints_PointMode:
298 return GrPrimitiveType::kPoints;
299 case SkCanvas::kLines_PointMode:
300 return GrPrimitiveType::kLines;
301 case SkCanvas::kPolygon_PointMode:
302 return GrPrimitiveType::kLineStrip;
303 }
304 SK_ABORT("Unexpected mode");
305 return GrPrimitiveType::kPoints;
306}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307
Mike Reeda1361362017-03-07 09:37:29 -0500308void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800310 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500311 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000312 SkScalar width = paint.getStrokeWidth();
313 if (width < 0) {
314 return;
315 }
316
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000317 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700318 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700319 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400320 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
321 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700322 return;
323 }
egdaniele61c4112014-06-12 10:24:21 -0700324 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700325 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700326 path.moveTo(pts[0]);
327 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700328 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
329 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700330 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000331 }
332
bsalomon6ade6dd2016-09-12 12:07:17 -0700333 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500334 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700335 SkScalarNearlyEqual(scales[0], 1.f) &&
336 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700337 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000338 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400339 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500340 SkRasterClip rc(this->devClipBounds());
341 SkDraw draw;
342 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
343 draw.fMatrix = &this->ctm();
344 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500345 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346 return;
347 }
348
Brian Salomon1459ebd2017-12-19 16:12:44 -0500349 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700350
Mike Reeda1361362017-03-07 09:37:29 -0500351 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700352#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
353 // This offsetting in device space matches the expectations of the Android framework for non-AA
354 // points and lines.
355 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600356 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700357 tempMatrix = *viewMatrix;
358 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
359 tempMatrix.postTranslate(kOffset, kOffset);
360 viewMatrix = &tempMatrix;
361 }
362#endif
363
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400365 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
366 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700367 return;
368 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369
Brian Osmanae0c50c2017-05-25 16:56:34 -0400370 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
371 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
372 nullptr);
373
374 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400375 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000376}
377
378///////////////////////////////////////////////////////////////////////////////
379
Mike Reeda1361362017-03-07 09:37:29 -0500380void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800381 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500382 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400383
384 GrStyle style(paint);
385
bsalomona7d85ba2016-07-06 11:54:59 -0700386 // A couple reasons we might need to call drawPath.
387 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400388 GrShape shape(rect, style);
389
390 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
391 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000392 return;
393 }
394
395 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400396 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
397 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700398 return;
399 }
Mike Klein744fb732014-06-23 15:13:26 -0400400
Chris Dalton3b51df12017-11-27 14:33:06 -0700401 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
402 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403}
404
Michael Ludwig75451902019-01-23 11:14:29 -0500405void SkGpuDevice::drawEdgeAARect(const SkRect& r, SkCanvas::QuadAAFlags aa, SkColor color,
406 SkBlendMode mode) {
407 ASSERT_SINGLE_OWNER
408 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawEdgeAARect", fContext.get());
409
410 SkPMColor4f dstColor = SkColor4fPrepForDst(SkColor4f::FromColor(color),
411 fRenderTargetContext->colorSpaceInfo(),
Robert Phillips9da87e02019-02-04 13:26:26 -0500412 *fContext->priv().caps())
Michael Ludwig75451902019-01-23 11:14:29 -0500413 .premul();
414
415 GrPaint grPaint;
416 grPaint.setColor4f(dstColor);
417 if (mode != SkBlendMode::kSrcOver) {
418 grPaint.setXPFactory(SkBlendMode_AsXPFactory(mode));
419 }
420
421 fRenderTargetContext->fillRectWithEdgeAA(this->clip(), std::move(grPaint),
422 SkToGrQuadAAFlags(aa), this->ctm(), r);
423}
424
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425///////////////////////////////////////////////////////////////////////////////
426
Mike Reeda1361362017-03-07 09:37:29 -0500427void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800428 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500429 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400430
Mike Reed80747ef2018-01-23 15:29:32 -0500431 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000432 if (mf) {
433 if (mf->hasFragmentProcessor()) {
434 mf = nullptr; // already handled in SkPaintToGrPaint
435 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400436 }
437
bsalomon6663acf2016-05-10 09:14:17 -0700438 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000439
Robert Phillipsa29a9562016-10-20 09:40:55 -0400440 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800441 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400442 GrShape shape(rrect, style);
443
Robert Phillips27927a52018-08-20 13:18:12 -0400444 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
445 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446 return;
447 }
Mike Klein744fb732014-06-23 15:13:26 -0400448
bsalomon6663acf2016-05-10 09:14:17 -0700449 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800450
Robert Phillipsa522d662018-08-23 13:50:16 -0400451 GrPaint grPaint;
452 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
453 this->ctm(), &grPaint)) {
454 return;
455 }
456
Chris Dalton3b51df12017-11-27 14:33:06 -0700457 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
458 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000459}
460
robertphillipsd7706102016-02-25 09:28:08 -0800461
Robert Phillips20390c32018-08-17 11:01:03 -0400462void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800463 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500464 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800465 if (outer.isEmpty()) {
466 return;
467 }
468
469 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500470 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800471 }
472
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000473 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000474
robertphillips0e7029e2015-11-30 05:45:06 -0800475 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800476 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400477 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
478 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700479 return;
480 }
robertphillips00095892016-02-29 13:50:40 -0800481
Brian Salomon0166cfd2017-03-13 17:58:25 -0400482 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700483 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800484 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000485 }
486
487 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700488 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000489 path.addRRect(outer);
490 path.addRRect(inner);
491 path.setFillType(SkPath::kEvenOdd_FillType);
492
Robert Phillips27927a52018-08-20 13:18:12 -0400493 // TODO: We are losing the possible mutability of the path here but this should probably be
494 // fixed by upgrading GrShape to handle DRRects.
495 GrShape shape(path, paint);
496
497 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
498 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000499}
500
501
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000502/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503
Mike Reeda1361362017-03-07 09:37:29 -0500504void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700505 if (paint.getMaskFilter()) {
506 SkPath path;
507 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400508 path.setIsVolatile(true);
509 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700510 }
511
512 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400513 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
514 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700515 return;
516 }
517
Chris Dalton3b51df12017-11-27 14:33:06 -0700518 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
519 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700520}
521
Mike Reeda1361362017-03-07 09:37:29 -0500522void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800523 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500524 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800525
robertphillips514450c2015-11-24 05:36:02 -0800526 if (paint.getMaskFilter()) {
527 // The RRect path can handle special case blurring
528 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500529 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000530 }
531
532 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400533 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
534 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700535 return;
536 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537
Chris Dalton3b51df12017-11-27 14:33:06 -0700538 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
539 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540}
541
Mike Reeda1361362017-03-07 09:37:29 -0500542void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700543 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
544 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500545 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700546 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500547 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700548 return;
549 }
550 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400551 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
552 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700553 return;
554 }
555
Chris Dalton3b51df12017-11-27 14:33:06 -0700556 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500557 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500558 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700559}
560
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562
563///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700564void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700565 const SkPaint& origPaint) {
566 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500567 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400568 // Adding support for round capping would require a
569 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700570 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
571 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
572 SkASSERT(!origPaint.getPathEffect());
573 SkASSERT(!origPaint.getMaskFilter());
574
575 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
576 SkASSERT(halfWidth > 0);
577
578 SkVector v = points[1] - points[0];
579
580 SkScalar length = SkPoint::Normalize(&v);
581 if (!length) {
582 v.fX = 1.0f;
583 v.fY = 0.0f;
584 }
585
586 SkPaint newPaint(origPaint);
587 newPaint.setStyle(SkPaint::kFill_Style);
588
589 SkScalar xtraLength = 0.0f;
590 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
591 xtraLength = halfWidth;
592 }
593
594 SkPoint mid = points[0] + points[1];
595 mid.scale(0.5f);
596
597 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
598 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
599 SkMatrix m;
600 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
601
602 SkMatrix local = m;
603
Mike Reeda1361362017-03-07 09:37:29 -0500604 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700605
606 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400607 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
608 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700609 return;
610 }
611
Brian Salomon82f44312017-01-11 13:42:54 -0500612 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700613 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700614}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000615
Robert Phillips20390c32018-08-17 11:01:03 -0400616void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800617 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400618 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700619 SkPoint points[2];
620 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
621 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500622 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700623 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500624 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700625 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500626 // Round capping support is currently disabled b.c. it would require a RRect
627 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500628 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700629 return;
630 }
631 }
robertphillipsff55b492015-11-24 07:56:59 -0800632 }
633
Hal Canary144caf52016-11-07 17:57:18 -0500634 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400635 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500636 GrPaint grPaint;
637 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
638 this->ctm(), &grPaint)) {
639 return;
640 }
641 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
642 this->ctm(), origSrcPath, GrStyle(paint));
643 return;
644 }
Robert Phillips27927a52018-08-20 13:18:12 -0400645
646 // TODO: losing possible mutability of 'origSrcPath' here
647 GrShape shape(origSrcPath, paint);
648
649 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
650 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651}
652
653static const int kBmpSmallTileSize = 1 << 10;
654
655static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
656 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
657 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
658 return tilesX * tilesY;
659}
660
reed85d91782015-09-10 14:33:38 -0700661static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 if (maxTileSize <= kBmpSmallTileSize) {
663 return maxTileSize;
664 }
665
666 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
667 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
668
669 maxTileTotalTileSize *= maxTileSize * maxTileSize;
670 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
671
672 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
673 return kBmpSmallTileSize;
674 } else {
675 return maxTileSize;
676 }
677}
678
679// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
680// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700681static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800682 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800683 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700684 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700685 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686 const SkRect* srcRectPtr,
687 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700688 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700689 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
690 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691 clippedSrcIRect->setEmpty();
692 return;
693 }
694 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
695 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700696 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 if (!clippedSrcRect.intersect(*srcRectPtr)) {
698 clippedSrcIRect->setEmpty();
699 return;
700 }
701 }
702 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700703 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 if (!clippedSrcIRect->intersect(bmpBounds)) {
705 clippedSrcIRect->setEmpty();
706 }
707}
708
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400709bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
710 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700711 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700712 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400713 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700714 const SkRect* srcRectPtr,
715 int maxTileSize,
716 int* tileSize,
717 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800718 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700719 // if it's larger than the max tile size, then we have no choice but tiling.
720 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400721 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400722 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
723 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700724 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
725 return true;
726 }
727
bsalomon1a1d0b82015-10-16 07:49:42 -0700728 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700729 const size_t area = imageRect.width() * imageRect.height();
730 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
731 return false;
732 }
733
reed85d91782015-09-10 14:33:38 -0700734 // At this point we know we could do the draw by uploading the entire bitmap
735 // as a texture. However, if the texture would be large compared to the
736 // cache size and we don't require most of it for this draw then tile to
737 // reduce the amount of upload and cache spill.
738
739 // assumption here is that sw bitmap size is a good proxy for its size as
740 // a texture
741 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
742 size_t cacheSize;
743 fContext->getResourceCacheLimits(nullptr, &cacheSize);
744 if (bmpSize < cacheSize / 2) {
745 return false;
746 }
747
bsalomon1a1d0b82015-10-16 07:49:42 -0700748 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
749 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400750 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
751 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400752 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700753 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
754 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500755 kBmpSmallTileSize * kBmpSmallTileSize *
756 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700757
Brian Osmand05cdc32017-02-06 13:24:47 -0500758 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700759}
760
reed85d91782015-09-10 14:33:38 -0700761bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
762 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700763 const SkMatrix& viewMatrix,
764 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800765 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400766 // If image is explicitly texture backed then we shouldn't get here.
767 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400769 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700770 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500771 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500772 quality, viewMatrix, srcToDstRect,
Robert Phillips9da87e02019-02-04 13:26:26 -0500773 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700774
775 int tileFilterPad;
776 if (doBicubic) {
777 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400778 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700779 tileFilterPad = 0;
780 } else {
781 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400783 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400785 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
reed85d91782015-09-10 14:33:38 -0700787 // these are output, which we safely ignore, as we just want to know the predicate
788 int outTileSize;
789 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790
bsalomone553b642016-08-17 09:02:09 -0700791 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400792 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700793 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794}
795
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000796// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
798// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000799static inline void clamped_outset_with_offset(SkIRect* iRect,
800 int outset,
801 SkPoint* offset,
802 const SkIRect& clamp) {
803 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000804
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000805 int leftClampDelta = clamp.fLeft - iRect->fLeft;
806 if (leftClampDelta > 0) {
807 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808 iRect->fLeft = clamp.fLeft;
809 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000810 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000812
813 int topClampDelta = clamp.fTop - iRect->fTop;
814 if (topClampDelta > 0) {
815 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000816 iRect->fTop = clamp.fTop;
817 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000818 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000819 }
820
821 if (iRect->fRight > clamp.fRight) {
822 iRect->fRight = clamp.fRight;
823 }
824 if (iRect->fBottom > clamp.fBottom) {
825 iRect->fBottom = clamp.fBottom;
826 }
827}
828
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829// Break 'bitmap' into several tiles to draw it since it has already
830// been determined to be too large to fit in VRAM
831void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800832 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700833 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000834 const SkRect& srcRect,
835 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400836 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800837 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700838 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000839 int tileSize,
840 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800841 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800842
ericrk983294f2016-04-18 09:14:00 -0700843 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800844 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700845 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800846
bsalomonc55271f2015-11-09 11:55:57 -0800847 const SkPaint* paint = &origPaint;
848 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400849 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800850 // Drop antialiasing to avoid seams at tile boundaries.
851 tempPaint = origPaint;
852 tempPaint.setAntiAlias(false);
853 paint = &tempPaint;
854 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
856
857 int nx = bitmap.width() / tileSize;
858 int ny = bitmap.height() / tileSize;
859 for (int x = 0; x <= nx; x++) {
860 for (int y = 0; y <= ny; y++) {
861 SkRect tileR;
862 tileR.set(SkIntToScalar(x * tileSize),
863 SkIntToScalar(y * tileSize),
864 SkIntToScalar((x + 1) * tileSize),
865 SkIntToScalar((y + 1) * tileSize));
866
867 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
868 continue;
869 }
870
871 if (!tileR.intersect(srcRect)) {
872 continue;
873 }
874
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 SkIRect iTileR;
876 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700877 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
878 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500879 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700880 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400881 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882 SkIRect iClampRect;
883
reeda5517e22015-07-14 10:54:12 -0700884 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885 // In bleed mode we want to always expand the tile on all edges
886 // but stay within the bitmap bounds
887 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
888 } else {
889 // In texture-domain/clamp mode we only want to expand the
890 // tile on edges interior to "srcRect" (i.e., we want to
891 // not bleed across the original clamped edges)
892 srcRect.roundOut(&iClampRect);
893 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000894 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
895 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000896 }
897
bsalomone553b642016-08-17 09:02:09 -0700898 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000899 if (bitmap.extractSubset(&tmpB, iTileR)) {
900 // now offset it to make it "local" to our tmp bitmap
901 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800902 // de-optimized this determination
903 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700904 this->drawBitmapTile(tmpB,
905 viewMatrix,
906 rectToDraw,
907 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500908 params,
bsalomone553b642016-08-17 09:02:09 -0700909 *paint,
910 constraint,
911 bicubic,
912 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913 }
914 }
915 }
916}
917
bsalomone553b642016-08-17 09:02:09 -0700918void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
919 const SkMatrix& viewMatrix,
920 const SkRect& dstRect,
921 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400922 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700923 const SkPaint& paint,
924 SkCanvas::SrcRectConstraint constraint,
925 bool bicubic,
926 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800927 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400928 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
929 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700930 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400931 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
932 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400933 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934
Brian Salomon2a943df2018-05-04 13:43:19 -0400935 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400936 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400937 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400938 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939 return;
940 }
bsalomone553b642016-08-17 09:02:09 -0700941
bsalomone553b642016-08-17 09:02:09 -0700942 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400943 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
944 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700945
946 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
947 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400948 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700949
reeda5517e22015-07-14 10:54:12 -0700950 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700952 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500954 domain.fLeft = srcRect.fLeft + 0.5f;
955 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500957 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 }
959 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500960 domain.fTop = srcRect.fTop + 0.5f;
961 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500963 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000964 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000965 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -0400966 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000967 } else {
Brian Osman2240be92017-10-18 13:15:13 -0400968 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -0400969 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000970 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000971 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400972 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
973 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -0400974 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 } else {
Brian Osman2240be92017-10-18 13:15:13 -0400976 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 }
978
Brian Osman21fc5ce2018-08-27 20:36:19 +0000979 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400980 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -0800981 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400982 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
983 viewMatrix, std::move(fp),
984 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700985 return;
986 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500988 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -0700989 GrAA aa = GrAA(paint.isAntiAlias() &&
990 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -0400991 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992}
993
Mike Reeda1361362017-03-07 09:37:29 -0500994void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800996 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500997 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998
Khushalc421ca12018-06-26 14:38:34 -0700999 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000 return;
1001 }
1002
Robert Phillipse14d3052017-02-15 13:18:21 -05001003 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1004 if (!srcImg) {
1005 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001006 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001007
Florin Malita53f77bd2017-04-28 13:48:37 -04001008 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001009}
1010
1011
Robert Phillipsc100d482018-07-10 10:11:01 -04001012void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001013 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001014 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001015 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001016
Florin Malita53f77bd2017-04-28 13:48:37 -04001017 // TODO: clipImage support.
1018
robertphillips970587b2016-07-14 14:12:55 -07001019 sk_sp<SkSpecialImage> result;
1020 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001021 SkIPoint offset = { 0, 0 };
1022
1023 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001024 if (!result) {
1025 return;
1026 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001027
1028 left += offset.fX;
1029 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001030 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001031 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001032 }
1033
1034 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001035 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001036 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001037 return;
1038 }
robertphillips970587b2016-07-14 14:12:55 -07001039
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001040 const GrPixelConfig config = proxy->config();
1041
robertphillips970587b2016-07-14 14:12:55 -07001042 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001043 if (tmpUnfiltered.getMaskFilter()) {
1044 SkMatrix ctm = this->ctm();
Robert Phillipsc100d482018-07-10 10:11:01 -04001045 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001046 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001047 }
1048
robertphillips970587b2016-07-14 14:12:55 -07001049 tmpUnfiltered.setImageFilter(nullptr);
1050
Brian Osman2240be92017-10-18 13:15:13 -04001051 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001052 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001053 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001054 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001055 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001056 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001057 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001058 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001059
1060 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001061 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1062 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001063 return;
1064 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065
robertphillips970587b2016-07-14 14:12:55 -07001066 const SkIRect& subset = result->subset();
1067
Brian Salomon82f44312017-01-11 13:42:54 -05001068 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001069 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001070 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001071 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001072 SkMatrix::I(),
Robert Phillipsc100d482018-07-10 10:11:01 -04001073 SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001074 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075}
1076
Mike Reeda1361362017-03-07 09:37:29 -05001077void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001078 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001079 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001080 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001081 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1082 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1083 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1084 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1085 const SkRect* dst = &origDst;
1086 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001088 if (!src) {
1089 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001091
bsalomonb1b01992015-11-18 10:56:08 -08001092 SkMatrix srcToDstMatrix;
1093 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1094 return;
1095 }
1096 SkRect tmpSrc, tmpDst;
1097 if (src != &bmpBounds) {
1098 if (!bmpBounds.contains(*src)) {
1099 tmpSrc = *src;
1100 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 return; // nothing to draw
1102 }
bsalomonb1b01992015-11-18 10:56:08 -08001103 src = &tmpSrc;
1104 srcToDstMatrix.mapRect(&tmpDst, *src);
1105 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106 }
1107 }
1108
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001109 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001110
bsalomonb1b01992015-11-18 10:56:08 -08001111 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1112 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001113 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1114 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1115 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001116
Brian Salomon7c8460e2017-05-12 11:36:10 -04001117 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001118
1119 if (!skipTileCheck) {
1120 int tileSize;
1121 SkIRect clippedSrcRect;
1122
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001123 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001124 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001125 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001126 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
Robert Phillips9da87e02019-02-04 13:26:26 -05001127 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001128
1129 int tileFilterPad;
1130
1131 if (doBicubic) {
1132 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001133 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001134 tileFilterPad = 0;
1135 } else {
1136 tileFilterPad = 1;
1137 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001138 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001139
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001140 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001141 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001142 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1143 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001144 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001145 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001146 return;
1147 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001148 }
Hal Canary144caf52016-11-07 17:57:18 -05001149 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001150 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151}
1152
robertphillips6451a0c2016-07-18 08:31:31 -07001153sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001154 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1155 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips9da87e02019-02-04 13:26:26 -05001156 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->priv().proxyProvider(),
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001157 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001158 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001159 return nullptr;
1160 }
1161
Robert Phillipse14d3052017-02-15 13:18:21 -05001162 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001163
Robert Phillipse14d3052017-02-15 13:18:21 -05001164 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1165 // the special image
1166 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1167 rect,
1168 bitmap.getGenerationID(),
1169 std::move(proxy),
1170 bitmap.refColorSpace(),
1171 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001172}
1173
reede51c3562016-07-19 14:33:20 -07001174sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001175 SkPixmap pm;
1176 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001177 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001178
Robert Phillips6de99042017-01-31 11:31:39 -05001179 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1180 SkIRect::MakeWH(image->width(), image->height()),
1181 image->uniqueID(),
1182 std::move(proxy),
1183 as_IB(image)->onImageInfo().refColorSpace(),
1184 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001185 } else if (image->peekPixels(&pm)) {
1186 SkBitmap bm;
1187
1188 bm.installPixels(pm);
1189 return this->makeSpecial(bm);
1190 } else {
1191 return nullptr;
1192 }
1193}
1194
1195sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Greg Danielbe7fc462019-01-03 16:40:42 -05001196 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1197 // since it would require us to make a copy of the underlying VkImage which we don't have access
1198 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1199 // command buffer.
1200 if (this->accessRenderTargetContext()->wrapsVkSecondaryCB()) {
1201 return nullptr;
1202 }
1203
Robert Phillips63c67462017-02-15 14:19:01 -05001204 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1205 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001206 // When the device doesn't have a texture, we create a temporary texture.
1207 // TODO: we should actually only copy the portion of the source needed to apply the image
1208 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001209 proxy = GrSurfaceProxy::Copy(fContext.get(),
1210 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001211 GrMipMapped::kNo,
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001212 SkBackingFit::kApprox,
Robert Phillips63c67462017-02-15 14:19:01 -05001213 SkBudgeted::kYes);
1214 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001215 return nullptr;
1216 }
robertphillips1b5f9682016-07-15 08:01:12 -07001217 }
1218
1219 const SkImageInfo ii = this->imageInfo();
1220 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1221
Robert Phillipse2f7d182016-12-15 09:23:05 -05001222 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1223 srcRect,
1224 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001225 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001226 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001227 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001228}
1229
Mike Reed148b7fd2018-12-18 17:38:18 -05001230sk_sp<SkSpecialImage> SkGpuDevice::snapBackImage(const SkIRect& subset) {
1231 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
Greg Danielbe7fc462019-01-03 16:40:42 -05001232
1233 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1234 // since it would require us to make a copy of the underlying VkImage which we don't have access
1235 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1236 // command buffer.
1237 if (rtc->wrapsVkSecondaryCB()) {
Mike Reed148b7fd2018-12-18 17:38:18 -05001238 return nullptr;
1239 }
1240
Greg Danielbe7fc462019-01-03 16:40:42 -05001241
Mike Reed148b7fd2018-12-18 17:38:18 -05001242 GrContext* ctx = this->context();
Greg Danielbe7fc462019-01-03 16:40:42 -05001243 SkASSERT(rtc->asSurfaceProxy());
Mike Reed148b7fd2018-12-18 17:38:18 -05001244
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001245 auto srcProxy =
1246 GrSurfaceProxy::Copy(ctx, rtc->asSurfaceProxy(), rtc->mipMapped(), subset,
1247 SkBackingFit::kApprox, rtc->asSurfaceProxy()->isBudgeted());
Mike Reed148b7fd2018-12-18 17:38:18 -05001248 if (!srcProxy) {
1249 return nullptr;
1250 }
1251
1252 // Note, can't move srcProxy since we also refer to this in the 2nd parameter
1253 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1254 SkIRect::MakeSize(srcProxy->isize()),
1255 kNeedNewImageUniqueID_SpecialImage,
1256 srcProxy,
1257 this->imageInfo().refColorSpace(),
1258 &this->surfaceProps());
1259}
1260
Mike Reeda1361362017-03-07 09:37:29 -05001261void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001262 int left, int top, const SkPaint& paint) {
1263 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001264
joshualittce894002016-01-11 13:29:31 -08001265 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001267 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001268
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001270 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001271 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001272 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 return;
1274 }
1275
Mike Reed2179b782018-02-07 11:59:57 -05001276 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277}
1278
Brian Salomon34169692017-08-28 15:32:01 -04001279void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1280 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001281 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -04001282 if (!src || src->contains(image->bounds())) {
1283 constraint = SkCanvas::kFast_SrcRectConstraint;
1284 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001285 if (as_IB(image)->isYUVA()) {
1286 GrYUVAImageTextureMaker maker(fContext.get(), image);
1287 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, false);
1288 return;
1289 }
1290 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001291 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001292 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1293 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001294 return;
1295 }
1296 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001297 SkMatrix srcToDstRect;
1298 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1299 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001300 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001301 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001302 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001303 if (!as_IB(image)->getROPixels(&bm)) {
bsalomonc55271f2015-11-09 11:55:57 -08001304 return;
1305 }
Mike Reeda1361362017-03-07 09:37:29 -05001306 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001307 return;
1308 }
1309 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001310 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001311 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, true);
Brian Salomon34169692017-08-28 15:32:01 -04001312 return;
1313 }
Brian Osmane50cdf02018-10-19 13:02:14 -04001314 if (as_IB(image)->getROPixels(&bm)) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001315 GrBitmapTextureMaker maker(fContext.get(), bm);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001316 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, true);
reeda85d4d02015-05-06 12:56:48 -07001317 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001318}
1319
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001320// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1321static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1322 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1323 return GrSamplerState::Filter::kNearest;
1324 }
1325
1326 return GrSamplerState::Filter::kBilerp;
1327}
1328
Mike Reeda1361362017-03-07 09:37:29 -05001329void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001330 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001331 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001332 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001333 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001334 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001335 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001336 image->alphaType(), pinnedUniqueID,
1337 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001338 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001339 } else {
1340 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001341 if (image->isLazyGenerated()) {
1342 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001343 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001344 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001345 GrBitmapTextureMaker maker(fContext.get(), bm);
1346 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001347 }
1348 }
1349}
1350
Mike Reeda1361362017-03-07 09:37:29 -05001351void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001352 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001353 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001354 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001355 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001356 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001357}
1358
Mike Reeda1361362017-03-07 09:37:29 -05001359void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001360 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1361 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001362 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001363 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001364
Brian Salomon2a943df2018-05-04 13:43:19 -04001365 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1366 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1367 }
msarett10e3d9b2016-08-18 15:46:03 -07001368 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001369 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1370 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001371 return;
1372 }
1373
Brian Salomon2a943df2018-05-04 13:43:19 -04001374 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1375 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Brian Osman6064e1c2018-10-19 14:27:54 -04001376 auto proxy = producer->refTextureProxyForParams(filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001377 if (!proxy) {
1378 return;
1379 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001380 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1381 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001382
Brian Salomon0166cfd2017-03-13 17:58:25 -04001383 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001384 std::move(proxy), std::move(csxf), filter,
1385 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001386}
1387
Mike Reeda1361362017-03-07 09:37:29 -05001388void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001389 const SkCanvas::Lattice& lattice, const SkRect& dst,
1390 const SkPaint& paint) {
1391 ASSERT_SINGLE_OWNER
1392 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001393 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001394 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001395 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001396 image->alphaType(), pinnedUniqueID,
1397 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001398 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001399 } else {
1400 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001401 if (image->isLazyGenerated()) {
1402 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001403 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001404 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001405 GrBitmapTextureMaker maker(fContext.get(), bm);
1406 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001407 }
1408 }
1409}
1410
Mike Reeda1361362017-03-07 09:37:29 -05001411void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001412 const SkCanvas::Lattice& lattice, const SkRect& dst,
1413 const SkPaint& paint) {
1414 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001415 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001416 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001417 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001418}
1419
Brian Salomond003d222018-11-26 13:25:05 -05001420void SkGpuDevice::drawImageSet(const SkCanvas::ImageSetEntry set[], int count,
Brian Salomond7065e72018-10-12 11:42:02 -04001421 SkFilterQuality filterQuality, SkBlendMode mode) {
1422 SkASSERT(count > 0);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001423
Brian Salomond7065e72018-10-12 11:42:02 -04001424 GrSamplerState sampler;
1425 sampler.setFilterMode(kNone_SkFilterQuality == filterQuality ? GrSamplerState::Filter::kNearest
1426 : GrSamplerState::Filter::kBilerp);
1427 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
Brian Salomond7065e72018-10-12 11:42:02 -04001428 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
1429 // issue the accumulated 'n' draws starting at 'base'.
1430 int base = 0, n = 0;
1431 auto draw = [&] {
1432 if (n > 0) {
1433 auto textureXform = GrColorSpaceXform::Make(
1434 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
1435 fRenderTargetContext->colorSpaceInfo().colorSpace(), kPremul_SkAlphaType);
1436 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001437 sampler.filter(), mode, this->ctm(),
Brian Osman3d139a42018-11-19 10:42:10 -05001438 std::move(textureXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001439 }
1440 };
1441 for (int i = 0; i < count; ++i) {
Brian Salomon0087c832018-10-15 14:48:20 -04001442 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
1443 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
1444 if (!set[i].fSrcRect.isSorted()) {
1445 draw();
1446 base = i + 1;
1447 n = 0;
1448 continue;
1449 }
Brian Salomonbf612c62018-10-22 09:55:12 -04001450 uint32_t uniqueID;
1451 textures[i].fProxy = as_IB(set[i].fImage.get())->refPinnedTextureProxy(&uniqueID);
1452 if (!textures[i].fProxy) {
1453 textures[i].fProxy =
1454 as_IB(set[i].fImage.get())
1455 ->asTextureProxyRef(fContext.get(), GrSamplerState::ClampBilerp(),
1456 nullptr);
1457 // If we failed to make a proxy then flush the accumulated set and reset for the next
1458 // image.
1459 if (!textures[i].fProxy) {
1460 draw();
1461 base = i + 1;
1462 n = 0;
1463 continue;
1464 }
1465 }
Brian Salomond7065e72018-10-12 11:42:02 -04001466 textures[i].fSrcRect = set[i].fSrcRect;
1467 textures[i].fDstRect = set[i].fDstRect;
Brian Salomon1da5cad2018-11-21 09:21:18 -05001468 textures[i].fAlpha = set[i].fAlpha;
Brian Salomond7065e72018-10-12 11:42:02 -04001469 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
Brian Salomon0087c832018-10-15 14:48:20 -04001470 if (n > 0 &&
Greg Daniel45723ac2018-11-30 10:12:43 -05001471 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(textures[i].fProxy.get(),
1472 textures[base].fProxy.get()) ||
Brian Salomon0087c832018-10-15 14:48:20 -04001473 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
1474 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Brian Salomond7065e72018-10-12 11:42:02 -04001475 draw();
1476 base = i;
1477 n = 1;
1478 } else {
1479 ++n;
1480 }
1481 }
1482 draw();
1483}
1484
Brian Salomonf3569f02017-10-24 12:52:33 -04001485static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1486 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001487 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001488 if (hasTexs && skPaint.getShader()) {
1489 if (hasColors) {
1490 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001491 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1492 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001493 } else {
1494 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001495 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001496 }
1497 } else {
1498 if (hasColors) {
1499 // We have colors, but either have no shader or no texture coords (which implies that
1500 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001501 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001502 } else {
1503 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001504 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001505 }
1506 }
1507}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508
Mike Reed887cdf12017-04-03 11:11:09 -04001509void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001510 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001511 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001512 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001513 const uint16_t indices[], int indexCount,
1514 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001515 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001516 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001517
Mike Reed2f6b5a42017-03-19 15:04:17 -04001518 SkPaint copy(paint);
1519 copy.setStyle(SkPaint::kStroke_Style);
1520 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001521
Mike Reed2f6b5a42017-03-19 15:04:17 -04001522 GrPaint grPaint;
1523 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001524 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1525 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001526 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001527 }
1528
Mike Reed2f6b5a42017-03-19 15:04:17 -04001529 int triangleCount = 0;
1530 int n = (nullptr == indices) ? vertexCount : indexCount;
1531 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001532 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001533 triangleCount = n / 3;
1534 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001535 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001536 triangleCount = n - 2;
1537 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001538 case SkVertices::kTriangleFan_VertexMode:
1539 SK_ABORT("Unexpected triangle fan.");
1540 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001541 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542
Mike Reed2f6b5a42017-03-19 15:04:17 -04001543 VertState state(vertexCount, indices, indexCount);
1544 VertState::Proc vertProc = state.chooseProc(vmode);
1545
1546 //number of indices for lines per triangle with kLines
1547 indexCount = triangleCount * 6;
1548
Brian Osmanae0c50c2017-05-25 16:56:34 -04001549 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1550 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1551 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1552
1553 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001554 int i = 0;
1555 while (vertProc(&state)) {
1556 lineIndices[i] = state.f0;
1557 lineIndices[i + 1] = state.f1;
1558 lineIndices[i + 2] = state.f1;
1559 lineIndices[i + 3] = state.f2;
1560 lineIndices[i + 4] = state.f2;
1561 lineIndices[i + 5] = state.f0;
1562 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001563 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001564
Chris Dalton3809bab2017-06-13 10:55:06 -06001565 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001566 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001567 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001568 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001569 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001570 bones,
1571 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001572 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573}
1574
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001575void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1576 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001577 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001578 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001579
1580 SkASSERT(vertices);
1581 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001582 bool hasColors = vertices->hasColors();
1583 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001584 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001585 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001586 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001587 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1588 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001589 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001590 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001591 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1592 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001593 return;
1594 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001595 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001596 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1597 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001598}
1599
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600///////////////////////////////////////////////////////////////////////////////
1601
Jim Van Verth3af1af92017-05-18 15:06:54 -04001602void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1603
1604 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001605 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1606
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001607 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001608 // failed to find an accelerated case
1609 this->INHERITED::drawShadow(path, rec);
1610 }
1611}
1612
1613///////////////////////////////////////////////////////////////////////////////
1614
Mike Reeda1361362017-03-07 09:37:29 -05001615void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001616 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001617 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001618 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001619 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001620 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001621 return;
1622 }
1623
Hal Canary144caf52016-11-07 17:57:18 -05001624 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001625
reedca109532015-06-25 16:25:25 -07001626 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001627 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001628
jvanverth31ff7622015-08-07 10:09:28 -07001629 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001630 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001631 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1632 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001633 return;
1634 }
1635 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001636 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1637 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001638 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001639 }
1640 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001641
Brian Salomon0166cfd2017-03-13 17:58:25 -04001642 fRenderTargetContext->drawAtlas(
1643 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001644}
1645
1646///////////////////////////////////////////////////////////////////////////////
1647
Herb Derbyb935cf82018-07-26 16:54:18 -04001648void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001649 ASSERT_SINGLE_OWNER
1650 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
Herb Derbyb983e6b2018-07-13 13:26:29 -04001651
Jim Van Verth87a30112018-09-24 16:13:58 -04001652 // Check for valid input
1653 const SkMatrix& ctm = this->ctm();
Mike Reed96345a22019-01-02 21:30:29 -05001654 if (!ctm.isFinite() || !glyphRunList.allFontsFinite()) {
Jim Van Verth87a30112018-09-24 16:13:58 -04001655 return;
1656 }
1657
1658 fRenderTargetContext->drawGlyphRunList(this->clip(), ctm, glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001659}
1660
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661///////////////////////////////////////////////////////////////////////////////
1662
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001663void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
Robert Phillips4217ea72019-01-30 13:08:28 -05001664 GrBackendApi api = this->context()->backend();
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001665 if (GrBackendApi::kVulkan == api) {
1666 const SkMatrix& ctm = canvas->getTotalMatrix();
1667 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1668 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -05001669 drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(),
1670 this->imageInfo());
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001671 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001672 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1673 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001674 }
1675 }
1676 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1677}
1678
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001679
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001680///////////////////////////////////////////////////////////////////////////////
1681
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001683 this->flushAndSignalSemaphores(0, nullptr);
1684}
1685
Greg Daniel51316782017-08-02 15:10:09 +00001686GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1687 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001688 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001689
Greg Danielc64ee462017-06-15 16:59:49 -04001690 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001691}
1692
Greg Danielc64ee462017-06-15 16:59:49 -04001693bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001694 ASSERT_SINGLE_OWNER
1695
Greg Danielc64ee462017-06-15 16:59:49 -04001696 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001697}
1698
1699///////////////////////////////////////////////////////////////////////////////
1700
reed76033be2015-03-14 10:54:31 -07001701SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001702 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703
robertphillipsca6eafc2016-05-17 09:57:46 -07001704 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001705
robertphillipsca6eafc2016-05-17 09:57:46 -07001706 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001707 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001708 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1709 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001710
Brian Osman10fc6fd2018-03-02 11:01:10 -05001711 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
Greg Daniel4065d452018-11-16 15:43:41 -05001712 const GrBackendFormat& origFormat = fRenderTargetContext->asSurfaceProxy()->backendFormat();
1713 GrBackendFormat format = origFormat.makeTexture2D();
1714 if (!format.isValid()) {
1715 return nullptr;
1716 }
Brian Osman10fc6fd2018-03-02 11:01:10 -05001717 if (kRGBA_1010102_GrPixelConfig == config) {
1718 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1719 // channel in the layer.
1720 config = kRGBA_8888_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -05001721 format =
Robert Phillips9da87e02019-02-04 13:26:26 -05001722 fContext->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Osman10fc6fd2018-03-02 11:01:10 -05001723 }
1724
Robert Phillips9da87e02019-02-04 13:26:26 -05001725 sk_sp<GrRenderTargetContext> rtc(fContext->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001726 format, fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001727 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001728 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1729 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001730 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001731 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001733
1734 // Skia's convention is to only clear a device if it is non-opaque.
1735 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1736
Robert Phillips9fab7e92016-11-17 12:45:04 -05001737 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1738 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739}
1740
reede8f30622016-03-23 18:59:25 -07001741sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001742 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001743 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001744 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001745 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001746 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001747 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001748}
1749
senorblanco900c3672016-04-27 11:31:23 -07001750SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001751 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001752 // We always return a transient cache, so it is freed after each
1753 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001754 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001755}
reedf037e0b2014-10-30 11:34:15 -07001756