blob: 0cf2ae589397fe78fda3646c1ecb3be210cb156a [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"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080012#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050013#include "GrImageTextureMaker.h"
14#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070015#include "GrStyle.h"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040016#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050017#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050018#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070021#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070023#include "SkGr.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070025#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050026#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050027#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070028#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
Brian Salomon6a639042016-12-14 11:08:17 -050034#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050035#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080036#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070037#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000039#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080040#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000041#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000042#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000043#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050044#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050045#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080046#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080047#include "effects/GrSimpleTextureEffect.h"
48#include "effects/GrTextureDomain.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040049#include "../private/SkShadowFlags.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080050#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051
reedf037e0b2014-10-30 11:34:15 -070052#if SK_SUPPORT_GPU
53
joshualittce894002016-01-11 13:29:31 -080054#define ASSERT_SINGLE_OWNER \
55 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
56
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058///////////////////////////////////////////////////////////////////////////////
59
bsalomon74f681d2015-06-23 14:38:48 -070060/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
61 should fail. */
62bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
63 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
64 *flags = 0;
65 if (info) {
66 switch (info->alphaType()) {
67 case kPremul_SkAlphaType:
68 break;
69 case kOpaque_SkAlphaType:
70 *flags |= SkGpuDevice::kIsOpaque_Flag;
71 break;
72 default: // If it is unpremul or unknown don't try to render
73 return false;
74 }
75 }
76 if (kClear_InitContents == init) {
77 *flags |= kNeedClear_Flag;
78 }
79 return true;
80}
81
Robert Phillips9fab7e92016-11-17 12:45:04 -050082sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
83 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070084 int width, int height,
85 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040086 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070087 return nullptr;
88 }
89 unsigned flags;
90 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
91 return nullptr;
92 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050093 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
94 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000095}
96
robertphillips24e91282016-04-29 06:46:36 -070097sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
98 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -070099 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700100 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700101 unsigned flags;
102 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700103 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700104 }
105
Brian Osman11052242016-10-27 14:47:55 -0400106 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
107 info, sampleCount,
108 origin, props));
109 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700110 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700111 }
112
Robert Phillips9fab7e92016-11-17 12:45:04 -0500113 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700114 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700115}
116
Brian Osman11052242016-10-27 14:47:55 -0400117static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700118 SkColorType colorType;
119 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
120 colorType = kUnknown_SkColorType;
121 }
122 return SkImageInfo::Make(w, h, colorType,
123 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
Robert Phillips75a475c2017-01-13 09:18:59 -0500124 context->refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700125}
126
Robert Phillips9fab7e92016-11-17 12:45:04 -0500127SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
128 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400129 : INHERITED(make_info(renderTargetContext.get(), width, height,
130 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500131 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400132 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700133{
robertphillips1f3923e2016-07-21 07:17:54 -0700134 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700135 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000136
bsalomone63ffef2016-02-05 07:17:34 -0800137 if (flags & kNeedClear_Flag) {
138 this->clearAll();
139 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140}
141
Brian Osman11052242016-10-27 14:47:55 -0400142sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
143 GrContext* context,
144 SkBudgeted budgeted,
145 const SkImageInfo& origInfo,
146 int sampleCount,
147 GrSurfaceOrigin origin,
148 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000149 if (kUnknown_SkColorType == origInfo.colorType() ||
150 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700151 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000152 }
153
bsalomonafe30052015-01-16 07:32:33 -0800154 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800156 }
157
Brian Osman0c2997b2017-01-11 16:58:42 -0500158 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400159 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
160 // they need to be exact.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400161 return context->makeDeferredRenderTargetContext(
162 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700163 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500164 config, origInfo.refColorSpace(), sampleCount,
robertphillips6738c702016-07-27 12:13:51 -0700165 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800166}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000167
Mike Reeda1361362017-03-07 09:37:29 -0500168sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700169 int left, int top,
170 SkIPoint* offset,
171 const SkImageFilter* filter) {
172 SkASSERT(srcImg->isTextureBacked());
173 SkASSERT(filter);
174
Mike Reeda1361362017-03-07 09:37:29 -0500175 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700176 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500177 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500178 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400179 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700180 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700181
182 return filter->filterImage(srcImg, ctx, offset);
183}
184
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000185///////////////////////////////////////////////////////////////////////////////
186
Mike Reed353196f2017-07-21 11:01:18 -0400187bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800188 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000189
Mike Reed353196f2017-07-21 11:01:18 -0400190 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500191 return false;
192 }
193
Mike Reed353196f2017-07-21 11:01:18 -0400194 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500195 if (!rec.trim(this->width(), this->height())) {
196 return false;
197 }
198
199 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000200}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201
Mike Reed353196f2017-07-21 11:01:18 -0400202bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800203 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700204
Mike Reed353196f2017-07-21 11:01:18 -0400205 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500206 return false;
207 }
208
Mike Reed353196f2017-07-21 11:01:18 -0400209 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500210 if (!rec.trim(this->width(), this->height())) {
211 return false;
212 }
213
214 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000215}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216
reed41e010c2015-06-09 12:16:53 -0700217bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800218 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700219 return false;
220}
221
Brian Osman11052242016-10-27 14:47:55 -0400222GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700223 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400224 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700225}
226
reed8eddfb52014-12-04 07:50:14 -0800227void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800228 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500229 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500230
reed8eddfb52014-12-04 07:50:14 -0800231 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500232 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800233}
234
Brian Osman11052242016-10-27 14:47:55 -0400235void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800236 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800237
Brian Osman693a5402016-10-27 15:13:22 -0400238 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800239
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400240 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
241 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400242 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400243 this->context(),
244 budgeted,
245 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400246 fRenderTargetContext->numColorSamples(),
247 fRenderTargetContext->origin(),
Brian Osman11052242016-10-27 14:47:55 -0400248 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400249 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800250 return;
251 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400252 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800253
254 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400255 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800256 return;
257 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500258 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800259 }
260
Brian Osman693a5402016-10-27 15:13:22 -0400261 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800262}
263
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000264///////////////////////////////////////////////////////////////////////////////
265
Mike Reeda1361362017-03-07 09:37:29 -0500266void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800267 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500268 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269
270 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500271 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400272 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700273 return;
274 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275
Brian Salomon0166cfd2017-03-13 17:58:25 -0400276 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277}
278
279// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700280static const GrPrimitiveType gPointMode2PrimitiveType[] = {
Chris Dalton3809bab2017-06-13 10:55:06 -0600281 GrPrimitiveType::kPoints,
282 GrPrimitiveType::kLines,
283 GrPrimitiveType::kLineStrip
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284};
285
Mike Reeda1361362017-03-07 09:37:29 -0500286void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800288 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500289 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290 SkScalar width = paint.getStrokeWidth();
291 if (width < 0) {
292 return;
293 }
294
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000295 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700296 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700297 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500298 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700299 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700300 return;
301 }
egdaniele61c4112014-06-12 10:24:21 -0700302 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700303 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700304 path.moveTo(pts[0]);
305 path.lineTo(pts[1]);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400306 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint),
307 GrBoolToAA(paint.isAntiAlias()), this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700308 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000309 }
310
bsalomon6ade6dd2016-09-12 12:07:17 -0700311 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500312 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700313 SkScalarNearlyEqual(scales[0], 1.f) &&
314 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700315 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400317 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500318 SkRasterClip rc(this->devClipBounds());
319 SkDraw draw;
320 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
321 draw.fMatrix = &this->ctm();
322 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500323 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324 return;
325 }
326
bsalomon6ade6dd2016-09-12 12:07:17 -0700327 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
328
Mike Reeda1361362017-03-07 09:37:29 -0500329 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700330#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
331 // This offsetting in device space matches the expectations of the Android framework for non-AA
332 // points and lines.
333 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600334 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700335 tempMatrix = *viewMatrix;
336 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
337 tempMatrix.postTranslate(kOffset, kOffset);
338 viewMatrix = &tempMatrix;
339 }
340#endif
341
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000342 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400343 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
344 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700345 return;
346 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347
Brian Osmanae0c50c2017-05-25 16:56:34 -0400348 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
349 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
350 nullptr);
351
352 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
353 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354}
355
356///////////////////////////////////////////////////////////////////////////////
357
Mike Reeda1361362017-03-07 09:37:29 -0500358void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800359 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500360 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
bsalomona7d85ba2016-07-06 11:54:59 -0700361 // A couple reasons we might need to call drawPath.
362 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700364 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500366 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400367 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500368 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369 return;
370 }
371
372 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500373 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400374 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700375 return;
376 }
Mike Klein744fb732014-06-23 15:13:26 -0400377
bsalomon6663acf2016-05-10 09:14:17 -0700378 GrStyle style(paint);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400379 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint),
380 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381}
382
383///////////////////////////////////////////////////////////////////////////////
384
Mike Reeda1361362017-03-07 09:37:29 -0500385void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800386 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500387 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000388 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500389 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400390 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700391 return;
392 }
Mike Klein744fb732014-06-23 15:13:26 -0400393
Robert Phillipsa29a9562016-10-20 09:40:55 -0400394 SkMaskFilter* mf = paint.getMaskFilter();
Robert Phillipsd9d84852017-06-09 10:48:29 -0400395 if (mf && mf->asFragmentProcessor(nullptr)) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400396 mf = nullptr; // already handled in SkPaintToGrPaint
397 }
398
bsalomon6663acf2016-05-10 09:14:17 -0700399 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400400 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000401 // try to hit the fast path for drawing filtered round rects
402
403 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500404 if (rrect.transform(this->ctm(), &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000405 if (devRRect.allCornersCircular()) {
406 SkRect maskRect;
Mike Reeda1361362017-03-07 09:37:29 -0500407 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(),
408 this->ctm(), &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000409 SkIRect finalIRect;
410 maskRect.roundOut(&finalIRect);
Mike Reeda1361362017-03-07 09:37:29 -0500411
412 // we used to test finalIRect for quickReject, but that seems unlikely
413 // given that the original shape was not rejected...
414
Robert Phillips26c90e02017-03-14 14:39:29 -0400415 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400416 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500417 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000418 return;
419 }
420 }
421
422 }
423 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000424 }
425
Robert Phillipsa29a9562016-10-20 09:40:55 -0400426 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800427 // The only mask filter the native rrect drawing code could've handle was taken
428 // care of above.
429 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700431 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700432 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500433 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400434 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500435 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436 return;
437 }
Mike Klein744fb732014-06-23 15:13:26 -0400438
bsalomon6663acf2016-05-10 09:14:17 -0700439 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800440
Brian Salomon0166cfd2017-03-13 17:58:25 -0400441 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint),
442 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443}
444
robertphillipsd7706102016-02-25 09:28:08 -0800445
Mike Reeda1361362017-03-07 09:37:29 -0500446void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800447 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800448 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500449 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800450 if (outer.isEmpty()) {
451 return;
452 }
453
454 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500455 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800456 }
457
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000458 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000459
robertphillips0e7029e2015-11-30 05:45:06 -0800460 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800461 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500462 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700463 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700464 return;
465 }
robertphillips00095892016-02-29 13:50:40 -0800466
Brian Salomon0166cfd2017-03-13 17:58:25 -0400467 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
468 GrBoolToAA(paint.isAntiAlias()), this->ctm(), outer,
469 inner);
robertphillips00095892016-02-29 13:50:40 -0800470 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000471 }
472
473 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700474 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000475 path.addRRect(outer);
476 path.addRRect(inner);
477 path.setFillType(SkPath::kEvenOdd_FillType);
478
Brian Salomon0166cfd2017-03-13 17:58:25 -0400479 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
480 path, paint, this->ctm(), nullptr, this->devClipBounds(),
481 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000482}
483
484
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000485/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000486
Mike Reeda1361362017-03-07 09:37:29 -0500487void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700488 if (paint.getMaskFilter()) {
489 SkPath path;
490 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500491 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700492 }
493
494 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500495 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400496 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700497 return;
498 }
499
Brian Salomon0166cfd2017-03-13 17:58:25 -0400500 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint),
501 GrBoolToAA(paint.isAntiAlias()), this->ctm(), region,
502 GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700503}
504
Mike Reeda1361362017-03-07 09:37:29 -0500505void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800506 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500507 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
robertphillips514450c2015-11-24 05:36:02 -0800508 // Presumably the path effect warps this to something other than an oval
509 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700511 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000512 path.addOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500513 this->drawPath(path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514 return;
herb11a7f7f2015-11-24 12:41:00 -0800515 }
516
robertphillips514450c2015-11-24 05:36:02 -0800517 if (paint.getMaskFilter()) {
518 // The RRect path can handle special case blurring
519 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500520 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000521 }
522
523 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500524 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400525 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700526 return;
527 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000528
Brian Salomon0166cfd2017-03-13 17:58:25 -0400529 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint),
530 GrBoolToAA(paint.isAntiAlias()), this->ctm(), oval,
531 GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000532}
533
Mike Reeda1361362017-03-07 09:37:29 -0500534void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700535 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
536 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500537 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700538 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500539 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700540 return;
541 }
542 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500543 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400544 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700545 return;
546 }
547
Brian Salomon0166cfd2017-03-13 17:58:25 -0400548 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500549 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500550 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700551}
552
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554
555///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700556void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700557 const SkPaint& origPaint) {
558 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500559 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400560 // Adding support for round capping would require a
561 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700562 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
563 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
564 SkASSERT(!origPaint.getPathEffect());
565 SkASSERT(!origPaint.getMaskFilter());
566
567 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
568 SkASSERT(halfWidth > 0);
569
570 SkVector v = points[1] - points[0];
571
572 SkScalar length = SkPoint::Normalize(&v);
573 if (!length) {
574 v.fX = 1.0f;
575 v.fY = 0.0f;
576 }
577
578 SkPaint newPaint(origPaint);
579 newPaint.setStyle(SkPaint::kFill_Style);
580
581 SkScalar xtraLength = 0.0f;
582 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
583 xtraLength = halfWidth;
584 }
585
586 SkPoint mid = points[0] + points[1];
587 mid.scale(0.5f);
588
589 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
590 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
591 SkMatrix m;
592 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
593
594 SkMatrix local = m;
595
Mike Reeda1361362017-03-07 09:37:29 -0500596 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700597
598 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400599 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700600 return;
601 }
602
Brian Salomon82f44312017-01-11 13:42:54 -0500603 fRenderTargetContext->fillRectWithLocalMatrix(
Brian Salomon0166cfd2017-03-13 17:58:25 -0400604 this->clip(), std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700605}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606
Mike Reeda1361362017-03-07 09:37:29 -0500607void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608 const SkPaint& paint, const SkMatrix* prePathMatrix,
609 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800610 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800611 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700612 SkPoint points[2];
613 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
614 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500615 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700616 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500617 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700618 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500619 // Round capping support is currently disabled b.c. it would require a RRect
620 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500621 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700622 return;
623 }
624 }
robertphillipsff55b492015-11-24 07:56:59 -0800625 bool isClosed;
626 SkRect rect;
627 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
Mike Reeda1361362017-03-07 09:37:29 -0500628 this->drawRect(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800629 return;
630 }
631 if (origSrcPath.isOval(&rect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500632 this->drawOval(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800633 return;
634 }
635 SkRRect rrect;
636 if (origSrcPath.isRRect(&rrect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500637 this->drawRRect(rrect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800638 return;
639 }
640 }
641
Hal Canary144caf52016-11-07 17:57:18 -0500642 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643
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
reed85d91782015-09-10 14:33:38 -0700705bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
706 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700707 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500708 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700709 const SkRect* srcRectPtr,
710 int maxTileSize,
711 int* tileSize,
712 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800713 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700714 // if it's larger than the max tile size, then we have no choice but tiling.
715 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400716 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400717 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
718 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700719 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
720 return true;
721 }
722
bsalomon1a1d0b82015-10-16 07:49:42 -0700723 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700724 const size_t area = imageRect.width() * imageRect.height();
725 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
726 return false;
727 }
728
reed85d91782015-09-10 14:33:38 -0700729 // At this point we know we could do the draw by uploading the entire bitmap
730 // as a texture. However, if the texture would be large compared to the
731 // cache size and we don't require most of it for this draw then tile to
732 // reduce the amount of upload and cache spill.
733
734 // assumption here is that sw bitmap size is a good proxy for its size as
735 // a texture
736 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
737 size_t cacheSize;
738 fContext->getResourceCacheLimits(nullptr, &cacheSize);
739 if (bmpSize < cacheSize / 2) {
740 return false;
741 }
742
bsalomon1a1d0b82015-10-16 07:49:42 -0700743 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
744 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400745 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
746 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400747 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700748 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
749 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500750 kBmpSmallTileSize * kBmpSmallTileSize *
751 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700752
Brian Osmand05cdc32017-02-06 13:24:47 -0500753 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700754}
755
reed85d91782015-09-10 14:33:38 -0700756bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
757 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700758 const SkMatrix& viewMatrix,
759 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800760 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400761 // If image is explicitly texture backed then we shouldn't get here.
762 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763
Brian Salomon514baff2016-11-17 15:17:07 -0500764 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700765 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500766 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700767 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700768
769 int tileFilterPad;
770 if (doBicubic) {
771 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500772 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700773 tileFilterPad = 0;
774 } else {
775 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 }
reed85d91782015-09-10 14:33:38 -0700777 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778
bsalomon8c07b7a2015-11-02 11:36:52 -0800779 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
reed85d91782015-09-10 14:33:38 -0700781 // these are output, which we safely ignore, as we just want to know the predicate
782 int outTileSize;
783 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784
bsalomone553b642016-08-17 09:02:09 -0700785 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
786 params, srcRectPtr, maxTileSize, &outTileSize,
787 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788}
789
Mike Reeda1361362017-03-07 09:37:29 -0500790void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400791 SkScalar x,
792 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400794 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800795 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800796 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500797 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700798
bsalomonb1b01992015-11-18 10:56:08 -0800799 int maxTileSize = fContext->caps()->maxTileSize();
800
801 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
802 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400803 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
804 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800805 bitmap.height() <= maxTileSize;
806
807 bool skipTileCheck = drawAA || paint.getMaskFilter();
808
809 if (!skipTileCheck) {
810 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
811 int tileSize;
812 SkIRect clippedSrcRect;
813
Brian Salomon514baff2016-11-17 15:17:07 -0500814 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800815 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500816 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800817 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
818 &doBicubic);
819
820 int tileFilterPad;
821
822 if (doBicubic) {
823 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500824 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800825 tileFilterPad = 0;
826 } else {
827 tileFilterPad = 1;
828 }
829 params.setFilterMode(textureFilterMode);
830
831 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700832 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
833 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
834 &tileSize, &clippedSrcRect)) {
835 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
836 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
837 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800838 return;
839 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000840 }
Hal Canary144caf52016-11-07 17:57:18 -0500841 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800842 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -0400843 viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844}
845
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000846// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
848// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000849static inline void clamped_outset_with_offset(SkIRect* iRect,
850 int outset,
851 SkPoint* offset,
852 const SkIRect& clamp) {
853 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000855 int leftClampDelta = clamp.fLeft - iRect->fLeft;
856 if (leftClampDelta > 0) {
857 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000858 iRect->fLeft = clamp.fLeft;
859 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000860 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000862
863 int topClampDelta = clamp.fTop - iRect->fTop;
864 if (topClampDelta > 0) {
865 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000866 iRect->fTop = clamp.fTop;
867 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000868 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 }
870
871 if (iRect->fRight > clamp.fRight) {
872 iRect->fRight = clamp.fRight;
873 }
874 if (iRect->fBottom > clamp.fBottom) {
875 iRect->fBottom = clamp.fBottom;
876 }
877}
878
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000879// Break 'bitmap' into several tiles to draw it since it has already
880// been determined to be too large to fit in VRAM
881void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800882 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700883 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884 const SkRect& srcRect,
885 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500886 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800887 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700888 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000889 int tileSize,
890 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800891 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800892
ericrk983294f2016-04-18 09:14:00 -0700893 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800894 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700895 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800896
bsalomonc55271f2015-11-09 11:55:57 -0800897 const SkPaint* paint = &origPaint;
898 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400899 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800900 // Drop antialiasing to avoid seams at tile boundaries.
901 tempPaint = origPaint;
902 tempPaint.setAntiAlias(false);
903 paint = &tempPaint;
904 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
906
907 int nx = bitmap.width() / tileSize;
908 int ny = bitmap.height() / tileSize;
909 for (int x = 0; x <= nx; x++) {
910 for (int y = 0; y <= ny; y++) {
911 SkRect tileR;
912 tileR.set(SkIntToScalar(x * tileSize),
913 SkIntToScalar(y * tileSize),
914 SkIntToScalar((x + 1) * tileSize),
915 SkIntToScalar((y + 1) * tileSize));
916
917 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
918 continue;
919 }
920
921 if (!tileR.intersect(srcRect)) {
922 continue;
923 }
924
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 SkIRect iTileR;
926 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700927 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
928 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500929 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700930 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500931 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000932 SkIRect iClampRect;
933
reeda5517e22015-07-14 10:54:12 -0700934 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000935 // In bleed mode we want to always expand the tile on all edges
936 // but stay within the bitmap bounds
937 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
938 } else {
939 // In texture-domain/clamp mode we only want to expand the
940 // tile on edges interior to "srcRect" (i.e., we want to
941 // not bleed across the original clamped edges)
942 srcRect.roundOut(&iClampRect);
943 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000944 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
945 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 }
947
bsalomone553b642016-08-17 09:02:09 -0700948 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 if (bitmap.extractSubset(&tmpB, iTileR)) {
950 // now offset it to make it "local" to our tmp bitmap
951 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800952 // de-optimized this determination
953 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700954 this->drawBitmapTile(tmpB,
955 viewMatrix,
956 rectToDraw,
957 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500958 params,
bsalomone553b642016-08-17 09:02:09 -0700959 *paint,
960 constraint,
961 bicubic,
962 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963 }
964 }
965 }
966}
967
bsalomone553b642016-08-17 09:02:09 -0700968void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
969 const SkMatrix& viewMatrix,
970 const SkRect& dstRect,
971 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500972 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -0700973 const SkPaint& paint,
974 SkCanvas::SrcRectConstraint constraint,
975 bool bicubic,
976 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800977 // We should have already handled bitmaps larger than the max texture size.
978 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
979 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700980 // We should be respecting the max tile size by the time we get here.
981 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
982 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983
Robert Phillipse14d3052017-02-15 13:18:21 -0500984 SkASSERT(SkShader::kClamp_TileMode == params.getTileModeX() &&
985 SkShader::kClamp_TileMode == params.getTileModeY());
986
Robert Phillips78075802017-03-23 11:11:59 -0400987 sk_sp<GrTextureProxy> proxy = GrRefCachedBitmapTextureProxy(fContext.get(), bitmap,
988 params, nullptr);
989 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 return;
991 }
brianosman500bb3e2016-07-22 10:33:07 -0700992 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -0400993 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -0700994
bsalomone553b642016-08-17 09:02:09 -0700995 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillipse14d3052017-02-15 13:18:21 -0500996 const SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect,
997 SkMatrix::kFill_ScaleToFit);
joshualitt5f10b5c2015-07-09 10:24:35 -0700998
999 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1000 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -04001001 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001002
reeda5517e22015-07-14 10:54:12 -07001003 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001004 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001005 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001006 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001007 domain.fLeft = srcRect.fLeft + 0.5f;
1008 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001010 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011 }
1012 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001013 domain.fTop = srcRect.fTop + 0.5f;
1014 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001016 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001018 if (bicubic) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001019 fp = GrBicubicEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001020 std::move(colorSpaceXform), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001021 } else {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001022 fp = GrTextureDomainEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001023 std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001024 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001025 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001026 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001027 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001028 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001029 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001030 fp = GrBicubicEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001031 std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032 } else {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001033 fp = GrSimpleTextureEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001034 std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 }
1036
joshualitt33a5fce2015-11-18 13:28:51 -08001037 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001038 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001039 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1040 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001041 return;
1042 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001043
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001044 // Coverage-based AA would cause seams between tiles.
1045 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001046 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001047 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048}
1049
Mike Reeda1361362017-03-07 09:37:29 -05001050void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001052 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001053 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054
robertphillips970587b2016-07-14 14:12:55 -07001055 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 return;
1057 }
1058
Robert Phillipse14d3052017-02-15 13:18:21 -05001059 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1060 if (!srcImg) {
1061 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001062 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063
Florin Malita53f77bd2017-04-28 13:48:37 -04001064 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001065}
1066
1067
Florin Malita53f77bd2017-04-28 13:48:37 -04001068void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
1069 SkImage* clipImage,const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001070 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001071 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001072
Florin Malita53f77bd2017-04-28 13:48:37 -04001073 // TODO: clipImage support.
1074
robertphillips970587b2016-07-14 14:12:55 -07001075 SkIPoint offset = { 0, 0 };
1076
1077 sk_sp<SkSpecialImage> result;
1078 if (paint.getImageFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -05001079 result = this->filterTexture(special1, left, top,
robertphillips970587b2016-07-14 14:12:55 -07001080 &offset,
1081 paint.getImageFilter());
1082 if (!result) {
1083 return;
1084 }
1085 } else {
1086 result = sk_ref_sp(special1);
1087 }
1088
1089 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001090 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001091 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001092 return;
1093 }
robertphillips970587b2016-07-14 14:12:55 -07001094
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001095 const GrPixelConfig config = proxy->config();
1096
robertphillips970587b2016-07-14 14:12:55 -07001097 SkPaint tmpUnfiltered(paint);
1098 tmpUnfiltered.setImageFilter(nullptr);
1099
brianosman77320db2016-09-07 08:09:10 -07001100 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001101 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001102
Brian Salomonaff329b2017-08-11 09:40:37 -04001103 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), std::move(colorSpaceXform),
1104 SkMatrix::I());
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 {
bungeman06ca8ec2016-06-09 08:01:03 -07001108 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001109 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001110
1111 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001112 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001113 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),
1122 GrBoolToAA(paint.isAntiAlias()),
1123 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 Salomon514baff2016-11-17 15:17:07 -05001175 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001176 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001177 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001178 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -08001179 &doBicubic);
1180
1181 int tileFilterPad;
1182
1183 if (doBicubic) {
1184 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001185 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001186 tileFilterPad = 0;
1187 } else {
1188 tileFilterPad = 1;
1189 }
1190 params.setFilterMode(textureFilterMode);
1191
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(),
bsalomone553b642016-08-17 09:02:09 -07001194 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1195 &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001196 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
bsalomone553b642016-08-17 09:02:09 -07001197 params, 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 Phillips26c90e02017-03-14 14:39:29 -04001208 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->resourceProvider(), bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001209 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001210 return nullptr;
1211 }
1212
Robert Phillipse14d3052017-02-15 13:18:21 -05001213 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001214
Robert Phillipse14d3052017-02-15 13:18:21 -05001215 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1216 // the special image
1217 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1218 rect,
1219 bitmap.getGenerationID(),
1220 std::move(proxy),
1221 bitmap.refColorSpace(),
1222 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001223}
1224
reede51c3562016-07-19 14:33:20 -07001225sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001226 SkPixmap pm;
1227 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001228 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001229
Robert Phillips6de99042017-01-31 11:31:39 -05001230 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1231 SkIRect::MakeWH(image->width(), image->height()),
1232 image->uniqueID(),
1233 std::move(proxy),
1234 as_IB(image)->onImageInfo().refColorSpace(),
1235 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001236 } else if (image->peekPixels(&pm)) {
1237 SkBitmap bm;
1238
1239 bm.installPixels(pm);
1240 return this->makeSpecial(bm);
1241 } else {
1242 return nullptr;
1243 }
1244}
1245
1246sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001247 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1248 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001249 // When the device doesn't have a texture, we create a temporary texture.
1250 // TODO: we should actually only copy the portion of the source needed to apply the image
1251 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001252 proxy = GrSurfaceProxy::Copy(fContext.get(),
1253 this->accessRenderTargetContext()->asSurfaceProxy(),
1254 SkBudgeted::kYes);
1255 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001256 return nullptr;
1257 }
robertphillips1b5f9682016-07-15 08:01:12 -07001258 }
1259
1260 const SkImageInfo ii = this->imageInfo();
1261 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1262
Robert Phillipse2f7d182016-12-15 09:23:05 -05001263 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1264 srcRect,
1265 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001266 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001267 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001268 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001269}
1270
Mike Reeda1361362017-03-07 09:37:29 -05001271void SkGpuDevice::drawDevice(SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001272 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001273 SkASSERT(!paint.getImageFilter());
1274
joshualittce894002016-01-11 13:29:31 -08001275 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001277 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001278
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001280 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001281 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001282 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283 return;
1284 }
1285
Florin Malita53f77bd2017-04-28 13:48:37 -04001286 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001287}
1288
Brian Salomon34169692017-08-28 15:32:01 -04001289void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001290 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001291 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001292 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001293 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001294 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001295 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1296 image->alphaType(), nullptr, nullptr,
1297 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001298 return;
Brian Salomon34169692017-08-28 15:32:01 -04001299 }
1300 SkBitmap bm;
1301 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1302 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1303 // only support tiling as bitmap at the moment, so force raster-version
1304 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
1305 return;
reed85d91782015-09-10 14:33:38 -07001306 }
Brian Salomon34169692017-08-28 15:32:01 -04001307 this->drawBitmap(bm, x, y, paint);
1308 return;
1309 }
1310 if (image->isLazyGenerated()) {
1311 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1312 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1313 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1314 return;
1315 }
1316 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
1317 GrBitmapTextureMaker maker(fContext.get(), bm);
1318 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1319 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001320 }
1321}
1322
Brian Salomon34169692017-08-28 15:32:01 -04001323void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1324 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001325 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001326 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001327 if (!src || src->contains(image->bounds())) {
1328 constraint = SkCanvas::kFast_SrcRectConstraint;
1329 }
Robert Phillips3798c862017-03-27 11:08:16 -04001330 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001331 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1332 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001333 return;
1334 }
1335 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001336 SkMatrix srcToDstRect;
1337 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1338 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001339 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001340 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001341 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001342 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001343 return;
1344 }
Mike Reeda1361362017-03-07 09:37:29 -05001345 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001346 return;
1347 }
1348 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001349 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001350 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1351 this->ctm(), paint);
1352 return;
1353 }
1354 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001355 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001356 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1357 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001358 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001359}
1360
Mike Reeda1361362017-03-07 09:37:29 -05001361void SkGpuDevice::drawProducerNine(GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001362 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001363 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001364
joshualittedb36442015-11-19 14:29:30 -08001365 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Salomon7c8460e2017-05-12 11:36:10 -04001366 GrFSAAType::kUnifiedMSAA == fRenderTargetContext->fsaaType();
joshualitt33a5fce2015-11-18 13:28:51 -08001367 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001368 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001369 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), SkMatrix::I(),
bsalomon2bbd0c62015-12-09 12:50:56 -08001370 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001371 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001372 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001373
1374 SkRect srcR, dstR;
1375 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001376 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -04001377 this->ctm(), paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001378 }
1379 return;
1380 }
1381
Brian Salomon514baff2016-11-17 15:17:07 -05001382 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
Brian Salomonaff329b2017-08-11 09:40:37 -04001383 auto fp = producer->createFragmentProcessor(
1384 SkMatrix::I(), SkRect::MakeIWH(producer->width(), producer->height()),
1385 GrTextureProducer::kNo_FilterConstraint, true, &kMode,
1386 fRenderTargetContext->getColorSpace());
Robert Phillips1c9686b2017-06-30 08:40:28 -04001387 if (!fp) {
1388 return;
1389 }
joshualitt33a5fce2015-11-18 13:28:51 -08001390 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001391 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001392 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001393 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001394 return;
1395 }
1396
msarett10e3d9b2016-08-18 15:46:03 -07001397 std::unique_ptr<SkLatticeIter> iter(
1398 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001399 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001400 producer->width(), producer->height(), std::move(iter),
1401 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001402}
1403
Mike Reeda1361362017-03-07 09:37:29 -05001404void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001405 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001406 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001407 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001408 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001409 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1410 image->alphaType(), image->bounds(),
Robert Phillips3798c862017-03-27 11:08:16 -04001411 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001412 this->drawProducerNine(&adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001413 } else {
1414 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001415 if (image->isLazyGenerated()) {
1416 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001417 this->drawProducerNine(&maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001418 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001419 this->drawBitmapNine(bm, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001420 }
1421 }
1422}
1423
Mike Reeda1361362017-03-07 09:37:29 -05001424void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001425 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001426 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001427 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001428 this->drawProducerNine(&maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001429}
1430
Mike Reeda1361362017-03-07 09:37:29 -05001431void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
msarett10e3d9b2016-08-18 15:46:03 -07001432 const SkCanvas::Lattice& lattice, const SkRect& dst,
1433 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001434 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001435
Brian Salomon514baff2016-11-17 15:17:07 -05001436 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
Brian Salomonaff329b2017-08-11 09:40:37 -04001437 std::unique_ptr<GrFragmentProcessor> fp(producer->createFragmentProcessor(
1438 SkMatrix::I(), SkRect::MakeIWH(producer->width(), producer->height()),
1439 GrTextureProducer::kNo_FilterConstraint, true, &kMode,
1440 fRenderTargetContext->getColorSpace()));
Robert Phillips1c9686b2017-06-30 08:40:28 -04001441 if (!fp) {
1442 return;
1443 }
msarett10e3d9b2016-08-18 15:46:03 -07001444 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001445 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001446 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001447 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001448 return;
1449 }
1450
1451 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001452 new SkLatticeIter(lattice, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001453 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001454 producer->width(), producer->height(), std::move(iter),
1455 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001456}
1457
Mike Reeda1361362017-03-07 09:37:29 -05001458void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001459 const SkCanvas::Lattice& lattice, const SkRect& dst,
1460 const SkPaint& paint) {
1461 ASSERT_SINGLE_OWNER
1462 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001463 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001464 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1465 image->alphaType(), image->bounds(),
Robert Phillips3798c862017-03-27 11:08:16 -04001466 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001467 this->drawProducerLattice(&adjuster, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001468 } else {
1469 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001470 if (image->isLazyGenerated()) {
1471 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001472 this->drawProducerLattice(&maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001473 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001474 this->drawBitmapLattice(bm, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001475 }
1476 }
1477}
1478
Mike Reeda1361362017-03-07 09:37:29 -05001479void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001480 const SkCanvas::Lattice& lattice, const SkRect& dst,
1481 const SkPaint& paint) {
1482 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001483 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001484 this->drawProducerLattice(&maker, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001485}
1486
Robert Phillips26c90e02017-03-14 14:39:29 -04001487static bool init_vertices_paint(GrContext* context, GrRenderTargetContext* rtc,
1488 const SkPaint& skPaint,
1489 const SkMatrix& matrix, SkBlendMode bmode,
1490 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001491 if (hasTexs && skPaint.getShader()) {
1492 if (hasColors) {
1493 // When there are texs and colors the shader and colors are combined using bmode.
Mike Reed185ba212017-04-28 12:31:05 -04001494 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, matrix, bmode, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001495 } else {
1496 // We have a shader, but no colors to blend it against.
1497 return SkPaintToGrPaint(context, rtc, skPaint, matrix, grPaint);
1498 }
1499 } else {
1500 if (hasColors) {
1501 // We have colors, but either have no shader or no texture coords (which implies that
1502 // we should ignore the shader).
1503 return SkPaintToGrPaintWithPrimitiveColor(context, rtc, skPaint, grPaint);
1504 } else {
1505 // No colors and no shaders. Just draw with the paint color.
Brian Osman33fa4542017-05-25 15:10:38 -04001506 return SkPaintToGrPaintNoShader(context, rtc, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001507 }
1508 }
1509}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001510
Mike Reed887cdf12017-04-03 11:11:09 -04001511void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001512 const SkPoint vertices[], SkBlendMode bmode,
1513 const uint16_t indices[], int indexCount,
1514 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001515 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001516 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001517
Mike Reed2f6b5a42017-03-19 15:04:17 -04001518 SkPaint copy(paint);
1519 copy.setStyle(SkPaint::kStroke_Style);
1520 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001521
Mike Reed2f6b5a42017-03-19 15:04:17 -04001522 GrPaint grPaint;
1523 // we ignore the shader since we have no texture coordinates.
1524 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001525 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001526 }
1527
Mike Reed2f6b5a42017-03-19 15:04:17 -04001528 int triangleCount = 0;
1529 int n = (nullptr == indices) ? vertexCount : indexCount;
1530 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001531 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001532 triangleCount = n / 3;
1533 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001534 case SkVertices::kTriangleStrip_VertexMode:
1535 case SkVertices::kTriangleFan_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001536 triangleCount = n - 2;
1537 break;
1538 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539
Mike Reed2f6b5a42017-03-19 15:04:17 -04001540 VertState state(vertexCount, indices, indexCount);
1541 VertState::Proc vertProc = state.chooseProc(vmode);
1542
1543 //number of indices for lines per triangle with kLines
1544 indexCount = triangleCount * 6;
1545
Brian Osmanae0c50c2017-05-25 16:56:34 -04001546 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1547 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1548 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1549
1550 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001551 int i = 0;
1552 while (vertProc(&state)) {
1553 lineIndices[i] = state.f0;
1554 lineIndices[i + 1] = state.f1;
1555 lineIndices[i + 2] = state.f1;
1556 lineIndices[i + 3] = state.f2;
1557 lineIndices[i + 4] = state.f2;
1558 lineIndices[i + 5] = state.f0;
1559 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001560 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001561
Chris Dalton3809bab2017-06-13 10:55:06 -06001562 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001563 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001564 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001565 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001566 builder.detach(),
1567 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001568}
1569
Brian Osman0b403f82017-05-26 10:39:51 -04001570void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001571 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001572 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001573
1574 SkASSERT(vertices);
1575 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001576 bool hasColors = vertices->hasColors();
1577 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001578 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001579 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001580 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1581 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001582 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001583 }
Robert Phillips26c90e02017-03-14 14:39:29 -04001584 if (!init_vertices_paint(fContext.get(), fRenderTargetContext.get(), paint, this->ctm(),
1585 mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001586 return;
1587 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001588 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001589 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001590}
1591
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592///////////////////////////////////////////////////////////////////////////////
1593
Jim Van Verth3af1af92017-05-18 15:06:54 -04001594void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1595
1596 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001597 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1598
Brian Salomon05969092017-07-13 11:20:51 -04001599 GrColor color = SkColorToPremulGrColor(rec.fColor);
1600 if (!fRenderTargetContext->drawFastShadow(this->clip(), color, this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001601 // failed to find an accelerated case
1602 this->INHERITED::drawShadow(path, rec);
1603 }
1604}
1605
1606///////////////////////////////////////////////////////////////////////////////
1607
Mike Reeda1361362017-03-07 09:37:29 -05001608void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001609 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001610 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001611 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001612 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001613 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001614 return;
1615 }
1616
Hal Canary144caf52016-11-07 17:57:18 -05001617 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001618
reedca109532015-06-25 16:25:25 -07001619 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001620 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001621
jvanverth31ff7622015-08-07 10:09:28 -07001622 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001623 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001624 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed185ba212017-04-28 12:31:05 -04001625 this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001626 return;
1627 }
1628 } else {
Mike Reeda1361362017-03-07 09:37:29 -05001629 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -04001630 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001631 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001632 }
1633 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001634
1635 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001636 fRenderTargetContext->drawAtlas(
1637 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001638}
1639
1640///////////////////////////////////////////////////////////////////////////////
1641
Mike Reeda1361362017-03-07 09:37:29 -05001642void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001643 size_t byteLength, SkScalar x, SkScalar y,
1644 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001645 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001646 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001647 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001648
Brian Salomon0166cfd2017-03-13 17:58:25 -04001649 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1650 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651}
1652
Mike Reeda1361362017-03-07 09:37:29 -05001653void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001654 const SkScalar pos[], int scalarsPerPos,
1655 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001656 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001657 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001658 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001659
Brian Salomon0166cfd2017-03-13 17:58:25 -04001660 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1661 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001662 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663}
1664
Mike Reeda1361362017-03-07 09:37:29 -05001665void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001666 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001667 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001668 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001669 SkDEBUGCODE(this->validate();)
1670
Brian Salomon0166cfd2017-03-13 17:58:25 -04001671 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1672 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001673}
1674
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675///////////////////////////////////////////////////////////////////////////////
1676
reedb2db8982014-11-13 12:41:02 -08001677bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001678 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679}
1680
Greg Daniela5cb7812017-06-16 09:45:32 -04001681///////////////////////////////////////////////////////////////////////////////
1682
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001684 this->flushAndSignalSemaphores(0, nullptr);
1685}
1686
Greg Daniel51316782017-08-02 15:10:09 +00001687GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1688 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001689 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001690
Greg Danielc64ee462017-06-15 16:59:49 -04001691 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001692}
1693
Greg Danielc64ee462017-06-15 16:59:49 -04001694bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001695 ASSERT_SINGLE_OWNER
1696
Greg Danielc64ee462017-06-15 16:59:49 -04001697 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698}
1699
1700///////////////////////////////////////////////////////////////////////////////
1701
reed76033be2015-03-14 10:54:31 -07001702SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001703 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001704
robertphillipsca6eafc2016-05-17 09:57:46 -07001705 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706
robertphillipsca6eafc2016-05-17 09:57:46 -07001707 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001708 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001709 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1710 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001711
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001712 sk_sp<GrRenderTargetContext> rtc(fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -04001713 fit,
1714 cinfo.fInfo.width(), cinfo.fInfo.height(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001715 fRenderTargetContext->config(),
1716 fRenderTargetContext->refColorSpace(),
Brian Salomon50e66d42017-05-15 16:28:07 -04001717 fRenderTargetContext->numStencilSamples(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001718 kBottomLeft_GrSurfaceOrigin,
Brian Osman11052242016-10-27 14:47:55 -04001719 &props));
1720 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001721 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001722 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001723
1724 // Skia's convention is to only clear a device if it is non-opaque.
1725 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1726
Robert Phillips9fab7e92016-11-17 12:45:04 -05001727 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1728 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001729}
1730
reede8f30622016-03-23 18:59:25 -07001731sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001732 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001733 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001734 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001735 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001736 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001737 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001738}
1739
senorblanco900c3672016-04-27 11:31:23 -07001740SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001741 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001742 // We always return a transient cache, so it is freed after each
1743 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001744 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001745}
reedf037e0b2014-10-30 11:34:15 -07001746
1747#endif