blob: 845b22c3cc98e674f740440c294640ca63a7e45b [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"
robertphillips714712b2016-08-04 06:20:45 -070020
robertphillips30d78412014-11-24 09:49:17 -080021#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070022#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070024#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070025#include "SkGrPriv.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080026#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070028#include "SkImageFilterCache.h"
Brian Salomon6a639042016-12-14 11:08:17 -050029#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070030#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000031#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000033#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070034#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
Brian Salomon6a639042016-12-14 11:08:17 -050036#include "SkRasterClip.h"
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"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080046#include "effects/GrSimpleTextureEffect.h"
47#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080048#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049
reedf037e0b2014-10-30 11:34:15 -070050#if SK_SUPPORT_GPU
51
joshualittce894002016-01-11 13:29:31 -080052#define ASSERT_SINGLE_OWNER \
53 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
54
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055#if 0
56 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058 do { \
59 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080060 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 } while (0)
62#else
joshualitt5531d512014-12-17 15:50:11 -080063 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064#endif
65
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066///////////////////////////////////////////////////////////////////////////////
67
bsalomon74f681d2015-06-23 14:38:48 -070068/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
69 should fail. */
70bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
71 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
72 *flags = 0;
73 if (info) {
74 switch (info->alphaType()) {
75 case kPremul_SkAlphaType:
76 break;
77 case kOpaque_SkAlphaType:
78 *flags |= SkGpuDevice::kIsOpaque_Flag;
79 break;
80 default: // If it is unpremul or unknown don't try to render
81 return false;
82 }
83 }
84 if (kClear_InitContents == init) {
85 *flags |= kNeedClear_Flag;
86 }
87 return true;
88}
89
Robert Phillips9fab7e92016-11-17 12:45:04 -050090sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
91 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070092 int width, int height,
93 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040094 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070095 return nullptr;
96 }
97 unsigned flags;
98 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
99 return nullptr;
100 }
Robert Phillips9fab7e92016-11-17 12:45:04 -0500101 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
102 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000103}
104
robertphillips24e91282016-04-29 06:46:36 -0700105sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
106 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700107 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700108 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700109 unsigned flags;
110 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700112 }
113
Brian Osman11052242016-10-27 14:47:55 -0400114 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
115 info, sampleCount,
116 origin, props));
117 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700119 }
120
Robert Phillips9fab7e92016-11-17 12:45:04 -0500121 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700122 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700123}
124
Brian Osman11052242016-10-27 14:47:55 -0400125static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700126 SkColorType colorType;
127 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
128 colorType = kUnknown_SkColorType;
129 }
130 return SkImageInfo::Make(w, h, colorType,
131 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
132 sk_ref_sp(context->getColorSpace()));
133}
134
Robert Phillips9fab7e92016-11-17 12:45:04 -0500135SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
136 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400137 : INHERITED(make_info(renderTargetContext.get(), width, height,
138 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500139 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400140 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700141{
robertphillips1f3923e2016-07-21 07:17:54 -0700142 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700143 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144
bsalomone63ffef2016-02-05 07:17:34 -0800145 if (flags & kNeedClear_Flag) {
146 this->clearAll();
147 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148}
149
Brian Osman11052242016-10-27 14:47:55 -0400150sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
151 GrContext* context,
152 SkBudgeted budgeted,
153 const SkImageInfo& origInfo,
154 int sampleCount,
155 GrSurfaceOrigin origin,
156 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000157 if (kUnknown_SkColorType == origInfo.colorType() ||
158 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 }
161
bsalomonafe30052015-01-16 07:32:33 -0800162 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700163 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800164 }
165
Brian Osman0c2997b2017-01-11 16:58:42 -0500166 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Brian Osman11052242016-10-27 14:47:55 -0400167 return context->makeRenderTargetContext(SkBackingFit::kExact, // Why exact?
robertphillips6738c702016-07-27 12:13:51 -0700168 origInfo.width(), origInfo.height(),
Brian Osman0c2997b2017-01-11 16:58:42 -0500169 config, sk_ref_sp(origInfo.colorSpace()), sampleCount,
robertphillips6738c702016-07-27 12:13:51 -0700170 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800171}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000172
robertphillips970587b2016-07-14 14:12:55 -0700173sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
174 SkSpecialImage* srcImg,
175 int left, int top,
176 SkIPoint* offset,
177 const SkImageFilter* filter) {
178 SkASSERT(srcImg->isTextureBacked());
179 SkASSERT(filter);
180
181 SkMatrix matrix = *draw.fMatrix;
182 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
183 const SkIRect clipBounds = draw.fRC->getBounds().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
Brian Osman11052242016-10-27 14:47:55 -0400197 return fRenderTargetContext->readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000198}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199
robertphillips1da3ecd2016-08-31 14:54:15 -0700200bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
201 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800202 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700203
Brian Osman11052242016-10-27 14:47:55 -0400204 return fRenderTargetContext->writePixels(srcInfo, srcPixels, srcRowBytes, x, y);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000205}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000206
reed41e010c2015-06-09 12:16:53 -0700207bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800208 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700209 return false;
210}
211
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000212// call this every draw call, to ensure that the context reflects our state,
213// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800214void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800215 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216
reed2c9e2002016-07-25 08:05:22 -0700217 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000218}
219
Brian Osman11052242016-10-27 14:47:55 -0400220GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700221 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400222 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700223}
224
reed8eddfb52014-12-04 07:50:14 -0800225void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800226 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500227 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500228
reed8eddfb52014-12-04 07:50:14 -0800229 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500230 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800231}
232
Brian Osman11052242016-10-27 14:47:55 -0400233void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800234 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800235
Brian Osman693a5402016-10-27 15:13:22 -0400236 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800237
Brian Osman693a5402016-10-27 15:13:22 -0400238 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400239 this->context(),
240 budgeted,
241 this->imageInfo(),
242 fRenderTargetContext->numColorSamples(),
243 fRenderTargetContext->origin(),
244 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400245 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800246 return;
247 }
248
249 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400250 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800251 return;
252 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500253 newRTC->copy(fRenderTargetContext->asDeferredSurface());
kkinnunenabcfab42015-02-22 22:53:44 -0800254 }
255
Brian Osman693a5402016-10-27 15:13:22 -0400256 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800257}
258
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259///////////////////////////////////////////////////////////////////////////////
260
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000261void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800262 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800263 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500264 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000265
266 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400267 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
268 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700269 return;
270 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271
Brian Salomon82f44312017-01-11 13:42:54 -0500272 fRenderTargetContext->drawPaint(fClip, std::move(grPaint), *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273}
274
275// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700276static const GrPrimitiveType gPointMode2PrimitiveType[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277 kPoints_GrPrimitiveType,
278 kLines_GrPrimitiveType,
279 kLineStrip_GrPrimitiveType
280};
281
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500282static inline bool is_int(float x) { return x == (float) sk_float_round2int(x); }
283
ethannicholas330bb952015-07-17 06:44:02 -0700284// suppress antialiasing on axis-aligned integer-coordinate lines
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500285static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
286 const SkMatrix& matrix) {
ethannicholas330bb952015-07-17 06:44:02 -0700287 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
288 return false;
289 }
290 if (count == 2) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500291 // We do not antialias horizontal or vertical lines along pixel centers, even when the ends
292 // of the line do not fully cover the first and last pixel of the line, which is slightly
293 // wrong.
294 if (!matrix.isScaleTranslate()) {
295 return true;
296 }
ethannicholas330bb952015-07-17 06:44:02 -0700297 if (pts[0].fX == pts[1].fX) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500298 SkScalar x = matrix.getScaleX() * pts[0].fX + matrix.getTranslateX();
299 return !is_int(x + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700300 }
301 if (pts[0].fY == pts[1].fY) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500302 SkScalar y = matrix.getScaleY() * pts[0].fY + matrix.getTranslateY();
303 return !is_int(y + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700304 }
305 }
306 return true;
307}
308
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
310 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800311 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500312 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800313 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000314
315 SkScalar width = paint.getStrokeWidth();
316 if (width < 0) {
317 return;
318 }
319
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000320 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700321 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700322 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400323 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700324 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700325 return;
326 }
egdaniele61c4112014-06-12 10:24:21 -0700327 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700328 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700329 path.moveTo(pts[0]);
330 path.lineTo(pts[1]);
Brian Salomon82f44312017-01-11 13:42:54 -0500331 fRenderTargetContext->drawPath(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500332 *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700333 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000334 }
335
bsalomon6ade6dd2016-09-12 12:07:17 -0700336 SkScalar scales[2];
337 bool isHairline = (0 == width) || (1 == width && draw.fMatrix->getMinMaxScales(scales) &&
338 SkScalarNearlyEqual(scales[0], 1.f) &&
339 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700340 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700342 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500343 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts, *draw.fMatrix))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000344 draw.drawPoints(mode, count, pts, paint, true);
345 return;
346 }
347
bsalomon6ade6dd2016-09-12 12:07:17 -0700348 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
349
350 const SkMatrix* viewMatrix = draw.fMatrix;
351#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
352 // This offsetting in device space matches the expectations of the Android framework for non-AA
353 // points and lines.
354 SkMatrix tempMatrix;
355 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
356 tempMatrix = *viewMatrix;
357 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
358 tempMatrix.postTranslate(kOffset, kOffset);
359 viewMatrix = &tempMatrix;
360 }
361#endif
362
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400364 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
365 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700366 return;
367 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
Brian Osman11052242016-10-27 14:47:55 -0400369 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500370 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -0400371 *viewMatrix,
372 primitiveType,
373 SkToS32(count),
374 (SkPoint*)pts,
375 nullptr,
376 nullptr,
377 nullptr,
378 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379}
380
381///////////////////////////////////////////////////////////////////////////////
382
robertphillipsff55b492015-11-24 07:56:59 -0800383void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800384 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500385 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800386 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000387
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000388
bsalomona7d85ba2016-07-06 11:54:59 -0700389 // A couple reasons we might need to call drawPath.
390 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700392 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500394 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800395 fClip, path, paint,
396 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700397 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398 return;
399 }
400
401 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400402 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
403 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700404 return;
405 }
Mike Klein744fb732014-06-23 15:13:26 -0400406
bsalomon6663acf2016-05-10 09:14:17 -0700407 GrStyle style(paint);
Brian Salomon82f44312017-01-11 13:42:54 -0500408 fRenderTargetContext->drawRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
409 *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000410}
411
412///////////////////////////////////////////////////////////////////////////////
413
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500414void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800415 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500416 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800417 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000419 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400420 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
421 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700422 return;
423 }
Mike Klein744fb732014-06-23 15:13:26 -0400424
Robert Phillipsa29a9562016-10-20 09:40:55 -0400425 SkMaskFilter* mf = paint.getMaskFilter();
426 if (mf && mf->asFragmentProcessor(nullptr, nullptr, *draw.fMatrix)) {
427 mf = nullptr; // already handled in SkPaintToGrPaint
428 }
429
bsalomon6663acf2016-05-10 09:14:17 -0700430 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400431 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000432 // try to hit the fast path for drawing filtered round rects
433
434 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700435 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000436 if (devRRect.allCornersCircular()) {
437 SkRect maskRect;
Robert Phillipsa29a9562016-10-20 09:40:55 -0400438 if (mf->canFilterMaskGPU(devRRect, draw.fRC->getBounds(),
439 *draw.fMatrix, &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000440 SkIRect finalIRect;
441 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700442 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000443 // clipped out
444 return;
445 }
Hal Canary144caf52016-11-07 17:57:18 -0500446 if (mf->directFilterRRectMaskGPU(fContext.get(), fRenderTargetContext.get(),
Brian Salomon82f44312017-01-11 13:42:54 -0500447 std::move(grPaint), fClip, *draw.fMatrix,
Hal Canary144caf52016-11-07 17:57:18 -0500448 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000449 return;
450 }
451 }
452
453 }
454 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000455 }
456
Robert Phillipsa29a9562016-10-20 09:40:55 -0400457 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800458 // The only mask filter the native rrect drawing code could've handle was taken
459 // care of above.
460 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000461 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700462 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700463 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500464 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800465 fClip, path, paint,
466 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700467 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000468 return;
469 }
Mike Klein744fb732014-06-23 15:13:26 -0400470
bsalomon6663acf2016-05-10 09:14:17 -0700471 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800472
Brian Salomon82f44312017-01-11 13:42:54 -0500473 fRenderTargetContext->drawRRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
474 *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475}
476
robertphillipsd7706102016-02-25 09:28:08 -0800477
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000478void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800479 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800480 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500481 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillips0e7029e2015-11-30 05:45:06 -0800482 CHECK_SHOULD_DRAW(draw);
483
robertphillipsd7706102016-02-25 09:28:08 -0800484 if (outer.isEmpty()) {
485 return;
486 }
487
488 if (inner.isEmpty()) {
489 return this->drawRRect(draw, outer, paint);
490 }
491
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000492 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000493
robertphillips0e7029e2015-11-30 05:45:06 -0800494 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800495 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400496 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700497 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700498 return;
499 }
robertphillips00095892016-02-29 13:50:40 -0800500
Brian Salomon82f44312017-01-11 13:42:54 -0500501 fRenderTargetContext->drawDRRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500502 *draw.fMatrix, outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800503 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000504 }
505
506 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700507 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000508 path.addRRect(outer);
509 path.addRRect(inner);
510 path.setFillType(SkPath::kEvenOdd_FillType);
511
Hal Canary144caf52016-11-07 17:57:18 -0500512 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800513 fClip, path, paint,
514 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700515 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000516}
517
518
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000519/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520
msarettcc319b92016-08-25 18:07:18 -0700521void SkGpuDevice::drawRegion(const SkDraw& draw, const SkRegion& region, const SkPaint& paint) {
522 if (paint.getMaskFilter()) {
523 SkPath path;
524 region.getBoundaryPath(&path);
525 return this->drawPath(draw, path, paint, nullptr, false);
526 }
527
528 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400529 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
530 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700531 return;
532 }
533
Brian Salomon82f44312017-01-11 13:42:54 -0500534 fRenderTargetContext->drawRegion(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
535 *draw.fMatrix, region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700536}
537
robertphillips514450c2015-11-24 05:36:02 -0800538void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800539 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500540 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800541 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542
robertphillips514450c2015-11-24 05:36:02 -0800543 // Presumably the path effect warps this to something other than an oval
544 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700546 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700548 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549 return;
herb11a7f7f2015-11-24 12:41:00 -0800550 }
551
robertphillips514450c2015-11-24 05:36:02 -0800552 if (paint.getMaskFilter()) {
553 // The RRect path can handle special case blurring
554 SkRRect rr = SkRRect::MakeOval(oval);
555 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 }
557
558 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400559 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
560 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700561 return;
562 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563
Brian Salomon82f44312017-01-11 13:42:54 -0500564 fRenderTargetContext->drawOval(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
565 *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566}
567
bsalomon4f3a0ca2016-08-22 13:14:26 -0700568void SkGpuDevice::drawArc(const SkDraw& draw, const SkRect& oval, SkScalar startAngle,
569 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
570 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500571 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700572 CHECK_SHOULD_DRAW(draw);
573
574 if (paint.getMaskFilter()) {
575 this->INHERITED::drawArc(draw, oval, startAngle, sweepAngle, useCenter, paint);
576 return;
577 }
578 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400579 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
580 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700581 return;
582 }
583
Brian Salomon82f44312017-01-11 13:42:54 -0500584 fRenderTargetContext->drawArc(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
585 *draw.fMatrix, oval, startAngle, sweepAngle, useCenter,
586 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700587}
588
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000590
591///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700592void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
593 const SkDraw& draw,
594 const SkPaint& origPaint) {
595 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500596 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
robertphillips0851d2d2016-06-02 05:21:34 -0700597 CHECK_SHOULD_DRAW(draw);
598
Brian Osman11052242016-10-27 14:47:55 -0400599 // Adding support for round capping would require a
600 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700601 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
602 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
603 SkASSERT(!origPaint.getPathEffect());
604 SkASSERT(!origPaint.getMaskFilter());
605
606 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
607 SkASSERT(halfWidth > 0);
608
609 SkVector v = points[1] - points[0];
610
611 SkScalar length = SkPoint::Normalize(&v);
612 if (!length) {
613 v.fX = 1.0f;
614 v.fY = 0.0f;
615 }
616
617 SkPaint newPaint(origPaint);
618 newPaint.setStyle(SkPaint::kFill_Style);
619
620 SkScalar xtraLength = 0.0f;
621 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
622 xtraLength = halfWidth;
623 }
624
625 SkPoint mid = points[0] + points[1];
626 mid.scale(0.5f);
627
628 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
629 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
630 SkMatrix m;
631 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
632
633 SkMatrix local = m;
634
635 m.postConcat(*draw.fMatrix);
636
637 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400638 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700639 return;
640 }
641
Brian Salomon82f44312017-01-11 13:42:54 -0500642 fRenderTargetContext->fillRectWithLocalMatrix(
643 fClip, std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700644}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000645
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
647 const SkPaint& paint, const SkMatrix* prePathMatrix,
648 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800649 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800650 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700651 SkPoint points[2];
652 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
653 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
654 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
655 // Path-based stroking looks better for thin rects
656 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700657 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500658 // Round capping support is currently disabled b.c. it would require a RRect
659 // GrDrawOp that takes a localMatrix.
robertphillips0851d2d2016-06-02 05:21:34 -0700660 this->drawStrokedLine(points, draw, paint);
661 return;
662 }
663 }
robertphillipsff55b492015-11-24 07:56:59 -0800664 bool isClosed;
665 SkRect rect;
666 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
667 this->drawRect(draw, rect, paint);
668 return;
669 }
670 if (origSrcPath.isOval(&rect)) {
671 this->drawOval(draw, rect, paint);
672 return;
673 }
674 SkRRect rrect;
675 if (origSrcPath.isRRect(&rrect)) {
676 this->drawRRect(draw, rrect, paint);
677 return;
678 }
679 }
680
joshualitt5531d512014-12-17 15:50:11 -0800681 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500682 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683
Hal Canary144caf52016-11-07 17:57:18 -0500684 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700685 fClip, origSrcPath, paint,
686 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700687 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000688}
689
690static const int kBmpSmallTileSize = 1 << 10;
691
692static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
693 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
694 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
695 return tilesX * tilesY;
696}
697
reed85d91782015-09-10 14:33:38 -0700698static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 if (maxTileSize <= kBmpSmallTileSize) {
700 return maxTileSize;
701 }
702
703 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
704 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
705
706 maxTileTotalTileSize *= maxTileSize * maxTileSize;
707 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
708
709 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
710 return kBmpSmallTileSize;
711 } else {
712 return maxTileSize;
713 }
714}
715
716// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
717// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700718static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800719 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800720 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700721 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700722 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 const SkRect* srcRectPtr,
724 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700725 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700726 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
727 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 clippedSrcIRect->setEmpty();
729 return;
730 }
731 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
732 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700733 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 if (!clippedSrcRect.intersect(*srcRectPtr)) {
735 clippedSrcIRect->setEmpty();
736 return;
737 }
738 }
739 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700740 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 if (!clippedSrcIRect->intersect(bmpBounds)) {
742 clippedSrcIRect->setEmpty();
743 }
744}
745
reed85d91782015-09-10 14:33:38 -0700746bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
747 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700748 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500749 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700750 const SkRect* srcRectPtr,
751 int maxTileSize,
752 int* tileSize,
753 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800754 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700755 // if it's larger than the max tile size, then we have no choice but tiling.
756 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400757 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
758 fClip, viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
759 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700760 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
761 return true;
762 }
763
bsalomon1a1d0b82015-10-16 07:49:42 -0700764 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700765 const size_t area = imageRect.width() * imageRect.height();
766 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
767 return false;
768 }
769
reed85d91782015-09-10 14:33:38 -0700770 // At this point we know we could do the draw by uploading the entire bitmap
771 // as a texture. However, if the texture would be large compared to the
772 // cache size and we don't require most of it for this draw then tile to
773 // reduce the amount of upload and cache spill.
774
775 // assumption here is that sw bitmap size is a good proxy for its size as
776 // a texture
777 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
778 size_t cacheSize;
779 fContext->getResourceCacheLimits(nullptr, &cacheSize);
780 if (bmpSize < cacheSize / 2) {
781 return false;
782 }
783
bsalomon1a1d0b82015-10-16 07:49:42 -0700784 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
785 // tiling memory savings would be < 50%.
Brian Osman11052242016-10-27 14:47:55 -0400786 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(), fClip,
787 viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
788 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700789 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
790 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
791 kBmpSmallTileSize * kBmpSmallTileSize;
792
793 return usedTileBytes < 2 * bmpSize;
794}
795
reed85d91782015-09-10 14:33:38 -0700796bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
797 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700798 const SkMatrix& viewMatrix,
799 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800800 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700801 // if image is explictly texture backed then just use the texture
802 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803 return false;
804 }
805
Brian Salomon514baff2016-11-17 15:17:07 -0500806 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700807 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500808 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700809 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700810
811 int tileFilterPad;
812 if (doBicubic) {
813 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500814 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700815 tileFilterPad = 0;
816 } else {
817 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 }
reed85d91782015-09-10 14:33:38 -0700819 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820
bsalomon8c07b7a2015-11-02 11:36:52 -0800821 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822
reed85d91782015-09-10 14:33:38 -0700823 // these are output, which we safely ignore, as we just want to know the predicate
824 int outTileSize;
825 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000826
bsalomone553b642016-08-17 09:02:09 -0700827 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
828 params, srcRectPtr, maxTileSize, &outTileSize,
829 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830}
831
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000832void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 const SkBitmap& bitmap,
834 const SkMatrix& m,
835 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800836 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800837 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800838 SkMatrix viewMatrix;
839 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700840
bsalomonb1b01992015-11-18 10:56:08 -0800841 int maxTileSize = fContext->caps()->maxTileSize();
842
843 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
844 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -0400845 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800846 paint.isAntiAlias() &&
847 bitmap.width() <= maxTileSize &&
848 bitmap.height() <= maxTileSize;
849
850 bool skipTileCheck = drawAA || paint.getMaskFilter();
851
852 if (!skipTileCheck) {
853 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
854 int tileSize;
855 SkIRect clippedSrcRect;
856
Brian Salomon514baff2016-11-17 15:17:07 -0500857 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800858 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500859 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800860 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
861 &doBicubic);
862
863 int tileFilterPad;
864
865 if (doBicubic) {
866 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500867 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800868 tileFilterPad = 0;
869 } else {
870 tileFilterPad = 1;
871 }
872 params.setFilterMode(textureFilterMode);
873
874 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700875 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
876 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
877 &tileSize, &clippedSrcRect)) {
878 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
879 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
880 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800881 return;
882 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000883 }
Hal Canary144caf52016-11-07 17:57:18 -0500884 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800885 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
886 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000887}
888
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000889// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
891// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000892static inline void clamped_outset_with_offset(SkIRect* iRect,
893 int outset,
894 SkPoint* offset,
895 const SkIRect& clamp) {
896 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000898 int leftClampDelta = clamp.fLeft - iRect->fLeft;
899 if (leftClampDelta > 0) {
900 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901 iRect->fLeft = clamp.fLeft;
902 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000903 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000905
906 int topClampDelta = clamp.fTop - iRect->fTop;
907 if (topClampDelta > 0) {
908 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 iRect->fTop = clamp.fTop;
910 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000911 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 }
913
914 if (iRect->fRight > clamp.fRight) {
915 iRect->fRight = clamp.fRight;
916 }
917 if (iRect->fBottom > clamp.fBottom) {
918 iRect->fBottom = clamp.fBottom;
919 }
920}
921
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922// Break 'bitmap' into several tiles to draw it since it has already
923// been determined to be too large to fit in VRAM
924void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800925 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700926 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 const SkRect& srcRect,
928 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500929 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800930 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700931 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000932 int tileSize,
933 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800934 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800935
ericrk983294f2016-04-18 09:14:00 -0700936 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800937 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700938 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800939
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000940 // The following pixel lock is technically redundant, but it is desirable
941 // to lock outside of the tile loop to prevent redecoding the whole image
942 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
943 // is larger than the limit of the discardable memory pool.
944 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800945
946 const SkPaint* paint = &origPaint;
947 SkPaint tempPaint;
Brian Osman11052242016-10-27 14:47:55 -0400948 if (origPaint.isAntiAlias() && !fRenderTargetContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800949 // Drop antialiasing to avoid seams at tile boundaries.
950 tempPaint = origPaint;
951 tempPaint.setAntiAlias(false);
952 paint = &tempPaint;
953 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000954 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
955
956 int nx = bitmap.width() / tileSize;
957 int ny = bitmap.height() / tileSize;
958 for (int x = 0; x <= nx; x++) {
959 for (int y = 0; y <= ny; y++) {
960 SkRect tileR;
961 tileR.set(SkIntToScalar(x * tileSize),
962 SkIntToScalar(y * tileSize),
963 SkIntToScalar((x + 1) * tileSize),
964 SkIntToScalar((y + 1) * tileSize));
965
966 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
967 continue;
968 }
969
970 if (!tileR.intersect(srcRect)) {
971 continue;
972 }
973
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 SkIRect iTileR;
975 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700976 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
977 SkIntToScalar(iTileR.fTop));
978 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
979 tileR.width(), tileR.height());
980 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500981 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 SkIRect iClampRect;
983
reeda5517e22015-07-14 10:54:12 -0700984 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 // In bleed mode we want to always expand the tile on all edges
986 // but stay within the bitmap bounds
987 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
988 } else {
989 // In texture-domain/clamp mode we only want to expand the
990 // tile on edges interior to "srcRect" (i.e., we want to
991 // not bleed across the original clamped edges)
992 srcRect.roundOut(&iClampRect);
993 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000994 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
995 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 }
997
bsalomone553b642016-08-17 09:02:09 -0700998 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 if (bitmap.extractSubset(&tmpB, iTileR)) {
1000 // now offset it to make it "local" to our tmp bitmap
1001 tileR.offset(-offset.fX, -offset.fY);
Brian Salomon514baff2016-11-17 15:17:07 -05001002 GrSamplerParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001003 // de-optimized this determination
1004 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001005 this->drawBitmapTile(tmpB,
1006 viewMatrix,
1007 rectToDraw,
1008 tileR,
1009 paramsTemp,
1010 *paint,
1011 constraint,
1012 bicubic,
1013 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 }
1015 }
1016 }
1017}
1018
bsalomone553b642016-08-17 09:02:09 -07001019void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1020 const SkMatrix& viewMatrix,
1021 const SkRect& dstRect,
1022 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001023 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001024 const SkPaint& paint,
1025 SkCanvas::SrcRectConstraint constraint,
1026 bool bicubic,
1027 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001028 // We should have already handled bitmaps larger than the max texture size.
1029 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1030 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001031 // We should be respecting the max tile size by the time we get here.
1032 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1033 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034
Brian Osman61624f02016-12-09 14:51:59 -05001035 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap, params);
halcanary96fcdcc2015-08-27 07:41:13 -07001036 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 return;
1038 }
brianosman500bb3e2016-07-22 10:33:07 -07001039 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001040 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001041
1042 SkScalar iw = 1.f / texture->width();
1043 SkScalar ih = 1.f / texture->height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001044
egdaniel79da63f2015-10-09 10:55:16 -07001045 SkMatrix texMatrix;
bsalomone553b642016-08-17 09:02:09 -07001046 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomond93f4a42016-11-14 14:41:58 -05001047 texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
bsalomone553b642016-08-17 09:02:09 -07001048 texMatrix.postScale(iw, ih);
joshualitt5f10b5c2015-07-09 10:24:35 -07001049
1050 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1051 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001052 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001053
reeda5517e22015-07-14 10:54:12 -07001054 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001056 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001058 domain.fLeft = srcRect.fLeft + 0.5f;
1059 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001061 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 }
1063 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001064 domain.fTop = srcRect.fTop + 0.5f;
1065 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001067 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001069 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001070 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001071 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001072 } else {
reedc7ec7c92016-07-25 08:29:10 -07001073 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001074 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001075 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001076 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001077 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001078 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001079 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001080 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 } else {
reedc7ec7c92016-07-25 08:29:10 -07001082 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 }
1084
joshualitt33a5fce2015-11-18 13:28:51 -08001085 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001086 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001087 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1088 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001089 return;
1090 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001091
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001092 // Coverage-based AA would cause seams between tiles.
1093 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
1094 fRenderTargetContext->isStencilBufferMultisampled());
Brian Salomon82f44312017-01-11 13:42:54 -05001095 fRenderTargetContext->drawRect(fClip, std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096}
1097
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1099 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001100 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001101 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001102 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103
robertphillips970587b2016-07-14 14:12:55 -07001104 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105 return;
1106 }
1107
reedc7ec7c92016-07-25 08:29:10 -07001108 sk_sp<GrTexture> texture;
1109 {
robertphillips970587b2016-07-14 14:12:55 -07001110 SkAutoLockPixels alp(bitmap, true);
1111 if (!bitmap.readyToDraw()) {
1112 return;
1113 }
1114
1115 // draw sprite neither filters nor tiles.
Brian Osman7b8400d2016-11-08 17:08:54 -05001116 texture.reset(
Brian Osman61624f02016-12-09 14:51:59 -05001117 GrRefCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter()));
robertphillips970587b2016-07-14 14:12:55 -07001118 if (!texture) {
1119 return;
1120 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001121 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122
robertphillips970587b2016-07-14 14:12:55 -07001123 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1124 bitmap.pixelRefOrigin().fY,
1125 bitmap.width(),
1126 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001127
robertphillips970587b2016-07-14 14:12:55 -07001128 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1129 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001130 std::move(texture),
1131 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001132 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001133
1134 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1135}
1136
1137
Mike Kleine54c75f2016-10-13 14:18:09 -04001138void SkGpuDevice::drawSpecial(const SkDraw& draw,
robertphillips970587b2016-07-14 14:12:55 -07001139 SkSpecialImage* special1,
1140 int left, int top,
1141 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001142 ASSERT_SINGLE_OWNER
1143 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001144 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001145
1146 SkIPoint offset = { 0, 0 };
1147
1148 sk_sp<SkSpecialImage> result;
1149 if (paint.getImageFilter()) {
1150 result = this->filterTexture(draw, special1, left, top,
1151 &offset,
1152 paint.getImageFilter());
1153 if (!result) {
1154 return;
1155 }
1156 } else {
1157 result = sk_ref_sp(special1);
1158 }
1159
1160 SkASSERT(result->isTextureBacked());
Hal Canary144caf52016-11-07 17:57:18 -05001161 sk_sp<GrTexture> texture = result->asTextureRef(fContext.get());
Robert Phillips833dcf42016-11-18 08:44:13 -05001162 if (!texture) {
1163 return;
1164 }
robertphillips970587b2016-07-14 14:12:55 -07001165
1166 SkPaint tmpUnfiltered(paint);
1167 tmpUnfiltered.setImageFilter(nullptr);
1168
brianosman77320db2016-09-07 08:09:10 -07001169 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001170 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001172 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1173 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001174 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001175 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001176 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001177 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001178 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001179 }
Brian Osman11052242016-10-27 14:47:55 -04001180 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001181 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001182 return;
1183 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184
robertphillips970587b2016-07-14 14:12:55 -07001185 const SkIRect& subset = result->subset();
1186
Brian Salomon82f44312017-01-11 13:42:54 -05001187 fRenderTargetContext->fillRectToRect(
1188 fClip,
1189 std::move(grPaint),
1190 GrBoolToAA(paint.isAntiAlias()),
1191 SkMatrix::I(),
1192 SkRect::Make(SkIRect::MakeXYWH(
1193 left + offset.fX, top + offset.fY, subset.width(), subset.height())),
1194 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1195 SkIntToScalar(subset.fTop) / texture->height(),
1196 SkIntToScalar(subset.width()) / texture->width(),
1197 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198}
1199
bsalomonb1b01992015-11-18 10:56:08 -08001200void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1201 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001202 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001203 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001204 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001205
bsalomonb1b01992015-11-18 10:56:08 -08001206 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1207 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1208 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1209 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1210 const SkRect* dst = &origDst;
1211 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001213 if (!src) {
1214 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001216
bsalomonb1b01992015-11-18 10:56:08 -08001217 SkMatrix srcToDstMatrix;
1218 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1219 return;
1220 }
1221 SkRect tmpSrc, tmpDst;
1222 if (src != &bmpBounds) {
1223 if (!bmpBounds.contains(*src)) {
1224 tmpSrc = *src;
1225 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 return; // nothing to draw
1227 }
bsalomonb1b01992015-11-18 10:56:08 -08001228 src = &tmpSrc;
1229 srcToDstMatrix.mapRect(&tmpDst, *src);
1230 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001231 }
1232 }
1233
bsalomonb1b01992015-11-18 10:56:08 -08001234 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001235
bsalomonb1b01992015-11-18 10:56:08 -08001236 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1237 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001238 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001239 paint.isAntiAlias() &&
1240 bitmap.width() <= maxTileSize &&
1241 bitmap.height() <= maxTileSize;
1242
1243 bool skipTileCheck = drawAA || paint.getMaskFilter();
1244
1245 if (!skipTileCheck) {
1246 int tileSize;
1247 SkIRect clippedSrcRect;
1248
Brian Salomon514baff2016-11-17 15:17:07 -05001249 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001250 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001251 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -08001252 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1253 &doBicubic);
1254
1255 int tileFilterPad;
1256
1257 if (doBicubic) {
1258 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001259 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001260 tileFilterPad = 0;
1261 } else {
1262 tileFilterPad = 1;
1263 }
1264 params.setFilterMode(textureFilterMode);
1265
1266 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001267 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1268 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1269 &clippedSrcRect)) {
1270 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1271 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001272 return;
1273 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001274 }
Hal Canary144caf52016-11-07 17:57:18 -05001275 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001276 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277}
1278
robertphillips6451a0c2016-07-18 08:31:31 -07001279sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001280 SkAutoLockPixels alp(bitmap, true);
1281 if (!bitmap.readyToDraw()) {
1282 return nullptr;
1283 }
1284
Brian Osman7b8400d2016-11-08 17:08:54 -05001285 sk_sp<GrTexture> texture =
Brian Osman61624f02016-12-09 14:51:59 -05001286 GrMakeCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter());
robertphillips6451a0c2016-07-18 08:31:31 -07001287 if (!texture) {
1288 return nullptr;
1289 }
1290
1291 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1292 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001293 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001294 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001295 &this->surfaceProps());
1296}
1297
reede51c3562016-07-19 14:33:20 -07001298sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001299 SkPixmap pm;
1300 if (image->isTextureBacked()) {
1301 GrTexture* texture = as_IB(image)->peekTexture();
1302
1303 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1304 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001305 sk_ref_sp(texture),
1306 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001307 &this->surfaceProps());
1308 } else if (image->peekPixels(&pm)) {
1309 SkBitmap bm;
1310
1311 bm.installPixels(pm);
1312 return this->makeSpecial(bm);
1313 } else {
1314 return nullptr;
1315 }
1316}
1317
1318sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillipse2f7d182016-12-15 09:23:05 -05001319 sk_sp<GrSurfaceProxy> sProxy(sk_ref_sp(this->accessRenderTargetContext()->asDeferredTexture()));
1320 if (!sProxy) {
robertphillips04d62182016-07-15 12:21:33 -07001321 // When the device doesn't have a texture, we create a temporary texture.
1322 // TODO: we should actually only copy the portion of the source needed to apply the image
1323 // filter
Robert Phillipse2f7d182016-12-15 09:23:05 -05001324 sProxy = GrSurfaceProxy::Copy(fContext.get(),
1325 this->accessRenderTargetContext()->asDeferredSurface(),
1326 SkBudgeted::kYes);
1327 if (!sProxy) {
robertphillips04d62182016-07-15 12:21:33 -07001328 return nullptr;
1329 }
robertphillips1b5f9682016-07-15 08:01:12 -07001330 }
1331
1332 const SkImageInfo ii = this->imageInfo();
1333 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1334
Robert Phillipse2f7d182016-12-15 09:23:05 -05001335 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1336 srcRect,
1337 kNeedNewImageUniqueID_SpecialImage,
1338 sProxy,
1339 sk_ref_sp(ii.colorSpace()),
1340 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001341}
1342
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001344 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001345 SkASSERT(!paint.getImageFilter());
1346
joshualittce894002016-01-11 13:29:31 -08001347 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001349 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001350
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001352 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353
robertphillips1b5f9682016-07-15 08:01:12 -07001354 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001355 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001356 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001357 return;
1358 }
1359
robertphillips1b5f9682016-07-15 08:01:12 -07001360 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361}
1362
reeda85d4d02015-05-06 12:56:48 -07001363void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1364 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001365 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001366 SkMatrix viewMatrix = *draw.fMatrix;
1367 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001368 uint32_t pinnedUniqueID;
1369 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001370 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001371 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001372 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001373 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1374 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001375 return;
reed85d91782015-09-10 14:33:38 -07001376 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001377 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001378 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001379 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001380 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001381 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
reed85d91782015-09-10 14:33:38 -07001382 return;
1383 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001384 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1385 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1386 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001387 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001388 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1389 viewMatrix, fClip, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001390 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001391 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001392 }
reeda85d4d02015-05-06 12:56:48 -07001393 }
1394}
1395
1396void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001397 const SkRect& dst, const SkPaint& paint,
1398 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001399 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001400 uint32_t pinnedUniqueID;
1401 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001402 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001403 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001404 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001405 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001406 return;
1407 }
1408 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001409 SkMatrix srcToDstRect;
1410 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1411 dst, SkMatrix::kFill_ScaleToFit);
1412 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1413 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001414 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001415 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001416 return;
1417 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001418 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1419 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1420 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001421 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001422 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001423 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001424 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001425 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001426}
1427
bsalomonf1ecd212015-12-09 17:06:02 -08001428void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001429 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001430 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001431
joshualitt33a5fce2015-11-18 13:28:51 -08001432 CHECK_SHOULD_DRAW(draw);
1433
joshualittedb36442015-11-19 14:29:30 -08001434 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001435 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001436 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001437 GrSamplerParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001438 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1439 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001440 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001441 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001442
1443 SkRect srcR, dstR;
1444 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001445 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001446 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001447 }
1448 return;
1449 }
1450
Brian Salomon514baff2016-11-17 15:17:07 -05001451 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001452 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001453 producer->createFragmentProcessor(SkMatrix::I(),
1454 SkRect::MakeIWH(producer->width(), producer->height()),
1455 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001456 &kMode, fRenderTargetContext->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -08001457 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001458 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1459 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1460 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001461 return;
1462 }
1463
msarett10e3d9b2016-08-18 15:46:03 -07001464 std::unique_ptr<SkLatticeIter> iter(
1465 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon82f44312017-01-11 13:42:54 -05001466 fRenderTargetContext->drawImageLattice(fClip, std::move(grPaint), *draw.fMatrix,
1467 producer->width(), producer->height(), std::move(iter),
1468 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001469}
1470
1471void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1472 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001473 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001474 uint32_t pinnedUniqueID;
1475 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1476 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001477 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001478 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001479 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001480 } else {
1481 SkBitmap bm;
1482 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001483 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001484 this->drawProducerNine(draw, &maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001485 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001486 this->drawBitmapNine(draw, bm, center, dst, paint);
1487 }
1488 }
1489}
1490
1491void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1492 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001493 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001494 GrBitmapTextureMaker maker(fContext.get(), bitmap);
reedc7ec7c92016-07-25 08:29:10 -07001495 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001496}
1497
msarett10e3d9b2016-08-18 15:46:03 -07001498void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1499 const SkCanvas::Lattice& lattice, const SkRect& dst,
1500 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001501 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001502
1503 CHECK_SHOULD_DRAW(draw);
1504
Brian Salomon514baff2016-11-17 15:17:07 -05001505 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001506 sk_sp<GrFragmentProcessor> fp(
1507 producer->createFragmentProcessor(SkMatrix::I(),
1508 SkRect::MakeIWH(producer->width(), producer->height()),
1509 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001510 &kMode, fRenderTargetContext->getColorSpace()));
msarett10e3d9b2016-08-18 15:46:03 -07001511 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001512 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1513 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1514 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001515 return;
1516 }
1517
1518 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001519 new SkLatticeIter(lattice, dst));
Brian Salomon82f44312017-01-11 13:42:54 -05001520 fRenderTargetContext->drawImageLattice(fClip, std::move(grPaint), *draw.fMatrix,
1521 producer->width(), producer->height(), std::move(iter),
1522 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001523}
1524
1525void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1526 const SkCanvas::Lattice& lattice, const SkRect& dst,
1527 const SkPaint& paint) {
1528 ASSERT_SINGLE_OWNER
1529 uint32_t pinnedUniqueID;
1530 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1531 CHECK_SHOULD_DRAW(draw);
1532 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1533 as_IB(image)->onImageInfo().colorSpace());
1534 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1535 } else {
1536 SkBitmap bm;
1537 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001538 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
msarett10e3d9b2016-08-18 15:46:03 -07001539 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001540 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
msarett10e3d9b2016-08-18 15:46:03 -07001541 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1542 }
1543 }
1544}
1545
1546void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1547 const SkCanvas::Lattice& lattice, const SkRect& dst,
1548 const SkPaint& paint) {
1549 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001550 GrBitmapTextureMaker maker(fContext.get(), bitmap);
msarett10e3d9b2016-08-18 15:46:03 -07001551 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1552}
1553
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001554///////////////////////////////////////////////////////////////////////////////
1555
1556// must be in SkCanvas::VertexMode order
1557static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1558 kTriangles_GrPrimitiveType,
1559 kTriangleStrip_GrPrimitiveType,
1560 kTriangleFan_GrPrimitiveType,
1561};
1562
1563void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1564 int vertexCount, const SkPoint vertices[],
1565 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001566 SkBlendMode bmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001567 const uint16_t indices[], int indexCount,
1568 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001569 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001570 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001571 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001572
halcanary96fcdcc2015-08-27 07:41:13 -07001573 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1574 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001575
halcanary96fcdcc2015-08-27 07:41:13 -07001576 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001577
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001578 SkPaint copy(paint);
1579 copy.setStyle(SkPaint::kStroke_Style);
1580 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001581
bsalomonf1b7a1d2015-09-28 06:26:28 -07001582 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001583 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001584 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1585 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001586 return;
1587 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001588
dandov32a311b2014-07-15 19:46:26 -07001589 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001590 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001591 switch (vmode) {
1592 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001593 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001594 break;
1595 case SkCanvas::kTriangleStrip_VertexMode:
1596 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001597 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001598 break;
1599 }
mtklein533eb782014-08-27 10:39:42 -07001600
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001601 VertState state(vertexCount, indices, indexCount);
1602 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001603
dandov32a311b2014-07-15 19:46:26 -07001604 //number of indices for lines per triangle with kLines
1605 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001606
Ben Wagner7ecc5962016-11-02 17:07:33 -04001607 std::unique_ptr<uint16_t[]> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001608 int i = 0;
1609 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001610 lineIndices[i] = state.f0;
1611 lineIndices[i + 1] = state.f1;
1612 lineIndices[i + 2] = state.f1;
1613 lineIndices[i + 3] = state.f2;
1614 lineIndices[i + 4] = state.f2;
1615 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001616 i += 6;
1617 }
Brian Osman11052242016-10-27 14:47:55 -04001618 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001619 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -04001620 *draw.fMatrix,
1621 kLines_GrPrimitiveType,
1622 vertexCount,
1623 vertices,
1624 texs,
1625 colors,
1626 lineIndices.get(),
1627 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001628 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001629 }
1630
bsalomonf1b7a1d2015-09-28 06:26:28 -07001631 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632
1633 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001634 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001635 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1636 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637 convertedColors.reset(vertexCount);
1638 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001639 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001640 }
1641 colors = convertedColors.get();
1642 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001643 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001644 if (texs && paint.getShader()) {
1645 if (colors) {
Mike Reedfaba3712016-11-03 14:45:31 -04001646 // When there are texs and colors the shader and colors are combined using bmode.
Brian Osman11052242016-10-27 14:47:55 -04001647 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), paint,
Mike Reedfaba3712016-11-03 14:45:31 -04001648 *draw.fMatrix, bmode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001649 return;
1650 }
1651 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001652 // We have a shader, but no colors to blend it against.
Brian Osman11052242016-10-27 14:47:55 -04001653 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001654 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001655 return;
1656 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001657 }
bsalomonaa48d362015-10-01 08:34:17 -07001658 } else {
1659 if (colors) {
1660 // We have colors, but either have no shader or no texture coords (which implies that
1661 // we should ignore the shader).
Brian Osman11052242016-10-27 14:47:55 -04001662 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext.get(),
1663 paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001664 return;
1665 }
1666 } else {
1667 // No colors and no shaders. Just draw with the paint color.
Brian Osman11052242016-10-27 14:47:55 -04001668 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), paint,
1669 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001670 return;
1671 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001672 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001673 }
1674
Brian Osman11052242016-10-27 14:47:55 -04001675 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001676 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -04001677 *draw.fMatrix,
1678 primType,
1679 vertexCount,
1680 vertices,
1681 texs,
1682 colors,
1683 indices,
1684 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685}
1686
1687///////////////////////////////////////////////////////////////////////////////
1688
jvanverth31ff7622015-08-07 10:09:28 -07001689void SkGpuDevice::drawAtlas(const SkDraw& draw, 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()) {
jvanverth31ff7622015-08-07 10:09:28 -07001694 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001695 return;
1696 }
1697
jvanverth31ff7622015-08-07 10:09:28 -07001698 CHECK_SHOULD_DRAW(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 Reed7d954ad2016-10-28 15:42:34 -04001707 *draw.fMatrix, (SkBlendMode)mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001708 return;
1709 }
1710 } else {
Brian Osman11052242016-10-27 14:47:55 -04001711 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, *draw.fMatrix,
1712 &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 Salomon82f44312017-01-11 13:42:54 -05001718 fRenderTargetContext->drawAtlas(fClip, std::move(grPaint), *draw.fMatrix, count, xform, texRect,
1719 colors);
reedca109532015-06-25 16:25:25 -07001720}
1721
1722///////////////////////////////////////////////////////////////////////////////
1723
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724void SkGpuDevice::drawText(const SkDraw& draw, 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
joshualitt5531d512014-12-17 15:50:11 -08001728 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001729 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730
jvanverth8c27a182014-10-14 08:45:50 -07001731 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001732 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1733 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001734 return;
1735 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001736
jvanverth8c27a182014-10-14 08:45:50 -07001737 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001738
Brian Salomon82f44312017-01-11 13:42:54 -05001739 fRenderTargetContext->drawText(fClip, std::move(grPaint), paint, *draw.fMatrix,
1740 (const char*)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741}
1742
fmalita05c4a432014-09-29 06:29:53 -07001743void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1744 const SkScalar pos[], int scalarsPerPos,
1745 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001746 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001747 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -08001748 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001749
jvanverth8c27a182014-10-14 08:45:50 -07001750 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001751 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1752 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001753 return;
1754 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001755
jvanverth8c27a182014-10-14 08:45:50 -07001756 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001757
Brian Salomon82f44312017-01-11 13:42:54 -05001758 fRenderTargetContext->drawPosText(fClip, std::move(grPaint), paint, *draw.fMatrix,
1759 (const char*)text, byteLength, pos, scalarsPerPos, offset,
Brian Osman11052242016-10-27 14:47:55 -04001760 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761}
1762
joshualitt9c328182015-03-23 08:13:04 -07001763void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1764 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001765 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001766 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001767 CHECK_SHOULD_DRAW(draw);
1768
1769 SkDEBUGCODE(this->validate();)
1770
Brian Osman11052242016-10-27 14:47:55 -04001771 fRenderTargetContext->drawTextBlob(fClip, paint, *draw.fMatrix,
1772 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001773}
1774
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775///////////////////////////////////////////////////////////////////////////////
1776
reedb2db8982014-11-13 12:41:02 -08001777bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001778 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001779}
1780
1781void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001782 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001783
Brian Osman11052242016-10-27 14:47:55 -04001784 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001785}
1786
1787///////////////////////////////////////////////////////////////////////////////
1788
reed76033be2015-03-14 10:54:31 -07001789SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001790 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791
robertphillipsca6eafc2016-05-17 09:57:46 -07001792 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001793
robertphillipsca6eafc2016-05-17 09:57:46 -07001794 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001795 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001796 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1797 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001798
Brian Osman11052242016-10-27 14:47:55 -04001799 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1800 fit,
1801 cinfo.fInfo.width(), cinfo.fInfo.height(),
1802 fRenderTargetContext->config(),
1803 sk_ref_sp(fRenderTargetContext->getColorSpace()),
1804 fRenderTargetContext->desc().fSampleCnt,
1805 kDefault_GrSurfaceOrigin,
1806 &props));
1807 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001808 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001810
1811 // Skia's convention is to only clear a device if it is non-opaque.
1812 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1813
Robert Phillips9fab7e92016-11-17 12:45:04 -05001814 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1815 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001816}
1817
reede8f30622016-03-23 18:59:25 -07001818sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001819 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001820 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001821 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001822 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Osman11052242016-10-27 14:47:55 -04001823 fRenderTargetContext->desc().fSampleCnt,
1824 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001825}
1826
senorblanco900c3672016-04-27 11:31:23 -07001827SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001828 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001829 // We always return a transient cache, so it is freed after each
1830 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001831 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001832}
reedf037e0b2014-10-30 11:34:15 -07001833
1834#endif