blob: 1002053397c0ae420a5e2e3e675fcc807d617e3f [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"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070028#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070030#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050031#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050032#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070033#include "SkLatticeIter.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040034#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050035#include "SkMaskFilterBase.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000037#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070038#include "SkPictureData.h"
Mike Reed242135a2018-02-22 13:41:39 -050039#include "SkRRectPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050040#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050041#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080042#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070043#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000045#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080046#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000047#include "SkTLazy.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include "SkTo.h"
Hal Canaryea60b952018-08-21 11:45:46 -040049#include "SkUTF.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000050#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050051#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050052#include "SkWritePixelsRec.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 Phillips0c4b7b12018-03-06 08:20:37 -050059SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->contextPriv().debugSingleOwner());)
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) {
Brian Osman11052242016-10-27 14:47:55 -040090 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
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 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400166 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
167 // they need to be exact.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500168 return context->contextPriv().makeDeferredRenderTargetContext(
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400169 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700170 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500171 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400172 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800173}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000174
Mike Reeda1361362017-03-07 09:37:29 -0500175sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700176 int left, int top,
177 SkIPoint* offset,
178 const SkImageFilter* filter) {
179 SkASSERT(srcImg->isTextureBacked());
180 SkASSERT(filter);
181
Mike Reeda1361362017-03-07 09:37:29 -0500182 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700183 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500184 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500185 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -0400186 SkColorType colorType;
187 if (!GrPixelConfigToColorType(fRenderTargetContext->colorSpaceInfo().config(), &colorType)) {
188 colorType = kN32_SkColorType;
189 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400190 SkImageFilter::OutputProperties outputProperties(
Brian Osmana50205f2018-07-06 13:57:01 -0400191 colorType, fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700192 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700193
194 return filter->filterImage(srcImg, ctx, offset);
195}
196
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000197///////////////////////////////////////////////////////////////////////////////
198
Mike Reed353196f2017-07-21 11:01:18 -0400199bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800200 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000201
Mike Reed353196f2017-07-21 11:01:18 -0400202 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500203 return false;
204 }
205
Mike Reed353196f2017-07-21 11:01:18 -0400206 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500207 if (!rec.trim(this->width(), this->height())) {
208 return false;
209 }
210
211 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000212}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213
Mike Reed353196f2017-07-21 11:01:18 -0400214bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800215 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700216
Mike Reed353196f2017-07-21 11:01:18 -0400217 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500218 return false;
219 }
220
Mike Reed353196f2017-07-21 11:01:18 -0400221 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500222 if (!rec.trim(this->width(), this->height())) {
223 return false;
224 }
225
226 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000227}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228
reed41e010c2015-06-09 12:16:53 -0700229bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800230 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700231 return false;
232}
233
Brian Osman11052242016-10-27 14:47:55 -0400234GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700235 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400236 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700237}
238
reed8eddfb52014-12-04 07:50:14 -0800239void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800240 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500241 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500242
reed8eddfb52014-12-04 07:50:14 -0800243 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Chris Dalton344e9032017-12-11 15:42:09 -0700244 fRenderTargetContext->clear(&rect, 0x0, GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800245}
246
Brian Osman11052242016-10-27 14:47:55 -0400247void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800248 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800249
Brian Osman693a5402016-10-27 15:13:22 -0400250 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800251
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400252 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
253 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400254 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400255 this->context(),
256 budgeted,
257 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400258 fRenderTargetContext->numColorSamples(),
259 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400260 &this->surfaceProps(),
261 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400262 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800263 return;
264 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400265 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800266
267 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400268 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800269 return;
270 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500271 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800272 }
273
Brian Osman693a5402016-10-27 15:13:22 -0400274 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800275}
276
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277///////////////////////////////////////////////////////////////////////////////
278
Mike Reeda1361362017-03-07 09:37:29 -0500279void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800280 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500281 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
283 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400284 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
285 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700286 return;
287 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
Brian Salomon0166cfd2017-03-13 17:58:25 -0400289 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290}
291
Brian Salomon1459ebd2017-12-19 16:12:44 -0500292static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
293 switch (mode) {
294 case SkCanvas::kPoints_PointMode:
295 return GrPrimitiveType::kPoints;
296 case SkCanvas::kLines_PointMode:
297 return GrPrimitiveType::kLines;
298 case SkCanvas::kPolygon_PointMode:
299 return GrPrimitiveType::kLineStrip;
300 }
301 SK_ABORT("Unexpected mode");
302 return GrPrimitiveType::kPoints;
303}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304
Mike Reeda1361362017-03-07 09:37:29 -0500305void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800307 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500308 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309 SkScalar width = paint.getStrokeWidth();
310 if (width < 0) {
311 return;
312 }
313
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000314 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700315 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700316 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400317 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
318 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700319 return;
320 }
egdaniele61c4112014-06-12 10:24:21 -0700321 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700322 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700323 path.moveTo(pts[0]);
324 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700325 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
326 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700327 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000328 }
329
bsalomon6ade6dd2016-09-12 12:07:17 -0700330 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500331 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700332 SkScalarNearlyEqual(scales[0], 1.f) &&
333 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700334 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400336 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500337 SkRasterClip rc(this->devClipBounds());
338 SkDraw draw;
339 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
340 draw.fMatrix = &this->ctm();
341 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500342 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343 return;
344 }
345
Brian Salomon1459ebd2017-12-19 16:12:44 -0500346 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700347
Mike Reeda1361362017-03-07 09:37:29 -0500348 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700349#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
350 // This offsetting in device space matches the expectations of the Android framework for non-AA
351 // points and lines.
352 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600353 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700354 tempMatrix = *viewMatrix;
355 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
356 tempMatrix.postTranslate(kOffset, kOffset);
357 viewMatrix = &tempMatrix;
358 }
359#endif
360
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400362 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
363 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700364 return;
365 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366
Brian Osmanae0c50c2017-05-25 16:56:34 -0400367 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
368 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
369 nullptr);
370
371 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400372 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373}
374
375///////////////////////////////////////////////////////////////////////////////
376
Mike Reeda1361362017-03-07 09:37:29 -0500377void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800378 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500379 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400380
381 GrStyle style(paint);
382
bsalomona7d85ba2016-07-06 11:54:59 -0700383 // A couple reasons we might need to call drawPath.
384 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400385 GrShape shape(rect, style);
386
387 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
388 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389 return;
390 }
391
392 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400393 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
394 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700395 return;
396 }
Mike Klein744fb732014-06-23 15:13:26 -0400397
Chris Dalton3b51df12017-11-27 14:33:06 -0700398 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
399 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400}
401
402///////////////////////////////////////////////////////////////////////////////
403
Mike Reeda1361362017-03-07 09:37:29 -0500404void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800405 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500406 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400407
Mike Reed80747ef2018-01-23 15:29:32 -0500408 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000409 if (mf) {
410 if (mf->hasFragmentProcessor()) {
411 mf = nullptr; // already handled in SkPaintToGrPaint
412 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400413 }
414
bsalomon6663acf2016-05-10 09:14:17 -0700415 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000416
Robert Phillipsa29a9562016-10-20 09:40:55 -0400417 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800418 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400419 GrShape shape(rrect, style);
420
Robert Phillips27927a52018-08-20 13:18:12 -0400421 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
422 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000423 return;
424 }
Mike Klein744fb732014-06-23 15:13:26 -0400425
bsalomon6663acf2016-05-10 09:14:17 -0700426 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800427
Robert Phillipsa522d662018-08-23 13:50:16 -0400428 GrPaint grPaint;
429 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
430 this->ctm(), &grPaint)) {
431 return;
432 }
433
Chris Dalton3b51df12017-11-27 14:33:06 -0700434 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
435 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436}
437
robertphillipsd7706102016-02-25 09:28:08 -0800438
Robert Phillips20390c32018-08-17 11:01:03 -0400439void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800440 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500441 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800442 if (outer.isEmpty()) {
443 return;
444 }
445
446 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500447 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800448 }
449
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000450 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000451
robertphillips0e7029e2015-11-30 05:45:06 -0800452 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800453 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400454 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
455 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700456 return;
457 }
robertphillips00095892016-02-29 13:50:40 -0800458
Brian Salomon0166cfd2017-03-13 17:58:25 -0400459 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700460 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800461 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000462 }
463
464 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700465 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000466 path.addRRect(outer);
467 path.addRRect(inner);
468 path.setFillType(SkPath::kEvenOdd_FillType);
469
Robert Phillips27927a52018-08-20 13:18:12 -0400470 // TODO: We are losing the possible mutability of the path here but this should probably be
471 // fixed by upgrading GrShape to handle DRRects.
472 GrShape shape(path, paint);
473
474 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
475 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000476}
477
478
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000479/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000480
Mike Reeda1361362017-03-07 09:37:29 -0500481void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700482 if (paint.getMaskFilter()) {
483 SkPath path;
484 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400485 path.setIsVolatile(true);
486 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700487 }
488
489 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400490 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
491 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700492 return;
493 }
494
Chris Dalton3b51df12017-11-27 14:33:06 -0700495 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
496 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700497}
498
Mike Reeda1361362017-03-07 09:37:29 -0500499void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800500 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500501 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800502
robertphillips514450c2015-11-24 05:36:02 -0800503 if (paint.getMaskFilter()) {
504 // The RRect path can handle special case blurring
505 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500506 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000507 }
508
509 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400510 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
511 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700512 return;
513 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514
Chris Dalton3b51df12017-11-27 14:33:06 -0700515 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
516 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000517}
518
Mike Reeda1361362017-03-07 09:37:29 -0500519void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700520 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
521 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500522 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700523 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500524 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700525 return;
526 }
527 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400528 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
529 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700530 return;
531 }
532
Chris Dalton3b51df12017-11-27 14:33:06 -0700533 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500534 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500535 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700536}
537
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000539
540///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700541void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700542 const SkPaint& origPaint) {
543 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500544 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400545 // Adding support for round capping would require a
546 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700547 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
548 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
549 SkASSERT(!origPaint.getPathEffect());
550 SkASSERT(!origPaint.getMaskFilter());
551
552 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
553 SkASSERT(halfWidth > 0);
554
555 SkVector v = points[1] - points[0];
556
557 SkScalar length = SkPoint::Normalize(&v);
558 if (!length) {
559 v.fX = 1.0f;
560 v.fY = 0.0f;
561 }
562
563 SkPaint newPaint(origPaint);
564 newPaint.setStyle(SkPaint::kFill_Style);
565
566 SkScalar xtraLength = 0.0f;
567 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
568 xtraLength = halfWidth;
569 }
570
571 SkPoint mid = points[0] + points[1];
572 mid.scale(0.5f);
573
574 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
575 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
576 SkMatrix m;
577 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
578
579 SkMatrix local = m;
580
Mike Reeda1361362017-03-07 09:37:29 -0500581 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700582
583 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400584 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
585 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700586 return;
587 }
588
Brian Salomon82f44312017-01-11 13:42:54 -0500589 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700590 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700591}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592
Robert Phillips20390c32018-08-17 11:01:03 -0400593void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800594 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400595 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700596 SkPoint points[2];
597 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
598 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500599 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700600 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500601 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700602 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500603 // Round capping support is currently disabled b.c. it would require a RRect
604 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500605 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700606 return;
607 }
608 }
robertphillipsff55b492015-11-24 07:56:59 -0800609 }
610
Hal Canary144caf52016-11-07 17:57:18 -0500611 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400612 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500613 GrPaint grPaint;
614 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
615 this->ctm(), &grPaint)) {
616 return;
617 }
618 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
619 this->ctm(), origSrcPath, GrStyle(paint));
620 return;
621 }
Robert Phillips27927a52018-08-20 13:18:12 -0400622
623 // TODO: losing possible mutability of 'origSrcPath' here
624 GrShape shape(origSrcPath, paint);
625
626 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
627 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000628}
629
630static const int kBmpSmallTileSize = 1 << 10;
631
632static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
633 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
634 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
635 return tilesX * tilesY;
636}
637
reed85d91782015-09-10 14:33:38 -0700638static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000639 if (maxTileSize <= kBmpSmallTileSize) {
640 return maxTileSize;
641 }
642
643 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
644 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
645
646 maxTileTotalTileSize *= maxTileSize * maxTileSize;
647 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
648
649 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
650 return kBmpSmallTileSize;
651 } else {
652 return maxTileSize;
653 }
654}
655
656// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
657// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700658static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800659 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800660 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700661 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700662 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663 const SkRect* srcRectPtr,
664 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700665 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700666 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
667 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668 clippedSrcIRect->setEmpty();
669 return;
670 }
671 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
672 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700673 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 if (!clippedSrcRect.intersect(*srcRectPtr)) {
675 clippedSrcIRect->setEmpty();
676 return;
677 }
678 }
679 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700680 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 if (!clippedSrcIRect->intersect(bmpBounds)) {
682 clippedSrcIRect->setEmpty();
683 }
684}
685
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400686bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
687 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700688 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700689 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400690 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700691 const SkRect* srcRectPtr,
692 int maxTileSize,
693 int* tileSize,
694 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800695 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700696 // if it's larger than the max tile size, then we have no choice but tiling.
697 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400698 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400699 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
700 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700701 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
702 return true;
703 }
704
bsalomon1a1d0b82015-10-16 07:49:42 -0700705 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700706 const size_t area = imageRect.width() * imageRect.height();
707 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
708 return false;
709 }
710
reed85d91782015-09-10 14:33:38 -0700711 // At this point we know we could do the draw by uploading the entire bitmap
712 // as a texture. However, if the texture would be large compared to the
713 // cache size and we don't require most of it for this draw then tile to
714 // reduce the amount of upload and cache spill.
715
716 // assumption here is that sw bitmap size is a good proxy for its size as
717 // a texture
718 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
719 size_t cacheSize;
720 fContext->getResourceCacheLimits(nullptr, &cacheSize);
721 if (bmpSize < cacheSize / 2) {
722 return false;
723 }
724
bsalomon1a1d0b82015-10-16 07:49:42 -0700725 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
726 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400727 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
728 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400729 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700730 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
731 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500732 kBmpSmallTileSize * kBmpSmallTileSize *
733 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700734
Brian Osmand05cdc32017-02-06 13:24:47 -0500735 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700736}
737
reed85d91782015-09-10 14:33:38 -0700738bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
739 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700740 const SkMatrix& viewMatrix,
741 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800742 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400743 // If image is explicitly texture backed then we shouldn't get here.
744 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400746 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700747 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500748 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
749 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
750 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700751
752 int tileFilterPad;
753 if (doBicubic) {
754 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400755 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700756 tileFilterPad = 0;
757 } else {
758 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400760 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400762 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763
reed85d91782015-09-10 14:33:38 -0700764 // these are output, which we safely ignore, as we just want to know the predicate
765 int outTileSize;
766 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767
bsalomone553b642016-08-17 09:02:09 -0700768 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400769 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700770 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771}
772
Mike Reeda1361362017-03-07 09:37:29 -0500773void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400774 SkScalar x,
775 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400777 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800778 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800779 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500780 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700781
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400782 int maxTileSize = this->caps()->maxTileSize();
bsalomonb1b01992015-11-18 10:56:08 -0800783
784 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
785 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400786 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
787 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800788 bitmap.height() <= maxTileSize;
789
790 bool skipTileCheck = drawAA || paint.getMaskFilter();
791
792 if (!skipTileCheck) {
793 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
794 int tileSize;
795 SkIRect clippedSrcRect;
796
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400797 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800798 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400799 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500800 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
801 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800802
803 int tileFilterPad;
804
805 if (doBicubic) {
806 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400807 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800808 tileFilterPad = 0;
809 } else {
810 tileFilterPad = 1;
811 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400812 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800813
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400814 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700815 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400816 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700817 &tileSize, &clippedSrcRect)) {
818 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400819 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
820 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800821 return;
822 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000823 }
Hal Canary144caf52016-11-07 17:57:18 -0500824 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0cdfc322018-05-14 16:17:43 -0400825 this->drawTextureMaker(&maker, bitmap.width(), bitmap.height(), nullptr, nullptr,
826 SkCanvas::kStrict_SrcRectConstraint, viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827}
828
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000829// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
831// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000832static inline void clamped_outset_with_offset(SkIRect* iRect,
833 int outset,
834 SkPoint* offset,
835 const SkIRect& clamp) {
836 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000838 int leftClampDelta = clamp.fLeft - iRect->fLeft;
839 if (leftClampDelta > 0) {
840 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 iRect->fLeft = clamp.fLeft;
842 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000843 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000845
846 int topClampDelta = clamp.fTop - iRect->fTop;
847 if (topClampDelta > 0) {
848 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849 iRect->fTop = clamp.fTop;
850 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000851 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000852 }
853
854 if (iRect->fRight > clamp.fRight) {
855 iRect->fRight = clamp.fRight;
856 }
857 if (iRect->fBottom > clamp.fBottom) {
858 iRect->fBottom = clamp.fBottom;
859 }
860}
861
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000862// Break 'bitmap' into several tiles to draw it since it has already
863// been determined to be too large to fit in VRAM
864void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800865 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700866 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000867 const SkRect& srcRect,
868 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400869 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800870 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700871 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000872 int tileSize,
873 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800874 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800875
ericrk983294f2016-04-18 09:14:00 -0700876 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800877 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700878 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800879
bsalomonc55271f2015-11-09 11:55:57 -0800880 const SkPaint* paint = &origPaint;
881 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400882 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800883 // Drop antialiasing to avoid seams at tile boundaries.
884 tempPaint = origPaint;
885 tempPaint.setAntiAlias(false);
886 paint = &tempPaint;
887 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
889
890 int nx = bitmap.width() / tileSize;
891 int ny = bitmap.height() / tileSize;
892 for (int x = 0; x <= nx; x++) {
893 for (int y = 0; y <= ny; y++) {
894 SkRect tileR;
895 tileR.set(SkIntToScalar(x * tileSize),
896 SkIntToScalar(y * tileSize),
897 SkIntToScalar((x + 1) * tileSize),
898 SkIntToScalar((y + 1) * tileSize));
899
900 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
901 continue;
902 }
903
904 if (!tileR.intersect(srcRect)) {
905 continue;
906 }
907
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000908 SkIRect iTileR;
909 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700910 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
911 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500912 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700913 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400914 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000915 SkIRect iClampRect;
916
reeda5517e22015-07-14 10:54:12 -0700917 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918 // In bleed mode we want to always expand the tile on all edges
919 // but stay within the bitmap bounds
920 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
921 } else {
922 // In texture-domain/clamp mode we only want to expand the
923 // tile on edges interior to "srcRect" (i.e., we want to
924 // not bleed across the original clamped edges)
925 srcRect.roundOut(&iClampRect);
926 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000927 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
928 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000929 }
930
bsalomone553b642016-08-17 09:02:09 -0700931 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000932 if (bitmap.extractSubset(&tmpB, iTileR)) {
933 // now offset it to make it "local" to our tmp bitmap
934 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800935 // de-optimized this determination
936 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700937 this->drawBitmapTile(tmpB,
938 viewMatrix,
939 rectToDraw,
940 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500941 params,
bsalomone553b642016-08-17 09:02:09 -0700942 *paint,
943 constraint,
944 bicubic,
945 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 }
947 }
948 }
949}
950
bsalomone553b642016-08-17 09:02:09 -0700951void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
952 const SkMatrix& viewMatrix,
953 const SkRect& dstRect,
954 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400955 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700956 const SkPaint& paint,
957 SkCanvas::SrcRectConstraint constraint,
958 bool bicubic,
959 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800960 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400961 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
962 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700963 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400964 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
965 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400966 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000967
Brian Salomon2a943df2018-05-04 13:43:19 -0400968 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400969 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400970 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400971 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 return;
973 }
bsalomone553b642016-08-17 09:02:09 -0700974
bsalomone553b642016-08-17 09:02:09 -0700975 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400976 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
977 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700978
979 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
980 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400981 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700982
reeda5517e22015-07-14 10:54:12 -0700983 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700985 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000986 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500987 domain.fLeft = srcRect.fLeft + 0.5f;
988 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000989 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500990 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 }
992 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500993 domain.fTop = srcRect.fTop + 0.5f;
994 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500996 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000998 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -0400999 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001000 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001001 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001002 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001003 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001004 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001005 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1006 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001007 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001009 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010 }
1011
Brian Osman21fc5ce2018-08-27 20:36:19 +00001012 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001013 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001014 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001015 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1016 viewMatrix, std::move(fp),
1017 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001018 return;
1019 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001021 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001022 GrAA aa = GrAA(paint.isAntiAlias() &&
1023 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001024 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001025}
1026
Mike Reeda1361362017-03-07 09:37:29 -05001027void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001028 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001029 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001030 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031
Khushalc421ca12018-06-26 14:38:34 -07001032 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001033 return;
1034 }
1035
Robert Phillipse14d3052017-02-15 13:18:21 -05001036 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1037 if (!srcImg) {
1038 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001039 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040
Florin Malita53f77bd2017-04-28 13:48:37 -04001041 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001042}
1043
1044
Robert Phillipsc100d482018-07-10 10:11:01 -04001045void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001046 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001047 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001048 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001049
Florin Malita53f77bd2017-04-28 13:48:37 -04001050 // TODO: clipImage support.
1051
robertphillips970587b2016-07-14 14:12:55 -07001052 sk_sp<SkSpecialImage> result;
1053 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001054 SkIPoint offset = { 0, 0 };
1055
1056 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001057 if (!result) {
1058 return;
1059 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001060
1061 left += offset.fX;
1062 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001063 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001064 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001065 }
1066
1067 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001068 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001069 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001070 return;
1071 }
robertphillips970587b2016-07-14 14:12:55 -07001072
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001073 const GrPixelConfig config = proxy->config();
1074
robertphillips970587b2016-07-14 14:12:55 -07001075 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001076 if (tmpUnfiltered.getMaskFilter()) {
1077 SkMatrix ctm = this->ctm();
Robert Phillipsc100d482018-07-10 10:11:01 -04001078 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001079 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001080 }
1081
robertphillips970587b2016-07-14 14:12:55 -07001082 tmpUnfiltered.setImageFilter(nullptr);
1083
Brian Osman2240be92017-10-18 13:15:13 -04001084 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001085 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001086 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001087 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001088 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001089 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001090 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001091 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001092
1093 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001094 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1095 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001096 return;
1097 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098
robertphillips970587b2016-07-14 14:12:55 -07001099 const SkIRect& subset = result->subset();
1100
Brian Salomon82f44312017-01-11 13:42:54 -05001101 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001102 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001103 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001104 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001105 SkMatrix::I(),
Robert Phillipsc100d482018-07-10 10:11:01 -04001106 SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001107 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108}
1109
Mike Reeda1361362017-03-07 09:37:29 -05001110void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001111 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001112 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001113 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001114 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1115 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1116 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1117 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1118 const SkRect* dst = &origDst;
1119 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001121 if (!src) {
1122 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001124
bsalomonb1b01992015-11-18 10:56:08 -08001125 SkMatrix srcToDstMatrix;
1126 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1127 return;
1128 }
1129 SkRect tmpSrc, tmpDst;
1130 if (src != &bmpBounds) {
1131 if (!bmpBounds.contains(*src)) {
1132 tmpSrc = *src;
1133 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 return; // nothing to draw
1135 }
bsalomonb1b01992015-11-18 10:56:08 -08001136 src = &tmpSrc;
1137 srcToDstMatrix.mapRect(&tmpDst, *src);
1138 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139 }
1140 }
1141
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001142 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001143
bsalomonb1b01992015-11-18 10:56:08 -08001144 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1145 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001146 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1147 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1148 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001149
Brian Salomon7c8460e2017-05-12 11:36:10 -04001150 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001151
1152 if (!skipTileCheck) {
1153 int tileSize;
1154 SkIRect clippedSrcRect;
1155
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001156 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001157 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001158 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001159 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1160 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001161
1162 int tileFilterPad;
1163
1164 if (doBicubic) {
1165 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001166 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001167 tileFilterPad = 0;
1168 } else {
1169 tileFilterPad = 1;
1170 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001171 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001172
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001173 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001174 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001175 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1176 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001177 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001178 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001179 return;
1180 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001181 }
Hal Canary144caf52016-11-07 17:57:18 -05001182 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0cdfc322018-05-14 16:17:43 -04001183 this->drawTextureMaker(&maker, bitmap.width(), bitmap.height(), src, dst, constraint,
1184 this->ctm(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185}
1186
robertphillips6451a0c2016-07-18 08:31:31 -07001187sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001188 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1189 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001190 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1191 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001192 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001193 return nullptr;
1194 }
1195
Robert Phillipse14d3052017-02-15 13:18:21 -05001196 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001197
Robert Phillipse14d3052017-02-15 13:18:21 -05001198 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1199 // the special image
1200 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1201 rect,
1202 bitmap.getGenerationID(),
1203 std::move(proxy),
1204 bitmap.refColorSpace(),
1205 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001206}
1207
reede51c3562016-07-19 14:33:20 -07001208sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001209 SkPixmap pm;
1210 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001211 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001212
Robert Phillips6de99042017-01-31 11:31:39 -05001213 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1214 SkIRect::MakeWH(image->width(), image->height()),
1215 image->uniqueID(),
1216 std::move(proxy),
1217 as_IB(image)->onImageInfo().refColorSpace(),
1218 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001219 } else if (image->peekPixels(&pm)) {
1220 SkBitmap bm;
1221
1222 bm.installPixels(pm);
1223 return this->makeSpecial(bm);
1224 } else {
1225 return nullptr;
1226 }
1227}
1228
1229sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001230 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1231 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001232 // When the device doesn't have a texture, we create a temporary texture.
1233 // TODO: we should actually only copy the portion of the source needed to apply the image
1234 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001235 proxy = GrSurfaceProxy::Copy(fContext.get(),
1236 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001237 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001238 SkBudgeted::kYes);
1239 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001240 return nullptr;
1241 }
robertphillips1b5f9682016-07-15 08:01:12 -07001242 }
1243
1244 const SkImageInfo ii = this->imageInfo();
1245 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1246
Robert Phillipse2f7d182016-12-15 09:23:05 -05001247 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1248 srcRect,
1249 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001250 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001251 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001252 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001253}
1254
Mike Reeda1361362017-03-07 09:37:29 -05001255void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001256 int left, int top, const SkPaint& paint) {
1257 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001258
joshualittce894002016-01-11 13:29:31 -08001259 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001261 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001262
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001263 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001264 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001265 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001266 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267 return;
1268 }
1269
Mike Reed2179b782018-02-07 11:59:57 -05001270 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271}
1272
Brian Salomon34169692017-08-28 15:32:01 -04001273void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001274 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001275 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001276 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001277 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001278 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001279 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1280 image->alphaType(), nullptr, nullptr,
1281 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001282 return;
Brian Salomon34169692017-08-28 15:32:01 -04001283 }
1284 SkBitmap bm;
1285 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1286 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1287 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001288 if (!as_IB(image)->getROPixels(&bm)) {
Brian Salomon34169692017-08-28 15:32:01 -04001289 return;
reed85d91782015-09-10 14:33:38 -07001290 }
Brian Salomon34169692017-08-28 15:32:01 -04001291 this->drawBitmap(bm, x, y, paint);
1292 return;
1293 }
1294 if (image->isLazyGenerated()) {
1295 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1296 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1297 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1298 return;
1299 }
Brian Osmane50cdf02018-10-19 13:02:14 -04001300 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon34169692017-08-28 15:32:01 -04001301 GrBitmapTextureMaker maker(fContext.get(), bm);
1302 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1303 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001304 }
1305}
1306
Brian Salomon34169692017-08-28 15:32:01 -04001307void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1308 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001309 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001310 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001311 if (!src || src->contains(image->bounds())) {
1312 constraint = SkCanvas::kFast_SrcRectConstraint;
1313 }
Robert Phillips3798c862017-03-27 11:08:16 -04001314 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001315 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1316 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001317 return;
1318 }
1319 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001320 SkMatrix srcToDstRect;
1321 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1322 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001323 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001324 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001325 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001326 if (!as_IB(image)->getROPixels(&bm)) {
bsalomonc55271f2015-11-09 11:55:57 -08001327 return;
1328 }
Mike Reeda1361362017-03-07 09:37:29 -05001329 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001330 return;
1331 }
1332 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001333 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001334 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1335 this->ctm(), paint);
1336 return;
1337 }
Brian Osmane50cdf02018-10-19 13:02:14 -04001338 if (as_IB(image)->getROPixels(&bm)) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001339 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001340 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1341 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001342 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001343}
1344
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001345// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1346static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1347 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1348 return GrSamplerState::Filter::kNearest;
1349 }
1350
1351 return GrSamplerState::Filter::kBilerp;
1352}
1353
Mike Reeda1361362017-03-07 09:37:29 -05001354void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001355 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001356 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001357 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001358 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001359 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001360 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001361 image->alphaType(), pinnedUniqueID,
1362 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001363 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001364 } else {
1365 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001366 if (image->isLazyGenerated()) {
1367 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001368 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001369 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001370 GrBitmapTextureMaker maker(fContext.get(), bm);
1371 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001372 }
1373 }
1374}
1375
Mike Reeda1361362017-03-07 09:37:29 -05001376void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001377 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001378 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001379 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001380 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001381 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001382}
1383
Mike Reeda1361362017-03-07 09:37:29 -05001384void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001385 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1386 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001387 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001388 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001389
Brian Salomon2a943df2018-05-04 13:43:19 -04001390 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1391 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1392 }
msarett10e3d9b2016-08-18 15:46:03 -07001393 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001394 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1395 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001396 return;
1397 }
1398
Brian Salomon2a943df2018-05-04 13:43:19 -04001399 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1400 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Brian Osman6064e1c2018-10-19 14:27:54 -04001401 auto proxy = producer->refTextureProxyForParams(filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001402 if (!proxy) {
1403 return;
1404 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001405 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1406 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001407
Brian Salomon0166cfd2017-03-13 17:58:25 -04001408 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001409 std::move(proxy), std::move(csxf), filter,
1410 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001411}
1412
Mike Reeda1361362017-03-07 09:37:29 -05001413void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001414 const SkCanvas::Lattice& lattice, const SkRect& dst,
1415 const SkPaint& paint) {
1416 ASSERT_SINGLE_OWNER
1417 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001418 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001419 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001420 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001421 image->alphaType(), pinnedUniqueID,
1422 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001423 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001424 } else {
1425 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001426 if (image->isLazyGenerated()) {
1427 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001428 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001429 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001430 GrBitmapTextureMaker maker(fContext.get(), bm);
1431 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001432 }
1433 }
1434}
1435
Mike Reeda1361362017-03-07 09:37:29 -05001436void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001437 const SkCanvas::Lattice& lattice, const SkRect& dst,
1438 const SkPaint& paint) {
1439 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001440 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001441 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001442 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001443}
1444
Brian Salomond7065e72018-10-12 11:42:02 -04001445void SkGpuDevice::drawImageSet(const SkCanvas::ImageSetEntry set[], int count, float alpha,
1446 SkFilterQuality filterQuality, SkBlendMode mode) {
1447 SkASSERT(count > 0);
1448 if (mode != SkBlendMode::kSrcOver ||
1449 !fContext->contextPriv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1450 INHERITED::drawImageSet(set, count, alpha, filterQuality, mode);
1451 return;
1452 }
1453 GrSamplerState sampler;
1454 sampler.setFilterMode(kNone_SkFilterQuality == filterQuality ? GrSamplerState::Filter::kNearest
1455 : GrSamplerState::Filter::kBilerp);
1456 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
1457 GrColor color = GrColorPackA4(SkToUInt(SkTClamp(SkScalarRoundToInt(alpha * 255), 0, 255)));
1458 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
1459 // issue the accumulated 'n' draws starting at 'base'.
1460 int base = 0, n = 0;
1461 auto draw = [&] {
1462 if (n > 0) {
1463 auto textureXform = GrColorSpaceXform::Make(
1464 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
1465 fRenderTargetContext->colorSpaceInfo().colorSpace(), kPremul_SkAlphaType);
1466 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n,
1467 sampler.filter(), color, this->ctm(),
1468 std::move(textureXform), nullptr);
1469 }
1470 };
1471 for (int i = 0; i < count; ++i) {
Brian Salomon0087c832018-10-15 14:48:20 -04001472 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
1473 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
1474 if (!set[i].fSrcRect.isSorted()) {
1475 draw();
1476 base = i + 1;
1477 n = 0;
1478 continue;
1479 }
Brian Salomond7065e72018-10-12 11:42:02 -04001480 textures[i].fProxy =
1481 as_IB(set[i].fImage.get())
Brian Osman6064e1c2018-10-19 14:27:54 -04001482 ->asTextureProxyRef(fContext.get(), GrSamplerState::ClampBilerp(), nullptr);
Brian Salomond7065e72018-10-12 11:42:02 -04001483 textures[i].fSrcRect = set[i].fSrcRect;
1484 textures[i].fDstRect = set[i].fDstRect;
1485 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
1486 // If we failed to make a proxy or our proxy can't be grouped with the previous proxies then
1487 // draw the accumulated set and reset.
1488 if (!textures[i].fProxy) {
1489 draw();
1490 base = i + 1;
1491 n = 0;
Brian Salomon0087c832018-10-15 14:48:20 -04001492 continue;
1493 }
1494 if (n > 0 &&
1495 (textures[i].fProxy->textureType() != textures[base].fProxy->textureType() ||
1496 textures[i].fProxy->config() != textures[base].fProxy->config() ||
1497 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
1498 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Brian Salomond7065e72018-10-12 11:42:02 -04001499 draw();
1500 base = i;
1501 n = 1;
1502 } else {
1503 ++n;
1504 }
1505 }
1506 draw();
1507}
1508
Brian Salomonf3569f02017-10-24 12:52:33 -04001509static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1510 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001511 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001512 if (hasTexs && skPaint.getShader()) {
1513 if (hasColors) {
1514 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001515 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1516 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001517 } else {
1518 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001519 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001520 }
1521 } else {
1522 if (hasColors) {
1523 // We have colors, but either have no shader or no texture coords (which implies that
1524 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001525 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001526 } else {
1527 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001528 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001529 }
1530 }
1531}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001532
Mike Reed887cdf12017-04-03 11:11:09 -04001533void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001534 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001535 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001536 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001537 const uint16_t indices[], int indexCount,
1538 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001539 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001540 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001541
Mike Reed2f6b5a42017-03-19 15:04:17 -04001542 SkPaint copy(paint);
1543 copy.setStyle(SkPaint::kStroke_Style);
1544 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001545
Mike Reed2f6b5a42017-03-19 15:04:17 -04001546 GrPaint grPaint;
1547 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001548 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1549 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001550 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551 }
1552
Mike Reed2f6b5a42017-03-19 15:04:17 -04001553 int triangleCount = 0;
1554 int n = (nullptr == indices) ? vertexCount : indexCount;
1555 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001556 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001557 triangleCount = n / 3;
1558 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001559 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001560 triangleCount = n - 2;
1561 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001562 case SkVertices::kTriangleFan_VertexMode:
1563 SK_ABORT("Unexpected triangle fan.");
1564 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001565 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566
Mike Reed2f6b5a42017-03-19 15:04:17 -04001567 VertState state(vertexCount, indices, indexCount);
1568 VertState::Proc vertProc = state.chooseProc(vmode);
1569
1570 //number of indices for lines per triangle with kLines
1571 indexCount = triangleCount * 6;
1572
Brian Osmanae0c50c2017-05-25 16:56:34 -04001573 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1574 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1575 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1576
1577 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001578 int i = 0;
1579 while (vertProc(&state)) {
1580 lineIndices[i] = state.f0;
1581 lineIndices[i + 1] = state.f1;
1582 lineIndices[i + 2] = state.f1;
1583 lineIndices[i + 3] = state.f2;
1584 lineIndices[i + 4] = state.f2;
1585 lineIndices[i + 5] = state.f0;
1586 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001587 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001588
Chris Dalton3809bab2017-06-13 10:55:06 -06001589 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001590 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001591 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001592 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001593 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001594 bones,
1595 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001596 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001597}
1598
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001599void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1600 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001601 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001602 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001603
1604 SkASSERT(vertices);
1605 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001606 bool hasColors = vertices->hasColors();
1607 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001608 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001609 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001610 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001611 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1612 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001613 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001614 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001615 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1616 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001617 return;
1618 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001619 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001620 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1621 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001622}
1623
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624///////////////////////////////////////////////////////////////////////////////
1625
Jim Van Verth3af1af92017-05-18 15:06:54 -04001626void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1627
1628 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001629 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1630
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001631 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001632 // failed to find an accelerated case
1633 this->INHERITED::drawShadow(path, rec);
1634 }
1635}
1636
1637///////////////////////////////////////////////////////////////////////////////
1638
Mike Reeda1361362017-03-07 09:37:29 -05001639void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001640 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001641 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001642 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001643 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001644 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001645 return;
1646 }
1647
Hal Canary144caf52016-11-07 17:57:18 -05001648 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001649
reedca109532015-06-25 16:25:25 -07001650 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001651 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001652
jvanverth31ff7622015-08-07 10:09:28 -07001653 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001654 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001655 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1656 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001657 return;
1658 }
1659 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001660 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1661 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001662 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001663 }
1664 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001665
1666 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001667 fRenderTargetContext->drawAtlas(
1668 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001669}
1670
1671///////////////////////////////////////////////////////////////////////////////
1672
Herb Derbyb935cf82018-07-26 16:54:18 -04001673void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001674 ASSERT_SINGLE_OWNER
1675 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
1676 SkDEBUGCODE(this->validate();)
1677
Jim Van Verth87a30112018-09-24 16:13:58 -04001678 // Check for valid input
1679 const SkMatrix& ctm = this->ctm();
1680 const SkPaint& paint = glyphRunList.paint();
1681 if (!ctm.isFinite() || !SkScalarIsFinite(paint.getTextSize()) ||
1682 !SkScalarIsFinite(paint.getTextScaleX()) || !SkScalarIsFinite(paint.getTextSkewX())) {
1683 return;
1684 }
1685
1686 fRenderTargetContext->drawGlyphRunList(this->clip(), ctm, glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001687}
1688
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689///////////////////////////////////////////////////////////////////////////////
1690
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001691void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
1692 GrBackendApi api = this->context()->contextPriv().getBackend();
1693 if (GrBackendApi::kVulkan == api) {
1694 const SkMatrix& ctm = canvas->getTotalMatrix();
1695 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1696 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
1697 drawable->snapGpuDrawHandler(api, combinedMatrix);
1698 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001699 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1700 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001701 }
1702 }
1703 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1704}
1705
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001706
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001707///////////////////////////////////////////////////////////////////////////////
1708
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001710 this->flushAndSignalSemaphores(0, nullptr);
1711}
1712
Greg Daniel51316782017-08-02 15:10:09 +00001713GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1714 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001715 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001716
Greg Danielc64ee462017-06-15 16:59:49 -04001717 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001718}
1719
Greg Danielc64ee462017-06-15 16:59:49 -04001720bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001721 ASSERT_SINGLE_OWNER
1722
Greg Danielc64ee462017-06-15 16:59:49 -04001723 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724}
1725
1726///////////////////////////////////////////////////////////////////////////////
1727
reed76033be2015-03-14 10:54:31 -07001728SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001729 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730
robertphillipsca6eafc2016-05-17 09:57:46 -07001731 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732
robertphillipsca6eafc2016-05-17 09:57:46 -07001733 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001734 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001735 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1736 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001737
Brian Osman10fc6fd2018-03-02 11:01:10 -05001738 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
1739 if (kRGBA_1010102_GrPixelConfig == config) {
1740 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1741 // channel in the layer.
1742 config = kRGBA_8888_GrPixelConfig;
1743 }
1744
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001745 sk_sp<GrRenderTargetContext> rtc(fContext->contextPriv().makeDeferredRenderTargetContext(
Brian Osman10fc6fd2018-03-02 11:01:10 -05001746 fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001747 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001748 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1749 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001750 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001751 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001753
1754 // Skia's convention is to only clear a device if it is non-opaque.
1755 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1756
Robert Phillips9fab7e92016-11-17 12:45:04 -05001757 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1758 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001759}
1760
reede8f30622016-03-23 18:59:25 -07001761sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001762 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001763 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001764 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001765 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001766 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001767 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001768}
1769
senorblanco900c3672016-04-27 11:31:23 -07001770SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001771 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001772 // We always return a transient cache, so it is freed after each
1773 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001774 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001775}
reedf037e0b2014-10-30 11:34:15 -07001776