blob: ee6e88d33af367879d68595f25e4e2c6fc67063c [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"
Robert Phillips0ae6faa2017-03-21 16:22:00 -040017#include "GrSurfaceProxyPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050018#include "GrTextureAdjuster.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050019#include "GrTextureProxy.h"
egdanielbbcb38d2014-06-19 10:19:29 -070020#include "GrTracing.h"
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"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070026#include "SkImageFilterCache.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050027#include "SkImageInfoPriv.h"
Brian Salomon6a639042016-12-14 11:08:17 -050028#include "SkImage_Base.h"
msarettc573a402016-08-02 08:05:56 -070029#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000030#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000032#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070033#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkRRect.h"
Brian Salomon6a639042016-12-14 11:08:17 -050035#include "SkRasterClip.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050036#include "SkReadPixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080037#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070038#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000040#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000042#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000044#include "SkVertState.h"
Brian Salomon199fb872017-02-06 09:41:10 -050045#include "SkVertices.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050046#include "SkWritePixelsRec.h"
kkinnunenabcfab42015-02-22 22:53:44 -080047#include "effects/GrBicubicEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080048#include "effects/GrSimpleTextureEffect.h"
49#include "effects/GrTextureDomain.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040050#include "../private/SkShadowFlags.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)();
Mike Reeda1361362017-03-07 09:37:29 -050060 #define CHECK_SHOULD_DRAW() \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 do { \
62 if (gShouldDrawProc && !gShouldDrawProc()) return; \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063 } while (0)
64#else
Brian Salomon0166cfd2017-03-13 17:58:25 -040065#define CHECK_SHOULD_DRAW()
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066#endif
67
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000068///////////////////////////////////////////////////////////////////////////////
69
bsalomon74f681d2015-06-23 14:38:48 -070070/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
71 should fail. */
72bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
73 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
74 *flags = 0;
75 if (info) {
76 switch (info->alphaType()) {
77 case kPremul_SkAlphaType:
78 break;
79 case kOpaque_SkAlphaType:
80 *flags |= SkGpuDevice::kIsOpaque_Flag;
81 break;
82 default: // If it is unpremul or unknown don't try to render
83 return false;
84 }
85 }
86 if (kClear_InitContents == init) {
87 *flags |= kNeedClear_Flag;
88 }
89 return true;
90}
91
Robert Phillips9fab7e92016-11-17 12:45:04 -050092sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
93 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070094 int width, int height,
95 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040096 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070097 return nullptr;
98 }
99 unsigned flags;
100 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
101 return nullptr;
102 }
Robert Phillips9fab7e92016-11-17 12:45:04 -0500103 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
104 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000105}
106
robertphillips24e91282016-04-29 06:46:36 -0700107sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
108 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700109 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700110 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700111 unsigned flags;
112 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700114 }
115
Brian Osman11052242016-10-27 14:47:55 -0400116 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
117 info, sampleCount,
118 origin, props));
119 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700121 }
122
Robert Phillips9fab7e92016-11-17 12:45:04 -0500123 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700124 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700125}
126
Brian Osman11052242016-10-27 14:47:55 -0400127static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700128 SkColorType colorType;
129 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
130 colorType = kUnknown_SkColorType;
131 }
132 return SkImageInfo::Make(w, h, colorType,
133 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
Robert Phillips75a475c2017-01-13 09:18:59 -0500134 context->refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700135}
136
Robert Phillips9fab7e92016-11-17 12:45:04 -0500137SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
138 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400139 : INHERITED(make_info(renderTargetContext.get(), width, height,
140 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500141 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400142 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700143{
robertphillips1f3923e2016-07-21 07:17:54 -0700144 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700145 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146
bsalomone63ffef2016-02-05 07:17:34 -0800147 if (flags & kNeedClear_Flag) {
148 this->clearAll();
149 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150}
151
Brian Osman11052242016-10-27 14:47:55 -0400152sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
153 GrContext* context,
154 SkBudgeted budgeted,
155 const SkImageInfo& origInfo,
156 int sampleCount,
157 GrSurfaceOrigin origin,
158 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000159 if (kUnknown_SkColorType == origInfo.colorType() ||
160 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700161 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000162 }
163
bsalomonafe30052015-01-16 07:32:33 -0800164 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800166 }
167
Brian Osman0c2997b2017-01-11 16:58:42 -0500168 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400169 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
170 // they need to be exact.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400171 return context->makeDeferredRenderTargetContext(
172 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700173 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500174 config, origInfo.refColorSpace(), sampleCount,
robertphillips6738c702016-07-27 12:13:51 -0700175 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800176}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177
Mike Reeda1361362017-03-07 09:37:29 -0500178sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700179 int left, int top,
180 SkIPoint* offset,
181 const SkImageFilter* filter) {
182 SkASSERT(srcImg->isTextureBacked());
183 SkASSERT(filter);
184
Mike Reeda1361362017-03-07 09:37:29 -0500185 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700186 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500187 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500188 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400189 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700190 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700191
192 return filter->filterImage(srcImg, ctx, offset);
193}
194
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000195///////////////////////////////////////////////////////////////////////////////
196
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000197bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
198 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800199 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000200
Matt Sarettcb6266b2017-01-17 10:48:53 -0500201 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
202 return false;
203 }
204
Matt Sarett03dd6d52017-01-23 12:15:09 -0500205 SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, x, y);
206 if (!rec.trim(this->width(), this->height())) {
207 return false;
208 }
209
210 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000211}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000212
robertphillips1da3ecd2016-08-31 14:54:15 -0700213bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
214 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800215 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700216
Matt Sarettcb6266b2017-01-17 10:48:53 -0500217 if (!SkImageInfoValidConversion(this->imageInfo(), srcInfo)) {
218 return false;
219 }
220
Matt Sarett03dd6d52017-01-23 12:15:09 -0500221 SkWritePixelsRec rec(srcInfo, srcPixels, srcRowBytes, x, y);
222 if (!rec.trim(this->width(), this->height())) {
223 return false;
224 }
225
226 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000227}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228
reed41e010c2015-06-09 12:16:53 -0700229bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800230 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700231 return false;
232}
233
Brian Osman11052242016-10-27 14:47:55 -0400234GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700235 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400236 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700237}
238
reed8eddfb52014-12-04 07:50:14 -0800239void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800240 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500241 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500242
reed8eddfb52014-12-04 07:50:14 -0800243 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500244 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800245}
246
Brian Osman11052242016-10-27 14:47:55 -0400247void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800248 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800249
Brian Osman693a5402016-10-27 15:13:22 -0400250 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800251
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400252 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
253 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400254 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400255 this->context(),
256 budgeted,
257 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400258 fRenderTargetContext->numColorSamples(),
259 fRenderTargetContext->origin(),
Brian Osman11052242016-10-27 14:47:55 -0400260 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400261 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800262 return;
263 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400264 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800265
266 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400267 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800268 return;
269 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500270 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800271 }
272
Brian Osman693a5402016-10-27 15:13:22 -0400273 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800274}
275
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276///////////////////////////////////////////////////////////////////////////////
277
Mike Reeda1361362017-03-07 09:37:29 -0500278void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800279 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -0500280 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -0500281 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
283 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500284 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400285 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700286 return;
287 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
Brian Salomon0166cfd2017-03-13 17:58:25 -0400289 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290}
291
292// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700293static const GrPrimitiveType gPointMode2PrimitiveType[] = {
Chris Dalton3809bab2017-06-13 10:55:06 -0600294 GrPrimitiveType::kPoints,
295 GrPrimitiveType::kLines,
296 GrPrimitiveType::kLineStrip
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000297};
298
Mike Reeda1361362017-03-07 09:37:29 -0500299void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800301 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500302 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500303 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304
305 SkScalar width = paint.getStrokeWidth();
306 if (width < 0) {
307 return;
308 }
309
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000310 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700311 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700312 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500313 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700314 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700315 return;
316 }
egdaniele61c4112014-06-12 10:24:21 -0700317 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700318 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700319 path.moveTo(pts[0]);
320 path.lineTo(pts[1]);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400321 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint),
322 GrBoolToAA(paint.isAntiAlias()), this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700323 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000324 }
325
bsalomon6ade6dd2016-09-12 12:07:17 -0700326 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500327 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700328 SkScalarNearlyEqual(scales[0], 1.f) &&
329 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700330 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400332 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500333 SkRasterClip rc(this->devClipBounds());
334 SkDraw draw;
335 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
336 draw.fMatrix = &this->ctm();
337 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500338 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000339 return;
340 }
341
bsalomon6ade6dd2016-09-12 12:07:17 -0700342 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
343
Mike Reeda1361362017-03-07 09:37:29 -0500344 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700345#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
346 // This offsetting in device space matches the expectations of the Android framework for non-AA
347 // points and lines.
348 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600349 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700350 tempMatrix = *viewMatrix;
351 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
352 tempMatrix.postTranslate(kOffset, kOffset);
353 viewMatrix = &tempMatrix;
354 }
355#endif
356
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400358 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
359 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700360 return;
361 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
Brian Osmanae0c50c2017-05-25 16:56:34 -0400363 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
364 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
365 nullptr);
366
367 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
368 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369}
370
371///////////////////////////////////////////////////////////////////////////////
372
Mike Reeda1361362017-03-07 09:37:29 -0500373void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800374 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500375 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500376 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378
bsalomona7d85ba2016-07-06 11:54:59 -0700379 // A couple reasons we might need to call drawPath.
380 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700382 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500384 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400385 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500386 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000387 return;
388 }
389
390 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500391 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400392 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700393 return;
394 }
Mike Klein744fb732014-06-23 15:13:26 -0400395
bsalomon6663acf2016-05-10 09:14:17 -0700396 GrStyle style(paint);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400397 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint),
398 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000399}
400
401///////////////////////////////////////////////////////////////////////////////
402
Mike Reeda1361362017-03-07 09:37:29 -0500403void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800404 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500405 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500406 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000408 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500409 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400410 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700411 return;
412 }
Mike Klein744fb732014-06-23 15:13:26 -0400413
Robert Phillipsa29a9562016-10-20 09:40:55 -0400414 SkMaskFilter* mf = paint.getMaskFilter();
Robert Phillipsd9d84852017-06-09 10:48:29 -0400415 if (mf && mf->asFragmentProcessor(nullptr)) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400416 mf = nullptr; // already handled in SkPaintToGrPaint
417 }
418
bsalomon6663acf2016-05-10 09:14:17 -0700419 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400420 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000421 // try to hit the fast path for drawing filtered round rects
422
423 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500424 if (rrect.transform(this->ctm(), &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000425 if (devRRect.allCornersCircular()) {
426 SkRect maskRect;
Mike Reeda1361362017-03-07 09:37:29 -0500427 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(),
428 this->ctm(), &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000429 SkIRect finalIRect;
430 maskRect.roundOut(&finalIRect);
Mike Reeda1361362017-03-07 09:37:29 -0500431
432 // we used to test finalIRect for quickReject, but that seems unlikely
433 // given that the original shape was not rejected...
434
Robert Phillips26c90e02017-03-14 14:39:29 -0400435 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400436 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500437 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000438 return;
439 }
440 }
441
442 }
443 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000444 }
445
Robert Phillipsa29a9562016-10-20 09:40:55 -0400446 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800447 // The only mask filter the native rrect drawing code could've handle was taken
448 // care of above.
449 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700451 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700452 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500453 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400454 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500455 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 return;
457 }
Mike Klein744fb732014-06-23 15:13:26 -0400458
bsalomon6663acf2016-05-10 09:14:17 -0700459 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800460
Brian Salomon0166cfd2017-03-13 17:58:25 -0400461 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint),
462 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000463}
464
robertphillipsd7706102016-02-25 09:28:08 -0800465
Mike Reeda1361362017-03-07 09:37:29 -0500466void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800467 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800468 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500469 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500470 CHECK_SHOULD_DRAW();
robertphillips0e7029e2015-11-30 05:45:06 -0800471
robertphillipsd7706102016-02-25 09:28:08 -0800472 if (outer.isEmpty()) {
473 return;
474 }
475
476 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500477 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800478 }
479
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000480 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000481
robertphillips0e7029e2015-11-30 05:45:06 -0800482 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800483 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500484 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700485 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700486 return;
487 }
robertphillips00095892016-02-29 13:50:40 -0800488
Brian Salomon0166cfd2017-03-13 17:58:25 -0400489 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
490 GrBoolToAA(paint.isAntiAlias()), this->ctm(), outer,
491 inner);
robertphillips00095892016-02-29 13:50:40 -0800492 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000493 }
494
495 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700496 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000497 path.addRRect(outer);
498 path.addRRect(inner);
499 path.setFillType(SkPath::kEvenOdd_FillType);
500
Brian Salomon0166cfd2017-03-13 17:58:25 -0400501 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
502 path, paint, this->ctm(), nullptr, this->devClipBounds(),
503 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000504}
505
506
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000507/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508
Mike Reeda1361362017-03-07 09:37:29 -0500509void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700510 if (paint.getMaskFilter()) {
511 SkPath path;
512 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500513 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700514 }
515
516 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500517 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400518 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700519 return;
520 }
521
Brian Salomon0166cfd2017-03-13 17:58:25 -0400522 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint),
523 GrBoolToAA(paint.isAntiAlias()), this->ctm(), region,
524 GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700525}
526
Mike Reeda1361362017-03-07 09:37:29 -0500527void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800528 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500529 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500530 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531
robertphillips514450c2015-11-24 05:36:02 -0800532 // Presumably the path effect warps this to something other than an oval
533 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700535 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536 path.addOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500537 this->drawPath(path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538 return;
herb11a7f7f2015-11-24 12:41:00 -0800539 }
540
robertphillips514450c2015-11-24 05:36:02 -0800541 if (paint.getMaskFilter()) {
542 // The RRect path can handle special case blurring
543 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500544 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 }
546
547 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500548 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400549 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700550 return;
551 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552
Brian Salomon0166cfd2017-03-13 17:58:25 -0400553 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint),
554 GrBoolToAA(paint.isAntiAlias()), this->ctm(), oval,
555 GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556}
557
Mike Reeda1361362017-03-07 09:37:29 -0500558void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700559 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
560 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500561 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500562 CHECK_SHOULD_DRAW();
bsalomon4f3a0ca2016-08-22 13:14:26 -0700563
564 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500565 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700566 return;
567 }
568 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500569 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400570 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700571 return;
572 }
573
Brian Salomon0166cfd2017-03-13 17:58:25 -0400574 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500575 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500576 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700577}
578
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000579#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580
581///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700582void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700583 const SkPaint& origPaint) {
584 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500585 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500586 CHECK_SHOULD_DRAW();
robertphillips0851d2d2016-06-02 05:21:34 -0700587
Brian Osman11052242016-10-27 14:47:55 -0400588 // Adding support for round capping would require a
589 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700590 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
591 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
592 SkASSERT(!origPaint.getPathEffect());
593 SkASSERT(!origPaint.getMaskFilter());
594
595 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
596 SkASSERT(halfWidth > 0);
597
598 SkVector v = points[1] - points[0];
599
600 SkScalar length = SkPoint::Normalize(&v);
601 if (!length) {
602 v.fX = 1.0f;
603 v.fY = 0.0f;
604 }
605
606 SkPaint newPaint(origPaint);
607 newPaint.setStyle(SkPaint::kFill_Style);
608
609 SkScalar xtraLength = 0.0f;
610 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
611 xtraLength = halfWidth;
612 }
613
614 SkPoint mid = points[0] + points[1];
615 mid.scale(0.5f);
616
617 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
618 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
619 SkMatrix m;
620 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
621
622 SkMatrix local = m;
623
Mike Reeda1361362017-03-07 09:37:29 -0500624 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700625
626 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400627 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700628 return;
629 }
630
Brian Salomon82f44312017-01-11 13:42:54 -0500631 fRenderTargetContext->fillRectWithLocalMatrix(
Brian Salomon0166cfd2017-03-13 17:58:25 -0400632 this->clip(), std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700633}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000634
Mike Reeda1361362017-03-07 09:37:29 -0500635void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636 const SkPaint& paint, const SkMatrix* prePathMatrix,
637 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800638 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800639 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700640 SkPoint points[2];
641 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
642 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500643 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700644 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500645 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700646 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500647 // Round capping support is currently disabled b.c. it would require a RRect
648 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500649 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700650 return;
651 }
652 }
robertphillipsff55b492015-11-24 07:56:59 -0800653 bool isClosed;
654 SkRect rect;
655 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
Mike Reeda1361362017-03-07 09:37:29 -0500656 this->drawRect(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800657 return;
658 }
659 if (origSrcPath.isOval(&rect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500660 this->drawOval(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800661 return;
662 }
663 SkRRect rrect;
664 if (origSrcPath.isRRect(&rrect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500665 this->drawRRect(rrect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800666 return;
667 }
668 }
669
Mike Reeda1361362017-03-07 09:37:29 -0500670 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -0500671 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672
Brian Salomon0166cfd2017-03-13 17:58:25 -0400673 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
674 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500675 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676}
677
678static const int kBmpSmallTileSize = 1 << 10;
679
680static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
681 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
682 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
683 return tilesX * tilesY;
684}
685
reed85d91782015-09-10 14:33:38 -0700686static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 if (maxTileSize <= kBmpSmallTileSize) {
688 return maxTileSize;
689 }
690
691 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
692 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
693
694 maxTileTotalTileSize *= maxTileSize * maxTileSize;
695 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
696
697 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
698 return kBmpSmallTileSize;
699 } else {
700 return maxTileSize;
701 }
702}
703
704// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
705// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700706static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800707 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800708 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700709 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700710 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000711 const SkRect* srcRectPtr,
712 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700713 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700714 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
715 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716 clippedSrcIRect->setEmpty();
717 return;
718 }
719 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
720 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700721 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 if (!clippedSrcRect.intersect(*srcRectPtr)) {
723 clippedSrcIRect->setEmpty();
724 return;
725 }
726 }
727 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700728 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000729 if (!clippedSrcIRect->intersect(bmpBounds)) {
730 clippedSrcIRect->setEmpty();
731 }
732}
733
reed85d91782015-09-10 14:33:38 -0700734bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
735 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700736 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500737 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700738 const SkRect* srcRectPtr,
739 int maxTileSize,
740 int* tileSize,
741 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800742 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700743 // if it's larger than the max tile size, then we have no choice but tiling.
744 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400745 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400746 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
747 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700748 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
749 return true;
750 }
751
bsalomon1a1d0b82015-10-16 07:49:42 -0700752 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700753 const size_t area = imageRect.width() * imageRect.height();
754 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
755 return false;
756 }
757
reed85d91782015-09-10 14:33:38 -0700758 // At this point we know we could do the draw by uploading the entire bitmap
759 // as a texture. However, if the texture would be large compared to the
760 // cache size and we don't require most of it for this draw then tile to
761 // reduce the amount of upload and cache spill.
762
763 // assumption here is that sw bitmap size is a good proxy for its size as
764 // a texture
765 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
766 size_t cacheSize;
767 fContext->getResourceCacheLimits(nullptr, &cacheSize);
768 if (bmpSize < cacheSize / 2) {
769 return false;
770 }
771
bsalomon1a1d0b82015-10-16 07:49:42 -0700772 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
773 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400774 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
775 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400776 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700777 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
778 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500779 kBmpSmallTileSize * kBmpSmallTileSize *
780 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700781
Brian Osmand05cdc32017-02-06 13:24:47 -0500782 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700783}
784
reed85d91782015-09-10 14:33:38 -0700785bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
786 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700787 const SkMatrix& viewMatrix,
788 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800789 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700790 // if image is explictly texture backed then just use the texture
Robert Phillips6de99042017-01-31 11:31:39 -0500791 if (image->isTextureBacked()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 return false;
793 }
794
Brian Salomon514baff2016-11-17 15:17:07 -0500795 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700796 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500797 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700798 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700799
800 int tileFilterPad;
801 if (doBicubic) {
802 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500803 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700804 tileFilterPad = 0;
805 } else {
806 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 }
reed85d91782015-09-10 14:33:38 -0700808 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809
bsalomon8c07b7a2015-11-02 11:36:52 -0800810 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811
reed85d91782015-09-10 14:33:38 -0700812 // these are output, which we safely ignore, as we just want to know the predicate
813 int outTileSize;
814 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815
bsalomone553b642016-08-17 09:02:09 -0700816 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
817 params, srcRectPtr, maxTileSize, &outTileSize,
818 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000819}
820
Mike Reeda1361362017-03-07 09:37:29 -0500821void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822 const SkMatrix& m,
823 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800824 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -0500825 CHECK_SHOULD_DRAW();
bsalomonb1b01992015-11-18 10:56:08 -0800826 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500827 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700828
bsalomonb1b01992015-11-18 10:56:08 -0800829 int maxTileSize = fContext->caps()->maxTileSize();
830
831 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
832 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400833 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
834 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800835 bitmap.height() <= maxTileSize;
836
837 bool skipTileCheck = drawAA || paint.getMaskFilter();
838
839 if (!skipTileCheck) {
840 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
841 int tileSize;
842 SkIRect clippedSrcRect;
843
Brian Salomon514baff2016-11-17 15:17:07 -0500844 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800845 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500846 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800847 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
848 &doBicubic);
849
850 int tileFilterPad;
851
852 if (doBicubic) {
853 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500854 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800855 tileFilterPad = 0;
856 } else {
857 tileFilterPad = 1;
858 }
859 params.setFilterMode(textureFilterMode);
860
861 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700862 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
863 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
864 &tileSize, &clippedSrcRect)) {
865 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
866 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
867 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800868 return;
869 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000870 }
Hal Canary144caf52016-11-07 17:57:18 -0500871 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800872 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -0400873 viewMatrix, this->clip(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874}
875
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000876// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
878// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000879static inline void clamped_outset_with_offset(SkIRect* iRect,
880 int outset,
881 SkPoint* offset,
882 const SkIRect& clamp) {
883 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000885 int leftClampDelta = clamp.fLeft - iRect->fLeft;
886 if (leftClampDelta > 0) {
887 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888 iRect->fLeft = clamp.fLeft;
889 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000890 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000891 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000892
893 int topClampDelta = clamp.fTop - iRect->fTop;
894 if (topClampDelta > 0) {
895 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000896 iRect->fTop = clamp.fTop;
897 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000898 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000899 }
900
901 if (iRect->fRight > clamp.fRight) {
902 iRect->fRight = clamp.fRight;
903 }
904 if (iRect->fBottom > clamp.fBottom) {
905 iRect->fBottom = clamp.fBottom;
906 }
907}
908
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909// Break 'bitmap' into several tiles to draw it since it has already
910// been determined to be too large to fit in VRAM
911void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800912 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700913 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 const SkRect& srcRect,
915 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500916 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800917 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700918 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000919 int tileSize,
920 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800921 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800922
ericrk983294f2016-04-18 09:14:00 -0700923 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800924 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700925 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800926
bsalomonc55271f2015-11-09 11:55:57 -0800927 const SkPaint* paint = &origPaint;
928 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400929 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800930 // Drop antialiasing to avoid seams at tile boundaries.
931 tempPaint = origPaint;
932 tempPaint.setAntiAlias(false);
933 paint = &tempPaint;
934 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000935 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
936
937 int nx = bitmap.width() / tileSize;
938 int ny = bitmap.height() / tileSize;
939 for (int x = 0; x <= nx; x++) {
940 for (int y = 0; y <= ny; y++) {
941 SkRect tileR;
942 tileR.set(SkIntToScalar(x * tileSize),
943 SkIntToScalar(y * tileSize),
944 SkIntToScalar((x + 1) * tileSize),
945 SkIntToScalar((y + 1) * tileSize));
946
947 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
948 continue;
949 }
950
951 if (!tileR.intersect(srcRect)) {
952 continue;
953 }
954
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 SkIRect iTileR;
956 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700957 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
958 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500959 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700960 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500961 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962 SkIRect iClampRect;
963
reeda5517e22015-07-14 10:54:12 -0700964 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 // In bleed mode we want to always expand the tile on all edges
966 // but stay within the bitmap bounds
967 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
968 } else {
969 // In texture-domain/clamp mode we only want to expand the
970 // tile on edges interior to "srcRect" (i.e., we want to
971 // not bleed across the original clamped edges)
972 srcRect.roundOut(&iClampRect);
973 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000974 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
975 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 }
977
bsalomone553b642016-08-17 09:02:09 -0700978 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 if (bitmap.extractSubset(&tmpB, iTileR)) {
980 // now offset it to make it "local" to our tmp bitmap
981 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800982 // de-optimized this determination
983 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700984 this->drawBitmapTile(tmpB,
985 viewMatrix,
986 rectToDraw,
987 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500988 params,
bsalomone553b642016-08-17 09:02:09 -0700989 *paint,
990 constraint,
991 bicubic,
992 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 }
994 }
995 }
996}
997
bsalomone553b642016-08-17 09:02:09 -0700998void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
999 const SkMatrix& viewMatrix,
1000 const SkRect& dstRect,
1001 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001002 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001003 const SkPaint& paint,
1004 SkCanvas::SrcRectConstraint constraint,
1005 bool bicubic,
1006 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001007 // We should have already handled bitmaps larger than the max texture size.
1008 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1009 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001010 // We should be respecting the max tile size by the time we get here.
1011 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1012 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001013
Robert Phillipse14d3052017-02-15 13:18:21 -05001014 SkASSERT(SkShader::kClamp_TileMode == params.getTileModeX() &&
1015 SkShader::kClamp_TileMode == params.getTileModeY());
1016
Robert Phillips78075802017-03-23 11:11:59 -04001017 sk_sp<GrTextureProxy> proxy = GrRefCachedBitmapTextureProxy(fContext.get(), bitmap,
1018 params, nullptr);
1019 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020 return;
1021 }
brianosman500bb3e2016-07-22 10:33:07 -07001022 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001023 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001024
bsalomone553b642016-08-17 09:02:09 -07001025 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillipse14d3052017-02-15 13:18:21 -05001026 const SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect,
1027 SkMatrix::kFill_ScaleToFit);
joshualitt5f10b5c2015-07-09 10:24:35 -07001028
1029 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1030 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001031 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001032
reeda5517e22015-07-14 10:54:12 -07001033 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001035 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001037 domain.fLeft = srcRect.fLeft + 0.5f;
1038 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001040 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 }
1042 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001043 domain.fTop = srcRect.fTop + 0.5f;
1044 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001046 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001048 if (bicubic) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001049 fp = GrBicubicEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001050 std::move(colorSpaceXform), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001051 } else {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001052 fp = GrTextureDomainEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001053 std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001054 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001055 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001056 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001057 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001058 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001059 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001060 fp = GrBicubicEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001061 std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 } else {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001063 fp = GrSimpleTextureEffect::Make(std::move(proxy),
Robert Phillips78075802017-03-23 11:11:59 -04001064 std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065 }
1066
joshualitt33a5fce2015-11-18 13:28:51 -08001067 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001068 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001069 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1070 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001071 return;
1072 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001074 // Coverage-based AA would cause seams between tiles.
1075 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001076 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001077 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078}
1079
Mike Reeda1361362017-03-07 09:37:29 -05001080void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001082 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001083 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001084 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085
robertphillips970587b2016-07-14 14:12:55 -07001086 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 return;
1088 }
1089
Robert Phillipse14d3052017-02-15 13:18:21 -05001090 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1091 if (!srcImg) {
1092 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001093 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094
Florin Malita53f77bd2017-04-28 13:48:37 -04001095 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001096}
1097
1098
Florin Malita53f77bd2017-04-28 13:48:37 -04001099void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
1100 SkImage* clipImage,const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001101 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001102 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001103 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001104
Florin Malita53f77bd2017-04-28 13:48:37 -04001105 // TODO: clipImage support.
1106
robertphillips970587b2016-07-14 14:12:55 -07001107 SkIPoint offset = { 0, 0 };
1108
1109 sk_sp<SkSpecialImage> result;
1110 if (paint.getImageFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -05001111 result = this->filterTexture(special1, left, top,
robertphillips970587b2016-07-14 14:12:55 -07001112 &offset,
1113 paint.getImageFilter());
1114 if (!result) {
1115 return;
1116 }
1117 } else {
1118 result = sk_ref_sp(special1);
1119 }
1120
1121 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001122 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001123 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001124 return;
1125 }
robertphillips970587b2016-07-14 14:12:55 -07001126
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001127 const GrPixelConfig config = proxy->config();
1128
robertphillips970587b2016-07-14 14:12:55 -07001129 SkPaint tmpUnfiltered(paint);
1130 tmpUnfiltered.setImageFilter(nullptr);
1131
brianosman77320db2016-09-07 08:09:10 -07001132 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001133 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001134
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001135 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(std::move(proxy),
brianosman77320db2016-09-07 08:09:10 -07001136 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001137 SkMatrix::I()));
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001138 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001139 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001140 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001141 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001142 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001143
1144 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001145 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001146 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001147 return;
1148 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149
robertphillips970587b2016-07-14 14:12:55 -07001150 const SkIRect& subset = result->subset();
1151
Brian Salomon82f44312017-01-11 13:42:54 -05001152 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001153 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001154 std::move(grPaint),
1155 GrBoolToAA(paint.isAntiAlias()),
1156 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001157 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1158 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001159 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001160}
1161
Mike Reeda1361362017-03-07 09:37:29 -05001162void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001163 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001164 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001165 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001166 CHECK_SHOULD_DRAW();
reedc7ec7c92016-07-25 08:29:10 -07001167
bsalomonb1b01992015-11-18 10:56:08 -08001168 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1169 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1170 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1171 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1172 const SkRect* dst = &origDst;
1173 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001175 if (!src) {
1176 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001178
bsalomonb1b01992015-11-18 10:56:08 -08001179 SkMatrix srcToDstMatrix;
1180 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1181 return;
1182 }
1183 SkRect tmpSrc, tmpDst;
1184 if (src != &bmpBounds) {
1185 if (!bmpBounds.contains(*src)) {
1186 tmpSrc = *src;
1187 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 return; // nothing to draw
1189 }
bsalomonb1b01992015-11-18 10:56:08 -08001190 src = &tmpSrc;
1191 srcToDstMatrix.mapRect(&tmpDst, *src);
1192 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 }
1194 }
1195
bsalomonb1b01992015-11-18 10:56:08 -08001196 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001197
bsalomonb1b01992015-11-18 10:56:08 -08001198 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1199 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001200 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1201 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1202 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001203
Brian Salomon7c8460e2017-05-12 11:36:10 -04001204 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001205
1206 if (!skipTileCheck) {
1207 int tileSize;
1208 SkIRect clippedSrcRect;
1209
Brian Salomon514baff2016-11-17 15:17:07 -05001210 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001211 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001212 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001213 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -08001214 &doBicubic);
1215
1216 int tileFilterPad;
1217
1218 if (doBicubic) {
1219 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001220 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001221 tileFilterPad = 0;
1222 } else {
1223 tileFilterPad = 1;
1224 }
1225 params.setFilterMode(textureFilterMode);
1226
1227 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001228 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001229 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1230 &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001231 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
bsalomone553b642016-08-17 09:02:09 -07001232 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001233 return;
1234 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001235 }
Hal Canary144caf52016-11-07 17:57:18 -05001236 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001237 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), this->clip(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238}
1239
robertphillips6451a0c2016-07-18 08:31:31 -07001240sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001241 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1242 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips26c90e02017-03-14 14:39:29 -04001243 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->resourceProvider(), bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001244 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001245 return nullptr;
1246 }
1247
Robert Phillipse14d3052017-02-15 13:18:21 -05001248 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001249
Robert Phillipse14d3052017-02-15 13:18:21 -05001250 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1251 // the special image
1252 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1253 rect,
1254 bitmap.getGenerationID(),
1255 std::move(proxy),
1256 bitmap.refColorSpace(),
1257 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001258}
1259
reede51c3562016-07-19 14:33:20 -07001260sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001261 SkPixmap pm;
1262 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001263 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001264
Robert Phillips6de99042017-01-31 11:31:39 -05001265 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1266 SkIRect::MakeWH(image->width(), image->height()),
1267 image->uniqueID(),
1268 std::move(proxy),
1269 as_IB(image)->onImageInfo().refColorSpace(),
1270 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001271 } else if (image->peekPixels(&pm)) {
1272 SkBitmap bm;
1273
1274 bm.installPixels(pm);
1275 return this->makeSpecial(bm);
1276 } else {
1277 return nullptr;
1278 }
1279}
1280
1281sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001282 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1283 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001284 // When the device doesn't have a texture, we create a temporary texture.
1285 // TODO: we should actually only copy the portion of the source needed to apply the image
1286 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001287 proxy = GrSurfaceProxy::Copy(fContext.get(),
1288 this->accessRenderTargetContext()->asSurfaceProxy(),
1289 SkBudgeted::kYes);
1290 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001291 return nullptr;
1292 }
robertphillips1b5f9682016-07-15 08:01:12 -07001293 }
1294
1295 const SkImageInfo ii = this->imageInfo();
1296 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1297
Robert Phillipse2f7d182016-12-15 09:23:05 -05001298 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1299 srcRect,
1300 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001301 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001302 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001303 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001304}
1305
Mike Reeda1361362017-03-07 09:37:29 -05001306void SkGpuDevice::drawDevice(SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001307 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001308 SkASSERT(!paint.getImageFilter());
1309
joshualittce894002016-01-11 13:29:31 -08001310 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001312 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001313
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314 // drawDevice is defined to be in device coords.
Mike Reeda1361362017-03-07 09:37:29 -05001315 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316
robertphillips1b5f9682016-07-15 08:01:12 -07001317 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001318 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001319 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 return;
1321 }
1322
Florin Malita53f77bd2017-04-28 13:48:37 -04001323 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324}
1325
Mike Reeda1361362017-03-07 09:37:29 -05001326void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y,
reeda85d4d02015-05-06 12:56:48 -07001327 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001328 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001329 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001330 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001331 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001332
1333 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001334 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001335 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1336 image->alphaType(), image->bounds(),
1337 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001338 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001339 viewMatrix, this->clip(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001340 return;
reed85d91782015-09-10 14:33:38 -07001341 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001342 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001343 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
Mike Reeda1361362017-03-07 09:37:29 -05001344 paint.getFilterQuality(), this->ctm(), SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001345 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001346 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
reed85d91782015-09-10 14:33:38 -07001347 return;
1348 }
Mike Reeda1361362017-03-07 09:37:29 -05001349 this->drawBitmap(bm, SkMatrix::MakeTrans(x, y), paint);
Brian Osmandf7e0752017-04-26 16:20:28 -04001350 } else if (image->isLazyGenerated()) {
Mike Reeda1361362017-03-07 09:37:29 -05001351 CHECK_SHOULD_DRAW();
Brian Osmandf7e0752017-04-26 16:20:28 -04001352 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001353 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001354 viewMatrix, this->clip(), paint);
Brian Osman61624f02016-12-09 14:51:59 -05001355 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001356 this->drawBitmap(bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001357 }
reeda85d4d02015-05-06 12:56:48 -07001358 }
1359}
1360
Mike Reeda1361362017-03-07 09:37:29 -05001361void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001362 const SkRect& dst, const SkPaint& paint,
1363 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001364 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001365 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001366 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001367 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001368 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1369 image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001370 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001371 this->drawTextureProducer(&adjuster, src, &dst, constraint, this->ctm(), this->clip(),
1372 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001373 return;
1374 }
1375 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001376 SkMatrix srcToDstRect;
1377 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1378 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001379 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001380 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001381 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001382 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001383 return;
1384 }
Mike Reeda1361362017-03-07 09:37:29 -05001385 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Osmandf7e0752017-04-26 16:20:28 -04001386 } else if (image->isLazyGenerated()) {
Mike Reeda1361362017-03-07 09:37:29 -05001387 CHECK_SHOULD_DRAW();
Brian Osmandf7e0752017-04-26 16:20:28 -04001388 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001389 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), this->clip(), paint);
Brian Osman61624f02016-12-09 14:51:59 -05001390 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001391 this->drawBitmapRect(bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001392 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001393}
1394
Mike Reeda1361362017-03-07 09:37:29 -05001395void SkGpuDevice::drawProducerNine(GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001396 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001397 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001398
Mike Reeda1361362017-03-07 09:37:29 -05001399 CHECK_SHOULD_DRAW();
joshualitt33a5fce2015-11-18 13:28:51 -08001400
joshualittedb36442015-11-19 14:29:30 -08001401 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Salomon7c8460e2017-05-12 11:36:10 -04001402 GrFSAAType::kUnifiedMSAA == fRenderTargetContext->fsaaType();
joshualitt33a5fce2015-11-18 13:28:51 -08001403 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001404 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001405 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), SkMatrix::I(),
bsalomon2bbd0c62015-12-09 12:50:56 -08001406 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001407 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001408 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001409
1410 SkRect srcR, dstR;
1411 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001412 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001413 this->ctm(), this->clip(), paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001414 }
1415 return;
1416 }
1417
Brian Salomon514baff2016-11-17 15:17:07 -05001418 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001419 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001420 producer->createFragmentProcessor(SkMatrix::I(),
1421 SkRect::MakeIWH(producer->width(), producer->height()),
1422 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001423 &kMode, fRenderTargetContext->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -08001424 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001425 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001426 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001427 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001428 return;
1429 }
1430
msarett10e3d9b2016-08-18 15:46:03 -07001431 std::unique_ptr<SkLatticeIter> iter(
1432 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001433 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001434 producer->width(), producer->height(), std::move(iter),
1435 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001436}
1437
Mike Reeda1361362017-03-07 09:37:29 -05001438void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001439 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001440 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001441 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001442 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001443 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001444 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1445 image->alphaType(), image->bounds(),
1446 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001447 this->drawProducerNine(&adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001448 } else {
1449 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001450 if (image->isLazyGenerated()) {
1451 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001452 this->drawProducerNine(&maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001453 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001454 this->drawBitmapNine(bm, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001455 }
1456 }
1457}
1458
Mike Reeda1361362017-03-07 09:37:29 -05001459void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001460 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001461 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001462 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001463 this->drawProducerNine(&maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001464}
1465
Mike Reeda1361362017-03-07 09:37:29 -05001466void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
msarett10e3d9b2016-08-18 15:46:03 -07001467 const SkCanvas::Lattice& lattice, const SkRect& dst,
1468 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001469 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001470
Mike Reeda1361362017-03-07 09:37:29 -05001471 CHECK_SHOULD_DRAW();
msarett10e3d9b2016-08-18 15:46:03 -07001472
Brian Salomon514baff2016-11-17 15:17:07 -05001473 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001474 sk_sp<GrFragmentProcessor> fp(
1475 producer->createFragmentProcessor(SkMatrix::I(),
1476 SkRect::MakeIWH(producer->width(), producer->height()),
1477 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001478 &kMode, fRenderTargetContext->getColorSpace()));
msarett10e3d9b2016-08-18 15:46:03 -07001479 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001480 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001481 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001482 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001483 return;
1484 }
1485
1486 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001487 new SkLatticeIter(lattice, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001488 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001489 producer->width(), producer->height(), std::move(iter),
1490 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001491}
1492
Mike Reeda1361362017-03-07 09:37:29 -05001493void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001494 const SkCanvas::Lattice& lattice, const SkRect& dst,
1495 const SkPaint& paint) {
1496 ASSERT_SINGLE_OWNER
1497 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001498 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001499 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001500 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1501 image->alphaType(), image->bounds(),
1502 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001503 this->drawProducerLattice(&adjuster, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001504 } else {
1505 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001506 if (image->isLazyGenerated()) {
1507 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001508 this->drawProducerLattice(&maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001509 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001510 this->drawBitmapLattice(bm, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001511 }
1512 }
1513}
1514
Mike Reeda1361362017-03-07 09:37:29 -05001515void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001516 const SkCanvas::Lattice& lattice, const SkRect& dst,
1517 const SkPaint& paint) {
1518 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001519 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001520 this->drawProducerLattice(&maker, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001521}
1522
Robert Phillips26c90e02017-03-14 14:39:29 -04001523static bool init_vertices_paint(GrContext* context, GrRenderTargetContext* rtc,
1524 const SkPaint& skPaint,
1525 const SkMatrix& matrix, SkBlendMode bmode,
1526 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001527 if (hasTexs && skPaint.getShader()) {
1528 if (hasColors) {
1529 // When there are texs and colors the shader and colors are combined using bmode.
Mike Reed185ba212017-04-28 12:31:05 -04001530 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, matrix, bmode, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001531 } else {
1532 // We have a shader, but no colors to blend it against.
1533 return SkPaintToGrPaint(context, rtc, skPaint, matrix, grPaint);
1534 }
1535 } else {
1536 if (hasColors) {
1537 // We have colors, but either have no shader or no texture coords (which implies that
1538 // we should ignore the shader).
1539 return SkPaintToGrPaintWithPrimitiveColor(context, rtc, skPaint, grPaint);
1540 } else {
1541 // No colors and no shaders. Just draw with the paint color.
Brian Osman33fa4542017-05-25 15:10:38 -04001542 return SkPaintToGrPaintNoShader(context, rtc, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001543 }
1544 }
1545}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546
Mike Reed887cdf12017-04-03 11:11:09 -04001547void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001548 const SkPoint vertices[], SkBlendMode bmode,
1549 const uint16_t indices[], int indexCount,
1550 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001551 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001552 CHECK_SHOULD_DRAW();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001553 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001554
Mike Reed2f6b5a42017-03-19 15:04:17 -04001555 SkPaint copy(paint);
1556 copy.setStyle(SkPaint::kStroke_Style);
1557 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001558
Mike Reed2f6b5a42017-03-19 15:04:17 -04001559 GrPaint grPaint;
1560 // we ignore the shader since we have no texture coordinates.
1561 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001562 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563 }
1564
Mike Reed2f6b5a42017-03-19 15:04:17 -04001565 int triangleCount = 0;
1566 int n = (nullptr == indices) ? vertexCount : indexCount;
1567 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001568 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001569 triangleCount = n / 3;
1570 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001571 case SkVertices::kTriangleStrip_VertexMode:
1572 case SkVertices::kTriangleFan_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001573 triangleCount = n - 2;
1574 break;
1575 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576
Mike Reed2f6b5a42017-03-19 15:04:17 -04001577 VertState state(vertexCount, indices, indexCount);
1578 VertState::Proc vertProc = state.chooseProc(vmode);
1579
1580 //number of indices for lines per triangle with kLines
1581 indexCount = triangleCount * 6;
1582
Brian Osmanae0c50c2017-05-25 16:56:34 -04001583 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1584 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1585 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1586
1587 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001588 int i = 0;
1589 while (vertProc(&state)) {
1590 lineIndices[i] = state.f0;
1591 lineIndices[i + 1] = state.f1;
1592 lineIndices[i + 2] = state.f1;
1593 lineIndices[i + 3] = state.f2;
1594 lineIndices[i + 4] = state.f2;
1595 lineIndices[i + 5] = state.f0;
1596 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001597 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001598
Chris Dalton3809bab2017-06-13 10:55:06 -06001599 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001600 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001601 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001602 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001603 builder.detach(),
1604 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001605}
1606
Brian Osman0b403f82017-05-26 10:39:51 -04001607void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001608 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001609 CHECK_SHOULD_DRAW();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001610 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001611
1612 SkASSERT(vertices);
1613 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001614 bool hasColors = vertices->hasColors();
1615 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001616 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001617 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001618 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1619 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001620 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001621 }
Robert Phillips26c90e02017-03-14 14:39:29 -04001622 if (!init_vertices_paint(fContext.get(), fRenderTargetContext.get(), paint, this->ctm(),
1623 mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001624 return;
1625 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001626 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001627 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001628}
1629
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001630///////////////////////////////////////////////////////////////////////////////
1631
Jim Van Verth3af1af92017-05-18 15:06:54 -04001632void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1633
1634 ASSERT_SINGLE_OWNER
1635 CHECK_SHOULD_DRAW();
1636 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1637
1638 SkPaint p;
1639 p.setColor(rec.fColor);
1640 GrPaint grPaint;
1641 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, this->ctm(),
1642 &grPaint)) {
1643 return;
1644 }
1645
1646 if (!fRenderTargetContext->drawFastShadow(this->clip(), std::move(grPaint),
1647 this->ctm(), path, rec)) {
1648 // failed to find an accelerated case
1649 this->INHERITED::drawShadow(path, rec);
1650 }
1651}
1652
1653///////////////////////////////////////////////////////////////////////////////
1654
Mike Reeda1361362017-03-07 09:37:29 -05001655void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001656 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001657 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001658 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001659 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001660 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001661 return;
1662 }
1663
Mike Reeda1361362017-03-07 09:37:29 -05001664 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001665 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001666
reedca109532015-06-25 16:25:25 -07001667 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001668 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001669
jvanverth31ff7622015-08-07 10:09:28 -07001670 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001671 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001672 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed185ba212017-04-28 12:31:05 -04001673 this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001674 return;
1675 }
1676 } else {
Mike Reeda1361362017-03-07 09:37:29 -05001677 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -04001678 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001679 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001680 }
1681 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001682
1683 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001684 fRenderTargetContext->drawAtlas(
1685 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001686}
1687
1688///////////////////////////////////////////////////////////////////////////////
1689
Mike Reeda1361362017-03-07 09:37:29 -05001690void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001691 size_t byteLength, SkScalar x, SkScalar y,
1692 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001693 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001694 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001695 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001696 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001697
Brian Salomon0166cfd2017-03-13 17:58:25 -04001698 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1699 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001700}
1701
Mike Reeda1361362017-03-07 09:37:29 -05001702void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001703 const SkScalar pos[], int scalarsPerPos,
1704 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001705 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001706 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -05001707 CHECK_SHOULD_DRAW();
jvanverth8c27a182014-10-14 08:45:50 -07001708 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001709
Brian Salomon0166cfd2017-03-13 17:58:25 -04001710 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1711 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001712 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001713}
1714
Mike Reeda1361362017-03-07 09:37:29 -05001715void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001716 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001717 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001718 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -05001719 CHECK_SHOULD_DRAW();
joshualitt9c328182015-03-23 08:13:04 -07001720
1721 SkDEBUGCODE(this->validate();)
1722
Brian Salomon0166cfd2017-03-13 17:58:25 -04001723 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1724 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001725}
1726
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001727///////////////////////////////////////////////////////////////////////////////
1728
reedb2db8982014-11-13 12:41:02 -08001729bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001730 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001731}
1732
Greg Daniela5cb7812017-06-16 09:45:32 -04001733///////////////////////////////////////////////////////////////////////////////
1734
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001735void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001736 this->flushAndSignalSemaphores(0, nullptr);
1737}
1738
1739void SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1740 GrBackendSemaphore* signalSemaphores) {
joshualittce894002016-01-11 13:29:31 -08001741 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001742
Greg Daniela5cb7812017-06-16 09:45:32 -04001743 fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
1744}
1745
1746void SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
1747 ASSERT_SINGLE_OWNER
1748
1749 fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750}
1751
1752///////////////////////////////////////////////////////////////////////////////
1753
reed76033be2015-03-14 10:54:31 -07001754SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001755 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001756
robertphillipsca6eafc2016-05-17 09:57:46 -07001757 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758
robertphillipsca6eafc2016-05-17 09:57:46 -07001759 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001760 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001761 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1762 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001763
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001764 sk_sp<GrRenderTargetContext> rtc(fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -04001765 fit,
1766 cinfo.fInfo.width(), cinfo.fInfo.height(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001767 fRenderTargetContext->config(),
1768 fRenderTargetContext->refColorSpace(),
Brian Salomon50e66d42017-05-15 16:28:07 -04001769 fRenderTargetContext->numStencilSamples(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001770 kBottomLeft_GrSurfaceOrigin,
Brian Osman11052242016-10-27 14:47:55 -04001771 &props));
1772 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001773 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001774 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001775
1776 // Skia's convention is to only clear a device if it is non-opaque.
1777 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1778
Robert Phillips9fab7e92016-11-17 12:45:04 -05001779 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1780 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001781}
1782
reede8f30622016-03-23 18:59:25 -07001783sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001784 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001785 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001786 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001787 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001788 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001789 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001790}
1791
senorblanco900c3672016-04-27 11:31:23 -07001792SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001793 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001794 // We always return a transient cache, so it is freed after each
1795 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001796 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001797}
reedf037e0b2014-10-30 11:34:15 -07001798
1799#endif