blob: 3e94d32956449db9ce3fd4892b51c0c521cfb551 [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
reede5ea5002014-09-03 11:54:58 -0700166 SkColorType ct = origInfo.colorType();
167 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700168 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700169 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700170 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800171 }
172 if (kOpaque_SkAlphaType != at) {
173 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000174 }
brianosman6b086522016-04-14 12:39:00 -0700175
brianosmanb109b8c2016-06-16 13:03:24 -0700176 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177
Brian Osman11052242016-10-27 14:47:55 -0400178 return context->makeRenderTargetContext(SkBackingFit::kExact, // Why exact?
robertphillips6738c702016-07-27 12:13:51 -0700179 origInfo.width(), origInfo.height(),
180 config, sk_ref_sp(cs), sampleCount,
181 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800182}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000183
robertphillips970587b2016-07-14 14:12:55 -0700184sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
185 SkSpecialImage* srcImg,
186 int left, int top,
187 SkIPoint* offset,
188 const SkImageFilter* filter) {
189 SkASSERT(srcImg->isTextureBacked());
190 SkASSERT(filter);
191
192 SkMatrix matrix = *draw.fMatrix;
193 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
194 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500195 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400196 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700197 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700198
199 return filter->filterImage(srcImg, ctx, offset);
200}
201
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000202///////////////////////////////////////////////////////////////////////////////
203
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000204bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
205 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800206 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000207
Brian Osman11052242016-10-27 14:47:55 -0400208 return fRenderTargetContext->readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000209}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000210
robertphillips1da3ecd2016-08-31 14:54:15 -0700211bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
212 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800213 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700214
Brian Osman11052242016-10-27 14:47:55 -0400215 return fRenderTargetContext->writePixels(srcInfo, srcPixels, srcRowBytes, x, y);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000216}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000217
reed41e010c2015-06-09 12:16:53 -0700218bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800219 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700220 return false;
221}
222
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223// call this every draw call, to ensure that the context reflects our state,
224// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800225void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800226 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000227
reed2c9e2002016-07-25 08:05:22 -0700228 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229}
230
Brian Osman11052242016-10-27 14:47:55 -0400231GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700232 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400233 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700234}
235
reed8eddfb52014-12-04 07:50:14 -0800236void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800237 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500238 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500239
reed8eddfb52014-12-04 07:50:14 -0800240 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500241 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800242}
243
Brian Osman11052242016-10-27 14:47:55 -0400244void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800245 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800246
Brian Osman693a5402016-10-27 15:13:22 -0400247 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800248
Brian Osman693a5402016-10-27 15:13:22 -0400249 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400250 this->context(),
251 budgeted,
252 this->imageInfo(),
253 fRenderTargetContext->numColorSamples(),
254 fRenderTargetContext->origin(),
255 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400256 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800257 return;
258 }
259
260 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400261 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800262 return;
263 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500264 newRTC->copy(fRenderTargetContext->asDeferredSurface());
kkinnunenabcfab42015-02-22 22:53:44 -0800265 }
266
Brian Osman693a5402016-10-27 15:13:22 -0400267 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800268}
269
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270///////////////////////////////////////////////////////////////////////////////
271
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800273 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800274 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500275 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276
277 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400278 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
279 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700280 return;
281 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
Brian Salomon82f44312017-01-11 13:42:54 -0500283 fRenderTargetContext->drawPaint(fClip, std::move(grPaint), *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284}
285
286// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700287static const GrPrimitiveType gPointMode2PrimitiveType[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288 kPoints_GrPrimitiveType,
289 kLines_GrPrimitiveType,
290 kLineStrip_GrPrimitiveType
291};
292
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500293static inline bool is_int(float x) { return x == (float) sk_float_round2int(x); }
294
ethannicholas330bb952015-07-17 06:44:02 -0700295// suppress antialiasing on axis-aligned integer-coordinate lines
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500296static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
297 const SkMatrix& matrix) {
ethannicholas330bb952015-07-17 06:44:02 -0700298 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
299 return false;
300 }
301 if (count == 2) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500302 // We do not antialias horizontal or vertical lines along pixel centers, even when the ends
303 // of the line do not fully cover the first and last pixel of the line, which is slightly
304 // wrong.
305 if (!matrix.isScaleTranslate()) {
306 return true;
307 }
ethannicholas330bb952015-07-17 06:44:02 -0700308 if (pts[0].fX == pts[1].fX) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500309 SkScalar x = matrix.getScaleX() * pts[0].fX + matrix.getTranslateX();
310 return !is_int(x + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700311 }
312 if (pts[0].fY == pts[1].fY) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500313 SkScalar y = matrix.getScaleY() * pts[0].fY + matrix.getTranslateY();
314 return !is_int(y + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700315 }
316 }
317 return true;
318}
319
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
321 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800322 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500323 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800324 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000325
326 SkScalar width = paint.getStrokeWidth();
327 if (width < 0) {
328 return;
329 }
330
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000331 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700332 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700333 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400334 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700335 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700336 return;
337 }
egdaniele61c4112014-06-12 10:24:21 -0700338 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700339 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700340 path.moveTo(pts[0]);
341 path.lineTo(pts[1]);
Brian Salomon82f44312017-01-11 13:42:54 -0500342 fRenderTargetContext->drawPath(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500343 *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700344 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000345 }
346
bsalomon6ade6dd2016-09-12 12:07:17 -0700347 SkScalar scales[2];
348 bool isHairline = (0 == width) || (1 == width && draw.fMatrix->getMinMaxScales(scales) &&
349 SkScalarNearlyEqual(scales[0], 1.f) &&
350 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700351 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000352 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700353 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500354 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts, *draw.fMatrix))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355 draw.drawPoints(mode, count, pts, paint, true);
356 return;
357 }
358
bsalomon6ade6dd2016-09-12 12:07:17 -0700359 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
360
361 const SkMatrix* viewMatrix = draw.fMatrix;
362#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
363 // This offsetting in device space matches the expectations of the Android framework for non-AA
364 // points and lines.
365 SkMatrix tempMatrix;
366 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
367 tempMatrix = *viewMatrix;
368 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
369 tempMatrix.postTranslate(kOffset, kOffset);
370 viewMatrix = &tempMatrix;
371 }
372#endif
373
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000374 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400375 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
376 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700377 return;
378 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379
Brian Osman11052242016-10-27 14:47:55 -0400380 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500381 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -0400382 *viewMatrix,
383 primitiveType,
384 SkToS32(count),
385 (SkPoint*)pts,
386 nullptr,
387 nullptr,
388 nullptr,
389 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390}
391
392///////////////////////////////////////////////////////////////////////////////
393
robertphillipsff55b492015-11-24 07:56:59 -0800394void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800395 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500396 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800397 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000399
bsalomona7d85ba2016-07-06 11:54:59 -0700400 // A couple reasons we might need to call drawPath.
401 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700403 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500405 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800406 fClip, path, paint,
407 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700408 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409 return;
410 }
411
412 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400413 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
414 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700415 return;
416 }
Mike Klein744fb732014-06-23 15:13:26 -0400417
bsalomon6663acf2016-05-10 09:14:17 -0700418 GrStyle style(paint);
Brian Salomon82f44312017-01-11 13:42:54 -0500419 fRenderTargetContext->drawRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
420 *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000421}
422
423///////////////////////////////////////////////////////////////////////////////
424
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500425void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800426 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500427 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800428 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000430 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400431 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
432 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700433 return;
434 }
Mike Klein744fb732014-06-23 15:13:26 -0400435
Robert Phillipsa29a9562016-10-20 09:40:55 -0400436 SkMaskFilter* mf = paint.getMaskFilter();
437 if (mf && mf->asFragmentProcessor(nullptr, nullptr, *draw.fMatrix)) {
438 mf = nullptr; // already handled in SkPaintToGrPaint
439 }
440
bsalomon6663acf2016-05-10 09:14:17 -0700441 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400442 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000443 // try to hit the fast path for drawing filtered round rects
444
445 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700446 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000447 if (devRRect.allCornersCircular()) {
448 SkRect maskRect;
Robert Phillipsa29a9562016-10-20 09:40:55 -0400449 if (mf->canFilterMaskGPU(devRRect, draw.fRC->getBounds(),
450 *draw.fMatrix, &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000451 SkIRect finalIRect;
452 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700453 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000454 // clipped out
455 return;
456 }
Hal Canary144caf52016-11-07 17:57:18 -0500457 if (mf->directFilterRRectMaskGPU(fContext.get(), fRenderTargetContext.get(),
Brian Salomon82f44312017-01-11 13:42:54 -0500458 std::move(grPaint), fClip, *draw.fMatrix,
Hal Canary144caf52016-11-07 17:57:18 -0500459 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000460 return;
461 }
462 }
463
464 }
465 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000466 }
467
Robert Phillipsa29a9562016-10-20 09:40:55 -0400468 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800469 // The only mask filter the native rrect drawing code could've handle was taken
470 // care of above.
471 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000472 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700473 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700474 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500475 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800476 fClip, path, paint,
477 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700478 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479 return;
480 }
Mike Klein744fb732014-06-23 15:13:26 -0400481
bsalomon6663acf2016-05-10 09:14:17 -0700482 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800483
Brian Salomon82f44312017-01-11 13:42:54 -0500484 fRenderTargetContext->drawRRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
485 *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000486}
487
robertphillipsd7706102016-02-25 09:28:08 -0800488
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000489void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800490 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800491 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500492 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillips0e7029e2015-11-30 05:45:06 -0800493 CHECK_SHOULD_DRAW(draw);
494
robertphillipsd7706102016-02-25 09:28:08 -0800495 if (outer.isEmpty()) {
496 return;
497 }
498
499 if (inner.isEmpty()) {
500 return this->drawRRect(draw, outer, paint);
501 }
502
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000503 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000504
robertphillips0e7029e2015-11-30 05:45:06 -0800505 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800506 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400507 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700508 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700509 return;
510 }
robertphillips00095892016-02-29 13:50:40 -0800511
Brian Salomon82f44312017-01-11 13:42:54 -0500512 fRenderTargetContext->drawDRRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500513 *draw.fMatrix, outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800514 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000515 }
516
517 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700518 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000519 path.addRRect(outer);
520 path.addRRect(inner);
521 path.setFillType(SkPath::kEvenOdd_FillType);
522
Hal Canary144caf52016-11-07 17:57:18 -0500523 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800524 fClip, path, paint,
525 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700526 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000527}
528
529
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000530/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531
msarettcc319b92016-08-25 18:07:18 -0700532void SkGpuDevice::drawRegion(const SkDraw& draw, const SkRegion& region, const SkPaint& paint) {
533 if (paint.getMaskFilter()) {
534 SkPath path;
535 region.getBoundaryPath(&path);
536 return this->drawPath(draw, path, paint, nullptr, false);
537 }
538
539 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400540 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
541 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700542 return;
543 }
544
Brian Salomon82f44312017-01-11 13:42:54 -0500545 fRenderTargetContext->drawRegion(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
546 *draw.fMatrix, region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700547}
548
robertphillips514450c2015-11-24 05:36:02 -0800549void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800550 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500551 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800552 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553
robertphillips514450c2015-11-24 05:36:02 -0800554 // Presumably the path effect warps this to something other than an oval
555 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700557 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000558 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700559 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000560 return;
herb11a7f7f2015-11-24 12:41:00 -0800561 }
562
robertphillips514450c2015-11-24 05:36:02 -0800563 if (paint.getMaskFilter()) {
564 // The RRect path can handle special case blurring
565 SkRRect rr = SkRRect::MakeOval(oval);
566 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567 }
568
569 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400570 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
571 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700572 return;
573 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000574
Brian Salomon82f44312017-01-11 13:42:54 -0500575 fRenderTargetContext->drawOval(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
576 *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000577}
578
bsalomon4f3a0ca2016-08-22 13:14:26 -0700579void SkGpuDevice::drawArc(const SkDraw& draw, const SkRect& oval, SkScalar startAngle,
580 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
581 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500582 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700583 CHECK_SHOULD_DRAW(draw);
584
585 if (paint.getMaskFilter()) {
586 this->INHERITED::drawArc(draw, oval, startAngle, sweepAngle, useCenter, paint);
587 return;
588 }
589 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400590 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
591 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700592 return;
593 }
594
Brian Salomon82f44312017-01-11 13:42:54 -0500595 fRenderTargetContext->drawArc(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
596 *draw.fMatrix, oval, startAngle, sweepAngle, useCenter,
597 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700598}
599
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601
602///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700603void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
604 const SkDraw& draw,
605 const SkPaint& origPaint) {
606 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500607 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
robertphillips0851d2d2016-06-02 05:21:34 -0700608 CHECK_SHOULD_DRAW(draw);
609
Brian Osman11052242016-10-27 14:47:55 -0400610 // Adding support for round capping would require a
611 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700612 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
613 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
614 SkASSERT(!origPaint.getPathEffect());
615 SkASSERT(!origPaint.getMaskFilter());
616
617 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
618 SkASSERT(halfWidth > 0);
619
620 SkVector v = points[1] - points[0];
621
622 SkScalar length = SkPoint::Normalize(&v);
623 if (!length) {
624 v.fX = 1.0f;
625 v.fY = 0.0f;
626 }
627
628 SkPaint newPaint(origPaint);
629 newPaint.setStyle(SkPaint::kFill_Style);
630
631 SkScalar xtraLength = 0.0f;
632 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
633 xtraLength = halfWidth;
634 }
635
636 SkPoint mid = points[0] + points[1];
637 mid.scale(0.5f);
638
639 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
640 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
641 SkMatrix m;
642 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
643
644 SkMatrix local = m;
645
646 m.postConcat(*draw.fMatrix);
647
648 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400649 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700650 return;
651 }
652
Brian Salomon82f44312017-01-11 13:42:54 -0500653 fRenderTargetContext->fillRectWithLocalMatrix(
654 fClip, std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700655}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000657void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
658 const SkPaint& paint, const SkMatrix* prePathMatrix,
659 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800660 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800661 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700662 SkPoint points[2];
663 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
664 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
665 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
666 // Path-based stroking looks better for thin rects
667 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700668 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500669 // Round capping support is currently disabled b.c. it would require a RRect
670 // GrDrawOp that takes a localMatrix.
robertphillips0851d2d2016-06-02 05:21:34 -0700671 this->drawStrokedLine(points, draw, paint);
672 return;
673 }
674 }
robertphillipsff55b492015-11-24 07:56:59 -0800675 bool isClosed;
676 SkRect rect;
677 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
678 this->drawRect(draw, rect, paint);
679 return;
680 }
681 if (origSrcPath.isOval(&rect)) {
682 this->drawOval(draw, rect, paint);
683 return;
684 }
685 SkRRect rrect;
686 if (origSrcPath.isRRect(&rrect)) {
687 this->drawRRect(draw, rrect, paint);
688 return;
689 }
690 }
691
joshualitt5531d512014-12-17 15:50:11 -0800692 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500693 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000694
Hal Canary144caf52016-11-07 17:57:18 -0500695 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700696 fClip, origSrcPath, paint,
697 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700698 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699}
700
701static const int kBmpSmallTileSize = 1 << 10;
702
703static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
704 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
705 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
706 return tilesX * tilesY;
707}
708
reed85d91782015-09-10 14:33:38 -0700709static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710 if (maxTileSize <= kBmpSmallTileSize) {
711 return maxTileSize;
712 }
713
714 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
715 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
716
717 maxTileTotalTileSize *= maxTileSize * maxTileSize;
718 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
719
720 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
721 return kBmpSmallTileSize;
722 } else {
723 return maxTileSize;
724 }
725}
726
727// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
728// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700729static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800730 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800731 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700732 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700733 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 const SkRect* srcRectPtr,
735 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700736 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700737 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
738 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739 clippedSrcIRect->setEmpty();
740 return;
741 }
742 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
743 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700744 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745 if (!clippedSrcRect.intersect(*srcRectPtr)) {
746 clippedSrcIRect->setEmpty();
747 return;
748 }
749 }
750 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700751 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000752 if (!clippedSrcIRect->intersect(bmpBounds)) {
753 clippedSrcIRect->setEmpty();
754 }
755}
756
reed85d91782015-09-10 14:33:38 -0700757bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
758 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700759 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500760 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700761 const SkRect* srcRectPtr,
762 int maxTileSize,
763 int* tileSize,
764 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800765 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700766 // if it's larger than the max tile size, then we have no choice but tiling.
767 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400768 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
769 fClip, viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
770 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700771 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
772 return true;
773 }
774
bsalomon1a1d0b82015-10-16 07:49:42 -0700775 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700776 const size_t area = imageRect.width() * imageRect.height();
777 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
778 return false;
779 }
780
reed85d91782015-09-10 14:33:38 -0700781 // At this point we know we could do the draw by uploading the entire bitmap
782 // as a texture. However, if the texture would be large compared to the
783 // cache size and we don't require most of it for this draw then tile to
784 // reduce the amount of upload and cache spill.
785
786 // assumption here is that sw bitmap size is a good proxy for its size as
787 // a texture
788 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
789 size_t cacheSize;
790 fContext->getResourceCacheLimits(nullptr, &cacheSize);
791 if (bmpSize < cacheSize / 2) {
792 return false;
793 }
794
bsalomon1a1d0b82015-10-16 07:49:42 -0700795 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
796 // tiling memory savings would be < 50%.
Brian Osman11052242016-10-27 14:47:55 -0400797 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(), fClip,
798 viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
799 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700800 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
801 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
802 kBmpSmallTileSize * kBmpSmallTileSize;
803
804 return usedTileBytes < 2 * bmpSize;
805}
806
reed85d91782015-09-10 14:33:38 -0700807bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
808 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700809 const SkMatrix& viewMatrix,
810 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800811 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700812 // if image is explictly texture backed then just use the texture
813 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814 return false;
815 }
816
Brian Salomon514baff2016-11-17 15:17:07 -0500817 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700818 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500819 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700820 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700821
822 int tileFilterPad;
823 if (doBicubic) {
824 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500825 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700826 tileFilterPad = 0;
827 } else {
828 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829 }
reed85d91782015-09-10 14:33:38 -0700830 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831
bsalomon8c07b7a2015-11-02 11:36:52 -0800832 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833
reed85d91782015-09-10 14:33:38 -0700834 // these are output, which we safely ignore, as we just want to know the predicate
835 int outTileSize;
836 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837
bsalomone553b642016-08-17 09:02:09 -0700838 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
839 params, srcRectPtr, maxTileSize, &outTileSize,
840 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841}
842
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000843void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844 const SkBitmap& bitmap,
845 const SkMatrix& m,
846 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800847 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800848 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800849 SkMatrix viewMatrix;
850 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700851
bsalomonb1b01992015-11-18 10:56:08 -0800852 int maxTileSize = fContext->caps()->maxTileSize();
853
854 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
855 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -0400856 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800857 paint.isAntiAlias() &&
858 bitmap.width() <= maxTileSize &&
859 bitmap.height() <= maxTileSize;
860
861 bool skipTileCheck = drawAA || paint.getMaskFilter();
862
863 if (!skipTileCheck) {
864 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
865 int tileSize;
866 SkIRect clippedSrcRect;
867
Brian Salomon514baff2016-11-17 15:17:07 -0500868 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800869 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500870 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800871 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
872 &doBicubic);
873
874 int tileFilterPad;
875
876 if (doBicubic) {
877 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500878 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800879 tileFilterPad = 0;
880 } else {
881 tileFilterPad = 1;
882 }
883 params.setFilterMode(textureFilterMode);
884
885 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700886 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
887 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
888 &tileSize, &clippedSrcRect)) {
889 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
890 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
891 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800892 return;
893 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000894 }
Hal Canary144caf52016-11-07 17:57:18 -0500895 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800896 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
897 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898}
899
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000900// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
902// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000903static inline void clamped_outset_with_offset(SkIRect* iRect,
904 int outset,
905 SkPoint* offset,
906 const SkIRect& clamp) {
907 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000908
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000909 int leftClampDelta = clamp.fLeft - iRect->fLeft;
910 if (leftClampDelta > 0) {
911 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 iRect->fLeft = clamp.fLeft;
913 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000914 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000915 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000916
917 int topClampDelta = clamp.fTop - iRect->fTop;
918 if (topClampDelta > 0) {
919 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 iRect->fTop = clamp.fTop;
921 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000922 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923 }
924
925 if (iRect->fRight > clamp.fRight) {
926 iRect->fRight = clamp.fRight;
927 }
928 if (iRect->fBottom > clamp.fBottom) {
929 iRect->fBottom = clamp.fBottom;
930 }
931}
932
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933// Break 'bitmap' into several tiles to draw it since it has already
934// been determined to be too large to fit in VRAM
935void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800936 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700937 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938 const SkRect& srcRect,
939 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500940 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800941 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700942 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000943 int tileSize,
944 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800945 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800946
ericrk983294f2016-04-18 09:14:00 -0700947 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800948 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700949 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800950
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000951 // The following pixel lock is technically redundant, but it is desirable
952 // to lock outside of the tile loop to prevent redecoding the whole image
953 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
954 // is larger than the limit of the discardable memory pool.
955 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800956
957 const SkPaint* paint = &origPaint;
958 SkPaint tempPaint;
Brian Osman11052242016-10-27 14:47:55 -0400959 if (origPaint.isAntiAlias() && !fRenderTargetContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800960 // Drop antialiasing to avoid seams at tile boundaries.
961 tempPaint = origPaint;
962 tempPaint.setAntiAlias(false);
963 paint = &tempPaint;
964 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
966
967 int nx = bitmap.width() / tileSize;
968 int ny = bitmap.height() / tileSize;
969 for (int x = 0; x <= nx; x++) {
970 for (int y = 0; y <= ny; y++) {
971 SkRect tileR;
972 tileR.set(SkIntToScalar(x * tileSize),
973 SkIntToScalar(y * tileSize),
974 SkIntToScalar((x + 1) * tileSize),
975 SkIntToScalar((y + 1) * tileSize));
976
977 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
978 continue;
979 }
980
981 if (!tileR.intersect(srcRect)) {
982 continue;
983 }
984
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 SkIRect iTileR;
986 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700987 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
988 SkIntToScalar(iTileR.fTop));
989 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
990 tileR.width(), tileR.height());
991 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500992 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 SkIRect iClampRect;
994
reeda5517e22015-07-14 10:54:12 -0700995 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 // In bleed mode we want to always expand the tile on all edges
997 // but stay within the bitmap bounds
998 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
999 } else {
1000 // In texture-domain/clamp mode we only want to expand the
1001 // tile on edges interior to "srcRect" (i.e., we want to
1002 // not bleed across the original clamped edges)
1003 srcRect.roundOut(&iClampRect);
1004 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001005 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1006 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001007 }
1008
bsalomone553b642016-08-17 09:02:09 -07001009 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010 if (bitmap.extractSubset(&tmpB, iTileR)) {
1011 // now offset it to make it "local" to our tmp bitmap
1012 tileR.offset(-offset.fX, -offset.fY);
Brian Salomon514baff2016-11-17 15:17:07 -05001013 GrSamplerParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001014 // de-optimized this determination
1015 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001016 this->drawBitmapTile(tmpB,
1017 viewMatrix,
1018 rectToDraw,
1019 tileR,
1020 paramsTemp,
1021 *paint,
1022 constraint,
1023 bicubic,
1024 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001025 }
1026 }
1027 }
1028}
1029
bsalomone553b642016-08-17 09:02:09 -07001030void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1031 const SkMatrix& viewMatrix,
1032 const SkRect& dstRect,
1033 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001034 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001035 const SkPaint& paint,
1036 SkCanvas::SrcRectConstraint constraint,
1037 bool bicubic,
1038 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001039 // We should have already handled bitmaps larger than the max texture size.
1040 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1041 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001042 // We should be respecting the max tile size by the time we get here.
1043 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1044 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045
Brian Osman61624f02016-12-09 14:51:59 -05001046 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap, params);
halcanary96fcdcc2015-08-27 07:41:13 -07001047 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048 return;
1049 }
brianosman500bb3e2016-07-22 10:33:07 -07001050 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001051 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001052
1053 SkScalar iw = 1.f / texture->width();
1054 SkScalar ih = 1.f / texture->height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055
egdaniel79da63f2015-10-09 10:55:16 -07001056 SkMatrix texMatrix;
bsalomone553b642016-08-17 09:02:09 -07001057 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomond93f4a42016-11-14 14:41:58 -05001058 texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
bsalomone553b642016-08-17 09:02:09 -07001059 texMatrix.postScale(iw, ih);
joshualitt5f10b5c2015-07-09 10:24:35 -07001060
1061 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1062 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001063 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001064
reeda5517e22015-07-14 10:54:12 -07001065 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001067 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001069 domain.fLeft = srcRect.fLeft + 0.5f;
1070 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001071 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001072 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073 }
1074 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001075 domain.fTop = srcRect.fTop + 0.5f;
1076 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001078 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001080 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001081 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001082 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001083 } else {
reedc7ec7c92016-07-25 08:29:10 -07001084 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001085 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001086 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001087 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001088 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001089 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001090 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001091 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 } else {
reedc7ec7c92016-07-25 08:29:10 -07001093 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 }
1095
joshualitt33a5fce2015-11-18 13:28:51 -08001096 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001097 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001098 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1099 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001100 return;
1101 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001103 // Coverage-based AA would cause seams between tiles.
1104 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
1105 fRenderTargetContext->isStencilBufferMultisampled());
Brian Salomon82f44312017-01-11 13:42:54 -05001106 fRenderTargetContext->drawRect(fClip, std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107}
1108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1110 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001111 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001112 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001113 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114
robertphillips970587b2016-07-14 14:12:55 -07001115 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 return;
1117 }
1118
reedc7ec7c92016-07-25 08:29:10 -07001119 sk_sp<GrTexture> texture;
1120 {
robertphillips970587b2016-07-14 14:12:55 -07001121 SkAutoLockPixels alp(bitmap, true);
1122 if (!bitmap.readyToDraw()) {
1123 return;
1124 }
1125
1126 // draw sprite neither filters nor tiles.
Brian Osman7b8400d2016-11-08 17:08:54 -05001127 texture.reset(
Brian Osman61624f02016-12-09 14:51:59 -05001128 GrRefCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter()));
robertphillips970587b2016-07-14 14:12:55 -07001129 if (!texture) {
1130 return;
1131 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001132 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133
robertphillips970587b2016-07-14 14:12:55 -07001134 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1135 bitmap.pixelRefOrigin().fY,
1136 bitmap.width(),
1137 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001138
robertphillips970587b2016-07-14 14:12:55 -07001139 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1140 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001141 std::move(texture),
1142 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001143 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001144
1145 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1146}
1147
1148
Mike Kleine54c75f2016-10-13 14:18:09 -04001149void SkGpuDevice::drawSpecial(const SkDraw& draw,
robertphillips970587b2016-07-14 14:12:55 -07001150 SkSpecialImage* special1,
1151 int left, int top,
1152 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001153 ASSERT_SINGLE_OWNER
1154 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001155 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001156
1157 SkIPoint offset = { 0, 0 };
1158
1159 sk_sp<SkSpecialImage> result;
1160 if (paint.getImageFilter()) {
1161 result = this->filterTexture(draw, special1, left, top,
1162 &offset,
1163 paint.getImageFilter());
1164 if (!result) {
1165 return;
1166 }
1167 } else {
1168 result = sk_ref_sp(special1);
1169 }
1170
1171 SkASSERT(result->isTextureBacked());
Hal Canary144caf52016-11-07 17:57:18 -05001172 sk_sp<GrTexture> texture = result->asTextureRef(fContext.get());
Robert Phillips833dcf42016-11-18 08:44:13 -05001173 if (!texture) {
1174 return;
1175 }
robertphillips970587b2016-07-14 14:12:55 -07001176
1177 SkPaint tmpUnfiltered(paint);
1178 tmpUnfiltered.setImageFilter(nullptr);
1179
brianosman77320db2016-09-07 08:09:10 -07001180 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001181 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001182 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001183 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1184 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001185 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001186 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001187 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001188 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001189 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001190 }
Brian Osman11052242016-10-27 14:47:55 -04001191 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001192 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001193 return;
1194 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195
robertphillips970587b2016-07-14 14:12:55 -07001196 const SkIRect& subset = result->subset();
1197
Brian Salomon82f44312017-01-11 13:42:54 -05001198 fRenderTargetContext->fillRectToRect(
1199 fClip,
1200 std::move(grPaint),
1201 GrBoolToAA(paint.isAntiAlias()),
1202 SkMatrix::I(),
1203 SkRect::Make(SkIRect::MakeXYWH(
1204 left + offset.fX, top + offset.fY, subset.width(), subset.height())),
1205 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1206 SkIntToScalar(subset.fTop) / texture->height(),
1207 SkIntToScalar(subset.width()) / texture->width(),
1208 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001209}
1210
bsalomonb1b01992015-11-18 10:56:08 -08001211void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1212 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001213 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001214 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001215 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001216
bsalomonb1b01992015-11-18 10:56:08 -08001217 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1218 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1219 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1220 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1221 const SkRect* dst = &origDst;
1222 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001224 if (!src) {
1225 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001227
bsalomonb1b01992015-11-18 10:56:08 -08001228 SkMatrix srcToDstMatrix;
1229 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1230 return;
1231 }
1232 SkRect tmpSrc, tmpDst;
1233 if (src != &bmpBounds) {
1234 if (!bmpBounds.contains(*src)) {
1235 tmpSrc = *src;
1236 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001237 return; // nothing to draw
1238 }
bsalomonb1b01992015-11-18 10:56:08 -08001239 src = &tmpSrc;
1240 srcToDstMatrix.mapRect(&tmpDst, *src);
1241 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 }
1243 }
1244
bsalomonb1b01992015-11-18 10:56:08 -08001245 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001246
bsalomonb1b01992015-11-18 10:56:08 -08001247 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1248 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001249 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001250 paint.isAntiAlias() &&
1251 bitmap.width() <= maxTileSize &&
1252 bitmap.height() <= maxTileSize;
1253
1254 bool skipTileCheck = drawAA || paint.getMaskFilter();
1255
1256 if (!skipTileCheck) {
1257 int tileSize;
1258 SkIRect clippedSrcRect;
1259
Brian Salomon514baff2016-11-17 15:17:07 -05001260 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001261 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001262 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -08001263 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1264 &doBicubic);
1265
1266 int tileFilterPad;
1267
1268 if (doBicubic) {
1269 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001270 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001271 tileFilterPad = 0;
1272 } else {
1273 tileFilterPad = 1;
1274 }
1275 params.setFilterMode(textureFilterMode);
1276
1277 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001278 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1279 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1280 &clippedSrcRect)) {
1281 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1282 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001283 return;
1284 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001285 }
Hal Canary144caf52016-11-07 17:57:18 -05001286 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001287 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288}
1289
robertphillips6451a0c2016-07-18 08:31:31 -07001290sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001291 SkAutoLockPixels alp(bitmap, true);
1292 if (!bitmap.readyToDraw()) {
1293 return nullptr;
1294 }
1295
Brian Osman7b8400d2016-11-08 17:08:54 -05001296 sk_sp<GrTexture> texture =
Brian Osman61624f02016-12-09 14:51:59 -05001297 GrMakeCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter());
robertphillips6451a0c2016-07-18 08:31:31 -07001298 if (!texture) {
1299 return nullptr;
1300 }
1301
1302 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1303 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001304 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001305 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001306 &this->surfaceProps());
1307}
1308
reede51c3562016-07-19 14:33:20 -07001309sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001310 SkPixmap pm;
1311 if (image->isTextureBacked()) {
1312 GrTexture* texture = as_IB(image)->peekTexture();
1313
1314 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1315 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001316 sk_ref_sp(texture),
1317 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001318 &this->surfaceProps());
1319 } else if (image->peekPixels(&pm)) {
1320 SkBitmap bm;
1321
1322 bm.installPixels(pm);
1323 return this->makeSpecial(bm);
1324 } else {
1325 return nullptr;
1326 }
1327}
1328
1329sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillipse2f7d182016-12-15 09:23:05 -05001330 sk_sp<GrSurfaceProxy> sProxy(sk_ref_sp(this->accessRenderTargetContext()->asDeferredTexture()));
1331 if (!sProxy) {
robertphillips04d62182016-07-15 12:21:33 -07001332 // When the device doesn't have a texture, we create a temporary texture.
1333 // TODO: we should actually only copy the portion of the source needed to apply the image
1334 // filter
Robert Phillipse2f7d182016-12-15 09:23:05 -05001335 sProxy = GrSurfaceProxy::Copy(fContext.get(),
1336 this->accessRenderTargetContext()->asDeferredSurface(),
1337 SkBudgeted::kYes);
1338 if (!sProxy) {
robertphillips04d62182016-07-15 12:21:33 -07001339 return nullptr;
1340 }
robertphillips1b5f9682016-07-15 08:01:12 -07001341 }
1342
1343 const SkImageInfo ii = this->imageInfo();
1344 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1345
Robert Phillipse2f7d182016-12-15 09:23:05 -05001346 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1347 srcRect,
1348 kNeedNewImageUniqueID_SpecialImage,
1349 sProxy,
1350 sk_ref_sp(ii.colorSpace()),
1351 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001352}
1353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001355 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001356 SkASSERT(!paint.getImageFilter());
1357
joshualittce894002016-01-11 13:29:31 -08001358 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001360 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001361
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001363 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364
robertphillips1b5f9682016-07-15 08:01:12 -07001365 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001366 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001367 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001368 return;
1369 }
1370
robertphillips1b5f9682016-07-15 08:01:12 -07001371 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372}
1373
reeda85d4d02015-05-06 12:56:48 -07001374void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1375 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001376 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001377 SkMatrix viewMatrix = *draw.fMatrix;
1378 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001379 uint32_t pinnedUniqueID;
1380 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001381 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001382 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001383 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001384 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1385 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001386 return;
reed85d91782015-09-10 14:33:38 -07001387 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001388 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001389 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001390 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001391 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001392 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
reed85d91782015-09-10 14:33:38 -07001393 return;
1394 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001395 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1396 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1397 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001398 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001399 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1400 viewMatrix, fClip, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001401 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001402 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001403 }
reeda85d4d02015-05-06 12:56:48 -07001404 }
1405}
1406
1407void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001408 const SkRect& dst, const SkPaint& paint,
1409 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001410 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001411 uint32_t pinnedUniqueID;
1412 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001413 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001414 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001415 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001416 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001417 return;
1418 }
1419 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001420 SkMatrix srcToDstRect;
1421 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1422 dst, SkMatrix::kFill_ScaleToFit);
1423 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1424 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001425 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001426 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001427 return;
1428 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001429 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1430 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1431 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001432 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001433 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001434 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001435 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001436 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001437}
1438
bsalomonf1ecd212015-12-09 17:06:02 -08001439void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001440 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001441 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001442
joshualitt33a5fce2015-11-18 13:28:51 -08001443 CHECK_SHOULD_DRAW(draw);
1444
joshualittedb36442015-11-19 14:29:30 -08001445 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001446 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001447 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001448 GrSamplerParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001449 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1450 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001451 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001452 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001453
1454 SkRect srcR, dstR;
1455 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001456 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001457 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001458 }
1459 return;
1460 }
1461
Brian Salomon514baff2016-11-17 15:17:07 -05001462 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001463 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001464 producer->createFragmentProcessor(SkMatrix::I(),
1465 SkRect::MakeIWH(producer->width(), producer->height()),
1466 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001467 &kMode, fRenderTargetContext->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -08001468 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001469 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1470 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1471 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001472 return;
1473 }
1474
msarett10e3d9b2016-08-18 15:46:03 -07001475 std::unique_ptr<SkLatticeIter> iter(
1476 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon82f44312017-01-11 13:42:54 -05001477 fRenderTargetContext->drawImageLattice(fClip, std::move(grPaint), *draw.fMatrix,
1478 producer->width(), producer->height(), std::move(iter),
1479 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001480}
1481
1482void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1483 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001484 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001485 uint32_t pinnedUniqueID;
1486 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1487 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001488 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001489 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001490 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001491 } else {
1492 SkBitmap bm;
1493 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001494 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001495 this->drawProducerNine(draw, &maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001496 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001497 this->drawBitmapNine(draw, bm, center, dst, paint);
1498 }
1499 }
1500}
1501
1502void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1503 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001504 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001505 GrBitmapTextureMaker maker(fContext.get(), bitmap);
reedc7ec7c92016-07-25 08:29:10 -07001506 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001507}
1508
msarett10e3d9b2016-08-18 15:46:03 -07001509void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1510 const SkCanvas::Lattice& lattice, const SkRect& dst,
1511 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001512 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001513
1514 CHECK_SHOULD_DRAW(draw);
1515
Brian Salomon514baff2016-11-17 15:17:07 -05001516 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001517 sk_sp<GrFragmentProcessor> fp(
1518 producer->createFragmentProcessor(SkMatrix::I(),
1519 SkRect::MakeIWH(producer->width(), producer->height()),
1520 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001521 &kMode, fRenderTargetContext->getColorSpace()));
msarett10e3d9b2016-08-18 15:46:03 -07001522 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001523 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1524 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1525 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001526 return;
1527 }
1528
1529 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001530 new SkLatticeIter(lattice, dst));
Brian Salomon82f44312017-01-11 13:42:54 -05001531 fRenderTargetContext->drawImageLattice(fClip, std::move(grPaint), *draw.fMatrix,
1532 producer->width(), producer->height(), std::move(iter),
1533 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001534}
1535
1536void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1537 const SkCanvas::Lattice& lattice, const SkRect& dst,
1538 const SkPaint& paint) {
1539 ASSERT_SINGLE_OWNER
1540 uint32_t pinnedUniqueID;
1541 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1542 CHECK_SHOULD_DRAW(draw);
1543 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1544 as_IB(image)->onImageInfo().colorSpace());
1545 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1546 } else {
1547 SkBitmap bm;
1548 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001549 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
msarett10e3d9b2016-08-18 15:46:03 -07001550 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001551 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
msarett10e3d9b2016-08-18 15:46:03 -07001552 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1553 }
1554 }
1555}
1556
1557void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1558 const SkCanvas::Lattice& lattice, const SkRect& dst,
1559 const SkPaint& paint) {
1560 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001561 GrBitmapTextureMaker maker(fContext.get(), bitmap);
msarett10e3d9b2016-08-18 15:46:03 -07001562 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1563}
1564
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001565///////////////////////////////////////////////////////////////////////////////
1566
1567// must be in SkCanvas::VertexMode order
1568static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1569 kTriangles_GrPrimitiveType,
1570 kTriangleStrip_GrPrimitiveType,
1571 kTriangleFan_GrPrimitiveType,
1572};
1573
1574void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1575 int vertexCount, const SkPoint vertices[],
1576 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001577 SkBlendMode bmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578 const uint16_t indices[], int indexCount,
1579 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001580 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001581 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001582 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001583
halcanary96fcdcc2015-08-27 07:41:13 -07001584 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1585 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001586
halcanary96fcdcc2015-08-27 07:41:13 -07001587 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001588
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001589 SkPaint copy(paint);
1590 copy.setStyle(SkPaint::kStroke_Style);
1591 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001592
bsalomonf1b7a1d2015-09-28 06:26:28 -07001593 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001594 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001595 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1596 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001597 return;
1598 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001599
dandov32a311b2014-07-15 19:46:26 -07001600 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001601 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001602 switch (vmode) {
1603 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001604 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001605 break;
1606 case SkCanvas::kTriangleStrip_VertexMode:
1607 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001608 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001609 break;
1610 }
mtklein533eb782014-08-27 10:39:42 -07001611
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001612 VertState state(vertexCount, indices, indexCount);
1613 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001614
dandov32a311b2014-07-15 19:46:26 -07001615 //number of indices for lines per triangle with kLines
1616 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001617
Ben Wagner7ecc5962016-11-02 17:07:33 -04001618 std::unique_ptr<uint16_t[]> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001619 int i = 0;
1620 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001621 lineIndices[i] = state.f0;
1622 lineIndices[i + 1] = state.f1;
1623 lineIndices[i + 2] = state.f1;
1624 lineIndices[i + 3] = state.f2;
1625 lineIndices[i + 4] = state.f2;
1626 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001627 i += 6;
1628 }
Brian Osman11052242016-10-27 14:47:55 -04001629 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001630 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -04001631 *draw.fMatrix,
1632 kLines_GrPrimitiveType,
1633 vertexCount,
1634 vertices,
1635 texs,
1636 colors,
1637 lineIndices.get(),
1638 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001639 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001640 }
1641
bsalomonf1b7a1d2015-09-28 06:26:28 -07001642 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643
1644 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001645 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001646 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1647 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001648 convertedColors.reset(vertexCount);
1649 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001650 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651 }
1652 colors = convertedColors.get();
1653 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001654 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001655 if (texs && paint.getShader()) {
1656 if (colors) {
Mike Reedfaba3712016-11-03 14:45:31 -04001657 // When there are texs and colors the shader and colors are combined using bmode.
Brian Osman11052242016-10-27 14:47:55 -04001658 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), paint,
Mike Reedfaba3712016-11-03 14:45:31 -04001659 *draw.fMatrix, bmode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001660 return;
1661 }
1662 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001663 // We have a shader, but no colors to blend it against.
Brian Osman11052242016-10-27 14:47:55 -04001664 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001665 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001666 return;
1667 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001668 }
bsalomonaa48d362015-10-01 08:34:17 -07001669 } else {
1670 if (colors) {
1671 // We have colors, but either have no shader or no texture coords (which implies that
1672 // we should ignore the shader).
Brian Osman11052242016-10-27 14:47:55 -04001673 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext.get(),
1674 paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001675 return;
1676 }
1677 } else {
1678 // No colors and no shaders. Just draw with the paint color.
Brian Osman11052242016-10-27 14:47:55 -04001679 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), paint,
1680 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001681 return;
1682 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001683 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001684 }
1685
Brian Osman11052242016-10-27 14:47:55 -04001686 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001687 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -04001688 *draw.fMatrix,
1689 primType,
1690 vertexCount,
1691 vertices,
1692 texs,
1693 colors,
1694 indices,
1695 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001696}
1697
1698///////////////////////////////////////////////////////////////////////////////
1699
jvanverth31ff7622015-08-07 10:09:28 -07001700void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001701 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001702 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001703 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001704 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001705 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001706 return;
1707 }
1708
jvanverth31ff7622015-08-07 10:09:28 -07001709 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001710 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001711
reedca109532015-06-25 16:25:25 -07001712 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001713 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001714
jvanverth31ff7622015-08-07 10:09:28 -07001715 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001716 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001717 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed7d954ad2016-10-28 15:42:34 -04001718 *draw.fMatrix, (SkBlendMode)mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001719 return;
1720 }
1721 } else {
Brian Osman11052242016-10-27 14:47:55 -04001722 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, *draw.fMatrix,
1723 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001724 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001725 }
1726 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001727
1728 SkDEBUGCODE(this->validate();)
Brian Salomon82f44312017-01-11 13:42:54 -05001729 fRenderTargetContext->drawAtlas(fClip, std::move(grPaint), *draw.fMatrix, count, xform, texRect,
1730 colors);
reedca109532015-06-25 16:25:25 -07001731}
1732
1733///////////////////////////////////////////////////////////////////////////////
1734
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001735void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001736 size_t byteLength, SkScalar x, SkScalar y,
1737 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001738 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001739 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001740 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741
jvanverth8c27a182014-10-14 08:45:50 -07001742 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001743 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1744 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001745 return;
1746 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001747
jvanverth8c27a182014-10-14 08:45:50 -07001748 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001749
Brian Salomon82f44312017-01-11 13:42:54 -05001750 fRenderTargetContext->drawText(fClip, std::move(grPaint), paint, *draw.fMatrix,
1751 (const char*)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752}
1753
fmalita05c4a432014-09-29 06:29:53 -07001754void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1755 const SkScalar pos[], int scalarsPerPos,
1756 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001757 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001758 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -08001759 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001760
jvanverth8c27a182014-10-14 08:45:50 -07001761 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001762 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1763 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001764 return;
1765 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001766
jvanverth8c27a182014-10-14 08:45:50 -07001767 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001768
Brian Salomon82f44312017-01-11 13:42:54 -05001769 fRenderTargetContext->drawPosText(fClip, std::move(grPaint), paint, *draw.fMatrix,
1770 (const char*)text, byteLength, pos, scalarsPerPos, offset,
Brian Osman11052242016-10-27 14:47:55 -04001771 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772}
1773
joshualitt9c328182015-03-23 08:13:04 -07001774void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1775 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001776 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001777 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001778 CHECK_SHOULD_DRAW(draw);
1779
1780 SkDEBUGCODE(this->validate();)
1781
Brian Osman11052242016-10-27 14:47:55 -04001782 fRenderTargetContext->drawTextBlob(fClip, paint, *draw.fMatrix,
1783 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001784}
1785
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786///////////////////////////////////////////////////////////////////////////////
1787
reedb2db8982014-11-13 12:41:02 -08001788bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001789 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001790}
1791
1792void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001793 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001794
Brian Osman11052242016-10-27 14:47:55 -04001795 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001796}
1797
1798///////////////////////////////////////////////////////////////////////////////
1799
reed76033be2015-03-14 10:54:31 -07001800SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001801 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802
robertphillipsca6eafc2016-05-17 09:57:46 -07001803 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001804
robertphillipsca6eafc2016-05-17 09:57:46 -07001805 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001806 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001807 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1808 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001809
Brian Osman11052242016-10-27 14:47:55 -04001810 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1811 fit,
1812 cinfo.fInfo.width(), cinfo.fInfo.height(),
1813 fRenderTargetContext->config(),
1814 sk_ref_sp(fRenderTargetContext->getColorSpace()),
1815 fRenderTargetContext->desc().fSampleCnt,
1816 kDefault_GrSurfaceOrigin,
1817 &props));
1818 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001819 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001821
1822 // Skia's convention is to only clear a device if it is non-opaque.
1823 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1824
Robert Phillips9fab7e92016-11-17 12:45:04 -05001825 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1826 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001827}
1828
reede8f30622016-03-23 18:59:25 -07001829sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001830 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001831 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001832 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001833 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Osman11052242016-10-27 14:47:55 -04001834 fRenderTargetContext->desc().fSampleCnt,
1835 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001836}
1837
senorblanco900c3672016-04-27 11:31:23 -07001838SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001839 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001840 // We always return a transient cache, so it is freed after each
1841 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001842 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001843}
reedf037e0b2014-10-30 11:34:15 -07001844
1845#endif