blob: ca84121015a647ee5fd2d331c2d9579f1ba09cf5 [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
reed589a39e2016-08-20 07:59:19 -0700125static SkImageInfo make_info(GrDrawContext* context, int w, int h, bool opaque) {
126 SkColorType colorType;
127 if (!GrPixelConfigToColorType(context->config(), &colorType)) {
128 colorType = kUnknown_SkColorType;
129 }
130 return SkImageInfo::Make(w, h, colorType,
131 opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
132 sk_ref_sp(context->getColorSpace()));
133}
134
135SkGpuDevice::SkGpuDevice(sk_sp<GrDrawContext> drawContext, int width, int height, unsigned flags)
136 : INHERITED(make_info(drawContext.get(), width, height, SkToBool(flags & kIsOpaque_Flag)),
137 drawContext->surfaceProps())
robertphillipsca6eafc2016-05-17 09:57:46 -0700138 , fContext(SkRef(drawContext->accessRenderTarget()->getContext()))
reed589a39e2016-08-20 07:59:19 -0700139 , fDrawContext(std::move(drawContext))
140{
robertphillips1f3923e2016-07-21 07:17:54 -0700141 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700142 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143
bsalomone63ffef2016-02-05 07:17:34 -0800144 if (flags & kNeedClear_Flag) {
145 this->clearAll();
146 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147}
148
robertphillips7e922762016-07-26 11:38:17 -0700149sk_sp<GrDrawContext> SkGpuDevice::MakeDrawContext(GrContext* context,
150 SkBudgeted budgeted,
151 const SkImageInfo& origInfo,
152 int sampleCount,
153 GrSurfaceOrigin origin,
154 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000155 if (kUnknown_SkColorType == origInfo.colorType() ||
156 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000158 }
159
bsalomonafe30052015-01-16 07:32:33 -0800160 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700161 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800162 }
163
reede5ea5002014-09-03 11:54:58 -0700164 SkColorType ct = origInfo.colorType();
165 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700166 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700167 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700168 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800169 }
170 if (kOpaque_SkAlphaType != at) {
171 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000172 }
brianosman6b086522016-04-14 12:39:00 -0700173
brianosmanb109b8c2016-06-16 13:03:24 -0700174 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175
robertphillips6738c702016-07-27 12:13:51 -0700176 return context->makeDrawContext(SkBackingFit::kExact, // Why exact?
177 origInfo.width(), origInfo.height(),
178 config, sk_ref_sp(cs), sampleCount,
179 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800180}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000181
robertphillips970587b2016-07-14 14:12:55 -0700182sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
183 SkSpecialImage* srcImg,
184 int left, int top,
185 SkIPoint* offset,
186 const SkImageFilter* filter) {
187 SkASSERT(srcImg->isTextureBacked());
188 SkASSERT(filter);
189
190 SkMatrix matrix = *draw.fMatrix;
191 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
192 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
193 SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
194 SkImageFilter::Context ctx(matrix, clipBounds, cache.get());
195
196 return filter->filterImage(srcImg, ctx, offset);
197}
198
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199///////////////////////////////////////////////////////////////////////////////
200
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000201bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
202 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800203 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000204
robertphillips1da3ecd2016-08-31 14:54:15 -0700205 return fDrawContext->readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000206}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207
robertphillips1da3ecd2016-08-31 14:54:15 -0700208bool SkGpuDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
209 size_t srcRowBytes, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800210 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700211
212 return fDrawContext->writePixels(srcInfo, srcPixels, srcRowBytes, x, y);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000213}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214
reed41e010c2015-06-09 12:16:53 -0700215bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800216 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700217 return false;
218}
219
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220// call this every draw call, to ensure that the context reflects our state,
221// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800222void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800223 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000224
reed2c9e2002016-07-25 08:05:22 -0700225 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000226}
227
robertphillips175dd9b2016-04-28 14:32:04 -0700228GrDrawContext* SkGpuDevice::accessDrawContext() {
229 ASSERT_SINGLE_OWNER
230 return fDrawContext.get();
231}
232
reed8eddfb52014-12-04 07:50:14 -0800233void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800234 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800235 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800236 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800237 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700238 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800239}
240
robertphillipsca6eafc2016-05-17 09:57:46 -0700241void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800242 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800243
robertphillips714712b2016-08-04 06:20:45 -0700244 SkBudgeted budgeted = fDrawContext->drawContextPriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800245
robertphillips7e922762016-07-26 11:38:17 -0700246 sk_sp<GrDrawContext> newDC(MakeDrawContext(this->context(),
247 budgeted,
248 this->imageInfo(),
249 fDrawContext->numColorSamples(),
250 fDrawContext->origin(),
251 &this->surfaceProps()));
robertphillipsca6eafc2016-05-17 09:57:46 -0700252 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800253 return;
254 }
255
256 if (shouldRetainContent) {
robertphillips714712b2016-08-04 06:20:45 -0700257 if (fDrawContext->wasAbandoned()) {
kkinnunenabcfab42015-02-22 22:53:44 -0800258 return;
259 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700260 newDC->copySurface(fDrawContext->asTexture().get(),
261 SkIRect::MakeWH(this->width(), this->height()),
262 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800263 }
264
robertphillipsca6eafc2016-05-17 09:57:46 -0700265 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800266}
267
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268///////////////////////////////////////////////////////////////////////////////
269
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800271 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800272 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800273 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274
275 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700276 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700277 return;
278 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279
robertphillips2e1e51f2015-10-15 08:01:48 -0700280 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281}
282
283// must be in SkCanvas::PointMode order
284static const GrPrimitiveType gPointMode2PrimtiveType[] = {
285 kPoints_GrPrimitiveType,
286 kLines_GrPrimitiveType,
287 kLineStrip_GrPrimitiveType
288};
289
ethannicholas330bb952015-07-17 06:44:02 -0700290// suppress antialiasing on axis-aligned integer-coordinate lines
291static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
292 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
293 return false;
294 }
295 if (count == 2) {
296 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
297 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800298 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700299 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800300 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700301 // the two end pixels as being the lesser of two evils.
302 if (pts[0].fX == pts[1].fX) {
303 return ((int) pts[0].fX) != pts[0].fX;
304 }
305 if (pts[0].fY == pts[1].fY) {
306 return ((int) pts[0].fY) != pts[0].fY;
307 }
308 }
309 return true;
310}
311
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000312void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
313 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800314 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800315 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800316 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317
318 SkScalar width = paint.getStrokeWidth();
319 if (width < 0) {
320 return;
321 }
322
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000323 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700324 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700325 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700326 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700327 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700328 return;
329 }
egdaniele61c4112014-06-12 10:24:21 -0700330 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700331 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700332 path.moveTo(pts[0]);
333 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700334 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700335 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000336 }
337
ethannicholas330bb952015-07-17 06:44:02 -0700338 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000339 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800340 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700341 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000342 draw.drawPoints(mode, count, pts, paint, true);
343 return;
344 }
345
346 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700347 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700348 return;
349 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000350
robertphillips2e1e51f2015-10-15 08:01:48 -0700351 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700352 grPaint,
353 *draw.fMatrix,
354 gPointMode2PrimtiveType[mode],
355 SkToS32(count),
356 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700357 nullptr,
358 nullptr,
359 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700360 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361}
362
363///////////////////////////////////////////////////////////////////////////////
364
robertphillipsff55b492015-11-24 07:56:59 -0800365void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800366 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800367 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800368 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000370
bsalomona7d85ba2016-07-06 11:54:59 -0700371 // A couple reasons we might need to call drawPath.
372 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700374 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000375 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700376 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800377 fClip, path, paint,
378 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700379 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380 return;
381 }
382
383 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700384 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700385 return;
386 }
Mike Klein744fb732014-06-23 15:13:26 -0400387
bsalomon6663acf2016-05-10 09:14:17 -0700388 GrStyle style(paint);
389 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390}
391
392///////////////////////////////////////////////////////////////////////////////
393
robertphillips9aff85a2016-08-05 07:51:29 -0700394void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect,
joshualitt5531d512014-12-17 15:50:11 -0800395 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800396 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800397 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800398 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000399
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000400 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700401 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700402 return;
403 }
Mike Klein744fb732014-06-23 15:13:26 -0400404
bsalomon6663acf2016-05-10 09:14:17 -0700405 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000406 if (paint.getMaskFilter()) {
407 // try to hit the fast path for drawing filtered round rects
408
409 SkRRect devRRect;
robertphillips9aff85a2016-08-05 07:51:29 -0700410 if (rrect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000411 if (devRRect.allCornersCircular()) {
412 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700413 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700414 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800415 *draw.fMatrix,
416 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000417 SkIRect finalIRect;
418 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700419 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000420 // clipped out
421 return;
422 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700423 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
robertphillips6c7e3252016-04-27 10:47:51 -0700424 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800425 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800426 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800427 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700428 style.strokeRec(),
robertphillips27cdd942016-08-10 16:25:25 -0700429 rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700430 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000431 return;
432 }
433 }
434
435 }
436 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000437 }
438
bsalomon6663acf2016-05-10 09:14:17 -0700439 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800440 // The only mask filter the native rrect drawing code could've handle was taken
441 // care of above.
442 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700444 path.setIsVolatile(true);
robertphillips9aff85a2016-08-05 07:51:29 -0700445 path.addRRect(rrect);
robertphillips6c7e3252016-04-27 10:47:51 -0700446 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800447 fClip, path, paint,
448 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700449 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450 return;
451 }
Mike Klein744fb732014-06-23 15:13:26 -0400452
bsalomon6663acf2016-05-10 09:14:17 -0700453 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800454
robertphillips9aff85a2016-08-05 07:51:29 -0700455 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456}
457
robertphillipsd7706102016-02-25 09:28:08 -0800458
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000459void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800460 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800461 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800462 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800463 CHECK_SHOULD_DRAW(draw);
464
robertphillipsd7706102016-02-25 09:28:08 -0800465 if (outer.isEmpty()) {
466 return;
467 }
468
469 if (inner.isEmpty()) {
470 return this->drawRRect(draw, outer, paint);
471 }
472
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000473 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000474
robertphillips0e7029e2015-11-30 05:45:06 -0800475 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800476 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700477 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700478 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700479 return;
480 }
robertphillips00095892016-02-29 13:50:40 -0800481
482 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
483 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000484 }
485
486 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700487 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000488 path.addRRect(outer);
489 path.addRRect(inner);
490 path.setFillType(SkPath::kEvenOdd_FillType);
491
robertphillips6c7e3252016-04-27 10:47:51 -0700492 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800493 fClip, path, paint,
494 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700495 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000496}
497
498
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000499/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000500
msarettcc319b92016-08-25 18:07:18 -0700501void SkGpuDevice::drawRegion(const SkDraw& draw, const SkRegion& region, const SkPaint& paint) {
502 if (paint.getMaskFilter()) {
503 SkPath path;
504 region.getBoundaryPath(&path);
505 return this->drawPath(draw, path, paint, nullptr, false);
506 }
507
508 GrPaint grPaint;
509 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
510 return;
511 }
512
513 fDrawContext->drawRegion(fClip, grPaint, *draw.fMatrix, region, GrStyle(paint));
514}
515
robertphillips514450c2015-11-24 05:36:02 -0800516void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800517 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800518 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800519 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520
robertphillips514450c2015-11-24 05:36:02 -0800521 // Presumably the path effect warps this to something other than an oval
522 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000523 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700524 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700526 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527 return;
herb11a7f7f2015-11-24 12:41:00 -0800528 }
529
robertphillips514450c2015-11-24 05:36:02 -0800530 if (paint.getMaskFilter()) {
531 // The RRect path can handle special case blurring
532 SkRRect rr = SkRRect::MakeOval(oval);
533 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534 }
535
536 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700537 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700538 return;
539 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540
bsalomon6663acf2016-05-10 09:14:17 -0700541 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542}
543
bsalomon4f3a0ca2016-08-22 13:14:26 -0700544void SkGpuDevice::drawArc(const SkDraw& draw, const SkRect& oval, SkScalar startAngle,
545 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
546 ASSERT_SINGLE_OWNER
547 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext);
548 CHECK_SHOULD_DRAW(draw);
549
550 if (paint.getMaskFilter()) {
551 this->INHERITED::drawArc(draw, oval, startAngle, sweepAngle, useCenter, paint);
552 return;
553 }
554 GrPaint grPaint;
555 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
556 return;
557 }
558
559 fDrawContext->drawArc(fClip, grPaint, *draw.fMatrix, oval, startAngle, sweepAngle, useCenter,
560 GrStyle(paint));
561}
562
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564
565///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700566void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
567 const SkDraw& draw,
568 const SkPaint& origPaint) {
569 ASSERT_SINGLE_OWNER
570 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
571 CHECK_SHOULD_DRAW(draw);
572
573 // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix
574 // entry point
575 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
576 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
577 SkASSERT(!origPaint.getPathEffect());
578 SkASSERT(!origPaint.getMaskFilter());
579
580 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
581 SkASSERT(halfWidth > 0);
582
583 SkVector v = points[1] - points[0];
584
585 SkScalar length = SkPoint::Normalize(&v);
586 if (!length) {
587 v.fX = 1.0f;
588 v.fY = 0.0f;
589 }
590
591 SkPaint newPaint(origPaint);
592 newPaint.setStyle(SkPaint::kFill_Style);
593
594 SkScalar xtraLength = 0.0f;
595 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
596 xtraLength = halfWidth;
597 }
598
599 SkPoint mid = points[0] + points[1];
600 mid.scale(0.5f);
601
602 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
603 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
604 SkMatrix m;
605 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
606
607 SkMatrix local = m;
608
609 m.postConcat(*draw.fMatrix);
610
611 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700612 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700613 return;
614 }
615
616 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
617}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000619void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
620 const SkPaint& paint, const SkMatrix* prePathMatrix,
621 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800622 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800623 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700624 SkPoint points[2];
625 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
626 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
627 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
628 // Path-based stroking looks better for thin rects
629 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700630 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700631 // Round capping support is currently disabled b.c. it would require
632 // a RRect batch that takes a localMatrix.
633 this->drawStrokedLine(points, draw, paint);
634 return;
635 }
636 }
robertphillipsff55b492015-11-24 07:56:59 -0800637 bool isClosed;
638 SkRect rect;
639 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
640 this->drawRect(draw, rect, paint);
641 return;
642 }
643 if (origSrcPath.isOval(&rect)) {
644 this->drawOval(draw, rect, paint);
645 return;
646 }
647 SkRRect rrect;
648 if (origSrcPath.isRRect(&rrect)) {
649 this->drawRRect(draw, rrect, paint);
650 return;
651 }
652 }
653
joshualitt5531d512014-12-17 15:50:11 -0800654 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800655 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656
robertphillips6c7e3252016-04-27 10:47:51 -0700657 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700658 fClip, origSrcPath, paint,
659 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700660 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661}
662
663static const int kBmpSmallTileSize = 1 << 10;
664
665static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
666 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
667 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
668 return tilesX * tilesY;
669}
670
reed85d91782015-09-10 14:33:38 -0700671static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 if (maxTileSize <= kBmpSmallTileSize) {
673 return maxTileSize;
674 }
675
676 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
677 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
678
679 maxTileTotalTileSize *= maxTileSize * maxTileSize;
680 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
681
682 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
683 return kBmpSmallTileSize;
684 } else {
685 return maxTileSize;
686 }
687}
688
689// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
690// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700691static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800692 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800693 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700694 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700695 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696 const SkRect* srcRectPtr,
697 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700698 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700699 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
700 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701 clippedSrcIRect->setEmpty();
702 return;
703 }
704 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
705 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700706 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707 if (!clippedSrcRect.intersect(*srcRectPtr)) {
708 clippedSrcIRect->setEmpty();
709 return;
710 }
711 }
712 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700713 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 if (!clippedSrcIRect->intersect(bmpBounds)) {
715 clippedSrcIRect->setEmpty();
716 }
717}
718
reed85d91782015-09-10 14:33:38 -0700719bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
720 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700721 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700722 const GrTextureParams& params,
723 const SkRect* srcRectPtr,
724 int maxTileSize,
725 int* tileSize,
726 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800727 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700728 // if it's larger than the max tile size, then we have no choice but tiling.
729 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
bsalomone553b642016-08-17 09:02:09 -0700730 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix,
731 srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700732 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
733 return true;
734 }
735
bsalomon1a1d0b82015-10-16 07:49:42 -0700736 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700737 const size_t area = imageRect.width() * imageRect.height();
738 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
739 return false;
740 }
741
reed85d91782015-09-10 14:33:38 -0700742 // At this point we know we could do the draw by uploading the entire bitmap
743 // as a texture. However, if the texture would be large compared to the
744 // cache size and we don't require most of it for this draw then tile to
745 // reduce the amount of upload and cache spill.
746
747 // assumption here is that sw bitmap size is a good proxy for its size as
748 // a texture
749 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
750 size_t cacheSize;
751 fContext->getResourceCacheLimits(nullptr, &cacheSize);
752 if (bmpSize < cacheSize / 2) {
753 return false;
754 }
755
bsalomon1a1d0b82015-10-16 07:49:42 -0700756 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
757 // tiling memory savings would be < 50%.
bsalomone553b642016-08-17 09:02:09 -0700758 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix,
759 srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700760 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
761 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
762 kBmpSmallTileSize * kBmpSmallTileSize;
763
764 return usedTileBytes < 2 * bmpSize;
765}
766
reed85d91782015-09-10 14:33:38 -0700767bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
768 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700769 const SkMatrix& viewMatrix,
770 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800771 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700772 // if image is explictly texture backed then just use the texture
773 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774 return false;
775 }
776
reed85d91782015-09-10 14:33:38 -0700777 GrTextureParams params;
778 bool doBicubic;
779 GrTextureParams::FilterMode textureFilterMode =
bsalomone553b642016-08-17 09:02:09 -0700780 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700781
782 int tileFilterPad;
783 if (doBicubic) {
784 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
785 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
786 tileFilterPad = 0;
787 } else {
788 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789 }
reed85d91782015-09-10 14:33:38 -0700790 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791
bsalomon8c07b7a2015-11-02 11:36:52 -0800792 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793
reed85d91782015-09-10 14:33:38 -0700794 // these are output, which we safely ignore, as we just want to know the predicate
795 int outTileSize;
796 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797
bsalomone553b642016-08-17 09:02:09 -0700798 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
799 params, srcRectPtr, maxTileSize, &outTileSize,
800 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801}
802
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000803void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000804 const SkBitmap& bitmap,
805 const SkMatrix& m,
806 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800807 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800808 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800809 SkMatrix viewMatrix;
810 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700811
bsalomonb1b01992015-11-18 10:56:08 -0800812 int maxTileSize = fContext->caps()->maxTileSize();
813
814 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
815 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700816 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800817 paint.isAntiAlias() &&
818 bitmap.width() <= maxTileSize &&
819 bitmap.height() <= maxTileSize;
820
821 bool skipTileCheck = drawAA || paint.getMaskFilter();
822
823 if (!skipTileCheck) {
824 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
825 int tileSize;
826 SkIRect clippedSrcRect;
827
828 GrTextureParams params;
829 bool doBicubic;
830 GrTextureParams::FilterMode textureFilterMode =
831 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
832 &doBicubic);
833
834 int tileFilterPad;
835
836 if (doBicubic) {
837 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
838 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
839 tileFilterPad = 0;
840 } else {
841 tileFilterPad = 1;
842 }
843 params.setFilterMode(textureFilterMode);
844
845 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -0700846 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
847 SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
848 &tileSize, &clippedSrcRect)) {
849 this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
850 params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
851 doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -0800852 return;
853 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000854 }
bsalomonb1b01992015-11-18 10:56:08 -0800855 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800856 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
857 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000858}
859
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000860// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
862// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000863static inline void clamped_outset_with_offset(SkIRect* iRect,
864 int outset,
865 SkPoint* offset,
866 const SkIRect& clamp) {
867 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000868
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000869 int leftClampDelta = clamp.fLeft - iRect->fLeft;
870 if (leftClampDelta > 0) {
871 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872 iRect->fLeft = clamp.fLeft;
873 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000874 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000876
877 int topClampDelta = clamp.fTop - iRect->fTop;
878 if (topClampDelta > 0) {
879 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880 iRect->fTop = clamp.fTop;
881 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000882 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883 }
884
885 if (iRect->fRight > clamp.fRight) {
886 iRect->fRight = clamp.fRight;
887 }
888 if (iRect->fBottom > clamp.fBottom) {
889 iRect->fBottom = clamp.fBottom;
890 }
891}
892
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000893// Break 'bitmap' into several tiles to draw it since it has already
894// been determined to be too large to fit in VRAM
895void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800896 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700897 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 const SkRect& srcRect,
899 const SkIRect& clippedSrcIRect,
900 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800901 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700902 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000903 int tileSize,
904 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800905 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800906
ericrk983294f2016-04-18 09:14:00 -0700907 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800908 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700909 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800910
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000911 // The following pixel lock is technically redundant, but it is desirable
912 // to lock outside of the tile loop to prevent redecoding the whole image
913 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
914 // is larger than the limit of the discardable memory pool.
915 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800916
917 const SkPaint* paint = &origPaint;
918 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -0700919 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800920 // Drop antialiasing to avoid seams at tile boundaries.
921 tempPaint = origPaint;
922 tempPaint.setAntiAlias(false);
923 paint = &tempPaint;
924 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
926
927 int nx = bitmap.width() / tileSize;
928 int ny = bitmap.height() / tileSize;
929 for (int x = 0; x <= nx; x++) {
930 for (int y = 0; y <= ny; y++) {
931 SkRect tileR;
932 tileR.set(SkIntToScalar(x * tileSize),
933 SkIntToScalar(y * tileSize),
934 SkIntToScalar((x + 1) * tileSize),
935 SkIntToScalar((y + 1) * tileSize));
936
937 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
938 continue;
939 }
940
941 if (!tileR.intersect(srcRect)) {
942 continue;
943 }
944
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000945 SkIRect iTileR;
946 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700947 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
948 SkIntToScalar(iTileR.fTop));
949 SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
950 tileR.width(), tileR.height());
951 dstMatrix.mapRect(&rectToDraw);
robertphillipsec8bb942014-11-21 10:16:25 -0800952 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 SkIRect iClampRect;
954
reeda5517e22015-07-14 10:54:12 -0700955 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 // In bleed mode we want to always expand the tile on all edges
957 // but stay within the bitmap bounds
958 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
959 } else {
960 // In texture-domain/clamp mode we only want to expand the
961 // tile on edges interior to "srcRect" (i.e., we want to
962 // not bleed across the original clamped edges)
963 srcRect.roundOut(&iClampRect);
964 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000965 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
966 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000967 }
968
bsalomone553b642016-08-17 09:02:09 -0700969 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970 if (bitmap.extractSubset(&tmpB, iTileR)) {
971 // now offset it to make it "local" to our tmp bitmap
972 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000973 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -0800974 // de-optimized this determination
975 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700976 this->drawBitmapTile(tmpB,
977 viewMatrix,
978 rectToDraw,
979 tileR,
980 paramsTemp,
981 *paint,
982 constraint,
983 bicubic,
984 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 }
986 }
987 }
988}
989
bsalomone553b642016-08-17 09:02:09 -0700990void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
991 const SkMatrix& viewMatrix,
992 const SkRect& dstRect,
993 const SkRect& srcRect,
994 const GrTextureParams& params,
995 const SkPaint& paint,
996 SkCanvas::SrcRectConstraint constraint,
997 bool bicubic,
998 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800999 // We should have already handled bitmaps larger than the max texture size.
1000 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1001 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001002 // We should be respecting the max tile size by the time we get here.
1003 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1004 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001005
brianosman8fe485b2016-07-25 12:31:51 -07001006 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap, params,
1007 fDrawContext->sourceGammaTreatment());
halcanary96fcdcc2015-08-27 07:41:13 -07001008 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 return;
1010 }
bsalomon5eb41fd2016-09-06 13:49:32 -07001011
brianosman500bb3e2016-07-22 10:33:07 -07001012 sk_sp<GrColorSpaceXform> colorSpaceXform =
brianosman77320db2016-09-07 08:09:10 -07001013 GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace(),
1014 bitmap.alphaType());
bsalomone553b642016-08-17 09:02:09 -07001015
1016 SkScalar iw = 1.f / texture->width();
1017 SkScalar ih = 1.f / texture->height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001018
egdaniel79da63f2015-10-09 10:55:16 -07001019 SkMatrix texMatrix;
bsalomone553b642016-08-17 09:02:09 -07001020 // Compute a matrix that maps the rect we will draw to the src rect.
1021 texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kStart_ScaleToFit);
1022 texMatrix.postScale(iw, ih);
joshualitt5f10b5c2015-07-09 10:24:35 -07001023
1024 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1025 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001026 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001027
reeda5517e22015-07-14 10:54:12 -07001028 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -07001030 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031 if (srcRect.width() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001032 domain.fLeft = (srcRect.fLeft + 0.5f) * iw;
1033 domain.fRight = (srcRect.fRight - 0.5f) * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 } else {
bsalomone553b642016-08-17 09:02:09 -07001035 domain.fLeft = domain.fRight = srcRect.centerX() * iw;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036 }
1037 if (srcRect.height() > SK_Scalar1) {
bsalomone553b642016-08-17 09:02:09 -07001038 domain.fTop = (srcRect.fTop + 0.5f) * ih;
1039 domain.fBottom = (srcRect.fBottom - 0.5f) * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040 } else {
bsalomone553b642016-08-17 09:02:09 -07001041 domain.fTop = domain.fBottom = srcRect.centerY() * ih;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001043 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001044 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001045 domain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001046 } else {
reedc7ec7c92016-07-25 08:29:10 -07001047 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
bsalomone553b642016-08-17 09:02:09 -07001048 domain, GrTextureDomain::kClamp_Mode,
brianosman54f30c12016-07-18 10:53:52 -07001049 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001050 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001051 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001052 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1053 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001054 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055 } else {
reedc7ec7c92016-07-25 08:29:10 -07001056 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057 }
1058
joshualitt33a5fce2015-11-18 13:28:51 -08001059 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001060 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, viewMatrix,
1061 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1062 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001063 return;
1064 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065
bsalomone553b642016-08-17 09:02:09 -07001066 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067}
1068
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1070 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001071 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001072 CHECK_SHOULD_DRAW(draw);
robertphillips970587b2016-07-14 14:12:55 -07001073 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074
robertphillips970587b2016-07-14 14:12:55 -07001075 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 return;
1077 }
1078
reedc7ec7c92016-07-25 08:29:10 -07001079 sk_sp<GrTexture> texture;
1080 {
robertphillips970587b2016-07-14 14:12:55 -07001081 SkAutoLockPixels alp(bitmap, true);
1082 if (!bitmap.readyToDraw()) {
1083 return;
1084 }
1085
1086 // draw sprite neither filters nor tiles.
1087 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap,
1088 GrTextureParams::ClampNoFilter(),
1089 SkSourceGammaTreatment::kRespect));
1090 if (!texture) {
1091 return;
1092 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001093 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094
robertphillips970587b2016-07-14 14:12:55 -07001095 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1096 bitmap.pixelRefOrigin().fY,
1097 bitmap.width(),
1098 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001099
robertphillips970587b2016-07-14 14:12:55 -07001100 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1101 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001102 std::move(texture),
1103 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001104 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001105
1106 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1107}
1108
1109
1110void SkGpuDevice::drawSpecial(const SkDraw& draw,
1111 SkSpecialImage* special1,
1112 int left, int top,
1113 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001114 ASSERT_SINGLE_OWNER
1115 CHECK_SHOULD_DRAW(draw);
1116 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext);
robertphillips970587b2016-07-14 14:12:55 -07001117
1118 SkIPoint offset = { 0, 0 };
1119
1120 sk_sp<SkSpecialImage> result;
1121 if (paint.getImageFilter()) {
1122 result = this->filterTexture(draw, special1, left, top,
1123 &offset,
1124 paint.getImageFilter());
1125 if (!result) {
1126 return;
1127 }
1128 } else {
1129 result = sk_ref_sp(special1);
1130 }
1131
1132 SkASSERT(result->isTextureBacked());
1133 sk_sp<GrTexture> texture = result->asTextureRef(fContext);
1134
1135 SkPaint tmpUnfiltered(paint);
1136 tmpUnfiltered.setImageFilter(nullptr);
1137
brianosman77320db2016-09-07 08:09:10 -07001138 sk_sp<GrColorSpaceXform> colorSpaceXform =
1139 GrColorSpaceXform::Make(result->getColorSpace(), fDrawContext->getColorSpace(),
1140 result->alphaType());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 GrPaint grPaint;
brianosman77320db2016-09-07 08:09:10 -07001142 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(),
1143 std::move(colorSpaceXform),
brianosman54f30c12016-07-18 10:53:52 -07001144 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001145 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001146 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001147 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001148 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001149 }
brianosman8fe485b2016-07-25 12:31:51 -07001150 if (!SkPaintToGrPaintReplaceShader(this->context(), fDrawContext.get(), tmpUnfiltered,
1151 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001152 return;
1153 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154
robertphillips970587b2016-07-14 14:12:55 -07001155 const SkIRect& subset = result->subset();
1156
bsalomona2e69fc2015-11-05 10:41:43 -08001157 fDrawContext->fillRectToRect(fClip,
1158 grPaint,
1159 SkMatrix::I(),
robertphillips970587b2016-07-14 14:12:55 -07001160 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY,
1161 subset.width(), subset.height())),
1162 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1163 SkIntToScalar(subset.fTop) / texture->height(),
1164 SkIntToScalar(subset.width()) / texture->width(),
1165 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166}
1167
bsalomonb1b01992015-11-18 10:56:08 -08001168void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1169 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001170 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001171 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001172 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001173
bsalomonb1b01992015-11-18 10:56:08 -08001174 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1175 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1176 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1177 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1178 const SkRect* dst = &origDst;
1179 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001181 if (!src) {
1182 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001184
bsalomonb1b01992015-11-18 10:56:08 -08001185 SkMatrix srcToDstMatrix;
1186 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1187 return;
1188 }
1189 SkRect tmpSrc, tmpDst;
1190 if (src != &bmpBounds) {
1191 if (!bmpBounds.contains(*src)) {
1192 tmpSrc = *src;
1193 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 return; // nothing to draw
1195 }
bsalomonb1b01992015-11-18 10:56:08 -08001196 src = &tmpSrc;
1197 srcToDstMatrix.mapRect(&tmpDst, *src);
1198 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 }
1200 }
1201
bsalomonb1b01992015-11-18 10:56:08 -08001202 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001203
bsalomonb1b01992015-11-18 10:56:08 -08001204 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1205 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001206 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001207 paint.isAntiAlias() &&
1208 bitmap.width() <= maxTileSize &&
1209 bitmap.height() <= maxTileSize;
1210
1211 bool skipTileCheck = drawAA || paint.getMaskFilter();
1212
1213 if (!skipTileCheck) {
1214 int tileSize;
1215 SkIRect clippedSrcRect;
1216
1217 GrTextureParams params;
1218 bool doBicubic;
1219 GrTextureParams::FilterMode textureFilterMode =
1220 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1221 &doBicubic);
1222
1223 int tileFilterPad;
1224
1225 if (doBicubic) {
1226 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1227 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1228 tileFilterPad = 0;
1229 } else {
1230 tileFilterPad = 1;
1231 }
1232 params.setFilterMode(textureFilterMode);
1233
1234 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
bsalomone553b642016-08-17 09:02:09 -07001235 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
1236 srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
1237 &clippedSrcRect)) {
1238 this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
1239 params, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001240 return;
1241 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001242 }
bsalomonb1b01992015-11-18 10:56:08 -08001243 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001244 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001245}
1246
robertphillips6451a0c2016-07-18 08:31:31 -07001247sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001248 SkAutoLockPixels alp(bitmap, true);
1249 if (!bitmap.readyToDraw()) {
1250 return nullptr;
1251 }
1252
reedc7ec7c92016-07-25 08:29:10 -07001253 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap,
1254 GrTextureParams::ClampNoFilter(),
1255 SkSourceGammaTreatment::kRespect);
robertphillips6451a0c2016-07-18 08:31:31 -07001256 if (!texture) {
1257 return nullptr;
1258 }
1259
1260 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1261 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001262 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001263 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001264 &this->surfaceProps());
1265}
1266
reede51c3562016-07-19 14:33:20 -07001267sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001268 SkPixmap pm;
1269 if (image->isTextureBacked()) {
1270 GrTexture* texture = as_IB(image)->peekTexture();
1271
1272 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1273 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001274 sk_ref_sp(texture),
1275 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001276 &this->surfaceProps());
1277 } else if (image->peekPixels(&pm)) {
1278 SkBitmap bm;
1279
1280 bm.installPixels(pm);
1281 return this->makeSpecial(bm);
1282 } else {
1283 return nullptr;
1284 }
1285}
1286
1287sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
robertphillips1b5f9682016-07-15 08:01:12 -07001288 sk_sp<GrTexture> texture(this->accessDrawContext()->asTexture());
1289 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001290 // When the device doesn't have a texture, we create a temporary texture.
1291 // TODO: we should actually only copy the portion of the source needed to apply the image
1292 // filter
1293 texture.reset(fContext->textureProvider()->createTexture(this->accessDrawContext()->desc(),
1294 SkBudgeted::kYes));
1295 if (!texture) {
1296 return nullptr;
1297 }
1298
reede51c3562016-07-19 14:33:20 -07001299 if (!fContext->copySurface(texture.get(), this->accessDrawContext()->accessRenderTarget())){
robertphillips04d62182016-07-15 12:21:33 -07001300 return nullptr;
1301 }
robertphillips1b5f9682016-07-15 08:01:12 -07001302 }
1303
1304 const SkImageInfo ii = this->imageInfo();
1305 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1306
1307 return SkSpecialImage::MakeFromGpu(srcRect,
1308 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -07001309 std::move(texture),
1310 sk_ref_sp(ii.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001311 &this->surfaceProps());
1312}
1313
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001315 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001316 SkASSERT(!paint.getImageFilter());
1317
joshualittce894002016-01-11 13:29:31 -08001318 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001320 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
kkinnunen2e4414e2015-02-19 07:20:40 -08001321
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001323 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324
robertphillips1b5f9682016-07-15 08:01:12 -07001325 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001326 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001327 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328 return;
1329 }
1330
robertphillips1b5f9682016-07-15 08:01:12 -07001331 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332}
1333
reeda85d4d02015-05-06 12:56:48 -07001334void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1335 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001336 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001337 SkMatrix viewMatrix = *draw.fMatrix;
1338 viewMatrix.preTranslate(x, y);
reed2d5b7142016-08-17 11:12:33 -07001339 uint32_t pinnedUniqueID;
1340 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001341 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001342 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001343 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001344 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1345 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001346 return;
reed85d91782015-09-10 14:33:38 -07001347 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001348 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001349 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
bsalomone553b642016-08-17 09:02:09 -07001350 paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
reed85d91782015-09-10 14:33:38 -07001351 // only support tiling as bitmap at the moment, so force raster-version
1352 if (!as_IB(image)->getROPixels(&bm)) {
1353 return;
1354 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001355 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1356 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1357 CHECK_SHOULD_DRAW(draw);
1358 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001359 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1360 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001361 } else if (as_IB(image)->getROPixels(&bm)) {
1362 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001363 }
reeda85d4d02015-05-06 12:56:48 -07001364 }
1365}
1366
1367void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001368 const SkRect& dst, const SkPaint& paint,
1369 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001370 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001371 uint32_t pinnedUniqueID;
1372 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
bsalomonc55271f2015-11-09 11:55:57 -08001373 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001374 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001375 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001376 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001377 return;
1378 }
1379 SkBitmap bm;
bsalomone553b642016-08-17 09:02:09 -07001380 SkMatrix srcToDstRect;
1381 srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
1382 dst, SkMatrix::kFill_ScaleToFit);
1383 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
1384 srcToDstRect)) {
bsalomonc55271f2015-11-09 11:55:57 -08001385 // only support tiling as bitmap at the moment, so force raster-version
1386 if (!as_IB(image)->getROPixels(&bm)) {
1387 return;
1388 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001389 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1390 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1391 CHECK_SHOULD_DRAW(draw);
1392 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001393 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001394 } else if (as_IB(image)->getROPixels(&bm)) {
1395 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001396 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001397}
1398
bsalomonf1ecd212015-12-09 17:06:02 -08001399void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001400 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001401 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001402
joshualitt33a5fce2015-11-18 13:28:51 -08001403 CHECK_SHOULD_DRAW(draw);
1404
joshualittedb36442015-11-19 14:29:30 -08001405 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001406 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001407 bool doBicubic;
1408 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001409 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1410 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001411 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
msarettc573a402016-08-02 08:05:56 -07001412 SkLatticeIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001413
1414 SkRect srcR, dstR;
1415 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001416 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001417 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001418 }
1419 return;
1420 }
1421
bsalomon2bbd0c62015-12-09 12:50:56 -08001422 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001423 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001424 producer->createFragmentProcessor(SkMatrix::I(),
1425 SkRect::MakeIWH(producer->width(), producer->height()),
1426 GrTextureProducer::kNo_FilterConstraint, true,
brianosman8fe485b2016-07-25 12:31:51 -07001427 &kMode, fDrawContext->getColorSpace(),
1428 fDrawContext->sourceGammaTreatment()));
joshualitt33a5fce2015-11-18 13:28:51 -08001429 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001430 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1431 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001432 return;
1433 }
1434
msarett10e3d9b2016-08-18 15:46:03 -07001435 std::unique_ptr<SkLatticeIter> iter(
1436 new SkLatticeIter(producer->width(), producer->height(), center, dst));
1437 fDrawContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1438 producer->height(), std::move(iter), dst);
bsalomon2bbd0c62015-12-09 12:50:56 -08001439}
1440
1441void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1442 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001443 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001444 uint32_t pinnedUniqueID;
1445 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1446 CHECK_SHOULD_DRAW(draw);
brianosman5814b8a2016-08-18 06:43:03 -07001447 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
reed2d5b7142016-08-17 11:12:33 -07001448 as_IB(image)->onImageInfo().colorSpace());
bsalomonf1ecd212015-12-09 17:06:02 -08001449 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001450 } else {
1451 SkBitmap bm;
1452 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1453 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001454 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001455 } else if (as_IB(image)->getROPixels(&bm)) {
1456 this->drawBitmapNine(draw, bm, center, dst, paint);
1457 }
1458 }
1459}
1460
1461void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1462 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001463 ASSERT_SINGLE_OWNER
reedc7ec7c92016-07-25 08:29:10 -07001464 GrBitmapTextureMaker maker(fContext, bitmap);
1465 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001466}
1467
msarett10e3d9b2016-08-18 15:46:03 -07001468void SkGpuDevice::drawProducerLattice(const SkDraw& draw, GrTextureProducer* producer,
1469 const SkCanvas::Lattice& lattice, const SkRect& dst,
1470 const SkPaint& paint) {
1471 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext);
1472
1473 CHECK_SHOULD_DRAW(draw);
1474
1475 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1476 sk_sp<GrFragmentProcessor> fp(
1477 producer->createFragmentProcessor(SkMatrix::I(),
1478 SkRect::MakeIWH(producer->width(), producer->height()),
1479 GrTextureProducer::kNo_FilterConstraint, true,
1480 &kMode, fDrawContext->getColorSpace(),
1481 fDrawContext->sourceGammaTreatment()));
1482 GrPaint grPaint;
1483 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1484 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
1485 return;
1486 }
1487
1488 std::unique_ptr<SkLatticeIter> iter(
1489 new SkLatticeIter(producer->width(), producer->height(), lattice, dst));
1490 fDrawContext->drawImageLattice(fClip, grPaint, *draw.fMatrix, producer->width(),
1491 producer->height(), std::move(iter), dst);
1492}
1493
1494void SkGpuDevice::drawImageLattice(const SkDraw& draw, const SkImage* image,
1495 const SkCanvas::Lattice& lattice, const SkRect& dst,
1496 const SkPaint& paint) {
1497 ASSERT_SINGLE_OWNER
1498 uint32_t pinnedUniqueID;
1499 if (sk_sp<GrTexture> tex = as_IB(image)->refPinnedTexture(&pinnedUniqueID)) {
1500 CHECK_SHOULD_DRAW(draw);
1501 GrTextureAdjuster adjuster(tex.get(), image->alphaType(), image->bounds(), pinnedUniqueID,
1502 as_IB(image)->onImageInfo().colorSpace());
1503 this->drawProducerLattice(draw, &adjuster, lattice, dst, paint);
1504 } else {
1505 SkBitmap bm;
1506 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1507 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
1508 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1509 } else if (as_IB(image)->getROPixels(&bm)) {
1510 this->drawBitmapLattice(draw, bm, lattice, dst, paint);
1511 }
1512 }
1513}
1514
1515void SkGpuDevice::drawBitmapLattice(const SkDraw& draw, const SkBitmap& bitmap,
1516 const SkCanvas::Lattice& lattice, const SkRect& dst,
1517 const SkPaint& paint) {
1518 ASSERT_SINGLE_OWNER
1519 GrBitmapTextureMaker maker(fContext, bitmap);
1520 this->drawProducerLattice(draw, &maker, lattice, dst, paint);
1521}
1522
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001523///////////////////////////////////////////////////////////////////////////////
1524
1525// must be in SkCanvas::VertexMode order
1526static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1527 kTriangles_GrPrimitiveType,
1528 kTriangleStrip_GrPrimitiveType,
1529 kTriangleFan_GrPrimitiveType,
1530};
1531
1532void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1533 int vertexCount, const SkPoint vertices[],
1534 const SkPoint texs[], const SkColor colors[],
1535 SkXfermode* xmode,
1536 const uint16_t indices[], int indexCount,
1537 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001538 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001539 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001540 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001541
halcanary96fcdcc2015-08-27 07:41:13 -07001542 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1543 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001544
halcanary96fcdcc2015-08-27 07:41:13 -07001545 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001546
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001547 SkPaint copy(paint);
1548 copy.setStyle(SkPaint::kStroke_Style);
1549 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001550
bsalomonf1b7a1d2015-09-28 06:26:28 -07001551 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001552 // we ignore the shader if texs is null.
brianosman8fe485b2016-07-25 12:31:51 -07001553 if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001554 return;
1555 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001556
dandov32a311b2014-07-15 19:46:26 -07001557 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001558 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001559 switch (vmode) {
1560 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001561 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001562 break;
1563 case SkCanvas::kTriangleStrip_VertexMode:
1564 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001565 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001566 break;
1567 }
mtklein533eb782014-08-27 10:39:42 -07001568
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001569 VertState state(vertexCount, indices, indexCount);
1570 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001571
dandov32a311b2014-07-15 19:46:26 -07001572 //number of indices for lines per triangle with kLines
1573 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001574
bsalomonf1b7a1d2015-09-28 06:26:28 -07001575 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001576 int i = 0;
1577 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001578 lineIndices[i] = state.f0;
1579 lineIndices[i + 1] = state.f1;
1580 lineIndices[i + 2] = state.f1;
1581 lineIndices[i + 3] = state.f2;
1582 lineIndices[i + 4] = state.f2;
1583 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001584 i += 6;
1585 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001586 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001587 grPaint,
1588 *draw.fMatrix,
1589 kLines_GrPrimitiveType,
1590 vertexCount,
1591 vertices,
1592 texs,
1593 colors,
1594 lineIndices.get(),
1595 indexCount);
1596 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001597 }
1598
bsalomonf1b7a1d2015-09-28 06:26:28 -07001599 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600
1601 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001602 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001603 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1604 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001605 convertedColors.reset(vertexCount);
1606 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001607 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001608 }
1609 colors = convertedColors.get();
1610 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001611 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001612 if (texs && paint.getShader()) {
1613 if (colors) {
1614 // When there are texs and colors the shader and colors are combined using xmode. A null
1615 // xmode is defined to mean modulate.
1616 SkXfermode::Mode colorMode;
1617 if (xmode) {
1618 if (!xmode->asMode(&colorMode)) {
1619 return;
1620 }
1621 } else {
1622 colorMode = SkXfermode::kModulate_Mode;
1623 }
brianosman8fe485b2016-07-25 12:31:51 -07001624 if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), paint,
1625 *draw.fMatrix, colorMode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001626 return;
1627 }
1628 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001629 // We have a shader, but no colors to blend it against.
brianosman8fe485b2016-07-25 12:31:51 -07001630 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1631 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001632 return;
1633 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001634 }
bsalomonaa48d362015-10-01 08:34:17 -07001635 } else {
1636 if (colors) {
1637 // We have colors, but either have no shader or no texture coords (which implies that
1638 // we should ignore the shader).
brianosman8fe485b2016-07-25 12:31:51 -07001639 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fDrawContext.get(), paint,
1640 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001641 return;
1642 }
1643 } else {
1644 // No colors and no shaders. Just draw with the paint color.
brianosman8fe485b2016-07-25 12:31:51 -07001645 if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001646 return;
1647 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001648 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001649 }
1650
robertphillips2e1e51f2015-10-15 08:01:48 -07001651 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001652 grPaint,
1653 *draw.fMatrix,
1654 primType,
1655 vertexCount,
1656 vertices,
1657 texs,
1658 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001659 indices,
robertphillipsea461502015-05-26 11:38:03 -07001660 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661}
1662
1663///////////////////////////////////////////////////////////////////////////////
1664
jvanverth31ff7622015-08-07 10:09:28 -07001665void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001666 const SkRect texRect[], const SkColor colors[], int count,
1667 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001668 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001669 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001670 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001671 return;
1672 }
1673
jvanverth31ff7622015-08-07 10:09:28 -07001674 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001675 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001676
reedca109532015-06-25 16:25:25 -07001677 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001678 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001679
jvanverth31ff7622015-08-07 10:09:28 -07001680 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001681 if (colors) {
brianosman8fe485b2016-07-25 12:31:51 -07001682 if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), p, *draw.fMatrix,
1683 mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001684 return;
1685 }
1686 } else {
brianosman8fe485b2016-07-25 12:31:51 -07001687 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001688 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001689 }
1690 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001691
1692 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001693 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001694}
1695
1696///////////////////////////////////////////////////////////////////////////////
1697
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001699 size_t byteLength, SkScalar x, SkScalar y,
1700 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001701 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001702 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001703 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001704
jvanverth8c27a182014-10-14 08:45:50 -07001705 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001706 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001707 return;
1708 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001709
jvanverth8c27a182014-10-14 08:45:50 -07001710 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001711
robertphillips2e1e51f2015-10-15 08:01:48 -07001712 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001713 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714}
1715
fmalita05c4a432014-09-29 06:29:53 -07001716void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1717 const SkScalar pos[], int scalarsPerPos,
1718 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001719 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001720 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001721 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001722
jvanverth8c27a182014-10-14 08:45:50 -07001723 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001724 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001725 return;
1726 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001727
jvanverth8c27a182014-10-14 08:45:50 -07001728 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001729
robertphillips2e1e51f2015-10-15 08:01:48 -07001730 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001731 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001732 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733}
1734
joshualitt9c328182015-03-23 08:13:04 -07001735void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1736 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001737 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001738 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001739 CHECK_SHOULD_DRAW(draw);
1740
1741 SkDEBUGCODE(this->validate();)
1742
robertphillips2e1e51f2015-10-15 08:01:48 -07001743 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001744 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001745}
1746
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747///////////////////////////////////////////////////////////////////////////////
1748
reedb2db8982014-11-13 12:41:02 -08001749bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001750 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001751}
1752
1753void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001754 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001755
robertphillips8c523e02016-07-26 07:41:00 -07001756 fDrawContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001757}
1758
1759///////////////////////////////////////////////////////////////////////////////
1760
reed76033be2015-03-14 10:54:31 -07001761SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001762 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763
robertphillipsca6eafc2016-05-17 09:57:46 -07001764 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765
robertphillipsca6eafc2016-05-17 09:57:46 -07001766 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001767 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001768 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1769 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001770
robertphillips6738c702016-07-27 12:13:51 -07001771 sk_sp<GrDrawContext> dc(fContext->makeDrawContext(fit,
1772 cinfo.fInfo.width(), cinfo.fInfo.height(),
1773 fDrawContext->config(),
1774 sk_ref_sp(fDrawContext->getColorSpace()),
1775 fDrawContext->desc().fSampleCnt,
1776 kDefault_GrSurfaceOrigin,
1777 &props));
robertphillipsca6eafc2016-05-17 09:57:46 -07001778 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001779 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001780 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001781 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001782 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001784
1785 // Skia's convention is to only clear a device if it is non-opaque.
1786 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1787
1788 return SkGpuDevice::Make(std::move(dc),
1789 cinfo.fInfo.width(), cinfo.fInfo.height(),
1790 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791}
1792
reede8f30622016-03-23 18:59:25 -07001793sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001794 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001795 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001796 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001797 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
robertphillips7e922762016-07-26 11:38:17 -07001798 fDrawContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001799}
1800
senorblanco900c3672016-04-27 11:31:23 -07001801SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001802 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001803 // We always return a transient cache, so it is freed after each
1804 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001805 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001806}
reedf037e0b2014-10-30 11:34:15 -07001807
1808#endif