blob: 3a54abbeea3e53c98c16c0f9c421fe7c28918a01 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Brian Salomon2a943df2018-05-04 13:43:19 -040010#include "../private/SkShadowFlags.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050011#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070012#include "GrBlurUtils.h"
Brian Osman1cb41712017-10-19 12:54:52 -040013#include "GrColorSpaceXform.h"
kkinnunenabcfab42015-02-22 22:53:44 -080014#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080016#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
18#include "GrRenderTargetContextPriv.h"
Robert Phillips27927a52018-08-20 13:18:12 -040019#include "GrShape.h"
bsalomon6663acf2016-05-10 09:14:17 -070020#include "GrStyle.h"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040021#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050022#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050023#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070024#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080025#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070026#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070028#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070030#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050031#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050032#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070033#include "SkLatticeIter.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040034#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050035#include "SkMaskFilterBase.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000037#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070038#include "SkPictureData.h"
Mike Reed242135a2018-02-22 13:41:39 -050039#include "SkRRectPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050040#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050041#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080042#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070043#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000045#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080046#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000047#include "SkTLazy.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include "SkTo.h"
Hal Canaryea60b952018-08-21 11:45:46 -040049#include "SkUTF.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000050#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050051#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050052#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080053#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080054#include "effects/GrSimpleTextureEffect.h"
55#include "effects/GrTextureDomain.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040056#include "text/GrTextTarget.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057
joshualittce894002016-01-11 13:29:31 -080058#define ASSERT_SINGLE_OWNER \
Robert Phillips0c4b7b12018-03-06 08:20:37 -050059SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->contextPriv().debugSingleOwner());)
joshualittce894002016-01-11 13:29:31 -080060
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062///////////////////////////////////////////////////////////////////////////////
63
bsalomon74f681d2015-06-23 14:38:48 -070064/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
65 should fail. */
66bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
67 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
68 *flags = 0;
69 if (info) {
70 switch (info->alphaType()) {
71 case kPremul_SkAlphaType:
72 break;
73 case kOpaque_SkAlphaType:
74 *flags |= SkGpuDevice::kIsOpaque_Flag;
75 break;
76 default: // If it is unpremul or unknown don't try to render
77 return false;
78 }
79 }
80 if (kClear_InitContents == init) {
81 *flags |= kNeedClear_Flag;
82 }
83 return true;
84}
85
Robert Phillips9fab7e92016-11-17 12:45:04 -050086sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
87 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070088 int width, int height,
89 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040090 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070091 return nullptr;
92 }
93 unsigned flags;
94 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
95 return nullptr;
96 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050097 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
98 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000099}
100
robertphillips24e91282016-04-29 06:46:36 -0700101sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
102 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400103 GrSurfaceOrigin origin, const SkSurfaceProps* props,
104 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700105 unsigned flags;
106 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700108 }
109
Brian Osman11052242016-10-27 14:47:55 -0400110 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
111 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400112 origin, props,
113 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400114 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700115 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700116 }
117
Robert Phillips9fab7e92016-11-17 12:45:04 -0500118 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700119 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700120}
121
Brian Osman11052242016-10-27 14:47:55 -0400122static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700123 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400124 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700125 colorType = kUnknown_SkColorType;
126 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400127 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
128 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700129}
130
Robert Phillips9fab7e92016-11-17 12:45:04 -0500131SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
132 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400133 : INHERITED(make_info(renderTargetContext.get(), width, height,
134 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500135 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400136 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700137{
robertphillips1f3923e2016-07-21 07:17:54 -0700138 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700139 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140
bsalomone63ffef2016-02-05 07:17:34 -0800141 if (flags & kNeedClear_Flag) {
142 this->clearAll();
143 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144}
145
Brian Osman11052242016-10-27 14:47:55 -0400146sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
147 GrContext* context,
148 SkBudgeted budgeted,
149 const SkImageInfo& origInfo,
150 int sampleCount,
151 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400152 const SkSurfaceProps* surfaceProps,
153 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000154 if (kUnknown_SkColorType == origInfo.colorType() ||
155 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000157 }
158
bsalomonafe30052015-01-16 07:32:33 -0800159 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800161 }
162
Brian Osman2b23c4b2018-06-01 12:25:08 -0400163 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500164 if (kUnknown_GrPixelConfig == config) {
165 return nullptr;
166 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400167 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
168 // they need to be exact.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500169 return context->contextPriv().makeDeferredRenderTargetContext(
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400170 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700171 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500172 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400173 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800174}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175
Mike Reeda1361362017-03-07 09:37:29 -0500176sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700177 int left, int top,
178 SkIPoint* offset,
179 const SkImageFilter* filter) {
180 SkASSERT(srcImg->isTextureBacked());
181 SkASSERT(filter);
182
Mike Reeda1361362017-03-07 09:37:29 -0500183 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700184 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500185 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500186 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -0400187 SkColorType colorType;
188 if (!GrPixelConfigToColorType(fRenderTargetContext->colorSpaceInfo().config(), &colorType)) {
189 colorType = kN32_SkColorType;
190 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400191 SkImageFilter::OutputProperties outputProperties(
Brian Osmana50205f2018-07-06 13:57:01 -0400192 colorType, fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700193 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700194
195 return filter->filterImage(srcImg, ctx, offset);
196}
197
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198///////////////////////////////////////////////////////////////////////////////
199
Mike Reed353196f2017-07-21 11:01:18 -0400200bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800201 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000202
Mike Reed353196f2017-07-21 11:01:18 -0400203 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500204 return false;
205 }
206
Mike Reed353196f2017-07-21 11:01:18 -0400207 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500208 if (!rec.trim(this->width(), this->height())) {
209 return false;
210 }
211
212 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000213}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214
Mike Reed353196f2017-07-21 11:01:18 -0400215bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800216 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700217
Mike Reed353196f2017-07-21 11:01:18 -0400218 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500219 return false;
220 }
221
Mike Reed353196f2017-07-21 11:01:18 -0400222 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500223 if (!rec.trim(this->width(), this->height())) {
224 return false;
225 }
226
227 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000228}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229
reed41e010c2015-06-09 12:16:53 -0700230bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800231 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700232 return false;
233}
234
Brian Osman11052242016-10-27 14:47:55 -0400235GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700236 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400237 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700238}
239
reed8eddfb52014-12-04 07:50:14 -0800240void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800241 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500242 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500243
reed8eddfb52014-12-04 07:50:14 -0800244 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Chris Dalton344e9032017-12-11 15:42:09 -0700245 fRenderTargetContext->clear(&rect, 0x0, GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800246}
247
Brian Osman11052242016-10-27 14:47:55 -0400248void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800249 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800250
Brian Osman693a5402016-10-27 15:13:22 -0400251 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800252
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400253 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
254 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400255 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400256 this->context(),
257 budgeted,
258 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400259 fRenderTargetContext->numColorSamples(),
260 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400261 &this->surfaceProps(),
262 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400263 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800264 return;
265 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400266 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800267
268 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400269 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800270 return;
271 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500272 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800273 }
274
Brian Osman693a5402016-10-27 15:13:22 -0400275 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800276}
277
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278///////////////////////////////////////////////////////////////////////////////
279
Mike Reeda1361362017-03-07 09:37:29 -0500280void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800281 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500282 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283
284 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400285 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
286 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700287 return;
288 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289
Brian Salomon0166cfd2017-03-13 17:58:25 -0400290 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000291}
292
Brian Salomon1459ebd2017-12-19 16:12:44 -0500293static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
294 switch (mode) {
295 case SkCanvas::kPoints_PointMode:
296 return GrPrimitiveType::kPoints;
297 case SkCanvas::kLines_PointMode:
298 return GrPrimitiveType::kLines;
299 case SkCanvas::kPolygon_PointMode:
300 return GrPrimitiveType::kLineStrip;
301 }
302 SK_ABORT("Unexpected mode");
303 return GrPrimitiveType::kPoints;
304}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305
Mike Reeda1361362017-03-07 09:37:29 -0500306void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800308 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500309 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310 SkScalar width = paint.getStrokeWidth();
311 if (width < 0) {
312 return;
313 }
314
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000315 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700316 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700317 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400318 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
319 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700320 return;
321 }
egdaniele61c4112014-06-12 10:24:21 -0700322 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700323 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700324 path.moveTo(pts[0]);
325 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700326 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
327 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700328 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000329 }
330
bsalomon6ade6dd2016-09-12 12:07:17 -0700331 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500332 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700333 SkScalarNearlyEqual(scales[0], 1.f) &&
334 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700335 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000336 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400337 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500338 SkRasterClip rc(this->devClipBounds());
339 SkDraw draw;
340 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
341 draw.fMatrix = &this->ctm();
342 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500343 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000344 return;
345 }
346
Brian Salomon1459ebd2017-12-19 16:12:44 -0500347 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700348
Mike Reeda1361362017-03-07 09:37:29 -0500349 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700350#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
351 // This offsetting in device space matches the expectations of the Android framework for non-AA
352 // points and lines.
353 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600354 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700355 tempMatrix = *viewMatrix;
356 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
357 tempMatrix.postTranslate(kOffset, kOffset);
358 viewMatrix = &tempMatrix;
359 }
360#endif
361
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400363 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
364 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700365 return;
366 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367
Brian Osmanae0c50c2017-05-25 16:56:34 -0400368 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
369 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
370 nullptr);
371
372 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400373 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000374}
375
376///////////////////////////////////////////////////////////////////////////////
377
Mike Reeda1361362017-03-07 09:37:29 -0500378void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800379 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500380 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400381
382 GrStyle style(paint);
383
bsalomona7d85ba2016-07-06 11:54:59 -0700384 // A couple reasons we might need to call drawPath.
385 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400386 GrShape shape(rect, style);
387
388 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
389 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390 return;
391 }
392
393 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400394 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
395 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700396 return;
397 }
Mike Klein744fb732014-06-23 15:13:26 -0400398
Chris Dalton3b51df12017-11-27 14:33:06 -0700399 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
400 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401}
402
403///////////////////////////////////////////////////////////////////////////////
404
Mike Reeda1361362017-03-07 09:37:29 -0500405void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800406 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500407 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400408
Mike Reed80747ef2018-01-23 15:29:32 -0500409 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000410 if (mf) {
411 if (mf->hasFragmentProcessor()) {
412 mf = nullptr; // already handled in SkPaintToGrPaint
413 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400414 }
415
bsalomon6663acf2016-05-10 09:14:17 -0700416 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000417
Robert Phillipsa29a9562016-10-20 09:40:55 -0400418 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800419 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400420 GrShape shape(rrect, style);
421
Robert Phillips27927a52018-08-20 13:18:12 -0400422 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
423 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424 return;
425 }
Mike Klein744fb732014-06-23 15:13:26 -0400426
bsalomon6663acf2016-05-10 09:14:17 -0700427 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800428
Robert Phillipsa522d662018-08-23 13:50:16 -0400429 GrPaint grPaint;
430 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
431 this->ctm(), &grPaint)) {
432 return;
433 }
434
Chris Dalton3b51df12017-11-27 14:33:06 -0700435 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
436 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000437}
438
robertphillipsd7706102016-02-25 09:28:08 -0800439
Robert Phillips20390c32018-08-17 11:01:03 -0400440void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800441 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500442 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800443 if (outer.isEmpty()) {
444 return;
445 }
446
447 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500448 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800449 }
450
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000451 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000452
robertphillips0e7029e2015-11-30 05:45:06 -0800453 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800454 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400455 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
456 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700457 return;
458 }
robertphillips00095892016-02-29 13:50:40 -0800459
Brian Salomon0166cfd2017-03-13 17:58:25 -0400460 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700461 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800462 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000463 }
464
465 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700466 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000467 path.addRRect(outer);
468 path.addRRect(inner);
469 path.setFillType(SkPath::kEvenOdd_FillType);
470
Robert Phillips27927a52018-08-20 13:18:12 -0400471 // TODO: We are losing the possible mutability of the path here but this should probably be
472 // fixed by upgrading GrShape to handle DRRects.
473 GrShape shape(path, paint);
474
475 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
476 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000477}
478
479
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481
Mike Reeda1361362017-03-07 09:37:29 -0500482void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700483 if (paint.getMaskFilter()) {
484 SkPath path;
485 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400486 path.setIsVolatile(true);
487 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700488 }
489
490 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400491 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
492 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700493 return;
494 }
495
Chris Dalton3b51df12017-11-27 14:33:06 -0700496 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
497 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700498}
499
Mike Reeda1361362017-03-07 09:37:29 -0500500void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800501 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500502 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800503
robertphillips514450c2015-11-24 05:36:02 -0800504 if (paint.getMaskFilter()) {
505 // The RRect path can handle special case blurring
506 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500507 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508 }
509
510 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400511 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
512 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700513 return;
514 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000515
Chris Dalton3b51df12017-11-27 14:33:06 -0700516 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
517 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518}
519
Mike Reeda1361362017-03-07 09:37:29 -0500520void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700521 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
522 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500523 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700524 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500525 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700526 return;
527 }
528 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400529 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
530 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700531 return;
532 }
533
Chris Dalton3b51df12017-11-27 14:33:06 -0700534 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500535 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500536 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700537}
538
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000539#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540
541///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700542void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700543 const SkPaint& origPaint) {
544 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500545 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400546 // Adding support for round capping would require a
547 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700548 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
549 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
550 SkASSERT(!origPaint.getPathEffect());
551 SkASSERT(!origPaint.getMaskFilter());
552
553 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
554 SkASSERT(halfWidth > 0);
555
556 SkVector v = points[1] - points[0];
557
558 SkScalar length = SkPoint::Normalize(&v);
559 if (!length) {
560 v.fX = 1.0f;
561 v.fY = 0.0f;
562 }
563
564 SkPaint newPaint(origPaint);
565 newPaint.setStyle(SkPaint::kFill_Style);
566
567 SkScalar xtraLength = 0.0f;
568 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
569 xtraLength = halfWidth;
570 }
571
572 SkPoint mid = points[0] + points[1];
573 mid.scale(0.5f);
574
575 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
576 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
577 SkMatrix m;
578 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
579
580 SkMatrix local = m;
581
Mike Reeda1361362017-03-07 09:37:29 -0500582 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700583
584 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400585 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
586 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700587 return;
588 }
589
Brian Salomon82f44312017-01-11 13:42:54 -0500590 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700591 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700592}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000593
Robert Phillips20390c32018-08-17 11:01:03 -0400594void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800595 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400596 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700597 SkPoint points[2];
598 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
599 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500600 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700601 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500602 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700603 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500604 // Round capping support is currently disabled b.c. it would require a RRect
605 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500606 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700607 return;
608 }
609 }
robertphillipsff55b492015-11-24 07:56:59 -0800610 }
611
Hal Canary144caf52016-11-07 17:57:18 -0500612 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400613 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500614 GrPaint grPaint;
615 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
616 this->ctm(), &grPaint)) {
617 return;
618 }
619 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
620 this->ctm(), origSrcPath, GrStyle(paint));
621 return;
622 }
Robert Phillips27927a52018-08-20 13:18:12 -0400623
624 // TODO: losing possible mutability of 'origSrcPath' here
625 GrShape shape(origSrcPath, paint);
626
627 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
628 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629}
630
631static const int kBmpSmallTileSize = 1 << 10;
632
633static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
634 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
635 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
636 return tilesX * tilesY;
637}
638
reed85d91782015-09-10 14:33:38 -0700639static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640 if (maxTileSize <= kBmpSmallTileSize) {
641 return maxTileSize;
642 }
643
644 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
645 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
646
647 maxTileTotalTileSize *= maxTileSize * maxTileSize;
648 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
649
650 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
651 return kBmpSmallTileSize;
652 } else {
653 return maxTileSize;
654 }
655}
656
657// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
658// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700659static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800660 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800661 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700662 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700663 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664 const SkRect* srcRectPtr,
665 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700666 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700667 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
668 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 clippedSrcIRect->setEmpty();
670 return;
671 }
672 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
673 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700674 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 if (!clippedSrcRect.intersect(*srcRectPtr)) {
676 clippedSrcIRect->setEmpty();
677 return;
678 }
679 }
680 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700681 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000682 if (!clippedSrcIRect->intersect(bmpBounds)) {
683 clippedSrcIRect->setEmpty();
684 }
685}
686
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400687bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
688 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700689 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700690 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400691 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700692 const SkRect* srcRectPtr,
693 int maxTileSize,
694 int* tileSize,
695 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800696 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700697 // if it's larger than the max tile size, then we have no choice but tiling.
698 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400699 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400700 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
701 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700702 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
703 return true;
704 }
705
bsalomon1a1d0b82015-10-16 07:49:42 -0700706 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700707 const size_t area = imageRect.width() * imageRect.height();
708 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
709 return false;
710 }
711
reed85d91782015-09-10 14:33:38 -0700712 // At this point we know we could do the draw by uploading the entire bitmap
713 // as a texture. However, if the texture would be large compared to the
714 // cache size and we don't require most of it for this draw then tile to
715 // reduce the amount of upload and cache spill.
716
717 // assumption here is that sw bitmap size is a good proxy for its size as
718 // a texture
719 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
720 size_t cacheSize;
721 fContext->getResourceCacheLimits(nullptr, &cacheSize);
722 if (bmpSize < cacheSize / 2) {
723 return false;
724 }
725
bsalomon1a1d0b82015-10-16 07:49:42 -0700726 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
727 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400728 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
729 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400730 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700731 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
732 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500733 kBmpSmallTileSize * kBmpSmallTileSize *
734 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700735
Brian Osmand05cdc32017-02-06 13:24:47 -0500736 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700737}
738
reed85d91782015-09-10 14:33:38 -0700739bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
740 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700741 const SkMatrix& viewMatrix,
742 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800743 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400744 // If image is explicitly texture backed then we shouldn't get here.
745 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400747 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700748 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500749 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
750 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
751 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700752
753 int tileFilterPad;
754 if (doBicubic) {
755 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400756 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700757 tileFilterPad = 0;
758 } else {
759 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400761 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400763 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764
reed85d91782015-09-10 14:33:38 -0700765 // these are output, which we safely ignore, as we just want to know the predicate
766 int outTileSize;
767 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768
bsalomone553b642016-08-17 09:02:09 -0700769 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400770 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700771 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772}
773
Mike Reeda1361362017-03-07 09:37:29 -0500774void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400775 SkScalar x,
776 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400778 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800779 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800780 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500781 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700782
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400783 int maxTileSize = this->caps()->maxTileSize();
bsalomonb1b01992015-11-18 10:56:08 -0800784
785 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
786 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400787 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
788 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800789 bitmap.height() <= maxTileSize;
790
791 bool skipTileCheck = drawAA || paint.getMaskFilter();
792
793 if (!skipTileCheck) {
794 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
795 int tileSize;
796 SkIRect clippedSrcRect;
797
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400798 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800799 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400800 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500801 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
802 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800803
804 int tileFilterPad;
805
806 if (doBicubic) {
807 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400808 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800809 tileFilterPad = 0;
810 } else {
811 tileFilterPad = 1;
812 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400813 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800814
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400815 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700816 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400817 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700818 &tileSize, &clippedSrcRect)) {
819 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400820 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
821 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800822 return;
823 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000824 }
Hal Canary144caf52016-11-07 17:57:18 -0500825 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0cdfc322018-05-14 16:17:43 -0400826 this->drawTextureMaker(&maker, bitmap.width(), bitmap.height(), nullptr, nullptr,
827 SkCanvas::kStrict_SrcRectConstraint, viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000828}
829
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000830// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
832// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000833static inline void clamped_outset_with_offset(SkIRect* iRect,
834 int outset,
835 SkPoint* offset,
836 const SkIRect& clamp) {
837 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000838
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000839 int leftClampDelta = clamp.fLeft - iRect->fLeft;
840 if (leftClampDelta > 0) {
841 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842 iRect->fLeft = clamp.fLeft;
843 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000844 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000845 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000846
847 int topClampDelta = clamp.fTop - iRect->fTop;
848 if (topClampDelta > 0) {
849 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000850 iRect->fTop = clamp.fTop;
851 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000852 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853 }
854
855 if (iRect->fRight > clamp.fRight) {
856 iRect->fRight = clamp.fRight;
857 }
858 if (iRect->fBottom > clamp.fBottom) {
859 iRect->fBottom = clamp.fBottom;
860 }
861}
862
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863// Break 'bitmap' into several tiles to draw it since it has already
864// been determined to be too large to fit in VRAM
865void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800866 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700867 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000868 const SkRect& srcRect,
869 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400870 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800871 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700872 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000873 int tileSize,
874 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800875 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800876
ericrk983294f2016-04-18 09:14:00 -0700877 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800878 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700879 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800880
bsalomonc55271f2015-11-09 11:55:57 -0800881 const SkPaint* paint = &origPaint;
882 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400883 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800884 // Drop antialiasing to avoid seams at tile boundaries.
885 tempPaint = origPaint;
886 tempPaint.setAntiAlias(false);
887 paint = &tempPaint;
888 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
890
891 int nx = bitmap.width() / tileSize;
892 int ny = bitmap.height() / tileSize;
893 for (int x = 0; x <= nx; x++) {
894 for (int y = 0; y <= ny; y++) {
895 SkRect tileR;
896 tileR.set(SkIntToScalar(x * tileSize),
897 SkIntToScalar(y * tileSize),
898 SkIntToScalar((x + 1) * tileSize),
899 SkIntToScalar((y + 1) * tileSize));
900
901 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
902 continue;
903 }
904
905 if (!tileR.intersect(srcRect)) {
906 continue;
907 }
908
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 SkIRect iTileR;
910 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700911 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
912 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500913 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700914 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400915 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000916 SkIRect iClampRect;
917
reeda5517e22015-07-14 10:54:12 -0700918 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000919 // In bleed mode we want to always expand the tile on all edges
920 // but stay within the bitmap bounds
921 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
922 } else {
923 // In texture-domain/clamp mode we only want to expand the
924 // tile on edges interior to "srcRect" (i.e., we want to
925 // not bleed across the original clamped edges)
926 srcRect.roundOut(&iClampRect);
927 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000928 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
929 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 }
931
bsalomone553b642016-08-17 09:02:09 -0700932 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 if (bitmap.extractSubset(&tmpB, iTileR)) {
934 // now offset it to make it "local" to our tmp bitmap
935 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800936 // de-optimized this determination
937 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700938 this->drawBitmapTile(tmpB,
939 viewMatrix,
940 rectToDraw,
941 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500942 params,
bsalomone553b642016-08-17 09:02:09 -0700943 *paint,
944 constraint,
945 bicubic,
946 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 }
948 }
949 }
950}
951
bsalomone553b642016-08-17 09:02:09 -0700952void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
953 const SkMatrix& viewMatrix,
954 const SkRect& dstRect,
955 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400956 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700957 const SkPaint& paint,
958 SkCanvas::SrcRectConstraint constraint,
959 bool bicubic,
960 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800961 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400962 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
963 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700964 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400965 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
966 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400967 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968
Brian Salomon2a943df2018-05-04 13:43:19 -0400969 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400970 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400971 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400972 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973 return;
974 }
bsalomone553b642016-08-17 09:02:09 -0700975
bsalomone553b642016-08-17 09:02:09 -0700976 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400977 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
978 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700979
980 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
981 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400982 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700983
reeda5517e22015-07-14 10:54:12 -0700984 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700986 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500988 domain.fLeft = srcRect.fLeft + 0.5f;
989 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500991 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992 }
993 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500994 domain.fTop = srcRect.fTop + 0.5f;
995 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500997 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000999 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001000 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001001 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001002 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001003 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001004 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001005 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001006 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1007 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001008 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001010 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011 }
1012
Brian Osman21fc5ce2018-08-27 20:36:19 +00001013 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001014 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001015 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001016 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1017 viewMatrix, std::move(fp),
1018 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001019 return;
1020 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001021
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001022 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001023 GrAA aa = GrAA(paint.isAntiAlias() &&
1024 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001025 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001026}
1027
Mike Reeda1361362017-03-07 09:37:29 -05001028void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001030 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001031 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032
Khushalc421ca12018-06-26 14:38:34 -07001033 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 return;
1035 }
1036
Robert Phillipse14d3052017-02-15 13:18:21 -05001037 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1038 if (!srcImg) {
1039 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001040 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041
Florin Malita53f77bd2017-04-28 13:48:37 -04001042 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001043}
1044
1045
Robert Phillipsc100d482018-07-10 10:11:01 -04001046void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001047 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001048 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001049 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001050
Florin Malita53f77bd2017-04-28 13:48:37 -04001051 // TODO: clipImage support.
1052
robertphillips970587b2016-07-14 14:12:55 -07001053 sk_sp<SkSpecialImage> result;
1054 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001055 SkIPoint offset = { 0, 0 };
1056
1057 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001058 if (!result) {
1059 return;
1060 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001061
1062 left += offset.fX;
1063 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001064 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001065 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001066 }
1067
1068 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001069 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001070 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001071 return;
1072 }
robertphillips970587b2016-07-14 14:12:55 -07001073
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001074 const GrPixelConfig config = proxy->config();
1075
robertphillips970587b2016-07-14 14:12:55 -07001076 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001077 if (tmpUnfiltered.getMaskFilter()) {
1078 SkMatrix ctm = this->ctm();
Robert Phillipsc100d482018-07-10 10:11:01 -04001079 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
Florin Malitac6c5ead2018-04-11 15:33:40 -04001080 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001081 }
1082
robertphillips970587b2016-07-14 14:12:55 -07001083 tmpUnfiltered.setImageFilter(nullptr);
1084
Brian Osman2240be92017-10-18 13:15:13 -04001085 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001086 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001087 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001088 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001089 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001090 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001091 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001092 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001093
1094 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001095 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1096 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001097 return;
1098 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099
robertphillips970587b2016-07-14 14:12:55 -07001100 const SkIRect& subset = result->subset();
1101
Brian Salomon82f44312017-01-11 13:42:54 -05001102 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001103 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001104 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001105 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001106 SkMatrix::I(),
Robert Phillipsc100d482018-07-10 10:11:01 -04001107 SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001108 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109}
1110
Mike Reeda1361362017-03-07 09:37:29 -05001111void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001112 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001113 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001114 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001115 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1116 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1117 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1118 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1119 const SkRect* dst = &origDst;
1120 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001121 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001122 if (!src) {
1123 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125
bsalomonb1b01992015-11-18 10:56:08 -08001126 SkMatrix srcToDstMatrix;
1127 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1128 return;
1129 }
1130 SkRect tmpSrc, tmpDst;
1131 if (src != &bmpBounds) {
1132 if (!bmpBounds.contains(*src)) {
1133 tmpSrc = *src;
1134 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001135 return; // nothing to draw
1136 }
bsalomonb1b01992015-11-18 10:56:08 -08001137 src = &tmpSrc;
1138 srcToDstMatrix.mapRect(&tmpDst, *src);
1139 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140 }
1141 }
1142
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001143 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001144
bsalomonb1b01992015-11-18 10:56:08 -08001145 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1146 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001147 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1148 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1149 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001150
Brian Salomon7c8460e2017-05-12 11:36:10 -04001151 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001152
1153 if (!skipTileCheck) {
1154 int tileSize;
1155 SkIRect clippedSrcRect;
1156
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001157 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001158 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001159 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001160 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1161 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001162
1163 int tileFilterPad;
1164
1165 if (doBicubic) {
1166 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001167 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001168 tileFilterPad = 0;
1169 } else {
1170 tileFilterPad = 1;
1171 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001172 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001173
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001174 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001175 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001176 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1177 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001178 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001179 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001180 return;
1181 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001182 }
Hal Canary144caf52016-11-07 17:57:18 -05001183 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0cdfc322018-05-14 16:17:43 -04001184 this->drawTextureMaker(&maker, bitmap.width(), bitmap.height(), src, dst, constraint,
1185 this->ctm(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186}
1187
robertphillips6451a0c2016-07-18 08:31:31 -07001188sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001189 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1190 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001191 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1192 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001193 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001194 return nullptr;
1195 }
1196
Robert Phillipse14d3052017-02-15 13:18:21 -05001197 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001198
Robert Phillipse14d3052017-02-15 13:18:21 -05001199 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1200 // the special image
1201 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1202 rect,
1203 bitmap.getGenerationID(),
1204 std::move(proxy),
1205 bitmap.refColorSpace(),
1206 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001207}
1208
reede51c3562016-07-19 14:33:20 -07001209sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001210 SkPixmap pm;
1211 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001212 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001213
Robert Phillips6de99042017-01-31 11:31:39 -05001214 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1215 SkIRect::MakeWH(image->width(), image->height()),
1216 image->uniqueID(),
1217 std::move(proxy),
1218 as_IB(image)->onImageInfo().refColorSpace(),
1219 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001220 } else if (image->peekPixels(&pm)) {
1221 SkBitmap bm;
1222
1223 bm.installPixels(pm);
1224 return this->makeSpecial(bm);
1225 } else {
1226 return nullptr;
1227 }
1228}
1229
1230sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001231 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1232 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001233 // When the device doesn't have a texture, we create a temporary texture.
1234 // TODO: we should actually only copy the portion of the source needed to apply the image
1235 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001236 proxy = GrSurfaceProxy::Copy(fContext.get(),
1237 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001238 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001239 SkBudgeted::kYes);
1240 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001241 return nullptr;
1242 }
robertphillips1b5f9682016-07-15 08:01:12 -07001243 }
1244
1245 const SkImageInfo ii = this->imageInfo();
1246 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1247
Robert Phillipse2f7d182016-12-15 09:23:05 -05001248 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1249 srcRect,
1250 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001251 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001252 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001253 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001254}
1255
Mike Reeda1361362017-03-07 09:37:29 -05001256void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001257 int left, int top, const SkPaint& paint) {
1258 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001259
joshualittce894002016-01-11 13:29:31 -08001260 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001262 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001263
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001265 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001266 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001267 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 return;
1269 }
1270
Mike Reed2179b782018-02-07 11:59:57 -05001271 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272}
1273
Brian Salomon34169692017-08-28 15:32:01 -04001274void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001275 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001276 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001277 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001278 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001279 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001280 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1281 image->alphaType(), nullptr, nullptr,
1282 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001283 return;
Brian Salomon34169692017-08-28 15:32:01 -04001284 }
1285 SkBitmap bm;
1286 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1287 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1288 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001289 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001290 return;
reed85d91782015-09-10 14:33:38 -07001291 }
Brian Salomon34169692017-08-28 15:32:01 -04001292 this->drawBitmap(bm, x, y, paint);
1293 return;
1294 }
1295 if (image->isLazyGenerated()) {
1296 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1297 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1298 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1299 return;
1300 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001301 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001302 GrBitmapTextureMaker maker(fContext.get(), bm);
1303 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1304 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001305 }
1306}
1307
Brian Salomon34169692017-08-28 15:32:01 -04001308void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1309 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001310 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001311 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001312 if (!src || src->contains(image->bounds())) {
1313 constraint = SkCanvas::kFast_SrcRectConstraint;
1314 }
Robert Phillips3798c862017-03-27 11:08:16 -04001315 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001316 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1317 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001318 return;
1319 }
1320 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001321 SkMatrix srcToDstRect;
1322 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1323 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001324 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001325 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001326 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001327 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001328 return;
1329 }
Mike Reeda1361362017-03-07 09:37:29 -05001330 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001331 return;
1332 }
1333 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001334 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001335 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1336 this->ctm(), paint);
1337 return;
1338 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001339 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001340 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001341 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1342 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001343 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001344}
1345
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001346// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1347static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1348 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1349 return GrSamplerState::Filter::kNearest;
1350 }
1351
1352 return GrSamplerState::Filter::kBilerp;
1353}
1354
Mike Reeda1361362017-03-07 09:37:29 -05001355void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001356 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001357 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001358 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001359 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001360 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001361 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001362 image->alphaType(), pinnedUniqueID,
1363 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001364 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001365 } else {
1366 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001367 if (image->isLazyGenerated()) {
1368 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001369 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001370 } else if (as_IB(image)->getROPixels(&bm,
1371 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001372 GrBitmapTextureMaker maker(fContext.get(), bm);
1373 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001374 }
1375 }
1376}
1377
Mike Reeda1361362017-03-07 09:37:29 -05001378void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001379 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001380 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001381 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001382 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001383 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001384}
1385
Mike Reeda1361362017-03-07 09:37:29 -05001386void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001387 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1388 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001389 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001390 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001391
Brian Salomon2a943df2018-05-04 13:43:19 -04001392 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1393 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1394 }
msarett10e3d9b2016-08-18 15:46:03 -07001395 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001396 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1397 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001398 return;
1399 }
1400
Brian Salomon2a943df2018-05-04 13:43:19 -04001401 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1402 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
1403 sk_sp<SkColorSpace> proxyColorSpace;
1404 auto proxy =
1405 producer->refTextureProxyForParams(filter, dstColorSpace, &proxyColorSpace, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001406 if (!proxy) {
1407 return;
1408 }
Brian Osman21fc5ce2018-08-27 20:36:19 +00001409 auto csxf = GrColorSpaceXform::Make(proxyColorSpace.get(), producer->alphaType(),
Mike Kleine03a1762018-08-22 11:52:16 -04001410 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001411
Brian Salomon0166cfd2017-03-13 17:58:25 -04001412 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001413 std::move(proxy), std::move(csxf), filter,
1414 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001415}
1416
Mike Reeda1361362017-03-07 09:37:29 -05001417void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001418 const SkCanvas::Lattice& lattice, const SkRect& dst,
1419 const SkPaint& paint) {
1420 ASSERT_SINGLE_OWNER
1421 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001422 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips3798c862017-03-27 11:08:16 -04001423 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001424 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001425 image->alphaType(), pinnedUniqueID,
1426 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001427 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001428 } else {
1429 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001430 if (image->isLazyGenerated()) {
1431 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001432 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001433 } else if (as_IB(image)->getROPixels(&bm,
1434 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001435 GrBitmapTextureMaker maker(fContext.get(), bm);
1436 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001437 }
1438 }
1439}
1440
Mike Reeda1361362017-03-07 09:37:29 -05001441void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001442 const SkCanvas::Lattice& lattice, const SkRect& dst,
1443 const SkPaint& paint) {
1444 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001445 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001446 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001447 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001448}
1449
Brian Salomonf3569f02017-10-24 12:52:33 -04001450static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1451 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001452 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001453 if (hasTexs && skPaint.getShader()) {
1454 if (hasColors) {
1455 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001456 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1457 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001458 } else {
1459 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001460 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001461 }
1462 } else {
1463 if (hasColors) {
1464 // We have colors, but either have no shader or no texture coords (which implies that
1465 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001466 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001467 } else {
1468 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001469 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001470 }
1471 }
1472}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473
Mike Reed887cdf12017-04-03 11:11:09 -04001474void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001475 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001476 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001477 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001478 const uint16_t indices[], int indexCount,
1479 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001480 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001481 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001482
Mike Reed2f6b5a42017-03-19 15:04:17 -04001483 SkPaint copy(paint);
1484 copy.setStyle(SkPaint::kStroke_Style);
1485 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001486
Mike Reed2f6b5a42017-03-19 15:04:17 -04001487 GrPaint grPaint;
1488 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001489 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1490 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001491 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001492 }
1493
Mike Reed2f6b5a42017-03-19 15:04:17 -04001494 int triangleCount = 0;
1495 int n = (nullptr == indices) ? vertexCount : indexCount;
1496 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001497 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001498 triangleCount = n / 3;
1499 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001500 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001501 triangleCount = n - 2;
1502 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001503 case SkVertices::kTriangleFan_VertexMode:
1504 SK_ABORT("Unexpected triangle fan.");
1505 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001506 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507
Mike Reed2f6b5a42017-03-19 15:04:17 -04001508 VertState state(vertexCount, indices, indexCount);
1509 VertState::Proc vertProc = state.chooseProc(vmode);
1510
1511 //number of indices for lines per triangle with kLines
1512 indexCount = triangleCount * 6;
1513
Brian Osmanae0c50c2017-05-25 16:56:34 -04001514 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1515 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1516 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1517
1518 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001519 int i = 0;
1520 while (vertProc(&state)) {
1521 lineIndices[i] = state.f0;
1522 lineIndices[i + 1] = state.f1;
1523 lineIndices[i + 2] = state.f1;
1524 lineIndices[i + 3] = state.f2;
1525 lineIndices[i + 4] = state.f2;
1526 lineIndices[i + 5] = state.f0;
1527 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001528 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001529
Chris Dalton3809bab2017-06-13 10:55:06 -06001530 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001531 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001532 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001533 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001534 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001535 bones,
1536 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001537 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538}
1539
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001540void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1541 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001542 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001543 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001544
1545 SkASSERT(vertices);
1546 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001547 bool hasColors = vertices->hasColors();
1548 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001549 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001550 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001551 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001552 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1553 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001554 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001555 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001556 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1557 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001558 return;
1559 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001560 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001561 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1562 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001563}
1564
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001565///////////////////////////////////////////////////////////////////////////////
1566
Jim Van Verth3af1af92017-05-18 15:06:54 -04001567void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1568
1569 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001570 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1571
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001572 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001573 // failed to find an accelerated case
1574 this->INHERITED::drawShadow(path, rec);
1575 }
1576}
1577
1578///////////////////////////////////////////////////////////////////////////////
1579
Mike Reeda1361362017-03-07 09:37:29 -05001580void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001581 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001582 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001583 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001584 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001585 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001586 return;
1587 }
1588
Hal Canary144caf52016-11-07 17:57:18 -05001589 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001590
reedca109532015-06-25 16:25:25 -07001591 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001592 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001593
jvanverth31ff7622015-08-07 10:09:28 -07001594 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001595 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001596 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1597 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001598 return;
1599 }
1600 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001601 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1602 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001603 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001604 }
1605 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001606
1607 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001608 fRenderTargetContext->drawAtlas(
1609 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001610}
1611
1612///////////////////////////////////////////////////////////////////////////////
1613
Herb Derbyb935cf82018-07-26 16:54:18 -04001614void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001615 ASSERT_SINGLE_OWNER
1616 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
1617 SkDEBUGCODE(this->validate();)
1618
Robert Phillipse4643cc2018-08-14 13:01:29 -04001619 fRenderTargetContext->drawGlyphRunList(this->clip(), this->ctm(), glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001620}
1621
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001622///////////////////////////////////////////////////////////////////////////////
1623
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001625 this->flushAndSignalSemaphores(0, nullptr);
1626}
1627
Greg Daniel51316782017-08-02 15:10:09 +00001628GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1629 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001630 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001631
Greg Danielc64ee462017-06-15 16:59:49 -04001632 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001633}
1634
Greg Danielc64ee462017-06-15 16:59:49 -04001635bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001636 ASSERT_SINGLE_OWNER
1637
Greg Danielc64ee462017-06-15 16:59:49 -04001638 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639}
1640
1641///////////////////////////////////////////////////////////////////////////////
1642
reed76033be2015-03-14 10:54:31 -07001643SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001644 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001645
robertphillipsca6eafc2016-05-17 09:57:46 -07001646 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001647
robertphillipsca6eafc2016-05-17 09:57:46 -07001648 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001649 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001650 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1651 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001652
Brian Osman10fc6fd2018-03-02 11:01:10 -05001653 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
1654 if (kRGBA_1010102_GrPixelConfig == config) {
1655 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1656 // channel in the layer.
1657 config = kRGBA_8888_GrPixelConfig;
1658 }
1659
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001660 sk_sp<GrRenderTargetContext> rtc(fContext->contextPriv().makeDeferredRenderTargetContext(
Brian Osman10fc6fd2018-03-02 11:01:10 -05001661 fit, cinfo.fInfo.width(), cinfo.fInfo.height(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001662 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001663 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1664 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001665 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001666 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001668
1669 // Skia's convention is to only clear a device if it is non-opaque.
1670 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1671
Robert Phillips9fab7e92016-11-17 12:45:04 -05001672 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1673 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674}
1675
reede8f30622016-03-23 18:59:25 -07001676sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001677 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001678 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001679 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001680 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001681 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001682 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001683}
1684
senorblanco900c3672016-04-27 11:31:23 -07001685SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001686 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001687 // We always return a transient cache, so it is freed after each
1688 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001689 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001690}
reedf037e0b2014-10-30 11:34:15 -07001691