blob: b81a94a194155d99c22a08b4f7bc56ef92d8ba50 [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"
herb11a7f7f2015-11-24 12:41:00 -080012#include "SkDraw.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
bsalomon6663acf2016-05-10 09:14:17 -070016#include "GrStyle.h"
egdanielbbcb38d2014-06-19 10:19:29 -070017#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080018#include "SkCanvasPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080019#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070021#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070022#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070023#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080024#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070026#include "SkImageFilterCache.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080028#include "SkNinePatchIter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070031#include "SkPictureData.h"
reed1e7f5e72016-04-27 07:49:17 -070032#include "SkRasterClip.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080034#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070035#include "SkSpecialImage.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080038#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000039#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000040#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000041#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070042#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070043#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080044#include "effects/GrBicubicEffect.h"
45#include "effects/GrDashingEffect.h"
46#include "effects/GrSimpleTextureEffect.h"
47#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080048#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049
reedf037e0b2014-10-30 11:34:15 -070050#if SK_SUPPORT_GPU
51
joshualittce894002016-01-11 13:29:31 -080052#define ASSERT_SINGLE_OWNER \
53 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
54
senorblanco55b6d8b2014-07-30 11:26:46 -070055enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
56
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057#if 0
58 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080059 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060 do { \
61 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080062 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063 } while (0)
64#else
joshualitt5531d512014-12-17 15:50:11 -080065 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066#endif
67
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000068///////////////////////////////////////////////////////////////////////////////
69
bsalomon74f681d2015-06-23 14:38:48 -070070/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
71 should fail. */
72bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
73 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
74 *flags = 0;
75 if (info) {
76 switch (info->alphaType()) {
77 case kPremul_SkAlphaType:
78 break;
79 case kOpaque_SkAlphaType:
80 *flags |= SkGpuDevice::kIsOpaque_Flag;
81 break;
82 default: // If it is unpremul or unknown don't try to render
83 return false;
84 }
85 }
86 if (kClear_InitContents == init) {
87 *flags |= kNeedClear_Flag;
88 }
89 return true;
90}
91
brianosmandfe4f2e2016-07-21 13:28:36 -070092sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, sk_sp<SkColorSpace> colorSpace,
93 const SkSurfaceProps* props, InitContents init) {
robertphillipsca6eafc2016-05-17 09:57:46 -070094 if (!rt || rt->wasDestroyed() || !rt->getContext()) {
halcanary96fcdcc2015-08-27 07:41:13 -070095 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000096 }
bsalomon74f681d2015-06-23 14:38:48 -070097 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -070098 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
99 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700100 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700101
102 const int width = rt->width();
103 const int height = rt->height();
104
105 GrContext* context = rt->getContext();
106
brianosmandfe4f2e2016-07-21 13:28:36 -0700107 sk_sp<GrDrawContext> drawContext(context->drawContext(std::move(rt), std::move(colorSpace),
108 props));
robertphillipsca6eafc2016-05-17 09:57:46 -0700109 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
110}
111
112sk_sp<SkBaseDevice> SkGpuDevice::Make(sk_sp<GrDrawContext> drawContext,
113 int width, int height,
114 InitContents init) {
115 if (!drawContext || drawContext->wasAbandoned()) {
116 return nullptr;
117 }
118 unsigned flags;
119 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
120 return nullptr;
121 }
122 return sk_sp<SkBaseDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000123}
124
robertphillips24e91282016-04-29 06:46:36 -0700125sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
126 const SkImageInfo& info, int sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700127 GrSurfaceOrigin origin,
robertphillips24e91282016-04-29 06:46:36 -0700128 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700129 unsigned flags;
130 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700131 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700132 }
133
robertphillips7e922762016-07-26 11:38:17 -0700134 sk_sp<GrDrawContext> drawContext(MakeDrawContext(context, budgeted, info,
135 sampleCount, origin, props));
robertphillipsca6eafc2016-05-17 09:57:46 -0700136 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700137 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700138 }
139
robertphillipsca6eafc2016-05-17 09:57:46 -0700140 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext),
141 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700142}
143
robertphillipsca6eafc2016-05-17 09:57:46 -0700144SkGpuDevice::SkGpuDevice(sk_sp<GrDrawContext> drawContext, int width, int height, unsigned flags)
145 : INHERITED(drawContext->surfaceProps())
146 , fContext(SkRef(drawContext->accessRenderTarget()->getContext()))
147 , fRenderTarget(drawContext->renderTarget())
148 , fDrawContext(std::move(drawContext)) {
robertphillips1f3923e2016-07-21 07:17:54 -0700149 fSize.set(width, height);
bsalomon74f681d2015-06-23 14:38:48 -0700150 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151
bsalomone63ffef2016-02-05 07:17:34 -0800152 if (flags & kNeedClear_Flag) {
153 this->clearAll();
154 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155}
156
robertphillips7e922762016-07-26 11:38:17 -0700157sk_sp<GrDrawContext> SkGpuDevice::MakeDrawContext(GrContext* context,
158 SkBudgeted budgeted,
159 const SkImageInfo& origInfo,
160 int sampleCount,
161 GrSurfaceOrigin origin,
162 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000163 if (kUnknown_SkColorType == origInfo.colorType() ||
164 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000166 }
167
bsalomonafe30052015-01-16 07:32:33 -0800168 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800170 }
171
reede5ea5002014-09-03 11:54:58 -0700172 SkColorType ct = origInfo.colorType();
173 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700174 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700175 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700176 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800177 }
178 if (kOpaque_SkAlphaType != at) {
179 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000180 }
brianosman6b086522016-04-14 12:39:00 -0700181
brianosmanb109b8c2016-06-16 13:03:24 -0700182 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
brianosman6b086522016-04-14 12:39:00 -0700183 if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) {
184 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
185 ct = kN32_SkColorType;
186 }
187
brianosmanb109b8c2016-06-16 13:03:24 -0700188 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189
robertphillipsca6eafc2016-05-17 09:57:46 -0700190 return context->newDrawContext(SkBackingFit::kExact, // Why exact?
191 origInfo.width(), origInfo.height(),
brianosmandfe4f2e2016-07-21 13:28:36 -0700192 config, sk_ref_sp(cs), sampleCount,
robertphillips7e922762016-07-26 11:38:17 -0700193 origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800194}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195
robertphillips970587b2016-07-14 14:12:55 -0700196sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
197 SkSpecialImage* srcImg,
198 int left, int top,
199 SkIPoint* offset,
200 const SkImageFilter* filter) {
201 SkASSERT(srcImg->isTextureBacked());
202 SkASSERT(filter);
203
204 SkMatrix matrix = *draw.fMatrix;
205 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
206 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
207 SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
208 SkImageFilter::Context ctx(matrix, clipBounds, cache.get());
209
210 return filter->filterImage(srcImg, ctx, offset);
211}
212
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213///////////////////////////////////////////////////////////////////////////////
214
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000215bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
216 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800217 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000218
219 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700220 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000221 if (kUnknown_GrPixelConfig == config) {
222 return false;
223 }
224
225 uint32_t flags = 0;
226 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
227 flags = GrContext::kUnpremul_PixelOpsFlag;
228 }
bsalomon74f681d2015-06-23 14:38:48 -0700229 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
230 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000231}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000232
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000233bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
234 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800235 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000236 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700237 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000238 if (kUnknown_GrPixelConfig == config) {
239 return false;
240 }
241 uint32_t flags = 0;
242 if (kUnpremul_SkAlphaType == info.alphaType()) {
243 flags = GrContext::kUnpremul_PixelOpsFlag;
244 }
245 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
246
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000247 return true;
248}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000249
reed41e010c2015-06-09 12:16:53 -0700250bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800251 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700252 return false;
253}
254
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000255// call this every draw call, to ensure that the context reflects our state,
256// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800257void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800258 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259
reed2c9e2002016-07-25 08:05:22 -0700260 fClip.reset(draw.fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000261}
262
robertphillips175dd9b2016-04-28 14:32:04 -0700263GrDrawContext* SkGpuDevice::accessDrawContext() {
264 ASSERT_SINGLE_OWNER
265 return fDrawContext.get();
266}
267
reed8eddfb52014-12-04 07:50:14 -0800268void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800269 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800270 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800271 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800272 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700273 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800274}
275
robertphillipsca6eafc2016-05-17 09:57:46 -0700276void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800277 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800278
bsalomon5ec26ae2016-02-25 08:33:02 -0800279 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800280
robertphillips7e922762016-07-26 11:38:17 -0700281 sk_sp<GrDrawContext> newDC(MakeDrawContext(this->context(),
282 budgeted,
283 this->imageInfo(),
284 fDrawContext->numColorSamples(),
285 fDrawContext->origin(),
286 &this->surfaceProps()));
robertphillipsca6eafc2016-05-17 09:57:46 -0700287 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800288 return;
289 }
290
291 if (shouldRetainContent) {
292 if (fRenderTarget->wasDestroyed()) {
293 return;
294 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700295 newDC->copySurface(fDrawContext->asTexture().get(),
296 SkIRect::MakeWH(this->width(), this->height()),
297 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800298 }
299
robertphillipsca6eafc2016-05-17 09:57:46 -0700300 SkASSERT(fDrawContext->accessRenderTarget() != newDC->accessRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800301
robertphillipsca6eafc2016-05-17 09:57:46 -0700302 fRenderTarget = newDC->renderTarget();
kkinnunenabcfab42015-02-22 22:53:44 -0800303
robertphillipsca6eafc2016-05-17 09:57:46 -0700304 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800305}
306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307///////////////////////////////////////////////////////////////////////////////
308
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800310 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800311 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800312 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000313
314 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700315 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700316 return;
317 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318
robertphillips2e1e51f2015-10-15 08:01:48 -0700319 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320}
321
322// must be in SkCanvas::PointMode order
323static const GrPrimitiveType gPointMode2PrimtiveType[] = {
324 kPoints_GrPrimitiveType,
325 kLines_GrPrimitiveType,
326 kLineStrip_GrPrimitiveType
327};
328
ethannicholas330bb952015-07-17 06:44:02 -0700329// suppress antialiasing on axis-aligned integer-coordinate lines
330static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
331 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
332 return false;
333 }
334 if (count == 2) {
335 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
336 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800337 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700338 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800339 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700340 // the two end pixels as being the lesser of two evils.
341 if (pts[0].fX == pts[1].fX) {
342 return ((int) pts[0].fX) != pts[0].fX;
343 }
344 if (pts[0].fY == pts[1].fY) {
345 return ((int) pts[0].fY) != pts[0].fY;
346 }
347 }
348 return true;
349}
350
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
352 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800353 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800354 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800355 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356
357 SkScalar width = paint.getStrokeWidth();
358 if (width < 0) {
359 return;
360 }
361
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000362 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700363 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700364 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700365 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700366 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700367 return;
368 }
egdaniele61c4112014-06-12 10:24:21 -0700369 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700370 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700371 path.moveTo(pts[0]);
372 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700373 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700374 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000375 }
376
ethannicholas330bb952015-07-17 06:44:02 -0700377 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800379 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700380 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381 draw.drawPoints(mode, count, pts, paint, true);
382 return;
383 }
384
385 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700386 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700387 return;
388 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389
robertphillips2e1e51f2015-10-15 08:01:48 -0700390 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700391 grPaint,
392 *draw.fMatrix,
393 gPointMode2PrimtiveType[mode],
394 SkToS32(count),
395 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700396 nullptr,
397 nullptr,
398 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700399 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400}
401
402///////////////////////////////////////////////////////////////////////////////
403
robertphillipsff55b492015-11-24 07:56:59 -0800404void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800405 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800406 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800407 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000408
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409
bsalomona7d85ba2016-07-06 11:54:59 -0700410 // A couple reasons we might need to call drawPath.
411 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700413 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700415 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800416 fClip, path, paint,
417 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700418 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000419 return;
420 }
421
422 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700423 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700424 return;
425 }
Mike Klein744fb732014-06-23 15:13:26 -0400426
bsalomon6663acf2016-05-10 09:14:17 -0700427 GrStyle style(paint);
428 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429}
430
431///////////////////////////////////////////////////////////////////////////////
432
433void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800434 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800435 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800436 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800437 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000439 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700440 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700441 return;
442 }
Mike Klein744fb732014-06-23 15:13:26 -0400443
bsalomon6663acf2016-05-10 09:14:17 -0700444 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000445 if (paint.getMaskFilter()) {
446 // try to hit the fast path for drawing filtered round rects
447
448 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800449 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000450 if (devRRect.allCornersCircular()) {
451 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700452 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700453 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800454 *draw.fMatrix,
455 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000456 SkIRect finalIRect;
457 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700458 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000459 // clipped out
460 return;
461 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700462 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
robertphillips6c7e3252016-04-27 10:47:51 -0700463 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800464 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800465 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800466 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700467 style.strokeRec(),
egdanield58a0ba2014-06-11 10:30:05 -0700468 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000469 return;
470 }
471 }
472
473 }
474 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000475 }
476
bsalomon6663acf2016-05-10 09:14:17 -0700477 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800478 // The only mask filter the native rrect drawing code could've handle was taken
479 // care of above.
480 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700482 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000483 path.addRRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700484 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800485 fClip, path, paint,
486 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700487 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000488 return;
489 }
Mike Klein744fb732014-06-23 15:13:26 -0400490
bsalomon6663acf2016-05-10 09:14:17 -0700491 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800492
bsalomon6663acf2016-05-10 09:14:17 -0700493 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494}
495
robertphillipsd7706102016-02-25 09:28:08 -0800496
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000497void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800498 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800499 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800500 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800501 CHECK_SHOULD_DRAW(draw);
502
robertphillipsd7706102016-02-25 09:28:08 -0800503 if (outer.isEmpty()) {
504 return;
505 }
506
507 if (inner.isEmpty()) {
508 return this->drawRRect(draw, outer, paint);
509 }
510
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000511 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000512
robertphillips0e7029e2015-11-30 05:45:06 -0800513 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800514 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700515 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700516 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700517 return;
518 }
robertphillips00095892016-02-29 13:50:40 -0800519
520 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
521 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000522 }
523
524 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700525 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000526 path.addRRect(outer);
527 path.addRRect(inner);
528 path.setFillType(SkPath::kEvenOdd_FillType);
529
robertphillips6c7e3252016-04-27 10:47:51 -0700530 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800531 fClip, path, paint,
532 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700533 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000534}
535
536
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000537/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538
robertphillips514450c2015-11-24 05:36:02 -0800539void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800540 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800541 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800542 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543
robertphillips514450c2015-11-24 05:36:02 -0800544 // Presumably the path effect warps this to something other than an oval
545 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700547 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700549 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 return;
herb11a7f7f2015-11-24 12:41:00 -0800551 }
552
robertphillips514450c2015-11-24 05:36:02 -0800553 if (paint.getMaskFilter()) {
554 // The RRect path can handle special case blurring
555 SkRRect rr = SkRRect::MakeOval(oval);
556 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557 }
558
559 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700560 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700561 return;
562 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563
bsalomon6663acf2016-05-10 09:14:17 -0700564 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000565}
566
567#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568
569///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700570void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
571 const SkDraw& draw,
572 const SkPaint& origPaint) {
573 ASSERT_SINGLE_OWNER
574 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
575 CHECK_SHOULD_DRAW(draw);
576
577 // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix
578 // entry point
579 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
580 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
581 SkASSERT(!origPaint.getPathEffect());
582 SkASSERT(!origPaint.getMaskFilter());
583
584 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
585 SkASSERT(halfWidth > 0);
586
587 SkVector v = points[1] - points[0];
588
589 SkScalar length = SkPoint::Normalize(&v);
590 if (!length) {
591 v.fX = 1.0f;
592 v.fY = 0.0f;
593 }
594
595 SkPaint newPaint(origPaint);
596 newPaint.setStyle(SkPaint::kFill_Style);
597
598 SkScalar xtraLength = 0.0f;
599 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
600 xtraLength = halfWidth;
601 }
602
603 SkPoint mid = points[0] + points[1];
604 mid.scale(0.5f);
605
606 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
607 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
608 SkMatrix m;
609 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
610
611 SkMatrix local = m;
612
613 m.postConcat(*draw.fMatrix);
614
615 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700616 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), newPaint, m, &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700617 return;
618 }
619
620 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
621}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000622
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000623void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
624 const SkPaint& paint, const SkMatrix* prePathMatrix,
625 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800626 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800627 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700628 SkPoint points[2];
629 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
630 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
631 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
632 // Path-based stroking looks better for thin rects
633 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700634 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700635 // Round capping support is currently disabled b.c. it would require
636 // a RRect batch that takes a localMatrix.
637 this->drawStrokedLine(points, draw, paint);
638 return;
639 }
640 }
robertphillipsff55b492015-11-24 07:56:59 -0800641 bool isClosed;
642 SkRect rect;
643 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
644 this->drawRect(draw, rect, paint);
645 return;
646 }
647 if (origSrcPath.isOval(&rect)) {
648 this->drawOval(draw, rect, paint);
649 return;
650 }
651 SkRRect rrect;
652 if (origSrcPath.isRRect(&rrect)) {
653 this->drawRRect(draw, rrect, paint);
654 return;
655 }
656 }
657
joshualitt5531d512014-12-17 15:50:11 -0800658 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800659 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660
robertphillips6c7e3252016-04-27 10:47:51 -0700661 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700662 fClip, origSrcPath, paint,
663 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700664 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665}
666
667static const int kBmpSmallTileSize = 1 << 10;
668
669static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
670 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
671 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
672 return tilesX * tilesY;
673}
674
reed85d91782015-09-10 14:33:38 -0700675static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 if (maxTileSize <= kBmpSmallTileSize) {
677 return maxTileSize;
678 }
679
680 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
681 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
682
683 maxTileTotalTileSize *= maxTileSize * maxTileSize;
684 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
685
686 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
687 return kBmpSmallTileSize;
688 } else {
689 return maxTileSize;
690 }
691}
692
693// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
694// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700695static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800696 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800697 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700698 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 const SkRect* srcRectPtr,
700 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700701 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800703 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 clippedSrcIRect->setEmpty();
705 return;
706 }
707 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
708 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700709 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000710 // we've setup src space 0,0 to map to the top left of the src rect.
711 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712 if (!clippedSrcRect.intersect(*srcRectPtr)) {
713 clippedSrcIRect->setEmpty();
714 return;
715 }
716 }
717 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700718 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 if (!clippedSrcIRect->intersect(bmpBounds)) {
720 clippedSrcIRect->setEmpty();
721 }
722}
723
reed85d91782015-09-10 14:33:38 -0700724bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
725 const SkMatrix& viewMatrix,
726 const GrTextureParams& params,
727 const SkRect* srcRectPtr,
728 int maxTileSize,
729 int* tileSize,
730 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800731 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700732 // if it's larger than the max tile size, then we have no choice but tiling.
733 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
robertphillipse5768742016-05-13 11:20:46 -0700734 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
735 fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700736 srcRectPtr, clippedSubset);
737 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
738 return true;
739 }
740
bsalomon1a1d0b82015-10-16 07:49:42 -0700741 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700742 const size_t area = imageRect.width() * imageRect.height();
743 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
744 return false;
745 }
746
reed85d91782015-09-10 14:33:38 -0700747 // At this point we know we could do the draw by uploading the entire bitmap
748 // as a texture. However, if the texture would be large compared to the
749 // cache size and we don't require most of it for this draw then tile to
750 // reduce the amount of upload and cache spill.
751
752 // assumption here is that sw bitmap size is a good proxy for its size as
753 // a texture
754 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
755 size_t cacheSize;
756 fContext->getResourceCacheLimits(nullptr, &cacheSize);
757 if (bmpSize < cacheSize / 2) {
758 return false;
759 }
760
bsalomon1a1d0b82015-10-16 07:49:42 -0700761 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
762 // tiling memory savings would be < 50%.
robertphillipse5768742016-05-13 11:20:46 -0700763 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
764 fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700765 clippedSubset);
766 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
767 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
768 kBmpSmallTileSize * kBmpSmallTileSize;
769
770 return usedTileBytes < 2 * bmpSize;
771}
772
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800774 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775 const GrTextureParams& params,
776 const SkRect* srcRectPtr,
777 int maxTileSize,
778 int* tileSize,
779 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800780 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700781 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
782 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
783}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784
reed85d91782015-09-10 14:33:38 -0700785bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
786 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
787 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800788 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700789 // if image is explictly texture backed then just use the texture
790 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 return false;
792 }
793
reed85d91782015-09-10 14:33:38 -0700794 GrTextureParams params;
795 bool doBicubic;
796 GrTextureParams::FilterMode textureFilterMode =
797 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
798
799 int tileFilterPad;
800 if (doBicubic) {
801 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
802 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
803 tileFilterPad = 0;
804 } else {
805 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806 }
reed85d91782015-09-10 14:33:38 -0700807 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808
bsalomon8c07b7a2015-11-02 11:36:52 -0800809 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810
reed85d91782015-09-10 14:33:38 -0700811 // these are output, which we safely ignore, as we just want to know the predicate
812 int outTileSize;
813 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814
reed85d91782015-09-10 14:33:38 -0700815 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
816 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817}
818
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000819void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820 const SkBitmap& bitmap,
821 const SkMatrix& m,
822 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800823 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800824 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800825 SkMatrix viewMatrix;
826 viewMatrix.setConcat(*origDraw.fMatrix, m);
reedc7ec7c92016-07-25 08:29:10 -0700827
bsalomonb1b01992015-11-18 10:56:08 -0800828 int maxTileSize = fContext->caps()->maxTileSize();
829
830 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
831 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700832 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800833 paint.isAntiAlias() &&
834 bitmap.width() <= maxTileSize &&
835 bitmap.height() <= maxTileSize;
836
837 bool skipTileCheck = drawAA || paint.getMaskFilter();
838
839 if (!skipTileCheck) {
840 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
841 int tileSize;
842 SkIRect clippedSrcRect;
843
844 GrTextureParams params;
845 bool doBicubic;
846 GrTextureParams::FilterMode textureFilterMode =
847 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
848 &doBicubic);
849
850 int tileFilterPad;
851
852 if (doBicubic) {
853 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
854 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
855 tileFilterPad = 0;
856 } else {
857 tileFilterPad = 1;
858 }
859 params.setFilterMode(textureFilterMode);
860
861 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
862 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
863 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
864 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
865 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
866 return;
867 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000868 }
bsalomonb1b01992015-11-18 10:56:08 -0800869 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800870 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
871 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872}
873
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000874// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
876// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000877static inline void clamped_outset_with_offset(SkIRect* iRect,
878 int outset,
879 SkPoint* offset,
880 const SkIRect& clamp) {
881 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000883 int leftClampDelta = clamp.fLeft - iRect->fLeft;
884 if (leftClampDelta > 0) {
885 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000886 iRect->fLeft = clamp.fLeft;
887 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000888 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000890
891 int topClampDelta = clamp.fTop - iRect->fTop;
892 if (topClampDelta > 0) {
893 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000894 iRect->fTop = clamp.fTop;
895 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000896 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897 }
898
899 if (iRect->fRight > clamp.fRight) {
900 iRect->fRight = clamp.fRight;
901 }
902 if (iRect->fBottom > clamp.fBottom) {
903 iRect->fBottom = clamp.fBottom;
904 }
905}
906
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907// Break 'bitmap' into several tiles to draw it since it has already
908// been determined to be too large to fit in VRAM
909void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800910 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911 const SkRect& srcRect,
912 const SkIRect& clippedSrcIRect,
913 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800914 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700915 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000916 int tileSize,
917 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800918 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800919
ericrk983294f2016-04-18 09:14:00 -0700920 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800921 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700922 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800923
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000924 // The following pixel lock is technically redundant, but it is desirable
925 // to lock outside of the tile loop to prevent redecoding the whole image
926 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
927 // is larger than the limit of the discardable memory pool.
928 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800929
930 const SkPaint* paint = &origPaint;
931 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -0700932 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -0800933 // Drop antialiasing to avoid seams at tile boundaries.
934 tempPaint = origPaint;
935 tempPaint.setAntiAlias(false);
936 paint = &tempPaint;
937 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
939
940 int nx = bitmap.width() / tileSize;
941 int ny = bitmap.height() / tileSize;
942 for (int x = 0; x <= nx; x++) {
943 for (int y = 0; y <= ny; y++) {
944 SkRect tileR;
945 tileR.set(SkIntToScalar(x * tileSize),
946 SkIntToScalar(y * tileSize),
947 SkIntToScalar((x + 1) * tileSize),
948 SkIntToScalar((y + 1) * tileSize));
949
950 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
951 continue;
952 }
953
954 if (!tileR.intersect(srcRect)) {
955 continue;
956 }
957
958 SkBitmap tmpB;
959 SkIRect iTileR;
960 tileR.roundOut(&iTileR);
961 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
962 SkIntToScalar(iTileR.fTop));
963
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000964 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -0800965 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000966 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000967 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800968 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000969
robertphillipsec8bb942014-11-21 10:16:25 -0800970 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000971 SkIRect iClampRect;
972
reeda5517e22015-07-14 10:54:12 -0700973 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 // In bleed mode we want to always expand the tile on all edges
975 // but stay within the bitmap bounds
976 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
977 } else {
978 // In texture-domain/clamp mode we only want to expand the
979 // tile on edges interior to "srcRect" (i.e., we want to
980 // not bleed across the original clamped edges)
981 srcRect.roundOut(&iClampRect);
982 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000983 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
984 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 }
986
987 if (bitmap.extractSubset(&tmpB, iTileR)) {
988 // now offset it to make it "local" to our tmp bitmap
989 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000990 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -0800991 // de-optimized this determination
992 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000993 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -0800994 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000995 tileR,
996 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -0800997 *paint,
reeda5517e22015-07-14 10:54:12 -0700998 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000999 bicubic,
1000 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 }
1002 }
1003 }
1004}
1005
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001006/*
1007 * This is called by drawBitmap(), which has to handle images that may be too
1008 * large to be represented by a single texture.
1009 *
1010 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1011 * and that non-texture portion of the GrPaint has already been setup.
1012 */
1013void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001014 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 const SkRect& srcRect,
1016 const GrTextureParams& params,
1017 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001018 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001019 bool bicubic,
1020 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001021 // We should have already handled bitmaps larger than the max texture size.
1022 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1023 bitmap.height() <= fContext->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -07001024 // We should be respecting the max tile size by the time we get here.
1025 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1026 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027
brianosman8fe485b2016-07-25 12:31:51 -07001028 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap, params,
1029 fDrawContext->sourceGammaTreatment());
halcanary96fcdcc2015-08-27 07:41:13 -07001030 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031 return;
1032 }
1033
brianosman500bb3e2016-07-22 10:33:07 -07001034 sk_sp<GrColorSpaceXform> colorSpaceXform =
1035 GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace());
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001036 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 SkRect paintRect;
1038 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1039 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1040 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1041 SkScalarMul(srcRect.fTop, hInv),
1042 SkScalarMul(srcRect.fRight, wInv),
1043 SkScalarMul(srcRect.fBottom, hInv));
1044
egdaniel79da63f2015-10-09 10:55:16 -07001045 SkMatrix texMatrix;
1046 texMatrix.reset();
1047 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1048 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1049 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1050 // Thus we need to pass in the transform matrix directly to the texture processor used for
1051 // the bitmap draw.
1052 texMatrix.setScale(wInv, hInv);
1053 }
1054
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001056
1057 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1058 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001059 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001060
reeda5517e22015-07-14 10:54:12 -07001061 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 // Use a constrained texture domain to avoid color bleeding
1063 SkScalar left, top, right, bottom;
1064 if (srcRect.width() > SK_Scalar1) {
1065 SkScalar border = SK_ScalarHalf / texture->width();
1066 left = paintRect.left() + border;
1067 right = paintRect.right() - border;
1068 } else {
1069 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1070 }
1071 if (srcRect.height() > SK_Scalar1) {
1072 SkScalar border = SK_ScalarHalf / texture->height();
1073 top = paintRect.top() + border;
1074 bottom = paintRect.bottom() - border;
1075 } else {
1076 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1077 }
1078 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001079 if (bicubic) {
reedc7ec7c92016-07-25 08:29:10 -07001080 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
brianosman54f30c12016-07-18 10:53:52 -07001081 textureDomain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001082 } else {
reedc7ec7c92016-07-25 08:29:10 -07001083 fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
brianosman54f30c12016-07-18 10:53:52 -07001084 textureDomain, GrTextureDomain::kClamp_Mode,
1085 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001086 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001087 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001088 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1089 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
reedc7ec7c92016-07-25 08:29:10 -07001090 fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001091 } else {
reedc7ec7c92016-07-25 08:29:10 -07001092 fp = GrSimpleTextureEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093 }
1094
joshualitt33a5fce2015-11-18 13:28:51 -08001095 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001096 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, viewMatrix,
1097 std::move(fp), kAlpha_8_SkColorType == bitmap.colorType(),
1098 &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001099 return;
1100 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101
egdaniel79da63f2015-10-09 10:55:16 -07001102 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1103 // We don't have local coords in this case and have previously set the transform
1104 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001105 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001106 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001107 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001108 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109}
1110
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1112 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001113 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001114 CHECK_SHOULD_DRAW(draw);
robertphillips970587b2016-07-14 14:12:55 -07001115 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116
robertphillips970587b2016-07-14 14:12:55 -07001117 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118 return;
1119 }
1120
reedc7ec7c92016-07-25 08:29:10 -07001121 sk_sp<GrTexture> texture;
1122 {
robertphillips970587b2016-07-14 14:12:55 -07001123 SkAutoLockPixels alp(bitmap, true);
1124 if (!bitmap.readyToDraw()) {
1125 return;
1126 }
1127
1128 // draw sprite neither filters nor tiles.
1129 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap,
1130 GrTextureParams::ClampNoFilter(),
1131 SkSourceGammaTreatment::kRespect));
1132 if (!texture) {
1133 return;
1134 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001135 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136
robertphillips970587b2016-07-14 14:12:55 -07001137 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1138 bitmap.pixelRefOrigin().fY,
1139 bitmap.width(),
1140 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001141
robertphillips970587b2016-07-14 14:12:55 -07001142 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1143 bitmap.getGenerationID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001144 std::move(texture),
1145 sk_ref_sp(bitmap.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001146 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001147
1148 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1149}
1150
1151
1152void SkGpuDevice::drawSpecial(const SkDraw& draw,
1153 SkSpecialImage* special1,
1154 int left, int top,
1155 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001156 ASSERT_SINGLE_OWNER
1157 CHECK_SHOULD_DRAW(draw);
1158 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext);
robertphillips970587b2016-07-14 14:12:55 -07001159
1160 SkIPoint offset = { 0, 0 };
1161
1162 sk_sp<SkSpecialImage> result;
1163 if (paint.getImageFilter()) {
1164 result = this->filterTexture(draw, special1, left, top,
1165 &offset,
1166 paint.getImageFilter());
1167 if (!result) {
1168 return;
1169 }
1170 } else {
1171 result = sk_ref_sp(special1);
1172 }
1173
1174 SkASSERT(result->isTextureBacked());
1175 sk_sp<GrTexture> texture = result->asTextureRef(fContext);
1176
1177 SkPaint tmpUnfiltered(paint);
1178 tmpUnfiltered.setImageFilter(nullptr);
1179
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180 GrPaint grPaint;
brianosman54f30c12016-07-18 10:53:52 -07001181 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), nullptr,
1182 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001183 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001184 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001185 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001186 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001187 }
brianosman8fe485b2016-07-25 12:31:51 -07001188 if (!SkPaintToGrPaintReplaceShader(this->context(), fDrawContext.get(), tmpUnfiltered,
1189 std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001190 return;
1191 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192
robertphillips970587b2016-07-14 14:12:55 -07001193 const SkIRect& subset = result->subset();
1194
bsalomona2e69fc2015-11-05 10:41:43 -08001195 fDrawContext->fillRectToRect(fClip,
1196 grPaint,
1197 SkMatrix::I(),
robertphillips970587b2016-07-14 14:12:55 -07001198 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY,
1199 subset.width(), subset.height())),
1200 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1201 SkIntToScalar(subset.fTop) / texture->height(),
1202 SkIntToScalar(subset.width()) / texture->width(),
1203 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204}
1205
bsalomonb1b01992015-11-18 10:56:08 -08001206void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1207 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001208 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001209 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001210 CHECK_SHOULD_DRAW(draw);
reedc7ec7c92016-07-25 08:29:10 -07001211
bsalomonb1b01992015-11-18 10:56:08 -08001212 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1213 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1214 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1215 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1216 const SkRect* dst = &origDst;
1217 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001219 if (!src) {
1220 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001222
bsalomonb1b01992015-11-18 10:56:08 -08001223 SkMatrix srcToDstMatrix;
1224 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1225 return;
1226 }
1227 SkRect tmpSrc, tmpDst;
1228 if (src != &bmpBounds) {
1229 if (!bmpBounds.contains(*src)) {
1230 tmpSrc = *src;
1231 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 return; // nothing to draw
1233 }
bsalomonb1b01992015-11-18 10:56:08 -08001234 src = &tmpSrc;
1235 srcToDstMatrix.mapRect(&tmpDst, *src);
1236 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001237 }
1238 }
1239
bsalomonb1b01992015-11-18 10:56:08 -08001240 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001241
bsalomonb1b01992015-11-18 10:56:08 -08001242 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1243 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001244 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001245 paint.isAntiAlias() &&
1246 bitmap.width() <= maxTileSize &&
1247 bitmap.height() <= maxTileSize;
1248
1249 bool skipTileCheck = drawAA || paint.getMaskFilter();
1250
1251 if (!skipTileCheck) {
1252 int tileSize;
1253 SkIRect clippedSrcRect;
1254
1255 GrTextureParams params;
1256 bool doBicubic;
1257 GrTextureParams::FilterMode textureFilterMode =
1258 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1259 &doBicubic);
1260
1261 int tileFilterPad;
1262
1263 if (doBicubic) {
1264 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1265 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1266 tileFilterPad = 0;
1267 } else {
1268 tileFilterPad = 1;
1269 }
1270 params.setFilterMode(textureFilterMode);
1271
1272 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1273 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1274 // we have a mask filter.
1275 SkMatrix viewMatrix = *draw.fMatrix;
1276 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1277 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1278 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1279 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1280 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1281 constraint, tileSize, doBicubic);
1282 return;
1283 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001284 }
bsalomonb1b01992015-11-18 10:56:08 -08001285 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001286 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001287}
1288
robertphillips6451a0c2016-07-18 08:31:31 -07001289sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
robertphillips6451a0c2016-07-18 08:31:31 -07001290 SkAutoLockPixels alp(bitmap, true);
1291 if (!bitmap.readyToDraw()) {
1292 return nullptr;
1293 }
1294
reedc7ec7c92016-07-25 08:29:10 -07001295 sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext, bitmap,
1296 GrTextureParams::ClampNoFilter(),
1297 SkSourceGammaTreatment::kRespect);
robertphillips6451a0c2016-07-18 08:31:31 -07001298 if (!texture) {
1299 return nullptr;
1300 }
1301
1302 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1303 bitmap.getGenerationID(),
reedc7ec7c92016-07-25 08:29:10 -07001304 texture,
brianosmanafbf71d2016-07-21 07:15:37 -07001305 sk_ref_sp(bitmap.colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001306 &this->surfaceProps());
1307}
1308
reede51c3562016-07-19 14:33:20 -07001309sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001310 SkPixmap pm;
1311 if (image->isTextureBacked()) {
1312 GrTexture* texture = as_IB(image)->peekTexture();
1313
1314 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1315 image->uniqueID(),
brianosmanafbf71d2016-07-21 07:15:37 -07001316 sk_ref_sp(texture),
1317 sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()),
robertphillips6451a0c2016-07-18 08:31:31 -07001318 &this->surfaceProps());
1319 } else if (image->peekPixels(&pm)) {
1320 SkBitmap bm;
1321
1322 bm.installPixels(pm);
1323 return this->makeSpecial(bm);
1324 } else {
1325 return nullptr;
1326 }
1327}
1328
1329sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
robertphillips1b5f9682016-07-15 08:01:12 -07001330 sk_sp<GrTexture> texture(this->accessDrawContext()->asTexture());
1331 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001332 // When the device doesn't have a texture, we create a temporary texture.
1333 // TODO: we should actually only copy the portion of the source needed to apply the image
1334 // filter
1335 texture.reset(fContext->textureProvider()->createTexture(this->accessDrawContext()->desc(),
1336 SkBudgeted::kYes));
1337 if (!texture) {
1338 return nullptr;
1339 }
1340
reede51c3562016-07-19 14:33:20 -07001341 if (!fContext->copySurface(texture.get(), this->accessDrawContext()->accessRenderTarget())){
robertphillips04d62182016-07-15 12:21:33 -07001342 return nullptr;
1343 }
robertphillips1b5f9682016-07-15 08:01:12 -07001344 }
1345
1346 const SkImageInfo ii = this->imageInfo();
1347 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1348
1349 return SkSpecialImage::MakeFromGpu(srcRect,
1350 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -07001351 std::move(texture),
1352 sk_ref_sp(ii.colorSpace()),
robertphillips1b5f9682016-07-15 08:01:12 -07001353 &this->surfaceProps());
1354}
1355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001356void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001357 int left, int top, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001358 SkASSERT(!paint.getImageFilter());
1359
joshualittce894002016-01-11 13:29:31 -08001360 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001362 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
kkinnunen2e4414e2015-02-19 07:20:40 -08001363
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001365 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366
robertphillips1b5f9682016-07-15 08:01:12 -07001367 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001368 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001369 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 return;
1371 }
1372
robertphillips1b5f9682016-07-15 08:01:12 -07001373 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001374}
1375
reeda85d4d02015-05-06 12:56:48 -07001376void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1377 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001378 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001379 SkMatrix viewMatrix = *draw.fMatrix;
1380 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001381 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001382 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001383 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001384 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1385 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001386 return;
reed85d91782015-09-10 14:33:38 -07001387 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001388 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001389 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1390 paint.getFilterQuality(), *draw.fMatrix)) {
1391 // only support tiling as bitmap at the moment, so force raster-version
1392 if (!as_IB(image)->getROPixels(&bm)) {
1393 return;
1394 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001395 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1396 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1397 CHECK_SHOULD_DRAW(draw);
1398 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001399 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1400 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001401 } else if (as_IB(image)->getROPixels(&bm)) {
1402 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001403 }
reeda85d4d02015-05-06 12:56:48 -07001404 }
1405}
1406
1407void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001408 const SkRect& dst, const SkPaint& paint,
1409 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001410 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001411 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001412 CHECK_SHOULD_DRAW(draw);
1413 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001414 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001415 return;
1416 }
1417 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001418 SkMatrix totalMatrix = *draw.fMatrix;
1419 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1420 dst.height() / (src ? src->height() : image->height()));
1421 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001422 // only support tiling as bitmap at the moment, so force raster-version
1423 if (!as_IB(image)->getROPixels(&bm)) {
1424 return;
1425 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001426 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1427 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1428 CHECK_SHOULD_DRAW(draw);
1429 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001430 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001431 } else if (as_IB(image)->getROPixels(&bm)) {
1432 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001433 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001434}
1435
bsalomonf1ecd212015-12-09 17:06:02 -08001436void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001437 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001438 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001439
joshualitt33a5fce2015-11-18 13:28:51 -08001440 CHECK_SHOULD_DRAW(draw);
1441
joshualittedb36442015-11-19 14:29:30 -08001442 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001443 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001444 bool doBicubic;
1445 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001446 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1447 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001448 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001449 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001450
1451 SkRect srcR, dstR;
1452 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001453 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001454 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001455 }
1456 return;
1457 }
1458
bsalomon2bbd0c62015-12-09 12:50:56 -08001459 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
bungeman06ca8ec2016-06-09 08:01:03 -07001460 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001461 producer->createFragmentProcessor(SkMatrix::I(),
1462 SkRect::MakeIWH(producer->width(), producer->height()),
1463 GrTextureProducer::kNo_FilterConstraint, true,
brianosman8fe485b2016-07-25 12:31:51 -07001464 &kMode, fDrawContext->getColorSpace(),
1465 fDrawContext->sourceGammaTreatment()));
joshualitt33a5fce2015-11-18 13:28:51 -08001466 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001467 if (!SkPaintToGrPaintWithTexture(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1468 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001469 return;
1470 }
1471
bsalomon2bbd0c62015-12-09 12:50:56 -08001472 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1473 producer->height(), center, dst);
1474}
1475
1476void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1477 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001478 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001479 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001480 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001481 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001482 } else {
1483 SkBitmap bm;
1484 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1485 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001486 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001487 } else if (as_IB(image)->getROPixels(&bm)) {
1488 this->drawBitmapNine(draw, bm, center, dst, paint);
1489 }
1490 }
1491}
1492
1493void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1494 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001495 ASSERT_SINGLE_OWNER
reedc7ec7c92016-07-25 08:29:10 -07001496 GrBitmapTextureMaker maker(fContext, bitmap);
1497 this->drawProducerNine(draw, &maker, center, dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001498}
1499
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500///////////////////////////////////////////////////////////////////////////////
1501
1502// must be in SkCanvas::VertexMode order
1503static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1504 kTriangles_GrPrimitiveType,
1505 kTriangleStrip_GrPrimitiveType,
1506 kTriangleFan_GrPrimitiveType,
1507};
1508
1509void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1510 int vertexCount, const SkPoint vertices[],
1511 const SkPoint texs[], const SkColor colors[],
1512 SkXfermode* xmode,
1513 const uint16_t indices[], int indexCount,
1514 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001515 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001516 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001517 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001518
halcanary96fcdcc2015-08-27 07:41:13 -07001519 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1520 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001521
halcanary96fcdcc2015-08-27 07:41:13 -07001522 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001523
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001524 SkPaint copy(paint);
1525 copy.setStyle(SkPaint::kStroke_Style);
1526 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001527
bsalomonf1b7a1d2015-09-28 06:26:28 -07001528 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001529 // we ignore the shader if texs is null.
brianosman8fe485b2016-07-25 12:31:51 -07001530 if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001531 return;
1532 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001533
dandov32a311b2014-07-15 19:46:26 -07001534 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001535 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001536 switch (vmode) {
1537 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001538 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001539 break;
1540 case SkCanvas::kTriangleStrip_VertexMode:
1541 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001542 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001543 break;
1544 }
mtklein533eb782014-08-27 10:39:42 -07001545
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001546 VertState state(vertexCount, indices, indexCount);
1547 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001548
dandov32a311b2014-07-15 19:46:26 -07001549 //number of indices for lines per triangle with kLines
1550 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001551
bsalomonf1b7a1d2015-09-28 06:26:28 -07001552 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001553 int i = 0;
1554 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001555 lineIndices[i] = state.f0;
1556 lineIndices[i + 1] = state.f1;
1557 lineIndices[i + 2] = state.f1;
1558 lineIndices[i + 3] = state.f2;
1559 lineIndices[i + 4] = state.f2;
1560 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001561 i += 6;
1562 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001563 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001564 grPaint,
1565 *draw.fMatrix,
1566 kLines_GrPrimitiveType,
1567 vertexCount,
1568 vertices,
1569 texs,
1570 colors,
1571 lineIndices.get(),
1572 indexCount);
1573 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001574 }
1575
bsalomonf1b7a1d2015-09-28 06:26:28 -07001576 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577
1578 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001579 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001580 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1581 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582 convertedColors.reset(vertexCount);
1583 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001584 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001585 }
1586 colors = convertedColors.get();
1587 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001588 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001589 if (texs && paint.getShader()) {
1590 if (colors) {
1591 // When there are texs and colors the shader and colors are combined using xmode. A null
1592 // xmode is defined to mean modulate.
1593 SkXfermode::Mode colorMode;
1594 if (xmode) {
1595 if (!xmode->asMode(&colorMode)) {
1596 return;
1597 }
1598 } else {
1599 colorMode = SkXfermode::kModulate_Mode;
1600 }
brianosman8fe485b2016-07-25 12:31:51 -07001601 if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), paint,
1602 *draw.fMatrix, colorMode, false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001603 return;
1604 }
1605 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001606 // We have a shader, but no colors to blend it against.
brianosman8fe485b2016-07-25 12:31:51 -07001607 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix,
1608 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001609 return;
1610 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001611 }
bsalomonaa48d362015-10-01 08:34:17 -07001612 } else {
1613 if (colors) {
1614 // We have colors, but either have no shader or no texture coords (which implies that
1615 // we should ignore the shader).
brianosman8fe485b2016-07-25 12:31:51 -07001616 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fDrawContext.get(), paint,
1617 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001618 return;
1619 }
1620 } else {
1621 // No colors and no shaders. Just draw with the paint color.
brianosman8fe485b2016-07-25 12:31:51 -07001622 if (!SkPaintToGrPaintNoShader(this->context(), fDrawContext.get(), paint, &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001623 return;
1624 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001625 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001626 }
1627
robertphillips2e1e51f2015-10-15 08:01:48 -07001628 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001629 grPaint,
1630 *draw.fMatrix,
1631 primType,
1632 vertexCount,
1633 vertices,
1634 texs,
1635 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001636 indices,
robertphillipsea461502015-05-26 11:38:03 -07001637 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638}
1639
1640///////////////////////////////////////////////////////////////////////////////
1641
jvanverth31ff7622015-08-07 10:09:28 -07001642void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001643 const SkRect texRect[], const SkColor colors[], int count,
1644 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001645 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001646 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001647 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001648 return;
1649 }
1650
jvanverth31ff7622015-08-07 10:09:28 -07001651 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001652 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001653
reedca109532015-06-25 16:25:25 -07001654 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001655 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001656
jvanverth31ff7622015-08-07 10:09:28 -07001657 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001658 if (colors) {
brianosman8fe485b2016-07-25 12:31:51 -07001659 if (!SkPaintToGrPaintWithXfermode(this->context(), fDrawContext.get(), p, *draw.fMatrix,
1660 mode, true, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001661 return;
1662 }
1663 } else {
brianosman8fe485b2016-07-25 12:31:51 -07001664 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001665 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001666 }
1667 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001668
1669 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001670 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001671}
1672
1673///////////////////////////////////////////////////////////////////////////////
1674
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001676 size_t byteLength, SkScalar x, SkScalar y,
1677 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001678 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001679 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001680 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681
jvanverth8c27a182014-10-14 08:45:50 -07001682 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001683 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001684 return;
1685 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001686
jvanverth8c27a182014-10-14 08:45:50 -07001687 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001688
robertphillips2e1e51f2015-10-15 08:01:48 -07001689 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001690 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691}
1692
fmalita05c4a432014-09-29 06:29:53 -07001693void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1694 const SkScalar pos[], int scalarsPerPos,
1695 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001696 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001697 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001698 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001699
jvanverth8c27a182014-10-14 08:45:50 -07001700 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -07001701 if (!SkPaintToGrPaint(this->context(), fDrawContext.get(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001702 return;
1703 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001704
jvanverth8c27a182014-10-14 08:45:50 -07001705 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001706
robertphillips2e1e51f2015-10-15 08:01:48 -07001707 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001708 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001709 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001710}
1711
joshualitt9c328182015-03-23 08:13:04 -07001712void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1713 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001714 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001715 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001716 CHECK_SHOULD_DRAW(draw);
1717
1718 SkDEBUGCODE(this->validate();)
1719
robertphillips2e1e51f2015-10-15 08:01:48 -07001720 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001721 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001722}
1723
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724///////////////////////////////////////////////////////////////////////////////
1725
reedb2db8982014-11-13 12:41:02 -08001726bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001727 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001728}
1729
1730void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001731 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001732
robertphillips8c523e02016-07-26 07:41:00 -07001733 fDrawContext->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001734}
1735
1736///////////////////////////////////////////////////////////////////////////////
1737
reed76033be2015-03-14 10:54:31 -07001738SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001739 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740
robertphillipsca6eafc2016-05-17 09:57:46 -07001741 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742
robertphillipsca6eafc2016-05-17 09:57:46 -07001743 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001744 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001745 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1746 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001747
robertphillipsca6eafc2016-05-17 09:57:46 -07001748 sk_sp<GrDrawContext> dc(fContext->newDrawContext(fit,
1749 cinfo.fInfo.width(), cinfo.fInfo.height(),
1750 fDrawContext->config(),
brianosmandfe4f2e2016-07-21 13:28:36 -07001751 sk_ref_sp(fDrawContext->getColorSpace()),
robertphillipsca6eafc2016-05-17 09:57:46 -07001752 fDrawContext->desc().fSampleCnt,
1753 kDefault_GrSurfaceOrigin,
1754 &props));
1755 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001756 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001757 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001758 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001759 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001760 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001761
1762 // Skia's convention is to only clear a device if it is non-opaque.
1763 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1764
1765 return SkGpuDevice::Make(std::move(dc),
1766 cinfo.fInfo.width(), cinfo.fInfo.height(),
1767 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768}
1769
reede8f30622016-03-23 18:59:25 -07001770sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001771 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001772 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001773 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001774 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
robertphillips7e922762016-07-26 11:38:17 -07001775 fDrawContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001776}
1777
senorblanco900c3672016-04-27 11:31:23 -07001778SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001779 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001780 // We always return a transient cache, so it is freed after each
1781 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001782 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001783}
reedf037e0b2014-10-30 11:34:15 -07001784
1785#endif