blob: 33837d391cf583ce68cfaba4a31e7ac7cfa5cc0e [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"
Brian Osman3b66ab62016-11-28 09:26:31 -05009#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
Brian Osman1cb41712017-10-19 12:54:52 -040011#include "GrColorSpaceXform.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
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059///////////////////////////////////////////////////////////////////////////////
60
bsalomon74f681d2015-06-23 14:38:48 -070061/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
62 should fail. */
63bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
64 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
65 *flags = 0;
66 if (info) {
67 switch (info->alphaType()) {
68 case kPremul_SkAlphaType:
69 break;
70 case kOpaque_SkAlphaType:
71 *flags |= SkGpuDevice::kIsOpaque_Flag;
72 break;
73 default: // If it is unpremul or unknown don't try to render
74 return false;
75 }
76 }
77 if (kClear_InitContents == init) {
78 *flags |= kNeedClear_Flag;
79 }
80 return true;
81}
82
Robert Phillips9fab7e92016-11-17 12:45:04 -050083sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
84 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070085 int width, int height,
86 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040087 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070088 return nullptr;
89 }
90 unsigned flags;
91 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
92 return nullptr;
93 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050094 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
95 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000096}
97
robertphillips24e91282016-04-29 06:46:36 -070098sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
99 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400100 GrSurfaceOrigin origin, const SkSurfaceProps* props,
101 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700102 unsigned flags;
103 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700105 }
106
Brian Osman11052242016-10-27 14:47:55 -0400107 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
108 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400109 origin, props,
110 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400111 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700113 }
114
Robert Phillips9fab7e92016-11-17 12:45:04 -0500115 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700116 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700117}
118
Brian Osman11052242016-10-27 14:47:55 -0400119static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700120 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400121 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700122 colorType = kUnknown_SkColorType;
123 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400124 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
125 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700126}
127
Robert Phillips9fab7e92016-11-17 12:45:04 -0500128SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
129 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400130 : INHERITED(make_info(renderTargetContext.get(), width, height,
131 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500132 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400133 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700134{
robertphillips1f3923e2016-07-21 07:17:54 -0700135 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700136 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
bsalomone63ffef2016-02-05 07:17:34 -0800138 if (flags & kNeedClear_Flag) {
139 this->clearAll();
140 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141}
142
Brian Osman11052242016-10-27 14:47:55 -0400143sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
144 GrContext* context,
145 SkBudgeted budgeted,
146 const SkImageInfo& origInfo,
147 int sampleCount,
148 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400149 const SkSurfaceProps* surfaceProps,
150 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000151 if (kUnknown_SkColorType == origInfo.colorType() ||
152 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700153 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000154 }
155
bsalomonafe30052015-01-16 07:32:33 -0800156 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800158 }
159
Brian Osman0c2997b2017-01-11 16:58:42 -0500160 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo, *context->caps());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400161 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
162 // they need to be exact.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400163 return context->makeDeferredRenderTargetContext(
164 SkBackingFit::kExact,
robertphillips6738c702016-07-27 12:13:51 -0700165 origInfo.width(), origInfo.height(),
Robert Phillips70b49fd2017-01-13 11:21:36 -0500166 config, origInfo.refColorSpace(), sampleCount,
Greg Daniel45d63032017-10-30 13:41:26 -0400167 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800168}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000169
Mike Reeda1361362017-03-07 09:37:29 -0500170sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700171 int left, int top,
172 SkIPoint* offset,
173 const SkImageFilter* filter) {
174 SkASSERT(srcImg->isTextureBacked());
175 SkASSERT(filter);
176
Mike Reeda1361362017-03-07 09:37:29 -0500177 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700178 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500179 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500180 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Salomonf3569f02017-10-24 12:52:33 -0400181 SkImageFilter::OutputProperties outputProperties(
182 fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700183 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700184
185 return filter->filterImage(srcImg, ctx, offset);
186}
187
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188///////////////////////////////////////////////////////////////////////////////
189
Mike Reed353196f2017-07-21 11:01:18 -0400190bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800191 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000192
Mike Reed353196f2017-07-21 11:01:18 -0400193 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500194 return false;
195 }
196
Mike Reed353196f2017-07-21 11:01:18 -0400197 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500198 if (!rec.trim(this->width(), this->height())) {
199 return false;
200 }
201
202 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000203}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000204
Mike Reed353196f2017-07-21 11:01:18 -0400205bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800206 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700207
Mike Reed353196f2017-07-21 11:01:18 -0400208 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500209 return false;
210 }
211
Mike Reed353196f2017-07-21 11:01:18 -0400212 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500213 if (!rec.trim(this->width(), this->height())) {
214 return false;
215 }
216
217 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000218}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219
reed41e010c2015-06-09 12:16:53 -0700220bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800221 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700222 return false;
223}
224
Brian Osman11052242016-10-27 14:47:55 -0400225GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700226 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400227 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700228}
229
reed8eddfb52014-12-04 07:50:14 -0800230void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800231 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500232 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500233
reed8eddfb52014-12-04 07:50:14 -0800234 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500235 fRenderTargetContext->clear(&rect, 0x0, true);
reed8eddfb52014-12-04 07:50:14 -0800236}
237
Brian Osman11052242016-10-27 14:47:55 -0400238void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800239 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800240
Brian Osman693a5402016-10-27 15:13:22 -0400241 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800242
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400243 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
244 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400245 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400246 this->context(),
247 budgeted,
248 this->imageInfo(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400249 fRenderTargetContext->numColorSamples(),
250 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400251 &this->surfaceProps(),
252 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400253 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800254 return;
255 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400256 SkASSERT(newRTC->asSurfaceProxy()->priv().isExact());
kkinnunenabcfab42015-02-22 22:53:44 -0800257
258 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400259 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800260 return;
261 }
Robert Phillipsf200a902017-01-30 13:27:37 -0500262 newRTC->copy(fRenderTargetContext->asSurfaceProxy());
kkinnunenabcfab42015-02-22 22:53:44 -0800263 }
264
Brian Osman693a5402016-10-27 15:13:22 -0400265 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800266}
267
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268///////////////////////////////////////////////////////////////////////////////
269
Mike Reeda1361362017-03-07 09:37:29 -0500270void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800271 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500272 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273
274 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400275 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
276 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700277 return;
278 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279
Brian Salomon0166cfd2017-03-13 17:58:25 -0400280 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281}
282
283// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700284static const GrPrimitiveType gPointMode2PrimitiveType[] = {
Chris Dalton3809bab2017-06-13 10:55:06 -0600285 GrPrimitiveType::kPoints,
286 GrPrimitiveType::kLines,
287 GrPrimitiveType::kLineStrip
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288};
289
Mike Reeda1361362017-03-07 09:37:29 -0500290void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000291 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800292 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500293 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294 SkScalar width = paint.getStrokeWidth();
295 if (width < 0) {
296 return;
297 }
298
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000299 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700300 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700301 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400302 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
303 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700304 return;
305 }
egdaniele61c4112014-06-12 10:24:21 -0700306 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700307 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700308 path.moveTo(pts[0]);
309 path.lineTo(pts[1]);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400310 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint),
311 GrBoolToAA(paint.isAntiAlias()), this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700312 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000313 }
314
bsalomon6ade6dd2016-09-12 12:07:17 -0700315 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500316 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700317 SkScalarNearlyEqual(scales[0], 1.f) &&
318 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700319 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400321 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500322 SkRasterClip rc(this->devClipBounds());
323 SkDraw draw;
324 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
325 draw.fMatrix = &this->ctm();
326 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500327 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328 return;
329 }
330
bsalomon6ade6dd2016-09-12 12:07:17 -0700331 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
332
Mike Reeda1361362017-03-07 09:37:29 -0500333 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700334#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
335 // This offsetting in device space matches the expectations of the Android framework for non-AA
336 // points and lines.
337 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600338 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700339 tempMatrix = *viewMatrix;
340 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
341 tempMatrix.postTranslate(kOffset, kOffset);
342 viewMatrix = &tempMatrix;
343 }
344#endif
345
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400347 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
348 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700349 return;
350 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351
Brian Osmanae0c50c2017-05-25 16:56:34 -0400352 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
353 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
354 nullptr);
355
356 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
357 std::move(vertices), &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358}
359
360///////////////////////////////////////////////////////////////////////////////
361
Mike Reeda1361362017-03-07 09:37:29 -0500362void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800363 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500364 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
bsalomona7d85ba2016-07-06 11:54:59 -0700365 // A couple reasons we might need to call drawPath.
366 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700368 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500370 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400371 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500372 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373 return;
374 }
375
376 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400377 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
378 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700379 return;
380 }
Mike Klein744fb732014-06-23 15:13:26 -0400381
bsalomon6663acf2016-05-10 09:14:17 -0700382 GrStyle style(paint);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400383 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint),
384 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385}
386
387///////////////////////////////////////////////////////////////////////////////
388
Mike Reeda1361362017-03-07 09:37:29 -0500389void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800390 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500391 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000392 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400393 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
394 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700395 return;
396 }
Mike Klein744fb732014-06-23 15:13:26 -0400397
Robert Phillipsa29a9562016-10-20 09:40:55 -0400398 SkMaskFilter* mf = paint.getMaskFilter();
Robert Phillipsd9d84852017-06-09 10:48:29 -0400399 if (mf && mf->asFragmentProcessor(nullptr)) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400400 mf = nullptr; // already handled in SkPaintToGrPaint
401 }
402
bsalomon6663acf2016-05-10 09:14:17 -0700403 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400404 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000405 // try to hit the fast path for drawing filtered round rects
406
407 SkRRect devRRect;
Mike Reeda1361362017-03-07 09:37:29 -0500408 if (rrect.transform(this->ctm(), &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000409 if (devRRect.allCornersCircular()) {
Brian Osmane191ce62017-09-14 15:43:50 -0400410 if (mf->canFilterMaskGPU(devRRect, this->devClipBounds(), this->ctm(), nullptr)) {
Robert Phillips26c90e02017-03-14 14:39:29 -0400411 if (mf->directFilterRRectMaskGPU(this->context(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400412 std::move(grPaint), this->clip(), this->ctm(),
Hal Canary144caf52016-11-07 17:57:18 -0500413 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000414 return;
415 }
416 }
417
418 }
419 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000420 }
421
Robert Phillipsa29a9562016-10-20 09:40:55 -0400422 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800423 // The only mask filter the native rrect drawing code could've handle was taken
424 // care of above.
425 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700427 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700428 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500429 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400430 this->clip(), path, paint, this->ctm(), nullptr,
Mike Reeda1361362017-03-07 09:37:29 -0500431 this->devClipBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000432 return;
433 }
Mike Klein744fb732014-06-23 15:13:26 -0400434
bsalomon6663acf2016-05-10 09:14:17 -0700435 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800436
Brian Salomon0166cfd2017-03-13 17:58:25 -0400437 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint),
438 GrBoolToAA(paint.isAntiAlias()), this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000439}
440
robertphillipsd7706102016-02-25 09:28:08 -0800441
Mike Reeda1361362017-03-07 09:37:29 -0500442void SkGpuDevice::drawDRRect(const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800443 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800444 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500445 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800446 if (outer.isEmpty()) {
447 return;
448 }
449
450 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500451 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800452 }
453
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000454 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000455
robertphillips0e7029e2015-11-30 05:45:06 -0800456 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800457 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400458 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
459 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700460 return;
461 }
robertphillips00095892016-02-29 13:50:40 -0800462
Brian Salomon0166cfd2017-03-13 17:58:25 -0400463 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
464 GrBoolToAA(paint.isAntiAlias()), this->ctm(), outer,
465 inner);
robertphillips00095892016-02-29 13:50:40 -0800466 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000467 }
468
469 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700470 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000471 path.addRRect(outer);
472 path.addRRect(inner);
473 path.setFillType(SkPath::kEvenOdd_FillType);
474
Brian Salomon0166cfd2017-03-13 17:58:25 -0400475 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
476 path, paint, this->ctm(), nullptr, this->devClipBounds(),
477 true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000478}
479
480
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000481/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482
Mike Reeda1361362017-03-07 09:37:29 -0500483void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700484 if (paint.getMaskFilter()) {
485 SkPath path;
486 region.getBoundaryPath(&path);
Mike Reeda1361362017-03-07 09:37:29 -0500487 return this->drawPath(path, paint, nullptr, false);
msarettcc319b92016-08-25 18:07:18 -0700488 }
489
490 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400491 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
492 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700493 return;
494 }
495
Brian Salomon0166cfd2017-03-13 17:58:25 -0400496 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint),
497 GrBoolToAA(paint.isAntiAlias()), this->ctm(), region,
498 GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700499}
500
Mike Reeda1361362017-03-07 09:37:29 -0500501void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800502 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500503 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
robertphillips514450c2015-11-24 05:36:02 -0800504 // Presumably the path effect warps this to something other than an oval
505 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000506 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700507 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508 path.addOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500509 this->drawPath(path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510 return;
herb11a7f7f2015-11-24 12:41:00 -0800511 }
512
robertphillips514450c2015-11-24 05:36:02 -0800513 if (paint.getMaskFilter()) {
514 // The RRect path can handle special case blurring
515 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500516 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000517 }
518
519 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400520 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
521 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700522 return;
523 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524
Brian Salomon0166cfd2017-03-13 17:58:25 -0400525 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint),
526 GrBoolToAA(paint.isAntiAlias()), this->ctm(), oval,
527 GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000528}
529
Mike Reeda1361362017-03-07 09:37:29 -0500530void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700531 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
532 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500533 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700534 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500535 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700536 return;
537 }
538 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400539 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
540 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700541 return;
542 }
543
Brian Salomon0166cfd2017-03-13 17:58:25 -0400544 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrBoolToAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500545 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500546 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700547}
548
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550
551///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700552void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700553 const SkPaint& origPaint) {
554 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500555 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400556 // Adding support for round capping would require a
557 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700558 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
559 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
560 SkASSERT(!origPaint.getPathEffect());
561 SkASSERT(!origPaint.getMaskFilter());
562
563 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
564 SkASSERT(halfWidth > 0);
565
566 SkVector v = points[1] - points[0];
567
568 SkScalar length = SkPoint::Normalize(&v);
569 if (!length) {
570 v.fX = 1.0f;
571 v.fY = 0.0f;
572 }
573
574 SkPaint newPaint(origPaint);
575 newPaint.setStyle(SkPaint::kFill_Style);
576
577 SkScalar xtraLength = 0.0f;
578 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
579 xtraLength = halfWidth;
580 }
581
582 SkPoint mid = points[0] + points[1];
583 mid.scale(0.5f);
584
585 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
586 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
587 SkMatrix m;
588 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
589
590 SkMatrix local = m;
591
Mike Reeda1361362017-03-07 09:37:29 -0500592 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700593
594 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400595 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
596 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700597 return;
598 }
599
Brian Salomon82f44312017-01-11 13:42:54 -0500600 fRenderTargetContext->fillRectWithLocalMatrix(
Brian Salomon0166cfd2017-03-13 17:58:25 -0400601 this->clip(), std::move(grPaint), GrBoolToAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700602}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603
Mike Reeda1361362017-03-07 09:37:29 -0500604void SkGpuDevice::drawPath(const SkPath& origSrcPath,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000605 const SkPaint& paint, const SkMatrix* prePathMatrix,
606 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800607 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800608 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700609 SkPoint points[2];
610 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
611 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500612 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700613 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500614 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700615 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500616 // Round capping support is currently disabled b.c. it would require a RRect
617 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500618 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700619 return;
620 }
621 }
robertphillipsff55b492015-11-24 07:56:59 -0800622 bool isClosed;
623 SkRect rect;
624 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
Mike Reeda1361362017-03-07 09:37:29 -0500625 this->drawRect(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800626 return;
627 }
628 if (origSrcPath.isOval(&rect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500629 this->drawOval(rect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800630 return;
631 }
632 SkRRect rrect;
633 if (origSrcPath.isRRect(&rrect)) {
Mike Reeda1361362017-03-07 09:37:29 -0500634 this->drawRRect(rrect, paint);
robertphillipsff55b492015-11-24 07:56:59 -0800635 return;
636 }
637 }
638
Hal Canary144caf52016-11-07 17:57:18 -0500639 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640
Brian Salomon0166cfd2017-03-13 17:58:25 -0400641 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
642 origSrcPath, paint, this->ctm(), prePathMatrix,
Mike Reeda1361362017-03-07 09:37:29 -0500643 this->devClipBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644}
645
646static const int kBmpSmallTileSize = 1 << 10;
647
648static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
649 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
650 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
651 return tilesX * tilesY;
652}
653
reed85d91782015-09-10 14:33:38 -0700654static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 if (maxTileSize <= kBmpSmallTileSize) {
656 return maxTileSize;
657 }
658
659 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
660 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
661
662 maxTileTotalTileSize *= maxTileSize * maxTileSize;
663 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
664
665 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
666 return kBmpSmallTileSize;
667 } else {
668 return maxTileSize;
669 }
670}
671
672// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
673// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700674static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800675 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800676 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700677 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700678 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000679 const SkRect* srcRectPtr,
680 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700681 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700682 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
683 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684 clippedSrcIRect->setEmpty();
685 return;
686 }
687 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
688 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700689 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000690 if (!clippedSrcRect.intersect(*srcRectPtr)) {
691 clippedSrcIRect->setEmpty();
692 return;
693 }
694 }
695 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700696 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 if (!clippedSrcIRect->intersect(bmpBounds)) {
698 clippedSrcIRect->setEmpty();
699 }
700}
701
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400702bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
703 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700704 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700705 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400706 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700707 const SkRect* srcRectPtr,
708 int maxTileSize,
709 int* tileSize,
710 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800711 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700712 // if it's larger than the max tile size, then we have no choice but tiling.
713 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400714 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400715 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
716 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700717 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
718 return true;
719 }
720
bsalomon1a1d0b82015-10-16 07:49:42 -0700721 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700722 const size_t area = imageRect.width() * imageRect.height();
723 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
724 return false;
725 }
726
reed85d91782015-09-10 14:33:38 -0700727 // At this point we know we could do the draw by uploading the entire bitmap
728 // as a texture. However, if the texture would be large compared to the
729 // cache size and we don't require most of it for this draw then tile to
730 // reduce the amount of upload and cache spill.
731
732 // assumption here is that sw bitmap size is a good proxy for its size as
733 // a texture
734 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
735 size_t cacheSize;
736 fContext->getResourceCacheLimits(nullptr, &cacheSize);
737 if (bmpSize < cacheSize / 2) {
738 return false;
739 }
740
bsalomon1a1d0b82015-10-16 07:49:42 -0700741 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
742 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400743 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
744 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400745 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700746 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
747 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500748 kBmpSmallTileSize * kBmpSmallTileSize *
749 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700750
Brian Osmand05cdc32017-02-06 13:24:47 -0500751 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700752}
753
reed85d91782015-09-10 14:33:38 -0700754bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
755 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700756 const SkMatrix& viewMatrix,
757 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800758 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400759 // If image is explicitly texture backed then we shouldn't get here.
760 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400762 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700763 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400764 GrSamplerState::Filter textureFilterMode =
765 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700766
767 int tileFilterPad;
768 if (doBicubic) {
769 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400770 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700771 tileFilterPad = 0;
772 } else {
773 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400775 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776
bsalomon8c07b7a2015-11-02 11:36:52 -0800777 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778
reed85d91782015-09-10 14:33:38 -0700779 // these are output, which we safely ignore, as we just want to know the predicate
780 int outTileSize;
781 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782
bsalomone553b642016-08-17 09:02:09 -0700783 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400784 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700785 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786}
787
Mike Reeda1361362017-03-07 09:37:29 -0500788void SkGpuDevice::drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -0400789 SkScalar x,
790 SkScalar y,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 const SkPaint& paint) {
Hal Canaryb9642382017-06-27 09:58:56 -0400792 SkMatrix m = SkMatrix::MakeTrans(x, y);
joshualittce894002016-01-11 13:29:31 -0800793 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800794 SkMatrix viewMatrix;
Mike Reeda1361362017-03-07 09:37:29 -0500795 viewMatrix.setConcat(this->ctm(), m);
reedc7ec7c92016-07-25 08:29:10 -0700796
bsalomonb1b01992015-11-18 10:56:08 -0800797 int maxTileSize = fContext->caps()->maxTileSize();
798
799 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
800 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400801 bool drawAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
802 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
bsalomonb1b01992015-11-18 10:56:08 -0800803 bitmap.height() <= maxTileSize;
804
805 bool skipTileCheck = drawAA || paint.getMaskFilter();
806
807 if (!skipTileCheck) {
808 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
809 int tileSize;
810 SkIRect clippedSrcRect;
811
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400812 GrSamplerState samplerState;
bsalomonb1b01992015-11-18 10:56:08 -0800813 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400814 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
815 paint.getFilterQuality(), viewMatrix, SkMatrix::I(), &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800816
817 int tileFilterPad;
818
819 if (doBicubic) {
820 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400821 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800822 tileFilterPad = 0;
823 } else {
824 tileFilterPad = 1;
825 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400826 samplerState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -0800827
828 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700829 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400830 SkMatrix::I(), samplerState, &srcRect, maxTileSizeForFilter,
bsalomone553b642016-08-17 09:02:09 -0700831 &tileSize, &clippedSrcRect)) {
832 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400833 samplerState, paint, SkCanvas::kStrict_SrcRectConstraint,
834 tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800835 return;
836 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000837 }
Hal Canary144caf52016-11-07 17:57:18 -0500838 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800839 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -0400840 viewMatrix, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841}
842
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000843// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
845// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000846static inline void clamped_outset_with_offset(SkIRect* iRect,
847 int outset,
848 SkPoint* offset,
849 const SkIRect& clamp) {
850 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000852 int leftClampDelta = clamp.fLeft - iRect->fLeft;
853 if (leftClampDelta > 0) {
854 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855 iRect->fLeft = clamp.fLeft;
856 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000857 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000858 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000859
860 int topClampDelta = clamp.fTop - iRect->fTop;
861 if (topClampDelta > 0) {
862 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863 iRect->fTop = clamp.fTop;
864 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000865 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000866 }
867
868 if (iRect->fRight > clamp.fRight) {
869 iRect->fRight = clamp.fRight;
870 }
871 if (iRect->fBottom > clamp.fBottom) {
872 iRect->fBottom = clamp.fBottom;
873 }
874}
875
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876// Break 'bitmap' into several tiles to draw it since it has already
877// been determined to be too large to fit in VRAM
878void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800879 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700880 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881 const SkRect& srcRect,
882 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400883 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800884 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700885 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000886 int tileSize,
887 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800888 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800889
ericrk983294f2016-04-18 09:14:00 -0700890 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800891 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700892 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800893
bsalomonc55271f2015-11-09 11:55:57 -0800894 const SkPaint* paint = &origPaint;
895 SkPaint tempPaint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400896 if (origPaint.isAntiAlias() && GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType()) {
bsalomonc55271f2015-11-09 11:55:57 -0800897 // Drop antialiasing to avoid seams at tile boundaries.
898 tempPaint = origPaint;
899 tempPaint.setAntiAlias(false);
900 paint = &tempPaint;
901 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000902 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
903
904 int nx = bitmap.width() / tileSize;
905 int ny = bitmap.height() / tileSize;
906 for (int x = 0; x <= nx; x++) {
907 for (int y = 0; y <= ny; y++) {
908 SkRect tileR;
909 tileR.set(SkIntToScalar(x * tileSize),
910 SkIntToScalar(y * tileSize),
911 SkIntToScalar((x + 1) * tileSize),
912 SkIntToScalar((y + 1) * tileSize));
913
914 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
915 continue;
916 }
917
918 if (!tileR.intersect(srcRect)) {
919 continue;
920 }
921
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922 SkIRect iTileR;
923 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700924 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
925 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500926 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700927 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400928 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000929 SkIRect iClampRect;
930
reeda5517e22015-07-14 10:54:12 -0700931 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000932 // In bleed mode we want to always expand the tile on all edges
933 // but stay within the bitmap bounds
934 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
935 } else {
936 // In texture-domain/clamp mode we only want to expand the
937 // tile on edges interior to "srcRect" (i.e., we want to
938 // not bleed across the original clamped edges)
939 srcRect.roundOut(&iClampRect);
940 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
942 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943 }
944
bsalomone553b642016-08-17 09:02:09 -0700945 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 if (bitmap.extractSubset(&tmpB, iTileR)) {
947 // now offset it to make it "local" to our tmp bitmap
948 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800949 // de-optimized this determination
950 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700951 this->drawBitmapTile(tmpB,
952 viewMatrix,
953 rectToDraw,
954 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500955 params,
bsalomone553b642016-08-17 09:02:09 -0700956 *paint,
957 constraint,
958 bicubic,
959 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 }
961 }
962 }
963}
964
bsalomone553b642016-08-17 09:02:09 -0700965void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
966 const SkMatrix& viewMatrix,
967 const SkRect& dstRect,
968 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400969 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700970 const SkPaint& paint,
971 SkCanvas::SrcRectConstraint constraint,
972 bool bicubic,
973 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800974 // We should have already handled bitmaps larger than the max texture size.
975 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
976 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700977 // We should be respecting the max tile size by the time we get here.
978 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
979 bitmap.height() <= fContext->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400980 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000981
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400982 sk_sp<GrTextureProxy> proxy =
983 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, nullptr);
Robert Phillips78075802017-03-23 11:11:59 -0400984 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 return;
986 }
bsalomone553b642016-08-17 09:02:09 -0700987
bsalomone553b642016-08-17 09:02:09 -0700988 // Compute a matrix that maps the rect we will draw to the src rect.
Robert Phillipse14d3052017-02-15 13:18:21 -0500989 const SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect,
990 SkMatrix::kFill_ScaleToFit);
joshualitt5f10b5c2015-07-09 10:24:35 -0700991
992 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
993 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400994 std::unique_ptr<GrFragmentProcessor> fp;
Brian Osmanf06ead92017-10-30 13:47:41 -0400995 GrPixelConfig config = proxy->config();
joshualitt5f10b5c2015-07-09 10:24:35 -0700996
reeda5517e22015-07-14 10:54:12 -0700997 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700999 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001001 domain.fLeft = srcRect.fLeft + 0.5f;
1002 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001004 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001005 }
1006 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -05001007 domain.fTop = srcRect.fTop + 0.5f;
1008 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -05001010 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001012 if (bicubic) {
Brian Osman5e341672017-10-18 10:23:18 -04001013 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001014 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001015 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -04001016 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001017 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001018 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001019 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
1020 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Osman5e341672017-10-18 10:23:18 -04001021 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001023 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024 }
1025
Brian Osmanf06ead92017-10-30 13:47:41 -04001026 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001027 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001028 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001029 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1030 viewMatrix, std::move(fp),
1031 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001032 return;
1033 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001035 // Coverage-based AA would cause seams between tiles.
1036 GrAA aa = GrBoolToAA(paint.isAntiAlias() &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001037 GrFSAAType::kNone != fRenderTargetContext->fsaaType());
Brian Salomon0166cfd2017-03-13 17:58:25 -04001038 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039}
1040
Mike Reeda1361362017-03-07 09:37:29 -05001041void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001043 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001044 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045
robertphillips970587b2016-07-14 14:12:55 -07001046 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047 return;
1048 }
1049
Robert Phillipse14d3052017-02-15 13:18:21 -05001050 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1051 if (!srcImg) {
1052 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001053 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054
Florin Malita53f77bd2017-04-28 13:48:37 -04001055 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001056}
1057
1058
Florin Malita53f77bd2017-04-28 13:48:37 -04001059void SkGpuDevice::drawSpecial(SkSpecialImage* special1, int left, int top, const SkPaint& paint,
1060 SkImage* clipImage,const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001061 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001062 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001063
Florin Malita53f77bd2017-04-28 13:48:37 -04001064 // TODO: clipImage support.
1065
robertphillips970587b2016-07-14 14:12:55 -07001066 SkIPoint offset = { 0, 0 };
1067
1068 sk_sp<SkSpecialImage> result;
1069 if (paint.getImageFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -05001070 result = this->filterTexture(special1, left, top,
robertphillips970587b2016-07-14 14:12:55 -07001071 &offset,
1072 paint.getImageFilter());
1073 if (!result) {
1074 return;
1075 }
1076 } else {
1077 result = sk_ref_sp(special1);
1078 }
1079
1080 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001081 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001082 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001083 return;
1084 }
robertphillips970587b2016-07-14 14:12:55 -07001085
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001086 const GrPixelConfig config = proxy->config();
1087
robertphillips970587b2016-07-14 14:12:55 -07001088 SkPaint tmpUnfiltered(paint);
1089 tmpUnfiltered.setImageFilter(nullptr);
1090
Brian Osman2240be92017-10-18 13:15:13 -04001091 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osmanf06ead92017-10-30 13:47:41 -04001092 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), config,
Brian Salomonf3569f02017-10-24 12:52:33 -04001093 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001094 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001095 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001096 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001097 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001098 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001099
1100 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001101 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1102 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001103 return;
1104 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105
robertphillips970587b2016-07-14 14:12:55 -07001106 const SkIRect& subset = result->subset();
1107
Brian Salomon82f44312017-01-11 13:42:54 -05001108 fRenderTargetContext->fillRectToRect(
Brian Salomon0166cfd2017-03-13 17:58:25 -04001109 this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001110 std::move(grPaint),
1111 GrBoolToAA(paint.isAntiAlias()),
1112 SkMatrix::I(),
Brian Salomon0166cfd2017-03-13 17:58:25 -04001113 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY, subset.width(),
1114 subset.height())),
Robert Phillips67c18d62017-01-20 12:44:06 -05001115 SkRect::Make(subset));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116}
1117
Mike Reeda1361362017-03-07 09:37:29 -05001118void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001119 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001120 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001121 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001122 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1123 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1124 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1125 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1126 const SkRect* dst = &origDst;
1127 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001128 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001129 if (!src) {
1130 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132
bsalomonb1b01992015-11-18 10:56:08 -08001133 SkMatrix srcToDstMatrix;
1134 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1135 return;
1136 }
1137 SkRect tmpSrc, tmpDst;
1138 if (src != &bmpBounds) {
1139 if (!bmpBounds.contains(*src)) {
1140 tmpSrc = *src;
1141 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142 return; // nothing to draw
1143 }
bsalomonb1b01992015-11-18 10:56:08 -08001144 src = &tmpSrc;
1145 srcToDstMatrix.mapRect(&tmpDst, *src);
1146 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 }
1148 }
1149
bsalomonb1b01992015-11-18 10:56:08 -08001150 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151
bsalomonb1b01992015-11-18 10:56:08 -08001152 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1153 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001154 bool useCoverageAA = GrFSAAType::kUnifiedMSAA != fRenderTargetContext->fsaaType() &&
1155 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1156 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001157
Brian Salomon7c8460e2017-05-12 11:36:10 -04001158 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001159
1160 if (!skipTileCheck) {
1161 int tileSize;
1162 SkIRect clippedSrcRect;
1163
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001164 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001165 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001166 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
1167 paint.getFilterQuality(), this->ctm(), srcToDstMatrix, &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001168
1169 int tileFilterPad;
1170
1171 if (doBicubic) {
1172 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001173 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001174 tileFilterPad = 0;
1175 } else {
1176 tileFilterPad = 1;
1177 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001178 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001179
1180 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001181 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001182 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1183 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001184 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001185 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001186 return;
1187 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001188 }
Hal Canary144caf52016-11-07 17:57:18 -05001189 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon34169692017-08-28 15:32:01 -04001190 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191}
1192
robertphillips6451a0c2016-07-18 08:31:31 -07001193sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001194 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1195 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips26c90e02017-03-14 14:39:29 -04001196 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->resourceProvider(), bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001197 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001198 return nullptr;
1199 }
1200
Robert Phillipse14d3052017-02-15 13:18:21 -05001201 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001202
Robert Phillipse14d3052017-02-15 13:18:21 -05001203 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1204 // the special image
1205 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1206 rect,
1207 bitmap.getGenerationID(),
1208 std::move(proxy),
1209 bitmap.refColorSpace(),
1210 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001211}
1212
reede51c3562016-07-19 14:33:20 -07001213sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001214 SkPixmap pm;
1215 if (image->isTextureBacked()) {
Robert Phillips6de99042017-01-31 11:31:39 -05001216 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef();
robertphillips6451a0c2016-07-18 08:31:31 -07001217
Robert Phillips6de99042017-01-31 11:31:39 -05001218 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1219 SkIRect::MakeWH(image->width(), image->height()),
1220 image->uniqueID(),
1221 std::move(proxy),
1222 as_IB(image)->onImageInfo().refColorSpace(),
1223 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001224 } else if (image->peekPixels(&pm)) {
1225 SkBitmap bm;
1226
1227 bm.installPixels(pm);
1228 return this->makeSpecial(bm);
1229 } else {
1230 return nullptr;
1231 }
1232}
1233
1234sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Robert Phillips63c67462017-02-15 14:19:01 -05001235 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1236 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001237 // When the device doesn't have a texture, we create a temporary texture.
1238 // TODO: we should actually only copy the portion of the source needed to apply the image
1239 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001240 proxy = GrSurfaceProxy::Copy(fContext.get(),
1241 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001242 GrMipMapped::kNo,
Robert Phillips63c67462017-02-15 14:19:01 -05001243 SkBudgeted::kYes);
1244 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001245 return nullptr;
1246 }
robertphillips1b5f9682016-07-15 08:01:12 -07001247 }
1248
1249 const SkImageInfo ii = this->imageInfo();
1250 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1251
Robert Phillipse2f7d182016-12-15 09:23:05 -05001252 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1253 srcRect,
1254 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001255 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001256 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001257 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001258}
1259
Mike Reeda1361362017-03-07 09:37:29 -05001260void SkGpuDevice::drawDevice(SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001261 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001262 SkASSERT(!paint.getImageFilter());
1263
joshualittce894002016-01-11 13:29:31 -08001264 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001265 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001266 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001267
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001269 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001270 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001271 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272 return;
1273 }
1274
Florin Malita53f77bd2017-04-28 13:48:37 -04001275 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276}
1277
Brian Salomon34169692017-08-28 15:32:01 -04001278void SkGpuDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001279 ASSERT_SINGLE_OWNER
Mike Reeda1361362017-03-07 09:37:29 -05001280 SkMatrix viewMatrix = this->ctm();
bsalomon1cf6f9b2015-12-08 10:53:43 -08001281 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001282 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001283 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001284 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1285 image->alphaType(), nullptr, nullptr,
1286 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001287 return;
Brian Salomon34169692017-08-28 15:32:01 -04001288 }
1289 SkBitmap bm;
1290 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1291 paint.getFilterQuality(), viewMatrix, SkMatrix::I())) {
1292 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001293 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001294 return;
reed85d91782015-09-10 14:33:38 -07001295 }
Brian Salomon34169692017-08-28 15:32:01 -04001296 this->drawBitmap(bm, x, y, paint);
1297 return;
1298 }
1299 if (image->isLazyGenerated()) {
1300 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
1301 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1302 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
1303 return;
1304 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001305 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Brian Salomon34169692017-08-28 15:32:01 -04001306 GrBitmapTextureMaker maker(fContext.get(), bm);
1307 this->drawTextureMaker(&maker, image->width(), image->height(), nullptr, nullptr,
1308 SkCanvas::kFast_SrcRectConstraint, viewMatrix, paint);
reeda85d4d02015-05-06 12:56:48 -07001309 }
1310}
1311
Brian Salomon34169692017-08-28 15:32:01 -04001312void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1313 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001314 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001315 uint32_t pinnedUniqueID;
Brian Salomon34169692017-08-28 15:32:01 -04001316 if (!src || src->contains(image->bounds())) {
1317 constraint = SkCanvas::kFast_SrcRectConstraint;
1318 }
Robert Phillips3798c862017-03-27 11:08:16 -04001319 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon34169692017-08-28 15:32:01 -04001320 this->drawPinnedTextureProxy(std::move(proxy), pinnedUniqueID, as_IB(image)->colorSpace(),
1321 image->alphaType(), src, &dst, constraint, this->ctm(), paint);
bsalomonc55271f2015-11-09 11:55:57 -08001322 return;
1323 }
1324 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001325 SkMatrix srcToDstRect;
1326 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1327 dst, SkMatrix::kFill_ScaleToFit);
Mike Reeda1361362017-03-07 09:37:29 -05001328 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), this->ctm(),
bsalomone553b642016-08-17 09:02:09 -07001329 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001330 // only support tiling as bitmap at the moment, so force raster-version
Brian Salomonf3569f02017-10-24 12:52:33 -04001331 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
bsalomonc55271f2015-11-09 11:55:57 -08001332 return;
1333 }
Mike Reeda1361362017-03-07 09:37:29 -05001334 this->drawBitmapRect(bm, src, dst, paint, constraint);
Brian Salomon34169692017-08-28 15:32:01 -04001335 return;
1336 }
1337 if (image->isLazyGenerated()) {
Brian Osmandf7e0752017-04-26 16:20:28 -04001338 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon34169692017-08-28 15:32:01 -04001339 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1340 this->ctm(), paint);
1341 return;
1342 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001343 if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Derek Sollenbergerdbb9e362017-08-11 14:18:59 -04001344 GrBitmapTextureMaker maker(fContext.get(), bm);
Brian Salomon34169692017-08-28 15:32:01 -04001345 this->drawTextureMaker(&maker, image->width(), image->height(), src, &dst, constraint,
1346 this->ctm(), paint);
reeda85d4d02015-05-06 12:56:48 -07001347 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001348}
1349
Mike Reeda1361362017-03-07 09:37:29 -05001350void SkGpuDevice::drawProducerNine(GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001351 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001352 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001353
joshualittedb36442015-11-19 14:29:30 -08001354 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Salomon7c8460e2017-05-12 11:36:10 -04001355 GrFSAAType::kUnifiedMSAA == fRenderTargetContext->fsaaType();
joshualitt33a5fce2015-11-18 13:28:51 -08001356 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001357 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
1358 paint.getFilterQuality(), this->ctm(), SkMatrix::I(), &doBicubic);
1359 if (useFallback || doBicubic || GrSamplerState::Filter::kNearest != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001360 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001361
1362 SkRect srcR, dstR;
1363 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001364 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
Brian Salomon34169692017-08-28 15:32:01 -04001365 this->ctm(), paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001366 }
1367 return;
1368 }
1369
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001370 static const GrSamplerState::Filter kMode = GrSamplerState::Filter::kNearest;
Brian Salomonaff329b2017-08-11 09:40:37 -04001371 auto fp = producer->createFragmentProcessor(
1372 SkMatrix::I(), SkRect::MakeIWH(producer->width(), producer->height()),
1373 GrTextureProducer::kNo_FilterConstraint, true, &kMode,
Brian Salomonf3569f02017-10-24 12:52:33 -04001374 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips1c9686b2017-06-30 08:40:28 -04001375 if (!fp) {
1376 return;
1377 }
joshualitt33a5fce2015-11-18 13:28:51 -08001378 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001379 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001380 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001381 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001382 return;
1383 }
1384
msarett10e3d9b2016-08-18 15:46:03 -07001385 std::unique_ptr<SkLatticeIter> iter(
1386 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001387 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001388 producer->width(), producer->height(), std::move(iter),
1389 dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001390}
1391
Mike Reeda1361362017-03-07 09:37:29 -05001392void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001393 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
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)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001397 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001398 image->alphaType(), pinnedUniqueID,
1399 as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001400 this->drawProducerNine(&adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001401 } else {
1402 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001403 if (image->isLazyGenerated()) {
1404 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001405 this->drawProducerNine(&maker, center, dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001406 } else if (as_IB(image)->getROPixels(&bm,
1407 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001408 this->drawBitmapNine(bm, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001409 }
1410 }
1411}
1412
Mike Reeda1361362017-03-07 09:37:29 -05001413void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001414 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001415 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001416 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001417 this->drawProducerNine(&maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001418}
1419
Mike Reeda1361362017-03-07 09:37:29 -05001420void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
msarett10e3d9b2016-08-18 15:46:03 -07001421 const SkCanvas::Lattice& lattice, const SkRect& dst,
1422 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001423 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001424
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001425 static const GrSamplerState::Filter kMode = GrSamplerState::Filter::kNearest;
Brian Salomonaff329b2017-08-11 09:40:37 -04001426 std::unique_ptr<GrFragmentProcessor> fp(producer->createFragmentProcessor(
1427 SkMatrix::I(), SkRect::MakeIWH(producer->width(), producer->height()),
1428 GrTextureProducer::kNo_FilterConstraint, true, &kMode,
Brian Salomonf3569f02017-10-24 12:52:33 -04001429 fRenderTargetContext->colorSpaceInfo().colorSpace()));
Robert Phillips1c9686b2017-06-30 08:40:28 -04001430 if (!fp) {
1431 return;
1432 }
msarett10e3d9b2016-08-18 15:46:03 -07001433 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001434 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
Mike Reeda1361362017-03-07 09:37:29 -05001435 this->ctm(), std::move(fp), producer->isAlphaOnly(),
Brian Osman11052242016-10-27 14:47:55 -04001436 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001437 return;
1438 }
1439
1440 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001441 new SkLatticeIter(lattice, dst));
Brian Salomon0166cfd2017-03-13 17:58:25 -04001442 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon82f44312017-01-11 13:42:54 -05001443 producer->width(), producer->height(), std::move(iter),
1444 dst);
msarett10e3d9b2016-08-18 15:46:03 -07001445}
1446
Mike Reeda1361362017-03-07 09:37:29 -05001447void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001448 const SkCanvas::Lattice& lattice, const SkRect& dst,
1449 const SkPaint& paint) {
1450 ASSERT_SINGLE_OWNER
1451 uint32_t pinnedUniqueID;
Robert Phillips3798c862017-03-27 11:08:16 -04001452 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
Brian Salomon4df00922017-09-07 16:34:11 +00001453 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
Greg Danielc77085d2017-11-01 16:38:48 -04001454 image->alphaType(), pinnedUniqueID,
1455 as_IB(image)->onImageInfo().colorSpace());
Mike Reeda1361362017-03-07 09:37:29 -05001456 this->drawProducerLattice(&adjuster, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001457 } else {
1458 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001459 if (image->isLazyGenerated()) {
1460 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Mike Reeda1361362017-03-07 09:37:29 -05001461 this->drawProducerLattice(&maker, lattice, dst, paint);
Brian Salomonf3569f02017-10-24 12:52:33 -04001462 } else if (as_IB(image)->getROPixels(&bm,
1463 fRenderTargetContext->colorSpaceInfo().colorSpace())) {
Mike Reeda1361362017-03-07 09:37:29 -05001464 this->drawBitmapLattice(bm, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001465 }
1466 }
1467}
1468
Mike Reeda1361362017-03-07 09:37:29 -05001469void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001470 const SkCanvas::Lattice& lattice, const SkRect& dst,
1471 const SkPaint& paint) {
1472 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001473 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Mike Reeda1361362017-03-07 09:37:29 -05001474 this->drawProducerLattice(&maker, lattice, dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001475}
1476
Brian Salomonf3569f02017-10-24 12:52:33 -04001477static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1478 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001479 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001480 if (hasTexs && skPaint.getShader()) {
1481 if (hasColors) {
1482 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001483 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1484 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001485 } else {
1486 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001487 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001488 }
1489 } else {
1490 if (hasColors) {
1491 // We have colors, but either have no shader or no texture coords (which implies that
1492 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001493 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001494 } else {
1495 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001496 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001497 }
1498 }
1499}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500
Mike Reed887cdf12017-04-03 11:11:09 -04001501void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001502 const SkPoint vertices[], SkBlendMode bmode,
1503 const uint16_t indices[], int indexCount,
1504 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001505 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001506 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001507
Mike Reed2f6b5a42017-03-19 15:04:17 -04001508 SkPaint copy(paint);
1509 copy.setStyle(SkPaint::kStroke_Style);
1510 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001511
Mike Reed2f6b5a42017-03-19 15:04:17 -04001512 GrPaint grPaint;
1513 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001514 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1515 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001516 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001517 }
1518
Mike Reed2f6b5a42017-03-19 15:04:17 -04001519 int triangleCount = 0;
1520 int n = (nullptr == indices) ? vertexCount : indexCount;
1521 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001522 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001523 triangleCount = n / 3;
1524 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001525 case SkVertices::kTriangleStrip_VertexMode:
1526 case SkVertices::kTriangleFan_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001527 triangleCount = n - 2;
1528 break;
1529 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001530
Mike Reed2f6b5a42017-03-19 15:04:17 -04001531 VertState state(vertexCount, indices, indexCount);
1532 VertState::Proc vertProc = state.chooseProc(vmode);
1533
1534 //number of indices for lines per triangle with kLines
1535 indexCount = triangleCount * 6;
1536
Brian Osmanae0c50c2017-05-25 16:56:34 -04001537 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1538 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1539 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1540
1541 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001542 int i = 0;
1543 while (vertProc(&state)) {
1544 lineIndices[i] = state.f0;
1545 lineIndices[i + 1] = state.f1;
1546 lineIndices[i + 2] = state.f1;
1547 lineIndices[i + 3] = state.f2;
1548 lineIndices[i + 4] = state.f2;
1549 lineIndices[i + 5] = state.f0;
1550 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001551 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001552
Chris Dalton3809bab2017-06-13 10:55:06 -06001553 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001554 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001555 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001556 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001557 builder.detach(),
1558 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559}
1560
Brian Osman0b403f82017-05-26 10:39:51 -04001561void SkGpuDevice::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001562 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001563 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001564
1565 SkASSERT(vertices);
1566 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001567 bool hasColors = vertices->hasColors();
1568 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001569 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001570 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001571 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
1572 mode, vertices->indices(), vertices->indexCount(), paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001573 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001574 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001575 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1576 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001577 return;
1578 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001579 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Mike Reede88a1cb2017-03-17 09:50:46 -04001580 sk_ref_sp(const_cast<SkVertices*>(vertices)));
Brian Salomon199fb872017-02-06 09:41:10 -05001581}
1582
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001583///////////////////////////////////////////////////////////////////////////////
1584
Jim Van Verth3af1af92017-05-18 15:06:54 -04001585void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1586
1587 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001588 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1589
Brian Salomon05969092017-07-13 11:20:51 -04001590 GrColor color = SkColorToPremulGrColor(rec.fColor);
1591 if (!fRenderTargetContext->drawFastShadow(this->clip(), color, this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001592 // failed to find an accelerated case
1593 this->INHERITED::drawShadow(path, rec);
1594 }
1595}
1596
1597///////////////////////////////////////////////////////////////////////////////
1598
Mike Reeda1361362017-03-07 09:37:29 -05001599void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001600 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001601 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001602 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001603 if (paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -05001604 this->INHERITED::drawAtlas(atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001605 return;
1606 }
1607
Hal Canary144caf52016-11-07 17:57:18 -05001608 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001609
reedca109532015-06-25 16:25:25 -07001610 SkPaint p(paint);
Mike Reed0acd7952017-04-28 11:12:19 -04001611 p.setShader(atlas->makeShader());
reedca109532015-06-25 16:25:25 -07001612
jvanverth31ff7622015-08-07 10:09:28 -07001613 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001614 if (colors) {
Brian Salomonf3569f02017-10-24 12:52:33 -04001615 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1616 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001617 return;
1618 }
1619 } else {
Brian Salomonf3569f02017-10-24 12:52:33 -04001620 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1621 this->ctm(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001622 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001623 }
1624 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001625
1626 SkDEBUGCODE(this->validate();)
Brian Salomon0166cfd2017-03-13 17:58:25 -04001627 fRenderTargetContext->drawAtlas(
1628 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001629}
1630
1631///////////////////////////////////////////////////////////////////////////////
1632
Mike Reeda1361362017-03-07 09:37:29 -05001633void SkGpuDevice::drawText(const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001634 size_t byteLength, SkScalar x, SkScalar y,
1635 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001636 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001637 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001638 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001639
Brian Salomon0166cfd2017-03-13 17:58:25 -04001640 fRenderTargetContext->drawText(this->clip(), paint, this->ctm(), (const char*)text, byteLength,
1641 x, y, this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642}
1643
Mike Reeda1361362017-03-07 09:37:29 -05001644void SkGpuDevice::drawPosText(const void* text, size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001645 const SkScalar pos[], int scalarsPerPos,
1646 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001647 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001648 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
jvanverth8c27a182014-10-14 08:45:50 -07001649 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001650
Brian Salomon0166cfd2017-03-13 17:58:25 -04001651 fRenderTargetContext->drawPosText(this->clip(), paint, this->ctm(), (const char*)text,
1652 byteLength, pos, scalarsPerPos, offset,
Mike Reeda1361362017-03-07 09:37:29 -05001653 this->devClipBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654}
1655
Mike Reeda1361362017-03-07 09:37:29 -05001656void SkGpuDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitt9c328182015-03-23 08:13:04 -07001657 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001658 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001659 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001660 SkDEBUGCODE(this->validate();)
1661
Brian Salomon0166cfd2017-03-13 17:58:25 -04001662 fRenderTargetContext->drawTextBlob(this->clip(), paint, this->ctm(), blob, x, y, drawFilter,
1663 this->devClipBounds());
joshualitt9c328182015-03-23 08:13:04 -07001664}
1665
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666///////////////////////////////////////////////////////////////////////////////
1667
reedb2db8982014-11-13 12:41:02 -08001668bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001669 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001670}
1671
Greg Daniela5cb7812017-06-16 09:45:32 -04001672///////////////////////////////////////////////////////////////////////////////
1673
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674void SkGpuDevice::flush() {
Greg Daniela5cb7812017-06-16 09:45:32 -04001675 this->flushAndSignalSemaphores(0, nullptr);
1676}
1677
Greg Daniel51316782017-08-02 15:10:09 +00001678GrSemaphoresSubmitted SkGpuDevice::flushAndSignalSemaphores(int numSemaphores,
1679 GrBackendSemaphore signalSemaphores[]) {
joshualittce894002016-01-11 13:29:31 -08001680 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001681
Greg Danielc64ee462017-06-15 16:59:49 -04001682 return fRenderTargetContext->prepareForExternalIO(numSemaphores, signalSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001683}
1684
Greg Danielc64ee462017-06-15 16:59:49 -04001685bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001686 ASSERT_SINGLE_OWNER
1687
Greg Danielc64ee462017-06-15 16:59:49 -04001688 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689}
1690
1691///////////////////////////////////////////////////////////////////////////////
1692
reed76033be2015-03-14 10:54:31 -07001693SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001694 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695
robertphillipsca6eafc2016-05-17 09:57:46 -07001696 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001697
robertphillipsca6eafc2016-05-17 09:57:46 -07001698 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001699 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001700 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1701 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001702
Robert Phillipsdd3b3f42017-04-24 10:57:28 -04001703 sk_sp<GrRenderTargetContext> rtc(fContext->makeDeferredRenderTargetContext(
Brian Salomonf3569f02017-10-24 12:52:33 -04001704 fit, cinfo.fInfo.width(), cinfo.fInfo.height(),
1705 fRenderTargetContext->colorSpaceInfo().config(),
1706 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Greg Daniel45d63032017-10-30 13:41:26 -04001707 fRenderTargetContext->numStencilSamples(), GrMipMapped::kNo,
1708 kBottomLeft_GrSurfaceOrigin, &props));
Brian Osman11052242016-10-27 14:47:55 -04001709 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001710 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001712
1713 // Skia's convention is to only clear a device if it is non-opaque.
1714 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1715
Robert Phillips9fab7e92016-11-17 12:45:04 -05001716 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1717 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718}
1719
reede8f30622016-03-23 18:59:25 -07001720sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001721 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001722 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001723 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001724 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Salomon50e66d42017-05-15 16:28:07 -04001725 fRenderTargetContext->numStencilSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001726 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001727}
1728
senorblanco900c3672016-04-27 11:31:23 -07001729SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001730 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001731 // We always return a transient cache, so it is freed after each
1732 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001733 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001734}
reedf037e0b2014-10-30 11:34:15 -07001735
1736#endif