blob: ea18116eb279c5e99e88298fa47365df0211dc66 [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 }
Greg Daniel4065d452018-11-16 15:43:41 -0500167 GrBackendFormat format =
168 context->contextPriv().caps()->getBackendFormatFromColorType(origInfo.colorType());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400169 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
170 // they need to be exact.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500171 return context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500172 format, SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700173 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500174 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400175 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800176}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177
Mike Reeda1361362017-03-07 09:37:29 -0500178sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700179 int left, int top,
180 SkIPoint* offset,
181 const SkImageFilter* filter) {
182 SkASSERT(srcImg->isTextureBacked());
183 SkASSERT(filter);
184
Mike Reeda1361362017-03-07 09:37:29 -0500185 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700186 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500187 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500188 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -0400189 SkColorType colorType;
190 if (!GrPixelConfigToColorType(fRenderTargetContext->colorSpaceInfo().config(), &colorType)) {
191 colorType = kN32_SkColorType;
192 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400193 SkImageFilter::OutputProperties outputProperties(
Brian Osmana50205f2018-07-06 13:57:01 -0400194 colorType, fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700195 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700196
197 return filter->filterImage(srcImg, ctx, offset);
198}
199
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200///////////////////////////////////////////////////////////////////////////////
201
Mike Reed353196f2017-07-21 11:01:18 -0400202bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800203 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000204
Mike Reed353196f2017-07-21 11:01:18 -0400205 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500206 return false;
207 }
208
Mike Reed353196f2017-07-21 11:01:18 -0400209 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500210 if (!rec.trim(this->width(), this->height())) {
211 return false;
212 }
213
214 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000215}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216
Mike Reed353196f2017-07-21 11:01:18 -0400217bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800218 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700219
Mike Reed353196f2017-07-21 11:01:18 -0400220 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500221 return false;
222 }
223
Mike Reed353196f2017-07-21 11:01:18 -0400224 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500225 if (!rec.trim(this->width(), this->height())) {
226 return false;
227 }
228
229 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000230}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000231
reed41e010c2015-06-09 12:16:53 -0700232bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800233 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700234 return false;
235}
236
Brian Osman11052242016-10-27 14:47:55 -0400237GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700238 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400239 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700240}
241
reed8eddfb52014-12-04 07:50:14 -0800242void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800243 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500244 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500245
reed8eddfb52014-12-04 07:50:14 -0800246 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman9a9baae2018-11-05 15:06:26 -0500247 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
248 GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800249}
250
Brian Osman11052242016-10-27 14:47:55 -0400251void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800252 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800253
Brian Osman693a5402016-10-27 15:13:22 -0400254 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800255
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400256 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
257 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400258 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400259 this->context(),
260 budgeted,
261 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400262 fRenderTargetContext->numColorSamples(),
263 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400264 &this->surfaceProps(),
265 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400266 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800267 return;
268 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400269 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800270
271 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400272 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800273 return;
274 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500275 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800276 }
277
Brian Osman693a5402016-10-27 15:13:22 -0400278 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800279}
280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281///////////////////////////////////////////////////////////////////////////////
282
Mike Reeda1361362017-03-07 09:37:29 -0500283void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800284 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500285 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286
287 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400288 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
289 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700290 return;
291 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292
Brian Salomon0166cfd2017-03-13 17:58:25 -0400293 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294}
295
Brian Salomon1459ebd2017-12-19 16:12:44 -0500296static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
297 switch (mode) {
298 case SkCanvas::kPoints_PointMode:
299 return GrPrimitiveType::kPoints;
300 case SkCanvas::kLines_PointMode:
301 return GrPrimitiveType::kLines;
302 case SkCanvas::kPolygon_PointMode:
303 return GrPrimitiveType::kLineStrip;
304 }
305 SK_ABORT("Unexpected mode");
306 return GrPrimitiveType::kPoints;
307}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308
Mike Reeda1361362017-03-07 09:37:29 -0500309void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800311 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500312 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000313 SkScalar width = paint.getStrokeWidth();
314 if (width < 0) {
315 return;
316 }
317
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000318 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700319 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700320 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400321 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
322 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700323 return;
324 }
egdaniele61c4112014-06-12 10:24:21 -0700325 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700326 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700327 path.moveTo(pts[0]);
328 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700329 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
330 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700331 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000332 }
333
bsalomon6ade6dd2016-09-12 12:07:17 -0700334 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500335 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700336 SkScalarNearlyEqual(scales[0], 1.f) &&
337 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700338 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000339 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400340 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500341 SkRasterClip rc(this->devClipBounds());
342 SkDraw draw;
343 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
344 draw.fMatrix = &this->ctm();
345 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500346 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347 return;
348 }
349
Brian Salomon1459ebd2017-12-19 16:12:44 -0500350 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700351
Mike Reeda1361362017-03-07 09:37:29 -0500352 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700353#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
354 // This offsetting in device space matches the expectations of the Android framework for non-AA
355 // points and lines.
356 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600357 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700358 tempMatrix = *viewMatrix;
359 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
360 tempMatrix.postTranslate(kOffset, kOffset);
361 viewMatrix = &tempMatrix;
362 }
363#endif
364
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400366 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
367 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700368 return;
369 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000370
Brian Osmanae0c50c2017-05-25 16:56:34 -0400371 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
372 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
373 nullptr);
374
375 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400376 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377}
378
379///////////////////////////////////////////////////////////////////////////////
380
Mike Reeda1361362017-03-07 09:37:29 -0500381void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800382 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500383 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400384
385 GrStyle style(paint);
386
bsalomona7d85ba2016-07-06 11:54:59 -0700387 // A couple reasons we might need to call drawPath.
388 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400389 GrShape shape(rect, style);
390
391 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
392 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393 return;
394 }
395
396 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400397 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
398 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700399 return;
400 }
Mike Klein744fb732014-06-23 15:13:26 -0400401
Chris Dalton3b51df12017-11-27 14:33:06 -0700402 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
403 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404}
405
406///////////////////////////////////////////////////////////////////////////////
407
Mike Reeda1361362017-03-07 09:37:29 -0500408void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800409 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500410 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400411
Mike Reed80747ef2018-01-23 15:29:32 -0500412 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000413 if (mf) {
414 if (mf->hasFragmentProcessor()) {
415 mf = nullptr; // already handled in SkPaintToGrPaint
416 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400417 }
418
bsalomon6663acf2016-05-10 09:14:17 -0700419 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000420
Robert Phillipsa29a9562016-10-20 09:40:55 -0400421 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800422 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400423 GrShape shape(rrect, style);
424
Robert Phillips27927a52018-08-20 13:18:12 -0400425 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
426 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427 return;
428 }
Mike Klein744fb732014-06-23 15:13:26 -0400429
bsalomon6663acf2016-05-10 09:14:17 -0700430 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800431
Robert Phillipsa522d662018-08-23 13:50:16 -0400432 GrPaint grPaint;
433 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
434 this->ctm(), &grPaint)) {
435 return;
436 }
437
Chris Dalton3b51df12017-11-27 14:33:06 -0700438 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
439 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440}
441
robertphillipsd7706102016-02-25 09:28:08 -0800442
Robert Phillips20390c32018-08-17 11:01:03 -0400443void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800444 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500445 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800446 if (outer.isEmpty()) {
447 return;
448 }
449
450 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500451 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800452 }
453
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000454 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000455
robertphillips0e7029e2015-11-30 05:45:06 -0800456 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800457 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400458 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
459 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700460 return;
461 }
robertphillips00095892016-02-29 13:50:40 -0800462
Brian Salomon0166cfd2017-03-13 17:58:25 -0400463 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700464 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800465 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000466 }
467
468 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700469 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000470 path.addRRect(outer);
471 path.addRRect(inner);
472 path.setFillType(SkPath::kEvenOdd_FillType);
473
Robert Phillips27927a52018-08-20 13:18:12 -0400474 // TODO: We are losing the possible mutability of the path here but this should probably be
475 // fixed by upgrading GrShape to handle DRRects.
476 GrShape shape(path, paint);
477
478 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
479 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000480}
481
482
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000483/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000484
Mike Reeda1361362017-03-07 09:37:29 -0500485void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700486 if (paint.getMaskFilter()) {
487 SkPath path;
488 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400489 path.setIsVolatile(true);
490 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700491 }
492
493 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400494 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
495 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700496 return;
497 }
498
Chris Dalton3b51df12017-11-27 14:33:06 -0700499 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
500 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700501}
502
Mike Reeda1361362017-03-07 09:37:29 -0500503void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800504 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500505 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800506
robertphillips514450c2015-11-24 05:36:02 -0800507 if (paint.getMaskFilter()) {
508 // The RRect path can handle special case blurring
509 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500510 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000511 }
512
513 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400514 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
515 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700516 return;
517 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518
Chris Dalton3b51df12017-11-27 14:33:06 -0700519 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
520 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000521}
522
Mike Reeda1361362017-03-07 09:37:29 -0500523void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700524 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
525 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500526 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700527 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500528 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700529 return;
530 }
531 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400532 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
533 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700534 return;
535 }
536
Chris Dalton3b51df12017-11-27 14:33:06 -0700537 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500538 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500539 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700540}
541
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543
544///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700545void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700546 const SkPaint& origPaint) {
547 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500548 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400549 // Adding support for round capping would require a
550 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700551 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
552 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
553 SkASSERT(!origPaint.getPathEffect());
554 SkASSERT(!origPaint.getMaskFilter());
555
556 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
557 SkASSERT(halfWidth > 0);
558
559 SkVector v = points[1] - points[0];
560
561 SkScalar length = SkPoint::Normalize(&v);
562 if (!length) {
563 v.fX = 1.0f;
564 v.fY = 0.0f;
565 }
566
567 SkPaint newPaint(origPaint);
568 newPaint.setStyle(SkPaint::kFill_Style);
569
570 SkScalar xtraLength = 0.0f;
571 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
572 xtraLength = halfWidth;
573 }
574
575 SkPoint mid = points[0] + points[1];
576 mid.scale(0.5f);
577
578 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
579 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
580 SkMatrix m;
581 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
582
583 SkMatrix local = m;
584
Mike Reeda1361362017-03-07 09:37:29 -0500585 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700586
587 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400588 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
589 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700590 return;
591 }
592
Brian Salomon82f44312017-01-11 13:42:54 -0500593 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700594 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700595}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596
Robert Phillips20390c32018-08-17 11:01:03 -0400597void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800598 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400599 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700600 SkPoint points[2];
601 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
602 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500603 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700604 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500605 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700606 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500607 // Round capping support is currently disabled b.c. it would require a RRect
608 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500609 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700610 return;
611 }
612 }
robertphillipsff55b492015-11-24 07:56:59 -0800613 }
614
Hal Canary144caf52016-11-07 17:57:18 -0500615 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400616 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500617 GrPaint grPaint;
618 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
619 this->ctm(), &grPaint)) {
620 return;
621 }
622 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
623 this->ctm(), origSrcPath, GrStyle(paint));
624 return;
625 }
Robert Phillips27927a52018-08-20 13:18:12 -0400626
627 // TODO: losing possible mutability of 'origSrcPath' here
628 GrShape shape(origSrcPath, paint);
629
630 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
631 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632}
633
634static const int kBmpSmallTileSize = 1 << 10;
635
636static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
637 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
638 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
639 return tilesX * tilesY;
640}
641
reed85d91782015-09-10 14:33:38 -0700642static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643 if (maxTileSize <= kBmpSmallTileSize) {
644 return maxTileSize;
645 }
646
647 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
648 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
649
650 maxTileTotalTileSize *= maxTileSize * maxTileSize;
651 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
652
653 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
654 return kBmpSmallTileSize;
655 } else {
656 return maxTileSize;
657 }
658}
659
660// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
661// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700662static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800663 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800664 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700665 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700666 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000667 const SkRect* srcRectPtr,
668 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700669 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700670 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
671 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 clippedSrcIRect->setEmpty();
673 return;
674 }
675 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
676 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700677 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 if (!clippedSrcRect.intersect(*srcRectPtr)) {
679 clippedSrcIRect->setEmpty();
680 return;
681 }
682 }
683 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700684 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000685 if (!clippedSrcIRect->intersect(bmpBounds)) {
686 clippedSrcIRect->setEmpty();
687 }
688}
689
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400690bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
691 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700692 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700693 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400694 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700695 const SkRect* srcRectPtr,
696 int maxTileSize,
697 int* tileSize,
698 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800699 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700700 // if it's larger than the max tile size, then we have no choice but tiling.
701 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400702 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400703 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
704 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700705 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
706 return true;
707 }
708
bsalomon1a1d0b82015-10-16 07:49:42 -0700709 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700710 const size_t area = imageRect.width() * imageRect.height();
711 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
712 return false;
713 }
714
reed85d91782015-09-10 14:33:38 -0700715 // At this point we know we could do the draw by uploading the entire bitmap
716 // as a texture. However, if the texture would be large compared to the
717 // cache size and we don't require most of it for this draw then tile to
718 // reduce the amount of upload and cache spill.
719
720 // assumption here is that sw bitmap size is a good proxy for its size as
721 // a texture
722 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
723 size_t cacheSize;
724 fContext->getResourceCacheLimits(nullptr, &cacheSize);
725 if (bmpSize < cacheSize / 2) {
726 return false;
727 }
728
bsalomon1a1d0b82015-10-16 07:49:42 -0700729 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
730 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400731 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
732 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400733 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700734 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
735 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500736 kBmpSmallTileSize * kBmpSmallTileSize *
737 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700738
Brian Osmand05cdc32017-02-06 13:24:47 -0500739 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700740}
741
reed85d91782015-09-10 14:33:38 -0700742bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
743 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700744 const SkMatrix& viewMatrix,
745 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800746 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400747 // If image is explicitly texture backed then we shouldn't get here.
748 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400750 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700751 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500752 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
753 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
754 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700755
756 int tileFilterPad;
757 if (doBicubic) {
758 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400759 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700760 tileFilterPad = 0;
761 } else {
762 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400764 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400766 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767
reed85d91782015-09-10 14:33:38 -0700768 // these are output, which we safely ignore, as we just want to know the predicate
769 int outTileSize;
770 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771
bsalomone553b642016-08-17 09:02:09 -0700772 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400773 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700774 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775}
776
Mike Reeda1361362017-03-07 09:37:29 -0500777void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400778 SkScalar x,
779 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400781 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800782 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800783 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500784 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700785
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400786 int maxTileSize = this->caps()->maxTileSize();
bsalomonb1b01992015-11-18 10:56:08 -0800787
788 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
789 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400790 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
791 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800792 bitmap.height() <= maxTileSize;
793
794 bool skipTileCheck = drawAA || paint.getMaskFilter();
795
796 if (!skipTileCheck) {
797 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
798 int tileSize;
799 SkIRect clippedSrcRect;
800
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400801 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800802 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400803 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500804 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
805 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800806
807 int tileFilterPad;
808
809 if (doBicubic) {
810 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400811 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800812 tileFilterPad = 0;
813 } else {
814 tileFilterPad = 1;
815 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400816 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800817
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400818 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700819 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400820 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700821 &tileSize, &clippedSrcRect)) {
822 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400823 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
824 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800825 return;
826 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000827 }
Hal Canary144caf52016-11-07 17:57:18 -0500828 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400829 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
830 viewMatrix, paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831}
832
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000833// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000834// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
835// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000836static inline void clamped_outset_with_offset(SkIRect* iRect,
837 int outset,
838 SkPoint* offset,
839 const SkIRect& clamp) {
840 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000842 int leftClampDelta = clamp.fLeft - iRect->fLeft;
843 if (leftClampDelta > 0) {
844 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000845 iRect->fLeft = clamp.fLeft;
846 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000847 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000849
850 int topClampDelta = clamp.fTop - iRect->fTop;
851 if (topClampDelta > 0) {
852 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853 iRect->fTop = clamp.fTop;
854 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000855 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 }
857
858 if (iRect->fRight > clamp.fRight) {
859 iRect->fRight = clamp.fRight;
860 }
861 if (iRect->fBottom > clamp.fBottom) {
862 iRect->fBottom = clamp.fBottom;
863 }
864}
865
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000866// Break 'bitmap' into several tiles to draw it since it has already
867// been determined to be too large to fit in VRAM
868void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800869 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700870 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871 const SkRect& srcRect,
872 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400873 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800874 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700875 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000876 int tileSize,
877 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800878 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800879
ericrk983294f2016-04-18 09:14:00 -0700880 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800881 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700882 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800883
bsalomonc55271f2015-11-09 11:55:57 -0800884 const SkPaint* paint = &origPaint;
885 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400886 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800887 // Drop antialiasing to avoid seams at tile boundaries.
888 tempPaint = origPaint;
889 tempPaint.setAntiAlias(false);
890 paint = &tempPaint;
891 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000892 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
893
894 int nx = bitmap.width() / tileSize;
895 int ny = bitmap.height() / tileSize;
896 for (int x = 0; x <= nx; x++) {
897 for (int y = 0; y <= ny; y++) {
898 SkRect tileR;
899 tileR.set(SkIntToScalar(x * tileSize),
900 SkIntToScalar(y * tileSize),
901 SkIntToScalar((x + 1) * tileSize),
902 SkIntToScalar((y + 1) * tileSize));
903
904 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
905 continue;
906 }
907
908 if (!tileR.intersect(srcRect)) {
909 continue;
910 }
911
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 SkIRect iTileR;
913 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700914 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
915 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500916 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700917 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400918 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000919 SkIRect iClampRect;
920
reeda5517e22015-07-14 10:54:12 -0700921 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922 // In bleed mode we want to always expand the tile on all edges
923 // but stay within the bitmap bounds
924 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
925 } else {
926 // In texture-domain/clamp mode we only want to expand the
927 // tile on edges interior to "srcRect" (i.e., we want to
928 // not bleed across the original clamped edges)
929 srcRect.roundOut(&iClampRect);
930 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000931 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
932 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 }
934
bsalomone553b642016-08-17 09:02:09 -0700935 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936 if (bitmap.extractSubset(&tmpB, iTileR)) {
937 // now offset it to make it "local" to our tmp bitmap
938 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800939 // de-optimized this determination
940 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700941 this->drawBitmapTile(tmpB,
942 viewMatrix,
943 rectToDraw,
944 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500945 params,
bsalomone553b642016-08-17 09:02:09 -0700946 *paint,
947 constraint,
948 bicubic,
949 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 }
951 }
952 }
953}
954
bsalomone553b642016-08-17 09:02:09 -0700955void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
956 const SkMatrix& viewMatrix,
957 const SkRect& dstRect,
958 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400959 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700960 const SkPaint& paint,
961 SkCanvas::SrcRectConstraint constraint,
962 bool bicubic,
963 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800964 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400965 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
966 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700967 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400968 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
969 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400970 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000971
Brian Salomon2a943df2018-05-04 13:43:19 -0400972 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400973 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400974 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400975 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 return;
977 }
bsalomone553b642016-08-17 09:02:09 -0700978
bsalomone553b642016-08-17 09:02:09 -0700979 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400980 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
981 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700982
983 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
984 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400985 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700986
reeda5517e22015-07-14 10:54:12 -0700987 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700989 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500991 domain.fLeft = srcRect.fLeft + 0.5f;
992 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500994 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 }
996 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500997 domain.fTop = srcRect.fTop + 0.5f;
998 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001000 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001002 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001003 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001004 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001005 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001006 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001007 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001008 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001009 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1010 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001011 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001013 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 }
1015
Brian Osman21fc5ce2018-08-27 20:36:19 +00001016 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001017 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001018 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001019 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1020 viewMatrix, std::move(fp),
1021 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001022 return;
1023 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001025 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001026 GrAA aa = GrAA(paint.isAntiAlias() &&
1027 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001028 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029}
1030
Mike Reeda1361362017-03-07 09:37:29 -05001031void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001033 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001034 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035
Khushalc421ca12018-06-26 14:38:34 -07001036 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 return;
1038 }
1039
Robert Phillipse14d3052017-02-15 13:18:21 -05001040 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1041 if (!srcImg) {
1042 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001043 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001044
Florin Malita53f77bd2017-04-28 13:48:37 -04001045 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001046}
1047
1048
Robert Phillipsc100d482018-07-10 10:11:01 -04001049void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001050 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001051 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001052 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001053
Florin Malita53f77bd2017-04-28 13:48:37 -04001054 // TODO: clipImage support.
1055
robertphillips970587b2016-07-14 14:12:55 -07001056 sk_sp<SkSpecialImage> result;
1057 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001058 SkIPoint offset = { 0, 0 };
1059
1060 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001061 if (!result) {
1062 return;
1063 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001064
1065 left += offset.fX;
1066 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001067 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001068 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001069 }
1070
1071 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001072 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001073 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001074 return;
1075 }
robertphillips970587b2016-07-14 14:12:55 -07001076
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001077 const GrPixelConfig config = proxy->config();
1078
robertphillips970587b2016-07-14 14:12:55 -07001079 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001080 if (tmpUnfiltered.getMaskFilter()) {
1081 SkMatrix ctm = this->ctm();
Robert Phillipsc100d482018-07-10 10:11:01 -04001082 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001083 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001084 }
1085
robertphillips970587b2016-07-14 14:12:55 -07001086 tmpUnfiltered.setImageFilter(nullptr);
1087
Brian Osman2240be92017-10-18 13:15:13 -04001088 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001089 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001090 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001091 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001092 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001093 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001094 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001095 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001096
1097 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001098 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1099 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001100 return;
1101 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102
robertphillips970587b2016-07-14 14:12:55 -07001103 const SkIRect& subset = result->subset();
1104
Brian Salomon82f44312017-01-11 13:42:54 -05001105 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001106 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001107 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001108 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001109 SkMatrix::I(),
Robert Phillipsc100d482018-07-10 10:11:01 -04001110 SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001111 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112}
1113
Mike Reeda1361362017-03-07 09:37:29 -05001114void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001115 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001116 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001117 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001118 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1119 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1120 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1121 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1122 const SkRect* dst = &origDst;
1123 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001125 if (!src) {
1126 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128
bsalomonb1b01992015-11-18 10:56:08 -08001129 SkMatrix srcToDstMatrix;
1130 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1131 return;
1132 }
1133 SkRect tmpSrc, tmpDst;
1134 if (src != &bmpBounds) {
1135 if (!bmpBounds.contains(*src)) {
1136 tmpSrc = *src;
1137 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138 return; // nothing to draw
1139 }
bsalomonb1b01992015-11-18 10:56:08 -08001140 src = &tmpSrc;
1141 srcToDstMatrix.mapRect(&tmpDst, *src);
1142 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 }
1144 }
1145
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001146 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001147
bsalomonb1b01992015-11-18 10:56:08 -08001148 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1149 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001150 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1151 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1152 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001153
Brian Salomon7c8460e2017-05-12 11:36:10 -04001154 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001155
1156 if (!skipTileCheck) {
1157 int tileSize;
1158 SkIRect clippedSrcRect;
1159
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001160 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001161 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001162 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001163 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1164 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001165
1166 int tileFilterPad;
1167
1168 if (doBicubic) {
1169 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001170 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001171 tileFilterPad = 0;
1172 } else {
1173 tileFilterPad = 1;
1174 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001175 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001176
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001177 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001178 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001179 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1180 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001181 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001182 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001183 return;
1184 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001185 }
Hal Canary144caf52016-11-07 17:57:18 -05001186 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001187 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188}
1189
robertphillips6451a0c2016-07-18 08:31:31 -07001190sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001191 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1192 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001193 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1194 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001195 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001196 return nullptr;
1197 }
1198
Robert Phillipse14d3052017-02-15 13:18:21 -05001199 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001200
Robert Phillipse14d3052017-02-15 13:18:21 -05001201 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1202 // the special image
1203 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1204 rect,
1205 bitmap.getGenerationID(),
1206 std::move(proxy),
1207 bitmap.refColorSpace(),
1208 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001209}
1210
reede51c3562016-07-19 14:33:20 -07001211sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001212 SkPixmap pm;
1213 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001214 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001215
Robert Phillips6de99042017-01-31 11:31:39 -05001216 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1217 SkIRect::MakeWH(image->width(), image->height()),
1218 image->uniqueID(),
1219 std::move(proxy),
1220 as_IB(image)->onImageInfo().refColorSpace(),
1221 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001222 } else if (image->peekPixels(&pm)) {
1223 SkBitmap bm;
1224
1225 bm.installPixels(pm);
1226 return this->makeSpecial(bm);
1227 } else {
1228 return nullptr;
1229 }
1230}
1231
1232sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Greg Danielbe7fc462019-01-03 16:40:42 -05001233 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1234 // since it would require us to make a copy of the underlying VkImage which we don't have access
1235 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1236 // command buffer.
1237 if (this->accessRenderTargetContext()->wrapsVkSecondaryCB()) {
1238 return nullptr;
1239 }
1240
Robert Phillips63c67462017-02-15 14:19:01 -05001241 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1242 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001243 // When the device doesn't have a texture, we create a temporary texture.
1244 // TODO: we should actually only copy the portion of the source needed to apply the image
1245 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001246 proxy = GrSurfaceProxy::Copy(fContext.get(),
1247 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001248 GrMipMapped::kNo,
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001249 SkBackingFit::kApprox,
Robert Phillips63c67462017-02-15 14:19:01 -05001250 SkBudgeted::kYes);
1251 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001252 return nullptr;
1253 }
robertphillips1b5f9682016-07-15 08:01:12 -07001254 }
1255
1256 const SkImageInfo ii = this->imageInfo();
1257 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1258
Robert Phillipse2f7d182016-12-15 09:23:05 -05001259 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1260 srcRect,
1261 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001262 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001263 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001264 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001265}
1266
Mike Reed148b7fd2018-12-18 17:38:18 -05001267sk_sp<SkSpecialImage> SkGpuDevice::snapBackImage(const SkIRect& subset) {
1268 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
Greg Danielbe7fc462019-01-03 16:40:42 -05001269
1270 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1271 // since it would require us to make a copy of the underlying VkImage which we don't have access
1272 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1273 // command buffer.
1274 if (rtc->wrapsVkSecondaryCB()) {
Mike Reed148b7fd2018-12-18 17:38:18 -05001275 return nullptr;
1276 }
1277
Greg Danielbe7fc462019-01-03 16:40:42 -05001278
Mike Reed148b7fd2018-12-18 17:38:18 -05001279 GrContext* ctx = this->context();
Greg Danielbe7fc462019-01-03 16:40:42 -05001280 SkASSERT(rtc->asSurfaceProxy());
Mike Reed148b7fd2018-12-18 17:38:18 -05001281
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001282 auto srcProxy =
1283 GrSurfaceProxy::Copy(ctx, rtc->asSurfaceProxy(), rtc->mipMapped(), subset,
1284 SkBackingFit::kApprox, rtc->asSurfaceProxy()->isBudgeted());
Mike Reed148b7fd2018-12-18 17:38:18 -05001285 if (!srcProxy) {
1286 return nullptr;
1287 }
1288
1289 // Note, can't move srcProxy since we also refer to this in the 2nd parameter
1290 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1291 SkIRect::MakeSize(srcProxy->isize()),
1292 kNeedNewImageUniqueID_SpecialImage,
1293 srcProxy,
1294 this->imageInfo().refColorSpace(),
1295 &this->surfaceProps());
1296}
1297
Mike Reeda1361362017-03-07 09:37:29 -05001298void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001299 int left, int top, const SkPaint& paint) {
1300 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001301
joshualittce894002016-01-11 13:29:31 -08001302 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001304 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001305
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001307 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001308 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001309 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001310 return;
1311 }
1312
Mike Reed2179b782018-02-07 11:59:57 -05001313 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314}
1315
Brian Salomon34169692017-08-28 15:32:01 -04001316void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001317 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001318 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001319 viewMatrix.preTranslate(x, y);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001320 if (as_IB(image)->isYUVA()) {
1321 GrYUVAImageTextureMaker maker(fContext.get(), image);
1322 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1323 viewMatrix, paint, false);
1324 return;
1325 }
reed2d5b7142016-08-17 11:12:33 -07001326 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001327 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001328 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1329 image->alphaType(), nullptr, nullptr,
1330 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001331 return;
Brian Salomon34169692017-08-28 15:32:01 -04001332 }
1333 SkBitmap bm;
1334 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1335 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1336 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001337 if (!as_IB(image)->getROPixels(&bm)) {
Brian Salomon34169692017-08-28 15:32:01 -04001338 return;
reed85d91782015-09-10 14:33:38 -07001339 }
Brian Salomon34169692017-08-28 15:32:01 -04001340 this->drawBitmap(bm, x, y, paint);
1341 return;
1342 }
1343 if (image->isLazyGenerated()) {
1344 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001345 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1346 viewMatrix, 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)) {
Brian Salomon34169692017-08-28 15:32:01 -04001350 GrBitmapTextureMaker maker(fContext.get(), bm);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001351 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1352 viewMatrix, paint, true);
reeda85d4d02015-05-06 12:56:48 -07001353 }
1354}
1355
Brian Salomon34169692017-08-28 15:32:01 -04001356void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1357 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001358 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -04001359 if (!src || src->contains(image->bounds())) {
1360 constraint = SkCanvas::kFast_SrcRectConstraint;
1361 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001362 if (as_IB(image)->isYUVA()) {
1363 GrYUVAImageTextureMaker maker(fContext.get(), image);
1364 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, false);
1365 return;
1366 }
1367 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001368 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001369 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1370 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001371 return;
1372 }
1373 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001374 SkMatrix srcToDstRect;
1375 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1376 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001377 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001378 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001379 // only support tiling as bitmap at the moment, so force raster-version
Brian Osmane50cdf02018-10-19 13:02:14 -04001380 if (!as_IB(image)->getROPixels(&bm)) {
bsalomonc55271f2015-11-09 11:55:57 -08001381 return;
1382 }
Mike Reeda1361362017-03-07 09:37:29 -05001383 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001384 return;
1385 }
1386 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001387 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001388 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, true);
Brian Salomon34169692017-08-28 15:32:01 -04001389 return;
1390 }
Brian Osmane50cdf02018-10-19 13:02:14 -04001391 if (as_IB(image)->getROPixels(&bm)) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001392 GrBitmapTextureMaker maker(fContext.get(), bm);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001393 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), paint, true);
reeda85d4d02015-05-06 12:56:48 -07001394 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001395}
1396
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001397// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1398static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1399 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1400 return GrSamplerState::Filter::kNearest;
1401 }
1402
1403 return GrSamplerState::Filter::kBilerp;
1404}
1405
Mike Reeda1361362017-03-07 09:37:29 -05001406void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001407 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001408 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001409 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001410 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001411 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001412 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001413 image->alphaType(), pinnedUniqueID,
1414 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001415 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001416 } else {
1417 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001418 if (image->isLazyGenerated()) {
1419 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001420 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001421 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001422 GrBitmapTextureMaker maker(fContext.get(), bm);
1423 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001424 }
1425 }
1426}
1427
Mike Reeda1361362017-03-07 09:37:29 -05001428void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001429 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001430 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001431 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001432 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001433 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001434}
1435
Mike Reeda1361362017-03-07 09:37:29 -05001436void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001437 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1438 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001439 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001440 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001441
Brian Salomon2a943df2018-05-04 13:43:19 -04001442 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1443 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1444 }
msarett10e3d9b2016-08-18 15:46:03 -07001445 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001446 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1447 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001448 return;
1449 }
1450
Brian Salomon2a943df2018-05-04 13:43:19 -04001451 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1452 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Brian Osman6064e1c2018-10-19 14:27:54 -04001453 auto proxy = producer->refTextureProxyForParams(filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001454 if (!proxy) {
1455 return;
1456 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001457 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1458 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001459
Brian Salomon0166cfd2017-03-13 17:58:25 -04001460 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001461 std::move(proxy), std::move(csxf), filter,
1462 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001463}
1464
Mike Reeda1361362017-03-07 09:37:29 -05001465void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001466 const SkCanvas::Lattice& lattice, const SkRect& dst,
1467 const SkPaint& paint) {
1468 ASSERT_SINGLE_OWNER
1469 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001470 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001471 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001472 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001473 image->alphaType(), pinnedUniqueID,
1474 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001475 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001476 } else {
1477 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001478 if (image->isLazyGenerated()) {
1479 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001480 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001481 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001482 GrBitmapTextureMaker maker(fContext.get(), bm);
1483 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001484 }
1485 }
1486}
1487
Mike Reeda1361362017-03-07 09:37:29 -05001488void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001489 const SkCanvas::Lattice& lattice, const SkRect& dst,
1490 const SkPaint& paint) {
1491 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001492 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001493 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001494 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001495}
1496
Brian Salomond003d222018-11-26 13:25:05 -05001497void SkGpuDevice::drawImageSet(const SkCanvas::ImageSetEntry set[], int count,
Brian Salomond7065e72018-10-12 11:42:02 -04001498 SkFilterQuality filterQuality, SkBlendMode mode) {
1499 SkASSERT(count > 0);
1500 if (mode != SkBlendMode::kSrcOver ||
1501 !fContext->contextPriv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Brian Salomond003d222018-11-26 13:25:05 -05001502 INHERITED::drawImageSet(set, count, filterQuality, mode);
Brian Salomond7065e72018-10-12 11:42:02 -04001503 return;
1504 }
1505 GrSamplerState sampler;
1506 sampler.setFilterMode(kNone_SkFilterQuality == filterQuality ? GrSamplerState::Filter::kNearest
1507 : GrSamplerState::Filter::kBilerp);
1508 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
Brian Salomond7065e72018-10-12 11:42:02 -04001509 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
1510 // issue the accumulated 'n' draws starting at 'base'.
1511 int base = 0, n = 0;
1512 auto draw = [&] {
1513 if (n > 0) {
1514 auto textureXform = GrColorSpaceXform::Make(
1515 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
1516 fRenderTargetContext->colorSpaceInfo().colorSpace(), kPremul_SkAlphaType);
1517 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n,
Brian Salomond003d222018-11-26 13:25:05 -05001518 sampler.filter(), this->ctm(),
Brian Osman3d139a42018-11-19 10:42:10 -05001519 std::move(textureXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001520 }
1521 };
1522 for (int i = 0; i < count; ++i) {
Brian Salomon0087c832018-10-15 14:48:20 -04001523 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
1524 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
1525 if (!set[i].fSrcRect.isSorted()) {
1526 draw();
1527 base = i + 1;
1528 n = 0;
1529 continue;
1530 }
Brian Salomonbf612c62018-10-22 09:55:12 -04001531 uint32_t uniqueID;
1532 textures[i].fProxy = as_IB(set[i].fImage.get())->refPinnedTextureProxy(&uniqueID);
1533 if (!textures[i].fProxy) {
1534 textures[i].fProxy =
1535 as_IB(set[i].fImage.get())
1536 ->asTextureProxyRef(fContext.get(), GrSamplerState::ClampBilerp(),
1537 nullptr);
1538 // If we failed to make a proxy then flush the accumulated set and reset for the next
1539 // image.
1540 if (!textures[i].fProxy) {
1541 draw();
1542 base = i + 1;
1543 n = 0;
1544 continue;
1545 }
1546 }
Brian Salomond7065e72018-10-12 11:42:02 -04001547 textures[i].fSrcRect = set[i].fSrcRect;
1548 textures[i].fDstRect = set[i].fDstRect;
Brian Salomon1da5cad2018-11-21 09:21:18 -05001549 textures[i].fAlpha = set[i].fAlpha;
Brian Salomond7065e72018-10-12 11:42:02 -04001550 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
Brian Salomon0087c832018-10-15 14:48:20 -04001551 if (n > 0 &&
Greg Daniel45723ac2018-11-30 10:12:43 -05001552 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(textures[i].fProxy.get(),
1553 textures[base].fProxy.get()) ||
Brian Salomon0087c832018-10-15 14:48:20 -04001554 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
1555 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Brian Salomond7065e72018-10-12 11:42:02 -04001556 draw();
1557 base = i;
1558 n = 1;
1559 } else {
1560 ++n;
1561 }
1562 }
1563 draw();
1564}
1565
Brian Salomonf3569f02017-10-24 12:52:33 -04001566static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1567 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001568 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001569 if (hasTexs && skPaint.getShader()) {
1570 if (hasColors) {
1571 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001572 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1573 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001574 } else {
1575 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001576 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001577 }
1578 } else {
1579 if (hasColors) {
1580 // We have colors, but either have no shader or no texture coords (which implies that
1581 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001582 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001583 } else {
1584 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001585 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001586 }
1587 }
1588}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001589
Mike Reed887cdf12017-04-03 11:11:09 -04001590void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001591 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001592 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001593 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001594 const uint16_t indices[], int indexCount,
1595 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001596 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001597 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001598
Mike Reed2f6b5a42017-03-19 15:04:17 -04001599 SkPaint copy(paint);
1600 copy.setStyle(SkPaint::kStroke_Style);
1601 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001602
Mike Reed2f6b5a42017-03-19 15:04:17 -04001603 GrPaint grPaint;
1604 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001605 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1606 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001607 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001608 }
1609
Mike Reed2f6b5a42017-03-19 15:04:17 -04001610 int triangleCount = 0;
1611 int n = (nullptr == indices) ? vertexCount : indexCount;
1612 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001613 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001614 triangleCount = n / 3;
1615 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001616 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001617 triangleCount = n - 2;
1618 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001619 case SkVertices::kTriangleFan_VertexMode:
1620 SK_ABORT("Unexpected triangle fan.");
1621 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001622 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623
Mike Reed2f6b5a42017-03-19 15:04:17 -04001624 VertState state(vertexCount, indices, indexCount);
1625 VertState::Proc vertProc = state.chooseProc(vmode);
1626
1627 //number of indices for lines per triangle with kLines
1628 indexCount = triangleCount * 6;
1629
Brian Osmanae0c50c2017-05-25 16:56:34 -04001630 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1631 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1632 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1633
1634 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001635 int i = 0;
1636 while (vertProc(&state)) {
1637 lineIndices[i] = state.f0;
1638 lineIndices[i + 1] = state.f1;
1639 lineIndices[i + 2] = state.f1;
1640 lineIndices[i + 3] = state.f2;
1641 lineIndices[i + 4] = state.f2;
1642 lineIndices[i + 5] = state.f0;
1643 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001644 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001645
Chris Dalton3809bab2017-06-13 10:55:06 -06001646 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001647 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001648 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001649 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001650 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001651 bones,
1652 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001653 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654}
1655
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001656void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1657 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001658 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001659 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001660
1661 SkASSERT(vertices);
1662 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001663 bool hasColors = vertices->hasColors();
1664 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001665 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001666 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001667 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001668 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1669 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001670 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001671 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001672 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1673 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001674 return;
1675 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001676 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001677 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1678 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001679}
1680
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681///////////////////////////////////////////////////////////////////////////////
1682
Jim Van Verth3af1af92017-05-18 15:06:54 -04001683void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1684
1685 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001686 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1687
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001688 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001689 // failed to find an accelerated case
1690 this->INHERITED::drawShadow(path, rec);
1691 }
1692}
1693
1694///////////////////////////////////////////////////////////////////////////////
1695
Mike Reeda1361362017-03-07 09:37:29 -05001696void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001697 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001698 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001699 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001700 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001701 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001702 return;
1703 }
1704
Hal Canary144caf52016-11-07 17:57:18 -05001705 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001706
reedca109532015-06-25 16:25:25 -07001707 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001708 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001709
jvanverth31ff7622015-08-07 10:09:28 -07001710 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001711 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001712 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1713 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001714 return;
1715 }
1716 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001717 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1718 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001719 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001720 }
1721 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001722
Brian Salomon0166cfd2017-03-13 17:58:25 -04001723 fRenderTargetContext->drawAtlas(
1724 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001725}
1726
1727///////////////////////////////////////////////////////////////////////////////
1728
Herb Derbyb935cf82018-07-26 16:54:18 -04001729void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001730 ASSERT_SINGLE_OWNER
1731 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
Herb Derbyb983e6b2018-07-13 13:26:29 -04001732
Jim Van Verth87a30112018-09-24 16:13:58 -04001733 // Check for valid input
1734 const SkMatrix& ctm = this->ctm();
Mike Reed96345a22019-01-02 21:30:29 -05001735 if (!ctm.isFinite() || !glyphRunList.allFontsFinite()) {
Jim Van Verth87a30112018-09-24 16:13:58 -04001736 return;
1737 }
1738
1739 fRenderTargetContext->drawGlyphRunList(this->clip(), ctm, glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001740}
1741
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742///////////////////////////////////////////////////////////////////////////////
1743
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001744void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
1745 GrBackendApi api = this->context()->contextPriv().getBackend();
1746 if (GrBackendApi::kVulkan == api) {
1747 const SkMatrix& ctm = canvas->getTotalMatrix();
1748 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1749 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -05001750 drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(),
1751 this->imageInfo());
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001752 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001753 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1754 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001755 }
1756 }
1757 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1758}
1759
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001760
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001761///////////////////////////////////////////////////////////////////////////////
1762
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001764 this->flushAndSignalSemaphores(0, nullptr);
1765}
1766
Greg Daniel51316782017-08-02 15:10:09 +00001767GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1768 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001769 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001770
Greg Danielc64ee462017-06-15 16:59:49 -04001771 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001772}
1773
Greg Danielc64ee462017-06-15 16:59:49 -04001774bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001775 ASSERT_SINGLE_OWNER
1776
Greg Danielc64ee462017-06-15 16:59:49 -04001777 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778}
1779
1780///////////////////////////////////////////////////////////////////////////////
1781
reed76033be2015-03-14 10:54:31 -07001782SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001783 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784
robertphillipsca6eafc2016-05-17 09:57:46 -07001785 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786
robertphillipsca6eafc2016-05-17 09:57:46 -07001787 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001788 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001789 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1790 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001791
Brian Osman10fc6fd2018-03-02 11:01:10 -05001792 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
Greg Daniel4065d452018-11-16 15:43:41 -05001793 const GrBackendFormat& origFormat = fRenderTargetContext->asSurfaceProxy()->backendFormat();
1794 GrBackendFormat format = origFormat.makeTexture2D();
1795 if (!format.isValid()) {
1796 return nullptr;
1797 }
Brian Osman10fc6fd2018-03-02 11:01:10 -05001798 if (kRGBA_1010102_GrPixelConfig == config) {
1799 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1800 // channel in the layer.
1801 config = kRGBA_8888_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -05001802 format =
1803 fContext->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Osman10fc6fd2018-03-02 11:01:10 -05001804 }
1805
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001806 sk_sp<GrRenderTargetContext> rtc(fContext->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001807 format, fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001808 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001809 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1810 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001811 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001812 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001813 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001814
1815 // Skia's convention is to only clear a device if it is non-opaque.
1816 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1817
Robert Phillips9fab7e92016-11-17 12:45:04 -05001818 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1819 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820}
1821
reede8f30622016-03-23 18:59:25 -07001822sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001823 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001824 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001825 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001826 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001827 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001828 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001829}
1830
senorblanco900c3672016-04-27 11:31:23 -07001831SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001832 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001833 // We always return a transient cache, so it is freed after each
1834 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001835 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001836}
reedf037e0b2014-10-30 11:34:15 -07001837