blob: ff9e443215b0b2f259fda4bd95b703d583065e29 [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"
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040053#include "SkYUVAIndex.h"
kkinnunenabcfab42015-02-22 22:53:44 -080054#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080055#include "effects/GrSimpleTextureEffect.h"
56#include "effects/GrTextureDomain.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040057#include "text/GrTextTarget.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058
joshualittce894002016-01-11 13:29:31 -080059#define ASSERT_SINGLE_OWNER \
Robert Phillips0c4b7b12018-03-06 08:20:37 -050060SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->contextPriv().debugSingleOwner());)
joshualittce894002016-01-11 13:29:31 -080061
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063///////////////////////////////////////////////////////////////////////////////
64
bsalomon74f681d2015-06-23 14:38:48 -070065/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
66 should fail. */
67bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
68 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
69 *flags = 0;
70 if (info) {
71 switch (info->alphaType()) {
72 case kPremul_SkAlphaType:
73 break;
74 case kOpaque_SkAlphaType:
75 *flags |= SkGpuDevice::kIsOpaque_Flag;
76 break;
77 default: // If it is unpremul or unknown don't try to render
78 return false;
79 }
80 }
81 if (kClear_InitContents == init) {
82 *flags |= kNeedClear_Flag;
83 }
84 return true;
85}
86
Robert Phillips9fab7e92016-11-17 12:45:04 -050087sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
88 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070089 int width, int height,
90 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040091 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070092 return nullptr;
93 }
94 unsigned flags;
95 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
96 return nullptr;
97 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050098 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
99 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000100}
101
robertphillips24e91282016-04-29 06:46:36 -0700102sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
103 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400104 GrSurfaceOrigin origin, const SkSurfaceProps* props,
105 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700106 unsigned flags;
107 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700108 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700109 }
110
Brian Osman11052242016-10-27 14:47:55 -0400111 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
112 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400113 origin, props,
114 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400115 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700116 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700117 }
118
Robert Phillips9fab7e92016-11-17 12:45:04 -0500119 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700120 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700121}
122
Brian Osman11052242016-10-27 14:47:55 -0400123static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700124 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400125 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700126 colorType = kUnknown_SkColorType;
127 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400128 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
129 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700130}
131
Robert Phillips9fab7e92016-11-17 12:45:04 -0500132SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
133 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400134 : INHERITED(make_info(renderTargetContext.get(), width, height,
135 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500136 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400137 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700138{
robertphillips1f3923e2016-07-21 07:17:54 -0700139 fSize.set(width, height);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140
bsalomone63ffef2016-02-05 07:17:34 -0800141 if (flags & kNeedClear_Flag) {
142 this->clearAll();
143 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144}
145
Brian Osman11052242016-10-27 14:47:55 -0400146sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
147 GrContext* context,
148 SkBudgeted budgeted,
149 const SkImageInfo& origInfo,
150 int sampleCount,
151 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400152 const SkSurfaceProps* surfaceProps,
153 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000154 if (kUnknown_SkColorType == origInfo.colorType() ||
155 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000157 }
158
bsalomonafe30052015-01-16 07:32:33 -0800159 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800161 }
162
Brian Osman2b23c4b2018-06-01 12:25:08 -0400163 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500164 if (kUnknown_GrPixelConfig == config) {
165 return nullptr;
166 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400167 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
168 // they need to be exact.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500169 return context->contextPriv().makeDeferredRenderTargetContext(
Greg Danield7157b22018-11-14 15:28:10 +0000170 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700171 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500172 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400173 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800174}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175
Mike Reeda1361362017-03-07 09:37:29 -0500176sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700177 int left, int top,
178 SkIPoint* offset,
179 const SkImageFilter* filter) {
180 SkASSERT(srcImg->isTextureBacked());
181 SkASSERT(filter);
182
Mike Reeda1361362017-03-07 09:37:29 -0500183 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700184 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500185 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500186 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -0400187 SkColorType colorType;
188 if (!GrPixelConfigToColorType(fRenderTargetContext->colorSpaceInfo().config(), &colorType)) {
189 colorType = kN32_SkColorType;
190 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400191 SkImageFilter::OutputProperties outputProperties(
Brian Osmana50205f2018-07-06 13:57:01 -0400192 colorType, fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700193 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700194
195 return filter->filterImage(srcImg, ctx, offset);
196}
197
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198///////////////////////////////////////////////////////////////////////////////
199
Mike Reed353196f2017-07-21 11:01:18 -0400200bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800201 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000202
Mike Reed353196f2017-07-21 11:01:18 -0400203 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500204 return false;
205 }
206
Mike Reed353196f2017-07-21 11:01:18 -0400207 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500208 if (!rec.trim(this->width(), this->height())) {
209 return false;
210 }
211
212 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000213}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214
Mike Reed353196f2017-07-21 11:01:18 -0400215bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800216 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700217
Mike Reed353196f2017-07-21 11:01:18 -0400218 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500219 return false;
220 }
221
Mike Reed353196f2017-07-21 11:01:18 -0400222 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500223 if (!rec.trim(this->width(), this->height())) {
224 return false;
225 }
226
227 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000228}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229
reed41e010c2015-06-09 12:16:53 -0700230bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800231 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700232 return false;
233}
234
Brian Osman11052242016-10-27 14:47:55 -0400235GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700236 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400237 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700238}
239
reed8eddfb52014-12-04 07:50:14 -0800240void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800241 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500242 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500243
reed8eddfb52014-12-04 07:50:14 -0800244 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman9a9baae2018-11-05 15:06:26 -0500245 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
246 GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800247}
248
Brian Osman11052242016-10-27 14:47:55 -0400249void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800250 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800251
Brian Osman693a5402016-10-27 15:13:22 -0400252 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800253
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400254 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
255 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400256 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400257 this->context(),
258 budgeted,
259 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400260 fRenderTargetContext->numColorSamples(),
261 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400262 &this->surfaceProps(),
263 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400264 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800265 return;
266 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400267 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800268
269 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400270 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800271 return;
272 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500273 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800274 }
275
Brian Osman693a5402016-10-27 15:13:22 -0400276 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800277}
278
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279///////////////////////////////////////////////////////////////////////////////
280
Mike Reeda1361362017-03-07 09:37:29 -0500281void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800282 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500283 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284
285 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400286 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
287 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700288 return;
289 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
Brian Salomon0166cfd2017-03-13 17:58:25 -0400291 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292}
293
Brian Salomon1459ebd2017-12-19 16:12:44 -0500294static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
295 switch (mode) {
296 case SkCanvas::kPoints_PointMode:
297 return GrPrimitiveType::kPoints;
298 case SkCanvas::kLines_PointMode:
299 return GrPrimitiveType::kLines;
300 case SkCanvas::kPolygon_PointMode:
301 return GrPrimitiveType::kLineStrip;
302 }
303 SK_ABORT("Unexpected mode");
304 return GrPrimitiveType::kPoints;
305}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306
Mike Reeda1361362017-03-07 09:37:29 -0500307void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800309 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500310 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000311 SkScalar width = paint.getStrokeWidth();
312 if (width < 0) {
313 return;
314 }
315
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000316 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700317 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700318 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400319 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
320 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700321 return;
322 }
egdaniele61c4112014-06-12 10:24:21 -0700323 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700324 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700325 path.moveTo(pts[0]);
326 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700327 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
328 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700329 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000330 }
331
bsalomon6ade6dd2016-09-12 12:07:17 -0700332 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500333 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700334 SkScalarNearlyEqual(scales[0], 1.f) &&
335 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700336 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000337 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400338 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500339 SkRasterClip rc(this->devClipBounds());
340 SkDraw draw;
341 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
342 draw.fMatrix = &this->ctm();
343 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500344 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345 return;
346 }
347
Brian Salomon1459ebd2017-12-19 16:12:44 -0500348 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700349
Mike Reeda1361362017-03-07 09:37:29 -0500350 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700351#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
352 // This offsetting in device space matches the expectations of the Android framework for non-AA
353 // points and lines.
354 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600355 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700356 tempMatrix = *viewMatrix;
357 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
358 tempMatrix.postTranslate(kOffset, kOffset);
359 viewMatrix = &tempMatrix;
360 }
361#endif
362
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400364 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
365 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700366 return;
367 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
Brian Osmanae0c50c2017-05-25 16:56:34 -0400369 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
370 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
371 nullptr);
372
373 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400374 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000375}
376
377///////////////////////////////////////////////////////////////////////////////
378
Mike Reeda1361362017-03-07 09:37:29 -0500379void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800380 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500381 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400382
383 GrStyle style(paint);
384
bsalomona7d85ba2016-07-06 11:54:59 -0700385 // A couple reasons we might need to call drawPath.
386 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400387 GrShape shape(rect, style);
388
389 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
390 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391 return;
392 }
393
394 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400395 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
396 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700397 return;
398 }
Mike Klein744fb732014-06-23 15:13:26 -0400399
Chris Dalton3b51df12017-11-27 14:33:06 -0700400 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
401 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402}
403
404///////////////////////////////////////////////////////////////////////////////
405
Mike Reeda1361362017-03-07 09:37:29 -0500406void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800407 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500408 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400409
Mike Reed80747ef2018-01-23 15:29:32 -0500410 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000411 if (mf) {
412 if (mf->hasFragmentProcessor()) {
413 mf = nullptr; // already handled in SkPaintToGrPaint
414 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400415 }
416
bsalomon6663acf2016-05-10 09:14:17 -0700417 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000418
Robert Phillipsa29a9562016-10-20 09:40:55 -0400419 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800420 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400421 GrShape shape(rrect, style);
422
Robert Phillips27927a52018-08-20 13:18:12 -0400423 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
424 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425 return;
426 }
Mike Klein744fb732014-06-23 15:13:26 -0400427
bsalomon6663acf2016-05-10 09:14:17 -0700428 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800429
Robert Phillipsa522d662018-08-23 13:50:16 -0400430 GrPaint grPaint;
431 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
432 this->ctm(), &grPaint)) {
433 return;
434 }
435
Chris Dalton3b51df12017-11-27 14:33:06 -0700436 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
437 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438}
439
robertphillipsd7706102016-02-25 09:28:08 -0800440
Robert Phillips20390c32018-08-17 11:01:03 -0400441void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800442 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500443 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800444 if (outer.isEmpty()) {
445 return;
446 }
447
448 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500449 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800450 }
451
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000452 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000453
robertphillips0e7029e2015-11-30 05:45:06 -0800454 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800455 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400456 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
457 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700458 return;
459 }
robertphillips00095892016-02-29 13:50:40 -0800460
Brian Salomon0166cfd2017-03-13 17:58:25 -0400461 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700462 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800463 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000464 }
465
466 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700467 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000468 path.addRRect(outer);
469 path.addRRect(inner);
470 path.setFillType(SkPath::kEvenOdd_FillType);
471
Robert Phillips27927a52018-08-20 13:18:12 -0400472 // TODO: We are losing the possible mutability of the path here but this should probably be
473 // fixed by upgrading GrShape to handle DRRects.
474 GrShape shape(path, paint);
475
476 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
477 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000478}
479
480
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000481/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482
Mike Reeda1361362017-03-07 09:37:29 -0500483void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700484 if (paint.getMaskFilter()) {
485 SkPath path;
486 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400487 path.setIsVolatile(true);
488 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700489 }
490
491 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400492 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
493 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700494 return;
495 }
496
Chris Dalton3b51df12017-11-27 14:33:06 -0700497 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
498 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700499}
500
Mike Reeda1361362017-03-07 09:37:29 -0500501void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800502 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500503 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800504
robertphillips514450c2015-11-24 05:36:02 -0800505 if (paint.getMaskFilter()) {
506 // The RRect path can handle special case blurring
507 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500508 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000509 }
510
511 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400512 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
513 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700514 return;
515 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000516
Chris Dalton3b51df12017-11-27 14:33:06 -0700517 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
518 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000519}
520
Mike Reeda1361362017-03-07 09:37:29 -0500521void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700522 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
523 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500524 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700525 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500526 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700527 return;
528 }
529 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400530 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
531 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700532 return;
533 }
534
Chris Dalton3b51df12017-11-27 14:33:06 -0700535 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500536 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500537 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700538}
539
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541
542///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700543void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700544 const SkPaint& origPaint) {
545 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500546 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400547 // Adding support for round capping would require a
548 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700549 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
550 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
551 SkASSERT(!origPaint.getPathEffect());
552 SkASSERT(!origPaint.getMaskFilter());
553
554 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
555 SkASSERT(halfWidth > 0);
556
557 SkVector v = points[1] - points[0];
558
559 SkScalar length = SkPoint::Normalize(&v);
560 if (!length) {
561 v.fX = 1.0f;
562 v.fY = 0.0f;
563 }
564
565 SkPaint newPaint(origPaint);
566 newPaint.setStyle(SkPaint::kFill_Style);
567
568 SkScalar xtraLength = 0.0f;
569 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
570 xtraLength = halfWidth;
571 }
572
573 SkPoint mid = points[0] + points[1];
574 mid.scale(0.5f);
575
576 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
577 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
578 SkMatrix m;
579 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
580
581 SkMatrix local = m;
582
Mike Reeda1361362017-03-07 09:37:29 -0500583 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700584
585 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400586 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
587 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700588 return;
589 }
590
Brian Salomon82f44312017-01-11 13:42:54 -0500591 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700592 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700593}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594
Robert Phillips20390c32018-08-17 11:01:03 -0400595void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800596 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400597 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700598 SkPoint points[2];
599 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
600 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500601 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700602 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500603 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700604 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500605 // Round capping support is currently disabled b.c. it would require a RRect
606 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500607 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700608 return;
609 }
610 }
robertphillipsff55b492015-11-24 07:56:59 -0800611 }
612
Hal Canary144caf52016-11-07 17:57:18 -0500613 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400614 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500615 GrPaint grPaint;
616 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
617 this->ctm(), &grPaint)) {
618 return;
619 }
620 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
621 this->ctm(), origSrcPath, GrStyle(paint));
622 return;
623 }
Robert Phillips27927a52018-08-20 13:18:12 -0400624
625 // TODO: losing possible mutability of 'origSrcPath' here
626 GrShape shape(origSrcPath, paint);
627
628 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
629 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630}
631
632static const int kBmpSmallTileSize = 1 << 10;
633
634static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
635 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
636 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
637 return tilesX * tilesY;
638}
639
reed85d91782015-09-10 14:33:38 -0700640static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641 if (maxTileSize <= kBmpSmallTileSize) {
642 return maxTileSize;
643 }
644
645 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
646 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
647
648 maxTileTotalTileSize *= maxTileSize * maxTileSize;
649 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
650
651 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
652 return kBmpSmallTileSize;
653 } else {
654 return maxTileSize;
655 }
656}
657
658// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
659// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700660static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800661 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800662 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700663 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700664 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665 const SkRect* srcRectPtr,
666 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700667 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700668 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
669 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 clippedSrcIRect->setEmpty();
671 return;
672 }
673 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
674 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700675 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 if (!clippedSrcRect.intersect(*srcRectPtr)) {
677 clippedSrcIRect->setEmpty();
678 return;
679 }
680 }
681 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700682 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683 if (!clippedSrcIRect->intersect(bmpBounds)) {
684 clippedSrcIRect->setEmpty();
685 }
686}
687
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400688bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
689 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700690 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700691 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400692 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700693 const SkRect* srcRectPtr,
694 int maxTileSize,
695 int* tileSize,
696 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800697 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700698 // if it's larger than the max tile size, then we have no choice but tiling.
699 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400700 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400701 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
702 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700703 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
704 return true;
705 }
706
bsalomon1a1d0b82015-10-16 07:49:42 -0700707 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700708 const size_t area = imageRect.width() * imageRect.height();
709 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
710 return false;
711 }
712
reed85d91782015-09-10 14:33:38 -0700713 // At this point we know we could do the draw by uploading the entire bitmap
714 // as a texture. However, if the texture would be large compared to the
715 // cache size and we don't require most of it for this draw then tile to
716 // reduce the amount of upload and cache spill.
717
718 // assumption here is that sw bitmap size is a good proxy for its size as
719 // a texture
720 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
721 size_t cacheSize;
722 fContext->getResourceCacheLimits(nullptr, &cacheSize);
723 if (bmpSize < cacheSize / 2) {
724 return false;
725 }
726
bsalomon1a1d0b82015-10-16 07:49:42 -0700727 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
728 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400729 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
730 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400731 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700732 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
733 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500734 kBmpSmallTileSize * kBmpSmallTileSize *
735 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700736
Brian Osmand05cdc32017-02-06 13:24:47 -0500737 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700738}
739
reed85d91782015-09-10 14:33:38 -0700740bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
741 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700742 const SkMatrix& viewMatrix,
743 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800744 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400745 // If image is explicitly texture backed then we shouldn't get here.
746 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400748 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700749 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500750 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
751 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
752 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700753
754 int tileFilterPad;
755 if (doBicubic) {
756 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400757 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700758 tileFilterPad = 0;
759 } else {
760 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400762 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400764 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765
reed85d91782015-09-10 14:33:38 -0700766 // these are output, which we safely ignore, as we just want to know the predicate
767 int outTileSize;
768 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769
bsalomone553b642016-08-17 09:02:09 -0700770 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400771 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700772 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773}
774
Mike Reeda1361362017-03-07 09:37:29 -0500775void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400776 SkScalar x,
777 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400779 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800780 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800781 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500782 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700783
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400784 int maxTileSize = this->caps()->maxTileSize();
bsalomonb1b01992015-11-18 10:56:08 -0800785
786 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
787 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400788 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
789 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800790 bitmap.height() <= maxTileSize;
791
792 bool skipTileCheck = drawAA || paint.getMaskFilter();
793
794 if (!skipTileCheck) {
795 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
796 int tileSize;
797 SkIRect clippedSrcRect;
798
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400799 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800800 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400801 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500802 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
803 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800804
805 int tileFilterPad;
806
807 if (doBicubic) {
808 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400809 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800810 tileFilterPad = 0;
811 } else {
812 tileFilterPad = 1;
813 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400814 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800815
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400816 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700817 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400818 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700819 &tileSize, &clippedSrcRect)) {
820 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400821 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
822 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800823 return;
824 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000825 }
Hal Canary144caf52016-11-07 17:57:18 -0500826 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400827 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
828 viewMatrix, paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829}
830
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000831// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
833// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000834static inline void clamped_outset_with_offset(SkIRect* iRect,
835 int outset,
836 SkPoint* offset,
837 const SkIRect& clamp) {
838 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000840 int leftClampDelta = clamp.fLeft - iRect->fLeft;
841 if (leftClampDelta > 0) {
842 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843 iRect->fLeft = clamp.fLeft;
844 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000845 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000846 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000847
848 int topClampDelta = clamp.fTop - iRect->fTop;
849 if (topClampDelta > 0) {
850 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851 iRect->fTop = clamp.fTop;
852 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000853 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854 }
855
856 if (iRect->fRight > clamp.fRight) {
857 iRect->fRight = clamp.fRight;
858 }
859 if (iRect->fBottom > clamp.fBottom) {
860 iRect->fBottom = clamp.fBottom;
861 }
862}
863
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000864// Break 'bitmap' into several tiles to draw it since it has already
865// been determined to be too large to fit in VRAM
866void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800867 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700868 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 const SkRect& srcRect,
870 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400871 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800872 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700873 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000874 int tileSize,
875 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800876 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800877
ericrk983294f2016-04-18 09:14:00 -0700878 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800879 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700880 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800881
bsalomonc55271f2015-11-09 11:55:57 -0800882 const SkPaint* paint = &origPaint;
883 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400884 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800885 // Drop antialiasing to avoid seams at tile boundaries.
886 tempPaint = origPaint;
887 tempPaint.setAntiAlias(false);
888 paint = &tempPaint;
889 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
891
892 int nx = bitmap.width() / tileSize;
893 int ny = bitmap.height() / tileSize;
894 for (int x = 0; x <= nx; x++) {
895 for (int y = 0; y <= ny; y++) {
896 SkRect tileR;
897 tileR.set(SkIntToScalar(x * tileSize),
898 SkIntToScalar(y * tileSize),
899 SkIntToScalar((x + 1) * tileSize),
900 SkIntToScalar((y + 1) * tileSize));
901
902 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
903 continue;
904 }
905
906 if (!tileR.intersect(srcRect)) {
907 continue;
908 }
909
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 SkIRect iTileR;
911 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700912 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
913 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500914 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700915 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400916 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000917 SkIRect iClampRect;
918
reeda5517e22015-07-14 10:54:12 -0700919 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 // In bleed mode we want to always expand the tile on all edges
921 // but stay within the bitmap bounds
922 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
923 } else {
924 // In texture-domain/clamp mode we only want to expand the
925 // tile on edges interior to "srcRect" (i.e., we want to
926 // not bleed across the original clamped edges)
927 srcRect.roundOut(&iClampRect);
928 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000929 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
930 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931 }
932
bsalomone553b642016-08-17 09:02:09 -0700933 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 if (bitmap.extractSubset(&tmpB, iTileR)) {
935 // now offset it to make it "local" to our tmp bitmap
936 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800937 // de-optimized this determination
938 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700939 this->drawBitmapTile(tmpB,
940 viewMatrix,
941 rectToDraw,
942 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500943 params,
bsalomone553b642016-08-17 09:02:09 -0700944 *paint,
945 constraint,
946 bicubic,
947 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 }
949 }
950 }
951}
952
bsalomone553b642016-08-17 09:02:09 -0700953void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
954 const SkMatrix& viewMatrix,
955 const SkRect& dstRect,
956 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400957 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700958 const SkPaint& paint,
959 SkCanvas::SrcRectConstraint constraint,
960 bool bicubic,
961 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800962 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400963 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
964 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700965 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400966 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
967 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400968 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969
Brian Salomon2a943df2018-05-04 13:43:19 -0400970 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400971 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400972 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400973 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 return;
975 }
bsalomone553b642016-08-17 09:02:09 -0700976
bsalomone553b642016-08-17 09:02:09 -0700977 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400978 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
979 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700980
981 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
982 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400983 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700984
reeda5517e22015-07-14 10:54:12 -0700985 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000986 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700987 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500989 domain.fLeft = srcRect.fLeft + 0.5f;
990 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500992 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 }
994 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500995 domain.fTop = srcRect.fTop + 0.5f;
996 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500998 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001000 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001001 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001002 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001003 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001004 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001005 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001006 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001007 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1008 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001009 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001011 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 }
1013
Brian Osman21fc5ce2018-08-27 20:36:19 +00001014 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001015 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001016 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001017 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1018 viewMatrix, std::move(fp),
1019 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001020 return;
1021 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001023 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001024 GrAA aa = GrAA(paint.isAntiAlias() &&
1025 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001026 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027}
1028
Mike Reeda1361362017-03-07 09:37:29 -05001029void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001030 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001031 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001032 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001033
Khushalc421ca12018-06-26 14:38:34 -07001034 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 return;
1036 }
1037
Robert Phillipse14d3052017-02-15 13:18:21 -05001038 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1039 if (!srcImg) {
1040 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001041 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042
Florin Malita53f77bd2017-04-28 13:48:37 -04001043 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001044}
1045
1046
Robert Phillipsc100d482018-07-10 10:11:01 -04001047void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001048 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001049 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001050 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001051
Florin Malita53f77bd2017-04-28 13:48:37 -04001052 // TODO: clipImage support.
1053
robertphillips970587b2016-07-14 14:12:55 -07001054 sk_sp<SkSpecialImage> result;
1055 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001056 SkIPoint offset = { 0, 0 };
1057
1058 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001059 if (!result) {
1060 return;
1061 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001062
1063 left += offset.fX;
1064 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001065 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001066 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001067 }
1068
1069 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001070 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001071 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001072 return;
1073 }
robertphillips970587b2016-07-14 14:12:55 -07001074
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001075 const GrPixelConfig config = proxy->config();
1076
robertphillips970587b2016-07-14 14:12:55 -07001077 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001078 if (tmpUnfiltered.getMaskFilter()) {
1079 SkMatrix ctm = this->ctm();
Robert Phillipsc100d482018-07-10 10:11:01 -04001080 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001081 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001082 }
1083
robertphillips970587b2016-07-14 14:12:55 -07001084 tmpUnfiltered.setImageFilter(nullptr);
1085
Brian Osman2240be92017-10-18 13:15:13 -04001086 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001087 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001088 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001089 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001090 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001091 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001092 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001093 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001094
1095 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001096 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1097 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001098 return;
1099 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100
robertphillips970587b2016-07-14 14:12:55 -07001101 const SkIRect& subset = result->subset();
1102
Brian Salomon82f44312017-01-11 13:42:54 -05001103 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001104 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001105 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001106 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001107 SkMatrix::I(),
Robert Phillipsc100d482018-07-10 10:11:01 -04001108 SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001109 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110}
1111
Mike Reeda1361362017-03-07 09:37:29 -05001112void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001113 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001114 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001115 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001116 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1117 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1118 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1119 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1120 const SkRect* dst = &origDst;
1121 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001123 if (!src) {
1124 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126
bsalomonb1b01992015-11-18 10:56:08 -08001127 SkMatrix srcToDstMatrix;
1128 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1129 return;
1130 }
1131 SkRect tmpSrc, tmpDst;
1132 if (src != &bmpBounds) {
1133 if (!bmpBounds.contains(*src)) {
1134 tmpSrc = *src;
1135 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136 return; // nothing to draw
1137 }
bsalomonb1b01992015-11-18 10:56:08 -08001138 src = &tmpSrc;
1139 srcToDstMatrix.mapRect(&tmpDst, *src);
1140 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 }
1142 }
1143
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001144 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001145
bsalomonb1b01992015-11-18 10:56:08 -08001146 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1147 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001148 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1149 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1150 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001151
Brian Salomon7c8460e2017-05-12 11:36:10 -04001152 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001153
1154 if (!skipTileCheck) {
1155 int tileSize;
1156 SkIRect clippedSrcRect;
1157
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001158 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001159 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001160 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001161 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1162 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001163
1164 int tileFilterPad;
1165
1166 if (doBicubic) {
1167 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001168 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001169 tileFilterPad = 0;
1170 } else {
1171 tileFilterPad = 1;
1172 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001173 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001174
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001175 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001176 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001177 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1178 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001179 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001180 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001181 return;
1182 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001183 }
Hal Canary144caf52016-11-07 17:57:18 -05001184 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001185 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186}
1187
robertphillips6451a0c2016-07-18 08:31:31 -07001188sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001189 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1190 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001191 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1192 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001193 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001194 return nullptr;
1195 }
1196
Robert Phillipse14d3052017-02-15 13:18:21 -05001197 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001198
Robert Phillipse14d3052017-02-15 13:18:21 -05001199 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1200 // the special image
1201 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1202 rect,
1203 bitmap.getGenerationID(),
1204 std::move(proxy),
1205 bitmap.refColorSpace(),
1206 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001207}
1208
reede51c3562016-07-19 14:33:20 -07001209sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001210 SkPixmap pm;
1211 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001212 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001213
Robert Phillips6de99042017-01-31 11:31:39 -05001214 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1215 SkIRect::MakeWH(image->width(), image->height()),
1216 image->uniqueID(),
1217 std::move(proxy),
1218 as_IB(image)->onImageInfo().refColorSpace(),
1219 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001220 } else if (image->peekPixels(&pm)) {
1221 SkBitmap bm;
1222
1223 bm.installPixels(pm);
1224 return this->makeSpecial(bm);
1225 } else {
1226 return nullptr;
1227 }
1228}
1229
1230sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001231 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1232 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001233 // When the device doesn't have a texture, we create a temporary texture.
1234 // TODO: we should actually only copy the portion of the source needed to apply the image
1235 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001236 proxy = GrSurfaceProxy::Copy(fContext.get(),
1237 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001238 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001239 SkBudgeted::kYes);
1240 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001241 return nullptr;
1242 }
robertphillips1b5f9682016-07-15 08:01:12 -07001243 }
1244
1245 const SkImageInfo ii = this->imageInfo();
1246 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1247
Robert Phillipse2f7d182016-12-15 09:23:05 -05001248 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1249 srcRect,
1250 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001251 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001252 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001253 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001254}
1255
Mike Reeda1361362017-03-07 09:37:29 -05001256void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001257 int left, int top, const SkPaint& paint) {
1258 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001259
joshualittce894002016-01-11 13:29:31 -08001260 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001262 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001263
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001265 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001266 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001267 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 return;
1269 }
1270
Mike Reed2179b782018-02-07 11:59:57 -05001271 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272}
1273
Brian Salomon34169692017-08-28 15:32:01 -04001274void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001275 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001276 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001277 viewMatrix.preTranslate(x, y);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001278 if (as_IB(image)->isYUVA()) {
1279 GrYUVAImageTextureMaker maker(fContext.get(), image);
1280 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1281 viewMatrix, paint, false);
1282 return;
1283 }
reed2d5b7142016-08-17 11:12:33 -07001284 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001285 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001286 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1287 image->alphaType(), nullptr, nullptr,
1288 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001289 return;
Brian Salomon34169692017-08-28 15:32:01 -04001290 }
1291 SkBitmap bm;
1292 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1293 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1294 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001295 if (!as_IB(image)->getROPixels(&bm)) {
Brian Salomon34169692017-08-28 15:32:01 -04001296 return;
reed85d91782015-09-10 14:33:38 -07001297 }
Brian Salomon34169692017-08-28 15:32:01 -04001298 this->drawBitmap(bm, x, y, paint);
1299 return;
1300 }
1301 if (image->isLazyGenerated()) {
1302 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001303 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1304 viewMatrix, paint, true);
Brian Salomon34169692017-08-28 15:32:01 -04001305 return;
1306 }
Brian Osmane50cdf02018-10-19 13:02:14 -04001307 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon34169692017-08-28 15:32:01 -04001308 GrBitmapTextureMaker maker(fContext.get(), bm);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001309 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1310 viewMatrix, paint, true);
reeda85d4d02015-05-06 12:56:48 -07001311 }
1312}
1313
Brian Salomon34169692017-08-28 15:32:01 -04001314void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1315 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001316 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -04001317 if (!src || src->contains(image->bounds())) {
1318 constraint = SkCanvas::kFast_SrcRectConstraint;
1319 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001320 if (as_IB(image)->isYUVA()) {
1321 GrYUVAImageTextureMaker maker(fContext.get(), image);
1322 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, false);
1323 return;
1324 }
1325 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001326 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001327 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1328 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001329 return;
1330 }
1331 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001332 SkMatrix srcToDstRect;
1333 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1334 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001335 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001336 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001337 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001338 if (!as_IB(image)->getROPixels(&bm)) {
bsalomonc55271f2015-11-09 11:55:57 -08001339 return;
1340 }
Mike Reeda1361362017-03-07 09:37:29 -05001341 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001342 return;
1343 }
1344 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001345 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001346 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, true);
Brian Salomon34169692017-08-28 15:32:01 -04001347 return;
1348 }
Brian Osmane50cdf02018-10-19 13:02:14 -04001349 if (as_IB(image)->getROPixels(&bm)) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001350 GrBitmapTextureMaker maker(fContext.get(), bm);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001351 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, true);
reeda85d4d02015-05-06 12:56:48 -07001352 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001353}
1354
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001355// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1356static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1357 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1358 return GrSamplerState::Filter::kNearest;
1359 }
1360
1361 return GrSamplerState::Filter::kBilerp;
1362}
1363
Mike Reeda1361362017-03-07 09:37:29 -05001364void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001365 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001366 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001367 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001368 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001369 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001370 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001371 image->alphaType(), pinnedUniqueID,
1372 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001373 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001374 } else {
1375 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001376 if (image->isLazyGenerated()) {
1377 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001378 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001379 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001380 GrBitmapTextureMaker maker(fContext.get(), bm);
1381 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001382 }
1383 }
1384}
1385
Mike Reeda1361362017-03-07 09:37:29 -05001386void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001387 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001388 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001389 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001390 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001391 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001392}
1393
Mike Reeda1361362017-03-07 09:37:29 -05001394void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001395 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1396 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001397 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001398 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001399
Brian Salomon2a943df2018-05-04 13:43:19 -04001400 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1401 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1402 }
msarett10e3d9b2016-08-18 15:46:03 -07001403 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001404 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1405 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001406 return;
1407 }
1408
Brian Salomon2a943df2018-05-04 13:43:19 -04001409 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1410 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Brian Osman6064e1c2018-10-19 14:27:54 -04001411 auto proxy = producer->refTextureProxyForParams(filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001412 if (!proxy) {
1413 return;
1414 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001415 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1416 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001417
Brian Salomon0166cfd2017-03-13 17:58:25 -04001418 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001419 std::move(proxy), std::move(csxf), filter,
1420 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001421}
1422
Mike Reeda1361362017-03-07 09:37:29 -05001423void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001424 const SkCanvas::Lattice& lattice, const SkRect& dst,
1425 const SkPaint& paint) {
1426 ASSERT_SINGLE_OWNER
1427 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001428 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001429 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001430 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001431 image->alphaType(), pinnedUniqueID,
1432 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001433 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001434 } else {
1435 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001436 if (image->isLazyGenerated()) {
1437 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001438 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001439 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001440 GrBitmapTextureMaker maker(fContext.get(), bm);
1441 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001442 }
1443 }
1444}
1445
Mike Reeda1361362017-03-07 09:37:29 -05001446void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001447 const SkCanvas::Lattice& lattice, const SkRect& dst,
1448 const SkPaint& paint) {
1449 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001450 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001451 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001452 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001453}
1454
Brian Salomond7065e72018-10-12 11:42:02 -04001455void SkGpuDevice::drawImageSet(const SkCanvas::ImageSetEntry set[], int count, float alpha,
1456 SkFilterQuality filterQuality, SkBlendMode mode) {
1457 SkASSERT(count > 0);
1458 if (mode != SkBlendMode::kSrcOver ||
1459 !fContext->contextPriv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1460 INHERITED::drawImageSet(set, count, alpha, filterQuality, mode);
1461 return;
1462 }
1463 GrSamplerState sampler;
1464 sampler.setFilterMode(kNone_SkFilterQuality == filterQuality ? GrSamplerState::Filter::kNearest
1465 : GrSamplerState::Filter::kBilerp);
1466 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
Brian Osman3d139a42018-11-19 10:42:10 -05001467 SkPMColor4f color = { alpha, alpha, alpha, alpha };
Brian Salomond7065e72018-10-12 11:42:02 -04001468 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
1469 // issue the accumulated 'n' draws starting at 'base'.
1470 int base = 0, n = 0;
1471 auto draw = [&] {
1472 if (n > 0) {
1473 auto textureXform = GrColorSpaceXform::Make(
1474 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
1475 fRenderTargetContext->colorSpaceInfo().colorSpace(), kPremul_SkAlphaType);
1476 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n,
1477 sampler.filter(), color, this->ctm(),
Brian Osman3d139a42018-11-19 10:42:10 -05001478 std::move(textureXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001479 }
1480 };
1481 for (int i = 0; i < count; ++i) {
Brian Salomon0087c832018-10-15 14:48:20 -04001482 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
1483 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
1484 if (!set[i].fSrcRect.isSorted()) {
1485 draw();
1486 base = i + 1;
1487 n = 0;
1488 continue;
1489 }
Brian Salomonbf612c62018-10-22 09:55:12 -04001490 uint32_t uniqueID;
1491 textures[i].fProxy = as_IB(set[i].fImage.get())->refPinnedTextureProxy(&uniqueID);
1492 if (!textures[i].fProxy) {
1493 textures[i].fProxy =
1494 as_IB(set[i].fImage.get())
1495 ->asTextureProxyRef(fContext.get(), GrSamplerState::ClampBilerp(),
1496 nullptr);
1497 // If we failed to make a proxy then flush the accumulated set and reset for the next
1498 // image.
1499 if (!textures[i].fProxy) {
1500 draw();
1501 base = i + 1;
1502 n = 0;
1503 continue;
1504 }
1505 }
Brian Salomond7065e72018-10-12 11:42:02 -04001506 textures[i].fSrcRect = set[i].fSrcRect;
1507 textures[i].fDstRect = set[i].fDstRect;
1508 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
Brian Salomon0087c832018-10-15 14:48:20 -04001509 if (n > 0 &&
1510 (textures[i].fProxy->textureType() != textures[base].fProxy->textureType() ||
1511 textures[i].fProxy->config() != textures[base].fProxy->config() ||
1512 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
1513 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Brian Salomond7065e72018-10-12 11:42:02 -04001514 draw();
1515 base = i;
1516 n = 1;
1517 } else {
1518 ++n;
1519 }
1520 }
1521 draw();
1522}
1523
Brian Salomonf3569f02017-10-24 12:52:33 -04001524static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1525 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001526 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001527 if (hasTexs && skPaint.getShader()) {
1528 if (hasColors) {
1529 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001530 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1531 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001532 } else {
1533 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001534 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001535 }
1536 } else {
1537 if (hasColors) {
1538 // We have colors, but either have no shader or no texture coords (which implies that
1539 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001540 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001541 } else {
1542 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001543 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001544 }
1545 }
1546}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001547
Mike Reed887cdf12017-04-03 11:11:09 -04001548void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001549 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001550 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001551 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001552 const uint16_t indices[], int indexCount,
1553 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001554 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001555 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001556
Mike Reed2f6b5a42017-03-19 15:04:17 -04001557 SkPaint copy(paint);
1558 copy.setStyle(SkPaint::kStroke_Style);
1559 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001560
Mike Reed2f6b5a42017-03-19 15:04:17 -04001561 GrPaint grPaint;
1562 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001563 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1564 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001565 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566 }
1567
Mike Reed2f6b5a42017-03-19 15:04:17 -04001568 int triangleCount = 0;
1569 int n = (nullptr == indices) ? vertexCount : indexCount;
1570 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001571 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001572 triangleCount = n / 3;
1573 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001574 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001575 triangleCount = n - 2;
1576 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001577 case SkVertices::kTriangleFan_VertexMode:
1578 SK_ABORT("Unexpected triangle fan.");
1579 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001580 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581
Mike Reed2f6b5a42017-03-19 15:04:17 -04001582 VertState state(vertexCount, indices, indexCount);
1583 VertState::Proc vertProc = state.chooseProc(vmode);
1584
1585 //number of indices for lines per triangle with kLines
1586 indexCount = triangleCount * 6;
1587
Brian Osmanae0c50c2017-05-25 16:56:34 -04001588 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1589 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1590 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1591
1592 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001593 int i = 0;
1594 while (vertProc(&state)) {
1595 lineIndices[i] = state.f0;
1596 lineIndices[i + 1] = state.f1;
1597 lineIndices[i + 2] = state.f1;
1598 lineIndices[i + 3] = state.f2;
1599 lineIndices[i + 4] = state.f2;
1600 lineIndices[i + 5] = state.f0;
1601 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001602 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001603
Chris Dalton3809bab2017-06-13 10:55:06 -06001604 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001605 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001606 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001607 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001608 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001609 bones,
1610 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001611 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001612}
1613
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001614void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1615 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001616 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001617 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001618
1619 SkASSERT(vertices);
1620 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001621 bool hasColors = vertices->hasColors();
1622 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001623 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001624 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001625 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001626 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1627 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001628 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001629 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001630 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1631 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001632 return;
1633 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001634 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001635 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1636 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001637}
1638
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639///////////////////////////////////////////////////////////////////////////////
1640
Jim Van Verth3af1af92017-05-18 15:06:54 -04001641void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1642
1643 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001644 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1645
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001646 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001647 // failed to find an accelerated case
1648 this->INHERITED::drawShadow(path, rec);
1649 }
1650}
1651
1652///////////////////////////////////////////////////////////////////////////////
1653
Mike Reeda1361362017-03-07 09:37:29 -05001654void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001655 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001656 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001657 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001658 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001659 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001660 return;
1661 }
1662
Hal Canary144caf52016-11-07 17:57:18 -05001663 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001664
reedca109532015-06-25 16:25:25 -07001665 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001666 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001667
jvanverth31ff7622015-08-07 10:09:28 -07001668 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001669 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001670 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1671 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001672 return;
1673 }
1674 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001675 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1676 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001677 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001678 }
1679 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001680
Brian Salomon0166cfd2017-03-13 17:58:25 -04001681 fRenderTargetContext->drawAtlas(
1682 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001683}
1684
1685///////////////////////////////////////////////////////////////////////////////
1686
Herb Derbyb935cf82018-07-26 16:54:18 -04001687void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001688 ASSERT_SINGLE_OWNER
1689 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
Herb Derbyb983e6b2018-07-13 13:26:29 -04001690
Jim Van Verth87a30112018-09-24 16:13:58 -04001691 // Check for valid input
1692 const SkMatrix& ctm = this->ctm();
1693 const SkPaint& paint = glyphRunList.paint();
1694 if (!ctm.isFinite() || !SkScalarIsFinite(paint.getTextSize()) ||
1695 !SkScalarIsFinite(paint.getTextScaleX()) || !SkScalarIsFinite(paint.getTextSkewX())) {
1696 return;
1697 }
1698
1699 fRenderTargetContext->drawGlyphRunList(this->clip(), ctm, glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001700}
1701
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702///////////////////////////////////////////////////////////////////////////////
1703
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001704void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
1705 GrBackendApi api = this->context()->contextPriv().getBackend();
1706 if (GrBackendApi::kVulkan == api) {
1707 const SkMatrix& ctm = canvas->getTotalMatrix();
1708 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1709 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
1710 drawable->snapGpuDrawHandler(api, combinedMatrix);
1711 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001712 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1713 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001714 }
1715 }
1716 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1717}
1718
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001719
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001720///////////////////////////////////////////////////////////////////////////////
1721
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001722void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001723 this->flushAndSignalSemaphores(0, nullptr);
1724}
1725
Greg Daniel51316782017-08-02 15:10:09 +00001726GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1727 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001728 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001729
Greg Danielc64ee462017-06-15 16:59:49 -04001730 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001731}
1732
Greg Danielc64ee462017-06-15 16:59:49 -04001733bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001734 ASSERT_SINGLE_OWNER
1735
Greg Danielc64ee462017-06-15 16:59:49 -04001736 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001737}
1738
1739///////////////////////////////////////////////////////////////////////////////
1740
reed76033be2015-03-14 10:54:31 -07001741SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001742 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743
robertphillipsca6eafc2016-05-17 09:57:46 -07001744 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001745
robertphillipsca6eafc2016-05-17 09:57:46 -07001746 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001747 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001748 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1749 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001750
Brian Osman10fc6fd2018-03-02 11:01:10 -05001751 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
1752 if (kRGBA_1010102_GrPixelConfig == config) {
1753 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1754 // channel in the layer.
1755 config = kRGBA_8888_GrPixelConfig;
1756 }
1757
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001758 sk_sp<GrRenderTargetContext> rtc(fContext->contextPriv().makeDeferredRenderTargetContext(
Greg Danield7157b22018-11-14 15:28:10 +00001759 fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001760 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001761 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1762 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001763 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001764 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001766
1767 // Skia's convention is to only clear a device if it is non-opaque.
1768 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1769
Robert Phillips9fab7e92016-11-17 12:45:04 -05001770 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1771 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772}
1773
reede8f30622016-03-23 18:59:25 -07001774sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001775 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001776 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001777 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001778 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001779 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001780 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001781}
1782
senorblanco900c3672016-04-27 11:31:23 -07001783SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001784 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001785 // We always return a transient cache, so it is freed after each
1786 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001787 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001788}
reedf037e0b2014-10-30 11:34:15 -07001789