blob: c5ade9e7b1f6167a778c40e34b4af6c982100130 [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"
Matt Sarettcb6266b2017-01-17 10:48:53 -050029#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050030#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070031#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000032#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkRRect.h"
Brian Salomon6a639042016-12-14 11:08:17 -050037#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050038#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080039#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070040#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000042#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080043#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000044#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000045#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000046#include "SkVertState.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050047#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080048#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080049#include "effects/GrSimpleTextureEffect.h"
50#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080051#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052
reedf037e0b2014-10-30 11:34:15 -070053#if SK_SUPPORT_GPU
54
joshualittce894002016-01-11 13:29:31 -080055#define ASSERT_SINGLE_OWNER \
56 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
57
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#if 0
59 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080060 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 do { \
62 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080063 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064 } while (0)
65#else
joshualitt5531d512014-12-17 15:50:11 -080066 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067#endif
68
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069///////////////////////////////////////////////////////////////////////////////
70
bsalomon74f681d2015-06-23 14:38:48 -070071/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
72 should fail. */
73bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
74 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
75 *flags = 0;
76 if (info) {
77 switch (info->alphaType()) {
78 case kPremul_SkAlphaType:
79 break;
80 case kOpaque_SkAlphaType:
81 *flags |= SkGpuDevice::kIsOpaque_Flag;
82 break;
83 default: // If it is unpremul or unknown don't try to render
84 return false;
85 }
86 }
87 if (kClear_InitContents == init) {
88 *flags |= kNeedClear_Flag;
89 }
90 return true;
91}
92
Robert Phillips9fab7e92016-11-17 12:45:04 -050093sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
94 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070095 int width, int height,
96 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040097 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070098 return nullptr;
99 }
100 unsigned flags;
101 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
102 return nullptr;
103 }
Robert Phillips9fab7e92016-11-17 12:45:04 -0500104 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
105 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000106}
107
robertphillips24e91282016-04-29 06:46:36 -0700108sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
109 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700110 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700111 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700112 unsigned flags;
113 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700114 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700115 }
116
Brian Osman11052242016-10-27 14:47:55 -0400117 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
118 info, sampleCount,
119 origin, props));
120 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700121 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700122 }
123
Robert Phillips9fab7e92016-11-17 12:45:04 -0500124 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700125 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700126}
127
Brian Osman11052242016-10-27 14:47:55 -0400128static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700129 SkColorType colorType;
130 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
131 colorType = kUnknown_SkColorType;
132 }
133 return SkImageInfo::Make(w, h, colorType,
134 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
Robert Phillips75a475c2017-01-13 09:18:59 -0500135 context->refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700136}
137
Robert Phillips9fab7e92016-11-17 12:45:04 -0500138SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
139 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400140 : INHERITED(make_info(renderTargetContext.get(), width, height,
141 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500142 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400143 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700144{
robertphillips1f3923e2016-07-21 07:17:54 -0700145 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700146 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147
bsalomone63ffef2016-02-05 07:17:34 -0800148 if (flags & kNeedClear_Flag) {
149 this->clearAll();
150 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151}
152
Brian Osman11052242016-10-27 14:47:55 -0400153sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
154 GrContext* context,
155 SkBudgeted budgeted,
156 const SkImageInfo& origInfo,
157 int sampleCount,
158 GrSurfaceOrigin origin,
159 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 if (kUnknown_SkColorType == origInfo.colorType() ||
161 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000163 }
164
bsalomonafe30052015-01-16 07:32:33 -0800165 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700166 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800167 }
168
Brian Osman0c2997b2017-01-11 16:58:42 -0500169 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Brian Osman11052242016-10-27 14:47:55 -0400170 return context->makeRenderTargetContext(SkBackingFit::kExact, // Why exact?
robertphillips6738c702016-07-27 12:13:51 -0700171 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500172 config, origInfo.refColorSpace(), sampleCount,
robertphillips6738c702016-07-27 12:13:51 -0700173 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800174}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175
robertphillips970587b2016-07-14 14:12:55 -0700176sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
177 SkSpecialImage* srcImg,
178 int left, int top,
179 SkIPoint* offset,
180 const SkImageFilter* filter) {
181 SkASSERT(srcImg->isTextureBacked());
182 SkASSERT(filter);
183
184 SkMatrix matrix = *draw.fMatrix;
185 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
186 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500187 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400188 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700189 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700190
191 return filter->filterImage(srcImg, ctx, offset);
192}
193
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194///////////////////////////////////////////////////////////////////////////////
195
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000196bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
197 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800198 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000199
Matt Sarettcb6266b2017-01-17 10:48:53 -0500200 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
201 return false;
202 }
203
Matt Sarett03dd6d52017-01-23 12:15:09 -0500204 SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, x, y);
205 if (!rec.trim(this->width(), this->height())) {
206 return false;
207 }
208
209 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000210}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000211
robertphillips1da3ecd2016-08-31 14:54:15 -0700212bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
213 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800214 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700215
Matt Sarettcb6266b2017-01-17 10:48:53 -0500216 if (!SkImageInfoValidConversion(this->imageInfo(), srcInfo)) {
217 return false;
218 }
219
Matt Sarett03dd6d52017-01-23 12:15:09 -0500220 SkWritePixelsRec rec(srcInfo, srcPixels, srcRowBytes, x, y);
221 if (!rec.trim(this->width(), this->height())) {
222 return false;
223 }
224
225 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000226}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000227
reed41e010c2015-06-09 12:16:53 -0700228bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800229 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700230 return false;
231}
232
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000233// call this every draw call, to ensure that the context reflects our state,
234// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800235void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800236 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000237
reed2c9e2002016-07-25 08:05:22 -0700238 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000239}
240
Brian Osman11052242016-10-27 14:47:55 -0400241GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700242 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400243 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700244}
245
reed8eddfb52014-12-04 07:50:14 -0800246void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800247 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500248 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500249
reed8eddfb52014-12-04 07:50:14 -0800250 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500251 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800252}
253
Brian Osman11052242016-10-27 14:47:55 -0400254void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800255 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800256
Brian Osman693a5402016-10-27 15:13:22 -0400257 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800258
Brian Osman693a5402016-10-27 15:13:22 -0400259 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400260 this->context(),
261 budgeted,
262 this->imageInfo(),
263 fRenderTargetContext->numColorSamples(),
264 fRenderTargetContext->origin(),
265 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400266 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800267 return;
268 }
269
270 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400271 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800272 return;
273 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500274 newRTC->copy(fRenderTargetContext->asDeferredSurface());
kkinnunenabcfab42015-02-22 22:53:44 -0800275 }
276
Brian Osman693a5402016-10-27 15:13:22 -0400277 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800278}
279
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000280///////////////////////////////////////////////////////////////////////////////
281
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800283 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800284 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500285 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286
287 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400288 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
289 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700290 return;
291 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292
Brian Salomon82f44312017-01-11 13:42:54 -0500293 fRenderTargetContext->drawPaint(fClip, std::move(grPaint), *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294}
295
296// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700297static const GrPrimitiveType gPointMode2PrimitiveType[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298 kPoints_GrPrimitiveType,
299 kLines_GrPrimitiveType,
300 kLineStrip_GrPrimitiveType
301};
302
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500303static inline bool is_int(float x) { return x == (float) sk_float_round2int(x); }
304
ethannicholas330bb952015-07-17 06:44:02 -0700305// suppress antialiasing on axis-aligned integer-coordinate lines
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500306static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
307 const SkMatrix& matrix) {
ethannicholas330bb952015-07-17 06:44:02 -0700308 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
309 return false;
310 }
311 if (count == 2) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500312 // We do not antialias horizontal or vertical lines along pixel centers, even when the ends
313 // of the line do not fully cover the first and last pixel of the line, which is slightly
314 // wrong.
315 if (!matrix.isScaleTranslate()) {
316 return true;
317 }
ethannicholas330bb952015-07-17 06:44:02 -0700318 if (pts[0].fX == pts[1].fX) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500319 SkScalar x = matrix.getScaleX() * pts[0].fX + matrix.getTranslateX();
320 return !is_int(x + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700321 }
322 if (pts[0].fY == pts[1].fY) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500323 SkScalar y = matrix.getScaleY() * pts[0].fY + matrix.getTranslateY();
324 return !is_int(y + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700325 }
326 }
327 return true;
328}
329
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000330void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
331 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800332 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500333 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800334 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335
336 SkScalar width = paint.getStrokeWidth();
337 if (width < 0) {
338 return;
339 }
340
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000341 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700342 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700343 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400344 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700345 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700346 return;
347 }
egdaniele61c4112014-06-12 10:24:21 -0700348 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700349 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700350 path.moveTo(pts[0]);
351 path.lineTo(pts[1]);
Brian Salomon82f44312017-01-11 13:42:54 -0500352 fRenderTargetContext->drawPath(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500353 *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700354 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000355 }
356
bsalomon6ade6dd2016-09-12 12:07:17 -0700357 SkScalar scales[2];
358 bool isHairline = (0 == width) || (1 == width && draw.fMatrix->getMinMaxScales(scales) &&
359 SkScalarNearlyEqual(scales[0], 1.f) &&
360 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700361 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700363 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500364 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts, *draw.fMatrix))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365 draw.drawPoints(mode, count, pts, paint, true);
366 return;
367 }
368
bsalomon6ade6dd2016-09-12 12:07:17 -0700369 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
370
371 const SkMatrix* viewMatrix = draw.fMatrix;
372#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
373 // This offsetting in device space matches the expectations of the Android framework for non-AA
374 // points and lines.
375 SkMatrix tempMatrix;
376 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
377 tempMatrix = *viewMatrix;
378 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
379 tempMatrix.postTranslate(kOffset, kOffset);
380 viewMatrix = &tempMatrix;
381 }
382#endif
383
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400385 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
386 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700387 return;
388 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389
Brian Osman11052242016-10-27 14:47:55 -0400390 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500391 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -0400392 *viewMatrix,
393 primitiveType,
394 SkToS32(count),
395 (SkPoint*)pts,
396 nullptr,
397 nullptr,
398 nullptr,
399 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400}
401
402///////////////////////////////////////////////////////////////////////////////
403
robertphillipsff55b492015-11-24 07:56:59 -0800404void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800405 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500406 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800407 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000408
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409
bsalomona7d85ba2016-07-06 11:54:59 -0700410 // A couple reasons we might need to call drawPath.
411 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700413 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500415 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800416 fClip, path, paint,
417 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700418 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000419 return;
420 }
421
422 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400423 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
424 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700425 return;
426 }
Mike Klein744fb732014-06-23 15:13:26 -0400427
bsalomon6663acf2016-05-10 09:14:17 -0700428 GrStyle style(paint);
Brian Salomon82f44312017-01-11 13:42:54 -0500429 fRenderTargetContext->drawRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
430 *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431}
432
433///////////////////////////////////////////////////////////////////////////////
434
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500435void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800436 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500437 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800438 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000439
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000440 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400441 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
442 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700443 return;
444 }
Mike Klein744fb732014-06-23 15:13:26 -0400445
Robert Phillipsa29a9562016-10-20 09:40:55 -0400446 SkMaskFilter* mf = paint.getMaskFilter();
447 if (mf && mf->asFragmentProcessor(nullptr, nullptr, *draw.fMatrix)) {
448 mf = nullptr; // already handled in SkPaintToGrPaint
449 }
450
bsalomon6663acf2016-05-10 09:14:17 -0700451 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400452 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000453 // try to hit the fast path for drawing filtered round rects
454
455 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700456 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000457 if (devRRect.allCornersCircular()) {
458 SkRect maskRect;
Robert Phillipsa29a9562016-10-20 09:40:55 -0400459 if (mf->canFilterMaskGPU(devRRect, draw.fRC->getBounds(),
460 *draw.fMatrix, &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000461 SkIRect finalIRect;
462 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700463 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000464 // clipped out
465 return;
466 }
Hal Canary144caf52016-11-07 17:57:18 -0500467 if (mf->directFilterRRectMaskGPU(fContext.get(), fRenderTargetContext.get(),
Brian Salomon82f44312017-01-11 13:42:54 -0500468 std::move(grPaint), fClip, *draw.fMatrix,
Hal Canary144caf52016-11-07 17:57:18 -0500469 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000470 return;
471 }
472 }
473
474 }
475 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000476 }
477
Robert Phillipsa29a9562016-10-20 09:40:55 -0400478 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800479 // The only mask filter the native rrect drawing code could've handle was taken
480 // care of above.
481 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700483 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700484 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500485 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800486 fClip, path, paint,
487 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700488 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489 return;
490 }
Mike Klein744fb732014-06-23 15:13:26 -0400491
bsalomon6663acf2016-05-10 09:14:17 -0700492 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800493
Brian Salomon82f44312017-01-11 13:42:54 -0500494 fRenderTargetContext->drawRRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
495 *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000496}
497
robertphillipsd7706102016-02-25 09:28:08 -0800498
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000499void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800500 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800501 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500502 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillips0e7029e2015-11-30 05:45:06 -0800503 CHECK_SHOULD_DRAW(draw);
504
robertphillipsd7706102016-02-25 09:28:08 -0800505 if (outer.isEmpty()) {
506 return;
507 }
508
509 if (inner.isEmpty()) {
510 return this->drawRRect(draw, outer, paint);
511 }
512
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000513 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000514
robertphillips0e7029e2015-11-30 05:45:06 -0800515 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800516 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400517 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700518 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700519 return;
520 }
robertphillips00095892016-02-29 13:50:40 -0800521
Brian Salomon82f44312017-01-11 13:42:54 -0500522 fRenderTargetContext->drawDRRect(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500523 *draw.fMatrix, outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800524 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000525 }
526
527 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700528 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000529 path.addRRect(outer);
530 path.addRRect(inner);
531 path.setFillType(SkPath::kEvenOdd_FillType);
532
Hal Canary144caf52016-11-07 17:57:18 -0500533 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800534 fClip, path, paint,
535 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700536 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000537}
538
539
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000540/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541
msarettcc319b92016-08-25 18:07:18 -0700542void SkGpuDevice::drawRegion(const SkDraw& draw, const SkRegion& region, const SkPaint& paint) {
543 if (paint.getMaskFilter()) {
544 SkPath path;
545 region.getBoundaryPath(&path);
546 return this->drawPath(draw, path, paint, nullptr, false);
547 }
548
549 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400550 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
551 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700552 return;
553 }
554
Brian Salomon82f44312017-01-11 13:42:54 -0500555 fRenderTargetContext->drawRegion(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
556 *draw.fMatrix, region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700557}
558
robertphillips514450c2015-11-24 05:36:02 -0800559void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800560 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500561 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800562 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563
robertphillips514450c2015-11-24 05:36:02 -0800564 // Presumably the path effect warps this to something other than an oval
565 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700567 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700569 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570 return;
herb11a7f7f2015-11-24 12:41:00 -0800571 }
572
robertphillips514450c2015-11-24 05:36:02 -0800573 if (paint.getMaskFilter()) {
574 // The RRect path can handle special case blurring
575 SkRRect rr = SkRRect::MakeOval(oval);
576 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000577 }
578
579 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400580 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
581 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700582 return;
583 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000584
Brian Salomon82f44312017-01-11 13:42:54 -0500585 fRenderTargetContext->drawOval(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
586 *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587}
588
bsalomon4f3a0ca2016-08-22 13:14:26 -0700589void SkGpuDevice::drawArc(const SkDraw& draw, const SkRect& oval, SkScalar startAngle,
590 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
591 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500592 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700593 CHECK_SHOULD_DRAW(draw);
594
595 if (paint.getMaskFilter()) {
596 this->INHERITED::drawArc(draw, oval, startAngle, sweepAngle, useCenter, paint);
597 return;
598 }
599 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400600 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
601 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700602 return;
603 }
604
Brian Salomon82f44312017-01-11 13:42:54 -0500605 fRenderTargetContext->drawArc(fClip, std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
606 *draw.fMatrix, oval, startAngle, sweepAngle, useCenter,
607 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700608}
609
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000610#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611
612///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700613void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
614 const SkDraw& draw,
615 const SkPaint& origPaint) {
616 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500617 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
robertphillips0851d2d2016-06-02 05:21:34 -0700618 CHECK_SHOULD_DRAW(draw);
619
Brian Osman11052242016-10-27 14:47:55 -0400620 // Adding support for round capping would require a
621 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700622 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
623 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
624 SkASSERT(!origPaint.getPathEffect());
625 SkASSERT(!origPaint.getMaskFilter());
626
627 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
628 SkASSERT(halfWidth > 0);
629
630 SkVector v = points[1] - points[0];
631
632 SkScalar length = SkPoint::Normalize(&v);
633 if (!length) {
634 v.fX = 1.0f;
635 v.fY = 0.0f;
636 }
637
638 SkPaint newPaint(origPaint);
639 newPaint.setStyle(SkPaint::kFill_Style);
640
641 SkScalar xtraLength = 0.0f;
642 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
643 xtraLength = halfWidth;
644 }
645
646 SkPoint mid = points[0] + points[1];
647 mid.scale(0.5f);
648
649 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
650 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
651 SkMatrix m;
652 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
653
654 SkMatrix local = m;
655
656 m.postConcat(*draw.fMatrix);
657
658 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400659 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700660 return;
661 }
662
Brian Salomon82f44312017-01-11 13:42:54 -0500663 fRenderTargetContext->fillRectWithLocalMatrix(
664 fClip, std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700665}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000667void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
668 const SkPaint& paint, const SkMatrix* prePathMatrix,
669 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800670 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800671 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700672 SkPoint points[2];
673 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
674 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
675 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
676 // Path-based stroking looks better for thin rects
677 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700678 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500679 // Round capping support is currently disabled b.c. it would require a RRect
680 // GrDrawOp that takes a localMatrix.
robertphillips0851d2d2016-06-02 05:21:34 -0700681 this->drawStrokedLine(points, draw, paint);
682 return;
683 }
684 }
robertphillipsff55b492015-11-24 07:56:59 -0800685 bool isClosed;
686 SkRect rect;
687 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
688 this->drawRect(draw, rect, paint);
689 return;
690 }
691 if (origSrcPath.isOval(&rect)) {
692 this->drawOval(draw, rect, paint);
693 return;
694 }
695 SkRRect rrect;
696 if (origSrcPath.isRRect(&rrect)) {
697 this->drawRRect(draw, rrect, paint);
698 return;
699 }
700 }
701
joshualitt5531d512014-12-17 15:50:11 -0800702 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500703 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704
Hal Canary144caf52016-11-07 17:57:18 -0500705 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700706 fClip, origSrcPath, paint,
707 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700708 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709}
710
711static const int kBmpSmallTileSize = 1 << 10;
712
713static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
714 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
715 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
716 return tilesX * tilesY;
717}
718
reed85d91782015-09-10 14:33:38 -0700719static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720 if (maxTileSize <= kBmpSmallTileSize) {
721 return maxTileSize;
722 }
723
724 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
725 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
726
727 maxTileTotalTileSize *= maxTileSize * maxTileSize;
728 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
729
730 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
731 return kBmpSmallTileSize;
732 } else {
733 return maxTileSize;
734 }
735}
736
737// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
738// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700739static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800740 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800741 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700742 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700743 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744 const SkRect* srcRectPtr,
745 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700746 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700747 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
748 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 clippedSrcIRect->setEmpty();
750 return;
751 }
752 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
753 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700754 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755 if (!clippedSrcRect.intersect(*srcRectPtr)) {
756 clippedSrcIRect->setEmpty();
757 return;
758 }
759 }
760 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700761 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 if (!clippedSrcIRect->intersect(bmpBounds)) {
763 clippedSrcIRect->setEmpty();
764 }
765}
766
reed85d91782015-09-10 14:33:38 -0700767bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
768 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700769 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500770 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700771 const SkRect* srcRectPtr,
772 int maxTileSize,
773 int* tileSize,
774 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800775 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700776 // if it's larger than the max tile size, then we have no choice but tiling.
777 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400778 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
779 fClip, viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
780 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700781 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
782 return true;
783 }
784
bsalomon1a1d0b82015-10-16 07:49:42 -0700785 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700786 const size_t area = imageRect.width() * imageRect.height();
787 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
788 return false;
789 }
790
reed85d91782015-09-10 14:33:38 -0700791 // At this point we know we could do the draw by uploading the entire bitmap
792 // as a texture. However, if the texture would be large compared to the
793 // cache size and we don't require most of it for this draw then tile to
794 // reduce the amount of upload and cache spill.
795
796 // assumption here is that sw bitmap size is a good proxy for its size as
797 // a texture
798 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
799 size_t cacheSize;
800 fContext->getResourceCacheLimits(nullptr, &cacheSize);
801 if (bmpSize < cacheSize / 2) {
802 return false;
803 }
804
bsalomon1a1d0b82015-10-16 07:49:42 -0700805 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
806 // tiling memory savings would be < 50%.
Brian Osman11052242016-10-27 14:47:55 -0400807 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(), fClip,
808 viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
809 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700810 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
811 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
812 kBmpSmallTileSize * kBmpSmallTileSize;
813
814 return usedTileBytes < 2 * bmpSize;
815}
816
reed85d91782015-09-10 14:33:38 -0700817bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
818 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700819 const SkMatrix& viewMatrix,
820 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800821 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700822 // if image is explictly texture backed then just use the texture
823 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000824 return false;
825 }
826
Brian Salomon514baff2016-11-17 15:17:07 -0500827 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700828 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500829 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700830 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700831
832 int tileFilterPad;
833 if (doBicubic) {
834 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500835 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700836 tileFilterPad = 0;
837 } else {
838 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839 }
reed85d91782015-09-10 14:33:38 -0700840 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841
bsalomon8c07b7a2015-11-02 11:36:52 -0800842 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843
reed85d91782015-09-10 14:33:38 -0700844 // these are output, which we safely ignore, as we just want to know the predicate
845 int outTileSize;
846 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847
bsalomone553b642016-08-17 09:02:09 -0700848 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
849 params, srcRectPtr, maxTileSize, &outTileSize,
850 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851}
852
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000853void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854 const SkBitmap& bitmap,
855 const SkMatrix& m,
856 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800857 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800858 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800859 SkMatrix viewMatrix;
860 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700861
bsalomonb1b01992015-11-18 10:56:08 -0800862 int maxTileSize = fContext->caps()->maxTileSize();
863
864 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
865 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -0400866 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800867 paint.isAntiAlias() &&
868 bitmap.width() <= maxTileSize &&
869 bitmap.height() <= maxTileSize;
870
871 bool skipTileCheck = drawAA || paint.getMaskFilter();
872
873 if (!skipTileCheck) {
874 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
875 int tileSize;
876 SkIRect clippedSrcRect;
877
Brian Salomon514baff2016-11-17 15:17:07 -0500878 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800879 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500880 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800881 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
882 &doBicubic);
883
884 int tileFilterPad;
885
886 if (doBicubic) {
887 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500888 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800889 tileFilterPad = 0;
890 } else {
891 tileFilterPad = 1;
892 }
893 params.setFilterMode(textureFilterMode);
894
895 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700896 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
897 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
898 &tileSize, &clippedSrcRect)) {
899 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
900 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
901 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800902 return;
903 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000904 }
Hal Canary144caf52016-11-07 17:57:18 -0500905 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800906 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
907 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000908}
909
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000910// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
912// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000913static inline void clamped_outset_with_offset(SkIRect* iRect,
914 int outset,
915 SkPoint* offset,
916 const SkIRect& clamp) {
917 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000919 int leftClampDelta = clamp.fLeft - iRect->fLeft;
920 if (leftClampDelta > 0) {
921 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922 iRect->fLeft = clamp.fLeft;
923 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000924 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000926
927 int topClampDelta = clamp.fTop - iRect->fTop;
928 if (topClampDelta > 0) {
929 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 iRect->fTop = clamp.fTop;
931 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000932 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 }
934
935 if (iRect->fRight > clamp.fRight) {
936 iRect->fRight = clamp.fRight;
937 }
938 if (iRect->fBottom > clamp.fBottom) {
939 iRect->fBottom = clamp.fBottom;
940 }
941}
942
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943// Break 'bitmap' into several tiles to draw it since it has already
944// been determined to be too large to fit in VRAM
945void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800946 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700947 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 const SkRect& srcRect,
949 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500950 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800951 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700952 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000953 int tileSize,
954 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800955 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800956
ericrk983294f2016-04-18 09:14:00 -0700957 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800958 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700959 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800960
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000961 // The following pixel lock is technically redundant, but it is desirable
962 // to lock outside of the tile loop to prevent redecoding the whole image
963 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
964 // is larger than the limit of the discardable memory pool.
965 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800966
967 const SkPaint* paint = &origPaint;
968 SkPaint tempPaint;
Brian Osman11052242016-10-27 14:47:55 -0400969 if (origPaint.isAntiAlias() && !fRenderTargetContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800970 // Drop antialiasing to avoid seams at tile boundaries.
971 tempPaint = origPaint;
972 tempPaint.setAntiAlias(false);
973 paint = &tempPaint;
974 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
976
977 int nx = bitmap.width() / tileSize;
978 int ny = bitmap.height() / tileSize;
979 for (int x = 0; x <= nx; x++) {
980 for (int y = 0; y <= ny; y++) {
981 SkRect tileR;
982 tileR.set(SkIntToScalar(x * tileSize),
983 SkIntToScalar(y * tileSize),
984 SkIntToScalar((x + 1) * tileSize),
985 SkIntToScalar((y + 1) * tileSize));
986
987 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
988 continue;
989 }
990
991 if (!tileR.intersect(srcRect)) {
992 continue;
993 }
994
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 SkIRect iTileR;
996 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700997 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
998 SkIntToScalar(iTileR.fTop));
999 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
1000 tileR.width(), tileR.height());
1001 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -05001002 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 SkIRect iClampRect;
1004
reeda5517e22015-07-14 10:54:12 -07001005 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001006 // In bleed mode we want to always expand the tile on all edges
1007 // but stay within the bitmap bounds
1008 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1009 } else {
1010 // In texture-domain/clamp mode we only want to expand the
1011 // tile on edges interior to "srcRect" (i.e., we want to
1012 // not bleed across the original clamped edges)
1013 srcRect.roundOut(&iClampRect);
1014 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001015 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1016 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 }
1018
bsalomone553b642016-08-17 09:02:09 -07001019 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020 if (bitmap.extractSubset(&tmpB, iTileR)) {
1021 // now offset it to make it "local" to our tmp bitmap
1022 tileR.offset(-offset.fX, -offset.fY);
Brian Salomon514baff2016-11-17 15:17:07 -05001023 GrSamplerParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001024 // de-optimized this determination
1025 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001026 this->drawBitmapTile(tmpB,
1027 viewMatrix,
1028 rectToDraw,
1029 tileR,
1030 paramsTemp,
1031 *paint,
1032 constraint,
1033 bicubic,
1034 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 }
1036 }
1037 }
1038}
1039
bsalomone553b642016-08-17 09:02:09 -07001040void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1041 const SkMatrix& viewMatrix,
1042 const SkRect& dstRect,
1043 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001044 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001045 const SkPaint& paint,
1046 SkCanvas::SrcRectConstraint constraint,
1047 bool bicubic,
1048 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001049 // We should have already handled bitmaps larger than the max texture size.
1050 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1051 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001052 // We should be respecting the max tile size by the time we get here.
1053 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1054 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055
Robert Phillips67c18d62017-01-20 12:44:06 -05001056 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
1057 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
1058 params, scaleAdjust);
halcanary96fcdcc2015-08-27 07:41:13 -07001059 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 return;
1061 }
brianosman500bb3e2016-07-22 10:33:07 -07001062 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001063 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001064
bsalomone553b642016-08-17 09:02:09 -07001065 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillips67c18d62017-01-20 12:44:06 -05001066 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
1067 texMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -07001068
1069 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1070 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001071 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001072
reeda5517e22015-07-14 10:54:12 -07001073 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001075 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001077 domain.fLeft = srcRect.fLeft + 0.5f;
1078 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001080 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 }
1082 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001083 domain.fTop = srcRect.fTop + 0.5f;
1084 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001086 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001088 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001089 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001090 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001091 } else {
reedc7ec7c92016-07-25 08:29:10 -07001092 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001093 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001094 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001095 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001096 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001097 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001098 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001099 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 } else {
reedc7ec7c92016-07-25 08:29:10 -07001101 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 }
1103
joshualitt33a5fce2015-11-18 13:28:51 -08001104 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001105 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001106 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1107 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001108 return;
1109 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001111 // Coverage-based AA would cause seams between tiles.
1112 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
1113 fRenderTargetContext->isStencilBufferMultisampled());
Brian Salomon82f44312017-01-11 13:42:54 -05001114 fRenderTargetContext->drawRect(fClip, std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115}
1116
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1118 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001119 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001120 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001121 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122
robertphillips970587b2016-07-14 14:12:55 -07001123 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 return;
1125 }
1126
reedc7ec7c92016-07-25 08:29:10 -07001127 sk_sp<GrTexture> texture;
1128 {
robertphillips970587b2016-07-14 14:12:55 -07001129 SkAutoLockPixels alp(bitmap, true);
1130 if (!bitmap.readyToDraw()) {
1131 return;
1132 }
1133
1134 // draw sprite neither filters nor tiles.
Robert Phillips67c18d62017-01-20 12:44:06 -05001135 texture.reset(GrRefCachedBitmapTexture(fContext.get(), bitmap,
1136 GrSamplerParams::ClampNoFilter(), nullptr));
robertphillips970587b2016-07-14 14:12:55 -07001137 if (!texture) {
1138 return;
1139 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001140 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141
robertphillips970587b2016-07-14 14:12:55 -07001142 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1143 bitmap.pixelRefOrigin().fY,
1144 bitmap.width(),
1145 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001146
robertphillips970587b2016-07-14 14:12:55 -07001147 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1148 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001149 std::move(texture),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001150 bitmap.refColorSpace(),
robertphillips1b5f9682016-07-15 08:01:12 -07001151 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001152
1153 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1154}
1155
1156
Mike Kleine54c75f2016-10-13 14:18:09 -04001157void SkGpuDevice::drawSpecial(const SkDraw& draw,
robertphillips970587b2016-07-14 14:12:55 -07001158 SkSpecialImage* special1,
1159 int left, int top,
1160 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001161 ASSERT_SINGLE_OWNER
1162 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001163 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001164
1165 SkIPoint offset = { 0, 0 };
1166
1167 sk_sp<SkSpecialImage> result;
1168 if (paint.getImageFilter()) {
1169 result = this->filterTexture(draw, special1, left, top,
1170 &offset,
1171 paint.getImageFilter());
1172 if (!result) {
1173 return;
1174 }
1175 } else {
1176 result = sk_ref_sp(special1);
1177 }
1178
1179 SkASSERT(result->isTextureBacked());
Hal Canary144caf52016-11-07 17:57:18 -05001180 sk_sp<GrTexture> texture = result->asTextureRef(fContext.get());
Robert Phillips833dcf42016-11-18 08:44:13 -05001181 if (!texture) {
1182 return;
1183 }
robertphillips970587b2016-07-14 14:12:55 -07001184
1185 SkPaint tmpUnfiltered(paint);
1186 tmpUnfiltered.setImageFilter(nullptr);
1187
brianosman77320db2016-09-07 08:09:10 -07001188 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001189 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001191 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1192 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001193 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001194 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001195 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001196 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001197 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001198 }
Brian Osman11052242016-10-27 14:47:55 -04001199 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001200 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001201 return;
1202 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203
robertphillips970587b2016-07-14 14:12:55 -07001204 const SkIRect& subset = result->subset();
1205
Brian Salomon82f44312017-01-11 13:42:54 -05001206 fRenderTargetContext->fillRectToRect(
1207 fClip,
1208 std::move(grPaint),
1209 GrBoolToAA(paint.isAntiAlias()),
1210 SkMatrix::I(),
1211 SkRect::Make(SkIRect::MakeXYWH(
1212 left + offset.fX, top + offset.fY, subset.width(), subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001213 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214}
1215
bsalomonb1b01992015-11-18 10:56:08 -08001216void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1217 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001218 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001219 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001220 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001221
bsalomonb1b01992015-11-18 10:56:08 -08001222 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1223 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1224 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1225 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1226 const SkRect* dst = &origDst;
1227 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001229 if (!src) {
1230 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001231 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001232
bsalomonb1b01992015-11-18 10:56:08 -08001233 SkMatrix srcToDstMatrix;
1234 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1235 return;
1236 }
1237 SkRect tmpSrc, tmpDst;
1238 if (src != &bmpBounds) {
1239 if (!bmpBounds.contains(*src)) {
1240 tmpSrc = *src;
1241 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 return; // nothing to draw
1243 }
bsalomonb1b01992015-11-18 10:56:08 -08001244 src = &tmpSrc;
1245 srcToDstMatrix.mapRect(&tmpDst, *src);
1246 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247 }
1248 }
1249
bsalomonb1b01992015-11-18 10:56:08 -08001250 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001251
bsalomonb1b01992015-11-18 10:56:08 -08001252 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1253 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001254 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001255 paint.isAntiAlias() &&
1256 bitmap.width() <= maxTileSize &&
1257 bitmap.height() <= maxTileSize;
1258
1259 bool skipTileCheck = drawAA || paint.getMaskFilter();
1260
1261 if (!skipTileCheck) {
1262 int tileSize;
1263 SkIRect clippedSrcRect;
1264
Brian Salomon514baff2016-11-17 15:17:07 -05001265 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001266 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001267 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -08001268 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1269 &doBicubic);
1270
1271 int tileFilterPad;
1272
1273 if (doBicubic) {
1274 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001275 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001276 tileFilterPad = 0;
1277 } else {
1278 tileFilterPad = 1;
1279 }
1280 params.setFilterMode(textureFilterMode);
1281
1282 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001283 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1284 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1285 &clippedSrcRect)) {
1286 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1287 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001288 return;
1289 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001290 }
Hal Canary144caf52016-11-07 17:57:18 -05001291 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001292 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001293}
1294
robertphillips6451a0c2016-07-18 08:31:31 -07001295sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001296 SkAutoLockPixels alp(bitmap, true);
1297 if (!bitmap.readyToDraw()) {
1298 return nullptr;
1299 }
1300
Robert Phillips67c18d62017-01-20 12:44:06 -05001301 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
1302 GrSamplerParams::ClampNoFilter(), nullptr);
robertphillips6451a0c2016-07-18 08:31:31 -07001303 if (!texture) {
1304 return nullptr;
1305 }
1306
1307 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1308 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001309 texture,
Robert Phillips70b49fd2017-01-13 11:21:36 -05001310 bitmap.refColorSpace(),
robertphillips6451a0c2016-07-18 08:31:31 -07001311 &this->surfaceProps());
1312}
1313
reede51c3562016-07-19 14:33:20 -07001314sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001315 SkPixmap pm;
1316 if (image->isTextureBacked()) {
1317 GrTexture* texture = as_IB(image)->peekTexture();
1318
1319 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1320 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001321 sk_ref_sp(texture),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001322 as_IB(image)->onImageInfo().refColorSpace(),
robertphillips6451a0c2016-07-18 08:31:31 -07001323 &this->surfaceProps());
1324 } else if (image->peekPixels(&pm)) {
1325 SkBitmap bm;
1326
1327 bm.installPixels(pm);
1328 return this->makeSpecial(bm);
1329 } else {
1330 return nullptr;
1331 }
1332}
1333
1334sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillipse2f7d182016-12-15 09:23:05 -05001335 sk_sp<GrSurfaceProxy> sProxy(sk_ref_sp(this->accessRenderTargetContext()->asDeferredTexture()));
1336 if (!sProxy) {
robertphillips04d62182016-07-15 12:21:33 -07001337 // When the device doesn't have a texture, we create a temporary texture.
1338 // TODO: we should actually only copy the portion of the source needed to apply the image
1339 // filter
Robert Phillipse2f7d182016-12-15 09:23:05 -05001340 sProxy = GrSurfaceProxy::Copy(fContext.get(),
1341 this->accessRenderTargetContext()->asDeferredSurface(),
1342 SkBudgeted::kYes);
1343 if (!sProxy) {
robertphillips04d62182016-07-15 12:21:33 -07001344 return nullptr;
1345 }
robertphillips1b5f9682016-07-15 08:01:12 -07001346 }
1347
1348 const SkImageInfo ii = this->imageInfo();
1349 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1350
Robert Phillipse2f7d182016-12-15 09:23:05 -05001351 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1352 srcRect,
1353 kNeedNewImageUniqueID_SpecialImage,
1354 sProxy,
Robert Phillips70b49fd2017-01-13 11:21:36 -05001355 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001356 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001357}
1358
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001360 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001361 SkASSERT(!paint.getImageFilter());
1362
joshualittce894002016-01-11 13:29:31 -08001363 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001365 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001366
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001368 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369
robertphillips1b5f9682016-07-15 08:01:12 -07001370 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001371 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001372 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001373 return;
1374 }
1375
robertphillips1b5f9682016-07-15 08:01:12 -07001376 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001377}
1378
reeda85d4d02015-05-06 12:56:48 -07001379void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1380 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001381 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001382 SkMatrix viewMatrix = *draw.fMatrix;
1383 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001384 uint32_t pinnedUniqueID;
1385 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001386 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001387 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001388 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001389 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1390 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001391 return;
reed85d91782015-09-10 14:33:38 -07001392 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001393 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001394 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001395 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001396 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001397 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
reed85d91782015-09-10 14:33:38 -07001398 return;
1399 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001400 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1401 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1402 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001403 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001404 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1405 viewMatrix, fClip, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001406 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001407 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001408 }
reeda85d4d02015-05-06 12:56:48 -07001409 }
1410}
1411
1412void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001413 const SkRect& dst, const SkPaint& paint,
1414 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001415 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001416 uint32_t pinnedUniqueID;
1417 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001418 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001419 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001420 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001421 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001422 return;
1423 }
1424 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001425 SkMatrix srcToDstRect;
1426 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1427 dst, SkMatrix::kFill_ScaleToFit);
1428 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1429 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001430 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001431 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001432 return;
1433 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001434 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1435 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1436 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001437 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001438 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001439 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001440 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001441 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001442}
1443
bsalomonf1ecd212015-12-09 17:06:02 -08001444void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001445 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001446 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001447
joshualitt33a5fce2015-11-18 13:28:51 -08001448 CHECK_SHOULD_DRAW(draw);
1449
joshualittedb36442015-11-19 14:29:30 -08001450 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001451 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001452 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001453 GrSamplerParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001454 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1455 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001456 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001457 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001458
1459 SkRect srcR, dstR;
1460 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001461 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001462 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001463 }
1464 return;
1465 }
1466
Brian Salomon514baff2016-11-17 15:17:07 -05001467 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001468 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001469 producer->createFragmentProcessor(SkMatrix::I(),
1470 SkRect::MakeIWH(producer->width(), producer->height()),
1471 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001472 &kMode, fRenderTargetContext->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -08001473 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001474 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1475 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1476 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001477 return;
1478 }
1479
msarett10e3d9b2016-08-18 15:46:03 -07001480 std::unique_ptr<SkLatticeIter> iter(
1481 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon82f44312017-01-11 13:42:54 -05001482 fRenderTargetContext->drawImageLattice(fClip, std::move(grPaint), *draw.fMatrix,
1483 producer->width(), producer->height(), std::move(iter),
1484 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001485}
1486
1487void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1488 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001489 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001490 uint32_t pinnedUniqueID;
1491 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1492 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001493 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001494 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001495 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001496 } else {
1497 SkBitmap bm;
1498 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001499 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001500 this->drawProducerNine(draw, &maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001501 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001502 this->drawBitmapNine(draw, bm, center, dst, paint);
1503 }
1504 }
1505}
1506
1507void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1508 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001509 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001510 GrBitmapTextureMaker maker(fContext.get(), bitmap);
reedc7ec7c92016-07-25 08:29:10 -07001511 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001512}
1513
msarett10e3d9b2016-08-18 15:46:03 -07001514void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1515 const SkCanvas::Lattice& lattice, const SkRect& dst,
1516 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001517 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001518
1519 CHECK_SHOULD_DRAW(draw);
1520
Brian Salomon514baff2016-11-17 15:17:07 -05001521 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001522 sk_sp<GrFragmentProcessor> fp(
1523 producer->createFragmentProcessor(SkMatrix::I(),
1524 SkRect::MakeIWH(producer->width(), producer->height()),
1525 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001526 &kMode, fRenderTargetContext->getColorSpace()));
msarett10e3d9b2016-08-18 15:46:03 -07001527 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001528 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1529 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1530 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001531 return;
1532 }
1533
1534 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001535 new SkLatticeIter(lattice, dst));
Brian Salomon82f44312017-01-11 13:42:54 -05001536 fRenderTargetContext->drawImageLattice(fClip, std::move(grPaint), *draw.fMatrix,
1537 producer->width(), producer->height(), std::move(iter),
1538 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001539}
1540
1541void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1542 const SkCanvas::Lattice& lattice, const SkRect& dst,
1543 const SkPaint& paint) {
1544 ASSERT_SINGLE_OWNER
1545 uint32_t pinnedUniqueID;
1546 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1547 CHECK_SHOULD_DRAW(draw);
1548 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1549 as_IB(image)->onImageInfo().colorSpace());
1550 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1551 } else {
1552 SkBitmap bm;
1553 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001554 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
msarett10e3d9b2016-08-18 15:46:03 -07001555 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001556 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
msarett10e3d9b2016-08-18 15:46:03 -07001557 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1558 }
1559 }
1560}
1561
1562void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1563 const SkCanvas::Lattice& lattice, const SkRect& dst,
1564 const SkPaint& paint) {
1565 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001566 GrBitmapTextureMaker maker(fContext.get(), bitmap);
msarett10e3d9b2016-08-18 15:46:03 -07001567 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1568}
1569
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570///////////////////////////////////////////////////////////////////////////////
1571
1572// must be in SkCanvas::VertexMode order
1573static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1574 kTriangles_GrPrimitiveType,
1575 kTriangleStrip_GrPrimitiveType,
1576 kTriangleFan_GrPrimitiveType,
1577};
1578
1579void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1580 int vertexCount, const SkPoint vertices[],
1581 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001582 SkBlendMode bmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001583 const uint16_t indices[], int indexCount,
1584 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001585 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001586 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001587 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001588
halcanary96fcdcc2015-08-27 07:41:13 -07001589 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1590 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001591
halcanary96fcdcc2015-08-27 07:41:13 -07001592 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001593
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001594 SkPaint copy(paint);
1595 copy.setStyle(SkPaint::kStroke_Style);
1596 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001597
bsalomonf1b7a1d2015-09-28 06:26:28 -07001598 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001599 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001600 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1601 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001602 return;
1603 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001604
dandov32a311b2014-07-15 19:46:26 -07001605 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001606 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001607 switch (vmode) {
1608 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001609 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001610 break;
1611 case SkCanvas::kTriangleStrip_VertexMode:
1612 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001613 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001614 break;
1615 }
mtklein533eb782014-08-27 10:39:42 -07001616
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001617 VertState state(vertexCount, indices, indexCount);
1618 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001619
dandov32a311b2014-07-15 19:46:26 -07001620 //number of indices for lines per triangle with kLines
1621 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001622
Ben Wagner7ecc5962016-11-02 17:07:33 -04001623 std::unique_ptr<uint16_t[]> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001624 int i = 0;
1625 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001626 lineIndices[i] = state.f0;
1627 lineIndices[i + 1] = state.f1;
1628 lineIndices[i + 2] = state.f1;
1629 lineIndices[i + 3] = state.f2;
1630 lineIndices[i + 4] = state.f2;
1631 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001632 i += 6;
1633 }
Brian Osman11052242016-10-27 14:47:55 -04001634 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001635 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -04001636 *draw.fMatrix,
1637 kLines_GrPrimitiveType,
1638 vertexCount,
1639 vertices,
1640 texs,
1641 colors,
1642 lineIndices.get(),
1643 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001644 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001645 }
1646
bsalomonf1b7a1d2015-09-28 06:26:28 -07001647 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001648
1649 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001650 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001651 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1652 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653 convertedColors.reset(vertexCount);
1654 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001655 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656 }
1657 colors = convertedColors.get();
1658 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001659 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001660 if (texs && paint.getShader()) {
1661 if (colors) {
Mike Reedfaba3712016-11-03 14:45:31 -04001662 // When there are texs and colors the shader and colors are combined using bmode.
Brian Osman11052242016-10-27 14:47:55 -04001663 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), paint,
Mike Reedfaba3712016-11-03 14:45:31 -04001664 *draw.fMatrix, bmode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001665 return;
1666 }
1667 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001668 // We have a shader, but no colors to blend it against.
Brian Osman11052242016-10-27 14:47:55 -04001669 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001670 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001671 return;
1672 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001673 }
bsalomonaa48d362015-10-01 08:34:17 -07001674 } else {
1675 if (colors) {
1676 // We have colors, but either have no shader or no texture coords (which implies that
1677 // we should ignore the shader).
Brian Osman11052242016-10-27 14:47:55 -04001678 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext.get(),
1679 paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001680 return;
1681 }
1682 } else {
1683 // No colors and no shaders. Just draw with the paint color.
Brian Osman11052242016-10-27 14:47:55 -04001684 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), paint,
1685 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001686 return;
1687 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001688 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001689 }
1690
Brian Osman11052242016-10-27 14:47:55 -04001691 fRenderTargetContext->drawVertices(fClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001692 std::move(grPaint),
Brian Osman11052242016-10-27 14:47:55 -04001693 *draw.fMatrix,
1694 primType,
1695 vertexCount,
1696 vertices,
1697 texs,
1698 colors,
1699 indices,
1700 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701}
1702
1703///////////////////////////////////////////////////////////////////////////////
1704
jvanverth31ff7622015-08-07 10:09:28 -07001705void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001706 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001707 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001708 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001709 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001710 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001711 return;
1712 }
1713
jvanverth31ff7622015-08-07 10:09:28 -07001714 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001715 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001716
reedca109532015-06-25 16:25:25 -07001717 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001718 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001719
jvanverth31ff7622015-08-07 10:09:28 -07001720 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001721 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001722 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed7d954ad2016-10-28 15:42:34 -04001723 *draw.fMatrix, (SkBlendMode)mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001724 return;
1725 }
1726 } else {
Brian Osman11052242016-10-27 14:47:55 -04001727 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, *draw.fMatrix,
1728 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001729 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001730 }
1731 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001732
1733 SkDEBUGCODE(this->validate();)
Brian Salomon82f44312017-01-11 13:42:54 -05001734 fRenderTargetContext->drawAtlas(fClip, std::move(grPaint), *draw.fMatrix, count, xform, texRect,
1735 colors);
reedca109532015-06-25 16:25:25 -07001736}
1737
1738///////////////////////////////////////////////////////////////////////////////
1739
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001741 size_t byteLength, SkScalar x, SkScalar y,
1742 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001743 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001744 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001745 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001746 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001747
Brian Salomon6f1d36c2017-01-13 12:02:17 -05001748 fRenderTargetContext->drawText(fClip, paint, *draw.fMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -05001749 (const char*)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750}
1751
fmalita05c4a432014-09-29 06:29:53 -07001752void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1753 const SkScalar pos[], int scalarsPerPos,
1754 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001755 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001756 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -08001757 CHECK_SHOULD_DRAW(draw);
jvanverth8c27a182014-10-14 08:45:50 -07001758 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001759
Brian Salomon6f1d36c2017-01-13 12:02:17 -05001760 fRenderTargetContext->drawPosText(fClip, paint, *draw.fMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -05001761 (const char*)text, byteLength, pos, scalarsPerPos, offset,
Brian Osman11052242016-10-27 14:47:55 -04001762 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763}
1764
joshualitt9c328182015-03-23 08:13:04 -07001765void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1766 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001767 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001768 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001769 CHECK_SHOULD_DRAW(draw);
1770
1771 SkDEBUGCODE(this->validate();)
1772
Brian Osman11052242016-10-27 14:47:55 -04001773 fRenderTargetContext->drawTextBlob(fClip, paint, *draw.fMatrix,
1774 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001775}
1776
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777///////////////////////////////////////////////////////////////////////////////
1778
reedb2db8982014-11-13 12:41:02 -08001779bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001780 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001781}
1782
1783void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001784 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001785
Brian Osman11052242016-10-27 14:47:55 -04001786 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001787}
1788
1789///////////////////////////////////////////////////////////////////////////////
1790
reed76033be2015-03-14 10:54:31 -07001791SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001792 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001793
robertphillipsca6eafc2016-05-17 09:57:46 -07001794 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795
robertphillipsca6eafc2016-05-17 09:57:46 -07001796 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001797 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001798 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1799 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001800
Brian Osman11052242016-10-27 14:47:55 -04001801 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1802 fit,
1803 cinfo.fInfo.width(), cinfo.fInfo.height(),
1804 fRenderTargetContext->config(),
Robert Phillips75a475c2017-01-13 09:18:59 -05001805 fRenderTargetContext->refColorSpace(),
Brian Osman11052242016-10-27 14:47:55 -04001806 fRenderTargetContext->desc().fSampleCnt,
1807 kDefault_GrSurfaceOrigin,
1808 &props));
1809 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001810 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001812
1813 // Skia's convention is to only clear a device if it is non-opaque.
1814 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1815
Robert Phillips9fab7e92016-11-17 12:45:04 -05001816 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1817 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818}
1819
reede8f30622016-03-23 18:59:25 -07001820sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001821 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001822 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001823 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001824 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Osman11052242016-10-27 14:47:55 -04001825 fRenderTargetContext->desc().fSampleCnt,
1826 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001827}
1828
senorblanco900c3672016-04-27 11:31:23 -07001829SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001830 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001831 // We always return a transient cache, so it is freed after each
1832 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001833 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001834}
reedf037e0b2014-10-30 11:34:15 -07001835
1836#endif