blob: 82c708946948c2ec3b3c420ff36c302ebd7173ff [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
Robert Phillips9fab7e92016-11-17 12:45:04 -050090sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
91 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070092 int width, int height,
93 InitContents init) {
Brian Osman11052242016-10-27 14:47:55 -040094 if (!renderTargetContext || renderTargetContext->wasAbandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070095 return nullptr;
96 }
97 unsigned flags;
98 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
99 return nullptr;
100 }
Robert Phillips9fab7e92016-11-17 12:45:04 -0500101 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
102 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000103}
104
robertphillips24e91282016-04-29 06:46:36 -0700105sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
106 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700107 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700108 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700109 unsigned flags;
110 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700112 }
113
Brian Osman11052242016-10-27 14:47:55 -0400114 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
115 info, sampleCount,
116 origin, props));
117 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700119 }
120
Robert Phillips9fab7e92016-11-17 12:45:04 -0500121 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700122 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700123}
124
Brian Osman11052242016-10-27 14:47:55 -0400125static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700126 SkColorType colorType;
127 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
128 colorType = kUnknown_SkColorType;
129 }
130 return SkImageInfo::Make(w, h, colorType,
131 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
132 sk_ref_sp(context->getColorSpace()));
133}
134
Robert Phillips9fab7e92016-11-17 12:45:04 -0500135SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
136 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400137 : INHERITED(make_info(renderTargetContext.get(), width, height,
138 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500139 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400140 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700141{
robertphillips1f3923e2016-07-21 07:17:54 -0700142 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700143 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144
bsalomone63ffef2016-02-05 07:17:34 -0800145 if (flags & kNeedClear_Flag) {
146 this->clearAll();
147 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148}
149
Brian Osman11052242016-10-27 14:47:55 -0400150sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
151 GrContext* context,
152 SkBudgeted budgeted,
153 const SkImageInfo& origInfo,
154 int sampleCount,
155 GrSurfaceOrigin origin,
156 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000157 if (kUnknown_SkColorType == origInfo.colorType() ||
158 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 }
161
bsalomonafe30052015-01-16 07:32:33 -0800162 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700163 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800164 }
165
reede5ea5002014-09-03 11:54:58 -0700166 SkColorType ct = origInfo.colorType();
167 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700168 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700169 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700170 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800171 }
172 if (kOpaque_SkAlphaType != at) {
173 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000174 }
brianosman6b086522016-04-14 12:39:00 -0700175
brianosmanb109b8c2016-06-16 13:03:24 -0700176 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177
Brian Osman11052242016-10-27 14:47:55 -0400178 return context->makeRenderTargetContext(SkBackingFit::kExact, // Why exact?
robertphillips6738c702016-07-27 12:13:51 -0700179 origInfo.width(), origInfo.height(),
180 config, sk_ref_sp(cs), sampleCount,
181 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800182}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000183
robertphillips970587b2016-07-14 14:12:55 -0700184sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
185 SkSpecialImage* srcImg,
186 int left, int top,
187 SkIPoint* offset,
188 const SkImageFilter* filter) {
189 SkASSERT(srcImg->isTextureBacked());
190 SkASSERT(filter);
191
192 SkMatrix matrix = *draw.fMatrix;
193 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
194 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500195 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osman11052242016-10-27 14:47:55 -0400196 SkImageFilter::OutputProperties outputProperties(fRenderTargetContext->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700197 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700198
199 return filter->filterImage(srcImg, ctx, offset);
200}
201
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000202///////////////////////////////////////////////////////////////////////////////
203
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000204bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
205 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800206 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000207
Brian Osman11052242016-10-27 14:47:55 -0400208 return fRenderTargetContext->readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000209}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000210
robertphillips1da3ecd2016-08-31 14:54:15 -0700211bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
212 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800213 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700214
Brian Osman11052242016-10-27 14:47:55 -0400215 return fRenderTargetContext->writePixels(srcInfo, srcPixels, srcRowBytes, x, y);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000216}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000217
reed41e010c2015-06-09 12:16:53 -0700218bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800219 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700220 return false;
221}
222
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223// call this every draw call, to ensure that the context reflects our state,
224// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800225void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800226 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000227
reed2c9e2002016-07-25 08:05:22 -0700228 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229}
230
Brian Osman11052242016-10-27 14:47:55 -0400231GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700232 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400233 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700234}
235
reed8eddfb52014-12-04 07:50:14 -0800236void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800237 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800238 GrColor color = 0;
Hal Canary144caf52016-11-07 17:57:18 -0500239 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
reed8eddfb52014-12-04 07:50:14 -0800240 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman11052242016-10-27 14:47:55 -0400241 fRenderTargetContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800242}
243
Brian Osman11052242016-10-27 14:47:55 -0400244void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800245 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800246
Brian Osman693a5402016-10-27 15:13:22 -0400247 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800248
Brian Osman693a5402016-10-27 15:13:22 -0400249 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400250 this->context(),
251 budgeted,
252 this->imageInfo(),
253 fRenderTargetContext->numColorSamples(),
254 fRenderTargetContext->origin(),
255 &this->surfaceProps()));
Brian Osman693a5402016-10-27 15:13:22 -0400256 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800257 return;
258 }
259
260 if (shouldRetainContent) {
Brian Osman11052242016-10-27 14:47:55 -0400261 if (fRenderTargetContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800262 return;
263 }
Brian Osman693a5402016-10-27 15:13:22 -0400264 newRTC->copySurface(fRenderTargetContext->asTexture().get(),
265 SkIRect::MakeWH(this->width(), this->height()),
266 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800267 }
268
Brian Osman693a5402016-10-27 15:13:22 -0400269 fRenderTargetContext = newRTC;
kkinnunenabcfab42015-02-22 22:53:44 -0800270}
271
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272///////////////////////////////////////////////////////////////////////////////
273
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800275 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800276 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500277 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278
279 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400280 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
281 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700282 return;
283 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284
Brian Osman11052242016-10-27 14:47:55 -0400285 fRenderTargetContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286}
287
288// must be in SkCanvas::PointMode order
bsalomon6ade6dd2016-09-12 12:07:17 -0700289static const GrPrimitiveType gPointMode2PrimitiveType[] = {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290 kPoints_GrPrimitiveType,
291 kLines_GrPrimitiveType,
292 kLineStrip_GrPrimitiveType
293};
294
ethannicholas330bb952015-07-17 06:44:02 -0700295// suppress antialiasing on axis-aligned integer-coordinate lines
296static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
297 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
298 return false;
299 }
300 if (count == 2) {
301 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
302 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800303 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700304 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800305 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700306 // the two end pixels as being the lesser of two evils.
307 if (pts[0].fX == pts[1].fX) {
308 return ((int) pts[0].fX) != pts[0].fX;
309 }
310 if (pts[0].fY == pts[1].fY) {
311 return ((int) pts[0].fY) != pts[0].fY;
312 }
313 }
314 return true;
315}
316
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
318 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800319 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500320 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800321 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
323 SkScalar width = paint.getStrokeWidth();
324 if (width < 0) {
325 return;
326 }
327
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000328 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700329 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700330 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400331 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700332 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700333 return;
334 }
egdaniele61c4112014-06-12 10:24:21 -0700335 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700336 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700337 path.moveTo(pts[0]);
338 path.lineTo(pts[1]);
Brian Osman11052242016-10-27 14:47:55 -0400339 fRenderTargetContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700340 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000341 }
342
bsalomon6ade6dd2016-09-12 12:07:17 -0700343 SkScalar scales[2];
344 bool isHairline = (0 == width) || (1 == width && draw.fMatrix->getMinMaxScales(scales) &&
345 SkScalarNearlyEqual(scales[0], 1.f) &&
346 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700347 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000348 // else we let the SkDraw call our drawPath()
bsalomon6ade6dd2016-09-12 12:07:17 -0700349 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700350 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351 draw.drawPoints(mode, count, pts, paint, true);
352 return;
353 }
354
bsalomon6ade6dd2016-09-12 12:07:17 -0700355 GrPrimitiveType primitiveType = gPointMode2PrimitiveType[mode];
356
357 const SkMatrix* viewMatrix = draw.fMatrix;
358#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
359 // This offsetting in device space matches the expectations of the Android framework for non-AA
360 // points and lines.
361 SkMatrix tempMatrix;
362 if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
363 tempMatrix = *viewMatrix;
364 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
365 tempMatrix.postTranslate(kOffset, kOffset);
366 viewMatrix = &tempMatrix;
367 }
368#endif
369
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000370 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400371 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *viewMatrix,
372 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700373 return;
374 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000375
Brian Osman11052242016-10-27 14:47:55 -0400376 fRenderTargetContext->drawVertices(fClip,
377 grPaint,
378 *viewMatrix,
379 primitiveType,
380 SkToS32(count),
381 (SkPoint*)pts,
382 nullptr,
383 nullptr,
384 nullptr,
385 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386}
387
388///////////////////////////////////////////////////////////////////////////////
389
robertphillipsff55b492015-11-24 07:56:59 -0800390void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800391 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500392 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800393 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395
bsalomona7d85ba2016-07-06 11:54:59 -0700396 // A couple reasons we might need to call drawPath.
397 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700399 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400 path.addRect(rect);
Hal Canary144caf52016-11-07 17:57:18 -0500401 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800402 fClip, path, paint,
403 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700404 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000405 return;
406 }
407
408 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400409 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
410 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700411 return;
412 }
Mike Klein744fb732014-06-23 15:13:26 -0400413
bsalomon6663acf2016-05-10 09:14:17 -0700414 GrStyle style(paint);
Brian Osman11052242016-10-27 14:47:55 -0400415 fRenderTargetContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000416}
417
418///////////////////////////////////////////////////////////////////////////////
419
robertphillips9aff85a2016-08-05 07:51:29 -0700420void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect,
joshualitt5531d512014-12-17 15:50:11 -0800421 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800422 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500423 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800424 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000426 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400427 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
428 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700429 return;
430 }
Mike Klein744fb732014-06-23 15:13:26 -0400431
Robert Phillipsa29a9562016-10-20 09:40:55 -0400432 SkMaskFilter* mf = paint.getMaskFilter();
433 if (mf && mf->asFragmentProcessor(nullptr, nullptr, *draw.fMatrix)) {
434 mf = nullptr; // already handled in SkPaintToGrPaint
435 }
436
bsalomon6663acf2016-05-10 09:14:17 -0700437 GrStyle style(paint);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400438 if (mf) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000439 // try to hit the fast path for drawing filtered round rects
440
441 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700442 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000443 if (devRRect.allCornersCircular()) {
444 SkRect maskRect;
Robert Phillipsa29a9562016-10-20 09:40:55 -0400445 if (mf->canFilterMaskGPU(devRRect, draw.fRC->getBounds(),
446 *draw.fMatrix, &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000447 SkIRect finalIRect;
448 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700449 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000450 // clipped out
451 return;
452 }
Hal Canary144caf52016-11-07 17:57:18 -0500453 if (mf->directFilterRRectMaskGPU(fContext.get(), fRenderTargetContext.get(),
454 &grPaint, fClip, *draw.fMatrix,
455 style.strokeRec(), rrect, devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000456 return;
457 }
458 }
459
460 }
461 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000462 }
463
Robert Phillipsa29a9562016-10-20 09:40:55 -0400464 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800465 // The only mask filter the native rrect drawing code could've handle was taken
466 // care of above.
467 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000468 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700469 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700470 path.addRRect(rrect);
Hal Canary144caf52016-11-07 17:57:18 -0500471 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800472 fClip, path, paint,
473 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700474 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475 return;
476 }
Mike Klein744fb732014-06-23 15:13:26 -0400477
bsalomon6663acf2016-05-10 09:14:17 -0700478 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800479
Brian Osman11052242016-10-27 14:47:55 -0400480 fRenderTargetContext->drawRRect(fClip, grPaint, *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481}
482
robertphillipsd7706102016-02-25 09:28:08 -0800483
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000484void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800485 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800486 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500487 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillips0e7029e2015-11-30 05:45:06 -0800488 CHECK_SHOULD_DRAW(draw);
489
robertphillipsd7706102016-02-25 09:28:08 -0800490 if (outer.isEmpty()) {
491 return;
492 }
493
494 if (inner.isEmpty()) {
495 return this->drawRRect(draw, outer, paint);
496 }
497
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000498 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000499
robertphillips0e7029e2015-11-30 05:45:06 -0800500 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800501 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400502 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700503 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700504 return;
505 }
robertphillips00095892016-02-29 13:50:40 -0800506
Brian Osman11052242016-10-27 14:47:55 -0400507 fRenderTargetContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800508 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000509 }
510
511 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700512 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000513 path.addRRect(outer);
514 path.addRRect(inner);
515 path.setFillType(SkPath::kEvenOdd_FillType);
516
Hal Canary144caf52016-11-07 17:57:18 -0500517 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800518 fClip, path, paint,
519 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700520 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000521}
522
523
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000524/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525
msarettcc319b92016-08-25 18:07:18 -0700526void SkGpuDevice::drawRegion(const SkDraw& draw, const SkRegion& region, const SkPaint& paint) {
527 if (paint.getMaskFilter()) {
528 SkPath path;
529 region.getBoundaryPath(&path);
530 return this->drawPath(draw, path, paint, nullptr, false);
531 }
532
533 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400534 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
535 &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700536 return;
537 }
538
Brian Osman11052242016-10-27 14:47:55 -0400539 fRenderTargetContext->drawRegion(fClip, grPaint, *draw.fMatrix, region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700540}
541
robertphillips514450c2015-11-24 05:36:02 -0800542void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800543 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500544 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -0800545 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546
robertphillips514450c2015-11-24 05:36:02 -0800547 // Presumably the path effect warps this to something other than an oval
548 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700550 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700552 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553 return;
herb11a7f7f2015-11-24 12:41:00 -0800554 }
555
robertphillips514450c2015-11-24 05:36:02 -0800556 if (paint.getMaskFilter()) {
557 // The RRect path can handle special case blurring
558 SkRRect rr = SkRRect::MakeOval(oval);
559 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000560 }
561
562 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400563 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
564 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700565 return;
566 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567
Brian Osman11052242016-10-27 14:47:55 -0400568 fRenderTargetContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000569}
570
bsalomon4f3a0ca2016-08-22 13:14:26 -0700571void SkGpuDevice::drawArc(const SkDraw& draw, const SkRect& oval, SkScalar startAngle,
572 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
573 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500574 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700575 CHECK_SHOULD_DRAW(draw);
576
577 if (paint.getMaskFilter()) {
578 this->INHERITED::drawArc(draw, oval, startAngle, sweepAngle, useCenter, paint);
579 return;
580 }
581 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400582 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
583 &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700584 return;
585 }
586
Brian Osman11052242016-10-27 14:47:55 -0400587 fRenderTargetContext->drawArc(fClip, grPaint, *draw.fMatrix, oval, startAngle, sweepAngle,
588 useCenter, GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700589}
590
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592
593///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700594void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
595 const SkDraw& draw,
596 const SkPaint& origPaint) {
597 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500598 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
robertphillips0851d2d2016-06-02 05:21:34 -0700599 CHECK_SHOULD_DRAW(draw);
600
Brian Osman11052242016-10-27 14:47:55 -0400601 // Adding support for round capping would require a
602 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700603 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
604 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
605 SkASSERT(!origPaint.getPathEffect());
606 SkASSERT(!origPaint.getMaskFilter());
607
608 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
609 SkASSERT(halfWidth > 0);
610
611 SkVector v = points[1] - points[0];
612
613 SkScalar length = SkPoint::Normalize(&v);
614 if (!length) {
615 v.fX = 1.0f;
616 v.fY = 0.0f;
617 }
618
619 SkPaint newPaint(origPaint);
620 newPaint.setStyle(SkPaint::kFill_Style);
621
622 SkScalar xtraLength = 0.0f;
623 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
624 xtraLength = halfWidth;
625 }
626
627 SkPoint mid = points[0] + points[1];
628 mid.scale(0.5f);
629
630 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
631 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
632 SkMatrix m;
633 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
634
635 SkMatrix local = m;
636
637 m.postConcat(*draw.fMatrix);
638
639 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400640 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700641 return;
642 }
643
Brian Osman11052242016-10-27 14:47:55 -0400644 fRenderTargetContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700645}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
648 const SkPaint& paint, const SkMatrix* prePathMatrix,
649 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800650 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800651 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700652 SkPoint points[2];
653 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
654 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
655 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
656 // Path-based stroking looks better for thin rects
657 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700658 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700659 // Round capping support is currently disabled b.c. it would require
660 // a RRect batch that takes a localMatrix.
661 this->drawStrokedLine(points, draw, paint);
662 return;
663 }
664 }
robertphillipsff55b492015-11-24 07:56:59 -0800665 bool isClosed;
666 SkRect rect;
667 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
668 this->drawRect(draw, rect, paint);
669 return;
670 }
671 if (origSrcPath.isOval(&rect)) {
672 this->drawOval(draw, rect, paint);
673 return;
674 }
675 SkRRect rrect;
676 if (origSrcPath.isRRect(&rrect)) {
677 this->drawRRect(draw, rrect, paint);
678 return;
679 }
680 }
681
joshualitt5531d512014-12-17 15:50:11 -0800682 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -0500683 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684
Hal Canary144caf52016-11-07 17:57:18 -0500685 GrBlurUtils::drawPathWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700686 fClip, origSrcPath, paint,
687 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700688 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000689}
690
691static const int kBmpSmallTileSize = 1 << 10;
692
693static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
694 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
695 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
696 return tilesX * tilesY;
697}
698
reed85d91782015-09-10 14:33:38 -0700699static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700 if (maxTileSize <= kBmpSmallTileSize) {
701 return maxTileSize;
702 }
703
704 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
705 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
706
707 maxTileTotalTileSize *= maxTileSize * maxTileSize;
708 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
709
710 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
711 return kBmpSmallTileSize;
712 } else {
713 return maxTileSize;
714 }
715}
716
717// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
718// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700719static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800720 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800721 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700722 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700723 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724 const SkRect* srcRectPtr,
725 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700726 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700727 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
728 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000729 clippedSrcIRect->setEmpty();
730 return;
731 }
732 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
733 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700734 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735 if (!clippedSrcRect.intersect(*srcRectPtr)) {
736 clippedSrcIRect->setEmpty();
737 return;
738 }
739 }
740 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700741 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 if (!clippedSrcIRect->intersect(bmpBounds)) {
743 clippedSrcIRect->setEmpty();
744 }
745}
746
reed85d91782015-09-10 14:33:38 -0700747bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
748 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700749 const SkMatrix& srcToDstRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500750 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700751 const SkRect* srcRectPtr,
752 int maxTileSize,
753 int* tileSize,
754 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800755 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700756 // if it's larger than the max tile size, then we have no choice but tiling.
757 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400758 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
759 fClip, viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
760 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700761 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
762 return true;
763 }
764
bsalomon1a1d0b82015-10-16 07:49:42 -0700765 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700766 const size_t area = imageRect.width() * imageRect.height();
767 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
768 return false;
769 }
770
reed85d91782015-09-10 14:33:38 -0700771 // At this point we know we could do the draw by uploading the entire bitmap
772 // as a texture. However, if the texture would be large compared to the
773 // cache size and we don't require most of it for this draw then tile to
774 // reduce the amount of upload and cache spill.
775
776 // assumption here is that sw bitmap size is a good proxy for its size as
777 // a texture
778 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
779 size_t cacheSize;
780 fContext->getResourceCacheLimits(nullptr, &cacheSize);
781 if (bmpSize < cacheSize / 2) {
782 return false;
783 }
784
bsalomon1a1d0b82015-10-16 07:49:42 -0700785 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
786 // tiling memory savings would be < 50%.
Brian Osman11052242016-10-27 14:47:55 -0400787 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(), fClip,
788 viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
789 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700790 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
791 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
792 kBmpSmallTileSize * kBmpSmallTileSize;
793
794 return usedTileBytes < 2 * bmpSize;
795}
796
reed85d91782015-09-10 14:33:38 -0700797bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
798 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700799 const SkMatrix& viewMatrix,
800 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800801 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700802 // if image is explictly texture backed then just use the texture
803 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000804 return false;
805 }
806
Brian Salomon514baff2016-11-17 15:17:07 -0500807 GrSamplerParams params;
reed85d91782015-09-10 14:33:38 -0700808 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500809 GrSamplerParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700810 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700811
812 int tileFilterPad;
813 if (doBicubic) {
814 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500815 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700816 tileFilterPad = 0;
817 } else {
818 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000819 }
reed85d91782015-09-10 14:33:38 -0700820 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000821
bsalomon8c07b7a2015-11-02 11:36:52 -0800822 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823
reed85d91782015-09-10 14:33:38 -0700824 // these are output, which we safely ignore, as we just want to know the predicate
825 int outTileSize;
826 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827
bsalomone553b642016-08-17 09:02:09 -0700828 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
829 params, srcRectPtr, maxTileSize, &outTileSize,
830 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831}
832
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000833void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000834 const SkBitmap& bitmap,
835 const SkMatrix& m,
836 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800837 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800838 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800839 SkMatrix viewMatrix;
840 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700841
bsalomonb1b01992015-11-18 10:56:08 -0800842 int maxTileSize = fContext->caps()->maxTileSize();
843
844 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
845 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -0400846 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800847 paint.isAntiAlias() &&
848 bitmap.width() <= maxTileSize &&
849 bitmap.height() <= maxTileSize;
850
851 bool skipTileCheck = drawAA || paint.getMaskFilter();
852
853 if (!skipTileCheck) {
854 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
855 int tileSize;
856 SkIRect clippedSrcRect;
857
Brian Salomon514baff2016-11-17 15:17:07 -0500858 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -0800859 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -0500860 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -0800861 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
862 &doBicubic);
863
864 int tileFilterPad;
865
866 if (doBicubic) {
867 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -0500868 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -0800869 tileFilterPad = 0;
870 } else {
871 tileFilterPad = 1;
872 }
873 params.setFilterMode(textureFilterMode);
874
875 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700876 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
877 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
878 &tileSize, &clippedSrcRect)) {
879 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
880 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
881 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800882 return;
883 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000884 }
Hal Canary144caf52016-11-07 17:57:18 -0500885 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800886 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
887 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888}
889
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000890// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000891// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
892// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000893static inline void clamped_outset_with_offset(SkIRect* iRect,
894 int outset,
895 SkPoint* offset,
896 const SkIRect& clamp) {
897 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000899 int leftClampDelta = clamp.fLeft - iRect->fLeft;
900 if (leftClampDelta > 0) {
901 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000902 iRect->fLeft = clamp.fLeft;
903 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000904 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000906
907 int topClampDelta = clamp.fTop - iRect->fTop;
908 if (topClampDelta > 0) {
909 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 iRect->fTop = clamp.fTop;
911 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000912 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913 }
914
915 if (iRect->fRight > clamp.fRight) {
916 iRect->fRight = clamp.fRight;
917 }
918 if (iRect->fBottom > clamp.fBottom) {
919 iRect->fBottom = clamp.fBottom;
920 }
921}
922
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923// Break 'bitmap' into several tiles to draw it since it has already
924// been determined to be too large to fit in VRAM
925void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800926 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700927 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 const SkRect& srcRect,
929 const SkIRect& clippedSrcIRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500930 const GrSamplerParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800931 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700932 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000933 int tileSize,
934 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800935 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800936
ericrk983294f2016-04-18 09:14:00 -0700937 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800938 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700939 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800940
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000941 // The following pixel lock is technically redundant, but it is desirable
942 // to lock outside of the tile loop to prevent redecoding the whole image
943 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
944 // is larger than the limit of the discardable memory pool.
945 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800946
947 const SkPaint* paint = &origPaint;
948 SkPaint tempPaint;
Brian Osman11052242016-10-27 14:47:55 -0400949 if (origPaint.isAntiAlias() && !fRenderTargetContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800950 // Drop antialiasing to avoid seams at tile boundaries.
951 tempPaint = origPaint;
952 tempPaint.setAntiAlias(false);
953 paint = &tempPaint;
954 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
956
957 int nx = bitmap.width() / tileSize;
958 int ny = bitmap.height() / tileSize;
959 for (int x = 0; x <= nx; x++) {
960 for (int y = 0; y <= ny; y++) {
961 SkRect tileR;
962 tileR.set(SkIntToScalar(x * tileSize),
963 SkIntToScalar(y * tileSize),
964 SkIntToScalar((x + 1) * tileSize),
965 SkIntToScalar((y + 1) * tileSize));
966
967 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
968 continue;
969 }
970
971 if (!tileR.intersect(srcRect)) {
972 continue;
973 }
974
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 SkIRect iTileR;
976 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700977 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
978 SkIntToScalar(iTileR.fTop));
979 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
980 tileR.width(), tileR.height());
981 dstMatrix.mapRect(&rectToDraw);
Brian Salomon514baff2016-11-17 15:17:07 -0500982 if (GrSamplerParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983 SkIRect iClampRect;
984
reeda5517e22015-07-14 10:54:12 -0700985 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000986 // In bleed mode we want to always expand the tile on all edges
987 // but stay within the bitmap bounds
988 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
989 } else {
990 // In texture-domain/clamp mode we only want to expand the
991 // tile on edges interior to "srcRect" (i.e., we want to
992 // not bleed across the original clamped edges)
993 srcRect.roundOut(&iClampRect);
994 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
996 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997 }
998
bsalomone553b642016-08-17 09:02:09 -0700999 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000 if (bitmap.extractSubset(&tmpB, iTileR)) {
1001 // now offset it to make it "local" to our tmp bitmap
1002 tileR.offset(-offset.fX, -offset.fY);
Brian Salomon514baff2016-11-17 15:17:07 -05001003 GrSamplerParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001004 // de-optimized this determination
1005 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -07001006 this->drawBitmapTile(tmpB,
1007 viewMatrix,
1008 rectToDraw,
1009 tileR,
1010 paramsTemp,
1011 *paint,
1012 constraint,
1013 bicubic,
1014 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 }
1016 }
1017 }
1018}
1019
bsalomone553b642016-08-17 09:02:09 -07001020void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
1021 const SkMatrix& viewMatrix,
1022 const SkRect& dstRect,
1023 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -05001024 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -07001025 const SkPaint& paint,
1026 SkCanvas::SrcRectConstraint constraint,
1027 bool bicubic,
1028 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001029 // We should have already handled bitmaps larger than the max texture size.
1030 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1031 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001032 // We should be respecting the max tile size by the time we get here.
1033 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1034 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035
Brian Osman11052242016-10-27 14:47:55 -04001036 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(
Brian Osman7b8400d2016-11-08 17:08:54 -05001037 fContext.get(), bitmap, params, fRenderTargetContext->colorMode());
halcanary96fcdcc2015-08-27 07:41:13 -07001038 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039 return;
1040 }
brianosman500bb3e2016-07-22 10:33:07 -07001041 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001042 GrColorSpaceXform::Make(bitmap.colorSpace(), fRenderTargetContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -07001043
1044 SkScalar iw = 1.f / texture->width();
1045 SkScalar ih = 1.f / texture->height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001046
egdaniel79da63f2015-10-09 10:55:16 -07001047 SkMatrix texMatrix;
bsalomone553b642016-08-17 09:02:09 -07001048 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomond93f4a42016-11-14 14:41:58 -05001049 texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
bsalomone553b642016-08-17 09:02:09 -07001050 texMatrix.postScale(iw, ih);
joshualitt5f10b5c2015-07-09 10:24:35 -07001051
1052 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1053 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001054 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001055
reeda5517e22015-07-14 10:54:12 -07001056 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001058 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 if (srcRect.width() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001060 domain.fLeft = (srcRect.fLeft + 0.5f) * iw;
1061 domain.fRight = (srcRect.fRight - 0.5f) * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 } else {
bsalomone553b642016-08-17 09:02:09 -07001063 domain.fLeft = domain.fRight = srcRect.centerX() * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001064 }
1065 if (srcRect.height() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001066 domain.fTop = (srcRect.fTop + 0.5f) * ih;
1067 domain.fBottom = (srcRect.fBottom - 0.5f) * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 } else {
bsalomone553b642016-08-17 09:02:09 -07001069 domain.fTop = domain.fBottom = srcRect.centerY() * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001070 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001071 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001072 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001073 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001074 } else {
reedc7ec7c92016-07-25 08:29:10 -07001075 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001076 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001077 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001078 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001079 } else if (bicubic) {
Brian Salomon514baff2016-11-17 15:17:07 -05001080 SkASSERT(GrSamplerParams::kNone_FilterMode == params.filterMode());
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001081 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001082 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 } else {
reedc7ec7c92016-07-25 08:29:10 -07001084 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085 }
1086
joshualitt33a5fce2015-11-18 13:28:51 -08001087 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001088 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint, viewMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001089 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1090 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001091 return;
1092 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093
Brian Osman11052242016-10-27 14:47:55 -04001094 fRenderTargetContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095}
1096
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1098 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001099 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001100 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001101 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102
robertphillips970587b2016-07-14 14:12:55 -07001103 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104 return;
1105 }
1106
reedc7ec7c92016-07-25 08:29:10 -07001107 sk_sp<GrTexture> texture;
1108 {
robertphillips970587b2016-07-14 14:12:55 -07001109 SkAutoLockPixels alp(bitmap, true);
1110 if (!bitmap.readyToDraw()) {
1111 return;
1112 }
1113
1114 // draw sprite neither filters nor tiles.
Brian Osman7b8400d2016-11-08 17:08:54 -05001115 texture.reset(
Brian Salomon514baff2016-11-17 15:17:07 -05001116 GrRefCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001117 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
robertphillips970587b2016-07-14 14:12:55 -07001118 if (!texture) {
1119 return;
1120 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001121 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122
robertphillips970587b2016-07-14 14:12:55 -07001123 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1124 bitmap.pixelRefOrigin().fY,
1125 bitmap.width(),
1126 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001127
robertphillips970587b2016-07-14 14:12:55 -07001128 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1129 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001130 std::move(texture),
1131 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001132 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001133
1134 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1135}
1136
1137
Mike Kleine54c75f2016-10-13 14:18:09 -04001138void SkGpuDevice::drawSpecial(const SkDraw& draw,
robertphillips970587b2016-07-14 14:12:55 -07001139 SkSpecialImage* special1,
1140 int left, int top,
1141 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001142 ASSERT_SINGLE_OWNER
1143 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001144 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001145
1146 SkIPoint offset = { 0, 0 };
1147
1148 sk_sp<SkSpecialImage> result;
1149 if (paint.getImageFilter()) {
1150 result = this->filterTexture(draw, special1, left, top,
1151 &offset,
1152 paint.getImageFilter());
1153 if (!result) {
1154 return;
1155 }
1156 } else {
1157 result = sk_ref_sp(special1);
1158 }
1159
1160 SkASSERT(result->isTextureBacked());
Hal Canary144caf52016-11-07 17:57:18 -05001161 sk_sp<GrTexture> texture = result->asTextureRef(fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001162
1163 SkPaint tmpUnfiltered(paint);
1164 tmpUnfiltered.setImageFilter(nullptr);
1165
brianosman77320db2016-09-07 08:09:10 -07001166 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001167 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001169 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1170 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001171 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001172 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001173 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001174 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001175 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001176 }
Brian Osman11052242016-10-27 14:47:55 -04001177 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001178 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001179 return;
1180 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181
robertphillips970587b2016-07-14 14:12:55 -07001182 const SkIRect& subset = result->subset();
1183
Brian Osman11052242016-10-27 14:47:55 -04001184 fRenderTargetContext->fillRectToRect(fClip,
1185 grPaint,
1186 SkMatrix::I(),
1187 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX,
1188 top + offset.fY,
1189 subset.width(),
1190 subset.height())),
1191 SkRect::MakeXYWH(
1192 SkIntToScalar(subset.fLeft) / texture->width(),
1193 SkIntToScalar(subset.fTop) / texture->height(),
1194 SkIntToScalar(subset.width()) / texture->width(),
1195 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196}
1197
bsalomonb1b01992015-11-18 10:56:08 -08001198void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1199 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001200 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001201 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001202 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001203
bsalomonb1b01992015-11-18 10:56:08 -08001204 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1205 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1206 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1207 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1208 const SkRect* dst = &origDst;
1209 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001210 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001211 if (!src) {
1212 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001214
bsalomonb1b01992015-11-18 10:56:08 -08001215 SkMatrix srcToDstMatrix;
1216 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1217 return;
1218 }
1219 SkRect tmpSrc, tmpDst;
1220 if (src != &bmpBounds) {
1221 if (!bmpBounds.contains(*src)) {
1222 tmpSrc = *src;
1223 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224 return; // nothing to draw
1225 }
bsalomonb1b01992015-11-18 10:56:08 -08001226 src = &tmpSrc;
1227 srcToDstMatrix.mapRect(&tmpDst, *src);
1228 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229 }
1230 }
1231
bsalomonb1b01992015-11-18 10:56:08 -08001232 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001233
bsalomonb1b01992015-11-18 10:56:08 -08001234 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1235 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001236 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001237 paint.isAntiAlias() &&
1238 bitmap.width() <= maxTileSize &&
1239 bitmap.height() <= maxTileSize;
1240
1241 bool skipTileCheck = drawAA || paint.getMaskFilter();
1242
1243 if (!skipTileCheck) {
1244 int tileSize;
1245 SkIRect clippedSrcRect;
1246
Brian Salomon514baff2016-11-17 15:17:07 -05001247 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001248 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001249 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -08001250 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1251 &doBicubic);
1252
1253 int tileFilterPad;
1254
1255 if (doBicubic) {
1256 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001257 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001258 tileFilterPad = 0;
1259 } else {
1260 tileFilterPad = 1;
1261 }
1262 params.setFilterMode(textureFilterMode);
1263
1264 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001265 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1266 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1267 &clippedSrcRect)) {
1268 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1269 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001270 return;
1271 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001272 }
Hal Canary144caf52016-11-07 17:57:18 -05001273 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001274 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001275}
1276
robertphillips6451a0c2016-07-18 08:31:31 -07001277sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001278 SkAutoLockPixels alp(bitmap, true);
1279 if (!bitmap.readyToDraw()) {
1280 return nullptr;
1281 }
1282
Brian Osman7b8400d2016-11-08 17:08:54 -05001283 sk_sp<GrTexture> texture =
Brian Salomon514baff2016-11-17 15:17:07 -05001284 GrMakeCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001285 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware);
robertphillips6451a0c2016-07-18 08:31:31 -07001286 if (!texture) {
1287 return nullptr;
1288 }
1289
1290 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1291 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001292 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001293 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001294 &this->surfaceProps());
1295}
1296
reede51c3562016-07-19 14:33:20 -07001297sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001298 SkPixmap pm;
1299 if (image->isTextureBacked()) {
1300 GrTexture* texture = as_IB(image)->peekTexture();
1301
1302 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1303 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001304 sk_ref_sp(texture),
1305 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001306 &this->surfaceProps());
1307 } else if (image->peekPixels(&pm)) {
1308 SkBitmap bm;
1309
1310 bm.installPixels(pm);
1311 return this->makeSpecial(bm);
1312 } else {
1313 return nullptr;
1314 }
1315}
1316
1317sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Brian Osman11052242016-10-27 14:47:55 -04001318 sk_sp<GrTexture> texture(this->accessRenderTargetContext()->asTexture());
robertphillips1b5f9682016-07-15 08:01:12 -07001319 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001320 // When the device doesn't have a texture, we create a temporary texture.
1321 // TODO: we should actually only copy the portion of the source needed to apply the image
1322 // filter
Brian Osman11052242016-10-27 14:47:55 -04001323 texture.reset(fContext->textureProvider()->createTexture(
1324 this->accessRenderTargetContext()->desc(), SkBudgeted::kYes));
robertphillips04d62182016-07-15 12:21:33 -07001325 if (!texture) {
1326 return nullptr;
1327 }
1328
Brian Osman11052242016-10-27 14:47:55 -04001329 if (!fContext->copySurface(texture.get(),
1330 this->accessRenderTargetContext()->accessRenderTarget())) {
robertphillips04d62182016-07-15 12:21:33 -07001331 return nullptr;
1332 }
robertphillips1b5f9682016-07-15 08:01:12 -07001333 }
1334
1335 const SkImageInfo ii = this->imageInfo();
1336 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1337
1338 return SkSpecialImage::MakeFromGpu(srcRect,
1339 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -07001340 std::move(texture),
1341 sk_ref_sp(ii.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001342 &this->surfaceProps());
1343}
1344
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001346 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001347 SkASSERT(!paint.getImageFilter());
1348
joshualittce894002016-01-11 13:29:31 -08001349 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001351 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001352
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001354 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355
robertphillips1b5f9682016-07-15 08:01:12 -07001356 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001357 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001358 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 return;
1360 }
1361
robertphillips1b5f9682016-07-15 08:01:12 -07001362 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363}
1364
reeda85d4d02015-05-06 12:56:48 -07001365void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1366 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001367 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001368 SkMatrix viewMatrix = *draw.fMatrix;
1369 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001370 uint32_t pinnedUniqueID;
1371 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001372 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001373 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001374 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001375 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1376 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001377 return;
reed85d91782015-09-10 14:33:38 -07001378 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001379 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001380 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001381 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001382 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman57ae6cf2016-11-15 18:07:26 +00001383 if (!as_IB(image)->getROPixels(&bm)) {
reed85d91782015-09-10 14:33:38 -07001384 return;
1385 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001386 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1387 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1388 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001389 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001390 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1391 viewMatrix, fClip, paint);
Brian Osman57ae6cf2016-11-15 18:07:26 +00001392 } else if (as_IB(image)->getROPixels(&bm)) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001393 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001394 }
reeda85d4d02015-05-06 12:56:48 -07001395 }
1396}
1397
1398void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001399 const SkRect& dst, const SkPaint& paint,
1400 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001401 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001402 uint32_t pinnedUniqueID;
1403 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001404 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001405 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001406 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001407 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001408 return;
1409 }
1410 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001411 SkMatrix srcToDstRect;
1412 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1413 dst, SkMatrix::kFill_ScaleToFit);
1414 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1415 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001416 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman57ae6cf2016-11-15 18:07:26 +00001417 if (!as_IB(image)->getROPixels(&bm)) {
bsalomonc55271f2015-11-09 11:55:57 -08001418 return;
1419 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001420 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1421 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1422 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001423 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001424 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
Brian Osman57ae6cf2016-11-15 18:07:26 +00001425 } else if (as_IB(image)->getROPixels(&bm)) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001426 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001427 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001428}
1429
bsalomonf1ecd212015-12-09 17:06:02 -08001430void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001431 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001432 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001433
joshualitt33a5fce2015-11-18 13:28:51 -08001434 CHECK_SHOULD_DRAW(draw);
1435
joshualittedb36442015-11-19 14:29:30 -08001436 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001437 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001438 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001439 GrSamplerParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001440 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1441 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001442 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001443 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001444
1445 SkRect srcR, dstR;
1446 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001447 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001448 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001449 }
1450 return;
1451 }
1452
Brian Salomon514baff2016-11-17 15:17:07 -05001453 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001454 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001455 producer->createFragmentProcessor(SkMatrix::I(),
1456 SkRect::MakeIWH(producer->width(), producer->height()),
1457 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman11052242016-10-27 14:47:55 -04001458 &kMode, fRenderTargetContext->getColorSpace(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001459 fRenderTargetContext->colorMode()));
joshualitt33a5fce2015-11-18 13:28:51 -08001460 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001461 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1462 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1463 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001464 return;
1465 }
1466
msarett10e3d9b2016-08-18 15:46:03 -07001467 std::unique_ptr<SkLatticeIter> iter(
1468 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Osman11052242016-10-27 14:47:55 -04001469 fRenderTargetContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1470 producer->height(), std::move(iter), dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001471}
1472
1473void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1474 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001475 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001476 uint32_t pinnedUniqueID;
1477 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1478 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001479 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001480 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001481 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001482 } else {
1483 SkBitmap bm;
1484 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001485 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001486 this->drawProducerNine(draw, &maker, center, dst, paint);
Brian Osman57ae6cf2016-11-15 18:07:26 +00001487 } else if (as_IB(image)->getROPixels(&bm)) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001488 this->drawBitmapNine(draw, bm, center, dst, paint);
1489 }
1490 }
1491}
1492
1493void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1494 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001495 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001496 GrBitmapTextureMaker maker(fContext.get(), bitmap);
reedc7ec7c92016-07-25 08:29:10 -07001497 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001498}
1499
msarett10e3d9b2016-08-18 15:46:03 -07001500void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1501 const SkCanvas::Lattice& lattice, const SkRect& dst,
1502 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001503 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001504
1505 CHECK_SHOULD_DRAW(draw);
1506
Brian Salomon514baff2016-11-17 15:17:07 -05001507 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001508 sk_sp<GrFragmentProcessor> fp(
1509 producer->createFragmentProcessor(SkMatrix::I(),
1510 SkRect::MakeIWH(producer->width(), producer->height()),
1511 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman11052242016-10-27 14:47:55 -04001512 &kMode, fRenderTargetContext->getColorSpace(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001513 fRenderTargetContext->colorMode()));
msarett10e3d9b2016-08-18 15:46:03 -07001514 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001515 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1516 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1517 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001518 return;
1519 }
1520
1521 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001522 new SkLatticeIter(lattice, dst));
Brian Osman11052242016-10-27 14:47:55 -04001523 fRenderTargetContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1524 producer->height(), std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001525}
1526
1527void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1528 const SkCanvas::Lattice& lattice, const SkRect& dst,
1529 const SkPaint& paint) {
1530 ASSERT_SINGLE_OWNER
1531 uint32_t pinnedUniqueID;
1532 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1533 CHECK_SHOULD_DRAW(draw);
1534 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1535 as_IB(image)->onImageInfo().colorSpace());
1536 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1537 } else {
1538 SkBitmap bm;
1539 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001540 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
msarett10e3d9b2016-08-18 15:46:03 -07001541 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
Brian Osman57ae6cf2016-11-15 18:07:26 +00001542 } else if (as_IB(image)->getROPixels(&bm)) {
msarett10e3d9b2016-08-18 15:46:03 -07001543 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1544 }
1545 }
1546}
1547
1548void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1549 const SkCanvas::Lattice& lattice, const SkRect& dst,
1550 const SkPaint& paint) {
1551 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001552 GrBitmapTextureMaker maker(fContext.get(), bitmap);
msarett10e3d9b2016-08-18 15:46:03 -07001553 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1554}
1555
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001556///////////////////////////////////////////////////////////////////////////////
1557
1558// must be in SkCanvas::VertexMode order
1559static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1560 kTriangles_GrPrimitiveType,
1561 kTriangleStrip_GrPrimitiveType,
1562 kTriangleFan_GrPrimitiveType,
1563};
1564
1565void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1566 int vertexCount, const SkPoint vertices[],
1567 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001568 SkBlendMode bmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001569 const uint16_t indices[], int indexCount,
1570 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001571 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001572 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001573 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001574
halcanary96fcdcc2015-08-27 07:41:13 -07001575 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1576 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001577
halcanary96fcdcc2015-08-27 07:41:13 -07001578 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001579
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001580 SkPaint copy(paint);
1581 copy.setStyle(SkPaint::kStroke_Style);
1582 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001583
bsalomonf1b7a1d2015-09-28 06:26:28 -07001584 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001585 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001586 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1587 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001588 return;
1589 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001590
dandov32a311b2014-07-15 19:46:26 -07001591 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001592 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001593 switch (vmode) {
1594 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001595 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001596 break;
1597 case SkCanvas::kTriangleStrip_VertexMode:
1598 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001599 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001600 break;
1601 }
mtklein533eb782014-08-27 10:39:42 -07001602
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001603 VertState state(vertexCount, indices, indexCount);
1604 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001605
dandov32a311b2014-07-15 19:46:26 -07001606 //number of indices for lines per triangle with kLines
1607 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001608
Ben Wagner7ecc5962016-11-02 17:07:33 -04001609 std::unique_ptr<uint16_t[]> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001610 int i = 0;
1611 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001612 lineIndices[i] = state.f0;
1613 lineIndices[i + 1] = state.f1;
1614 lineIndices[i + 2] = state.f1;
1615 lineIndices[i + 3] = state.f2;
1616 lineIndices[i + 4] = state.f2;
1617 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001618 i += 6;
1619 }
Brian Osman11052242016-10-27 14:47:55 -04001620 fRenderTargetContext->drawVertices(fClip,
1621 grPaint,
1622 *draw.fMatrix,
1623 kLines_GrPrimitiveType,
1624 vertexCount,
1625 vertices,
1626 texs,
1627 colors,
1628 lineIndices.get(),
1629 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001630 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001631 }
1632
bsalomonf1b7a1d2015-09-28 06:26:28 -07001633 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634
1635 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001636 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001637 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1638 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639 convertedColors.reset(vertexCount);
1640 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001641 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642 }
1643 colors = convertedColors.get();
1644 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001645 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001646 if (texs && paint.getShader()) {
1647 if (colors) {
Mike Reedfaba3712016-11-03 14:45:31 -04001648 // When there are texs and colors the shader and colors are combined using bmode.
Brian Osman11052242016-10-27 14:47:55 -04001649 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), paint,
Mike Reedfaba3712016-11-03 14:45:31 -04001650 *draw.fMatrix, bmode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001651 return;
1652 }
1653 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001654 // We have a shader, but no colors to blend it against.
Brian Osman11052242016-10-27 14:47:55 -04001655 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001656 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001657 return;
1658 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001659 }
bsalomonaa48d362015-10-01 08:34:17 -07001660 } else {
1661 if (colors) {
1662 // We have colors, but either have no shader or no texture coords (which implies that
1663 // we should ignore the shader).
Brian Osman11052242016-10-27 14:47:55 -04001664 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext.get(),
1665 paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001666 return;
1667 }
1668 } else {
1669 // No colors and no shaders. Just draw with the paint color.
Brian Osman11052242016-10-27 14:47:55 -04001670 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), paint,
1671 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001672 return;
1673 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001674 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001675 }
1676
Brian Osman11052242016-10-27 14:47:55 -04001677 fRenderTargetContext->drawVertices(fClip,
1678 grPaint,
1679 *draw.fMatrix,
1680 primType,
1681 vertexCount,
1682 vertices,
1683 texs,
1684 colors,
1685 indices,
1686 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687}
1688
1689///////////////////////////////////////////////////////////////////////////////
1690
jvanverth31ff7622015-08-07 10:09:28 -07001691void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001692 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001693 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001694 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001695 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001696 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001697 return;
1698 }
1699
jvanverth31ff7622015-08-07 10:09:28 -07001700 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001701 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001702
reedca109532015-06-25 16:25:25 -07001703 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001704 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001705
jvanverth31ff7622015-08-07 10:09:28 -07001706 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001707 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001708 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed7d954ad2016-10-28 15:42:34 -04001709 *draw.fMatrix, (SkBlendMode)mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001710 return;
1711 }
1712 } else {
Brian Osman11052242016-10-27 14:47:55 -04001713 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, *draw.fMatrix,
1714 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001715 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001716 }
1717 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001718
1719 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001720 fRenderTargetContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001721}
1722
1723///////////////////////////////////////////////////////////////////////////////
1724
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001725void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001726 size_t byteLength, SkScalar x, SkScalar y,
1727 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001728 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001729 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001730 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001731
jvanverth8c27a182014-10-14 08:45:50 -07001732 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001733 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1734 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001735 return;
1736 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001737
jvanverth8c27a182014-10-14 08:45:50 -07001738 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001739
Brian Osman11052242016-10-27 14:47:55 -04001740 fRenderTargetContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
1741 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742}
1743
fmalita05c4a432014-09-29 06:29:53 -07001744void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1745 const SkScalar pos[], int scalarsPerPos,
1746 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001747 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001748 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -08001749 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750
jvanverth8c27a182014-10-14 08:45:50 -07001751 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001752 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1753 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001754 return;
1755 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001756
jvanverth8c27a182014-10-14 08:45:50 -07001757 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001758
Brian Osman11052242016-10-27 14:47:55 -04001759 fRenderTargetContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
1760 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1761 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001762}
1763
joshualitt9c328182015-03-23 08:13:04 -07001764void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1765 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001766 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001767 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001768 CHECK_SHOULD_DRAW(draw);
1769
1770 SkDEBUGCODE(this->validate();)
1771
Brian Osman11052242016-10-27 14:47:55 -04001772 fRenderTargetContext->drawTextBlob(fClip, paint, *draw.fMatrix,
1773 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001774}
1775
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776///////////////////////////////////////////////////////////////////////////////
1777
reedb2db8982014-11-13 12:41:02 -08001778bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001779 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780}
1781
1782void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001783 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001784
Brian Osman11052242016-10-27 14:47:55 -04001785 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
reed76033be2015-03-14 10:54:31 -07001790SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001791 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792
robertphillipsca6eafc2016-05-17 09:57:46 -07001793 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794
robertphillipsca6eafc2016-05-17 09:57:46 -07001795 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001796 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001797 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1798 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001799
Brian Osman11052242016-10-27 14:47:55 -04001800 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1801 fit,
1802 cinfo.fInfo.width(), cinfo.fInfo.height(),
1803 fRenderTargetContext->config(),
1804 sk_ref_sp(fRenderTargetContext->getColorSpace()),
1805 fRenderTargetContext->desc().fSampleCnt,
1806 kDefault_GrSurfaceOrigin,
1807 &props));
1808 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001809 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001810 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001811
1812 // Skia's convention is to only clear a device if it is non-opaque.
1813 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1814
Robert Phillips9fab7e92016-11-17 12:45:04 -05001815 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1816 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001817}
1818
reede8f30622016-03-23 18:59:25 -07001819sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001820 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001821 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001822 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001823 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Osman11052242016-10-27 14:47:55 -04001824 fRenderTargetContext->desc().fSampleCnt,
1825 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001826}
1827
senorblanco900c3672016-04-27 11:31:23 -07001828SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001829 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001830 // We always return a transient cache, so it is freed after each
1831 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001832 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001833}
reedf037e0b2014-10-30 11:34:15 -07001834
1835#endif