blob: 376d5e1b57669789c47d504126db628f06313761 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
robertphillips714712b2016-08-04 06:20:45 -070012#include "GrDrawContextPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
bsalomonc55271f2015-11-09 11:55:57 -080014#include "GrImageIDTextureAdjuster.h"
bsalomon6663acf2016-05-10 09:14:17 -070015#include "GrStyle.h"
egdanielbbcb38d2014-06-19 10:19:29 -070016#include "GrTracing.h"
robertphillips714712b2016-08-04 06:20:45 -070017
robertphillips30d78412014-11-24 09:49:17 -080018#include "SkCanvasPriv.h"
robertphillips714712b2016-08-04 06:20:45 -070019#include "SkDraw.h"
kkinnunenabcfab42015-02-22 22:53:44 -080020#include "SkErrorInternals.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"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070044#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "effects/GrBicubicEffect.h"
46#include "effects/GrDashingEffect.h"
47#include "effects/GrSimpleTextureEffect.h"
48#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080049#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050
reedf037e0b2014-10-30 11:34:15 -070051#if SK_SUPPORT_GPU
52
joshualittce894002016-01-11 13:29:31 -080053#define ASSERT_SINGLE_OWNER \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
55
senorblanco55b6d8b2014-07-30 11:26:46 -070056enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
57
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#if 0
59 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080060 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 do { \
62 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080063 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064 } while (0)
65#else
joshualitt5531d512014-12-17 15:50:11 -080066 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067#endif
68
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069///////////////////////////////////////////////////////////////////////////////
70
bsalomon74f681d2015-06-23 14:38:48 -070071/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
72 should fail. */
73bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
74 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
75 *flags = 0;
76 if (info) {
77 switch (info->alphaType()) {
78 case kPremul_SkAlphaType:
79 break;
80 case kOpaque_SkAlphaType:
81 *flags |= SkGpuDevice::kIsOpaque_Flag;
82 break;
83 default: // If it is unpremul or unknown don't try to render
84 return false;
85 }
86 }
87 if (kClear_InitContents == init) {
88 *flags |= kNeedClear_Flag;
89 }
90 return true;
91}
92
robertphillips15c42ca2016-08-04 08:45:02 -070093sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrDrawContext> drawContext,
94 int width, int height,
95 InitContents init) {
robertphillipsca6eafc2016-05-17 09:57:46 -070096 if (!drawContext || drawContext->wasAbandoned()) {
97 return nullptr;
98 }
99 unsigned flags;
100 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
101 return nullptr;
102 }
robertphillips15c42ca2016-08-04 08:45:02 -0700103 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000104}
105
robertphillips24e91282016-04-29 06:46:36 -0700106sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
107 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700108 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700109 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700110 unsigned flags;
111 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700113 }
114
robertphillips7e922762016-07-26 11:38:17 -0700115 sk_sp<GrDrawContext> drawContext(MakeDrawContext(context, budgeted, info,
116 sampleCount, origin, props));
robertphillipsca6eafc2016-05-17 09:57:46 -0700117 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700119 }
120
robertphillipsca6eafc2016-05-17 09:57:46 -0700121 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext),
122 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700123}
124
robertphillipsca6eafc2016-05-17 09:57:46 -0700125SkGpuDevice::SkGpuDevice(sk_sp<GrDrawContext> drawContext, int width, int height, unsigned flags)
126 : INHERITED(drawContext->surfaceProps())
127 , fContext(SkRef(drawContext->accessRenderTarget()->getContext()))
robertphillipsca6eafc2016-05-17 09:57:46 -0700128 , fDrawContext(std::move(drawContext)) {
robertphillips1f3923e2016-07-21 07:17:54 -0700129 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700130 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000131
bsalomone63ffef2016-02-05 07:17:34 -0800132 if (flags & kNeedClear_Flag) {
133 this->clearAll();
134 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135}
136
robertphillips7e922762016-07-26 11:38:17 -0700137sk_sp<GrDrawContext> SkGpuDevice::MakeDrawContext(GrContext* context,
138 SkBudgeted budgeted,
139 const SkImageInfo& origInfo,
140 int sampleCount,
141 GrSurfaceOrigin origin,
142 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000143 if (kUnknown_SkColorType == origInfo.colorType() ||
144 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000146 }
147
bsalomonafe30052015-01-16 07:32:33 -0800148 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700149 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800150 }
151
reede5ea5002014-09-03 11:54:58 -0700152 SkColorType ct = origInfo.colorType();
153 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700154 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700155 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700156 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800157 }
158 if (kOpaque_SkAlphaType != at) {
159 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 }
brianosman6b086522016-04-14 12:39:00 -0700161
brianosmanb109b8c2016-06-16 13:03:24 -0700162 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
brianosman6b086522016-04-14 12:39:00 -0700163 if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) {
164 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
165 ct = kN32_SkColorType;
166 }
167
brianosmanb109b8c2016-06-16 13:03:24 -0700168 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000169
robertphillips6738c702016-07-27 12:13:51 -0700170 return context->makeDrawContext(SkBackingFit::kExact, // Why exact?
171 origInfo.width(), origInfo.height(),
172 config, sk_ref_sp(cs), sampleCount,
173 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800174}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175
robertphillips970587b2016-07-14 14:12:55 -0700176sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
177 SkSpecialImage* srcImg,
178 int left, int top,
179 SkIPoint* offset,
180 const SkImageFilter* filter) {
181 SkASSERT(srcImg->isTextureBacked());
182 SkASSERT(filter);
183
184 SkMatrix matrix = *draw.fMatrix;
185 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
186 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
187 SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
188 SkImageFilter::Context ctx(matrix, clipBounds, cache.get());
189
190 return filter->filterImage(srcImg, ctx, offset);
191}
192
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000193///////////////////////////////////////////////////////////////////////////////
194
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000195bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
196 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800197 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000198
199 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700200 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000201 if (kUnknown_GrPixelConfig == config) {
202 return false;
203 }
204
205 uint32_t flags = 0;
206 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
207 flags = GrContext::kUnpremul_PixelOpsFlag;
208 }
robertphillips714712b2016-08-04 06:20:45 -0700209 return fDrawContext->accessRenderTarget()->readPixels(x, y,
210 dstInfo.width(), dstInfo.height(),
211 config, dstPixels,
212 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000213}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000215bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
216 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800217 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000218 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700219 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000220 if (kUnknown_GrPixelConfig == config) {
221 return false;
222 }
223 uint32_t flags = 0;
224 if (kUnpremul_SkAlphaType == info.alphaType()) {
225 flags = GrContext::kUnpremul_PixelOpsFlag;
226 }
robertphillips15c42ca2016-08-04 08:45:02 -0700227 return fDrawContext->accessRenderTarget()->writePixels(x, y, info.width(), info.height(),
228 config, pixels, rowBytes, flags);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000229}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000230
reed41e010c2015-06-09 12:16:53 -0700231bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800232 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700233 return false;
234}
235
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000236// call this every draw call, to ensure that the context reflects our state,
237// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800238void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800239 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000240
reed2c9e2002016-07-25 08:05:22 -0700241 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000242}
243
robertphillips175dd9b2016-04-28 14:32:04 -0700244GrDrawContext* SkGpuDevice::accessDrawContext() {
245 ASSERT_SINGLE_OWNER
246 return fDrawContext.get();
247}
248
reed8eddfb52014-12-04 07:50:14 -0800249void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800250 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800251 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800252 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800253 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700254 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800255}
256
robertphillipsca6eafc2016-05-17 09:57:46 -0700257void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800258 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800259
robertphillips714712b2016-08-04 06:20:45 -0700260 SkBudgeted budgeted = fDrawContext->drawContextPriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800261
robertphillips7e922762016-07-26 11:38:17 -0700262 sk_sp<GrDrawContext> newDC(MakeDrawContext(this->context(),
263 budgeted,
264 this->imageInfo(),
265 fDrawContext->numColorSamples(),
266 fDrawContext->origin(),
267 &this->surfaceProps()));
robertphillipsca6eafc2016-05-17 09:57:46 -0700268 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800269 return;
270 }
271
272 if (shouldRetainContent) {
robertphillips714712b2016-08-04 06:20:45 -0700273 if (fDrawContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800274 return;
275 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700276 newDC->copySurface(fDrawContext->asTexture().get(),
277 SkIRect::MakeWH(this->width(), this->height()),
278 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800279 }
280
robertphillipsca6eafc2016-05-17 09:57:46 -0700281 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800282}
283
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284///////////////////////////////////////////////////////////////////////////////
285
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800287 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800288 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800289 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
291 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700292 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700293 return;
294 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000295
robertphillips2e1e51f2015-10-15 08:01:48 -0700296 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000297}
298
299// must be in SkCanvas::PointMode order
300static const GrPrimitiveType gPointMode2PrimtiveType[] = {
301 kPoints_GrPrimitiveType,
302 kLines_GrPrimitiveType,
303 kLineStrip_GrPrimitiveType
304};
305
ethannicholas330bb952015-07-17 06:44:02 -0700306// suppress antialiasing on axis-aligned integer-coordinate lines
307static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
308 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
309 return false;
310 }
311 if (count == 2) {
312 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
313 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800314 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700315 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800316 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700317 // the two end pixels as being the lesser of two evils.
318 if (pts[0].fX == pts[1].fX) {
319 return ((int) pts[0].fX) != pts[0].fX;
320 }
321 if (pts[0].fY == pts[1].fY) {
322 return ((int) pts[0].fY) != pts[0].fY;
323 }
324 }
325 return true;
326}
327
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
329 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800330 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800331 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800332 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000333
334 SkScalar width = paint.getStrokeWidth();
335 if (width < 0) {
336 return;
337 }
338
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000339 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700340 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700341 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700342 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700343 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700344 return;
345 }
egdaniele61c4112014-06-12 10:24:21 -0700346 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700347 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700348 path.moveTo(pts[0]);
349 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700350 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700351 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000352 }
353
ethannicholas330bb952015-07-17 06:44:02 -0700354 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800356 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700357 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358 draw.drawPoints(mode, count, pts, paint, true);
359 return;
360 }
361
362 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700363 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700364 return;
365 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366
robertphillips2e1e51f2015-10-15 08:01:48 -0700367 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700368 grPaint,
369 *draw.fMatrix,
370 gPointMode2PrimtiveType[mode],
371 SkToS32(count),
372 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700373 nullptr,
374 nullptr,
375 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700376 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377}
378
379///////////////////////////////////////////////////////////////////////////////
380
robertphillipsff55b492015-11-24 07:56:59 -0800381void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800382 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800383 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800384 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386
bsalomona7d85ba2016-07-06 11:54:59 -0700387 // A couple reasons we might need to call drawPath.
388 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700390 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700392 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800393 fClip, path, paint,
394 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700395 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000396 return;
397 }
398
399 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700400 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700401 return;
402 }
Mike Klein744fb732014-06-23 15:13:26 -0400403
bsalomon6663acf2016-05-10 09:14:17 -0700404 GrStyle style(paint);
405 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406}
407
408///////////////////////////////////////////////////////////////////////////////
409
robertphillips9aff85a2016-08-05 07:51:29 -0700410void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect,
joshualitt5531d512014-12-17 15:50:11 -0800411 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800412 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800413 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800414 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000416 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700417 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700418 return;
419 }
Mike Klein744fb732014-06-23 15:13:26 -0400420
bsalomon6663acf2016-05-10 09:14:17 -0700421 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000422 if (paint.getMaskFilter()) {
423 // try to hit the fast path for drawing filtered round rects
424
425 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700426 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000427 if (devRRect.allCornersCircular()) {
428 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700429 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700430 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800431 *draw.fMatrix,
432 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000433 SkIRect finalIRect;
434 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700435 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000436 // clipped out
437 return;
438 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700439 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
robertphillips6c7e3252016-04-27 10:47:51 -0700440 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800441 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800442 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800443 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700444 style.strokeRec(),
robertphillips27cdd942016-08-10 16:25:25 -0700445 rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700446 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000447 return;
448 }
449 }
450
451 }
452 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000453 }
454
bsalomon6663acf2016-05-10 09:14:17 -0700455 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800456 // The only mask filter the native rrect drawing code could've handle was taken
457 // care of above.
458 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000459 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700460 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700461 path.addRRect(rrect);
robertphillips6c7e3252016-04-27 10:47:51 -0700462 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800463 fClip, path, paint,
464 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700465 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000466 return;
467 }
Mike Klein744fb732014-06-23 15:13:26 -0400468
bsalomon6663acf2016-05-10 09:14:17 -0700469 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800470
robertphillips9aff85a2016-08-05 07:51:29 -0700471 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000472}
473
robertphillipsd7706102016-02-25 09:28:08 -0800474
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000475void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800476 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800477 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800478 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800479 CHECK_SHOULD_DRAW(draw);
480
robertphillipsd7706102016-02-25 09:28:08 -0800481 if (outer.isEmpty()) {
482 return;
483 }
484
485 if (inner.isEmpty()) {
486 return this->drawRRect(draw, outer, paint);
487 }
488
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000489 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000490
robertphillips0e7029e2015-11-30 05:45:06 -0800491 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800492 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700493 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700494 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700495 return;
496 }
robertphillips00095892016-02-29 13:50:40 -0800497
498 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
499 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000500 }
501
502 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700503 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000504 path.addRRect(outer);
505 path.addRRect(inner);
506 path.setFillType(SkPath::kEvenOdd_FillType);
507
robertphillips6c7e3252016-04-27 10:47:51 -0700508 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800509 fClip, path, paint,
510 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700511 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000512}
513
514
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000515/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000516
robertphillips514450c2015-11-24 05:36:02 -0800517void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800518 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800519 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800520 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000521
robertphillips514450c2015-11-24 05:36:02 -0800522 // Presumably the path effect warps this to something other than an oval
523 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700525 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000526 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700527 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000528 return;
herb11a7f7f2015-11-24 12:41:00 -0800529 }
530
robertphillips514450c2015-11-24 05:36:02 -0800531 if (paint.getMaskFilter()) {
532 // The RRect path can handle special case blurring
533 SkRRect rr = SkRRect::MakeOval(oval);
534 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000535 }
536
537 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700538 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700539 return;
540 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541
bsalomon6663acf2016-05-10 09:14:17 -0700542 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543}
544
545#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546
547///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700548void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
549 const SkDraw& draw,
550 const SkPaint& origPaint) {
551 ASSERT_SINGLE_OWNER
552 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
553 CHECK_SHOULD_DRAW(draw);
554
555 // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix
556 // entry point
557 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
558 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
559 SkASSERT(!origPaint.getPathEffect());
560 SkASSERT(!origPaint.getMaskFilter());
561
562 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
563 SkASSERT(halfWidth > 0);
564
565 SkVector v = points[1] - points[0];
566
567 SkScalar length = SkPoint::Normalize(&v);
568 if (!length) {
569 v.fX = 1.0f;
570 v.fY = 0.0f;
571 }
572
573 SkPaint newPaint(origPaint);
574 newPaint.setStyle(SkPaint::kFill_Style);
575
576 SkScalar xtraLength = 0.0f;
577 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
578 xtraLength = halfWidth;
579 }
580
581 SkPoint mid = points[0] + points[1];
582 mid.scale(0.5f);
583
584 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
585 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
586 SkMatrix m;
587 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
588
589 SkMatrix local = m;
590
591 m.postConcat(*draw.fMatrix);
592
593 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700594 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700595 return;
596 }
597
598 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
599}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
602 const SkPaint& paint, const SkMatrix* prePathMatrix,
603 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800604 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800605 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700606 SkPoint points[2];
607 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
608 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
609 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
610 // Path-based stroking looks better for thin rects
611 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700612 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700613 // Round capping support is currently disabled b.c. it would require
614 // a RRect batch that takes a localMatrix.
615 this->drawStrokedLine(points, draw, paint);
616 return;
617 }
618 }
robertphillipsff55b492015-11-24 07:56:59 -0800619 bool isClosed;
620 SkRect rect;
621 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
622 this->drawRect(draw, rect, paint);
623 return;
624 }
625 if (origSrcPath.isOval(&rect)) {
626 this->drawOval(draw, rect, paint);
627 return;
628 }
629 SkRRect rrect;
630 if (origSrcPath.isRRect(&rrect)) {
631 this->drawRRect(draw, rrect, paint);
632 return;
633 }
634 }
635
joshualitt5531d512014-12-17 15:50:11 -0800636 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800637 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638
robertphillips6c7e3252016-04-27 10:47:51 -0700639 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700640 fClip, origSrcPath, paint,
641 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700642 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643}
644
645static const int kBmpSmallTileSize = 1 << 10;
646
647static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
648 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
649 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
650 return tilesX * tilesY;
651}
652
reed85d91782015-09-10 14:33:38 -0700653static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 if (maxTileSize <= kBmpSmallTileSize) {
655 return maxTileSize;
656 }
657
658 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
659 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
660
661 maxTileTotalTileSize *= maxTileSize * maxTileSize;
662 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
663
664 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
665 return kBmpSmallTileSize;
666 } else {
667 return maxTileSize;
668 }
669}
670
671// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
672// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700673static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800674 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800675 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700676 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700677 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 const SkRect* srcRectPtr,
679 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700680 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700681 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
682 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683 clippedSrcIRect->setEmpty();
684 return;
685 }
686 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
687 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700688 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000689 if (!clippedSrcRect.intersect(*srcRectPtr)) {
690 clippedSrcIRect->setEmpty();
691 return;
692 }
693 }
694 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700695 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696 if (!clippedSrcIRect->intersect(bmpBounds)) {
697 clippedSrcIRect->setEmpty();
698 }
699}
700
reed85d91782015-09-10 14:33:38 -0700701bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
702 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700703 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700704 const GrTextureParams& params,
705 const SkRect* srcRectPtr,
706 int maxTileSize,
707 int* tileSize,
708 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800709 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700710 // if it's larger than the max tile size, then we have no choice but tiling.
711 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
bsalomone553b642016-08-17 09:02:09 -0700712 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix,
713 srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700714 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
715 return true;
716 }
717
bsalomon1a1d0b82015-10-16 07:49:42 -0700718 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700719 const size_t area = imageRect.width() * imageRect.height();
720 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
721 return false;
722 }
723
reed85d91782015-09-10 14:33:38 -0700724 // At this point we know we could do the draw by uploading the entire bitmap
725 // as a texture. However, if the texture would be large compared to the
726 // cache size and we don't require most of it for this draw then tile to
727 // reduce the amount of upload and cache spill.
728
729 // assumption here is that sw bitmap size is a good proxy for its size as
730 // a texture
731 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
732 size_t cacheSize;
733 fContext->getResourceCacheLimits(nullptr, &cacheSize);
734 if (bmpSize < cacheSize / 2) {
735 return false;
736 }
737
bsalomon1a1d0b82015-10-16 07:49:42 -0700738 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
739 // tiling memory savings would be < 50%.
bsalomone553b642016-08-17 09:02:09 -0700740 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix,
741 srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700742 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
743 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
744 kBmpSmallTileSize * kBmpSmallTileSize;
745
746 return usedTileBytes < 2 * bmpSize;
747}
748
reed85d91782015-09-10 14:33:38 -0700749bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
750 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700751 const SkMatrix& viewMatrix,
752 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800753 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700754 // if image is explictly texture backed then just use the texture
755 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756 return false;
757 }
758
reed85d91782015-09-10 14:33:38 -0700759 GrTextureParams params;
760 bool doBicubic;
761 GrTextureParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700762 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700763
764 int tileFilterPad;
765 if (doBicubic) {
766 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
767 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
768 tileFilterPad = 0;
769 } else {
770 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771 }
reed85d91782015-09-10 14:33:38 -0700772 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773
bsalomon8c07b7a2015-11-02 11:36:52 -0800774 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775
reed85d91782015-09-10 14:33:38 -0700776 // these are output, which we safely ignore, as we just want to know the predicate
777 int outTileSize;
778 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779
bsalomone553b642016-08-17 09:02:09 -0700780 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
781 params, srcRectPtr, maxTileSize, &outTileSize,
782 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783}
784
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000785void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 const SkBitmap& bitmap,
787 const SkMatrix& m,
788 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800789 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800790 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800791 SkMatrix viewMatrix;
792 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700793
bsalomonb1b01992015-11-18 10:56:08 -0800794 int maxTileSize = fContext->caps()->maxTileSize();
795
796 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
797 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700798 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800799 paint.isAntiAlias() &&
800 bitmap.width() <= maxTileSize &&
801 bitmap.height() <= maxTileSize;
802
803 bool skipTileCheck = drawAA || paint.getMaskFilter();
804
805 if (!skipTileCheck) {
806 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
807 int tileSize;
808 SkIRect clippedSrcRect;
809
810 GrTextureParams params;
811 bool doBicubic;
812 GrTextureParams::FilterMode textureFilterMode =
813 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
814 &doBicubic);
815
816 int tileFilterPad;
817
818 if (doBicubic) {
819 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
820 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
821 tileFilterPad = 0;
822 } else {
823 tileFilterPad = 1;
824 }
825 params.setFilterMode(textureFilterMode);
826
827 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700828 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
829 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
830 &tileSize, &clippedSrcRect)) {
831 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
832 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
833 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800834 return;
835 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000836 }
bsalomonb1b01992015-11-18 10:56:08 -0800837 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800838 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
839 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840}
841
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000842// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
844// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000845static inline void clamped_outset_with_offset(SkIRect* iRect,
846 int outset,
847 SkPoint* offset,
848 const SkIRect& clamp) {
849 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000850
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000851 int leftClampDelta = clamp.fLeft - iRect->fLeft;
852 if (leftClampDelta > 0) {
853 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854 iRect->fLeft = clamp.fLeft;
855 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000856 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000857 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000858
859 int topClampDelta = clamp.fTop - iRect->fTop;
860 if (topClampDelta > 0) {
861 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000862 iRect->fTop = clamp.fTop;
863 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000864 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865 }
866
867 if (iRect->fRight > clamp.fRight) {
868 iRect->fRight = clamp.fRight;
869 }
870 if (iRect->fBottom > clamp.fBottom) {
871 iRect->fBottom = clamp.fBottom;
872 }
873}
874
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875// Break 'bitmap' into several tiles to draw it since it has already
876// been determined to be too large to fit in VRAM
877void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800878 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700879 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880 const SkRect& srcRect,
881 const SkIRect& clippedSrcIRect,
882 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800883 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700884 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000885 int tileSize,
886 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800887 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800888
ericrk983294f2016-04-18 09:14:00 -0700889 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800890 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700891 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800892
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000893 // The following pixel lock is technically redundant, but it is desirable
894 // to lock outside of the tile loop to prevent redecoding the whole image
895 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
896 // is larger than the limit of the discardable memory pool.
897 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800898
899 const SkPaint* paint = &origPaint;
900 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -0700901 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800902 // Drop antialiasing to avoid seams at tile boundaries.
903 tempPaint = origPaint;
904 tempPaint.setAntiAlias(false);
905 paint = &tempPaint;
906 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
908
909 int nx = bitmap.width() / tileSize;
910 int ny = bitmap.height() / tileSize;
911 for (int x = 0; x <= nx; x++) {
912 for (int y = 0; y <= ny; y++) {
913 SkRect tileR;
914 tileR.set(SkIntToScalar(x * tileSize),
915 SkIntToScalar(y * tileSize),
916 SkIntToScalar((x + 1) * tileSize),
917 SkIntToScalar((y + 1) * tileSize));
918
919 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
920 continue;
921 }
922
923 if (!tileR.intersect(srcRect)) {
924 continue;
925 }
926
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 SkIRect iTileR;
928 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700929 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
930 SkIntToScalar(iTileR.fTop));
931 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
932 tileR.width(), tileR.height());
933 dstMatrix.mapRect(&rectToDraw);
robertphillipsec8bb942014-11-21 10:16:25 -0800934 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000935 SkIRect iClampRect;
936
reeda5517e22015-07-14 10:54:12 -0700937 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938 // In bleed mode we want to always expand the tile on all edges
939 // but stay within the bitmap bounds
940 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
941 } else {
942 // In texture-domain/clamp mode we only want to expand the
943 // tile on edges interior to "srcRect" (i.e., we want to
944 // not bleed across the original clamped edges)
945 srcRect.roundOut(&iClampRect);
946 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000947 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
948 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 }
950
bsalomone553b642016-08-17 09:02:09 -0700951 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 if (bitmap.extractSubset(&tmpB, iTileR)) {
953 // now offset it to make it "local" to our tmp bitmap
954 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000955 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -0800956 // de-optimized this determination
957 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700958 this->drawBitmapTile(tmpB,
959 viewMatrix,
960 rectToDraw,
961 tileR,
962 paramsTemp,
963 *paint,
964 constraint,
965 bicubic,
966 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000967 }
968 }
969 }
970}
971
bsalomone553b642016-08-17 09:02:09 -0700972void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
973 const SkMatrix& viewMatrix,
974 const SkRect& dstRect,
975 const SkRect& srcRect,
976 const GrTextureParams& params,
977 const SkPaint& paint,
978 SkCanvas::SrcRectConstraint constraint,
979 bool bicubic,
980 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800981 // We should have already handled bitmaps larger than the max texture size.
982 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
983 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700984 // We should be respecting the max tile size by the time we get here.
985 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
986 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987
brianosman8fe485b2016-07-25 12:31:51 -0700988 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap, params,
989 fDrawContext->sourceGammaTreatment());
halcanary96fcdcc2015-08-27 07:41:13 -0700990 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 return;
992 }
993
brianosman500bb3e2016-07-22 10:33:07 -0700994 sk_sp<GrColorSpaceXform> colorSpaceXform =
995 GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace());
bsalomone553b642016-08-17 09:02:09 -0700996
997 SkScalar iw = 1.f / texture->width();
998 SkScalar ih = 1.f / texture->height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999
egdaniel79da63f2015-10-09 10:55:16 -07001000 SkMatrix texMatrix;
bsalomone553b642016-08-17 09:02:09 -07001001 // Compute a matrix that maps the rect we will draw to the src rect.
1002 texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kStart_ScaleToFit);
1003 texMatrix.postScale(iw, ih);
joshualitt5f10b5c2015-07-09 10:24:35 -07001004
1005 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1006 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001007 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001008
reeda5517e22015-07-14 10:54:12 -07001009 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001011 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 if (srcRect.width() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001013 domain.fLeft = (srcRect.fLeft + 0.5f) * iw;
1014 domain.fRight = (srcRect.fRight - 0.5f) * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 } else {
bsalomone553b642016-08-17 09:02:09 -07001016 domain.fLeft = domain.fRight = srcRect.centerX() * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 }
1018 if (srcRect.height() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001019 domain.fTop = (srcRect.fTop + 0.5f) * ih;
1020 domain.fBottom = (srcRect.fBottom - 0.5f) * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001021 } else {
bsalomone553b642016-08-17 09:02:09 -07001022 domain.fTop = domain.fBottom = srcRect.centerY() * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001024 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001025 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001026 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001027 } else {
reedc7ec7c92016-07-25 08:29:10 -07001028 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001029 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001030 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001031 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001032 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001033 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1034 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001035 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036 } else {
reedc7ec7c92016-07-25 08:29:10 -07001037 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001038 }
1039
joshualitt33a5fce2015-11-18 13:28:51 -08001040 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001041 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, viewMatrix,
1042 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1043 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001044 return;
1045 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001046
bsalomone553b642016-08-17 09:02:09 -07001047 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048}
1049
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1051 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001052 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001053 CHECK_SHOULD_DRAW(draw);
robertphillips970587b2016-07-14 14:12:55 -07001054 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055
robertphillips970587b2016-07-14 14:12:55 -07001056 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057 return;
1058 }
1059
reedc7ec7c92016-07-25 08:29:10 -07001060 sk_sp<GrTexture> texture;
1061 {
robertphillips970587b2016-07-14 14:12:55 -07001062 SkAutoLockPixels alp(bitmap, true);
1063 if (!bitmap.readyToDraw()) {
1064 return;
1065 }
1066
1067 // draw sprite neither filters nor tiles.
1068 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap,
1069 GrTextureParams::ClampNoFilter(),
1070 SkSourceGammaTreatment::kRespect));
1071 if (!texture) {
1072 return;
1073 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001074 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075
robertphillips970587b2016-07-14 14:12:55 -07001076 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1077 bitmap.pixelRefOrigin().fY,
1078 bitmap.width(),
1079 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001080
robertphillips970587b2016-07-14 14:12:55 -07001081 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1082 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001083 std::move(texture),
1084 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001085 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001086
1087 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1088}
1089
1090
1091void SkGpuDevice::drawSpecial(const SkDraw& draw,
1092 SkSpecialImage* special1,
1093 int left, int top,
1094 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001095 ASSERT_SINGLE_OWNER
1096 CHECK_SHOULD_DRAW(draw);
1097 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext);
robertphillips970587b2016-07-14 14:12:55 -07001098
1099 SkIPoint offset = { 0, 0 };
1100
1101 sk_sp<SkSpecialImage> result;
1102 if (paint.getImageFilter()) {
1103 result = this->filterTexture(draw, special1, left, top,
1104 &offset,
1105 paint.getImageFilter());
1106 if (!result) {
1107 return;
1108 }
1109 } else {
1110 result = sk_ref_sp(special1);
1111 }
1112
1113 SkASSERT(result->isTextureBacked());
1114 sk_sp<GrTexture> texture = result->asTextureRef(fContext);
1115
1116 SkPaint tmpUnfiltered(paint);
1117 tmpUnfiltered.setImageFilter(nullptr);
1118
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001119 GrPaint grPaint;
brianosman54f30c12016-07-18 10:53:52 -07001120 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), nullptr,
1121 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001122 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001123 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001124 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001125 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001126 }
brianosman8fe485b2016-07-25 12:31:51 -07001127 if (!SkPaintToGrPaintReplaceShader(this->context(), fDrawContext.get(), tmpUnfiltered,
1128 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001129 return;
1130 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131
robertphillips970587b2016-07-14 14:12:55 -07001132 const SkIRect& subset = result->subset();
1133
bsalomona2e69fc2015-11-05 10:41:43 -08001134 fDrawContext->fillRectToRect(fClip,
1135 grPaint,
1136 SkMatrix::I(),
robertphillips970587b2016-07-14 14:12:55 -07001137 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY,
1138 subset.width(), subset.height())),
1139 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1140 SkIntToScalar(subset.fTop) / texture->height(),
1141 SkIntToScalar(subset.width()) / texture->width(),
1142 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143}
1144
bsalomonb1b01992015-11-18 10:56:08 -08001145void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1146 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001147 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001148 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001149 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001150
bsalomonb1b01992015-11-18 10:56:08 -08001151 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1152 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1153 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1154 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1155 const SkRect* dst = &origDst;
1156 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001157 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001158 if (!src) {
1159 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001160 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001161
bsalomonb1b01992015-11-18 10:56:08 -08001162 SkMatrix srcToDstMatrix;
1163 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1164 return;
1165 }
1166 SkRect tmpSrc, tmpDst;
1167 if (src != &bmpBounds) {
1168 if (!bmpBounds.contains(*src)) {
1169 tmpSrc = *src;
1170 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 return; // nothing to draw
1172 }
bsalomonb1b01992015-11-18 10:56:08 -08001173 src = &tmpSrc;
1174 srcToDstMatrix.mapRect(&tmpDst, *src);
1175 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 }
1177 }
1178
bsalomonb1b01992015-11-18 10:56:08 -08001179 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001180
bsalomonb1b01992015-11-18 10:56:08 -08001181 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1182 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001183 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001184 paint.isAntiAlias() &&
1185 bitmap.width() <= maxTileSize &&
1186 bitmap.height() <= maxTileSize;
1187
1188 bool skipTileCheck = drawAA || paint.getMaskFilter();
1189
1190 if (!skipTileCheck) {
1191 int tileSize;
1192 SkIRect clippedSrcRect;
1193
1194 GrTextureParams params;
1195 bool doBicubic;
1196 GrTextureParams::FilterMode textureFilterMode =
1197 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1198 &doBicubic);
1199
1200 int tileFilterPad;
1201
1202 if (doBicubic) {
1203 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1204 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1205 tileFilterPad = 0;
1206 } else {
1207 tileFilterPad = 1;
1208 }
1209 params.setFilterMode(textureFilterMode);
1210
1211 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001212 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1213 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1214 &clippedSrcRect)) {
1215 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1216 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001217 return;
1218 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001219 }
bsalomonb1b01992015-11-18 10:56:08 -08001220 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001221 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222}
1223
robertphillips6451a0c2016-07-18 08:31:31 -07001224sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001225 SkAutoLockPixels alp(bitmap, true);
1226 if (!bitmap.readyToDraw()) {
1227 return nullptr;
1228 }
1229
reedc7ec7c92016-07-25 08:29:10 -07001230 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap,
1231 GrTextureParams::ClampNoFilter(),
1232 SkSourceGammaTreatment::kRespect);
robertphillips6451a0c2016-07-18 08:31:31 -07001233 if (!texture) {
1234 return nullptr;
1235 }
1236
1237 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1238 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001239 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001240 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001241 &this->surfaceProps());
1242}
1243
reede51c3562016-07-19 14:33:20 -07001244sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001245 SkPixmap pm;
1246 if (image->isTextureBacked()) {
1247 GrTexture* texture = as_IB(image)->peekTexture();
1248
1249 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1250 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001251 sk_ref_sp(texture),
1252 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001253 &this->surfaceProps());
1254 } else if (image->peekPixels(&pm)) {
1255 SkBitmap bm;
1256
1257 bm.installPixels(pm);
1258 return this->makeSpecial(bm);
1259 } else {
1260 return nullptr;
1261 }
1262}
1263
1264sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
robertphillips1b5f9682016-07-15 08:01:12 -07001265 sk_sp<GrTexture> texture(this->accessDrawContext()->asTexture());
1266 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001267 // When the device doesn't have a texture, we create a temporary texture.
1268 // TODO: we should actually only copy the portion of the source needed to apply the image
1269 // filter
1270 texture.reset(fContext->textureProvider()->createTexture(this->accessDrawContext()->desc(),
1271 SkBudgeted::kYes));
1272 if (!texture) {
1273 return nullptr;
1274 }
1275
reede51c3562016-07-19 14:33:20 -07001276 if (!fContext->copySurface(texture.get(), this->accessDrawContext()->accessRenderTarget())){
robertphillips04d62182016-07-15 12:21:33 -07001277 return nullptr;
1278 }
robertphillips1b5f9682016-07-15 08:01:12 -07001279 }
1280
1281 const SkImageInfo ii = this->imageInfo();
1282 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1283
1284 return SkSpecialImage::MakeFromGpu(srcRect,
1285 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -07001286 std::move(texture),
1287 sk_ref_sp(ii.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001288 &this->surfaceProps());
1289}
1290
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001292 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001293 SkASSERT(!paint.getImageFilter());
1294
joshualittce894002016-01-11 13:29:31 -08001295 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001297 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
kkinnunen2e4414e2015-02-19 07:20:40 -08001298
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001300 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301
robertphillips1b5f9682016-07-15 08:01:12 -07001302 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001303 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001304 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001305 return;
1306 }
1307
robertphillips1b5f9682016-07-15 08:01:12 -07001308 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309}
1310
reeda85d4d02015-05-06 12:56:48 -07001311void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1312 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001313 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001314 SkMatrix viewMatrix = *draw.fMatrix;
1315 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001316 uint32_t pinnedUniqueID;
1317 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001318 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001319 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001320 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001321 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1322 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001323 return;
reed85d91782015-09-10 14:33:38 -07001324 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001325 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001326 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001327 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001328 // only support tiling as bitmap at the moment, so force raster-version
1329 if (!as_IB(image)->getROPixels(&bm)) {
1330 return;
1331 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001332 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1333 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1334 CHECK_SHOULD_DRAW(draw);
1335 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001336 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1337 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001338 } else if (as_IB(image)->getROPixels(&bm)) {
1339 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001340 }
reeda85d4d02015-05-06 12:56:48 -07001341 }
1342}
1343
1344void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001345 const SkRect& dst, const SkPaint& paint,
1346 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001347 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001348 uint32_t pinnedUniqueID;
1349 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001350 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001351 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001352 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001353 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001354 return;
1355 }
1356 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001357 SkMatrix srcToDstRect;
1358 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1359 dst, SkMatrix::kFill_ScaleToFit);
1360 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1361 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001362 // only support tiling as bitmap at the moment, so force raster-version
1363 if (!as_IB(image)->getROPixels(&bm)) {
1364 return;
1365 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001366 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1367 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1368 CHECK_SHOULD_DRAW(draw);
1369 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001370 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001371 } else if (as_IB(image)->getROPixels(&bm)) {
1372 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001373 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001374}
1375
bsalomonf1ecd212015-12-09 17:06:02 -08001376void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001377 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001378 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001379
joshualitt33a5fce2015-11-18 13:28:51 -08001380 CHECK_SHOULD_DRAW(draw);
1381
joshualittedb36442015-11-19 14:29:30 -08001382 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001383 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001384 bool doBicubic;
1385 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001386 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1387 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001388 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001389 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001390
1391 SkRect srcR, dstR;
1392 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001393 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001394 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001395 }
1396 return;
1397 }
1398
bsalomon2bbd0c62015-12-09 12:50:56 -08001399 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001400 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001401 producer->createFragmentProcessor(SkMatrix::I(),
1402 SkRect::MakeIWH(producer->width(), producer->height()),
1403 GrTextureProducer::kNo_FilterConstraint, true,
brianosman8fe485b2016-07-25 12:31:51 -07001404 &kMode, fDrawContext->getColorSpace(),
1405 fDrawContext->sourceGammaTreatment()));
joshualitt33a5fce2015-11-18 13:28:51 -08001406 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001407 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1408 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001409 return;
1410 }
1411
bsalomon2bbd0c62015-12-09 12:50:56 -08001412 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1413 producer->height(), center, dst);
1414}
1415
1416void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1417 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001418 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001419 uint32_t pinnedUniqueID;
1420 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1421 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001422 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001423 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001424 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001425 } else {
1426 SkBitmap bm;
1427 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1428 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001429 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001430 } else if (as_IB(image)->getROPixels(&bm)) {
1431 this->drawBitmapNine(draw, bm, center, dst, paint);
1432 }
1433 }
1434}
1435
1436void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1437 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001438 ASSERT_SINGLE_OWNER
reedc7ec7c92016-07-25 08:29:10 -07001439 GrBitmapTextureMaker maker(fContext, bitmap);
1440 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001441}
1442
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443///////////////////////////////////////////////////////////////////////////////
1444
1445// must be in SkCanvas::VertexMode order
1446static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1447 kTriangles_GrPrimitiveType,
1448 kTriangleStrip_GrPrimitiveType,
1449 kTriangleFan_GrPrimitiveType,
1450};
1451
1452void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1453 int vertexCount, const SkPoint vertices[],
1454 const SkPoint texs[], const SkColor colors[],
1455 SkXfermode* xmode,
1456 const uint16_t indices[], int indexCount,
1457 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001458 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001459 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001460 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001461
halcanary96fcdcc2015-08-27 07:41:13 -07001462 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1463 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001464
halcanary96fcdcc2015-08-27 07:41:13 -07001465 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001466
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001467 SkPaint copy(paint);
1468 copy.setStyle(SkPaint::kStroke_Style);
1469 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001470
bsalomonf1b7a1d2015-09-28 06:26:28 -07001471 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001472 // we ignore the shader if texs is null.
brianosman8fe485b2016-07-25 12:31:51 -07001473 if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001474 return;
1475 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001476
dandov32a311b2014-07-15 19:46:26 -07001477 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001478 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001479 switch (vmode) {
1480 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001481 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001482 break;
1483 case SkCanvas::kTriangleStrip_VertexMode:
1484 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001485 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001486 break;
1487 }
mtklein533eb782014-08-27 10:39:42 -07001488
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001489 VertState state(vertexCount, indices, indexCount);
1490 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001491
dandov32a311b2014-07-15 19:46:26 -07001492 //number of indices for lines per triangle with kLines
1493 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001494
bsalomonf1b7a1d2015-09-28 06:26:28 -07001495 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001496 int i = 0;
1497 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001498 lineIndices[i] = state.f0;
1499 lineIndices[i + 1] = state.f1;
1500 lineIndices[i + 2] = state.f1;
1501 lineIndices[i + 3] = state.f2;
1502 lineIndices[i + 4] = state.f2;
1503 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001504 i += 6;
1505 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001506 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001507 grPaint,
1508 *draw.fMatrix,
1509 kLines_GrPrimitiveType,
1510 vertexCount,
1511 vertices,
1512 texs,
1513 colors,
1514 lineIndices.get(),
1515 indexCount);
1516 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001517 }
1518
bsalomonf1b7a1d2015-09-28 06:26:28 -07001519 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001520
1521 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001522 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001523 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1524 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001525 convertedColors.reset(vertexCount);
1526 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001527 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001528 }
1529 colors = convertedColors.get();
1530 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001531 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001532 if (texs && paint.getShader()) {
1533 if (colors) {
1534 // When there are texs and colors the shader and colors are combined using xmode. A null
1535 // xmode is defined to mean modulate.
1536 SkXfermode::Mode colorMode;
1537 if (xmode) {
1538 if (!xmode->asMode(&colorMode)) {
1539 return;
1540 }
1541 } else {
1542 colorMode = SkXfermode::kModulate_Mode;
1543 }
brianosman8fe485b2016-07-25 12:31:51 -07001544 if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), paint,
1545 *draw.fMatrix, colorMode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001546 return;
1547 }
1548 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001549 // We have a shader, but no colors to blend it against.
brianosman8fe485b2016-07-25 12:31:51 -07001550 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1551 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001552 return;
1553 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001554 }
bsalomonaa48d362015-10-01 08:34:17 -07001555 } else {
1556 if (colors) {
1557 // We have colors, but either have no shader or no texture coords (which implies that
1558 // we should ignore the shader).
brianosman8fe485b2016-07-25 12:31:51 -07001559 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fDrawContext.get(), paint,
1560 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001561 return;
1562 }
1563 } else {
1564 // No colors and no shaders. Just draw with the paint color.
brianosman8fe485b2016-07-25 12:31:51 -07001565 if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001566 return;
1567 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001568 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001569 }
1570
robertphillips2e1e51f2015-10-15 08:01:48 -07001571 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001572 grPaint,
1573 *draw.fMatrix,
1574 primType,
1575 vertexCount,
1576 vertices,
1577 texs,
1578 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001579 indices,
robertphillipsea461502015-05-26 11:38:03 -07001580 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581}
1582
1583///////////////////////////////////////////////////////////////////////////////
1584
jvanverth31ff7622015-08-07 10:09:28 -07001585void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001586 const SkRect texRect[], const SkColor colors[], int count,
1587 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001588 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001589 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001590 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001591 return;
1592 }
1593
jvanverth31ff7622015-08-07 10:09:28 -07001594 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001595 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001596
reedca109532015-06-25 16:25:25 -07001597 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001598 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001599
jvanverth31ff7622015-08-07 10:09:28 -07001600 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001601 if (colors) {
brianosman8fe485b2016-07-25 12:31:51 -07001602 if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), p, *draw.fMatrix,
1603 mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001604 return;
1605 }
1606 } else {
brianosman8fe485b2016-07-25 12:31:51 -07001607 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001608 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001609 }
1610 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001611
1612 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001613 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001614}
1615
1616///////////////////////////////////////////////////////////////////////////////
1617
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001618void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001619 size_t byteLength, SkScalar x, SkScalar y,
1620 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001621 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001622 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001623 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624
jvanverth8c27a182014-10-14 08:45:50 -07001625 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001626 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001627 return;
1628 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001629
jvanverth8c27a182014-10-14 08:45:50 -07001630 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001631
robertphillips2e1e51f2015-10-15 08:01:48 -07001632 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001633 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634}
1635
fmalita05c4a432014-09-29 06:29:53 -07001636void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1637 const SkScalar pos[], int scalarsPerPos,
1638 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001639 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001641 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642
jvanverth8c27a182014-10-14 08:45:50 -07001643 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001644 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001645 return;
1646 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001647
jvanverth8c27a182014-10-14 08:45:50 -07001648 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001649
robertphillips2e1e51f2015-10-15 08:01:48 -07001650 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001651 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001652 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653}
1654
joshualitt9c328182015-03-23 08:13:04 -07001655void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1656 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001657 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001658 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001659 CHECK_SHOULD_DRAW(draw);
1660
1661 SkDEBUGCODE(this->validate();)
1662
robertphillips2e1e51f2015-10-15 08:01:48 -07001663 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001664 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001665}
1666
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667///////////////////////////////////////////////////////////////////////////////
1668
reedb2db8982014-11-13 12:41:02 -08001669bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001670 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001671}
1672
1673void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001674 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001675
robertphillips8c523e02016-07-26 07:41:00 -07001676 fDrawContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001677}
1678
1679///////////////////////////////////////////////////////////////////////////////
1680
reed76033be2015-03-14 10:54:31 -07001681SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001682 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683
robertphillipsca6eafc2016-05-17 09:57:46 -07001684 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685
robertphillipsca6eafc2016-05-17 09:57:46 -07001686 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001687 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001688 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1689 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001690
robertphillips6738c702016-07-27 12:13:51 -07001691 sk_sp<GrDrawContext> dc(fContext->makeDrawContext(fit,
1692 cinfo.fInfo.width(), cinfo.fInfo.height(),
1693 fDrawContext->config(),
1694 sk_ref_sp(fDrawContext->getColorSpace()),
1695 fDrawContext->desc().fSampleCnt,
1696 kDefault_GrSurfaceOrigin,
1697 &props));
robertphillipsca6eafc2016-05-17 09:57:46 -07001698 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001699 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001700 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001701 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001702 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001704
1705 // Skia's convention is to only clear a device if it is non-opaque.
1706 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1707
1708 return SkGpuDevice::Make(std::move(dc),
1709 cinfo.fInfo.width(), cinfo.fInfo.height(),
1710 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711}
1712
reede8f30622016-03-23 18:59:25 -07001713sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001714 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001715 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001716 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001717 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
robertphillips7e922762016-07-26 11:38:17 -07001718 fDrawContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001719}
1720
senorblanco900c3672016-04-27 11:31:23 -07001721SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001722 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001723 // We always return a transient cache, so it is freed after each
1724 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001725 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001726}
reedf037e0b2014-10-30 11:34:15 -07001727
1728#endif