blob: bc5a34b1327d19a4987045f41510a9fca568feb1 [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"
Brian Osman3b66ab62016-11-28 09:26:31 -05009#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
Brian Osman1cb41712017-10-19 12:54:52 -040011#include "GrColorSpaceXform.h"
kkinnunenabcfab42015-02-22 22:53:44 -080012#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080014#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050015#include "GrImageTextureMaker.h"
16#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070017#include "GrStyle.h"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040018#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050019#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050020#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080022#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070023#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070025#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000026#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070027#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050028#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050029#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070030#include "SkLatticeIter.h"
Mike Reed80747ef2018-01-23 15:29:32 -050031#include "SkMaskFilterBase.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000033#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070034#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
Brian Salomon6a639042016-12-14 11:08:17 -050036#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050037#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080038#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070039#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000040#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000041#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080042#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000043#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000045#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050046#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050047#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080048#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080049#include "effects/GrSimpleTextureEffect.h"
50#include "effects/GrTextureDomain.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040051#include "../private/SkShadowFlags.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080052#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053
reedf037e0b2014-10-30 11:34:15 -070054#if SK_SUPPORT_GPU
55
joshualittce894002016-01-11 13:29:31 -080056#define ASSERT_SINGLE_OWNER \
57 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
58
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060///////////////////////////////////////////////////////////////////////////////
61
bsalomon74f681d2015-06-23 14:38:48 -070062/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
63 should fail. */
64bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
65 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
66 *flags = 0;
67 if (info) {
68 switch (info->alphaType()) {
69 case kPremul_SkAlphaType:
70 break;
71 case kOpaque_SkAlphaType:
72 *flags |= SkGpuDevice::kIsOpaque_Flag;
73 break;
74 default: // If it is unpremul or unknown don't try to render
75 return false;
76 }
77 }
78 if (kClear_InitContents == init) {
79 *flags |= kNeedClear_Flag;
80 }
81 return true;
82}
83
Robert Phillips9fab7e92016-11-17 12:45:04 -050084sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
85 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070086 int width, int height,
87 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040088 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070089 return nullptr;
90 }
91 unsigned flags;
92 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
93 return nullptr;
94 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050095 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
96 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000097}
98
robertphillips24e91282016-04-29 06:46:36 -070099sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
100 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400101 GrSurfaceOrigin origin, const SkSurfaceProps* props,
102 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700103 unsigned flags;
104 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700105 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700106 }
107
Brian Osman11052242016-10-27 14:47:55 -0400108 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
109 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400110 origin, props,
111 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400112 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700114 }
115
Robert Phillips9fab7e92016-11-17 12:45:04 -0500116 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700117 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700118}
119
Brian Osman11052242016-10-27 14:47:55 -0400120static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700121 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400122 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700123 colorType = kUnknown_SkColorType;
124 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400125 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
126 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700127}
128
Robert Phillips9fab7e92016-11-17 12:45:04 -0500129SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
130 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400131 : INHERITED(make_info(renderTargetContext.get(), width, height,
132 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500133 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400134 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700135{
robertphillips1f3923e2016-07-21 07:17:54 -0700136 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700137 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000138
bsalomone63ffef2016-02-05 07:17:34 -0800139 if (flags & kNeedClear_Flag) {
140 this->clearAll();
141 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142}
143
Brian Osman11052242016-10-27 14:47:55 -0400144sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
145 GrContext* context,
146 SkBudgeted budgeted,
147 const SkImageInfo& origInfo,
148 int sampleCount,
149 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400150 const SkSurfaceProps* surfaceProps,
151 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000152 if (kUnknown_SkColorType == origInfo.colorType() ||
153 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700154 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000155 }
156
bsalomonafe30052015-01-16 07:32:33 -0800157 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700158 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800159 }
160
Brian Osman0c2997b2017-01-11 16:58:42 -0500161 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500162 if (kUnknown_GrPixelConfig == config) {
163 return nullptr;
164 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400165 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
166 // they need to be exact.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400167 return context->makeDeferredRenderTargetContext(
168 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700169 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500170 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400171 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800172}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000173
Mike Reeda1361362017-03-07 09:37:29 -0500174sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700175 int left, int top,
176 SkIPoint* offset,
177 const SkImageFilter* filter) {
178 SkASSERT(srcImg->isTextureBacked());
179 SkASSERT(filter);
180
Mike Reeda1361362017-03-07 09:37:29 -0500181 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700182 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500183 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500184 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Salomonf3569f02017-10-24 12:52:33 -0400185 SkImageFilter::OutputProperties outputProperties(
186 fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700187 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700188
189 return filter->filterImage(srcImg, ctx, offset);
190}
191
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000192///////////////////////////////////////////////////////////////////////////////
193
Mike Reed353196f2017-07-21 11:01:18 -0400194bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800195 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000196
Mike Reed353196f2017-07-21 11:01:18 -0400197 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500198 return false;
199 }
200
Mike Reed353196f2017-07-21 11:01:18 -0400201 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500202 if (!rec.trim(this->width(), this->height())) {
203 return false;
204 }
205
206 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000207}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000208
Mike Reed353196f2017-07-21 11:01:18 -0400209bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800210 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700211
Mike Reed353196f2017-07-21 11:01:18 -0400212 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500213 return false;
214 }
215
Mike Reed353196f2017-07-21 11:01:18 -0400216 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500217 if (!rec.trim(this->width(), this->height())) {
218 return false;
219 }
220
221 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000222}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223
reed41e010c2015-06-09 12:16:53 -0700224bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800225 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700226 return false;
227}
228
Brian Osman11052242016-10-27 14:47:55 -0400229GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700230 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400231 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700232}
233
reed8eddfb52014-12-04 07:50:14 -0800234void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800235 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500236 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500237
reed8eddfb52014-12-04 07:50:14 -0800238 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Chris Dalton344e9032017-12-11 15:42:09 -0700239 fRenderTargetContext->clear(&rect, 0x0, GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800240}
241
Brian Osman11052242016-10-27 14:47:55 -0400242void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800243 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800244
Brian Osman693a5402016-10-27 15:13:22 -0400245 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800246
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400247 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
248 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400249 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400250 this->context(),
251 budgeted,
252 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400253 fRenderTargetContext->numColorSamples(),
254 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400255 &this->surfaceProps(),
256 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400257 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800258 return;
259 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400260 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800261
262 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400263 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800264 return;
265 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500266 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800267 }
268
Brian Osman693a5402016-10-27 15:13:22 -0400269 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800270}
271
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272///////////////////////////////////////////////////////////////////////////////
273
Mike Reeda1361362017-03-07 09:37:29 -0500274void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800275 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500276 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277
278 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400279 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
280 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700281 return;
282 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283
Brian Salomon0166cfd2017-03-13 17:58:25 -0400284 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000285}
286
Brian Salomon1459ebd2017-12-19 16:12:44 -0500287static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
288 switch (mode) {
289 case SkCanvas::kPoints_PointMode:
290 return GrPrimitiveType::kPoints;
291 case SkCanvas::kLines_PointMode:
292 return GrPrimitiveType::kLines;
293 case SkCanvas::kPolygon_PointMode:
294 return GrPrimitiveType::kLineStrip;
295 }
296 SK_ABORT("Unexpected mode");
297 return GrPrimitiveType::kPoints;
298}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000299
Mike Reeda1361362017-03-07 09:37:29 -0500300void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000301 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800302 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500303 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304 SkScalar width = paint.getStrokeWidth();
305 if (width < 0) {
306 return;
307 }
308
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000309 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700310 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700311 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400312 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
313 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700314 return;
315 }
egdaniele61c4112014-06-12 10:24:21 -0700316 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700317 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700318 path.moveTo(pts[0]);
319 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700320 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
321 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700322 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000323 }
324
bsalomon6ade6dd2016-09-12 12:07:17 -0700325 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500326 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700327 SkScalarNearlyEqual(scales[0], 1.f) &&
328 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700329 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000330 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400331 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500332 SkRasterClip rc(this->devClipBounds());
333 SkDraw draw;
334 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
335 draw.fMatrix = &this->ctm();
336 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500337 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000338 return;
339 }
340
Brian Salomon1459ebd2017-12-19 16:12:44 -0500341 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700342
Mike Reeda1361362017-03-07 09:37:29 -0500343 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700344#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
345 // This offsetting in device space matches the expectations of the Android framework for non-AA
346 // points and lines.
347 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600348 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700349 tempMatrix = *viewMatrix;
350 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
351 tempMatrix.postTranslate(kOffset, kOffset);
352 viewMatrix = &tempMatrix;
353 }
354#endif
355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400357 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
358 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700359 return;
360 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361
Brian Osmanae0c50c2017-05-25 16:56:34 -0400362 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
363 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
364 nullptr);
365
366 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
367 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368}
369
370///////////////////////////////////////////////////////////////////////////////
371
Mike Reeda1361362017-03-07 09:37:29 -0500372void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800373 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500374 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
bsalomona7d85ba2016-07-06 11:54:59 -0700375 // A couple reasons we might need to call drawPath.
376 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700378 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500380 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400381 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500382 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383 return;
384 }
385
386 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400387 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
388 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700389 return;
390 }
Mike Klein744fb732014-06-23 15:13:26 -0400391
bsalomon6663acf2016-05-10 09:14:17 -0700392 GrStyle style(paint);
Chris Dalton3b51df12017-11-27 14:33:06 -0700393 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
394 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395}
396
397///////////////////////////////////////////////////////////////////////////////
398
Mike Reeda1361362017-03-07 09:37:29 -0500399void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800400 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500401 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000402 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400403 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
404 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700405 return;
406 }
Mike Klein744fb732014-06-23 15:13:26 -0400407
Mike Reed80747ef2018-01-23 15:29:32 -0500408 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000409 if (mf) {
410 if (mf->hasFragmentProcessor()) {
411 mf = nullptr; // already handled in SkPaintToGrPaint
412 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400413 }
414
bsalomon6663acf2016-05-10 09:14:17 -0700415 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400416 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000417 // try to hit the fast path for drawing filtered round rects
418
419 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500420 if (rrect.transform(this->ctm(), &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000421 if (devRRect.allCornersCircular()) {
Brian Osmane191ce62017-09-14 15:43:50 -0400422 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(), this->ctm(), nullptr)) {
Robert Phillips26c90e02017-03-14 14:39:29 -0400423 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400424 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500425 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000426 return;
427 }
428 }
429
430 }
431 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000432 }
433
Robert Phillipsa29a9562016-10-20 09:40:55 -0400434 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800435 // The only mask filter the native rrect drawing code could've handle was taken
436 // care of above.
437 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700439 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700440 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500441 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400442 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500443 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000444 return;
445 }
Mike Klein744fb732014-06-23 15:13:26 -0400446
bsalomon6663acf2016-05-10 09:14:17 -0700447 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800448
Chris Dalton3b51df12017-11-27 14:33:06 -0700449 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
450 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000451}
452
robertphillipsd7706102016-02-25 09:28:08 -0800453
Mike Reeda1361362017-03-07 09:37:29 -0500454void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800455 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800456 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500457 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800458 if (outer.isEmpty()) {
459 return;
460 }
461
462 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500463 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800464 }
465
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000466 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000467
robertphillips0e7029e2015-11-30 05:45:06 -0800468 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800469 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400470 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
471 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700472 return;
473 }
robertphillips00095892016-02-29 13:50:40 -0800474
Brian Salomon0166cfd2017-03-13 17:58:25 -0400475 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700476 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800477 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000478 }
479
480 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700481 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000482 path.addRRect(outer);
483 path.addRRect(inner);
484 path.setFillType(SkPath::kEvenOdd_FillType);
485
Brian Salomon0166cfd2017-03-13 17:58:25 -0400486 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
487 path, paint, this->ctm(), nullptr, this->devClipBounds(),
488 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000489}
490
491
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000492/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000493
Mike Reeda1361362017-03-07 09:37:29 -0500494void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700495 if (paint.getMaskFilter()) {
496 SkPath path;
497 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500498 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700499 }
500
501 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400502 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
503 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700504 return;
505 }
506
Chris Dalton3b51df12017-11-27 14:33:06 -0700507 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
508 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700509}
510
Mike Reeda1361362017-03-07 09:37:29 -0500511void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800512 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500513 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
robertphillips514450c2015-11-24 05:36:02 -0800514 // Presumably the path effect warps this to something other than an oval
515 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000516 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700517 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518 path.addOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500519 this->drawPath(path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520 return;
herb11a7f7f2015-11-24 12:41:00 -0800521 }
522
robertphillips514450c2015-11-24 05:36:02 -0800523 if (paint.getMaskFilter()) {
524 // The RRect path can handle special case blurring
525 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500526 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527 }
528
529 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400530 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
531 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700532 return;
533 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534
Chris Dalton3b51df12017-11-27 14:33:06 -0700535 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
536 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537}
538
Mike Reeda1361362017-03-07 09:37:29 -0500539void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700540 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
541 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500542 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700543 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500544 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700545 return;
546 }
547 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400548 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
549 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700550 return;
551 }
552
Chris Dalton3b51df12017-11-27 14:33:06 -0700553 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500554 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500555 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700556}
557
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000558#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559
560///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700561void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700562 const SkPaint& origPaint) {
563 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500564 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400565 // Adding support for round capping would require a
566 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700567 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
568 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
569 SkASSERT(!origPaint.getPathEffect());
570 SkASSERT(!origPaint.getMaskFilter());
571
572 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
573 SkASSERT(halfWidth > 0);
574
575 SkVector v = points[1] - points[0];
576
577 SkScalar length = SkPoint::Normalize(&v);
578 if (!length) {
579 v.fX = 1.0f;
580 v.fY = 0.0f;
581 }
582
583 SkPaint newPaint(origPaint);
584 newPaint.setStyle(SkPaint::kFill_Style);
585
586 SkScalar xtraLength = 0.0f;
587 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
588 xtraLength = halfWidth;
589 }
590
591 SkPoint mid = points[0] + points[1];
592 mid.scale(0.5f);
593
594 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
595 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
596 SkMatrix m;
597 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
598
599 SkMatrix local = m;
600
Mike Reeda1361362017-03-07 09:37:29 -0500601 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700602
603 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400604 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
605 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700606 return;
607 }
608
Brian Salomon82f44312017-01-11 13:42:54 -0500609 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700610 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700611}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000612
Mike Reeda1361362017-03-07 09:37:29 -0500613void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614 const SkPaint& paint, const SkMatrix* prePathMatrix,
615 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800616 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800617 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700618 SkPoint points[2];
619 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
620 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500621 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700622 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500623 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700624 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500625 // Round capping support is currently disabled b.c. it would require a RRect
626 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500627 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700628 return;
629 }
630 }
robertphillipsff55b492015-11-24 07:56:59 -0800631 }
632
Hal Canary144caf52016-11-07 17:57:18 -0500633 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Brian Salomona3cf3652018-01-03 15:11:00 -0500634 if (!prePathMatrix && !paint.getMaskFilter()) {
635 GrPaint grPaint;
636 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
637 this->ctm(), &grPaint)) {
638 return;
639 }
640 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
641 this->ctm(), origSrcPath, GrStyle(paint));
642 return;
643 }
Brian Salomon0166cfd2017-03-13 17:58:25 -0400644 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
645 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500646 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647}
648
649static const int kBmpSmallTileSize = 1 << 10;
650
651static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
652 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
653 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
654 return tilesX * tilesY;
655}
656
reed85d91782015-09-10 14:33:38 -0700657static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658 if (maxTileSize <= kBmpSmallTileSize) {
659 return maxTileSize;
660 }
661
662 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
663 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
664
665 maxTileTotalTileSize *= maxTileSize * maxTileSize;
666 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
667
668 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
669 return kBmpSmallTileSize;
670 } else {
671 return maxTileSize;
672 }
673}
674
675// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
676// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700677static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800678 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800679 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700680 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700681 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000682 const SkRect* srcRectPtr,
683 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700684 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700685 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
686 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 clippedSrcIRect->setEmpty();
688 return;
689 }
690 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
691 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700692 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 if (!clippedSrcRect.intersect(*srcRectPtr)) {
694 clippedSrcIRect->setEmpty();
695 return;
696 }
697 }
698 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700699 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700 if (!clippedSrcIRect->intersect(bmpBounds)) {
701 clippedSrcIRect->setEmpty();
702 }
703}
704
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400705bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
706 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700707 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700708 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400709 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700710 const SkRect* srcRectPtr,
711 int maxTileSize,
712 int* tileSize,
713 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800714 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700715 // if it's larger than the max tile size, then we have no choice but tiling.
716 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400717 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400718 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
719 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700720 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
721 return true;
722 }
723
bsalomon1a1d0b82015-10-16 07:49:42 -0700724 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700725 const size_t area = imageRect.width() * imageRect.height();
726 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
727 return false;
728 }
729
reed85d91782015-09-10 14:33:38 -0700730 // At this point we know we could do the draw by uploading the entire bitmap
731 // as a texture. However, if the texture would be large compared to the
732 // cache size and we don't require most of it for this draw then tile to
733 // reduce the amount of upload and cache spill.
734
735 // assumption here is that sw bitmap size is a good proxy for its size as
736 // a texture
737 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
738 size_t cacheSize;
739 fContext->getResourceCacheLimits(nullptr, &cacheSize);
740 if (bmpSize < cacheSize / 2) {
741 return false;
742 }
743
bsalomon1a1d0b82015-10-16 07:49:42 -0700744 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
745 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400746 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
747 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400748 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700749 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
750 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500751 kBmpSmallTileSize * kBmpSmallTileSize *
752 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700753
Brian Osmand05cdc32017-02-06 13:24:47 -0500754 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700755}
756
reed85d91782015-09-10 14:33:38 -0700757bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
758 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700759 const SkMatrix& viewMatrix,
760 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800761 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400762 // If image is explicitly texture backed then we shouldn't get here.
763 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400765 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700766 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500767 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
768 quality, viewMatrix, srcToDstRect, fContext->contextPriv().sharpenMipmappedTextures(),
769 &doBicubic);
reed85d91782015-09-10 14:33:38 -0700770
771 int tileFilterPad;
772 if (doBicubic) {
773 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400774 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700775 tileFilterPad = 0;
776 } else {
777 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400779 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
bsalomon8c07b7a2015-11-02 11:36:52 -0800781 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782
reed85d91782015-09-10 14:33:38 -0700783 // these are output, which we safely ignore, as we just want to know the predicate
784 int outTileSize;
785 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
bsalomone553b642016-08-17 09:02:09 -0700787 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400788 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700789 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790}
791
Mike Reeda1361362017-03-07 09:37:29 -0500792void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400793 SkScalar x,
794 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400796 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800797 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800798 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500799 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700800
bsalomonb1b01992015-11-18 10:56:08 -0800801 int maxTileSize = fContext->caps()->maxTileSize();
802
803 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
804 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400805 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
806 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800807 bitmap.height() <= maxTileSize;
808
809 bool skipTileCheck = drawAA || paint.getMaskFilter();
810
811 if (!skipTileCheck) {
812 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
813 int tileSize;
814 SkIRect clippedSrcRect;
815
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400816 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800817 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400818 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500819 paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
820 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800821
822 int tileFilterPad;
823
824 if (doBicubic) {
825 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400826 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800827 tileFilterPad = 0;
828 } else {
829 tileFilterPad = 1;
830 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400831 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800832
833 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700834 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400835 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700836 &tileSize, &clippedSrcRect)) {
837 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400838 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
839 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800840 return;
841 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000842 }
Hal Canary144caf52016-11-07 17:57:18 -0500843 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800844 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -0400845 viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000846}
847
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000848// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
850// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000851static inline void clamped_outset_with_offset(SkIRect* iRect,
852 int outset,
853 SkPoint* offset,
854 const SkIRect& clamp) {
855 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000857 int leftClampDelta = clamp.fLeft - iRect->fLeft;
858 if (leftClampDelta > 0) {
859 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000860 iRect->fLeft = clamp.fLeft;
861 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000862 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000864
865 int topClampDelta = clamp.fTop - iRect->fTop;
866 if (topClampDelta > 0) {
867 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000868 iRect->fTop = clamp.fTop;
869 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000870 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871 }
872
873 if (iRect->fRight > clamp.fRight) {
874 iRect->fRight = clamp.fRight;
875 }
876 if (iRect->fBottom > clamp.fBottom) {
877 iRect->fBottom = clamp.fBottom;
878 }
879}
880
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881// Break 'bitmap' into several tiles to draw it since it has already
882// been determined to be too large to fit in VRAM
883void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800884 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700885 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000886 const SkRect& srcRect,
887 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400888 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800889 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700890 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000891 int tileSize,
892 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800893 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800894
ericrk983294f2016-04-18 09:14:00 -0700895 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800896 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700897 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800898
bsalomonc55271f2015-11-09 11:55:57 -0800899 const SkPaint* paint = &origPaint;
900 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400901 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800902 // Drop antialiasing to avoid seams at tile boundaries.
903 tempPaint = origPaint;
904 tempPaint.setAntiAlias(false);
905 paint = &tempPaint;
906 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
908
909 int nx = bitmap.width() / tileSize;
910 int ny = bitmap.height() / tileSize;
911 for (int x = 0; x <= nx; x++) {
912 for (int y = 0; y <= ny; y++) {
913 SkRect tileR;
914 tileR.set(SkIntToScalar(x * tileSize),
915 SkIntToScalar(y * tileSize),
916 SkIntToScalar((x + 1) * tileSize),
917 SkIntToScalar((y + 1) * tileSize));
918
919 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
920 continue;
921 }
922
923 if (!tileR.intersect(srcRect)) {
924 continue;
925 }
926
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 SkIRect iTileR;
928 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700929 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
930 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500931 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700932 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400933 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 SkIRect iClampRect;
935
reeda5517e22015-07-14 10:54:12 -0700936 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000937 // In bleed mode we want to always expand the tile on all edges
938 // but stay within the bitmap bounds
939 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
940 } else {
941 // In texture-domain/clamp mode we only want to expand the
942 // tile on edges interior to "srcRect" (i.e., we want to
943 // not bleed across the original clamped edges)
944 srcRect.roundOut(&iClampRect);
945 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
947 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 }
949
bsalomone553b642016-08-17 09:02:09 -0700950 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 if (bitmap.extractSubset(&tmpB, iTileR)) {
952 // now offset it to make it "local" to our tmp bitmap
953 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800954 // de-optimized this determination
955 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700956 this->drawBitmapTile(tmpB,
957 viewMatrix,
958 rectToDraw,
959 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500960 params,
bsalomone553b642016-08-17 09:02:09 -0700961 *paint,
962 constraint,
963 bicubic,
964 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 }
966 }
967 }
968}
969
bsalomone553b642016-08-17 09:02:09 -0700970void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
971 const SkMatrix& viewMatrix,
972 const SkRect& dstRect,
973 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400974 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700975 const SkPaint& paint,
976 SkCanvas::SrcRectConstraint constraint,
977 bool bicubic,
978 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800979 // We should have already handled bitmaps larger than the max texture size.
980 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
981 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700982 // We should be respecting the max tile size by the time we get here.
983 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
984 bitmap.height() <= fContext->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400985 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000986
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400987 sk_sp<GrTextureProxy> proxy =
988 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, nullptr);
Robert Phillips78075802017-03-23 11:11:59 -0400989 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 return;
991 }
bsalomone553b642016-08-17 09:02:09 -0700992
bsalomone553b642016-08-17 09:02:09 -0700993 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillipse14d3052017-02-15 13:18:21 -0500994 const SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect,
995 SkMatrix::kFill_ScaleToFit);
joshualitt5f10b5c2015-07-09 10:24:35 -0700996
997 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
998 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400999 std::unique_ptr<GrFragmentProcessor> fp;
Brian Osmanf06ead92017-10-30 13:47:41 -04001000 GrPixelConfig config = proxy->config();
joshualitt5f10b5c2015-07-09 10:24:35 -07001001
reeda5517e22015-07-14 10:54:12 -07001002 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001004 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001005 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001006 domain.fLeft = srcRect.fLeft + 0.5f;
1007 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001009 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010 }
1011 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001012 domain.fTop = srcRect.fTop + 0.5f;
1013 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001015 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001016 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001017 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001018 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001019 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001020 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001021 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001022 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001023 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001024 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1025 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001026 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001028 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029 }
1030
Brian Osmanf06ead92017-10-30 13:47:41 -04001031 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001032 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001033 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001034 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1035 viewMatrix, std::move(fp),
1036 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001037 return;
1038 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001040 // Coverage-based AA would cause seams between tiles.
Chris Dalton3b51df12017-11-27 14:33:06 -07001041 GrAA aa = GrAA(paint.isAntiAlias() &&
1042 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001043 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001044}
1045
Mike Reeda1361362017-03-07 09:37:29 -05001046void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001048 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001049 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050
robertphillips970587b2016-07-14 14:12:55 -07001051 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052 return;
1053 }
1054
Robert Phillipse14d3052017-02-15 13:18:21 -05001055 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1056 if (!srcImg) {
1057 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001058 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059
Florin Malita53f77bd2017-04-28 13:48:37 -04001060 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001061}
1062
1063
Florin Malita53f77bd2017-04-28 13:48:37 -04001064void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
1065 SkImage* clipImage,const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001066 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001067 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001068
Florin Malita53f77bd2017-04-28 13:48:37 -04001069 // TODO: clipImage support.
1070
robertphillips970587b2016-07-14 14:12:55 -07001071 SkIPoint offset = { 0, 0 };
1072
1073 sk_sp<SkSpecialImage> result;
1074 if (paint.getImageFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -05001075 result = this->filterTexture(special1, left, top,
robertphillips970587b2016-07-14 14:12:55 -07001076 &offset,
1077 paint.getImageFilter());
1078 if (!result) {
1079 return;
1080 }
1081 } else {
1082 result = sk_ref_sp(special1);
1083 }
1084
1085 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001086 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001087 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001088 return;
1089 }
robertphillips970587b2016-07-14 14:12:55 -07001090
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001091 const GrPixelConfig config = proxy->config();
1092
robertphillips970587b2016-07-14 14:12:55 -07001093 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001094 if (tmpUnfiltered.getMaskFilter()) {
1095 SkMatrix ctm = this->ctm();
1096 ctm.postTranslate(-SkIntToScalar(left + offset.fX), -SkIntToScalar(top + offset.fY));
1097 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithLocalMatrix(ctm));
1098 }
1099
robertphillips970587b2016-07-14 14:12:55 -07001100 tmpUnfiltered.setImageFilter(nullptr);
1101
Brian Osman2240be92017-10-18 13:15:13 -04001102 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osmanf06ead92017-10-30 13:47:41 -04001103 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001104 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001105 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001106 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001107 } else {
Mike Reed28eaed22018-02-01 11:24:53 -05001108 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001109 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001110
1111 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001112 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1113 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001114 return;
1115 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116
robertphillips970587b2016-07-14 14:12:55 -07001117 const SkIRect& subset = result->subset();
1118
Brian Salomon82f44312017-01-11 13:42:54 -05001119 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001120 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001121 std::move(grPaint),
Mike Reed2179b782018-02-07 11:59:57 -05001122 GrAA(tmpUnfiltered.isAntiAlias()),
Brian Salomon82f44312017-01-11 13:42:54 -05001123 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001124 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1125 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001126 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127}
1128
Mike Reeda1361362017-03-07 09:37:29 -05001129void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001130 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001131 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001132 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001133 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1134 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1135 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1136 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1137 const SkRect* dst = &origDst;
1138 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001140 if (!src) {
1141 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001143
bsalomonb1b01992015-11-18 10:56:08 -08001144 SkMatrix srcToDstMatrix;
1145 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1146 return;
1147 }
1148 SkRect tmpSrc, tmpDst;
1149 if (src != &bmpBounds) {
1150 if (!bmpBounds.contains(*src)) {
1151 tmpSrc = *src;
1152 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 return; // nothing to draw
1154 }
bsalomonb1b01992015-11-18 10:56:08 -08001155 src = &tmpSrc;
1156 srcToDstMatrix.mapRect(&tmpDst, *src);
1157 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158 }
1159 }
1160
bsalomonb1b01992015-11-18 10:56:08 -08001161 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001162
bsalomonb1b01992015-11-18 10:56:08 -08001163 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1164 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001165 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1166 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1167 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001168
Brian Salomon7c8460e2017-05-12 11:36:10 -04001169 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001170
1171 if (!skipTileCheck) {
1172 int tileSize;
1173 SkIRect clippedSrcRect;
1174
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001175 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001176 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001177 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001178 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
1179 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001180
1181 int tileFilterPad;
1182
1183 if (doBicubic) {
1184 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001185 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001186 tileFilterPad = 0;
1187 } else {
1188 tileFilterPad = 1;
1189 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001190 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001191
1192 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001193 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001194 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1195 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001196 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001197 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001198 return;
1199 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001200 }
Hal Canary144caf52016-11-07 17:57:18 -05001201 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon34169692017-08-28 15:32:01 -04001202 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203}
1204
robertphillips6451a0c2016-07-18 08:31:31 -07001205sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001206 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1207 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001208 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->contextPriv().proxyProvider(),
1209 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001210 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001211 return nullptr;
1212 }
1213
Robert Phillipse14d3052017-02-15 13:18:21 -05001214 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001215
Robert Phillipse14d3052017-02-15 13:18:21 -05001216 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1217 // the special image
1218 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1219 rect,
1220 bitmap.getGenerationID(),
1221 std::move(proxy),
1222 bitmap.refColorSpace(),
1223 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001224}
1225
reede51c3562016-07-19 14:33:20 -07001226sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001227 SkPixmap pm;
1228 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001229 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001230
Robert Phillips6de99042017-01-31 11:31:39 -05001231 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1232 SkIRect::MakeWH(image->width(), image->height()),
1233 image->uniqueID(),
1234 std::move(proxy),
1235 as_IB(image)->onImageInfo().refColorSpace(),
1236 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001237 } else if (image->peekPixels(&pm)) {
1238 SkBitmap bm;
1239
1240 bm.installPixels(pm);
1241 return this->makeSpecial(bm);
1242 } else {
1243 return nullptr;
1244 }
1245}
1246
1247sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001248 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1249 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001250 // When the device doesn't have a texture, we create a temporary texture.
1251 // TODO: we should actually only copy the portion of the source needed to apply the image
1252 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001253 proxy = GrSurfaceProxy::Copy(fContext.get(),
1254 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001255 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001256 SkBudgeted::kYes);
1257 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001258 return nullptr;
1259 }
robertphillips1b5f9682016-07-15 08:01:12 -07001260 }
1261
1262 const SkImageInfo ii = this->imageInfo();
1263 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1264
Robert Phillipse2f7d182016-12-15 09:23:05 -05001265 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1266 srcRect,
1267 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001268 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001269 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001270 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001271}
1272
Mike Reeda1361362017-03-07 09:37:29 -05001273void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001274 int left, int top, const SkPaint& paint) {
1275 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001276
joshualittce894002016-01-11 13:29:31 -08001277 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001279 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001282 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001283 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001284 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001285 return;
1286 }
1287
Mike Reed2179b782018-02-07 11:59:57 -05001288 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001289}
1290
Brian Salomon34169692017-08-28 15:32:01 -04001291void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001292 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001293 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001294 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001295 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001296 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001297 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1298 image->alphaType(), nullptr, nullptr,
1299 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001300 return;
Brian Salomon34169692017-08-28 15:32:01 -04001301 }
1302 SkBitmap bm;
1303 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1304 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1305 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001306 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001307 return;
reed85d91782015-09-10 14:33:38 -07001308 }
Brian Salomon34169692017-08-28 15:32:01 -04001309 this->drawBitmap(bm, x, y, paint);
1310 return;
1311 }
1312 if (image->isLazyGenerated()) {
1313 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1314 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1315 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1316 return;
1317 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001318 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001319 GrBitmapTextureMaker maker(fContext.get(), bm);
1320 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1321 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001322 }
1323}
1324
Brian Salomon34169692017-08-28 15:32:01 -04001325void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1326 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001327 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001328 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001329 if (!src || src->contains(image->bounds())) {
1330 constraint = SkCanvas::kFast_SrcRectConstraint;
1331 }
Robert Phillips3798c862017-03-27 11:08:16 -04001332 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001333 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1334 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001335 return;
1336 }
1337 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001338 SkMatrix srcToDstRect;
1339 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1340 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001341 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001342 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001343 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001344 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001345 return;
1346 }
Mike Reeda1361362017-03-07 09:37:29 -05001347 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001348 return;
1349 }
1350 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001351 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001352 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1353 this->ctm(), paint);
1354 return;
1355 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001356 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001357 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001358 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1359 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001360 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001361}
1362
Mike Reeda1361362017-03-07 09:37:29 -05001363void SkGpuDevice::drawProducerNine(GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001364 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001365 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001366
joshualittedb36442015-11-19 14:29:30 -08001367 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Salomon7c8460e2017-05-12 11:36:10 -04001368 GrFSAAType::kUnifiedMSAA == fRenderTargetContext->fsaaType();
joshualitt33a5fce2015-11-18 13:28:51 -08001369 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001370 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001371 paint.getFilterQuality(), this->ctm(), SkMatrix::I(),
1372 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001373 if (useFallback || doBicubic || GrSamplerState::Filter::kNearest != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001374 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001375
1376 SkRect srcR, dstR;
1377 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001378 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -04001379 this->ctm(), paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001380 }
1381 return;
1382 }
1383
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001384 static const GrSamplerState::Filter kMode = GrSamplerState::Filter::kNearest;
Brian Salomonaff329b2017-08-11 09:40:37 -04001385 auto fp = producer->createFragmentProcessor(
1386 SkMatrix::I(), SkRect::MakeIWH(producer->width(), producer->height()),
1387 GrTextureProducer::kNo_FilterConstraint, true, &kMode,
Brian Salomonf3569f02017-10-24 12:52:33 -04001388 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips1c9686b2017-06-30 08:40:28 -04001389 if (!fp) {
1390 return;
1391 }
joshualitt33a5fce2015-11-18 13:28:51 -08001392 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001393 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001394 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001395 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001396 return;
1397 }
1398
msarett10e3d9b2016-08-18 15:46:03 -07001399 std::unique_ptr<SkLatticeIter> iter(
1400 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001401 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001402 producer->width(), producer->height(), std::move(iter),
1403 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001404}
1405
Mike Reeda1361362017-03-07 09:37:29 -05001406void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001407 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001408 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001409 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001410 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001411 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001412 image->alphaType(), pinnedUniqueID,
1413 as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001414 this->drawProducerNine(&adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001415 } else {
1416 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001417 if (image->isLazyGenerated()) {
1418 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001419 this->drawProducerNine(&maker, center, dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001420 } else if (as_IB(image)->getROPixels(&bm,
1421 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001422 this->drawBitmapNine(bm, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001423 }
1424 }
1425}
1426
Mike Reeda1361362017-03-07 09:37:29 -05001427void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001428 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001429 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001430 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001431 this->drawProducerNine(&maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001432}
1433
Mike Reeda1361362017-03-07 09:37:29 -05001434void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
msarett10e3d9b2016-08-18 15:46:03 -07001435 const SkCanvas::Lattice& lattice, const SkRect& dst,
1436 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001437 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001438
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001439 static const GrSamplerState::Filter kMode = GrSamplerState::Filter::kNearest;
Brian Salomonaff329b2017-08-11 09:40:37 -04001440 std::unique_ptr<GrFragmentProcessor> fp(producer->createFragmentProcessor(
1441 SkMatrix::I(), SkRect::MakeIWH(producer->width(), producer->height()),
1442 GrTextureProducer::kNo_FilterConstraint, true, &kMode,
Brian Salomonf3569f02017-10-24 12:52:33 -04001443 fRenderTargetContext->colorSpaceInfo().colorSpace()));
Robert Phillips1c9686b2017-06-30 08:40:28 -04001444 if (!fp) {
1445 return;
1446 }
msarett10e3d9b2016-08-18 15:46:03 -07001447 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001448 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001449 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001450 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001451 return;
1452 }
1453
1454 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001455 new SkLatticeIter(lattice, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001456 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001457 producer->width(), producer->height(), std::move(iter),
1458 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001459}
1460
Mike Reeda1361362017-03-07 09:37:29 -05001461void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001462 const SkCanvas::Lattice& lattice, const SkRect& dst,
1463 const SkPaint& paint) {
1464 ASSERT_SINGLE_OWNER
1465 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001466 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001467 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001468 image->alphaType(), pinnedUniqueID,
1469 as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001470 this->drawProducerLattice(&adjuster, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001471 } else {
1472 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001473 if (image->isLazyGenerated()) {
1474 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001475 this->drawProducerLattice(&maker, lattice, dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001476 } else if (as_IB(image)->getROPixels(&bm,
1477 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001478 this->drawBitmapLattice(bm, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001479 }
1480 }
1481}
1482
Mike Reeda1361362017-03-07 09:37:29 -05001483void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001484 const SkCanvas::Lattice& lattice, const SkRect& dst,
1485 const SkPaint& paint) {
1486 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001487 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001488 this->drawProducerLattice(&maker, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001489}
1490
Brian Salomonf3569f02017-10-24 12:52:33 -04001491static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1492 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001493 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001494 if (hasTexs && skPaint.getShader()) {
1495 if (hasColors) {
1496 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001497 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1498 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001499 } else {
1500 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001501 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001502 }
1503 } else {
1504 if (hasColors) {
1505 // We have colors, but either have no shader or no texture coords (which implies that
1506 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001507 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001508 } else {
1509 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001510 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001511 }
1512 }
1513}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001514
Mike Reed887cdf12017-04-03 11:11:09 -04001515void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001516 const SkPoint vertices[], SkBlendMode bmode,
1517 const uint16_t indices[], int indexCount,
1518 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001519 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001520 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001521
Mike Reed2f6b5a42017-03-19 15:04:17 -04001522 SkPaint copy(paint);
1523 copy.setStyle(SkPaint::kStroke_Style);
1524 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001525
Mike Reed2f6b5a42017-03-19 15:04:17 -04001526 GrPaint grPaint;
1527 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001528 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1529 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001530 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001531 }
1532
Mike Reed2f6b5a42017-03-19 15:04:17 -04001533 int triangleCount = 0;
1534 int n = (nullptr == indices) ? vertexCount : indexCount;
1535 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001536 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001537 triangleCount = n / 3;
1538 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001539 case SkVertices::kTriangleStrip_VertexMode:
1540 case SkVertices::kTriangleFan_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001541 triangleCount = n - 2;
1542 break;
1543 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001544
Mike Reed2f6b5a42017-03-19 15:04:17 -04001545 VertState state(vertexCount, indices, indexCount);
1546 VertState::Proc vertProc = state.chooseProc(vmode);
1547
1548 //number of indices for lines per triangle with kLines
1549 indexCount = triangleCount * 6;
1550
Brian Osmanae0c50c2017-05-25 16:56:34 -04001551 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1552 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1553 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1554
1555 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001556 int i = 0;
1557 while (vertProc(&state)) {
1558 lineIndices[i] = state.f0;
1559 lineIndices[i + 1] = state.f1;
1560 lineIndices[i + 2] = state.f1;
1561 lineIndices[i + 3] = state.f2;
1562 lineIndices[i + 4] = state.f2;
1563 lineIndices[i + 5] = state.f0;
1564 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001565 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001566
Chris Dalton3809bab2017-06-13 10:55:06 -06001567 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001568 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001569 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001570 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001571 builder.detach(),
1572 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573}
1574
Brian Osman0b403f82017-05-26 10:39:51 -04001575void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001576 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001577 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001578
1579 SkASSERT(vertices);
1580 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001581 bool hasColors = vertices->hasColors();
1582 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001583 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001584 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001585 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1586 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001587 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001588 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001589 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1590 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001591 return;
1592 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001593 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001594 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001595}
1596
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001597///////////////////////////////////////////////////////////////////////////////
1598
Jim Van Verth3af1af92017-05-18 15:06:54 -04001599void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1600
1601 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001602 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1603
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001604 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001605 // failed to find an accelerated case
1606 this->INHERITED::drawShadow(path, rec);
1607 }
1608}
1609
1610///////////////////////////////////////////////////////////////////////////////
1611
Mike Reeda1361362017-03-07 09:37:29 -05001612void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001613 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001614 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001615 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001616 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001617 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001618 return;
1619 }
1620
Hal Canary144caf52016-11-07 17:57:18 -05001621 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001622
reedca109532015-06-25 16:25:25 -07001623 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001624 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001625
jvanverth31ff7622015-08-07 10:09:28 -07001626 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001627 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001628 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1629 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001630 return;
1631 }
1632 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001633 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1634 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001635 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001636 }
1637 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001638
1639 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001640 fRenderTargetContext->drawAtlas(
1641 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001642}
1643
1644///////////////////////////////////////////////////////////////////////////////
1645
Mike Reeda1361362017-03-07 09:37:29 -05001646void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001647 size_t byteLength, SkScalar x, SkScalar y,
1648 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001649 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001650 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001651 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001652
Brian Salomon0166cfd2017-03-13 17:58:25 -04001653 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1654 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001655}
1656
Mike Reeda1361362017-03-07 09:37:29 -05001657void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001658 const SkScalar pos[], int scalarsPerPos,
1659 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001660 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001661 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001662 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001663
Brian Salomon0166cfd2017-03-13 17:58:25 -04001664 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1665 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001666 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667}
1668
Mike Reeda1361362017-03-07 09:37:29 -05001669void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001670 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001671 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001672 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001673 SkDEBUGCODE(this->validate();)
1674
Brian Salomon0166cfd2017-03-13 17:58:25 -04001675 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1676 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001677}
1678
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679///////////////////////////////////////////////////////////////////////////////
1680
reedb2db8982014-11-13 12:41:02 -08001681bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001682 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683}
1684
Greg Daniela5cb7812017-06-16 09:45:32 -04001685///////////////////////////////////////////////////////////////////////////////
1686
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001688 this->flushAndSignalSemaphores(0, nullptr);
1689}
1690
Greg Daniel51316782017-08-02 15:10:09 +00001691GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1692 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001693 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001694
Greg Danielc64ee462017-06-15 16:59:49 -04001695 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001696}
1697
Greg Danielc64ee462017-06-15 16:59:49 -04001698bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001699 ASSERT_SINGLE_OWNER
1700
Greg Danielc64ee462017-06-15 16:59:49 -04001701 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702}
1703
1704///////////////////////////////////////////////////////////////////////////////
1705
reed76033be2015-03-14 10:54:31 -07001706SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001707 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708
robertphillipsca6eafc2016-05-17 09:57:46 -07001709 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001710
robertphillipsca6eafc2016-05-17 09:57:46 -07001711 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001712 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001713 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1714 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001715
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001716 sk_sp<GrRenderTargetContext> rtc(fContext->makeDeferredRenderTargetContext(
Brian Salomonf3569f02017-10-24 12:52:33 -04001717 fit, cinfo.fInfo.width(), cinfo.fInfo.height(),
1718 fRenderTargetContext->colorSpaceInfo().config(),
1719 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001720 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1721 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001722 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001723 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001725
1726 // Skia's convention is to only clear a device if it is non-opaque.
1727 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1728
Robert Phillips9fab7e92016-11-17 12:45:04 -05001729 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1730 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001731}
1732
reede8f30622016-03-23 18:59:25 -07001733sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001734 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001735 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001736 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001737 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001738 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001739 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001740}
1741
senorblanco900c3672016-04-27 11:31:23 -07001742SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001743 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001744 // We always return a transient cache, so it is freed after each
1745 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001746 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001747}
reedf037e0b2014-10-30 11:34:15 -07001748
1749#endif