blob: dbb4699c1a27ba322baf112f256698f6a0f36ca9 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
bsalomonc55271f2015-11-09 11:55:57 -080014#include "GrImageIDTextureAdjuster.h"
bsalomon6663acf2016-05-10 09:14:17 -070015#include "GrStyle.h"
egdanielbbcb38d2014-06-19 10:19:29 -070016#include "GrTracing.h"
robertphillips714712b2016-08-04 06:20:45 -070017
robertphillips30d78412014-11-24 09:49:17 -080018#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070019#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070021#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070022#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070023#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080024#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070026#include "SkImageFilterCache.h"
msarettc573a402016-08-02 08:05:56 -070027#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070031#include "SkPictureData.h"
reed1e7f5e72016-04-27 07:49:17 -070032#include "SkRasterClip.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080034#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070035#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080038#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000039#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000040#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000041#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070042#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070043#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080044#include "effects/GrBicubicEffect.h"
45#include "effects/GrDashingEffect.h"
46#include "effects/GrSimpleTextureEffect.h"
47#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080048#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049
reedf037e0b2014-10-30 11:34:15 -070050#if SK_SUPPORT_GPU
51
joshualittce894002016-01-11 13:29:31 -080052#define ASSERT_SINGLE_OWNER \
53 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
54
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055#if 0
56 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058 do { \
59 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080060 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 } while (0)
62#else
joshualitt5531d512014-12-17 15:50:11 -080063 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064#endif
65
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066///////////////////////////////////////////////////////////////////////////////
67
bsalomon74f681d2015-06-23 14:38:48 -070068/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
69 should fail. */
70bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
71 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
72 *flags = 0;
73 if (info) {
74 switch (info->alphaType()) {
75 case kPremul_SkAlphaType:
76 break;
77 case kOpaque_SkAlphaType:
78 *flags |= SkGpuDevice::kIsOpaque_Flag;
79 break;
80 default: // If it is unpremul or unknown don't try to render
81 return false;
82 }
83 }
84 if (kClear_InitContents == init) {
85 *flags |= kNeedClear_Flag;
86 }
87 return true;
88}
89
Brian Osman11052242016-10-27 14:47:55 -040090sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070091 int width, int height,
92 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040093 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070094 return nullptr;
95 }
96 unsigned flags;
97 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
98 return nullptr;
99 }
Brian Osman11052242016-10-27 14:47:55 -0400100 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(renderTargetContext), width, height,
101 flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000102}
103
robertphillips24e91282016-04-29 06:46:36 -0700104sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
105 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700106 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700107 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700108 unsigned flags;
109 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700110 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700111 }
112
Brian Osman11052242016-10-27 14:47:55 -0400113 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
114 info, sampleCount,
115 origin, props));
116 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700117 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700118 }
119
Brian Osman11052242016-10-27 14:47:55 -0400120 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700121 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700122}
123
Brian Osman11052242016-10-27 14:47:55 -0400124static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700125 SkColorType colorType;
126 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
127 colorType = kUnknown_SkColorType;
128 }
129 return SkImageInfo::Make(w, h, colorType,
130 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
131 sk_ref_sp(context->getColorSpace()));
132}
133
Brian Osman11052242016-10-27 14:47:55 -0400134SkGpuDevice::SkGpuDevice(sk_sp<GrRenderTargetContext> renderTargetContext, int width, int height,
135 unsigned flags)
136 : INHERITED(make_info(renderTargetContext.get(), width, height,
137 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
138 , fContext(SkRef(renderTargetContext->accessRenderTarget()->getContext()))
139 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700140{
robertphillips1f3923e2016-07-21 07:17:54 -0700141 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700142 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143
bsalomone63ffef2016-02-05 07:17:34 -0800144 if (flags & kNeedClear_Flag) {
145 this->clearAll();
146 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147}
148
Brian Osman11052242016-10-27 14:47:55 -0400149sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
150 GrContext* context,
151 SkBudgeted budgeted,
152 const SkImageInfo& origInfo,
153 int sampleCount,
154 GrSurfaceOrigin origin,
155 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000156 if (kUnknown_SkColorType == origInfo.colorType() ||
157 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700158 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000159 }
160
bsalomonafe30052015-01-16 07:32:33 -0800161 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800163 }
164
reede5ea5002014-09-03 11:54:58 -0700165 SkColorType ct = origInfo.colorType();
166 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700167 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700168 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700169 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800170 }
171 if (kOpaque_SkAlphaType != at) {
172 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000173 }
brianosman6b086522016-04-14 12:39:00 -0700174
brianosmanb109b8c2016-06-16 13:03:24 -0700175 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000176
Brian Osman11052242016-10-27 14:47:55 -0400177 return context->makeRenderTargetContext(SkBackingFit::kExact, // Why exact?
robertphillips6738c702016-07-27 12:13:51 -0700178 origInfo.width(), origInfo.height(),
179 config, sk_ref_sp(cs), sampleCount,
180 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800181}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000182
robertphillips970587b2016-07-14 14:12:55 -0700183sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
184 SkSpecialImage* srcImg,
185 int left, int top,
186 SkIPoint* offset,
187 const SkImageFilter* filter) {
188 SkASSERT(srcImg->isTextureBacked());
189 SkASSERT(filter);
190
191 SkMatrix matrix = *draw.fMatrix;
192 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
193 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
194 SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400195 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700196 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700197
198 return filter->filterImage(srcImg, ctx, offset);
199}
200
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201///////////////////////////////////////////////////////////////////////////////
202
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000203bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
204 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800205 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000206
Brian Osman11052242016-10-27 14:47:55 -0400207 return fRenderTargetContext->readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000208}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000209
robertphillips1da3ecd2016-08-31 14:54:15 -0700210bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
211 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800212 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700213
Brian Osman11052242016-10-27 14:47:55 -0400214 return fRenderTargetContext->writePixels(srcInfo, srcPixels, srcRowBytes, x, y);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000215}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216
reed41e010c2015-06-09 12:16:53 -0700217bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800218 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700219 return false;
220}
221
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000222// call this every draw call, to ensure that the context reflects our state,
223// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800224void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800225 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000226
reed2c9e2002016-07-25 08:05:22 -0700227 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228}
229
Brian Osman11052242016-10-27 14:47:55 -0400230GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700231 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400232 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700233}
234
reed8eddfb52014-12-04 07:50:14 -0800235void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800236 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800237 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800238 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800239 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman11052242016-10-27 14:47:55 -0400240 fRenderTargetContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800241}
242
Brian Osman11052242016-10-27 14:47:55 -0400243void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800244 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800245
Brian Osman693a5402016-10-27 15:13:22 -0400246 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800247
Brian Osman693a5402016-10-27 15:13:22 -0400248 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400249 this->context(),
250 budgeted,
251 this->imageInfo(),
252 fRenderTargetContext->numColorSamples(),
253 fRenderTargetContext->origin(),
254 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400255 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800256 return;
257 }
258
259 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400260 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800261 return;
262 }
Brian Osman693a5402016-10-27 15:13:22 -0400263 newRTC->copySurface(fRenderTargetContext->asTexture().get(),
264 SkIRect::MakeWH(this->width(), this->height()),
265 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800266 }
267
Brian Osman693a5402016-10-27 15:13:22 -0400268 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800269}
270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271///////////////////////////////////////////////////////////////////////////////
272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800274 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800275 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800276 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277
278 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400279 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
280 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700281 return;
282 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283
Brian Osman11052242016-10-27 14:47:55 -0400284 fRenderTargetContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000285}
286
287// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700288static const GrPrimitiveType gPointMode2PrimitiveType[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289 kPoints_GrPrimitiveType,
290 kLines_GrPrimitiveType,
291 kLineStrip_GrPrimitiveType
292};
293
ethannicholas330bb952015-07-17 06:44:02 -0700294// suppress antialiasing on axis-aligned integer-coordinate lines
295static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
296 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
297 return false;
298 }
299 if (count == 2) {
300 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
301 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800302 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700303 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800304 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700305 // the two end pixels as being the lesser of two evils.
306 if (pts[0].fX == pts[1].fX) {
307 return ((int) pts[0].fX) != pts[0].fX;
308 }
309 if (pts[0].fY == pts[1].fY) {
310 return ((int) pts[0].fY) != pts[0].fY;
311 }
312 }
313 return true;
314}
315
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
317 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800318 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800319 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800320 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321
322 SkScalar width = paint.getStrokeWidth();
323 if (width < 0) {
324 return;
325 }
326
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000327 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700328 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700329 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400330 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700331 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700332 return;
333 }
egdaniele61c4112014-06-12 10:24:21 -0700334 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700335 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700336 path.moveTo(pts[0]);
337 path.lineTo(pts[1]);
Brian Osman11052242016-10-27 14:47:55 -0400338 fRenderTargetContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700339 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000340 }
341
bsalomon6ade6dd2016-09-12 12:07:17 -0700342 SkScalar scales[2];
343 bool isHairline = (0 == width) || (1 == width && draw.fMatrix->getMinMaxScales(scales) &&
344 SkScalarNearlyEqual(scales[0], 1.f) &&
345 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700346 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700348 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700349 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000350 draw.drawPoints(mode, count, pts, paint, true);
351 return;
352 }
353
bsalomon6ade6dd2016-09-12 12:07:17 -0700354 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
355
356 const SkMatrix* viewMatrix = draw.fMatrix;
357#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
358 // This offsetting in device space matches the expectations of the Android framework for non-AA
359 // points and lines.
360 SkMatrix tempMatrix;
361 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
362 tempMatrix = *viewMatrix;
363 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
364 tempMatrix.postTranslate(kOffset, kOffset);
365 viewMatrix = &tempMatrix;
366 }
367#endif
368
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400370 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
371 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700372 return;
373 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000374
Brian Osman11052242016-10-27 14:47:55 -0400375 fRenderTargetContext->drawVertices(fClip,
376 grPaint,
377 *viewMatrix,
378 primitiveType,
379 SkToS32(count),
380 (SkPoint*)pts,
381 nullptr,
382 nullptr,
383 nullptr,
384 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385}
386
387///////////////////////////////////////////////////////////////////////////////
388
robertphillipsff55b492015-11-24 07:56:59 -0800389void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800390 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800391 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800392 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394
bsalomona7d85ba2016-07-06 11:54:59 -0700395 // A couple reasons we might need to call drawPath.
396 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700398 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000399 path.addRect(rect);
Brian Osman11052242016-10-27 14:47:55 -0400400 GrBlurUtils::drawPathWithMaskFilter(fContext, fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800401 fClip, path, paint,
402 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700403 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404 return;
405 }
406
407 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400408 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
409 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700410 return;
411 }
Mike Klein744fb732014-06-23 15:13:26 -0400412
bsalomon6663acf2016-05-10 09:14:17 -0700413 GrStyle style(paint);
Brian Osman11052242016-10-27 14:47:55 -0400414 fRenderTargetContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415}
416
417///////////////////////////////////////////////////////////////////////////////
418
robertphillips9aff85a2016-08-05 07:51:29 -0700419void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect,
joshualitt5531d512014-12-17 15:50:11 -0800420 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800421 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800422 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800423 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000425 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400426 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
427 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700428 return;
429 }
Mike Klein744fb732014-06-23 15:13:26 -0400430
Robert Phillipsa29a9562016-10-20 09:40:55 -0400431 SkMaskFilter* mf = paint.getMaskFilter();
432 if (mf && mf->asFragmentProcessor(nullptr, nullptr, *draw.fMatrix)) {
433 mf = nullptr; // already handled in SkPaintToGrPaint
434 }
435
bsalomon6663acf2016-05-10 09:14:17 -0700436 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400437 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000438 // try to hit the fast path for drawing filtered round rects
439
440 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700441 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000442 if (devRRect.allCornersCircular()) {
443 SkRect maskRect;
Robert Phillipsa29a9562016-10-20 09:40:55 -0400444 if (mf->canFilterMaskGPU(devRRect, draw.fRC->getBounds(),
445 *draw.fMatrix, &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000446 SkIRect finalIRect;
447 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700448 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000449 // clipped out
450 return;
451 }
Brian Osman11052242016-10-27 14:47:55 -0400452 if (mf->directFilterRRectMaskGPU(fContext, fRenderTargetContext.get(), &grPaint,
453 fClip, *draw.fMatrix, style.strokeRec(), rrect,
Robert Phillipsa29a9562016-10-20 09:40:55 -0400454 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000455 return;
456 }
457 }
458
459 }
460 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000461 }
462
Robert Phillipsa29a9562016-10-20 09:40:55 -0400463 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800464 // The only mask filter the native rrect drawing code could've handle was taken
465 // care of above.
466 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700468 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700469 path.addRRect(rrect);
Brian Osman11052242016-10-27 14:47:55 -0400470 GrBlurUtils::drawPathWithMaskFilter(fContext, fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800471 fClip, path, paint,
472 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700473 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000474 return;
475 }
Mike Klein744fb732014-06-23 15:13:26 -0400476
bsalomon6663acf2016-05-10 09:14:17 -0700477 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800478
Brian Osman11052242016-10-27 14:47:55 -0400479 fRenderTargetContext->drawRRect(fClip, grPaint, *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000480}
481
robertphillipsd7706102016-02-25 09:28:08 -0800482
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000483void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800484 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800485 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800486 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800487 CHECK_SHOULD_DRAW(draw);
488
robertphillipsd7706102016-02-25 09:28:08 -0800489 if (outer.isEmpty()) {
490 return;
491 }
492
493 if (inner.isEmpty()) {
494 return this->drawRRect(draw, outer, paint);
495 }
496
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000497 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000498
robertphillips0e7029e2015-11-30 05:45:06 -0800499 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800500 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400501 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700502 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700503 return;
504 }
robertphillips00095892016-02-29 13:50:40 -0800505
Brian Osman11052242016-10-27 14:47:55 -0400506 fRenderTargetContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800507 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000508 }
509
510 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700511 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000512 path.addRRect(outer);
513 path.addRRect(inner);
514 path.setFillType(SkPath::kEvenOdd_FillType);
515
Brian Osman11052242016-10-27 14:47:55 -0400516 GrBlurUtils::drawPathWithMaskFilter(fContext, fRenderTargetContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800517 fClip, path, paint,
518 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700519 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000520}
521
522
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000523/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524
msarettcc319b92016-08-25 18:07:18 -0700525void SkGpuDevice::drawRegion(const SkDraw& draw, const SkRegion& region, const SkPaint& paint) {
526 if (paint.getMaskFilter()) {
527 SkPath path;
528 region.getBoundaryPath(&path);
529 return this->drawPath(draw, path, paint, nullptr, false);
530 }
531
532 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400533 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
534 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700535 return;
536 }
537
Brian Osman11052242016-10-27 14:47:55 -0400538 fRenderTargetContext->drawRegion(fClip, grPaint, *draw.fMatrix, region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700539}
540
robertphillips514450c2015-11-24 05:36:02 -0800541void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800542 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800543 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800544 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545
robertphillips514450c2015-11-24 05:36:02 -0800546 // Presumably the path effect warps this to something other than an oval
547 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700549 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700551 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552 return;
herb11a7f7f2015-11-24 12:41:00 -0800553 }
554
robertphillips514450c2015-11-24 05:36:02 -0800555 if (paint.getMaskFilter()) {
556 // The RRect path can handle special case blurring
557 SkRRect rr = SkRRect::MakeOval(oval);
558 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559 }
560
561 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400562 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
563 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700564 return;
565 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566
Brian Osman11052242016-10-27 14:47:55 -0400567 fRenderTargetContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568}
569
bsalomon4f3a0ca2016-08-22 13:14:26 -0700570void SkGpuDevice::drawArc(const SkDraw& draw, const SkRect& oval, SkScalar startAngle,
571 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
572 ASSERT_SINGLE_OWNER
573 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext);
574 CHECK_SHOULD_DRAW(draw);
575
576 if (paint.getMaskFilter()) {
577 this->INHERITED::drawArc(draw, oval, startAngle, sweepAngle, useCenter, paint);
578 return;
579 }
580 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400581 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
582 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700583 return;
584 }
585
Brian Osman11052242016-10-27 14:47:55 -0400586 fRenderTargetContext->drawArc(fClip, grPaint, *draw.fMatrix, oval, startAngle, sweepAngle,
587 useCenter, GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700588}
589
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000590#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591
592///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700593void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
594 const SkDraw& draw,
595 const SkPaint& origPaint) {
596 ASSERT_SINGLE_OWNER
597 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
598 CHECK_SHOULD_DRAW(draw);
599
Brian Osman11052242016-10-27 14:47:55 -0400600 // Adding support for round capping would require a
601 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700602 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
603 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
604 SkASSERT(!origPaint.getPathEffect());
605 SkASSERT(!origPaint.getMaskFilter());
606
607 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
608 SkASSERT(halfWidth > 0);
609
610 SkVector v = points[1] - points[0];
611
612 SkScalar length = SkPoint::Normalize(&v);
613 if (!length) {
614 v.fX = 1.0f;
615 v.fY = 0.0f;
616 }
617
618 SkPaint newPaint(origPaint);
619 newPaint.setStyle(SkPaint::kFill_Style);
620
621 SkScalar xtraLength = 0.0f;
622 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
623 xtraLength = halfWidth;
624 }
625
626 SkPoint mid = points[0] + points[1];
627 mid.scale(0.5f);
628
629 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
630 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
631 SkMatrix m;
632 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
633
634 SkMatrix local = m;
635
636 m.postConcat(*draw.fMatrix);
637
638 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400639 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700640 return;
641 }
642
Brian Osman11052242016-10-27 14:47:55 -0400643 fRenderTargetContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700644}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000645
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
647 const SkPaint& paint, const SkMatrix* prePathMatrix,
648 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800649 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800650 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700651 SkPoint points[2];
652 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
653 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
654 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
655 // Path-based stroking looks better for thin rects
656 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700657 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700658 // Round capping support is currently disabled b.c. it would require
659 // a RRect batch that takes a localMatrix.
660 this->drawStrokedLine(points, draw, paint);
661 return;
662 }
663 }
robertphillipsff55b492015-11-24 07:56:59 -0800664 bool isClosed;
665 SkRect rect;
666 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
667 this->drawRect(draw, rect, paint);
668 return;
669 }
670 if (origSrcPath.isOval(&rect)) {
671 this->drawOval(draw, rect, paint);
672 return;
673 }
674 SkRRect rrect;
675 if (origSrcPath.isRRect(&rrect)) {
676 this->drawRRect(draw, rrect, paint);
677 return;
678 }
679 }
680
joshualitt5531d512014-12-17 15:50:11 -0800681 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800682 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683
Brian Osman11052242016-10-27 14:47:55 -0400684 GrBlurUtils::drawPathWithMaskFilter(fContext, fRenderTargetContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700685 fClip, origSrcPath, paint,
686 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700687 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000688}
689
690static const int kBmpSmallTileSize = 1 << 10;
691
692static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
693 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
694 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
695 return tilesX * tilesY;
696}
697
reed85d91782015-09-10 14:33:38 -0700698static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 if (maxTileSize <= kBmpSmallTileSize) {
700 return maxTileSize;
701 }
702
703 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
704 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
705
706 maxTileTotalTileSize *= maxTileSize * maxTileSize;
707 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
708
709 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
710 return kBmpSmallTileSize;
711 } else {
712 return maxTileSize;
713 }
714}
715
716// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
717// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700718static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800719 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800720 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700721 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700722 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 const SkRect* srcRectPtr,
724 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700725 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700726 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
727 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 clippedSrcIRect->setEmpty();
729 return;
730 }
731 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
732 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700733 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 if (!clippedSrcRect.intersect(*srcRectPtr)) {
735 clippedSrcIRect->setEmpty();
736 return;
737 }
738 }
739 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700740 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 if (!clippedSrcIRect->intersect(bmpBounds)) {
742 clippedSrcIRect->setEmpty();
743 }
744}
745
reed85d91782015-09-10 14:33:38 -0700746bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
747 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700748 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700749 const GrTextureParams& params,
750 const SkRect* srcRectPtr,
751 int maxTileSize,
752 int* tileSize,
753 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800754 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700755 // if it's larger than the max tile size, then we have no choice but tiling.
756 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400757 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
758 fClip, viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
759 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700760 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
761 return true;
762 }
763
bsalomon1a1d0b82015-10-16 07:49:42 -0700764 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700765 const size_t area = imageRect.width() * imageRect.height();
766 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
767 return false;
768 }
769
reed85d91782015-09-10 14:33:38 -0700770 // At this point we know we could do the draw by uploading the entire bitmap
771 // as a texture. However, if the texture would be large compared to the
772 // cache size and we don't require most of it for this draw then tile to
773 // reduce the amount of upload and cache spill.
774
775 // assumption here is that sw bitmap size is a good proxy for its size as
776 // a texture
777 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
778 size_t cacheSize;
779 fContext->getResourceCacheLimits(nullptr, &cacheSize);
780 if (bmpSize < cacheSize / 2) {
781 return false;
782 }
783
bsalomon1a1d0b82015-10-16 07:49:42 -0700784 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
785 // tiling memory savings would be < 50%.
Brian Osman11052242016-10-27 14:47:55 -0400786 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(), fClip,
787 viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
788 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700789 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
790 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
791 kBmpSmallTileSize * kBmpSmallTileSize;
792
793 return usedTileBytes < 2 * bmpSize;
794}
795
reed85d91782015-09-10 14:33:38 -0700796bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
797 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700798 const SkMatrix& viewMatrix,
799 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800800 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700801 // if image is explictly texture backed then just use the texture
802 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803 return false;
804 }
805
reed85d91782015-09-10 14:33:38 -0700806 GrTextureParams params;
807 bool doBicubic;
808 GrTextureParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700809 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700810
811 int tileFilterPad;
812 if (doBicubic) {
813 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
814 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
815 tileFilterPad = 0;
816 } else {
817 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 }
reed85d91782015-09-10 14:33:38 -0700819 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820
bsalomon8c07b7a2015-11-02 11:36:52 -0800821 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822
reed85d91782015-09-10 14:33:38 -0700823 // these are output, which we safely ignore, as we just want to know the predicate
824 int outTileSize;
825 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000826
bsalomone553b642016-08-17 09:02:09 -0700827 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
828 params, srcRectPtr, maxTileSize, &outTileSize,
829 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830}
831
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000832void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 const SkBitmap& bitmap,
834 const SkMatrix& m,
835 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800836 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800837 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800838 SkMatrix viewMatrix;
839 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700840
bsalomonb1b01992015-11-18 10:56:08 -0800841 int maxTileSize = fContext->caps()->maxTileSize();
842
843 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
844 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -0400845 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800846 paint.isAntiAlias() &&
847 bitmap.width() <= maxTileSize &&
848 bitmap.height() <= maxTileSize;
849
850 bool skipTileCheck = drawAA || paint.getMaskFilter();
851
852 if (!skipTileCheck) {
853 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
854 int tileSize;
855 SkIRect clippedSrcRect;
856
857 GrTextureParams params;
858 bool doBicubic;
859 GrTextureParams::FilterMode textureFilterMode =
860 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
861 &doBicubic);
862
863 int tileFilterPad;
864
865 if (doBicubic) {
866 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
867 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
868 tileFilterPad = 0;
869 } else {
870 tileFilterPad = 1;
871 }
872 params.setFilterMode(textureFilterMode);
873
874 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700875 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
876 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
877 &tileSize, &clippedSrcRect)) {
878 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
879 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
880 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800881 return;
882 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000883 }
bsalomonb1b01992015-11-18 10:56:08 -0800884 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800885 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
886 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000887}
888
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000889// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
891// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000892static inline void clamped_outset_with_offset(SkIRect* iRect,
893 int outset,
894 SkPoint* offset,
895 const SkIRect& clamp) {
896 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000898 int leftClampDelta = clamp.fLeft - iRect->fLeft;
899 if (leftClampDelta > 0) {
900 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901 iRect->fLeft = clamp.fLeft;
902 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000903 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000905
906 int topClampDelta = clamp.fTop - iRect->fTop;
907 if (topClampDelta > 0) {
908 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 iRect->fTop = clamp.fTop;
910 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000911 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 }
913
914 if (iRect->fRight > clamp.fRight) {
915 iRect->fRight = clamp.fRight;
916 }
917 if (iRect->fBottom > clamp.fBottom) {
918 iRect->fBottom = clamp.fBottom;
919 }
920}
921
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922// Break 'bitmap' into several tiles to draw it since it has already
923// been determined to be too large to fit in VRAM
924void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800925 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700926 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 const SkRect& srcRect,
928 const SkIRect& clippedSrcIRect,
929 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800930 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700931 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000932 int tileSize,
933 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800934 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800935
ericrk983294f2016-04-18 09:14:00 -0700936 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800937 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700938 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800939
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000940 // The following pixel lock is technically redundant, but it is desirable
941 // to lock outside of the tile loop to prevent redecoding the whole image
942 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
943 // is larger than the limit of the discardable memory pool.
944 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800945
946 const SkPaint* paint = &origPaint;
947 SkPaint tempPaint;
Brian Osman11052242016-10-27 14:47:55 -0400948 if (origPaint.isAntiAlias() && !fRenderTargetContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800949 // Drop antialiasing to avoid seams at tile boundaries.
950 tempPaint = origPaint;
951 tempPaint.setAntiAlias(false);
952 paint = &tempPaint;
953 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000954 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
955
956 int nx = bitmap.width() / tileSize;
957 int ny = bitmap.height() / tileSize;
958 for (int x = 0; x <= nx; x++) {
959 for (int y = 0; y <= ny; y++) {
960 SkRect tileR;
961 tileR.set(SkIntToScalar(x * tileSize),
962 SkIntToScalar(y * tileSize),
963 SkIntToScalar((x + 1) * tileSize),
964 SkIntToScalar((y + 1) * tileSize));
965
966 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
967 continue;
968 }
969
970 if (!tileR.intersect(srcRect)) {
971 continue;
972 }
973
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 SkIRect iTileR;
975 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700976 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
977 SkIntToScalar(iTileR.fTop));
978 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
979 tileR.width(), tileR.height());
980 dstMatrix.mapRect(&rectToDraw);
robertphillipsec8bb942014-11-21 10:16:25 -0800981 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 SkIRect iClampRect;
983
reeda5517e22015-07-14 10:54:12 -0700984 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 // In bleed mode we want to always expand the tile on all edges
986 // but stay within the bitmap bounds
987 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
988 } else {
989 // In texture-domain/clamp mode we only want to expand the
990 // tile on edges interior to "srcRect" (i.e., we want to
991 // not bleed across the original clamped edges)
992 srcRect.roundOut(&iClampRect);
993 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000994 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
995 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 }
997
bsalomone553b642016-08-17 09:02:09 -0700998 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 if (bitmap.extractSubset(&tmpB, iTileR)) {
1000 // now offset it to make it "local" to our tmp bitmap
1001 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001002 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001003 // de-optimized this determination
1004 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001005 this->drawBitmapTile(tmpB,
1006 viewMatrix,
1007 rectToDraw,
1008 tileR,
1009 paramsTemp,
1010 *paint,
1011 constraint,
1012 bicubic,
1013 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 }
1015 }
1016 }
1017}
1018
bsalomone553b642016-08-17 09:02:09 -07001019void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1020 const SkMatrix& viewMatrix,
1021 const SkRect& dstRect,
1022 const SkRect& srcRect,
1023 const GrTextureParams& params,
1024 const SkPaint& paint,
1025 SkCanvas::SrcRectConstraint constraint,
1026 bool bicubic,
1027 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001028 // We should have already handled bitmaps larger than the max texture size.
1029 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1030 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001031 // We should be respecting the max tile size by the time we get here.
1032 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1033 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034
Brian Osman11052242016-10-27 14:47:55 -04001035 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(
1036 fContext, bitmap, params, fRenderTargetContext->sourceGammaTreatment());
halcanary96fcdcc2015-08-27 07:41:13 -07001037 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001038 return;
1039 }
brianosman500bb3e2016-07-22 10:33:07 -07001040 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001041 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001042
1043 SkScalar iw = 1.f / texture->width();
1044 SkScalar ih = 1.f / texture->height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045
egdaniel79da63f2015-10-09 10:55:16 -07001046 SkMatrix texMatrix;
bsalomone553b642016-08-17 09:02:09 -07001047 // Compute a matrix that maps the rect we will draw to the src rect.
1048 texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kStart_ScaleToFit);
1049 texMatrix.postScale(iw, ih);
joshualitt5f10b5c2015-07-09 10:24:35 -07001050
1051 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1052 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001053 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001054
reeda5517e22015-07-14 10:54:12 -07001055 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001057 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001058 if (srcRect.width() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001059 domain.fLeft = (srcRect.fLeft + 0.5f) * iw;
1060 domain.fRight = (srcRect.fRight - 0.5f) * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061 } else {
bsalomone553b642016-08-17 09:02:09 -07001062 domain.fLeft = domain.fRight = srcRect.centerX() * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063 }
1064 if (srcRect.height() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001065 domain.fTop = (srcRect.fTop + 0.5f) * ih;
1066 domain.fBottom = (srcRect.fBottom - 0.5f) * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 } else {
bsalomone553b642016-08-17 09:02:09 -07001068 domain.fTop = domain.fBottom = srcRect.centerY() * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001070 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001071 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001072 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001073 } else {
reedc7ec7c92016-07-25 08:29:10 -07001074 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001075 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001076 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001077 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001078 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001079 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1080 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001081 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082 } else {
reedc7ec7c92016-07-25 08:29:10 -07001083 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 }
1085
joshualitt33a5fce2015-11-18 13:28:51 -08001086 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001087 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001088 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1089 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001090 return;
1091 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092
Brian Osman11052242016-10-27 14:47:55 -04001093 fRenderTargetContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094}
1095
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1097 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001098 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001099 CHECK_SHOULD_DRAW(draw);
robertphillips970587b2016-07-14 14:12:55 -07001100 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101
robertphillips970587b2016-07-14 14:12:55 -07001102 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103 return;
1104 }
1105
reedc7ec7c92016-07-25 08:29:10 -07001106 sk_sp<GrTexture> texture;
1107 {
robertphillips970587b2016-07-14 14:12:55 -07001108 SkAutoLockPixels alp(bitmap, true);
1109 if (!bitmap.readyToDraw()) {
1110 return;
1111 }
1112
1113 // draw sprite neither filters nor tiles.
1114 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap,
1115 GrTextureParams::ClampNoFilter(),
1116 SkSourceGammaTreatment::kRespect));
1117 if (!texture) {
1118 return;
1119 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001120 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001121
robertphillips970587b2016-07-14 14:12:55 -07001122 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1123 bitmap.pixelRefOrigin().fY,
1124 bitmap.width(),
1125 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001126
robertphillips970587b2016-07-14 14:12:55 -07001127 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1128 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001129 std::move(texture),
1130 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001131 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001132
1133 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1134}
1135
1136
Mike Kleine54c75f2016-10-13 14:18:09 -04001137void SkGpuDevice::drawSpecial(const SkDraw& draw,
robertphillips970587b2016-07-14 14:12:55 -07001138 SkSpecialImage* special1,
1139 int left, int top,
1140 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001141 ASSERT_SINGLE_OWNER
1142 CHECK_SHOULD_DRAW(draw);
1143 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext);
robertphillips970587b2016-07-14 14:12:55 -07001144
1145 SkIPoint offset = { 0, 0 };
1146
1147 sk_sp<SkSpecialImage> result;
1148 if (paint.getImageFilter()) {
1149 result = this->filterTexture(draw, special1, left, top,
1150 &offset,
1151 paint.getImageFilter());
1152 if (!result) {
1153 return;
1154 }
1155 } else {
1156 result = sk_ref_sp(special1);
1157 }
1158
1159 SkASSERT(result->isTextureBacked());
1160 sk_sp<GrTexture> texture = result->asTextureRef(fContext);
1161
1162 SkPaint tmpUnfiltered(paint);
1163 tmpUnfiltered.setImageFilter(nullptr);
1164
brianosman77320db2016-09-07 08:09:10 -07001165 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001166 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001168 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1169 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001170 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001171 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001172 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001173 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001174 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001175 }
Brian Osman11052242016-10-27 14:47:55 -04001176 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001177 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001178 return;
1179 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180
robertphillips970587b2016-07-14 14:12:55 -07001181 const SkIRect& subset = result->subset();
1182
Brian Osman11052242016-10-27 14:47:55 -04001183 fRenderTargetContext->fillRectToRect(fClip,
1184 grPaint,
1185 SkMatrix::I(),
1186 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX,
1187 top + offset.fY,
1188 subset.width(),
1189 subset.height())),
1190 SkRect::MakeXYWH(
1191 SkIntToScalar(subset.fLeft) / texture->width(),
1192 SkIntToScalar(subset.fTop) / texture->height(),
1193 SkIntToScalar(subset.width()) / texture->width(),
1194 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195}
1196
bsalomonb1b01992015-11-18 10:56:08 -08001197void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1198 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001199 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001200 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001201 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001202
bsalomonb1b01992015-11-18 10:56:08 -08001203 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1204 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1205 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1206 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1207 const SkRect* dst = &origDst;
1208 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001209 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001210 if (!src) {
1211 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001213
bsalomonb1b01992015-11-18 10:56:08 -08001214 SkMatrix srcToDstMatrix;
1215 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1216 return;
1217 }
1218 SkRect tmpSrc, tmpDst;
1219 if (src != &bmpBounds) {
1220 if (!bmpBounds.contains(*src)) {
1221 tmpSrc = *src;
1222 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 return; // nothing to draw
1224 }
bsalomonb1b01992015-11-18 10:56:08 -08001225 src = &tmpSrc;
1226 srcToDstMatrix.mapRect(&tmpDst, *src);
1227 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 }
1229 }
1230
bsalomonb1b01992015-11-18 10:56:08 -08001231 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001232
bsalomonb1b01992015-11-18 10:56:08 -08001233 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1234 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001235 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001236 paint.isAntiAlias() &&
1237 bitmap.width() <= maxTileSize &&
1238 bitmap.height() <= maxTileSize;
1239
1240 bool skipTileCheck = drawAA || paint.getMaskFilter();
1241
1242 if (!skipTileCheck) {
1243 int tileSize;
1244 SkIRect clippedSrcRect;
1245
1246 GrTextureParams params;
1247 bool doBicubic;
1248 GrTextureParams::FilterMode textureFilterMode =
1249 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1250 &doBicubic);
1251
1252 int tileFilterPad;
1253
1254 if (doBicubic) {
1255 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1256 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1257 tileFilterPad = 0;
1258 } else {
1259 tileFilterPad = 1;
1260 }
1261 params.setFilterMode(textureFilterMode);
1262
1263 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001264 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1265 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1266 &clippedSrcRect)) {
1267 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1268 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001269 return;
1270 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001271 }
bsalomonb1b01992015-11-18 10:56:08 -08001272 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001273 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001274}
1275
robertphillips6451a0c2016-07-18 08:31:31 -07001276sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001277 SkAutoLockPixels alp(bitmap, true);
1278 if (!bitmap.readyToDraw()) {
1279 return nullptr;
1280 }
1281
reedc7ec7c92016-07-25 08:29:10 -07001282 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap,
1283 GrTextureParams::ClampNoFilter(),
1284 SkSourceGammaTreatment::kRespect);
robertphillips6451a0c2016-07-18 08:31:31 -07001285 if (!texture) {
1286 return nullptr;
1287 }
1288
1289 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1290 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001291 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001292 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001293 &this->surfaceProps());
1294}
1295
reede51c3562016-07-19 14:33:20 -07001296sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001297 SkPixmap pm;
1298 if (image->isTextureBacked()) {
1299 GrTexture* texture = as_IB(image)->peekTexture();
1300
1301 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1302 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001303 sk_ref_sp(texture),
1304 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001305 &this->surfaceProps());
1306 } else if (image->peekPixels(&pm)) {
1307 SkBitmap bm;
1308
1309 bm.installPixels(pm);
1310 return this->makeSpecial(bm);
1311 } else {
1312 return nullptr;
1313 }
1314}
1315
1316sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Brian Osman11052242016-10-27 14:47:55 -04001317 sk_sp<GrTexture> texture(this->accessRenderTargetContext()->asTexture());
robertphillips1b5f9682016-07-15 08:01:12 -07001318 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001319 // When the device doesn't have a texture, we create a temporary texture.
1320 // TODO: we should actually only copy the portion of the source needed to apply the image
1321 // filter
Brian Osman11052242016-10-27 14:47:55 -04001322 texture.reset(fContext->textureProvider()->createTexture(
1323 this->accessRenderTargetContext()->desc(), SkBudgeted::kYes));
robertphillips04d62182016-07-15 12:21:33 -07001324 if (!texture) {
1325 return nullptr;
1326 }
1327
Brian Osman11052242016-10-27 14:47:55 -04001328 if (!fContext->copySurface(texture.get(),
1329 this->accessRenderTargetContext()->accessRenderTarget())) {
robertphillips04d62182016-07-15 12:21:33 -07001330 return nullptr;
1331 }
robertphillips1b5f9682016-07-15 08:01:12 -07001332 }
1333
1334 const SkImageInfo ii = this->imageInfo();
1335 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1336
1337 return SkSpecialImage::MakeFromGpu(srcRect,
1338 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -07001339 std::move(texture),
1340 sk_ref_sp(ii.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001341 &this->surfaceProps());
1342}
1343
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001345 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001346 SkASSERT(!paint.getImageFilter());
1347
joshualittce894002016-01-11 13:29:31 -08001348 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001350 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
kkinnunen2e4414e2015-02-19 07:20:40 -08001351
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001353 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354
robertphillips1b5f9682016-07-15 08:01:12 -07001355 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001356 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001357 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001358 return;
1359 }
1360
robertphillips1b5f9682016-07-15 08:01:12 -07001361 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362}
1363
reeda85d4d02015-05-06 12:56:48 -07001364void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1365 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001366 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001367 SkMatrix viewMatrix = *draw.fMatrix;
1368 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001369 uint32_t pinnedUniqueID;
1370 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001371 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001372 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001373 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001374 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1375 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001376 return;
reed85d91782015-09-10 14:33:38 -07001377 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001378 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001379 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001380 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001381 // only support tiling as bitmap at the moment, so force raster-version
1382 if (!as_IB(image)->getROPixels(&bm)) {
1383 return;
1384 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001385 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1386 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1387 CHECK_SHOULD_DRAW(draw);
1388 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001389 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1390 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001391 } else if (as_IB(image)->getROPixels(&bm)) {
1392 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001393 }
reeda85d4d02015-05-06 12:56:48 -07001394 }
1395}
1396
1397void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001398 const SkRect& dst, const SkPaint& paint,
1399 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001400 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001401 uint32_t pinnedUniqueID;
1402 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001403 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001404 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001405 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001406 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001407 return;
1408 }
1409 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001410 SkMatrix srcToDstRect;
1411 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1412 dst, SkMatrix::kFill_ScaleToFit);
1413 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1414 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001415 // only support tiling as bitmap at the moment, so force raster-version
1416 if (!as_IB(image)->getROPixels(&bm)) {
1417 return;
1418 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001419 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1420 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1421 CHECK_SHOULD_DRAW(draw);
1422 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001423 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001424 } else if (as_IB(image)->getROPixels(&bm)) {
1425 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001426 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001427}
1428
bsalomonf1ecd212015-12-09 17:06:02 -08001429void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001430 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001431 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001432
joshualitt33a5fce2015-11-18 13:28:51 -08001433 CHECK_SHOULD_DRAW(draw);
1434
joshualittedb36442015-11-19 14:29:30 -08001435 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001436 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001437 bool doBicubic;
1438 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001439 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1440 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001441 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001442 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001443
1444 SkRect srcR, dstR;
1445 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001446 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001447 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001448 }
1449 return;
1450 }
1451
bsalomon2bbd0c62015-12-09 12:50:56 -08001452 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001453 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001454 producer->createFragmentProcessor(SkMatrix::I(),
1455 SkRect::MakeIWH(producer->width(), producer->height()),
1456 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman11052242016-10-27 14:47:55 -04001457 &kMode, fRenderTargetContext->getColorSpace(),
1458 fRenderTargetContext->sourceGammaTreatment()));
joshualitt33a5fce2015-11-18 13:28:51 -08001459 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001460 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1461 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1462 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001463 return;
1464 }
1465
msarett10e3d9b2016-08-18 15:46:03 -07001466 std::unique_ptr<SkLatticeIter> iter(
1467 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Osman11052242016-10-27 14:47:55 -04001468 fRenderTargetContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1469 producer->height(), std::move(iter), dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001470}
1471
1472void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1473 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001474 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001475 uint32_t pinnedUniqueID;
1476 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1477 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001478 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001479 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001480 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001481 } else {
1482 SkBitmap bm;
1483 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1484 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001485 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001486 } else if (as_IB(image)->getROPixels(&bm)) {
1487 this->drawBitmapNine(draw, bm, center, dst, paint);
1488 }
1489 }
1490}
1491
1492void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1493 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001494 ASSERT_SINGLE_OWNER
reedc7ec7c92016-07-25 08:29:10 -07001495 GrBitmapTextureMaker maker(fContext, bitmap);
1496 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001497}
1498
msarett10e3d9b2016-08-18 15:46:03 -07001499void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1500 const SkCanvas::Lattice& lattice, const SkRect& dst,
1501 const SkPaint& paint) {
1502 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext);
1503
1504 CHECK_SHOULD_DRAW(draw);
1505
1506 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1507 sk_sp<GrFragmentProcessor> fp(
1508 producer->createFragmentProcessor(SkMatrix::I(),
1509 SkRect::MakeIWH(producer->width(), producer->height()),
1510 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman11052242016-10-27 14:47:55 -04001511 &kMode, fRenderTargetContext->getColorSpace(),
1512 fRenderTargetContext->sourceGammaTreatment()));
msarett10e3d9b2016-08-18 15:46:03 -07001513 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001514 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1515 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1516 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001517 return;
1518 }
1519
1520 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001521 new SkLatticeIter(lattice, dst));
Brian Osman11052242016-10-27 14:47:55 -04001522 fRenderTargetContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1523 producer->height(), std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001524}
1525
1526void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1527 const SkCanvas::Lattice& lattice, const SkRect& dst,
1528 const SkPaint& paint) {
1529 ASSERT_SINGLE_OWNER
1530 uint32_t pinnedUniqueID;
1531 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1532 CHECK_SHOULD_DRAW(draw);
1533 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1534 as_IB(image)->onImageInfo().colorSpace());
1535 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1536 } else {
1537 SkBitmap bm;
1538 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1539 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
1540 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1541 } else if (as_IB(image)->getROPixels(&bm)) {
1542 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1543 }
1544 }
1545}
1546
1547void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1548 const SkCanvas::Lattice& lattice, const SkRect& dst,
1549 const SkPaint& paint) {
1550 ASSERT_SINGLE_OWNER
1551 GrBitmapTextureMaker maker(fContext, bitmap);
1552 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1553}
1554
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001555///////////////////////////////////////////////////////////////////////////////
1556
1557// must be in SkCanvas::VertexMode order
1558static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1559 kTriangles_GrPrimitiveType,
1560 kTriangleStrip_GrPrimitiveType,
1561 kTriangleFan_GrPrimitiveType,
1562};
1563
1564void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1565 int vertexCount, const SkPoint vertices[],
1566 const SkPoint texs[], const SkColor colors[],
1567 SkXfermode* xmode,
1568 const uint16_t indices[], int indexCount,
1569 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001570 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001571 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001572 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001573
halcanary96fcdcc2015-08-27 07:41:13 -07001574 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1575 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001576
halcanary96fcdcc2015-08-27 07:41:13 -07001577 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001578
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001579 SkPaint copy(paint);
1580 copy.setStyle(SkPaint::kStroke_Style);
1581 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001582
bsalomonf1b7a1d2015-09-28 06:26:28 -07001583 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001584 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001585 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1586 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001587 return;
1588 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001589
dandov32a311b2014-07-15 19:46:26 -07001590 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001591 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001592 switch (vmode) {
1593 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001594 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001595 break;
1596 case SkCanvas::kTriangleStrip_VertexMode:
1597 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001598 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001599 break;
1600 }
mtklein533eb782014-08-27 10:39:42 -07001601
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001602 VertState state(vertexCount, indices, indexCount);
1603 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001604
dandov32a311b2014-07-15 19:46:26 -07001605 //number of indices for lines per triangle with kLines
1606 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001607
bsalomonf1b7a1d2015-09-28 06:26:28 -07001608 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001609 int i = 0;
1610 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001611 lineIndices[i] = state.f0;
1612 lineIndices[i + 1] = state.f1;
1613 lineIndices[i + 2] = state.f1;
1614 lineIndices[i + 3] = state.f2;
1615 lineIndices[i + 4] = state.f2;
1616 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001617 i += 6;
1618 }
Brian Osman11052242016-10-27 14:47:55 -04001619 fRenderTargetContext->drawVertices(fClip,
1620 grPaint,
1621 *draw.fMatrix,
1622 kLines_GrPrimitiveType,
1623 vertexCount,
1624 vertices,
1625 texs,
1626 colors,
1627 lineIndices.get(),
1628 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001629 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001630 }
1631
bsalomonf1b7a1d2015-09-28 06:26:28 -07001632 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633
1634 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001635 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001636 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1637 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638 convertedColors.reset(vertexCount);
1639 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001640 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641 }
1642 colors = convertedColors.get();
1643 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001644 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001645 if (texs && paint.getShader()) {
1646 if (colors) {
1647 // When there are texs and colors the shader and colors are combined using xmode. A null
1648 // xmode is defined to mean modulate.
1649 SkXfermode::Mode colorMode;
1650 if (xmode) {
1651 if (!xmode->asMode(&colorMode)) {
1652 return;
1653 }
1654 } else {
1655 colorMode = SkXfermode::kModulate_Mode;
1656 }
Brian Osman11052242016-10-27 14:47:55 -04001657 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), paint,
brianosman8fe485b2016-07-25 12:31:51 -07001658 *draw.fMatrix, colorMode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001659 return;
1660 }
1661 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001662 // We have a shader, but no colors to blend it against.
Brian Osman11052242016-10-27 14:47:55 -04001663 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001664 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001665 return;
1666 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001667 }
bsalomonaa48d362015-10-01 08:34:17 -07001668 } else {
1669 if (colors) {
1670 // We have colors, but either have no shader or no texture coords (which implies that
1671 // we should ignore the shader).
Brian Osman11052242016-10-27 14:47:55 -04001672 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext.get(),
1673 paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001674 return;
1675 }
1676 } else {
1677 // No colors and no shaders. Just draw with the paint color.
Brian Osman11052242016-10-27 14:47:55 -04001678 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), paint,
1679 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001680 return;
1681 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001682 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001683 }
1684
Brian Osman11052242016-10-27 14:47:55 -04001685 fRenderTargetContext->drawVertices(fClip,
1686 grPaint,
1687 *draw.fMatrix,
1688 primType,
1689 vertexCount,
1690 vertices,
1691 texs,
1692 colors,
1693 indices,
1694 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695}
1696
1697///////////////////////////////////////////////////////////////////////////////
1698
jvanverth31ff7622015-08-07 10:09:28 -07001699void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001700 const SkRect texRect[], const SkColor colors[], int count,
1701 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001702 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001703 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001704 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001705 return;
1706 }
1707
jvanverth31ff7622015-08-07 10:09:28 -07001708 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001709 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001710
reedca109532015-06-25 16:25:25 -07001711 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001712 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001713
jvanverth31ff7622015-08-07 10:09:28 -07001714 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001715 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001716 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
1717 *draw.fMatrix, mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001718 return;
1719 }
1720 } else {
Brian Osman11052242016-10-27 14:47:55 -04001721 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, *draw.fMatrix,
1722 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001723 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001724 }
1725 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001726
1727 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001728 fRenderTargetContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001729}
1730
1731///////////////////////////////////////////////////////////////////////////////
1732
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001734 size_t byteLength, SkScalar x, SkScalar y,
1735 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001736 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001737 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001738 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739
jvanverth8c27a182014-10-14 08:45:50 -07001740 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001741 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1742 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001743 return;
1744 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001745
jvanverth8c27a182014-10-14 08:45:50 -07001746 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001747
Brian Osman11052242016-10-27 14:47:55 -04001748 fRenderTargetContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
1749 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750}
1751
fmalita05c4a432014-09-29 06:29:53 -07001752void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1753 const SkScalar pos[], int scalarsPerPos,
1754 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001755 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001756 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001757 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758
jvanverth8c27a182014-10-14 08:45:50 -07001759 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001760 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1761 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001762 return;
1763 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001764
jvanverth8c27a182014-10-14 08:45:50 -07001765 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001766
Brian Osman11052242016-10-27 14:47:55 -04001767 fRenderTargetContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
1768 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1769 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001770}
1771
joshualitt9c328182015-03-23 08:13:04 -07001772void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1773 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001774 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001775 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001776 CHECK_SHOULD_DRAW(draw);
1777
1778 SkDEBUGCODE(this->validate();)
1779
Brian Osman11052242016-10-27 14:47:55 -04001780 fRenderTargetContext->drawTextBlob(fClip, paint, *draw.fMatrix,
1781 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001782}
1783
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784///////////////////////////////////////////////////////////////////////////////
1785
reedb2db8982014-11-13 12:41:02 -08001786bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001787 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788}
1789
1790void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001791 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001792
Brian Osman11052242016-10-27 14:47:55 -04001793 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794}
1795
1796///////////////////////////////////////////////////////////////////////////////
1797
reed76033be2015-03-14 10:54:31 -07001798SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001799 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800
robertphillipsca6eafc2016-05-17 09:57:46 -07001801 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802
robertphillipsca6eafc2016-05-17 09:57:46 -07001803 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001804 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001805 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1806 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001807
Brian Osman11052242016-10-27 14:47:55 -04001808 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1809 fit,
1810 cinfo.fInfo.width(), cinfo.fInfo.height(),
1811 fRenderTargetContext->config(),
1812 sk_ref_sp(fRenderTargetContext->getColorSpace()),
1813 fRenderTargetContext->desc().fSampleCnt,
1814 kDefault_GrSurfaceOrigin,
1815 &props));
1816 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001817 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001819
1820 // Skia's convention is to only clear a device if it is non-opaque.
1821 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1822
Brian Osman11052242016-10-27 14:47:55 -04001823 return SkGpuDevice::Make(std::move(rtc),
robertphillipsca6eafc2016-05-17 09:57:46 -07001824 cinfo.fInfo.width(), cinfo.fInfo.height(),
1825 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001826}
1827
reede8f30622016-03-23 18:59:25 -07001828sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001829 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001830 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001831 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Brian Osman11052242016-10-27 14:47:55 -04001832 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info,
1833 fRenderTargetContext->desc().fSampleCnt,
1834 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001835}
1836
senorblanco900c3672016-04-27 11:31:23 -07001837SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001838 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001839 // We always return a transient cache, so it is freed after each
1840 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001841 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001842}
reedf037e0b2014-10-30 11:34:15 -07001843
1844#endif