blob: 5a98b0822090ed36cc8c5f077e85414f7469aef8 [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"
9
Brian Osman3b66ab62016-11-28 09:26:31 -050010#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070011#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080012#include "GrContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050014#include "GrImageTextureMaker.h"
15#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070016#include "GrStyle.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"
bsalomon1cf6f9b2015-12-08 10:53:43 -080024#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070026#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050027#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050028#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070029#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000030#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000032#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070033#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkRRect.h"
Brian Salomon6a639042016-12-14 11:08:17 -050035#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050036#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080037#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070038#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000040#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000042#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000044#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050045#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050046#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080047#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080048#include "effects/GrSimpleTextureEffect.h"
49#include "effects/GrTextureDomain.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#if 0
58 extern bool (*gShouldDrawProc)();
Mike Reeda1361362017-03-07 09:37:29 -050059 #define CHECK_SHOULD_DRAW() \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060 do { \
61 if (gShouldDrawProc && !gShouldDrawProc()) return; \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062 } while (0)
63#else
Brian Salomon0166cfd2017-03-13 17:58:25 -040064#define CHECK_SHOULD_DRAW()
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000065#endif
66
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067///////////////////////////////////////////////////////////////////////////////
68
bsalomon74f681d2015-06-23 14:38:48 -070069/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
70 should fail. */
71bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
72 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
73 *flags = 0;
74 if (info) {
75 switch (info->alphaType()) {
76 case kPremul_SkAlphaType:
77 break;
78 case kOpaque_SkAlphaType:
79 *flags |= SkGpuDevice::kIsOpaque_Flag;
80 break;
81 default: // If it is unpremul or unknown don't try to render
82 return false;
83 }
84 }
85 if (kClear_InitContents == init) {
86 *flags |= kNeedClear_Flag;
87 }
88 return true;
89}
90
Robert Phillips9fab7e92016-11-17 12:45:04 -050091sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
92 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070093 int width, int height,
94 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040095 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070096 return nullptr;
97 }
98 unsigned flags;
99 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
100 return nullptr;
101 }
Robert Phillips9fab7e92016-11-17 12:45:04 -0500102 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
103 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000104}
105
robertphillips24e91282016-04-29 06:46:36 -0700106sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
107 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700108 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700109 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700110 unsigned flags;
111 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700113 }
114
Brian Osman11052242016-10-27 14:47:55 -0400115 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
116 info, sampleCount,
117 origin, props));
118 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700119 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700120 }
121
Robert Phillips9fab7e92016-11-17 12:45:04 -0500122 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700123 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700124}
125
Brian Osman11052242016-10-27 14:47:55 -0400126static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700127 SkColorType colorType;
128 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
129 colorType = kUnknown_SkColorType;
130 }
131 return SkImageInfo::Make(w, h, colorType,
132 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
Robert Phillips75a475c2017-01-13 09:18:59 -0500133 context->refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700134}
135
Robert Phillips9fab7e92016-11-17 12:45:04 -0500136SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
137 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400138 : INHERITED(make_info(renderTargetContext.get(), width, height,
139 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500140 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400141 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700142{
robertphillips1f3923e2016-07-21 07:17:54 -0700143 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700144 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145
bsalomone63ffef2016-02-05 07:17:34 -0800146 if (flags & kNeedClear_Flag) {
147 this->clearAll();
148 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149}
150
Brian Osman11052242016-10-27 14:47:55 -0400151sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
152 GrContext* context,
153 SkBudgeted budgeted,
154 const SkImageInfo& origInfo,
155 int sampleCount,
156 GrSurfaceOrigin origin,
157 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000158 if (kUnknown_SkColorType == origInfo.colorType() ||
159 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000161 }
162
bsalomonafe30052015-01-16 07:32:33 -0800163 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700164 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800165 }
166
Brian Osman0c2997b2017-01-11 16:58:42 -0500167 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Brian Osman11052242016-10-27 14:47:55 -0400168 return context->makeRenderTargetContext(SkBackingFit::kExact, // Why exact?
robertphillips6738c702016-07-27 12:13:51 -0700169 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500170 config, origInfo.refColorSpace(), sampleCount,
robertphillips6738c702016-07-27 12:13:51 -0700171 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800172}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000173
Mike Reeda1361362017-03-07 09:37:29 -0500174sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700175 int left, int top,
176 SkIPoint* offset,
177 const SkImageFilter* filter) {
178 SkASSERT(srcImg->isTextureBacked());
179 SkASSERT(filter);
180
Mike Reeda1361362017-03-07 09:37:29 -0500181 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700182 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500183 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500184 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400185 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700186 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700187
188 return filter->filterImage(srcImg, ctx, offset);
189}
190
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191///////////////////////////////////////////////////////////////////////////////
192
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000193bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
194 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800195 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000196
Matt Sarettcb6266b2017-01-17 10:48:53 -0500197 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
198 return false;
199 }
200
Matt Sarett03dd6d52017-01-23 12:15:09 -0500201 SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, x, y);
202 if (!rec.trim(this->width(), this->height())) {
203 return false;
204 }
205
206 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000207}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000208
robertphillips1da3ecd2016-08-31 14:54:15 -0700209bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
210 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800211 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700212
Matt Sarettcb6266b2017-01-17 10:48:53 -0500213 if (!SkImageInfoValidConversion(this->imageInfo(), srcInfo)) {
214 return false;
215 }
216
Matt Sarett03dd6d52017-01-23 12:15:09 -0500217 SkWritePixelsRec rec(srcInfo, srcPixels, srcRowBytes, x, y);
218 if (!rec.trim(this->width(), this->height())) {
219 return false;
220 }
221
222 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000223}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000224
reed41e010c2015-06-09 12:16:53 -0700225bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800226 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700227 return false;
228}
229
Brian Osman11052242016-10-27 14:47:55 -0400230GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700231 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400232 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700233}
234
reed8eddfb52014-12-04 07:50:14 -0800235void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800236 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500237 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500238
reed8eddfb52014-12-04 07:50:14 -0800239 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500240 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800241}
242
Brian Osman11052242016-10-27 14:47:55 -0400243void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800244 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800245
Brian Osman693a5402016-10-27 15:13:22 -0400246 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800247
Brian Osman693a5402016-10-27 15:13:22 -0400248 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400249 this->context(),
250 budgeted,
251 this->imageInfo(),
252 fRenderTargetContext->numColorSamples(),
253 fRenderTargetContext->origin(),
254 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400255 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800256 return;
257 }
258
259 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400260 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800261 return;
262 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500263 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800264 }
265
Brian Osman693a5402016-10-27 15:13:22 -0400266 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800267}
268
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269///////////////////////////////////////////////////////////////////////////////
270
Mike Reeda1361362017-03-07 09:37:29 -0500271void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800272 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -0500273 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -0500274 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275
276 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500277 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400278 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700279 return;
280 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281
Brian Salomon0166cfd2017-03-13 17:58:25 -0400282 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283}
284
285// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700286static const GrPrimitiveType gPointMode2PrimitiveType[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287 kPoints_GrPrimitiveType,
288 kLines_GrPrimitiveType,
289 kLineStrip_GrPrimitiveType
290};
291
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500292static inline bool is_int(float x) { return x == (float) sk_float_round2int(x); }
293
ethannicholas330bb952015-07-17 06:44:02 -0700294// suppress antialiasing on axis-aligned integer-coordinate lines
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500295static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
296 const SkMatrix& matrix) {
ethannicholas330bb952015-07-17 06:44:02 -0700297 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
298 return false;
299 }
300 if (count == 2) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500301 // We do not antialias horizontal or vertical lines along pixel centers, even when the ends
302 // of the line do not fully cover the first and last pixel of the line, which is slightly
303 // wrong.
304 if (!matrix.isScaleTranslate()) {
305 return true;
306 }
ethannicholas330bb952015-07-17 06:44:02 -0700307 if (pts[0].fX == pts[1].fX) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500308 SkScalar x = matrix.getScaleX() * pts[0].fX + matrix.getTranslateX();
309 return !is_int(x + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700310 }
311 if (pts[0].fY == pts[1].fY) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500312 SkScalar y = matrix.getScaleY() * pts[0].fY + matrix.getTranslateY();
313 return !is_int(y + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700314 }
315 }
316 return true;
317}
318
Mike Reeda1361362017-03-07 09:37:29 -0500319void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800321 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500322 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500323 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324
325 SkScalar width = paint.getStrokeWidth();
326 if (width < 0) {
327 return;
328 }
329
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000330 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700331 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700332 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500333 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700334 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700335 return;
336 }
egdaniele61c4112014-06-12 10:24:21 -0700337 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700338 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700339 path.moveTo(pts[0]);
340 path.lineTo(pts[1]);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400341 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint),
342 GrBoolToAA(paint.isAntiAlias()), this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700343 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000344 }
345
bsalomon6ade6dd2016-09-12 12:07:17 -0700346 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500347 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700348 SkScalarNearlyEqual(scales[0], 1.f) &&
349 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700350 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700352 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
Mike Reeda1361362017-03-07 09:37:29 -0500353 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts, this->ctm())))
354 {
355 SkRasterClip rc(this->devClipBounds());
356 SkDraw draw;
357 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
358 draw.fMatrix = &this->ctm();
359 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500360 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361 return;
362 }
363
bsalomon6ade6dd2016-09-12 12:07:17 -0700364 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
365
Mike Reeda1361362017-03-07 09:37:29 -0500366 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700367#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
368 // This offsetting in device space matches the expectations of the Android framework for non-AA
369 // points and lines.
370 SkMatrix tempMatrix;
371 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
372 tempMatrix = *viewMatrix;
373 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
374 tempMatrix.postTranslate(kOffset, kOffset);
375 viewMatrix = &tempMatrix;
376 }
377#endif
378
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400380 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
381 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700382 return;
383 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384
Brian Salomon0166cfd2017-03-13 17:58:25 -0400385 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -0500386 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -0400387 *viewMatrix,
388 primitiveType,
389 SkToS32(count),
390 (SkPoint*)pts,
391 nullptr,
392 nullptr,
393 nullptr,
394 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395}
396
397///////////////////////////////////////////////////////////////////////////////
398
Mike Reeda1361362017-03-07 09:37:29 -0500399void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800400 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500401 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500402 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404
bsalomona7d85ba2016-07-06 11:54:59 -0700405 // A couple reasons we might need to call drawPath.
406 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700408 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500410 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400411 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500412 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000413 return;
414 }
415
416 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500417 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400418 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700419 return;
420 }
Mike Klein744fb732014-06-23 15:13:26 -0400421
bsalomon6663acf2016-05-10 09:14:17 -0700422 GrStyle style(paint);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400423 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint),
424 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425}
426
427///////////////////////////////////////////////////////////////////////////////
428
Mike Reeda1361362017-03-07 09:37:29 -0500429void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800430 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500431 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500432 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000433
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000434 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500435 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400436 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700437 return;
438 }
Mike Klein744fb732014-06-23 15:13:26 -0400439
Robert Phillipsa29a9562016-10-20 09:40:55 -0400440 SkMaskFilter* mf = paint.getMaskFilter();
Mike Reeda1361362017-03-07 09:37:29 -0500441 if (mf && mf->asFragmentProcessor(nullptr, nullptr, this->ctm())) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400442 mf = nullptr; // already handled in SkPaintToGrPaint
443 }
444
bsalomon6663acf2016-05-10 09:14:17 -0700445 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400446 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000447 // try to hit the fast path for drawing filtered round rects
448
449 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500450 if (rrect.transform(this->ctm(), &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000451 if (devRRect.allCornersCircular()) {
452 SkRect maskRect;
Mike Reeda1361362017-03-07 09:37:29 -0500453 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(),
454 this->ctm(), &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000455 SkIRect finalIRect;
456 maskRect.roundOut(&finalIRect);
Mike Reeda1361362017-03-07 09:37:29 -0500457
458 // we used to test finalIRect for quickReject, but that seems unlikely
459 // given that the original shape was not rejected...
460
Robert Phillips26c90e02017-03-14 14:39:29 -0400461 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400462 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500463 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000464 return;
465 }
466 }
467
468 }
469 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000470 }
471
Robert Phillipsa29a9562016-10-20 09:40:55 -0400472 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800473 // The only mask filter the native rrect drawing code could've handle was taken
474 // care of above.
475 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000476 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700477 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700478 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500479 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400480 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500481 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482 return;
483 }
Mike Klein744fb732014-06-23 15:13:26 -0400484
bsalomon6663acf2016-05-10 09:14:17 -0700485 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800486
Brian Salomon0166cfd2017-03-13 17:58:25 -0400487 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint),
488 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489}
490
robertphillipsd7706102016-02-25 09:28:08 -0800491
Mike Reeda1361362017-03-07 09:37:29 -0500492void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800493 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800494 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500495 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500496 CHECK_SHOULD_DRAW();
robertphillips0e7029e2015-11-30 05:45:06 -0800497
robertphillipsd7706102016-02-25 09:28:08 -0800498 if (outer.isEmpty()) {
499 return;
500 }
501
502 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500503 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800504 }
505
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000506 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000507
robertphillips0e7029e2015-11-30 05:45:06 -0800508 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800509 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500510 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700511 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700512 return;
513 }
robertphillips00095892016-02-29 13:50:40 -0800514
Brian Salomon0166cfd2017-03-13 17:58:25 -0400515 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
516 GrBoolToAA(paint.isAntiAlias()), this->ctm(), outer,
517 inner);
robertphillips00095892016-02-29 13:50:40 -0800518 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000519 }
520
521 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700522 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000523 path.addRRect(outer);
524 path.addRRect(inner);
525 path.setFillType(SkPath::kEvenOdd_FillType);
526
Brian Salomon0166cfd2017-03-13 17:58:25 -0400527 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
528 path, paint, this->ctm(), nullptr, this->devClipBounds(),
529 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000530}
531
532
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000533/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534
Mike Reeda1361362017-03-07 09:37:29 -0500535void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700536 if (paint.getMaskFilter()) {
537 SkPath path;
538 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500539 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700540 }
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)) {
msarettcc319b92016-08-25 18:07:18 -0700545 return;
546 }
547
Brian Salomon0166cfd2017-03-13 17:58:25 -0400548 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint),
549 GrBoolToAA(paint.isAntiAlias()), this->ctm(), region,
550 GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700551}
552
Mike Reeda1361362017-03-07 09:37:29 -0500553void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800554 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500555 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500556 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557
robertphillips514450c2015-11-24 05:36:02 -0800558 // Presumably the path effect warps this to something other than an oval
559 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000560 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700561 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562 path.addOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500563 this->drawPath(path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564 return;
herb11a7f7f2015-11-24 12:41:00 -0800565 }
566
robertphillips514450c2015-11-24 05:36:02 -0800567 if (paint.getMaskFilter()) {
568 // The RRect path can handle special case blurring
569 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500570 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000571 }
572
573 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500574 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400575 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700576 return;
577 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000578
Brian Salomon0166cfd2017-03-13 17:58:25 -0400579 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint),
580 GrBoolToAA(paint.isAntiAlias()), this->ctm(), oval,
581 GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582}
583
Mike Reeda1361362017-03-07 09:37:29 -0500584void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700585 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
586 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500587 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500588 CHECK_SHOULD_DRAW();
bsalomon4f3a0ca2016-08-22 13:14:26 -0700589
590 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500591 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700592 return;
593 }
594 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500595 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400596 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700597 return;
598 }
599
Brian Salomon0166cfd2017-03-13 17:58:25 -0400600 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500601 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500602 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700603}
604
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000605#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606
607///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700608void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700609 const SkPaint& origPaint) {
610 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500611 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500612 CHECK_SHOULD_DRAW();
robertphillips0851d2d2016-06-02 05:21:34 -0700613
Brian Osman11052242016-10-27 14:47:55 -0400614 // Adding support for round capping would require a
615 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700616 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
617 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
618 SkASSERT(!origPaint.getPathEffect());
619 SkASSERT(!origPaint.getMaskFilter());
620
621 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
622 SkASSERT(halfWidth > 0);
623
624 SkVector v = points[1] - points[0];
625
626 SkScalar length = SkPoint::Normalize(&v);
627 if (!length) {
628 v.fX = 1.0f;
629 v.fY = 0.0f;
630 }
631
632 SkPaint newPaint(origPaint);
633 newPaint.setStyle(SkPaint::kFill_Style);
634
635 SkScalar xtraLength = 0.0f;
636 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
637 xtraLength = halfWidth;
638 }
639
640 SkPoint mid = points[0] + points[1];
641 mid.scale(0.5f);
642
643 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
644 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
645 SkMatrix m;
646 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
647
648 SkMatrix local = m;
649
Mike Reeda1361362017-03-07 09:37:29 -0500650 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700651
652 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400653 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700654 return;
655 }
656
Brian Salomon82f44312017-01-11 13:42:54 -0500657 fRenderTargetContext->fillRectWithLocalMatrix(
Brian Salomon0166cfd2017-03-13 17:58:25 -0400658 this->clip(), std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700659}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660
Mike Reeda1361362017-03-07 09:37:29 -0500661void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 const SkPaint& paint, const SkMatrix* prePathMatrix,
663 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800664 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800665 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700666 SkPoint points[2];
667 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
668 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500669 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700670 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500671 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700672 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500673 // Round capping support is currently disabled b.c. it would require a RRect
674 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500675 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700676 return;
677 }
678 }
robertphillipsff55b492015-11-24 07:56:59 -0800679 bool isClosed;
680 SkRect rect;
681 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
Mike Reeda1361362017-03-07 09:37:29 -0500682 this->drawRect(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800683 return;
684 }
685 if (origSrcPath.isOval(&rect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500686 this->drawOval(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800687 return;
688 }
689 SkRRect rrect;
690 if (origSrcPath.isRRect(&rrect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500691 this->drawRRect(rrect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800692 return;
693 }
694 }
695
Mike Reeda1361362017-03-07 09:37:29 -0500696 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -0500697 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000698
Brian Salomon0166cfd2017-03-13 17:58:25 -0400699 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
700 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500701 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702}
703
704static const int kBmpSmallTileSize = 1 << 10;
705
706static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
707 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
708 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
709 return tilesX * tilesY;
710}
711
reed85d91782015-09-10 14:33:38 -0700712static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713 if (maxTileSize <= kBmpSmallTileSize) {
714 return maxTileSize;
715 }
716
717 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
718 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
719
720 maxTileTotalTileSize *= maxTileSize * maxTileSize;
721 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
722
723 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
724 return kBmpSmallTileSize;
725 } else {
726 return maxTileSize;
727 }
728}
729
730// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
731// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700732static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800733 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800734 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700735 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700736 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737 const SkRect* srcRectPtr,
738 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700739 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700740 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
741 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 clippedSrcIRect->setEmpty();
743 return;
744 }
745 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
746 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700747 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 if (!clippedSrcRect.intersect(*srcRectPtr)) {
749 clippedSrcIRect->setEmpty();
750 return;
751 }
752 }
753 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700754 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755 if (!clippedSrcIRect->intersect(bmpBounds)) {
756 clippedSrcIRect->setEmpty();
757 }
758}
759
reed85d91782015-09-10 14:33:38 -0700760bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
761 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700762 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500763 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700764 const SkRect* srcRectPtr,
765 int maxTileSize,
766 int* tileSize,
767 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800768 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700769 // if it's larger than the max tile size, then we have no choice but tiling.
770 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400771 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400772 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
773 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700774 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
775 return true;
776 }
777
bsalomon1a1d0b82015-10-16 07:49:42 -0700778 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700779 const size_t area = imageRect.width() * imageRect.height();
780 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
781 return false;
782 }
783
reed85d91782015-09-10 14:33:38 -0700784 // At this point we know we could do the draw by uploading the entire bitmap
785 // as a texture. However, if the texture would be large compared to the
786 // cache size and we don't require most of it for this draw then tile to
787 // reduce the amount of upload and cache spill.
788
789 // assumption here is that sw bitmap size is a good proxy for its size as
790 // a texture
791 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
792 size_t cacheSize;
793 fContext->getResourceCacheLimits(nullptr, &cacheSize);
794 if (bmpSize < cacheSize / 2) {
795 return false;
796 }
797
bsalomon1a1d0b82015-10-16 07:49:42 -0700798 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
799 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400800 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
801 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400802 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700803 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
804 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500805 kBmpSmallTileSize * kBmpSmallTileSize *
806 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700807
Brian Osmand05cdc32017-02-06 13:24:47 -0500808 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700809}
810
reed85d91782015-09-10 14:33:38 -0700811bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
812 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700813 const SkMatrix& viewMatrix,
814 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800815 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700816 // if image is explictly texture backed then just use the texture
Robert Phillips6de99042017-01-31 11:31:39 -0500817 if (image->isTextureBacked()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 return false;
819 }
820
Brian Salomon514baff2016-11-17 15:17:07 -0500821 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700822 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500823 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700824 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700825
826 int tileFilterPad;
827 if (doBicubic) {
828 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500829 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700830 tileFilterPad = 0;
831 } else {
832 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 }
reed85d91782015-09-10 14:33:38 -0700834 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835
bsalomon8c07b7a2015-11-02 11:36:52 -0800836 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837
reed85d91782015-09-10 14:33:38 -0700838 // these are output, which we safely ignore, as we just want to know the predicate
839 int outTileSize;
840 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841
bsalomone553b642016-08-17 09:02:09 -0700842 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
843 params, srcRectPtr, maxTileSize, &outTileSize,
844 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000845}
846
Mike Reeda1361362017-03-07 09:37:29 -0500847void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848 const SkMatrix& m,
849 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800850 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -0500851 CHECK_SHOULD_DRAW();
bsalomonb1b01992015-11-18 10:56:08 -0800852 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500853 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700854
bsalomonb1b01992015-11-18 10:56:08 -0800855 int maxTileSize = fContext->caps()->maxTileSize();
856
857 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
858 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -0400859 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800860 paint.isAntiAlias() &&
861 bitmap.width() <= maxTileSize &&
862 bitmap.height() <= maxTileSize;
863
864 bool skipTileCheck = drawAA || paint.getMaskFilter();
865
866 if (!skipTileCheck) {
867 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
868 int tileSize;
869 SkIRect clippedSrcRect;
870
Brian Salomon514baff2016-11-17 15:17:07 -0500871 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800872 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500873 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800874 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
875 &doBicubic);
876
877 int tileFilterPad;
878
879 if (doBicubic) {
880 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500881 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800882 tileFilterPad = 0;
883 } else {
884 tileFilterPad = 1;
885 }
886 params.setFilterMode(textureFilterMode);
887
888 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700889 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
890 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
891 &tileSize, &clippedSrcRect)) {
892 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
893 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
894 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800895 return;
896 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000897 }
Hal Canary144caf52016-11-07 17:57:18 -0500898 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800899 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -0400900 viewMatrix, this->clip(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901}
902
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000903// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
905// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000906static inline void clamped_outset_with_offset(SkIRect* iRect,
907 int outset,
908 SkPoint* offset,
909 const SkIRect& clamp) {
910 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000912 int leftClampDelta = clamp.fLeft - iRect->fLeft;
913 if (leftClampDelta > 0) {
914 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000915 iRect->fLeft = clamp.fLeft;
916 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000917 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000919
920 int topClampDelta = clamp.fTop - iRect->fTop;
921 if (topClampDelta > 0) {
922 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923 iRect->fTop = clamp.fTop;
924 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000925 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000926 }
927
928 if (iRect->fRight > clamp.fRight) {
929 iRect->fRight = clamp.fRight;
930 }
931 if (iRect->fBottom > clamp.fBottom) {
932 iRect->fBottom = clamp.fBottom;
933 }
934}
935
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936// Break 'bitmap' into several tiles to draw it since it has already
937// been determined to be too large to fit in VRAM
938void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800939 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700940 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 const SkRect& srcRect,
942 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500943 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800944 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700945 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946 int tileSize,
947 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800948 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800949
ericrk983294f2016-04-18 09:14:00 -0700950 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800951 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700952 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800953
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000954 // The following pixel lock is technically redundant, but it is desirable
955 // to lock outside of the tile loop to prevent redecoding the whole image
956 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
957 // is larger than the limit of the discardable memory pool.
958 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800959
960 const SkPaint* paint = &origPaint;
961 SkPaint tempPaint;
Brian Osman11052242016-10-27 14:47:55 -0400962 if (origPaint.isAntiAlias() && !fRenderTargetContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800963 // Drop antialiasing to avoid seams at tile boundaries.
964 tempPaint = origPaint;
965 tempPaint.setAntiAlias(false);
966 paint = &tempPaint;
967 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
969
970 int nx = bitmap.width() / tileSize;
971 int ny = bitmap.height() / tileSize;
972 for (int x = 0; x <= nx; x++) {
973 for (int y = 0; y <= ny; y++) {
974 SkRect tileR;
975 tileR.set(SkIntToScalar(x * tileSize),
976 SkIntToScalar(y * tileSize),
977 SkIntToScalar((x + 1) * tileSize),
978 SkIntToScalar((y + 1) * tileSize));
979
980 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
981 continue;
982 }
983
984 if (!tileR.intersect(srcRect)) {
985 continue;
986 }
987
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 SkIRect iTileR;
989 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700990 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
991 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500992 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700993 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500994 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 SkIRect iClampRect;
996
reeda5517e22015-07-14 10:54:12 -0700997 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 // In bleed mode we want to always expand the tile on all edges
999 // but stay within the bitmap bounds
1000 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1001 } else {
1002 // In texture-domain/clamp mode we only want to expand the
1003 // tile on edges interior to "srcRect" (i.e., we want to
1004 // not bleed across the original clamped edges)
1005 srcRect.roundOut(&iClampRect);
1006 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001007 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1008 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 }
1010
bsalomone553b642016-08-17 09:02:09 -07001011 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 if (bitmap.extractSubset(&tmpB, iTileR)) {
1013 // now offset it to make it "local" to our tmp bitmap
1014 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -08001015 // de-optimized this determination
1016 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001017 this->drawBitmapTile(tmpB,
1018 viewMatrix,
1019 rectToDraw,
1020 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -05001021 params,
bsalomone553b642016-08-17 09:02:09 -07001022 *paint,
1023 constraint,
1024 bicubic,
1025 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001026 }
1027 }
1028 }
1029}
1030
bsalomone553b642016-08-17 09:02:09 -07001031void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1032 const SkMatrix& viewMatrix,
1033 const SkRect& dstRect,
1034 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001035 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001036 const SkPaint& paint,
1037 SkCanvas::SrcRectConstraint constraint,
1038 bool bicubic,
1039 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001040 // We should have already handled bitmaps larger than the max texture size.
1041 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1042 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001043 // We should be respecting the max tile size by the time we get here.
1044 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1045 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001046
Robert Phillipse14d3052017-02-15 13:18:21 -05001047 SkASSERT(SkShader::kClamp_TileMode == params.getTileModeX() &&
1048 SkShader::kClamp_TileMode == params.getTileModeY());
1049
Robert Phillips67c18d62017-01-20 12:44:06 -05001050 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
Robert Phillipse14d3052017-02-15 13:18:21 -05001051 params, nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -07001052 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001053 return;
1054 }
brianosman500bb3e2016-07-22 10:33:07 -07001055 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001056 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001057
bsalomone553b642016-08-17 09:02:09 -07001058 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillipse14d3052017-02-15 13:18:21 -05001059 const SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect,
1060 SkMatrix::kFill_ScaleToFit);
joshualitt5f10b5c2015-07-09 10:24:35 -07001061
1062 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1063 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001064 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001065
reeda5517e22015-07-14 10:54:12 -07001066 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001068 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001070 domain.fLeft = srcRect.fLeft + 0.5f;
1071 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001072 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001073 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 }
1075 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001076 domain.fTop = srcRect.fTop + 0.5f;
1077 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001079 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001080 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001081 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001082 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001083 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001084 } else {
reedc7ec7c92016-07-25 08:29:10 -07001085 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001086 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001087 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001088 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001089 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001090 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001091 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001092 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093 } else {
reedc7ec7c92016-07-25 08:29:10 -07001094 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 }
1096
joshualitt33a5fce2015-11-18 13:28:51 -08001097 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001098 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001099 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1100 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001101 return;
1102 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001104 // Coverage-based AA would cause seams between tiles.
1105 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
1106 fRenderTargetContext->isStencilBufferMultisampled());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001107 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108}
1109
Mike Reeda1361362017-03-07 09:37:29 -05001110void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001112 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001113 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001114 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
robertphillips970587b2016-07-14 14:12:55 -07001116 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117 return;
1118 }
1119
Robert Phillipse14d3052017-02-15 13:18:21 -05001120 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1121 if (!srcImg) {
1122 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001123 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124
Mike Reeda1361362017-03-07 09:37:29 -05001125 this->drawSpecial(srcImg.get(), left, top, paint);
robertphillips970587b2016-07-14 14:12:55 -07001126}
1127
1128
Mike Reeda1361362017-03-07 09:37:29 -05001129void SkGpuDevice::drawSpecial(SkSpecialImage* special1,
robertphillips970587b2016-07-14 14:12:55 -07001130 int left, int top,
1131 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001132 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001133 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001134 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001135
1136 SkIPoint offset = { 0, 0 };
1137
1138 sk_sp<SkSpecialImage> result;
1139 if (paint.getImageFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -05001140 result = this->filterTexture(special1, left, top,
robertphillips970587b2016-07-14 14:12:55 -07001141 &offset,
1142 paint.getImageFilter());
1143 if (!result) {
1144 return;
1145 }
1146 } else {
1147 result = sk_ref_sp(special1);
1148 }
1149
1150 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001151 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001152 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001153 return;
1154 }
robertphillips970587b2016-07-14 14:12:55 -07001155
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001156 const GrPixelConfig config = proxy->config();
1157
robertphillips970587b2016-07-14 14:12:55 -07001158 SkPaint tmpUnfiltered(paint);
1159 tmpUnfiltered.setImageFilter(nullptr);
1160
brianosman77320db2016-09-07 08:09:10 -07001161 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001162 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001163
1164 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(this->context(),
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001165 std::move(proxy),
brianosman77320db2016-09-07 08:09:10 -07001166 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001167 SkMatrix::I()));
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001168 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001169 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001170 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001171 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001172 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001173
1174 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001175 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001176 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001177 return;
1178 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179
robertphillips970587b2016-07-14 14:12:55 -07001180 const SkIRect& subset = result->subset();
1181
Brian Salomon82f44312017-01-11 13:42:54 -05001182 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001183 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001184 std::move(grPaint),
1185 GrBoolToAA(paint.isAntiAlias()),
1186 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001187 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1188 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001189 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190}
1191
Mike Reeda1361362017-03-07 09:37:29 -05001192void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001193 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001194 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001195 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001196 CHECK_SHOULD_DRAW();
reedc7ec7c92016-07-25 08:29:10 -07001197
bsalomonb1b01992015-11-18 10:56:08 -08001198 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1199 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1200 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1201 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1202 const SkRect* dst = &origDst;
1203 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001205 if (!src) {
1206 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208
bsalomonb1b01992015-11-18 10:56:08 -08001209 SkMatrix srcToDstMatrix;
1210 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1211 return;
1212 }
1213 SkRect tmpSrc, tmpDst;
1214 if (src != &bmpBounds) {
1215 if (!bmpBounds.contains(*src)) {
1216 tmpSrc = *src;
1217 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 return; // nothing to draw
1219 }
bsalomonb1b01992015-11-18 10:56:08 -08001220 src = &tmpSrc;
1221 srcToDstMatrix.mapRect(&tmpDst, *src);
1222 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 }
1224 }
1225
bsalomonb1b01992015-11-18 10:56:08 -08001226 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001227
bsalomonb1b01992015-11-18 10:56:08 -08001228 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1229 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001230 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001231 paint.isAntiAlias() &&
1232 bitmap.width() <= maxTileSize &&
1233 bitmap.height() <= maxTileSize;
1234
1235 bool skipTileCheck = drawAA || paint.getMaskFilter();
1236
1237 if (!skipTileCheck) {
1238 int tileSize;
1239 SkIRect clippedSrcRect;
1240
Brian Salomon514baff2016-11-17 15:17:07 -05001241 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001242 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001243 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001244 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -08001245 &doBicubic);
1246
1247 int tileFilterPad;
1248
1249 if (doBicubic) {
1250 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001251 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001252 tileFilterPad = 0;
1253 } else {
1254 tileFilterPad = 1;
1255 }
1256 params.setFilterMode(textureFilterMode);
1257
1258 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001259 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001260 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1261 &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001262 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
bsalomone553b642016-08-17 09:02:09 -07001263 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001264 return;
1265 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001266 }
Hal Canary144caf52016-11-07 17:57:18 -05001267 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001268 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), this->clip(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269}
1270
robertphillips6451a0c2016-07-18 08:31:31 -07001271sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001272 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1273 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips26c90e02017-03-14 14:39:29 -04001274 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->resourceProvider(), bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001275 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001276 return nullptr;
1277 }
1278
Robert Phillipse14d3052017-02-15 13:18:21 -05001279 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001280
Robert Phillipse14d3052017-02-15 13:18:21 -05001281 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1282 // the special image
1283 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1284 rect,
1285 bitmap.getGenerationID(),
1286 std::move(proxy),
1287 bitmap.refColorSpace(),
1288 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001289}
1290
reede51c3562016-07-19 14:33:20 -07001291sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001292 SkPixmap pm;
1293 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001294 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001295
Robert Phillips6de99042017-01-31 11:31:39 -05001296 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1297 SkIRect::MakeWH(image->width(), image->height()),
1298 image->uniqueID(),
1299 std::move(proxy),
1300 as_IB(image)->onImageInfo().refColorSpace(),
1301 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001302 } else if (image->peekPixels(&pm)) {
1303 SkBitmap bm;
1304
1305 bm.installPixels(pm);
1306 return this->makeSpecial(bm);
1307 } else {
1308 return nullptr;
1309 }
1310}
1311
1312sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001313 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1314 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001315 // When the device doesn't have a texture, we create a temporary texture.
1316 // TODO: we should actually only copy the portion of the source needed to apply the image
1317 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001318 proxy = GrSurfaceProxy::Copy(fContext.get(),
1319 this->accessRenderTargetContext()->asSurfaceProxy(),
1320 SkBudgeted::kYes);
1321 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001322 return nullptr;
1323 }
robertphillips1b5f9682016-07-15 08:01:12 -07001324 }
1325
1326 const SkImageInfo ii = this->imageInfo();
1327 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1328
Robert Phillipse2f7d182016-12-15 09:23:05 -05001329 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1330 srcRect,
1331 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001332 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001333 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001334 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001335}
1336
Mike Reeda1361362017-03-07 09:37:29 -05001337void SkGpuDevice::drawDevice(SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001338 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001339 SkASSERT(!paint.getImageFilter());
1340
joshualittce894002016-01-11 13:29:31 -08001341 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001343 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001344
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345 // drawDevice is defined to be in device coords.
Mike Reeda1361362017-03-07 09:37:29 -05001346 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347
robertphillips1b5f9682016-07-15 08:01:12 -07001348 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001349 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001350 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 return;
1352 }
1353
Mike Reeda1361362017-03-07 09:37:29 -05001354 this->drawSpecial(srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355}
1356
Mike Reeda1361362017-03-07 09:37:29 -05001357void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y,
reeda85d4d02015-05-06 12:56:48 -07001358 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001359 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001360 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001361 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001362 uint32_t pinnedUniqueID;
1363 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001364 CHECK_SHOULD_DRAW();
brianosman5814b8a2016-08-18 06:43:03 -07001365 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001366 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001367 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001368 viewMatrix, this->clip(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001369 return;
reed85d91782015-09-10 14:33:38 -07001370 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001371 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001372 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
Mike Reeda1361362017-03-07 09:37:29 -05001373 paint.getFilterQuality(), this->ctm(), SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001374 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001375 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
reed85d91782015-09-10 14:33:38 -07001376 return;
1377 }
Mike Reeda1361362017-03-07 09:37:29 -05001378 this->drawBitmap(bm, SkMatrix::MakeTrans(x, y), paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001379 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Mike Reeda1361362017-03-07 09:37:29 -05001380 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001381 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001382 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001383 viewMatrix, this->clip(), paint);
Brian Osman61624f02016-12-09 14:51:59 -05001384 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001385 this->drawBitmap(bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001386 }
reeda85d4d02015-05-06 12:56:48 -07001387 }
1388}
1389
Mike Reeda1361362017-03-07 09:37:29 -05001390void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001391 const SkRect& dst, const SkPaint& paint,
1392 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001393 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001394 uint32_t pinnedUniqueID;
1395 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001396 CHECK_SHOULD_DRAW();
brianosman5814b8a2016-08-18 06:43:03 -07001397 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001398 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001399 this->drawTextureProducer(&adjuster, src, &dst, constraint, this->ctm(), this->clip(),
1400 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001401 return;
1402 }
1403 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001404 SkMatrix srcToDstRect;
1405 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1406 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001407 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001408 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001409 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001410 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001411 return;
1412 }
Mike Reeda1361362017-03-07 09:37:29 -05001413 this->drawBitmapRect(bm, src, dst, paint, constraint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001414 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Mike Reeda1361362017-03-07 09:37:29 -05001415 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001416 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001417 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), this->clip(), 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->drawBitmapRect(bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001420 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001421}
1422
Mike Reeda1361362017-03-07 09:37:29 -05001423void SkGpuDevice::drawProducerNine(GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001424 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001425 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001426
Mike Reeda1361362017-03-07 09:37:29 -05001427 CHECK_SHOULD_DRAW();
joshualitt33a5fce2015-11-18 13:28:51 -08001428
joshualittedb36442015-11-19 14:29:30 -08001429 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001430 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001431 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001432 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001433 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), SkMatrix::I(),
bsalomon2bbd0c62015-12-09 12:50:56 -08001434 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001435 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001436 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001437
1438 SkRect srcR, dstR;
1439 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001440 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001441 this->ctm(), this->clip(), paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001442 }
1443 return;
1444 }
1445
Brian Salomon514baff2016-11-17 15:17:07 -05001446 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001447 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001448 producer->createFragmentProcessor(SkMatrix::I(),
1449 SkRect::MakeIWH(producer->width(), producer->height()),
1450 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001451 &kMode, fRenderTargetContext->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -08001452 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001453 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001454 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001455 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001456 return;
1457 }
1458
msarett10e3d9b2016-08-18 15:46:03 -07001459 std::unique_ptr<SkLatticeIter> iter(
1460 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001461 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001462 producer->width(), producer->height(), std::move(iter),
1463 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001464}
1465
Mike Reeda1361362017-03-07 09:37:29 -05001466void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001467 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001468 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001469 uint32_t pinnedUniqueID;
1470 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001471 CHECK_SHOULD_DRAW();
brianosman5814b8a2016-08-18 06:43:03 -07001472 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001473 as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001474 this->drawProducerNine(&adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001475 } else {
1476 SkBitmap bm;
1477 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001478 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001479 this->drawProducerNine(&maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001480 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001481 this->drawBitmapNine(bm, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001482 }
1483 }
1484}
1485
Mike Reeda1361362017-03-07 09:37:29 -05001486void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001487 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001488 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001489 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001490 this->drawProducerNine(&maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001491}
1492
Mike Reeda1361362017-03-07 09:37:29 -05001493void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
msarett10e3d9b2016-08-18 15:46:03 -07001494 const SkCanvas::Lattice& lattice, const SkRect& dst,
1495 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001496 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001497
Mike Reeda1361362017-03-07 09:37:29 -05001498 CHECK_SHOULD_DRAW();
msarett10e3d9b2016-08-18 15:46:03 -07001499
Brian Salomon514baff2016-11-17 15:17:07 -05001500 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001501 sk_sp<GrFragmentProcessor> fp(
1502 producer->createFragmentProcessor(SkMatrix::I(),
1503 SkRect::MakeIWH(producer->width(), producer->height()),
1504 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001505 &kMode, fRenderTargetContext->getColorSpace()));
msarett10e3d9b2016-08-18 15:46:03 -07001506 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001507 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001508 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001509 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001510 return;
1511 }
1512
1513 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001514 new SkLatticeIter(lattice, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001515 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001516 producer->width(), producer->height(), std::move(iter),
1517 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001518}
1519
Mike Reeda1361362017-03-07 09:37:29 -05001520void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001521 const SkCanvas::Lattice& lattice, const SkRect& dst,
1522 const SkPaint& paint) {
1523 ASSERT_SINGLE_OWNER
1524 uint32_t pinnedUniqueID;
1525 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001526 CHECK_SHOULD_DRAW();
msarett10e3d9b2016-08-18 15:46:03 -07001527 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1528 as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001529 this->drawProducerLattice(&adjuster, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001530 } else {
1531 SkBitmap bm;
1532 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001533 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001534 this->drawProducerLattice(&maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001535 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001536 this->drawBitmapLattice(bm, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001537 }
1538 }
1539}
1540
Mike Reeda1361362017-03-07 09:37:29 -05001541void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001542 const SkCanvas::Lattice& lattice, const SkRect& dst,
1543 const SkPaint& paint) {
1544 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001545 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001546 this->drawProducerLattice(&maker, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001547}
1548
Robert Phillips26c90e02017-03-14 14:39:29 -04001549static bool init_vertices_paint(GrContext* context, GrRenderTargetContext* rtc,
1550 const SkPaint& skPaint,
1551 const SkMatrix& matrix, SkBlendMode bmode,
1552 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001553 if (hasTexs && skPaint.getShader()) {
1554 if (hasColors) {
1555 // When there are texs and colors the shader and colors are combined using bmode.
1556 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, matrix, bmode, false,
1557 grPaint);
1558 } else {
1559 // We have a shader, but no colors to blend it against.
1560 return SkPaintToGrPaint(context, rtc, skPaint, matrix, grPaint);
1561 }
1562 } else {
1563 if (hasColors) {
1564 // We have colors, but either have no shader or no texture coords (which implies that
1565 // we should ignore the shader).
1566 return SkPaintToGrPaintWithPrimitiveColor(context, rtc, skPaint, grPaint);
1567 } else {
1568 // No colors and no shaders. Just draw with the paint color.
1569 return (!SkPaintToGrPaintNoShader(context, rtc, skPaint, grPaint));
1570 }
1571 }
1572}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573
Mike Reeda1361362017-03-07 09:37:29 -05001574void SkGpuDevice::drawVertices(SkCanvas::VertexMode vmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001575 int vertexCount, const SkPoint vertices[],
1576 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001577 SkBlendMode bmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578 const uint16_t indices[], int indexCount,
1579 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001580 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001581 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001582 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001583
halcanary96fcdcc2015-08-27 07:41:13 -07001584 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1585 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001586
halcanary96fcdcc2015-08-27 07:41:13 -07001587 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001588
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001589 SkPaint copy(paint);
1590 copy.setStyle(SkPaint::kStroke_Style);
1591 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001592
bsalomonf1b7a1d2015-09-28 06:26:28 -07001593 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001594 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001595 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1596 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001597 return;
1598 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001599
dandov32a311b2014-07-15 19:46:26 -07001600 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001601 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001602 switch (vmode) {
1603 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001604 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001605 break;
1606 case SkCanvas::kTriangleStrip_VertexMode:
1607 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001608 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001609 break;
1610 }
mtklein533eb782014-08-27 10:39:42 -07001611
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001612 VertState state(vertexCount, indices, indexCount);
1613 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001614
dandov32a311b2014-07-15 19:46:26 -07001615 //number of indices for lines per triangle with kLines
1616 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001617
Ben Wagner7ecc5962016-11-02 17:07:33 -04001618 std::unique_ptr<uint16_t[]> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001619 int i = 0;
1620 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001621 lineIndices[i] = state.f0;
1622 lineIndices[i + 1] = state.f1;
1623 lineIndices[i + 2] = state.f1;
1624 lineIndices[i + 3] = state.f2;
1625 lineIndices[i + 4] = state.f2;
1626 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001627 i += 6;
1628 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001629 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001630 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001631 this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -04001632 kLines_GrPrimitiveType,
1633 vertexCount,
1634 vertices,
1635 texs,
1636 colors,
1637 lineIndices.get(),
1638 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001639 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001640 }
1641
Brian Salomon199fb872017-02-06 09:41:10 -05001642 GrPrimitiveType primType = SkVertexModeToGrPrimitiveType(vmode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643
bsalomonf1b7a1d2015-09-28 06:26:28 -07001644 GrPaint grPaint;
Robert Phillips26c90e02017-03-14 14:39:29 -04001645 if (!init_vertices_paint(fContext.get(), fRenderTargetContext.get(),
1646 paint, this->ctm(), bmode, SkToBool(texs),
1647 SkToBool(colors), &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001648 return;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001649 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001650 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001651 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001652 this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -04001653 primType,
1654 vertexCount,
1655 vertices,
1656 texs,
1657 colors,
1658 indices,
Brian Salomon3de0aee2017-01-29 09:34:17 -05001659 indexCount,
1660 GrRenderTargetContext::ColorArrayType::kSkColor);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661}
1662
Mike Reeda1361362017-03-07 09:37:29 -05001663void SkGpuDevice::drawVerticesObject(sk_sp<SkVertices> vertices,
Brian Salomon199fb872017-02-06 09:41:10 -05001664 SkBlendMode mode, const SkPaint& paint, uint32_t flags) {
1665 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001666 CHECK_SHOULD_DRAW();
Brian Salomon199fb872017-02-06 09:41:10 -05001667 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVerticesObject", fContext.get());
1668
1669 SkASSERT(vertices);
1670 GrPaint grPaint;
1671 bool hasColors = vertices->hasColors() && !(SkCanvas::kIgnoreColors_VerticesFlag & flags);
1672 bool hasTexs = vertices->hasTexCoords() & !(SkCanvas::kIgnoreTexCoords_VerticesFlag & flags);
1673 if (!hasTexs && !hasColors) {
1674 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reeda1361362017-03-07 09:37:29 -05001675 this->drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Brian Salomon199fb872017-02-06 09:41:10 -05001676 nullptr, nullptr, mode, vertices->indices(), vertices->indexCount(),
1677 paint);
1678 }
Robert Phillips26c90e02017-03-14 14:39:29 -04001679 if (!init_vertices_paint(fContext.get(), fRenderTargetContext.get(), paint, this->ctm(),
1680 mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001681 return;
1682 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001683 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon199fb872017-02-06 09:41:10 -05001684 std::move(vertices), flags);
1685}
1686
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687///////////////////////////////////////////////////////////////////////////////
1688
Mike Reeda1361362017-03-07 09:37:29 -05001689void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001690 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001691 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001692 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001693 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001694 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001695 return;
1696 }
1697
Mike Reeda1361362017-03-07 09:37:29 -05001698 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001699 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001700
reedca109532015-06-25 16:25:25 -07001701 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001702 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001703
jvanverth31ff7622015-08-07 10:09:28 -07001704 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001705 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001706 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reeda1361362017-03-07 09:37:29 -05001707 this->ctm(), (SkBlendMode)mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001708 return;
1709 }
1710 } else {
Mike Reeda1361362017-03-07 09:37:29 -05001711 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -04001712 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001713 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001714 }
1715 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001716
1717 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001718 fRenderTargetContext->drawAtlas(
1719 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001720}
1721
1722///////////////////////////////////////////////////////////////////////////////
1723
Mike Reeda1361362017-03-07 09:37:29 -05001724void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001725 size_t byteLength, SkScalar x, SkScalar y,
1726 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001727 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001728 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001729 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001730 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001731
Brian Salomon0166cfd2017-03-13 17:58:25 -04001732 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1733 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001734}
1735
Mike Reeda1361362017-03-07 09:37:29 -05001736void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001737 const SkScalar pos[], int scalarsPerPos,
1738 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001739 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001740 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -05001741 CHECK_SHOULD_DRAW();
jvanverth8c27a182014-10-14 08:45:50 -07001742 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001743
Brian Salomon0166cfd2017-03-13 17:58:25 -04001744 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1745 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001746 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747}
1748
Mike Reeda1361362017-03-07 09:37:29 -05001749void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001750 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001751 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001752 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -05001753 CHECK_SHOULD_DRAW();
joshualitt9c328182015-03-23 08:13:04 -07001754
1755 SkDEBUGCODE(this->validate();)
1756
Brian Salomon0166cfd2017-03-13 17:58:25 -04001757 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1758 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001759}
1760
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761///////////////////////////////////////////////////////////////////////////////
1762
reedb2db8982014-11-13 12:41:02 -08001763bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001764 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765}
1766
1767void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001768 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001769
Brian Osman11052242016-10-27 14:47:55 -04001770 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001771}
1772
1773///////////////////////////////////////////////////////////////////////////////
1774
reed76033be2015-03-14 10:54:31 -07001775SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001776 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777
robertphillipsca6eafc2016-05-17 09:57:46 -07001778 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001779
robertphillipsca6eafc2016-05-17 09:57:46 -07001780 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001781 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001782 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1783 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001784
Brian Osman11052242016-10-27 14:47:55 -04001785 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1786 fit,
1787 cinfo.fInfo.width(), cinfo.fInfo.height(),
1788 fRenderTargetContext->config(),
Robert Phillips75a475c2017-01-13 09:18:59 -05001789 fRenderTargetContext->refColorSpace(),
Brian Osman11052242016-10-27 14:47:55 -04001790 fRenderTargetContext->desc().fSampleCnt,
1791 kDefault_GrSurfaceOrigin,
1792 &props));
1793 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001794 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001796
1797 // Skia's convention is to only clear a device if it is non-opaque.
1798 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1799
Robert Phillips9fab7e92016-11-17 12:45:04 -05001800 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1801 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802}
1803
reede8f30622016-03-23 18:59:25 -07001804sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001805 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001806 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001807 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001808 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Osman11052242016-10-27 14:47:55 -04001809 fRenderTargetContext->desc().fSampleCnt,
1810 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001811}
1812
senorblanco900c3672016-04-27 11:31:23 -07001813SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001814 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001815 // We always return a transient cache, so it is freed after each
1816 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001817 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001818}
reedf037e0b2014-10-30 11:34:15 -07001819
1820#endif