blob: c03526a2815277e9ce3ce96747c24900202ea8f4 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/SkGpuDevice.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkImageFilter.h"
11#include "include/core/SkPathEffect.h"
12#include "include/core/SkPicture.h"
13#include "include/core/SkSurface.h"
14#include "include/core/SkVertices.h"
15#include "include/gpu/GrContext.h"
16#include "include/private/SkImageInfoPriv.h"
17#include "include/private/SkShadowFlags.h"
18#include "include/private/SkTo.h"
19#include "src/core/SkCanvasPriv.h"
20#include "src/core/SkClipStack.h"
21#include "src/core/SkDraw.h"
22#include "src/core/SkImageFilterCache.h"
23#include "src/core/SkLatticeIter.h"
24#include "src/core/SkMakeUnique.h"
25#include "src/core/SkPictureData.h"
26#include "src/core/SkRRectPriv.h"
27#include "src/core/SkRasterClip.h"
28#include "src/core/SkRecord.h"
29#include "src/core/SkSpecialImage.h"
30#include "src/core/SkStroke.h"
31#include "src/core/SkTLazy.h"
32#include "src/core/SkVertState.h"
33#include "src/core/SkWritePixelsRec.h"
34#include "src/gpu/GrBitmapTextureMaker.h"
35#include "src/gpu/GrBlurUtils.h"
36#include "src/gpu/GrContextPriv.h"
37#include "src/gpu/GrGpu.h"
38#include "src/gpu/GrImageTextureMaker.h"
39#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrStyle.h"
41#include "src/gpu/GrSurfaceProxyPriv.h"
42#include "src/gpu/GrTextureAdjuster.h"
43#include "src/gpu/GrTracing.h"
44#include "src/gpu/SkGr.h"
45#include "src/gpu/effects/GrBicubicEffect.h"
46#include "src/gpu/effects/GrTextureDomain.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040047#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/text/GrTextTarget.h"
49#include "src/image/SkImage_Base.h"
50#include "src/image/SkReadPixelsRec.h"
51#include "src/image/SkSurface_Gpu.h"
52#include "src/utils/SkUTF.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053
joshualittce894002016-01-11 13:29:31 -080054#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050055SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->priv().singleOwner());)
joshualittce894002016-01-11 13:29:31 -080056
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058///////////////////////////////////////////////////////////////////////////////
59
bsalomon74f681d2015-06-23 14:38:48 -070060/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
61 should fail. */
62bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
63 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
64 *flags = 0;
65 if (info) {
66 switch (info->alphaType()) {
67 case kPremul_SkAlphaType:
68 break;
69 case kOpaque_SkAlphaType:
70 *flags |= SkGpuDevice::kIsOpaque_Flag;
71 break;
72 default: // If it is unpremul or unknown don't try to render
73 return false;
74 }
75 }
76 if (kClear_InitContents == init) {
77 *flags |= kNeedClear_Flag;
78 }
79 return true;
80}
81
Robert Phillips9fab7e92016-11-17 12:45:04 -050082sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
83 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070084 int width, int height,
85 InitContents init) {
Robert Phillips920d4882019-03-04 15:16:44 -050086 if (!renderTargetContext || context->priv().abandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070087 return nullptr;
88 }
89 unsigned flags;
90 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
91 return nullptr;
92 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050093 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
94 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000095}
96
robertphillips24e91282016-04-29 06:46:36 -070097sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
98 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -040099 GrSurfaceOrigin origin, const SkSurfaceProps* props,
100 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700101 unsigned flags;
102 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700103 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700104 }
105
Brian Osman11052242016-10-27 14:47:55 -0400106 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
107 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400108 origin, props,
109 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400110 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700112 }
113
Robert Phillips9fab7e92016-11-17 12:45:04 -0500114 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700115 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700116}
117
Brian Osman11052242016-10-27 14:47:55 -0400118static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
reed589a39e2016-08-20 07:59:19 -0700119 SkColorType colorType;
Brian Salomonf3569f02017-10-24 12:52:33 -0400120 if (!GrPixelConfigToColorType(context->colorSpaceInfo().config(), &colorType)) {
reed589a39e2016-08-20 07:59:19 -0700121 colorType = kUnknown_SkColorType;
122 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400123 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
124 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700125}
126
Robert Phillips9fab7e92016-11-17 12:45:04 -0500127SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
128 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400129 : INHERITED(make_info(renderTargetContext.get(), width, height,
130 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500131 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400132 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700133{
robertphillips1f3923e2016-07-21 07:17:54 -0700134 fSize.set(width, height);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135
bsalomone63ffef2016-02-05 07:17:34 -0800136 if (flags & kNeedClear_Flag) {
137 this->clearAll();
138 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139}
140
Brian Osman11052242016-10-27 14:47:55 -0400141sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
142 GrContext* context,
143 SkBudgeted budgeted,
144 const SkImageInfo& origInfo,
145 int sampleCount,
146 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400147 const SkSurfaceProps* surfaceProps,
148 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000149 if (kUnknown_SkColorType == origInfo.colorType() ||
150 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700151 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000152 }
153
bsalomonafe30052015-01-16 07:32:33 -0800154 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800156 }
157
Brian Osman2b23c4b2018-06-01 12:25:08 -0400158 GrPixelConfig config = SkImageInfo2GrPixelConfig(origInfo);
Greg Daniel0a7aa142018-02-21 13:02:32 -0500159 if (kUnknown_GrPixelConfig == config) {
160 return nullptr;
161 }
Greg Daniel4065d452018-11-16 15:43:41 -0500162 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500163 context->priv().caps()->getBackendFormatFromColorType(origInfo.colorType());
Greg Danielfa55f2e2019-06-13 15:21:38 -0400164 if (!format.isValid()) {
165 return nullptr;
166 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400167 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
168 // they need to be exact.
Robert Phillips9da87e02019-02-04 13:26:26 -0500169 return context->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400170 format, SkBackingFit::kExact, origInfo.width(), origInfo.height(), config,
171 SkColorTypeToGrColorType(origInfo.colorType()), origInfo.refColorSpace(), sampleCount,
172 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800173}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000174
Mike Reeda1361362017-03-07 09:37:29 -0500175sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700176 int left, int top,
177 SkIPoint* offset,
178 const SkImageFilter* filter) {
179 SkASSERT(srcImg->isTextureBacked());
180 SkASSERT(filter);
181
Mike Reeda1361362017-03-07 09:37:29 -0500182 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700183 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500184 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500185 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -0400186 SkColorType colorType;
187 if (!GrPixelConfigToColorType(fRenderTargetContext->colorSpaceInfo().config(), &colorType)) {
188 colorType = kN32_SkColorType;
189 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400190 SkImageFilter::OutputProperties outputProperties(
Brian Osmana50205f2018-07-06 13:57:01 -0400191 colorType, fRenderTargetContext->colorSpaceInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700192 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
robertphillips970587b2016-07-14 14:12:55 -0700193
194 return filter->filterImage(srcImg, ctx, offset);
195}
196
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000197///////////////////////////////////////////////////////////////////////////////
198
Mike Reed353196f2017-07-21 11:01:18 -0400199bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800200 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000201
Mike Reed353196f2017-07-21 11:01:18 -0400202 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500203 return false;
204 }
205
Mike Reed353196f2017-07-21 11:01:18 -0400206 SkReadPixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500207 if (!rec.trim(this->width(), this->height())) {
208 return false;
209 }
210
211 return fRenderTargetContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000212}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213
Mike Reed353196f2017-07-21 11:01:18 -0400214bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800215 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700216
Mike Reed353196f2017-07-21 11:01:18 -0400217 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500218 return false;
219 }
220
Mike Reed353196f2017-07-21 11:01:18 -0400221 SkWritePixelsRec rec(pm, x, y);
Matt Sarett03dd6d52017-01-23 12:15:09 -0500222 if (!rec.trim(this->width(), this->height())) {
223 return false;
224 }
225
226 return fRenderTargetContext->writePixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000227}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228
reed41e010c2015-06-09 12:16:53 -0700229bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800230 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700231 return false;
232}
233
Brian Osman11052242016-10-27 14:47:55 -0400234GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700235 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400236 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700237}
238
reed8eddfb52014-12-04 07:50:14 -0800239void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800240 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500241 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500242
reed8eddfb52014-12-04 07:50:14 -0800243 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman9a9baae2018-11-05 15:06:26 -0500244 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
245 GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800246}
247
Brian Salomonaad83152019-05-24 10:16:35 -0400248void SkGpuDevice::replaceRenderTargetContext(sk_sp<GrRenderTargetContext> rtc,
249 bool shouldRetainContent) {
250 SkASSERT(rtc->width() == this->width());
251 SkASSERT(rtc->height() == this->height());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600252 SkASSERT(rtc->numSamples() == fRenderTargetContext->numSamples());
Brian Salomonaad83152019-05-24 10:16:35 -0400253 SkASSERT(rtc->asSurfaceProxy()->priv().isExact());
254 if (shouldRetainContent) {
255 if (this->context()->abandoned()) {
256 return;
257 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400258
259 SkASSERT(fRenderTargetContext->asTextureProxy());
260 SkAssertResult(rtc->blitTexture(fRenderTargetContext->asTextureProxy(),
261 SkIRect::MakeWH(this->width(), this->height()),
262 SkIPoint::Make(0,0)));
Brian Salomonaad83152019-05-24 10:16:35 -0400263 }
264
265 fRenderTargetContext = std::move(rtc);
266}
267
Brian Osman11052242016-10-27 14:47:55 -0400268void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800269 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800270
Brian Osman693a5402016-10-27 15:13:22 -0400271 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800272
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400273 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
274 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400275 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400276 this->context(),
277 budgeted,
278 this->imageInfo(),
Chris Dalton6ce447a2019-06-23 18:07:38 -0600279 fRenderTargetContext->numSamples(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400280 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400281 &this->surfaceProps(),
282 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400283 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800284 return;
285 }
Brian Salomonaad83152019-05-24 10:16:35 -0400286 this->replaceRenderTargetContext(std::move(newRTC), shouldRetainContent);
kkinnunenabcfab42015-02-22 22:53:44 -0800287}
288
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289///////////////////////////////////////////////////////////////////////////////
290
Mike Reeda1361362017-03-07 09:37:29 -0500291void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800292 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500293 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294
295 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400296 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
297 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700298 return;
299 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300
Brian Salomon0166cfd2017-03-13 17:58:25 -0400301 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302}
303
Brian Salomon1459ebd2017-12-19 16:12:44 -0500304static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
305 switch (mode) {
306 case SkCanvas::kPoints_PointMode:
307 return GrPrimitiveType::kPoints;
308 case SkCanvas::kLines_PointMode:
309 return GrPrimitiveType::kLines;
310 case SkCanvas::kPolygon_PointMode:
311 return GrPrimitiveType::kLineStrip;
312 }
313 SK_ABORT("Unexpected mode");
314 return GrPrimitiveType::kPoints;
315}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316
Mike Reeda1361362017-03-07 09:37:29 -0500317void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800319 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500320 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321 SkScalar width = paint.getStrokeWidth();
322 if (width < 0) {
323 return;
324 }
325
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000326 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700327 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700328 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400329 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
330 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700331 return;
332 }
egdaniele61c4112014-06-12 10:24:21 -0700333 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700334 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700335 path.moveTo(pts[0]);
336 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700337 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
338 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700339 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000340 }
341
bsalomon6ade6dd2016-09-12 12:07:17 -0700342 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500343 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700344 SkScalarNearlyEqual(scales[0], 1.f) &&
345 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700346 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400348 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500349 SkRasterClip rc(this->devClipBounds());
350 SkDraw draw;
351 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
352 draw.fMatrix = &this->ctm();
353 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500354 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355 return;
356 }
357
Brian Salomon1459ebd2017-12-19 16:12:44 -0500358 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700359
Mike Reeda1361362017-03-07 09:37:29 -0500360 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700361#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
362 // This offsetting in device space matches the expectations of the Android framework for non-AA
363 // points and lines.
364 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600365 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700366 tempMatrix = *viewMatrix;
367 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
368 tempMatrix.postTranslate(kOffset, kOffset);
369 viewMatrix = &tempMatrix;
370 }
371#endif
372
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400374 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
375 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700376 return;
377 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378
Brian Osmanae0c50c2017-05-25 16:56:34 -0400379 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
380 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
381 nullptr);
382
383 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400384 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385}
386
387///////////////////////////////////////////////////////////////////////////////
388
Mike Reeda1361362017-03-07 09:37:29 -0500389void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800390 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500391 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400392
393 GrStyle style(paint);
394
bsalomona7d85ba2016-07-06 11:54:59 -0700395 // A couple reasons we might need to call drawPath.
396 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400397 GrShape shape(rect, style);
398
399 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
400 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401 return;
402 }
403
404 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400405 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
406 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700407 return;
408 }
Mike Klein744fb732014-06-23 15:13:26 -0400409
Chris Dalton3b51df12017-11-27 14:33:06 -0700410 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
411 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412}
413
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400414void SkGpuDevice::drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
415 SkCanvas::QuadAAFlags aaFlags, SkColor color, SkBlendMode mode) {
Michael Ludwig75451902019-01-23 11:14:29 -0500416 ASSERT_SINGLE_OWNER
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400417 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawEdgeAAQuad", fContext.get());
Michael Ludwig75451902019-01-23 11:14:29 -0500418
419 SkPMColor4f dstColor = SkColor4fPrepForDst(SkColor4f::FromColor(color),
Brian Osman8fa7ab42019-03-18 10:22:42 -0400420 fRenderTargetContext->colorSpaceInfo())
Michael Ludwig75451902019-01-23 11:14:29 -0500421 .premul();
422
423 GrPaint grPaint;
424 grPaint.setColor4f(dstColor);
425 if (mode != SkBlendMode::kSrcOver) {
426 grPaint.setXPFactory(SkBlendMode_AsXPFactory(mode));
427 }
428
Michael Ludwig136f45a2019-02-19 11:44:41 -0500429 // This is exclusively meant for tiling operations, so keep AA enabled to handle MSAA seaming
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500430 GrQuadAAFlags grAA = SkToGrQuadAAFlags(aaFlags);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400431 if (clip) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500432 // Use fillQuadWithEdgeAA
433 fRenderTargetContext->fillQuadWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
434 this->ctm(), clip, nullptr);
435 } else {
436 // Use fillRectWithEdgeAA to preserve mathematical properties of dst being rectangular
437 fRenderTargetContext->fillRectWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
438 this->ctm(), rect);
439 }
Michael Ludwig75451902019-01-23 11:14:29 -0500440}
441
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000442///////////////////////////////////////////////////////////////////////////////
443
Mike Reeda1361362017-03-07 09:37:29 -0500444void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800445 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500446 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400447
Mike Reed80747ef2018-01-23 15:29:32 -0500448 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000449 if (mf) {
450 if (mf->hasFragmentProcessor()) {
451 mf = nullptr; // already handled in SkPaintToGrPaint
452 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400453 }
454
bsalomon6663acf2016-05-10 09:14:17 -0700455 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000456
Robert Phillipsa29a9562016-10-20 09:40:55 -0400457 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800458 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400459 GrShape shape(rrect, style);
460
Robert Phillips27927a52018-08-20 13:18:12 -0400461 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
462 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000463 return;
464 }
Mike Klein744fb732014-06-23 15:13:26 -0400465
bsalomon6663acf2016-05-10 09:14:17 -0700466 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800467
Robert Phillipsa522d662018-08-23 13:50:16 -0400468 GrPaint grPaint;
469 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
470 this->ctm(), &grPaint)) {
471 return;
472 }
473
Chris Dalton3b51df12017-11-27 14:33:06 -0700474 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
475 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000476}
477
robertphillipsd7706102016-02-25 09:28:08 -0800478
Robert Phillips20390c32018-08-17 11:01:03 -0400479void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800480 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500481 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800482 if (outer.isEmpty()) {
483 return;
484 }
485
486 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500487 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800488 }
489
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000490 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000491
robertphillips0e7029e2015-11-30 05:45:06 -0800492 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800493 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400494 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
495 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700496 return;
497 }
robertphillips00095892016-02-29 13:50:40 -0800498
Brian Salomon0166cfd2017-03-13 17:58:25 -0400499 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700500 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800501 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000502 }
503
504 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700505 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000506 path.addRRect(outer);
507 path.addRRect(inner);
508 path.setFillType(SkPath::kEvenOdd_FillType);
509
Robert Phillips27927a52018-08-20 13:18:12 -0400510 // TODO: We are losing the possible mutability of the path here but this should probably be
511 // fixed by upgrading GrShape to handle DRRects.
512 GrShape shape(path, paint);
513
514 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
515 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000516}
517
518
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000519/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520
Mike Reeda1361362017-03-07 09:37:29 -0500521void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700522 if (paint.getMaskFilter()) {
523 SkPath path;
524 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400525 path.setIsVolatile(true);
526 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700527 }
528
529 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400530 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
531 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700532 return;
533 }
534
Chris Dalton3b51df12017-11-27 14:33:06 -0700535 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
536 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700537}
538
Mike Reeda1361362017-03-07 09:37:29 -0500539void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800540 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500541 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800542
robertphillips514450c2015-11-24 05:36:02 -0800543 if (paint.getMaskFilter()) {
544 // The RRect path can handle special case blurring
545 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500546 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 }
548
549 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400550 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
551 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700552 return;
553 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554
Chris Dalton3b51df12017-11-27 14:33:06 -0700555 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
556 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557}
558
Mike Reeda1361362017-03-07 09:37:29 -0500559void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700560 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
561 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500562 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700563 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500564 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700565 return;
566 }
567 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400568 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
569 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700570 return;
571 }
572
Chris Dalton3b51df12017-11-27 14:33:06 -0700573 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500574 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500575 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700576}
577
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500578#include "include/core/SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000579
580///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700581void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700582 const SkPaint& origPaint) {
583 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500584 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400585 // Adding support for round capping would require a
586 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700587 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
588 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
589 SkASSERT(!origPaint.getPathEffect());
590 SkASSERT(!origPaint.getMaskFilter());
591
592 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
593 SkASSERT(halfWidth > 0);
594
595 SkVector v = points[1] - points[0];
596
597 SkScalar length = SkPoint::Normalize(&v);
598 if (!length) {
599 v.fX = 1.0f;
600 v.fY = 0.0f;
601 }
602
603 SkPaint newPaint(origPaint);
604 newPaint.setStyle(SkPaint::kFill_Style);
605
606 SkScalar xtraLength = 0.0f;
607 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
608 xtraLength = halfWidth;
609 }
610
611 SkPoint mid = points[0] + points[1];
612 mid.scale(0.5f);
613
614 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
615 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
616 SkMatrix m;
617 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
618
619 SkMatrix local = m;
620
Mike Reeda1361362017-03-07 09:37:29 -0500621 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700622
623 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400624 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
625 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700626 return;
627 }
628
Brian Salomon82f44312017-01-11 13:42:54 -0500629 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700630 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700631}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632
Robert Phillips20390c32018-08-17 11:01:03 -0400633void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800634 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400635 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700636 SkPoint points[2];
637 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
638 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500639 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700640 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500641 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700642 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500643 // Round capping support is currently disabled b.c. it would require a RRect
644 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500645 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700646 return;
647 }
648 }
robertphillipsff55b492015-11-24 07:56:59 -0800649 }
650
Hal Canary144caf52016-11-07 17:57:18 -0500651 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400652 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500653 GrPaint grPaint;
654 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
655 this->ctm(), &grPaint)) {
656 return;
657 }
658 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
659 this->ctm(), origSrcPath, GrStyle(paint));
660 return;
661 }
Robert Phillips27927a52018-08-20 13:18:12 -0400662
663 // TODO: losing possible mutability of 'origSrcPath' here
664 GrShape shape(origSrcPath, paint);
665
666 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
667 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668}
669
670static const int kBmpSmallTileSize = 1 << 10;
671
672static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
673 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
674 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
675 return tilesX * tilesY;
676}
677
reed85d91782015-09-10 14:33:38 -0700678static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000679 if (maxTileSize <= kBmpSmallTileSize) {
680 return maxTileSize;
681 }
682
683 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
684 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
685
686 maxTileTotalTileSize *= maxTileSize * maxTileSize;
687 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
688
689 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
690 return kBmpSmallTileSize;
691 } else {
692 return maxTileSize;
693 }
694}
695
696// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
697// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700698static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800699 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800700 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700701 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700702 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703 const SkRect* srcRectPtr,
704 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700705 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700706 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
707 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708 clippedSrcIRect->setEmpty();
709 return;
710 }
711 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
712 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700713 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 if (!clippedSrcRect.intersect(*srcRectPtr)) {
715 clippedSrcIRect->setEmpty();
716 return;
717 }
718 }
719 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700720 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 if (!clippedSrcIRect->intersect(bmpBounds)) {
722 clippedSrcIRect->setEmpty();
723 }
724}
725
Robert Phillips920d4882019-03-04 15:16:44 -0500726const GrCaps* SkGpuDevice::caps() const {
727 return fContext->priv().caps();
728}
729
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400730bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
731 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700732 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700733 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400734 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700735 const SkRect* srcRectPtr,
736 int maxTileSize,
737 int* tileSize,
738 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800739 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700740 // if it's larger than the max tile size, then we have no choice but tiling.
741 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400742 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400743 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
744 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700745 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
746 return true;
747 }
748
bsalomon1a1d0b82015-10-16 07:49:42 -0700749 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700750 const size_t area = imageRect.width() * imageRect.height();
751 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
752 return false;
753 }
754
reed85d91782015-09-10 14:33:38 -0700755 // At this point we know we could do the draw by uploading the entire bitmap
756 // as a texture. However, if the texture would be large compared to the
757 // cache size and we don't require most of it for this draw then tile to
758 // reduce the amount of upload and cache spill.
759
760 // assumption here is that sw bitmap size is a good proxy for its size as
761 // a texture
762 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
763 size_t cacheSize;
764 fContext->getResourceCacheLimits(nullptr, &cacheSize);
765 if (bmpSize < cacheSize / 2) {
766 return false;
767 }
768
bsalomon1a1d0b82015-10-16 07:49:42 -0700769 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
770 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400771 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
772 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400773 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700774 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
775 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500776 kBmpSmallTileSize * kBmpSmallTileSize *
777 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700778
Brian Osmand05cdc32017-02-06 13:24:47 -0500779 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700780}
781
reed85d91782015-09-10 14:33:38 -0700782bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
783 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700784 const SkMatrix& viewMatrix,
785 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800786 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400787 // If image is explicitly texture backed then we shouldn't get here.
788 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400790 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700791 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500792 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500793 quality, viewMatrix, srcToDstRect,
Robert Phillips9da87e02019-02-04 13:26:26 -0500794 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700795
796 int tileFilterPad;
797 if (doBicubic) {
798 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400799 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700800 tileFilterPad = 0;
801 } else {
802 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400804 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400806 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807
reed85d91782015-09-10 14:33:38 -0700808 // these are output, which we safely ignore, as we just want to know the predicate
809 int outTileSize;
810 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811
bsalomone553b642016-08-17 09:02:09 -0700812 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400813 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700814 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815}
816
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000817// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
819// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000820static inline void clamped_outset_with_offset(SkIRect* iRect,
821 int outset,
822 SkPoint* offset,
823 const SkIRect& clamp) {
824 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000826 int leftClampDelta = clamp.fLeft - iRect->fLeft;
827 if (leftClampDelta > 0) {
828 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829 iRect->fLeft = clamp.fLeft;
830 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000831 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000833
834 int topClampDelta = clamp.fTop - iRect->fTop;
835 if (topClampDelta > 0) {
836 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837 iRect->fTop = clamp.fTop;
838 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000839 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840 }
841
842 if (iRect->fRight > clamp.fRight) {
843 iRect->fRight = clamp.fRight;
844 }
845 if (iRect->fBottom > clamp.fBottom) {
846 iRect->fBottom = clamp.fBottom;
847 }
848}
849
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000850// Break 'bitmap' into several tiles to draw it since it has already
851// been determined to be too large to fit in VRAM
852void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800853 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700854 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855 const SkRect& srcRect,
856 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400857 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800858 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700859 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000860 int tileSize,
861 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800862 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800863
ericrk983294f2016-04-18 09:14:00 -0700864 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800865 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
Michael Ludwig3e2cc062019-02-19 12:11:40 -0500866 LogDrawScaleFactor(viewMatrix, SkMatrix::I(), origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800867
bsalomonc55271f2015-11-09 11:55:57 -0800868 const SkPaint* paint = &origPaint;
869 SkPaint tempPaint;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600870 if (origPaint.isAntiAlias() && fRenderTargetContext->numSamples() <= 1) {
bsalomonc55271f2015-11-09 11:55:57 -0800871 // Drop antialiasing to avoid seams at tile boundaries.
872 tempPaint = origPaint;
873 tempPaint.setAntiAlias(false);
874 paint = &tempPaint;
875 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
877
878 int nx = bitmap.width() / tileSize;
879 int ny = bitmap.height() / tileSize;
880 for (int x = 0; x <= nx; x++) {
881 for (int y = 0; y <= ny; y++) {
882 SkRect tileR;
883 tileR.set(SkIntToScalar(x * tileSize),
884 SkIntToScalar(y * tileSize),
885 SkIntToScalar((x + 1) * tileSize),
886 SkIntToScalar((y + 1) * tileSize));
887
888 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
889 continue;
890 }
891
892 if (!tileR.intersect(srcRect)) {
893 continue;
894 }
895
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000896 SkIRect iTileR;
897 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700898 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
899 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500900 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700901 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400902 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000903 SkIRect iClampRect;
904
reeda5517e22015-07-14 10:54:12 -0700905 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 // In bleed mode we want to always expand the tile on all edges
907 // but stay within the bitmap bounds
908 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
909 } else {
910 // In texture-domain/clamp mode we only want to expand the
911 // tile on edges interior to "srcRect" (i.e., we want to
912 // not bleed across the original clamped edges)
913 srcRect.roundOut(&iClampRect);
914 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000915 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
916 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000917 }
918
bsalomone553b642016-08-17 09:02:09 -0700919 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 if (bitmap.extractSubset(&tmpB, iTileR)) {
921 // now offset it to make it "local" to our tmp bitmap
922 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800923 // de-optimized this determination
924 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700925 this->drawBitmapTile(tmpB,
926 viewMatrix,
927 rectToDraw,
928 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500929 params,
bsalomone553b642016-08-17 09:02:09 -0700930 *paint,
931 constraint,
932 bicubic,
933 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 }
935 }
936 }
937}
938
bsalomone553b642016-08-17 09:02:09 -0700939void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
940 const SkMatrix& viewMatrix,
941 const SkRect& dstRect,
942 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400943 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700944 const SkPaint& paint,
945 SkCanvas::SrcRectConstraint constraint,
946 bool bicubic,
947 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800948 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400949 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
950 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700951 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400952 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
953 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400954 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955
Brian Salomon2a943df2018-05-04 13:43:19 -0400956 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400957 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400958 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400959 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 return;
961 }
bsalomone553b642016-08-17 09:02:09 -0700962
bsalomone553b642016-08-17 09:02:09 -0700963 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400964 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
965 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700966
967 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
968 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400969 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700970
reeda5517e22015-07-14 10:54:12 -0700971 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700973 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500975 domain.fLeft = srcRect.fLeft + 0.5f;
976 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500978 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 }
980 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500981 domain.fTop = srcRect.fTop + 0.5f;
982 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500984 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000986 if (bicubic) {
Brian Salomona86fc7a2019-05-28 20:42:58 -0400987 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomon1127c0b2019-06-13 20:22:10 +0000988 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain, kDir,
Brian Salomona86fc7a2019-05-28 20:42:58 -0400989 bitmap.alphaType());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000990 } else {
Brian Osman2240be92017-10-18 13:15:13 -0400991 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -0400992 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000993 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000994 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400995 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
996 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Salomona86fc7a2019-05-28 20:42:58 -0400997 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomon1127c0b2019-06-13 20:22:10 +0000998 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode, kDir, bitmap.alphaType());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 } else {
Brian Osman2240be92017-10-18 13:15:13 -04001000 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 }
1002
Brian Osman21fc5ce2018-08-27 20:36:19 +00001003 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001004 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -08001005 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001006 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
1007 viewMatrix, std::move(fp),
1008 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001009 return;
1010 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001012 // Coverage-based AA would cause seams between tiles.
Chris Dalton6ce447a2019-06-23 18:07:38 -06001013 GrAA aa = GrAA(paint.isAntiAlias() && fRenderTargetContext->numSamples() > 1);
Brian Salomon0166cfd2017-03-13 17:58:25 -04001014 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015}
1016
Mike Reeda1361362017-03-07 09:37:29 -05001017void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001018 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001019 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001020 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001021
Robert Phillips920d4882019-03-04 15:16:44 -05001022 if (fContext->priv().abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 return;
1024 }
1025
Robert Phillipse14d3052017-02-15 13:18:21 -05001026 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1027 if (!srcImg) {
1028 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001029 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001030
Florin Malita53f77bd2017-04-28 13:48:37 -04001031 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001032}
1033
1034
Robert Phillipsc100d482018-07-10 10:11:01 -04001035void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001036 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001037 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001038 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001039
robertphillips970587b2016-07-14 14:12:55 -07001040 sk_sp<SkSpecialImage> result;
1041 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001042 SkIPoint offset = { 0, 0 };
1043
1044 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001045 if (!result) {
1046 return;
1047 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001048
1049 left += offset.fX;
1050 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001051 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001052 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001053 }
1054
1055 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001056 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001057 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001058 return;
1059 }
robertphillips970587b2016-07-14 14:12:55 -07001060
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001061 const GrPixelConfig config = proxy->config();
1062
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001063 SkMatrix ctm = this->ctm();
1064 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1065
robertphillips970587b2016-07-14 14:12:55 -07001066 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001067 if (tmpUnfiltered.getMaskFilter()) {
Florin Malitac6c5ead2018-04-11 15:33:40 -04001068 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001069 }
1070
robertphillips970587b2016-07-14 14:12:55 -07001071 tmpUnfiltered.setImageFilter(nullptr);
1072
Brian Osman2240be92017-10-18 13:15:13 -04001073 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001074 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001075 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001076 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001077 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001078 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -04001079 if (paint.getColor4f().isOpaque()) {
1080 fp = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
1081 } else {
1082 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
1083 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001084 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001085
1086 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001087 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1088 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001089 return;
1090 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001091
robertphillips970587b2016-07-14 14:12:55 -07001092 const SkIRect& subset = result->subset();
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001093 SkRect dstRect = SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()));
1094 SkRect srcRect = SkRect::Make(subset);
1095 if (clipImage) {
1096 // Add the image as a simple texture effect applied to coverage. Accessing content outside
1097 // of the clip image should behave as if it were a decal (i.e. zero coverage). However, to
1098 // limit pixels touched and hardware checks, we draw the clip image geometry to get the
1099 // decal effect.
1100 GrSamplerState sampler = paint.getFilterQuality() > kNone_SkFilterQuality ?
1101 GrSamplerState::ClampBilerp() : GrSamplerState::ClampNearest();
1102 sk_sp<GrTextureProxy> clipProxy = as_IB(clipImage)->asTextureProxyRef(this->context(),
1103 sampler, nullptr);
1104 // Fold clip matrix into ctm
1105 ctm.preConcat(clipMatrix);
1106 SkMatrix inverseClipMatrix;
robertphillips970587b2016-07-14 14:12:55 -07001107
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001108 std::unique_ptr<GrFragmentProcessor> cfp;
1109 if (clipProxy && ctm.invert(&inverseClipMatrix)) {
1110 cfp = GrSimpleTextureEffect::Make(std::move(clipProxy), inverseClipMatrix, sampler);
1111 if (clipImage->colorType() != kAlpha_8_SkColorType) {
1112 cfp = GrFragmentProcessor::SwizzleOutput(std::move(cfp), GrSwizzle::AAAA());
1113 }
1114 }
1115
1116 if (cfp) {
1117 // If the grPaint already has coverage, this adds an additional stage that multiples
1118 // the image's alpha channel with the prior coverage.
1119 grPaint.addCoverageFragmentProcessor(std::move(cfp));
1120
1121 // Undo the offset that was needed for shader coord transforms to get the transform for
1122 // the actual drawn geometry.
1123 ctm.postTranslate(SkIntToScalar(left), SkIntToScalar(top));
1124 inverseClipMatrix.preTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1125 SkRect clipGeometry = SkRect::MakeWH(clipImage->width(), clipImage->height());
1126 if (!clipGeometry.contains(inverseClipMatrix.mapRect(dstRect))) {
1127 // Draw the clip geometry since it is smaller, using dstRect as an extra scissor
1128 SkClipStack clip(this->cs());
1129 clip.clipDevRect(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()),
1130 SkClipOp::kIntersect);
1131 SkMatrix local = SkMatrix::Concat(SkMatrix::MakeRectToRect(
1132 dstRect, srcRect, SkMatrix::kFill_ScaleToFit), ctm);
1133 fRenderTargetContext->fillRectWithLocalMatrix(GrClipStackClip(&clip),
1134 std::move(grPaint), GrAA(paint.isAntiAlias()), ctm, clipGeometry, local);
1135 return;
1136 }
1137 // Else fall through and draw the subset since that is contained in the clip geometry
1138 }
1139 // Else some issue configuring the coverage FP, so just draw without the clip mask image
1140 }
1141 // Draw directly in screen space, possibly with an extra coverage processor
1142 fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint),
1143 GrAA(paint.isAntiAlias()), SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144}
1145
Mike Reeda1361362017-03-07 09:37:29 -05001146void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001147 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001148 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001149 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001150 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1151 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1152 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1153 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1154 const SkRect* dst = &origDst;
1155 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001157 if (!src) {
1158 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001160
bsalomonb1b01992015-11-18 10:56:08 -08001161 SkMatrix srcToDstMatrix;
1162 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1163 return;
1164 }
1165 SkRect tmpSrc, tmpDst;
1166 if (src != &bmpBounds) {
1167 if (!bmpBounds.contains(*src)) {
1168 tmpSrc = *src;
1169 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 return; // nothing to draw
1171 }
bsalomonb1b01992015-11-18 10:56:08 -08001172 src = &tmpSrc;
1173 srcToDstMatrix.mapRect(&tmpDst, *src);
1174 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 }
1176 }
1177
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001178 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001179
bsalomonb1b01992015-11-18 10:56:08 -08001180 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1181 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Chris Dalton6ce447a2019-06-23 18:07:38 -06001182 bool useCoverageAA = fRenderTargetContext->numSamples() <= 1 &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001183 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1184 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001185
Brian Salomon7c8460e2017-05-12 11:36:10 -04001186 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001187
1188 if (!skipTileCheck) {
1189 int tileSize;
1190 SkIRect clippedSrcRect;
1191
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001192 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001193 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001194 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -05001195 paint.getFilterQuality(), this->ctm(), srcToDstMatrix,
Robert Phillips9da87e02019-02-04 13:26:26 -05001196 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001197
1198 int tileFilterPad;
1199
1200 if (doBicubic) {
1201 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001202 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001203 tileFilterPad = 0;
1204 } else {
1205 tileFilterPad = 1;
1206 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001207 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001208
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001209 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001210 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001211 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1212 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001213 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001214 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001215 return;
1216 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001217 }
Hal Canary144caf52016-11-07 17:57:18 -05001218 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001219 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220}
1221
robertphillips6451a0c2016-07-18 08:31:31 -07001222sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001223 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1224 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips9da87e02019-02-04 13:26:26 -05001225 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->priv().proxyProvider(),
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001226 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001227 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001228 return nullptr;
1229 }
1230
Robert Phillipse14d3052017-02-15 13:18:21 -05001231 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001232
Robert Phillipse14d3052017-02-15 13:18:21 -05001233 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1234 // the special image
1235 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1236 rect,
1237 bitmap.getGenerationID(),
1238 std::move(proxy),
1239 bitmap.refColorSpace(),
1240 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001241}
1242
reede51c3562016-07-19 14:33:20 -07001243sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001244 SkPixmap pm;
1245 if (image->isTextureBacked()) {
Robert Phillips6603a172019-03-05 12:35:44 -05001246 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(this->context());
robertphillips6451a0c2016-07-18 08:31:31 -07001247
Robert Phillips6de99042017-01-31 11:31:39 -05001248 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1249 SkIRect::MakeWH(image->width(), image->height()),
1250 image->uniqueID(),
1251 std::move(proxy),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001252 image->refColorSpace(),
Robert Phillips6de99042017-01-31 11:31:39 -05001253 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001254 } else if (image->peekPixels(&pm)) {
1255 SkBitmap bm;
1256
1257 bm.installPixels(pm);
1258 return this->makeSpecial(bm);
1259 } else {
1260 return nullptr;
1261 }
1262}
1263
1264sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Greg Danielbe7fc462019-01-03 16:40:42 -05001265 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1266 // since it would require us to make a copy of the underlying VkImage which we don't have access
1267 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1268 // command buffer.
1269 if (this->accessRenderTargetContext()->wrapsVkSecondaryCB()) {
1270 return nullptr;
1271 }
1272
Robert Phillips63c67462017-02-15 14:19:01 -05001273 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1274 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001275 // When the device doesn't have a texture, we create a temporary texture.
1276 // TODO: we should actually only copy the portion of the source needed to apply the image
1277 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001278 proxy = GrSurfaceProxy::Copy(fContext.get(),
1279 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001280 GrMipMapped::kNo,
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001281 SkBackingFit::kApprox,
Robert Phillips63c67462017-02-15 14:19:01 -05001282 SkBudgeted::kYes);
1283 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001284 return nullptr;
1285 }
robertphillips1b5f9682016-07-15 08:01:12 -07001286 }
1287
1288 const SkImageInfo ii = this->imageInfo();
1289 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1290
Robert Phillipse2f7d182016-12-15 09:23:05 -05001291 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1292 srcRect,
1293 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001294 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001295 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001296 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001297}
1298
Mike Reed148b7fd2018-12-18 17:38:18 -05001299sk_sp<SkSpecialImage> SkGpuDevice::snapBackImage(const SkIRect& subset) {
1300 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
Greg Danielbe7fc462019-01-03 16:40:42 -05001301
1302 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1303 // since it would require us to make a copy of the underlying VkImage which we don't have access
1304 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1305 // command buffer.
1306 if (rtc->wrapsVkSecondaryCB()) {
Mike Reed148b7fd2018-12-18 17:38:18 -05001307 return nullptr;
1308 }
1309
Greg Danielbe7fc462019-01-03 16:40:42 -05001310
Mike Reed148b7fd2018-12-18 17:38:18 -05001311 GrContext* ctx = this->context();
Greg Danielbe7fc462019-01-03 16:40:42 -05001312 SkASSERT(rtc->asSurfaceProxy());
Mike Reed148b7fd2018-12-18 17:38:18 -05001313
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001314 auto srcProxy =
1315 GrSurfaceProxy::Copy(ctx, rtc->asSurfaceProxy(), rtc->mipMapped(), subset,
1316 SkBackingFit::kApprox, rtc->asSurfaceProxy()->isBudgeted());
Mike Reed148b7fd2018-12-18 17:38:18 -05001317 if (!srcProxy) {
1318 return nullptr;
1319 }
1320
1321 // Note, can't move srcProxy since we also refer to this in the 2nd parameter
1322 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1323 SkIRect::MakeSize(srcProxy->isize()),
1324 kNeedNewImageUniqueID_SpecialImage,
1325 srcProxy,
1326 this->imageInfo().refColorSpace(),
1327 &this->surfaceProps());
1328}
1329
Mike Reeda1361362017-03-07 09:37:29 -05001330void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001331 int left, int top, const SkPaint& paint) {
1332 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001333
joshualittce894002016-01-11 13:29:31 -08001334 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001336 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001337
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001338 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001339 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001340 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001341 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 return;
1343 }
1344
Mike Reed2179b782018-02-07 11:59:57 -05001345 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346}
1347
Brian Salomon34169692017-08-28 15:32:01 -04001348void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1349 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001350 ASSERT_SINGLE_OWNER
Michael Ludwig1433cfd2019-02-27 17:12:30 -05001351 GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -05001352 this->drawImageQuad(image, src, &dst, nullptr, GrAA(paint.isAntiAlias()), aaFlags, nullptr,
1353 paint, constraint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001354}
1355
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001356// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1357static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1358 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1359 return GrSamplerState::Filter::kNearest;
1360 }
1361
1362 return GrSamplerState::Filter::kBilerp;
1363}
1364
Mike Reeda1361362017-03-07 09:37:29 -05001365void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001366 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001367 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001368 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001369 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips6603a172019-03-05 12:35:44 -05001370 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
1371 &pinnedUniqueID)) {
Brian Salomond6287472019-06-24 15:50:07 -04001372 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1373 SkColorTypeToGrColorType(image->colorType()), image->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001374 pinnedUniqueID, image->colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001375 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001376 } else {
1377 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001378 if (image->isLazyGenerated()) {
1379 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001380 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001381 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001382 GrBitmapTextureMaker maker(fContext.get(), bm);
1383 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001384 }
1385 }
1386}
1387
Mike Reeda1361362017-03-07 09:37:29 -05001388void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001389 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001390 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001391 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001392 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001393 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001394}
1395
Mike Reeda1361362017-03-07 09:37:29 -05001396void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001397 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1398 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001399 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001400 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001401
Brian Salomon2a943df2018-05-04 13:43:19 -04001402 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1403 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1404 }
msarett10e3d9b2016-08-18 15:46:03 -07001405 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001406 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1407 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001408 return;
1409 }
1410
Brian Salomon2a943df2018-05-04 13:43:19 -04001411 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1412 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Michael Ludwigddeed372019-02-20 16:50:10 -05001413 auto proxy = producer->refTextureProxyForParams(&filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001414 if (!proxy) {
1415 return;
1416 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001417 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1418 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001419
Brian Salomon0166cfd2017-03-13 17:58:25 -04001420 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001421 std::move(proxy), std::move(csxf), filter,
1422 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001423}
1424
Mike Reeda1361362017-03-07 09:37:29 -05001425void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001426 const SkCanvas::Lattice& lattice, const SkRect& dst,
1427 const SkPaint& paint) {
1428 ASSERT_SINGLE_OWNER
1429 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001430 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips6603a172019-03-05 12:35:44 -05001431 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
1432 &pinnedUniqueID)) {
Brian Salomond6287472019-06-24 15:50:07 -04001433 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1434 SkColorTypeToGrColorType(image->colorType()), image->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001435 pinnedUniqueID, image->colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001436 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001437 } else {
1438 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001439 if (image->isLazyGenerated()) {
1440 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001441 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001442 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001443 GrBitmapTextureMaker maker(fContext.get(), bm);
1444 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001445 }
1446 }
1447}
1448
Mike Reeda1361362017-03-07 09:37:29 -05001449void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001450 const SkCanvas::Lattice& lattice, const SkRect& dst,
1451 const SkPaint& paint) {
1452 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001453 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001454 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001455 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001456}
1457
Brian Salomonf3569f02017-10-24 12:52:33 -04001458static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1459 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001460 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001461 if (hasTexs && skPaint.getShader()) {
1462 if (hasColors) {
1463 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001464 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1465 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001466 } else {
1467 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001468 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001469 }
1470 } else {
1471 if (hasColors) {
1472 // We have colors, but either have no shader or no texture coords (which implies that
1473 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001474 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001475 } else {
1476 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001477 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001478 }
1479 }
1480}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001481
Mike Reed887cdf12017-04-03 11:11:09 -04001482void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001483 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001484 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001485 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001486 const uint16_t indices[], int indexCount,
1487 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001488 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001489 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001490
Mike Reed2f6b5a42017-03-19 15:04:17 -04001491 SkPaint copy(paint);
1492 copy.setStyle(SkPaint::kStroke_Style);
1493 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001494
Mike Reed2f6b5a42017-03-19 15:04:17 -04001495 GrPaint grPaint;
1496 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001497 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1498 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001499 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500 }
1501
Mike Reed2f6b5a42017-03-19 15:04:17 -04001502 int triangleCount = 0;
1503 int n = (nullptr == indices) ? vertexCount : indexCount;
1504 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001505 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001506 triangleCount = n / 3;
1507 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001508 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001509 triangleCount = n - 2;
1510 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001511 case SkVertices::kTriangleFan_VertexMode:
1512 SK_ABORT("Unexpected triangle fan.");
1513 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001514 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001515
Mike Reed2f6b5a42017-03-19 15:04:17 -04001516 VertState state(vertexCount, indices, indexCount);
1517 VertState::Proc vertProc = state.chooseProc(vmode);
1518
1519 //number of indices for lines per triangle with kLines
1520 indexCount = triangleCount * 6;
1521
Brian Osmanae0c50c2017-05-25 16:56:34 -04001522 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1523 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1524 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1525
1526 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001527 int i = 0;
1528 while (vertProc(&state)) {
1529 lineIndices[i] = state.f0;
1530 lineIndices[i + 1] = state.f1;
1531 lineIndices[i + 2] = state.f1;
1532 lineIndices[i + 3] = state.f2;
1533 lineIndices[i + 4] = state.f2;
1534 lineIndices[i + 5] = state.f0;
1535 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001536 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001537
Chris Dalton3809bab2017-06-13 10:55:06 -06001538 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001539 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001540 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001541 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001542 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001543 bones,
1544 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001545 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546}
1547
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001548void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1549 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001550 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001551 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001552
1553 SkASSERT(vertices);
1554 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001555 bool hasColors = vertices->hasColors();
1556 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001557 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001558 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001559 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001560 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1561 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001562 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001563 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001564 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1565 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001566 return;
1567 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001568 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001569 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1570 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001571}
1572
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573///////////////////////////////////////////////////////////////////////////////
1574
Jim Van Verth3af1af92017-05-18 15:06:54 -04001575void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1576
1577 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001578 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1579
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001580 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001581 // failed to find an accelerated case
1582 this->INHERITED::drawShadow(path, rec);
1583 }
1584}
1585
1586///////////////////////////////////////////////////////////////////////////////
1587
Brian Osman4d92b892019-03-24 00:53:23 +00001588void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
1589 const SkRect texRect[], const SkColor colors[], int count,
1590 SkBlendMode mode, const SkPaint& paint) {
1591 ASSERT_SINGLE_OWNER
Brian Osmanddba4e62019-03-25 09:52:17 -04001592 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawAtlas", fContext.get());
Brian Osman4d92b892019-03-24 00:53:23 +00001593
1594 SkPaint p(paint);
1595 p.setShader(atlas->makeShader());
1596
1597 GrPaint grPaint;
1598 if (colors) {
1599 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1600 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
1601 return;
1602 }
1603 } else {
1604 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1605 this->ctm(), &grPaint)) {
1606 return;
1607 }
1608 }
1609
1610 fRenderTargetContext->drawAtlas(
1611 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
1612}
1613
1614///////////////////////////////////////////////////////////////////////////////
1615
Herb Derbyb935cf82018-07-26 16:54:18 -04001616void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001617 ASSERT_SINGLE_OWNER
1618 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
Herb Derbyb983e6b2018-07-13 13:26:29 -04001619
Jim Van Verth87a30112018-09-24 16:13:58 -04001620 // Check for valid input
1621 const SkMatrix& ctm = this->ctm();
Mike Reed96345a22019-01-02 21:30:29 -05001622 if (!ctm.isFinite() || !glyphRunList.allFontsFinite()) {
Jim Van Verth87a30112018-09-24 16:13:58 -04001623 return;
1624 }
1625
1626 fRenderTargetContext->drawGlyphRunList(this->clip(), ctm, glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001627}
1628
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001629///////////////////////////////////////////////////////////////////////////////
1630
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001631void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
Robert Phillips4217ea72019-01-30 13:08:28 -05001632 GrBackendApi api = this->context()->backend();
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001633 if (GrBackendApi::kVulkan == api) {
1634 const SkMatrix& ctm = canvas->getTotalMatrix();
1635 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1636 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -05001637 drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(),
1638 this->imageInfo());
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001639 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001640 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1641 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001642 }
1643 }
1644 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1645}
1646
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001647
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001648///////////////////////////////////////////////////////////////////////////////
1649
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650void SkGpuDevice::flush() {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001651 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo());
Greg Daniela5cb7812017-06-16 09:45:32 -04001652}
1653
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001654GrSemaphoresSubmitted SkGpuDevice::flush(SkSurface::BackendSurfaceAccess access,
1655 const GrFlushInfo& info) {
joshualittce894002016-01-11 13:29:31 -08001656 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001657
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001658 return fRenderTargetContext->flush(access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001659}
1660
Greg Danielc64ee462017-06-15 16:59:49 -04001661bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001662 ASSERT_SINGLE_OWNER
1663
Greg Danielc64ee462017-06-15 16:59:49 -04001664 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665}
1666
1667///////////////////////////////////////////////////////////////////////////////
1668
reed76033be2015-03-14 10:54:31 -07001669SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001670 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001671
robertphillipsca6eafc2016-05-17 09:57:46 -07001672 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673
robertphillipsca6eafc2016-05-17 09:57:46 -07001674 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001675 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001676 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1677 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001678
Brian Osman10fc6fd2018-03-02 11:01:10 -05001679 GrPixelConfig config = fRenderTargetContext->colorSpaceInfo().config();
Greg Daniel4065d452018-11-16 15:43:41 -05001680 const GrBackendFormat& origFormat = fRenderTargetContext->asSurfaceProxy()->backendFormat();
1681 GrBackendFormat format = origFormat.makeTexture2D();
1682 if (!format.isValid()) {
1683 return nullptr;
1684 }
Brian Osman10fc6fd2018-03-02 11:01:10 -05001685 if (kRGBA_1010102_GrPixelConfig == config) {
1686 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1687 // channel in the layer.
1688 config = kRGBA_8888_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -05001689 format =
Robert Phillips9da87e02019-02-04 13:26:26 -05001690 fContext->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Osman10fc6fd2018-03-02 11:01:10 -05001691 }
1692
Robert Phillips9da87e02019-02-04 13:26:26 -05001693 sk_sp<GrRenderTargetContext> rtc(fContext->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -04001694 format,
1695 fit,
1696 cinfo.fInfo.width(),
1697 cinfo.fInfo.height(),
1698 config,
1699 fRenderTargetContext->colorSpaceInfo().colorType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001700 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Brian Salomond6287472019-06-24 15:50:07 -04001701 fRenderTargetContext->numSamples(),
1702 GrMipMapped::kNo,
1703 kBottomLeft_GrSurfaceOrigin,
1704 &props,
1705 SkBudgeted::kYes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001706 fRenderTargetContext->asSurfaceProxy()->isProtected() ? GrProtected::kYes
1707 : GrProtected::kNo));
Brian Osman11052242016-10-27 14:47:55 -04001708 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001709 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001710 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001711
1712 // Skia's convention is to only clear a device if it is non-opaque.
1713 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1714
Robert Phillips9fab7e92016-11-17 12:45:04 -05001715 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1716 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001717}
1718
reede8f30622016-03-23 18:59:25 -07001719sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001720 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001721 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001722 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001723 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001724 fRenderTargetContext->numSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001725 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001726}
1727
senorblanco900c3672016-04-27 11:31:23 -07001728SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001729 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001730 // We always return a transient cache, so it is freed after each
1731 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001732 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001733}
reedf037e0b2014-10-30 11:34:15 -07001734