blob: 6585e856d71af6ec7a1abac3d9495d6c5545a877 [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[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294 kPoints_GrPrimitiveType,
295 kLines_GrPrimitiveType,
296 kLineStrip_GrPrimitiveType
297};
298
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500299static inline bool is_int(float x) { return x == (float) sk_float_round2int(x); }
300
ethannicholas330bb952015-07-17 06:44:02 -0700301// suppress antialiasing on axis-aligned integer-coordinate lines
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500302static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
303 const SkMatrix& matrix) {
ethannicholas330bb952015-07-17 06:44:02 -0700304 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
305 return false;
306 }
307 if (count == 2) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500308 // We do not antialias horizontal or vertical lines along pixel centers, even when the ends
309 // of the line do not fully cover the first and last pixel of the line, which is slightly
310 // wrong.
311 if (!matrix.isScaleTranslate()) {
312 return true;
313 }
ethannicholas330bb952015-07-17 06:44:02 -0700314 if (pts[0].fX == pts[1].fX) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500315 SkScalar x = matrix.getScaleX() * pts[0].fX + matrix.getTranslateX();
316 return !is_int(x + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700317 }
318 if (pts[0].fY == pts[1].fY) {
Brian Salomonbd72f5a2016-12-06 16:52:24 -0500319 SkScalar y = matrix.getScaleY() * pts[0].fY + matrix.getTranslateY();
320 return !is_int(y + 0.5f);
ethannicholas330bb952015-07-17 06:44:02 -0700321 }
322 }
323 return true;
324}
325
Mike Reeda1361362017-03-07 09:37:29 -0500326void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000327 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800328 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500329 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500330 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331
332 SkScalar width = paint.getStrokeWidth();
333 if (width < 0) {
334 return;
335 }
336
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000337 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700338 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700339 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500340 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700341 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700342 return;
343 }
egdaniele61c4112014-06-12 10:24:21 -0700344 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700345 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700346 path.moveTo(pts[0]);
347 path.lineTo(pts[1]);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400348 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint),
349 GrBoolToAA(paint.isAntiAlias()), this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700350 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000351 }
352
bsalomon6ade6dd2016-09-12 12:07:17 -0700353 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500354 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700355 SkScalarNearlyEqual(scales[0], 1.f) &&
356 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700357 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700359 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
Mike Reeda1361362017-03-07 09:37:29 -0500360 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts, this->ctm())))
361 {
362 SkRasterClip rc(this->devClipBounds());
363 SkDraw draw;
364 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
365 draw.fMatrix = &this->ctm();
366 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500367 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368 return;
369 }
370
bsalomon6ade6dd2016-09-12 12:07:17 -0700371 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
372
Mike Reeda1361362017-03-07 09:37:29 -0500373 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700374#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
375 // This offsetting in device space matches the expectations of the Android framework for non-AA
376 // points and lines.
377 SkMatrix tempMatrix;
378 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
379 tempMatrix = *viewMatrix;
380 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
381 tempMatrix.postTranslate(kOffset, kOffset);
382 viewMatrix = &tempMatrix;
383 }
384#endif
385
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400387 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
388 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700389 return;
390 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391
Brian Osmanae0c50c2017-05-25 16:56:34 -0400392 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
393 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
394 nullptr);
395
396 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
397 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398}
399
400///////////////////////////////////////////////////////////////////////////////
401
Mike Reeda1361362017-03-07 09:37:29 -0500402void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800403 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500404 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500405 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407
bsalomona7d85ba2016-07-06 11:54:59 -0700408 // A couple reasons we might need to call drawPath.
409 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000410 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700411 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500413 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400414 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500415 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000416 return;
417 }
418
419 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500420 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400421 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700422 return;
423 }
Mike Klein744fb732014-06-23 15:13:26 -0400424
bsalomon6663acf2016-05-10 09:14:17 -0700425 GrStyle style(paint);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400426 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint),
427 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000428}
429
430///////////////////////////////////////////////////////////////////////////////
431
Mike Reeda1361362017-03-07 09:37:29 -0500432void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800433 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500434 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500435 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000437 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500438 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400439 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700440 return;
441 }
Mike Klein744fb732014-06-23 15:13:26 -0400442
Robert Phillipsa29a9562016-10-20 09:40:55 -0400443 SkMaskFilter* mf = paint.getMaskFilter();
Mike Reeda1361362017-03-07 09:37:29 -0500444 if (mf && mf->asFragmentProcessor(nullptr, nullptr, this->ctm())) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400445 mf = nullptr; // already handled in SkPaintToGrPaint
446 }
447
bsalomon6663acf2016-05-10 09:14:17 -0700448 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400449 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000450 // try to hit the fast path for drawing filtered round rects
451
452 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500453 if (rrect.transform(this->ctm(), &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000454 if (devRRect.allCornersCircular()) {
455 SkRect maskRect;
Mike Reeda1361362017-03-07 09:37:29 -0500456 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(),
457 this->ctm(), &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000458 SkIRect finalIRect;
459 maskRect.roundOut(&finalIRect);
Mike Reeda1361362017-03-07 09:37:29 -0500460
461 // we used to test finalIRect for quickReject, but that seems unlikely
462 // given that the original shape was not rejected...
463
Robert Phillips26c90e02017-03-14 14:39:29 -0400464 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400465 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500466 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000467 return;
468 }
469 }
470
471 }
472 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000473 }
474
Robert Phillipsa29a9562016-10-20 09:40:55 -0400475 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800476 // The only mask filter the native rrect drawing code could've handle was taken
477 // care of above.
478 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700480 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700481 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500482 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400483 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500484 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000485 return;
486 }
Mike Klein744fb732014-06-23 15:13:26 -0400487
bsalomon6663acf2016-05-10 09:14:17 -0700488 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800489
Brian Salomon0166cfd2017-03-13 17:58:25 -0400490 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint),
491 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000492}
493
robertphillipsd7706102016-02-25 09:28:08 -0800494
Mike Reeda1361362017-03-07 09:37:29 -0500495void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800496 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800497 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500498 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500499 CHECK_SHOULD_DRAW();
robertphillips0e7029e2015-11-30 05:45:06 -0800500
robertphillipsd7706102016-02-25 09:28:08 -0800501 if (outer.isEmpty()) {
502 return;
503 }
504
505 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500506 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800507 }
508
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000509 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000510
robertphillips0e7029e2015-11-30 05:45:06 -0800511 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800512 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500513 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
brianosman1638c0d2016-07-25 05:12:53 -0700514 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700515 return;
516 }
robertphillips00095892016-02-29 13:50:40 -0800517
Brian Salomon0166cfd2017-03-13 17:58:25 -0400518 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
519 GrBoolToAA(paint.isAntiAlias()), this->ctm(), outer,
520 inner);
robertphillips00095892016-02-29 13:50:40 -0800521 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000522 }
523
524 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700525 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000526 path.addRRect(outer);
527 path.addRRect(inner);
528 path.setFillType(SkPath::kEvenOdd_FillType);
529
Brian Salomon0166cfd2017-03-13 17:58:25 -0400530 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
531 path, paint, this->ctm(), nullptr, this->devClipBounds(),
532 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000533}
534
535
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000536/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537
Mike Reeda1361362017-03-07 09:37:29 -0500538void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700539 if (paint.getMaskFilter()) {
540 SkPath path;
541 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500542 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700543 }
544
545 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500546 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400547 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700548 return;
549 }
550
Brian Salomon0166cfd2017-03-13 17:58:25 -0400551 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint),
552 GrBoolToAA(paint.isAntiAlias()), this->ctm(), region,
553 GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700554}
555
Mike Reeda1361362017-03-07 09:37:29 -0500556void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800557 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500558 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500559 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000560
robertphillips514450c2015-11-24 05:36:02 -0800561 // Presumably the path effect warps this to something other than an oval
562 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700564 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000565 path.addOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500566 this->drawPath(path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567 return;
herb11a7f7f2015-11-24 12:41:00 -0800568 }
569
robertphillips514450c2015-11-24 05:36:02 -0800570 if (paint.getMaskFilter()) {
571 // The RRect path can handle special case blurring
572 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500573 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000574 }
575
576 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500577 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400578 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700579 return;
580 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581
Brian Salomon0166cfd2017-03-13 17:58:25 -0400582 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint),
583 GrBoolToAA(paint.isAntiAlias()), this->ctm(), oval,
584 GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585}
586
Mike Reeda1361362017-03-07 09:37:29 -0500587void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700588 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
589 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500590 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500591 CHECK_SHOULD_DRAW();
bsalomon4f3a0ca2016-08-22 13:14:26 -0700592
593 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500594 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700595 return;
596 }
597 GrPaint grPaint;
Mike Reeda1361362017-03-07 09:37:29 -0500598 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -0400599 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700600 return;
601 }
602
Brian Salomon0166cfd2017-03-13 17:58:25 -0400603 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500604 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500605 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700606}
607
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609
610///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700611void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700612 const SkPaint& origPaint) {
613 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500614 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -0500615 CHECK_SHOULD_DRAW();
robertphillips0851d2d2016-06-02 05:21:34 -0700616
Brian Osman11052242016-10-27 14:47:55 -0400617 // Adding support for round capping would require a
618 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700619 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
620 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
621 SkASSERT(!origPaint.getPathEffect());
622 SkASSERT(!origPaint.getMaskFilter());
623
624 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
625 SkASSERT(halfWidth > 0);
626
627 SkVector v = points[1] - points[0];
628
629 SkScalar length = SkPoint::Normalize(&v);
630 if (!length) {
631 v.fX = 1.0f;
632 v.fY = 0.0f;
633 }
634
635 SkPaint newPaint(origPaint);
636 newPaint.setStyle(SkPaint::kFill_Style);
637
638 SkScalar xtraLength = 0.0f;
639 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
640 xtraLength = halfWidth;
641 }
642
643 SkPoint mid = points[0] + points[1];
644 mid.scale(0.5f);
645
646 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
647 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
648 SkMatrix m;
649 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
650
651 SkMatrix local = m;
652
Mike Reeda1361362017-03-07 09:37:29 -0500653 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700654
655 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400656 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700657 return;
658 }
659
Brian Salomon82f44312017-01-11 13:42:54 -0500660 fRenderTargetContext->fillRectWithLocalMatrix(
Brian Salomon0166cfd2017-03-13 17:58:25 -0400661 this->clip(), std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700662}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663
Mike Reeda1361362017-03-07 09:37:29 -0500664void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665 const SkPaint& paint, const SkMatrix* prePathMatrix,
666 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800667 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800668 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700669 SkPoint points[2];
670 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
671 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500672 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700673 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500674 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700675 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500676 // Round capping support is currently disabled b.c. it would require a RRect
677 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500678 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700679 return;
680 }
681 }
robertphillipsff55b492015-11-24 07:56:59 -0800682 bool isClosed;
683 SkRect rect;
684 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
Mike Reeda1361362017-03-07 09:37:29 -0500685 this->drawRect(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800686 return;
687 }
688 if (origSrcPath.isOval(&rect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500689 this->drawOval(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800690 return;
691 }
692 SkRRect rrect;
693 if (origSrcPath.isRRect(&rrect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500694 this->drawRRect(rrect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800695 return;
696 }
697 }
698
Mike Reeda1361362017-03-07 09:37:29 -0500699 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -0500700 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701
Brian Salomon0166cfd2017-03-13 17:58:25 -0400702 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
703 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500704 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000705}
706
707static const int kBmpSmallTileSize = 1 << 10;
708
709static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
710 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
711 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
712 return tilesX * tilesY;
713}
714
reed85d91782015-09-10 14:33:38 -0700715static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716 if (maxTileSize <= kBmpSmallTileSize) {
717 return maxTileSize;
718 }
719
720 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
721 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
722
723 maxTileTotalTileSize *= maxTileSize * maxTileSize;
724 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
725
726 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
727 return kBmpSmallTileSize;
728 } else {
729 return maxTileSize;
730 }
731}
732
733// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
734// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700735static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800736 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800737 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700738 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700739 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 const SkRect* srcRectPtr,
741 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700742 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700743 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
744 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745 clippedSrcIRect->setEmpty();
746 return;
747 }
748 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
749 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700750 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 if (!clippedSrcRect.intersect(*srcRectPtr)) {
752 clippedSrcIRect->setEmpty();
753 return;
754 }
755 }
756 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700757 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000758 if (!clippedSrcIRect->intersect(bmpBounds)) {
759 clippedSrcIRect->setEmpty();
760 }
761}
762
reed85d91782015-09-10 14:33:38 -0700763bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
764 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700765 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500766 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700767 const SkRect* srcRectPtr,
768 int maxTileSize,
769 int* tileSize,
770 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800771 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700772 // if it's larger than the max tile size, then we have no choice but tiling.
773 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400774 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400775 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
776 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700777 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
778 return true;
779 }
780
bsalomon1a1d0b82015-10-16 07:49:42 -0700781 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700782 const size_t area = imageRect.width() * imageRect.height();
783 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
784 return false;
785 }
786
reed85d91782015-09-10 14:33:38 -0700787 // At this point we know we could do the draw by uploading the entire bitmap
788 // as a texture. However, if the texture would be large compared to the
789 // cache size and we don't require most of it for this draw then tile to
790 // reduce the amount of upload and cache spill.
791
792 // assumption here is that sw bitmap size is a good proxy for its size as
793 // a texture
794 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
795 size_t cacheSize;
796 fContext->getResourceCacheLimits(nullptr, &cacheSize);
797 if (bmpSize < cacheSize / 2) {
798 return false;
799 }
800
bsalomon1a1d0b82015-10-16 07:49:42 -0700801 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
802 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400803 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
804 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400805 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700806 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
807 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500808 kBmpSmallTileSize * kBmpSmallTileSize *
809 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700810
Brian Osmand05cdc32017-02-06 13:24:47 -0500811 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700812}
813
reed85d91782015-09-10 14:33:38 -0700814bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
815 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700816 const SkMatrix& viewMatrix,
817 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800818 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700819 // if image is explictly texture backed then just use the texture
Robert Phillips6de99042017-01-31 11:31:39 -0500820 if (image->isTextureBacked()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000821 return false;
822 }
823
Brian Salomon514baff2016-11-17 15:17:07 -0500824 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700825 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500826 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700827 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700828
829 int tileFilterPad;
830 if (doBicubic) {
831 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500832 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700833 tileFilterPad = 0;
834 } else {
835 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000836 }
reed85d91782015-09-10 14:33:38 -0700837 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000838
bsalomon8c07b7a2015-11-02 11:36:52 -0800839 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840
reed85d91782015-09-10 14:33:38 -0700841 // these are output, which we safely ignore, as we just want to know the predicate
842 int outTileSize;
843 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844
bsalomone553b642016-08-17 09:02:09 -0700845 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
846 params, srcRectPtr, maxTileSize, &outTileSize,
847 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848}
849
Mike Reeda1361362017-03-07 09:37:29 -0500850void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851 const SkMatrix& m,
852 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800853 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -0500854 CHECK_SHOULD_DRAW();
bsalomonb1b01992015-11-18 10:56:08 -0800855 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500856 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700857
bsalomonb1b01992015-11-18 10:56:08 -0800858 int maxTileSize = fContext->caps()->maxTileSize();
859
860 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
861 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400862 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
863 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800864 bitmap.height() <= maxTileSize;
865
866 bool skipTileCheck = drawAA || paint.getMaskFilter();
867
868 if (!skipTileCheck) {
869 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
870 int tileSize;
871 SkIRect clippedSrcRect;
872
Brian Salomon514baff2016-11-17 15:17:07 -0500873 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800874 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500875 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800876 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
877 &doBicubic);
878
879 int tileFilterPad;
880
881 if (doBicubic) {
882 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500883 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800884 tileFilterPad = 0;
885 } else {
886 tileFilterPad = 1;
887 }
888 params.setFilterMode(textureFilterMode);
889
890 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700891 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
892 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
893 &tileSize, &clippedSrcRect)) {
894 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
895 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
896 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800897 return;
898 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000899 }
Hal Canary144caf52016-11-07 17:57:18 -0500900 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800901 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -0400902 viewMatrix, this->clip(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000903}
904
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000905// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
907// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000908static inline void clamped_outset_with_offset(SkIRect* iRect,
909 int outset,
910 SkPoint* offset,
911 const SkIRect& clamp) {
912 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000914 int leftClampDelta = clamp.fLeft - iRect->fLeft;
915 if (leftClampDelta > 0) {
916 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000917 iRect->fLeft = clamp.fLeft;
918 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000919 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000921
922 int topClampDelta = clamp.fTop - iRect->fTop;
923 if (topClampDelta > 0) {
924 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 iRect->fTop = clamp.fTop;
926 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000927 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 }
929
930 if (iRect->fRight > clamp.fRight) {
931 iRect->fRight = clamp.fRight;
932 }
933 if (iRect->fBottom > clamp.fBottom) {
934 iRect->fBottom = clamp.fBottom;
935 }
936}
937
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938// Break 'bitmap' into several tiles to draw it since it has already
939// been determined to be too large to fit in VRAM
940void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800941 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700942 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943 const SkRect& srcRect,
944 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500945 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800946 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700947 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948 int tileSize,
949 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800950 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800951
ericrk983294f2016-04-18 09:14:00 -0700952 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800953 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700954 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800955
bsalomonc55271f2015-11-09 11:55:57 -0800956 const SkPaint* paint = &origPaint;
957 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400958 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800959 // Drop antialiasing to avoid seams at tile boundaries.
960 tempPaint = origPaint;
961 tempPaint.setAntiAlias(false);
962 paint = &tempPaint;
963 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000964 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
965
966 int nx = bitmap.width() / tileSize;
967 int ny = bitmap.height() / tileSize;
968 for (int x = 0; x <= nx; x++) {
969 for (int y = 0; y <= ny; y++) {
970 SkRect tileR;
971 tileR.set(SkIntToScalar(x * tileSize),
972 SkIntToScalar(y * tileSize),
973 SkIntToScalar((x + 1) * tileSize),
974 SkIntToScalar((y + 1) * tileSize));
975
976 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
977 continue;
978 }
979
980 if (!tileR.intersect(srcRect)) {
981 continue;
982 }
983
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 SkIRect iTileR;
985 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700986 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
987 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500988 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700989 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500990 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 SkIRect iClampRect;
992
reeda5517e22015-07-14 10:54:12 -0700993 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 // In bleed mode we want to always expand the tile on all edges
995 // but stay within the bitmap bounds
996 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
997 } else {
998 // In texture-domain/clamp mode we only want to expand the
999 // tile on edges interior to "srcRect" (i.e., we want to
1000 // not bleed across the original clamped edges)
1001 srcRect.roundOut(&iClampRect);
1002 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001003 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1004 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001005 }
1006
bsalomone553b642016-08-17 09:02:09 -07001007 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008 if (bitmap.extractSubset(&tmpB, iTileR)) {
1009 // now offset it to make it "local" to our tmp bitmap
1010 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -08001011 // de-optimized this determination
1012 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001013 this->drawBitmapTile(tmpB,
1014 viewMatrix,
1015 rectToDraw,
1016 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -05001017 params,
bsalomone553b642016-08-17 09:02:09 -07001018 *paint,
1019 constraint,
1020 bicubic,
1021 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022 }
1023 }
1024 }
1025}
1026
bsalomone553b642016-08-17 09:02:09 -07001027void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1028 const SkMatrix& viewMatrix,
1029 const SkRect& dstRect,
1030 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001031 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001032 const SkPaint& paint,
1033 SkCanvas::SrcRectConstraint constraint,
1034 bool bicubic,
1035 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001036 // We should have already handled bitmaps larger than the max texture size.
1037 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1038 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001039 // We should be respecting the max tile size by the time we get here.
1040 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1041 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042
Robert Phillipse14d3052017-02-15 13:18:21 -05001043 SkASSERT(SkShader::kClamp_TileMode == params.getTileModeX() &&
1044 SkShader::kClamp_TileMode == params.getTileModeY());
1045
Robert Phillips78075802017-03-23 11:11:59 -04001046 sk_sp<GrTextureProxy> proxy = GrRefCachedBitmapTextureProxy(fContext.get(), bitmap,
1047 params, nullptr);
1048 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049 return;
1050 }
brianosman500bb3e2016-07-22 10:33:07 -07001051 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001052 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001053
bsalomone553b642016-08-17 09:02:09 -07001054 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillipse14d3052017-02-15 13:18:21 -05001055 const SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect,
1056 SkMatrix::kFill_ScaleToFit);
joshualitt5f10b5c2015-07-09 10:24:35 -07001057
1058 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1059 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001060 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001061
reeda5517e22015-07-14 10:54:12 -07001062 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001064 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001066 domain.fLeft = srcRect.fLeft + 0.5f;
1067 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001069 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001070 }
1071 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001072 domain.fTop = srcRect.fTop + 0.5f;
1073 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001075 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001077 if (bicubic) {
Robert Phillips78075802017-03-23 11:11:59 -04001078 fp = GrBicubicEffect::Make(this->context()->resourceProvider(), std::move(proxy),
1079 std::move(colorSpaceXform), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001080 } else {
Robert Phillips78075802017-03-23 11:11:59 -04001081 fp = GrTextureDomainEffect::Make(this->context()->resourceProvider(), std::move(proxy),
1082 std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001083 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001084 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001085 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001086 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001087 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001088 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
Robert Phillips78075802017-03-23 11:11:59 -04001089 fp = GrBicubicEffect::Make(this->context()->resourceProvider(), std::move(proxy),
1090 std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001091 } else {
Robert Phillips78075802017-03-23 11:11:59 -04001092 fp = GrSimpleTextureEffect::Make(this->context()->resourceProvider(), std::move(proxy),
1093 std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 }
1095
joshualitt33a5fce2015-11-18 13:28:51 -08001096 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001097 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001098 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1099 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001100 return;
1101 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001103 // Coverage-based AA would cause seams between tiles.
1104 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001105 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001106 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107}
1108
Mike Reeda1361362017-03-07 09:37:29 -05001109void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001111 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001112 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001113 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114
robertphillips970587b2016-07-14 14:12:55 -07001115 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 return;
1117 }
1118
Robert Phillipse14d3052017-02-15 13:18:21 -05001119 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1120 if (!srcImg) {
1121 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001122 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123
Florin Malita53f77bd2017-04-28 13:48:37 -04001124 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001125}
1126
1127
Florin Malita53f77bd2017-04-28 13:48:37 -04001128void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
1129 SkImage* clipImage,const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001130 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001131 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001132 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001133
Florin Malita53f77bd2017-04-28 13:48:37 -04001134 // TODO: clipImage support.
1135
robertphillips970587b2016-07-14 14:12:55 -07001136 SkIPoint offset = { 0, 0 };
1137
1138 sk_sp<SkSpecialImage> result;
1139 if (paint.getImageFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -05001140 result = this->filterTexture(special1, left, top,
robertphillips970587b2016-07-14 14:12:55 -07001141 &offset,
1142 paint.getImageFilter());
1143 if (!result) {
1144 return;
1145 }
1146 } else {
1147 result = sk_ref_sp(special1);
1148 }
1149
1150 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001151 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001152 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001153 return;
1154 }
robertphillips970587b2016-07-14 14:12:55 -07001155
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001156 const GrPixelConfig config = proxy->config();
1157
robertphillips970587b2016-07-14 14:12:55 -07001158 SkPaint tmpUnfiltered(paint);
1159 tmpUnfiltered.setImageFilter(nullptr);
1160
brianosman77320db2016-09-07 08:09:10 -07001161 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001162 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001163
Robert Phillips296b1cc2017-03-15 10:42:12 -04001164 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(this->context()->resourceProvider(),
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001165 std::move(proxy),
brianosman77320db2016-09-07 08:09:10 -07001166 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001167 SkMatrix::I()));
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001168 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001169 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001170 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001171 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001172 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001173
1174 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001175 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001176 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001177 return;
1178 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179
robertphillips970587b2016-07-14 14:12:55 -07001180 const SkIRect& subset = result->subset();
1181
Brian Salomon82f44312017-01-11 13:42:54 -05001182 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001183 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001184 std::move(grPaint),
1185 GrBoolToAA(paint.isAntiAlias()),
1186 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001187 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1188 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001189 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190}
1191
Mike Reeda1361362017-03-07 09:37:29 -05001192void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001193 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001194 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001195 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001196 CHECK_SHOULD_DRAW();
reedc7ec7c92016-07-25 08:29:10 -07001197
bsalomonb1b01992015-11-18 10:56:08 -08001198 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1199 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1200 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1201 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1202 const SkRect* dst = &origDst;
1203 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001205 if (!src) {
1206 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208
bsalomonb1b01992015-11-18 10:56:08 -08001209 SkMatrix srcToDstMatrix;
1210 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1211 return;
1212 }
1213 SkRect tmpSrc, tmpDst;
1214 if (src != &bmpBounds) {
1215 if (!bmpBounds.contains(*src)) {
1216 tmpSrc = *src;
1217 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 return; // nothing to draw
1219 }
bsalomonb1b01992015-11-18 10:56:08 -08001220 src = &tmpSrc;
1221 srcToDstMatrix.mapRect(&tmpDst, *src);
1222 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 }
1224 }
1225
bsalomonb1b01992015-11-18 10:56:08 -08001226 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001227
bsalomonb1b01992015-11-18 10:56:08 -08001228 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1229 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001230 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1231 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1232 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001233
Brian Salomon7c8460e2017-05-12 11:36:10 -04001234 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001235
1236 if (!skipTileCheck) {
1237 int tileSize;
1238 SkIRect clippedSrcRect;
1239
Brian Salomon514baff2016-11-17 15:17:07 -05001240 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001241 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001242 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001243 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -08001244 &doBicubic);
1245
1246 int tileFilterPad;
1247
1248 if (doBicubic) {
1249 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001250 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001251 tileFilterPad = 0;
1252 } else {
1253 tileFilterPad = 1;
1254 }
1255 params.setFilterMode(textureFilterMode);
1256
1257 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001258 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001259 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1260 &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001261 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
bsalomone553b642016-08-17 09:02:09 -07001262 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001263 return;
1264 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001265 }
Hal Canary144caf52016-11-07 17:57:18 -05001266 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001267 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), this->clip(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268}
1269
robertphillips6451a0c2016-07-18 08:31:31 -07001270sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001271 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1272 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips26c90e02017-03-14 14:39:29 -04001273 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->resourceProvider(), bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001274 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001275 return nullptr;
1276 }
1277
Robert Phillipse14d3052017-02-15 13:18:21 -05001278 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001279
Robert Phillipse14d3052017-02-15 13:18:21 -05001280 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1281 // the special image
1282 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1283 rect,
1284 bitmap.getGenerationID(),
1285 std::move(proxy),
1286 bitmap.refColorSpace(),
1287 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001288}
1289
reede51c3562016-07-19 14:33:20 -07001290sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001291 SkPixmap pm;
1292 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001293 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001294
Robert Phillips6de99042017-01-31 11:31:39 -05001295 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1296 SkIRect::MakeWH(image->width(), image->height()),
1297 image->uniqueID(),
1298 std::move(proxy),
1299 as_IB(image)->onImageInfo().refColorSpace(),
1300 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001301 } else if (image->peekPixels(&pm)) {
1302 SkBitmap bm;
1303
1304 bm.installPixels(pm);
1305 return this->makeSpecial(bm);
1306 } else {
1307 return nullptr;
1308 }
1309}
1310
1311sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001312 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1313 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001314 // When the device doesn't have a texture, we create a temporary texture.
1315 // TODO: we should actually only copy the portion of the source needed to apply the image
1316 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001317 proxy = GrSurfaceProxy::Copy(fContext.get(),
1318 this->accessRenderTargetContext()->asSurfaceProxy(),
1319 SkBudgeted::kYes);
1320 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001321 return nullptr;
1322 }
robertphillips1b5f9682016-07-15 08:01:12 -07001323 }
1324
1325 const SkImageInfo ii = this->imageInfo();
1326 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1327
Robert Phillipse2f7d182016-12-15 09:23:05 -05001328 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1329 srcRect,
1330 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001331 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001332 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001333 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001334}
1335
Mike Reeda1361362017-03-07 09:37:29 -05001336void SkGpuDevice::drawDevice(SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001337 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001338 SkASSERT(!paint.getImageFilter());
1339
joshualittce894002016-01-11 13:29:31 -08001340 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001342 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001343
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344 // drawDevice is defined to be in device coords.
Mike Reeda1361362017-03-07 09:37:29 -05001345 CHECK_SHOULD_DRAW();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346
robertphillips1b5f9682016-07-15 08:01:12 -07001347 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001348 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001349 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 return;
1351 }
1352
Florin Malita53f77bd2017-04-28 13:48:37 -04001353 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354}
1355
Mike Reeda1361362017-03-07 09:37:29 -05001356void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y,
reeda85d4d02015-05-06 12:56:48 -07001357 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001358 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001359 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001360 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001361 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001362
1363 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001364 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001365 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1366 image->alphaType(), image->bounds(),
1367 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001368 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001369 viewMatrix, this->clip(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001370 return;
reed85d91782015-09-10 14:33:38 -07001371 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001372 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001373 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
Mike Reeda1361362017-03-07 09:37:29 -05001374 paint.getFilterQuality(), this->ctm(), SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001375 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001376 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
reed85d91782015-09-10 14:33:38 -07001377 return;
1378 }
Mike Reeda1361362017-03-07 09:37:29 -05001379 this->drawBitmap(bm, SkMatrix::MakeTrans(x, y), paint);
Brian Osmandf7e0752017-04-26 16:20:28 -04001380 } else if (image->isLazyGenerated()) {
Mike Reeda1361362017-03-07 09:37:29 -05001381 CHECK_SHOULD_DRAW();
Brian Osmandf7e0752017-04-26 16:20:28 -04001382 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001383 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001384 viewMatrix, this->clip(), paint);
Brian Osman61624f02016-12-09 14:51:59 -05001385 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001386 this->drawBitmap(bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001387 }
reeda85d4d02015-05-06 12:56:48 -07001388 }
1389}
1390
Mike Reeda1361362017-03-07 09:37:29 -05001391void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001392 const SkRect& dst, const SkPaint& paint,
1393 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001394 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001395 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001396 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001397 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001398 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1399 image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001400 as_IB(image)->onImageInfo().colorSpace());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001401 this->drawTextureProducer(&adjuster, src, &dst, constraint, this->ctm(), this->clip(),
1402 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001403 return;
1404 }
1405 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001406 SkMatrix srcToDstRect;
1407 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1408 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001409 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001410 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001411 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman61624f02016-12-09 14:51:59 -05001412 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001413 return;
1414 }
Mike Reeda1361362017-03-07 09:37:29 -05001415 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Osmandf7e0752017-04-26 16:20:28 -04001416 } else if (image->isLazyGenerated()) {
Mike Reeda1361362017-03-07 09:37:29 -05001417 CHECK_SHOULD_DRAW();
Brian Osmandf7e0752017-04-26 16:20:28 -04001418 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001419 this->drawTextureProducer(&maker, src, &dst, constraint, this->ctm(), this->clip(), paint);
Brian Osman61624f02016-12-09 14:51:59 -05001420 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001421 this->drawBitmapRect(bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001422 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001423}
1424
Mike Reeda1361362017-03-07 09:37:29 -05001425void SkGpuDevice::drawProducerNine(GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001426 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001427 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001428
Mike Reeda1361362017-03-07 09:37:29 -05001429 CHECK_SHOULD_DRAW();
joshualitt33a5fce2015-11-18 13:28:51 -08001430
joshualittedb36442015-11-19 14:29:30 -08001431 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Salomon7c8460e2017-05-12 11:36:10 -04001432 GrFSAAType::kUnifiedMSAA == fRenderTargetContext->fsaaType();
joshualitt33a5fce2015-11-18 13:28:51 -08001433 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001434 GrSamplerParams::FilterMode textureFilterMode =
Mike Reeda1361362017-03-07 09:37:29 -05001435 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), this->ctm(), SkMatrix::I(),
bsalomon2bbd0c62015-12-09 12:50:56 -08001436 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001437 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001438 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001439
1440 SkRect srcR, dstR;
1441 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001442 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon0166cfd2017-03-13 17:58:25 -04001443 this->ctm(), this->clip(), paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001444 }
1445 return;
1446 }
1447
Brian Salomon514baff2016-11-17 15:17:07 -05001448 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001449 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001450 producer->createFragmentProcessor(SkMatrix::I(),
1451 SkRect::MakeIWH(producer->width(), producer->height()),
1452 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001453 &kMode, fRenderTargetContext->getColorSpace()));
joshualitt33a5fce2015-11-18 13:28:51 -08001454 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001455 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001456 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001457 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001458 return;
1459 }
1460
msarett10e3d9b2016-08-18 15:46:03 -07001461 std::unique_ptr<SkLatticeIter> iter(
1462 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001463 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001464 producer->width(), producer->height(), std::move(iter),
1465 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001466}
1467
Mike Reeda1361362017-03-07 09:37:29 -05001468void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001469 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001470 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001471 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001472 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001473 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001474 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1475 image->alphaType(), image->bounds(),
1476 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001477 this->drawProducerNine(&adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001478 } else {
1479 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001480 if (image->isLazyGenerated()) {
1481 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001482 this->drawProducerNine(&maker, center, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001483 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001484 this->drawBitmapNine(bm, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001485 }
1486 }
1487}
1488
Mike Reeda1361362017-03-07 09:37:29 -05001489void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001490 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001491 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001492 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001493 this->drawProducerNine(&maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001494}
1495
Mike Reeda1361362017-03-07 09:37:29 -05001496void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
msarett10e3d9b2016-08-18 15:46:03 -07001497 const SkCanvas::Lattice& lattice, const SkRect& dst,
1498 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001499 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001500
Mike Reeda1361362017-03-07 09:37:29 -05001501 CHECK_SHOULD_DRAW();
msarett10e3d9b2016-08-18 15:46:03 -07001502
Brian Salomon514baff2016-11-17 15:17:07 -05001503 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001504 sk_sp<GrFragmentProcessor> fp(
1505 producer->createFragmentProcessor(SkMatrix::I(),
1506 SkRect::MakeIWH(producer->width(), producer->height()),
1507 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman61624f02016-12-09 14:51:59 -05001508 &kMode, fRenderTargetContext->getColorSpace()));
msarett10e3d9b2016-08-18 15:46:03 -07001509 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001510 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001511 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001512 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001513 return;
1514 }
1515
1516 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001517 new SkLatticeIter(lattice, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001518 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001519 producer->width(), producer->height(), std::move(iter),
1520 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001521}
1522
Mike Reeda1361362017-03-07 09:37:29 -05001523void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001524 const SkCanvas::Lattice& lattice, const SkRect& dst,
1525 const SkPaint& paint) {
1526 ASSERT_SINGLE_OWNER
1527 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001528 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Mike Reeda1361362017-03-07 09:37:29 -05001529 CHECK_SHOULD_DRAW();
Robert Phillips3798c862017-03-27 11:08:16 -04001530 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1531 image->alphaType(), image->bounds(),
1532 pinnedUniqueID, as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001533 this->drawProducerLattice(&adjuster, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001534 } else {
1535 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001536 if (image->isLazyGenerated()) {
1537 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001538 this->drawProducerLattice(&maker, lattice, dst, paint);
Brian Osman61624f02016-12-09 14:51:59 -05001539 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->getColorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001540 this->drawBitmapLattice(bm, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001541 }
1542 }
1543}
1544
Mike Reeda1361362017-03-07 09:37:29 -05001545void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001546 const SkCanvas::Lattice& lattice, const SkRect& dst,
1547 const SkPaint& paint) {
1548 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001549 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001550 this->drawProducerLattice(&maker, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001551}
1552
Robert Phillips26c90e02017-03-14 14:39:29 -04001553static bool init_vertices_paint(GrContext* context, GrRenderTargetContext* rtc,
1554 const SkPaint& skPaint,
1555 const SkMatrix& matrix, SkBlendMode bmode,
1556 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001557 if (hasTexs && skPaint.getShader()) {
1558 if (hasColors) {
1559 // When there are texs and colors the shader and colors are combined using bmode.
Mike Reed185ba212017-04-28 12:31:05 -04001560 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, matrix, bmode, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001561 } else {
1562 // We have a shader, but no colors to blend it against.
1563 return SkPaintToGrPaint(context, rtc, skPaint, matrix, grPaint);
1564 }
1565 } else {
1566 if (hasColors) {
1567 // We have colors, but either have no shader or no texture coords (which implies that
1568 // we should ignore the shader).
1569 return SkPaintToGrPaintWithPrimitiveColor(context, rtc, skPaint, grPaint);
1570 } else {
1571 // No colors and no shaders. Just draw with the paint color.
Brian Osman33fa4542017-05-25 15:10:38 -04001572 return SkPaintToGrPaintNoShader(context, rtc, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001573 }
1574 }
1575}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576
Mike Reed887cdf12017-04-03 11:11:09 -04001577void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001578 const SkPoint vertices[], SkBlendMode bmode,
1579 const uint16_t indices[], int indexCount,
1580 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001581 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001582 CHECK_SHOULD_DRAW();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001583 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001584
Mike Reed2f6b5a42017-03-19 15:04:17 -04001585 SkPaint copy(paint);
1586 copy.setStyle(SkPaint::kStroke_Style);
1587 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001588
Mike Reed2f6b5a42017-03-19 15:04:17 -04001589 GrPaint grPaint;
1590 // we ignore the shader since we have no texture coordinates.
1591 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001592 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593 }
1594
Mike Reed2f6b5a42017-03-19 15:04:17 -04001595 int triangleCount = 0;
1596 int n = (nullptr == indices) ? vertexCount : indexCount;
1597 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001598 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001599 triangleCount = n / 3;
1600 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001601 case SkVertices::kTriangleStrip_VertexMode:
1602 case SkVertices::kTriangleFan_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001603 triangleCount = n - 2;
1604 break;
1605 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001606
Mike Reed2f6b5a42017-03-19 15:04:17 -04001607 VertState state(vertexCount, indices, indexCount);
1608 VertState::Proc vertProc = state.chooseProc(vmode);
1609
1610 //number of indices for lines per triangle with kLines
1611 indexCount = triangleCount * 6;
1612
Brian Osmanae0c50c2017-05-25 16:56:34 -04001613 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1614 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1615 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1616
1617 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001618 int i = 0;
1619 while (vertProc(&state)) {
1620 lineIndices[i] = state.f0;
1621 lineIndices[i + 1] = state.f1;
1622 lineIndices[i + 2] = state.f1;
1623 lineIndices[i + 3] = state.f2;
1624 lineIndices[i + 4] = state.f2;
1625 lineIndices[i + 5] = state.f0;
1626 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001627 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001628
1629 GrPrimitiveType primitiveType = kLines_GrPrimitiveType;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001630 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001631 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001632 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001633 builder.detach(),
1634 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001635}
1636
Brian Osman0b403f82017-05-26 10:39:51 -04001637void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001638 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001639 CHECK_SHOULD_DRAW();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001641
1642 SkASSERT(vertices);
1643 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001644 bool hasColors = vertices->hasColors();
1645 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001646 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001647 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001648 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1649 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001650 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001651 }
Robert Phillips26c90e02017-03-14 14:39:29 -04001652 if (!init_vertices_paint(fContext.get(), fRenderTargetContext.get(), paint, this->ctm(),
1653 mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001654 return;
1655 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001656 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001657 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001658}
1659
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001660///////////////////////////////////////////////////////////////////////////////
1661
Jim Van Verth3af1af92017-05-18 15:06:54 -04001662void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1663
1664 ASSERT_SINGLE_OWNER
1665 CHECK_SHOULD_DRAW();
1666 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1667
1668 SkPaint p;
1669 p.setColor(rec.fColor);
1670 GrPaint grPaint;
1671 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, this->ctm(),
1672 &grPaint)) {
1673 return;
1674 }
1675
1676 if (!fRenderTargetContext->drawFastShadow(this->clip(), std::move(grPaint),
1677 this->ctm(), path, rec)) {
1678 // failed to find an accelerated case
1679 this->INHERITED::drawShadow(path, rec);
1680 }
1681}
1682
1683///////////////////////////////////////////////////////////////////////////////
1684
Mike Reeda1361362017-03-07 09:37:29 -05001685void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001686 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001687 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001688 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001689 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001690 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001691 return;
1692 }
1693
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());
herb11a7f7f2015-11-24 12:41:00 -08001696
reedca109532015-06-25 16:25:25 -07001697 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001698 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001699
jvanverth31ff7622015-08-07 10:09:28 -07001700 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001701 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001702 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed185ba212017-04-28 12:31:05 -04001703 this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001704 return;
1705 }
1706 } else {
Mike Reeda1361362017-03-07 09:37:29 -05001707 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, this->ctm(),
Brian Osman11052242016-10-27 14:47:55 -04001708 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001709 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001710 }
1711 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001712
1713 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001714 fRenderTargetContext->drawAtlas(
1715 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001716}
1717
1718///////////////////////////////////////////////////////////////////////////////
1719
Mike Reeda1361362017-03-07 09:37:29 -05001720void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001721 size_t byteLength, SkScalar x, SkScalar y,
1722 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001723 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001724 CHECK_SHOULD_DRAW();
Hal Canary144caf52016-11-07 17:57:18 -05001725 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001726 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001727
Brian Salomon0166cfd2017-03-13 17:58:25 -04001728 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1729 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730}
1731
Mike Reeda1361362017-03-07 09:37:29 -05001732void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001733 const SkScalar pos[], int scalarsPerPos,
1734 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001735 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001736 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -05001737 CHECK_SHOULD_DRAW();
jvanverth8c27a182014-10-14 08:45:50 -07001738 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001739
Brian Salomon0166cfd2017-03-13 17:58:25 -04001740 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1741 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001742 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743}
1744
Mike Reeda1361362017-03-07 09:37:29 -05001745void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001746 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001747 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001748 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
Mike Reeda1361362017-03-07 09:37:29 -05001749 CHECK_SHOULD_DRAW();
joshualitt9c328182015-03-23 08:13:04 -07001750
1751 SkDEBUGCODE(this->validate();)
1752
Brian Salomon0166cfd2017-03-13 17:58:25 -04001753 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1754 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001755}
1756
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001757///////////////////////////////////////////////////////////////////////////////
1758
reedb2db8982014-11-13 12:41:02 -08001759bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001760 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761}
1762
1763void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001764 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001765
Brian Osman11052242016-10-27 14:47:55 -04001766 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001767}
1768
1769///////////////////////////////////////////////////////////////////////////////
1770
reed76033be2015-03-14 10:54:31 -07001771SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001772 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001773
robertphillipsca6eafc2016-05-17 09:57:46 -07001774 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775
robertphillipsca6eafc2016-05-17 09:57:46 -07001776 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001777 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001778 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1779 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001780
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001781 sk_sp<GrRenderTargetContext> rtc(fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -04001782 fit,
1783 cinfo.fInfo.width(), cinfo.fInfo.height(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001784 fRenderTargetContext->config(),
1785 fRenderTargetContext->refColorSpace(),
Brian Salomon50e66d42017-05-15 16:28:07 -04001786 fRenderTargetContext->numStencilSamples(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001787 kBottomLeft_GrSurfaceOrigin,
Brian Osman11052242016-10-27 14:47:55 -04001788 &props));
1789 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001790 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001792
1793 // Skia's convention is to only clear a device if it is non-opaque.
1794 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1795
Robert Phillips9fab7e92016-11-17 12:45:04 -05001796 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1797 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001798}
1799
reede8f30622016-03-23 18:59:25 -07001800sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001801 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001802 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001803 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001804 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001805 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001806 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001807}
1808
senorblanco900c3672016-04-27 11:31:23 -07001809SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001810 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001811 // We always return a transient cache, so it is freed after each
1812 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001813 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001814}
reedf037e0b2014-10-30 11:34:15 -07001815
1816#endif