blob: aae3b95ffb6f45174cb696c5ddbd894d5ee11e95 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
Brian Osman3b66ab62016-11-28 09:26:31 -050010#include "GrBitmapTextureMaker.h"
robertphillipsccb1b572015-05-27 11:02:55 -070011#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080012#include "GrContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050014#include "GrImageTextureMaker.h"
15#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070016#include "GrStyle.h"
egdanielbbcb38d2014-06-19 10:19:29 -070017#include "GrTracing.h"
robertphillips714712b2016-08-04 06:20:45 -070018
robertphillips30d78412014-11-24 09:49:17 -080019#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070020#include "SkDraw.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000021#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070022#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070023#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070024#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080025#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000026#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070027#include "SkImageFilterCache.h"
msarettc573a402016-08-02 08:05:56 -070028#include "SkLatticeIter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
reed1e7f5e72016-04-27 07:49:17 -070033#include "SkRasterClip.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070036#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000038#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080039#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.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());
Robert Phillips833dcf42016-11-18 08:44:13 -05001162 if (!texture) {
1163 return;
1164 }
robertphillips970587b2016-07-14 14:12:55 -07001165
1166 SkPaint tmpUnfiltered(paint);
1167 tmpUnfiltered.setImageFilter(nullptr);
1168
brianosman77320db2016-09-07 08:09:10 -07001169 sk_sp<GrColorSpaceXform> colorSpaceXform =
Brian Osman11052242016-10-27 14:47:55 -04001170 GrColorSpaceXform::Make(result->getColorSpace(), fRenderTargetContext->getColorSpace());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001172 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1173 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001174 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001175 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001176 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001177 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001178 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001179 }
Brian Osman11052242016-10-27 14:47:55 -04001180 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext.get(), tmpUnfiltered,
brianosman8fe485b2016-07-25 12:31:51 -07001181 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001182 return;
1183 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184
robertphillips970587b2016-07-14 14:12:55 -07001185 const SkIRect& subset = result->subset();
1186
Brian Osman11052242016-10-27 14:47:55 -04001187 fRenderTargetContext->fillRectToRect(fClip,
1188 grPaint,
1189 SkMatrix::I(),
1190 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX,
1191 top + offset.fY,
1192 subset.width(),
1193 subset.height())),
1194 SkRect::MakeXYWH(
1195 SkIntToScalar(subset.fLeft) / texture->width(),
1196 SkIntToScalar(subset.fTop) / texture->height(),
1197 SkIntToScalar(subset.width()) / texture->width(),
1198 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199}
1200
bsalomonb1b01992015-11-18 10:56:08 -08001201void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1202 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001203 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001204 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001205 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001206
bsalomonb1b01992015-11-18 10:56:08 -08001207 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1208 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1209 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1210 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1211 const SkRect* dst = &origDst;
1212 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001214 if (!src) {
1215 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001217
bsalomonb1b01992015-11-18 10:56:08 -08001218 SkMatrix srcToDstMatrix;
1219 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1220 return;
1221 }
1222 SkRect tmpSrc, tmpDst;
1223 if (src != &bmpBounds) {
1224 if (!bmpBounds.contains(*src)) {
1225 tmpSrc = *src;
1226 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001227 return; // nothing to draw
1228 }
bsalomonb1b01992015-11-18 10:56:08 -08001229 src = &tmpSrc;
1230 srcToDstMatrix.mapRect(&tmpDst, *src);
1231 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 }
1233 }
1234
bsalomonb1b01992015-11-18 10:56:08 -08001235 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001236
bsalomonb1b01992015-11-18 10:56:08 -08001237 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1238 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Brian Osman11052242016-10-27 14:47:55 -04001239 bool drawAA = !fRenderTargetContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001240 paint.isAntiAlias() &&
1241 bitmap.width() <= maxTileSize &&
1242 bitmap.height() <= maxTileSize;
1243
1244 bool skipTileCheck = drawAA || paint.getMaskFilter();
1245
1246 if (!skipTileCheck) {
1247 int tileSize;
1248 SkIRect clippedSrcRect;
1249
Brian Salomon514baff2016-11-17 15:17:07 -05001250 GrSamplerParams params;
bsalomonb1b01992015-11-18 10:56:08 -08001251 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001252 GrSamplerParams::FilterMode textureFilterMode =
bsalomonb1b01992015-11-18 10:56:08 -08001253 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1254 &doBicubic);
1255
1256 int tileFilterPad;
1257
1258 if (doBicubic) {
1259 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon514baff2016-11-17 15:17:07 -05001260 } else if (GrSamplerParams::kNone_FilterMode == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001261 tileFilterPad = 0;
1262 } else {
1263 tileFilterPad = 1;
1264 }
1265 params.setFilterMode(textureFilterMode);
1266
1267 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001268 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1269 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1270 &clippedSrcRect)) {
1271 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1272 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001273 return;
1274 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001275 }
Hal Canary144caf52016-11-07 17:57:18 -05001276 GrBitmapTextureMaker maker(fContext.get(), bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001277 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278}
1279
robertphillips6451a0c2016-07-18 08:31:31 -07001280sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001281 SkAutoLockPixels alp(bitmap, true);
1282 if (!bitmap.readyToDraw()) {
1283 return nullptr;
1284 }
1285
Brian Osman7b8400d2016-11-08 17:08:54 -05001286 sk_sp<GrTexture> texture =
Brian Salomon514baff2016-11-17 15:17:07 -05001287 GrMakeCachedBitmapTexture(fContext.get(), bitmap, GrSamplerParams::ClampNoFilter(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001288 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware);
robertphillips6451a0c2016-07-18 08:31:31 -07001289 if (!texture) {
1290 return nullptr;
1291 }
1292
1293 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1294 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001295 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001296 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001297 &this->surfaceProps());
1298}
1299
reede51c3562016-07-19 14:33:20 -07001300sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001301 SkPixmap pm;
1302 if (image->isTextureBacked()) {
1303 GrTexture* texture = as_IB(image)->peekTexture();
1304
1305 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1306 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001307 sk_ref_sp(texture),
1308 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001309 &this->surfaceProps());
1310 } else if (image->peekPixels(&pm)) {
1311 SkBitmap bm;
1312
1313 bm.installPixels(pm);
1314 return this->makeSpecial(bm);
1315 } else {
1316 return nullptr;
1317 }
1318}
1319
1320sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Brian Osman11052242016-10-27 14:47:55 -04001321 sk_sp<GrTexture> texture(this->accessRenderTargetContext()->asTexture());
robertphillips1b5f9682016-07-15 08:01:12 -07001322 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001323 // When the device doesn't have a texture, we create a temporary texture.
1324 // TODO: we should actually only copy the portion of the source needed to apply the image
1325 // filter
Brian Osman11052242016-10-27 14:47:55 -04001326 texture.reset(fContext->textureProvider()->createTexture(
1327 this->accessRenderTargetContext()->desc(), SkBudgeted::kYes));
robertphillips04d62182016-07-15 12:21:33 -07001328 if (!texture) {
1329 return nullptr;
1330 }
1331
Brian Osman11052242016-10-27 14:47:55 -04001332 if (!fContext->copySurface(texture.get(),
1333 this->accessRenderTargetContext()->accessRenderTarget())) {
robertphillips04d62182016-07-15 12:21:33 -07001334 return nullptr;
1335 }
robertphillips1b5f9682016-07-15 08:01:12 -07001336 }
1337
1338 const SkImageInfo ii = this->imageInfo();
1339 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1340
1341 return SkSpecialImage::MakeFromGpu(srcRect,
1342 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -07001343 std::move(texture),
1344 sk_ref_sp(ii.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001345 &this->surfaceProps());
1346}
1347
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001349 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001350 SkASSERT(!paint.getImageFilter());
1351
joshualittce894002016-01-11 13:29:31 -08001352 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001354 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001356 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001357 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001358
robertphillips1b5f9682016-07-15 08:01:12 -07001359 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001360 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001361 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362 return;
1363 }
1364
robertphillips1b5f9682016-07-15 08:01:12 -07001365 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366}
1367
reeda85d4d02015-05-06 12:56:48 -07001368void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1369 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001370 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001371 SkMatrix viewMatrix = *draw.fMatrix;
1372 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001373 uint32_t pinnedUniqueID;
1374 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001375 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001376 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001377 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001378 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1379 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001380 return;
reed85d91782015-09-10 14:33:38 -07001381 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001382 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001383 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001384 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001385 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman7992da32016-11-18 11:28:24 -05001386 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorMode())) {
reed85d91782015-09-10 14:33:38 -07001387 return;
1388 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001389 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1390 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1391 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001392 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001393 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1394 viewMatrix, fClip, paint);
Brian Osman7992da32016-11-18 11:28:24 -05001395 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorMode())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001396 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001397 }
reeda85d4d02015-05-06 12:56:48 -07001398 }
1399}
1400
1401void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001402 const SkRect& dst, const SkPaint& paint,
1403 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001404 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001405 uint32_t pinnedUniqueID;
1406 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001407 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001408 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001409 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001410 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001411 return;
1412 }
1413 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001414 SkMatrix srcToDstRect;
1415 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1416 dst, SkMatrix::kFill_ScaleToFit);
1417 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1418 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001419 // only support tiling as bitmap at the moment, so force raster-version
Brian Osman7992da32016-11-18 11:28:24 -05001420 if (!as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorMode())) {
bsalomonc55271f2015-11-09 11:55:57 -08001421 return;
1422 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001423 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1424 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1425 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001426 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001427 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
Brian Osman7992da32016-11-18 11:28:24 -05001428 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorMode())) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001429 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001430 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001431}
1432
bsalomonf1ecd212015-12-09 17:06:02 -08001433void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001434 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001435 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext.get());
joshualitt33a5fce2015-11-18 13:28:51 -08001436
joshualitt33a5fce2015-11-18 13:28:51 -08001437 CHECK_SHOULD_DRAW(draw);
1438
joshualittedb36442015-11-19 14:29:30 -08001439 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
Brian Osman11052242016-10-27 14:47:55 -04001440 fRenderTargetContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001441 bool doBicubic;
Brian Salomon514baff2016-11-17 15:17:07 -05001442 GrSamplerParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001443 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1444 &doBicubic);
Brian Salomon514baff2016-11-17 15:17:07 -05001445 if (useFallback || doBicubic || GrSamplerParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001446 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001447
1448 SkRect srcR, dstR;
1449 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001450 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001451 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001452 }
1453 return;
1454 }
1455
Brian Salomon514baff2016-11-17 15:17:07 -05001456 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001457 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001458 producer->createFragmentProcessor(SkMatrix::I(),
1459 SkRect::MakeIWH(producer->width(), producer->height()),
1460 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman11052242016-10-27 14:47:55 -04001461 &kMode, fRenderTargetContext->getColorSpace(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001462 fRenderTargetContext->colorMode()));
joshualitt33a5fce2015-11-18 13:28:51 -08001463 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001464 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1465 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1466 &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001467 return;
1468 }
1469
msarett10e3d9b2016-08-18 15:46:03 -07001470 std::unique_ptr<SkLatticeIter> iter(
1471 new SkLatticeIter(producer->width(), producer->height(), center, dst));
Brian Osman11052242016-10-27 14:47:55 -04001472 fRenderTargetContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1473 producer->height(), std::move(iter), dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001474}
1475
1476void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1477 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001478 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001479 uint32_t pinnedUniqueID;
1480 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1481 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001482 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001483 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001484 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001485 } else {
1486 SkBitmap bm;
1487 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001488 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001489 this->drawProducerNine(draw, &maker, center, dst, paint);
Brian Osman7992da32016-11-18 11:28:24 -05001490 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorMode())) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001491 this->drawBitmapNine(draw, bm, center, dst, paint);
1492 }
1493 }
1494}
1495
1496void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1497 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001498 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001499 GrBitmapTextureMaker maker(fContext.get(), bitmap);
reedc7ec7c92016-07-25 08:29:10 -07001500 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001501}
1502
msarett10e3d9b2016-08-18 15:46:03 -07001503void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1504 const SkCanvas::Lattice& lattice, const SkRect& dst,
1505 const SkPaint& paint) {
Hal Canary144caf52016-11-07 17:57:18 -05001506 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
msarett10e3d9b2016-08-18 15:46:03 -07001507
1508 CHECK_SHOULD_DRAW(draw);
1509
Brian Salomon514baff2016-11-17 15:17:07 -05001510 static const GrSamplerParams::FilterMode kMode = GrSamplerParams::kNone_FilterMode;
msarett10e3d9b2016-08-18 15:46:03 -07001511 sk_sp<GrFragmentProcessor> fp(
1512 producer->createFragmentProcessor(SkMatrix::I(),
1513 SkRect::MakeIWH(producer->width(), producer->height()),
1514 GrTextureProducer::kNo_FilterConstraint, true,
Brian Osman11052242016-10-27 14:47:55 -04001515 &kMode, fRenderTargetContext->getColorSpace(),
Brian Osman7b8400d2016-11-08 17:08:54 -05001516 fRenderTargetContext->colorMode()));
msarett10e3d9b2016-08-18 15:46:03 -07001517 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001518 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext.get(), paint,
1519 *draw.fMatrix, std::move(fp), producer->isAlphaOnly(),
1520 &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001521 return;
1522 }
1523
1524 std::unique_ptr<SkLatticeIter> iter(
msarett71df2d72016-09-30 12:41:42 -07001525 new SkLatticeIter(lattice, dst));
Brian Osman11052242016-10-27 14:47:55 -04001526 fRenderTargetContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1527 producer->height(), std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001528}
1529
1530void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1531 const SkCanvas::Lattice& lattice, const SkRect& dst,
1532 const SkPaint& paint) {
1533 ASSERT_SINGLE_OWNER
1534 uint32_t pinnedUniqueID;
1535 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1536 CHECK_SHOULD_DRAW(draw);
1537 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1538 as_IB(image)->onImageInfo().colorSpace());
1539 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1540 } else {
1541 SkBitmap bm;
1542 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
Hal Canary144caf52016-11-07 17:57:18 -05001543 GrImageTextureMaker maker(fContext.get(), cacher, image, SkImage::kAllow_CachingHint);
msarett10e3d9b2016-08-18 15:46:03 -07001544 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
Brian Osman7992da32016-11-18 11:28:24 -05001545 } else if (as_IB(image)->getROPixels(&bm, fRenderTargetContext->colorMode())) {
msarett10e3d9b2016-08-18 15:46:03 -07001546 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1547 }
1548 }
1549}
1550
1551void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1552 const SkCanvas::Lattice& lattice, const SkRect& dst,
1553 const SkPaint& paint) {
1554 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001555 GrBitmapTextureMaker maker(fContext.get(), bitmap);
msarett10e3d9b2016-08-18 15:46:03 -07001556 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1557}
1558
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559///////////////////////////////////////////////////////////////////////////////
1560
1561// must be in SkCanvas::VertexMode order
1562static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1563 kTriangles_GrPrimitiveType,
1564 kTriangleStrip_GrPrimitiveType,
1565 kTriangleFan_GrPrimitiveType,
1566};
1567
1568void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1569 int vertexCount, const SkPoint vertices[],
1570 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001571 SkBlendMode bmode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 const uint16_t indices[], int indexCount,
1573 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001574 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001575 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001576 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001577
halcanary96fcdcc2015-08-27 07:41:13 -07001578 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1579 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001580
halcanary96fcdcc2015-08-27 07:41:13 -07001581 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001582
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001583 SkPaint copy(paint);
1584 copy.setStyle(SkPaint::kStroke_Style);
1585 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001586
bsalomonf1b7a1d2015-09-28 06:26:28 -07001587 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001588 // we ignore the shader if texs is null.
Brian Osman11052242016-10-27 14:47:55 -04001589 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), copy,
1590 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001591 return;
1592 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001593
dandov32a311b2014-07-15 19:46:26 -07001594 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001595 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001596 switch (vmode) {
1597 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001598 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001599 break;
1600 case SkCanvas::kTriangleStrip_VertexMode:
1601 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001602 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001603 break;
1604 }
mtklein533eb782014-08-27 10:39:42 -07001605
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001606 VertState state(vertexCount, indices, indexCount);
1607 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001608
dandov32a311b2014-07-15 19:46:26 -07001609 //number of indices for lines per triangle with kLines
1610 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001611
Ben Wagner7ecc5962016-11-02 17:07:33 -04001612 std::unique_ptr<uint16_t[]> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001613 int i = 0;
1614 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001615 lineIndices[i] = state.f0;
1616 lineIndices[i + 1] = state.f1;
1617 lineIndices[i + 2] = state.f1;
1618 lineIndices[i + 3] = state.f2;
1619 lineIndices[i + 4] = state.f2;
1620 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001621 i += 6;
1622 }
Brian Osman11052242016-10-27 14:47:55 -04001623 fRenderTargetContext->drawVertices(fClip,
1624 grPaint,
1625 *draw.fMatrix,
1626 kLines_GrPrimitiveType,
1627 vertexCount,
1628 vertices,
1629 texs,
1630 colors,
1631 lineIndices.get(),
1632 indexCount);
bsalomonf1b7a1d2015-09-28 06:26:28 -07001633 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634 }
1635
bsalomonf1b7a1d2015-09-28 06:26:28 -07001636 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637
1638 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001639 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001640 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1641 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642 convertedColors.reset(vertexCount);
1643 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001644 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001645 }
1646 colors = convertedColors.get();
1647 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001648 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001649 if (texs && paint.getShader()) {
1650 if (colors) {
Mike Reedfaba3712016-11-03 14:45:31 -04001651 // When there are texs and colors the shader and colors are combined using bmode.
Brian Osman11052242016-10-27 14:47:55 -04001652 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), paint,
Mike Reedfaba3712016-11-03 14:45:31 -04001653 *draw.fMatrix, bmode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001654 return;
1655 }
1656 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001657 // We have a shader, but no colors to blend it against.
Brian Osman11052242016-10-27 14:47:55 -04001658 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
brianosman8fe485b2016-07-25 12:31:51 -07001659 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001660 return;
1661 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001662 }
bsalomonaa48d362015-10-01 08:34:17 -07001663 } else {
1664 if (colors) {
1665 // We have colors, but either have no shader or no texture coords (which implies that
1666 // we should ignore the shader).
Brian Osman11052242016-10-27 14:47:55 -04001667 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext.get(),
1668 paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001669 return;
1670 }
1671 } else {
1672 // No colors and no shaders. Just draw with the paint color.
Brian Osman11052242016-10-27 14:47:55 -04001673 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext.get(), paint,
1674 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001675 return;
1676 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001677 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001678 }
1679
Brian Osman11052242016-10-27 14:47:55 -04001680 fRenderTargetContext->drawVertices(fClip,
1681 grPaint,
1682 *draw.fMatrix,
1683 primType,
1684 vertexCount,
1685 vertices,
1686 texs,
1687 colors,
1688 indices,
1689 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001690}
1691
1692///////////////////////////////////////////////////////////////////////////////
1693
jvanverth31ff7622015-08-07 10:09:28 -07001694void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001695 const SkRect texRect[], const SkColor colors[], int count,
Mike Reedfaba3712016-11-03 14:45:31 -04001696 SkBlendMode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001697 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001698 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001699 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001700 return;
1701 }
1702
jvanverth31ff7622015-08-07 10:09:28 -07001703 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001704 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -08001705
reedca109532015-06-25 16:25:25 -07001706 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001707 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001708
jvanverth31ff7622015-08-07 10:09:28 -07001709 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001710 if (colors) {
Brian Osman11052242016-10-27 14:47:55 -04001711 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext.get(), p,
Mike Reed7d954ad2016-10-28 15:42:34 -04001712 *draw.fMatrix, (SkBlendMode)mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001713 return;
1714 }
1715 } else {
Brian Osman11052242016-10-27 14:47:55 -04001716 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), p, *draw.fMatrix,
1717 &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001718 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001719 }
1720 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001721
1722 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001723 fRenderTargetContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001724}
1725
1726///////////////////////////////////////////////////////////////////////////////
1727
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001728void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001729 size_t byteLength, SkScalar x, SkScalar y,
1730 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001731 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001732 CHECK_SHOULD_DRAW(draw);
Hal Canary144caf52016-11-07 17:57:18 -05001733 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001734
jvanverth8c27a182014-10-14 08:45:50 -07001735 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001736 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1737 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001738 return;
1739 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001740
jvanverth8c27a182014-10-14 08:45:50 -07001741 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001742
Brian Osman11052242016-10-27 14:47:55 -04001743 fRenderTargetContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
1744 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001745}
1746
fmalita05c4a432014-09-29 06:29:53 -07001747void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1748 const SkScalar pos[], int scalarsPerPos,
1749 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001750 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001751 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext.get());
joshualitt5531d512014-12-17 15:50:11 -08001752 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001753
jvanverth8c27a182014-10-14 08:45:50 -07001754 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -04001755 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext.get(), paint, *draw.fMatrix,
1756 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001757 return;
1758 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001759
jvanverth8c27a182014-10-14 08:45:50 -07001760 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001761
Brian Osman11052242016-10-27 14:47:55 -04001762 fRenderTargetContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
1763 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1764 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765}
1766
joshualitt9c328182015-03-23 08:13:04 -07001767void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1768 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001769 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001770 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext.get());
joshualitt9c328182015-03-23 08:13:04 -07001771 CHECK_SHOULD_DRAW(draw);
1772
1773 SkDEBUGCODE(this->validate();)
1774
Brian Osman11052242016-10-27 14:47:55 -04001775 fRenderTargetContext->drawTextBlob(fClip, paint, *draw.fMatrix,
1776 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001777}
1778
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001779///////////////////////////////////////////////////////////////////////////////
1780
reedb2db8982014-11-13 12:41:02 -08001781bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001782 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783}
1784
1785void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001786 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001787
Brian Osman11052242016-10-27 14:47:55 -04001788 fRenderTargetContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789}
1790
1791///////////////////////////////////////////////////////////////////////////////
1792
reed76033be2015-03-14 10:54:31 -07001793SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001794 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795
robertphillipsca6eafc2016-05-17 09:57:46 -07001796 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797
robertphillipsca6eafc2016-05-17 09:57:46 -07001798 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001799 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001800 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1801 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001802
Brian Osman11052242016-10-27 14:47:55 -04001803 sk_sp<GrRenderTargetContext> rtc(fContext->makeRenderTargetContext(
1804 fit,
1805 cinfo.fInfo.width(), cinfo.fInfo.height(),
1806 fRenderTargetContext->config(),
1807 sk_ref_sp(fRenderTargetContext->getColorSpace()),
1808 fRenderTargetContext->desc().fSampleCnt,
1809 kDefault_GrSurfaceOrigin,
1810 &props));
1811 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001812 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001813 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001814
1815 // Skia's convention is to only clear a device if it is non-opaque.
1816 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1817
Robert Phillips9fab7e92016-11-17 12:45:04 -05001818 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1819 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820}
1821
reede8f30622016-03-23 18:59:25 -07001822sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001823 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001824 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001825 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001826 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Brian Osman11052242016-10-27 14:47:55 -04001827 fRenderTargetContext->desc().fSampleCnt,
1828 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001829}
1830
senorblanco900c3672016-04-27 11:31:23 -07001831SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001832 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001833 // We always return a transient cache, so it is freed after each
1834 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001835 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001836}
reedf037e0b2014-10-30 11:34:15 -07001837
1838#endif