blob: fb4c537ceff63e5c738872206e438f9eac0f42e3 [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"
Michael Ludwig8ee6cf32019-08-02 09:57:04 -040023#include "src/core/SkImageFilter_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkLatticeIter.h"
25#include "src/core/SkMakeUnique.h"
26#include "src/core/SkPictureData.h"
27#include "src/core/SkRRectPriv.h"
28#include "src/core/SkRasterClip.h"
29#include "src/core/SkRecord.h"
30#include "src/core/SkSpecialImage.h"
31#include "src/core/SkStroke.h"
32#include "src/core/SkTLazy.h"
33#include "src/core/SkVertState.h"
34#include "src/core/SkWritePixelsRec.h"
35#include "src/gpu/GrBitmapTextureMaker.h"
36#include "src/gpu/GrBlurUtils.h"
37#include "src/gpu/GrContextPriv.h"
38#include "src/gpu/GrGpu.h"
39#include "src/gpu/GrImageTextureMaker.h"
40#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrStyle.h"
42#include "src/gpu/GrSurfaceProxyPriv.h"
43#include "src/gpu/GrTextureAdjuster.h"
44#include "src/gpu/GrTracing.h"
45#include "src/gpu/SkGr.h"
46#include "src/gpu/effects/GrBicubicEffect.h"
47#include "src/gpu/effects/GrTextureDomain.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040048#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/text/GrTextTarget.h"
50#include "src/image/SkImage_Base.h"
51#include "src/image/SkReadPixelsRec.h"
52#include "src/image/SkSurface_Gpu.h"
53#include "src/utils/SkUTF.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054
joshualittce894002016-01-11 13:29:31 -080055#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050056SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->priv().singleOwner());)
joshualittce894002016-01-11 13:29:31 -080057
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059///////////////////////////////////////////////////////////////////////////////
60
bsalomon74f681d2015-06-23 14:38:48 -070061/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
62 should fail. */
63bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
64 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
65 *flags = 0;
66 if (info) {
67 switch (info->alphaType()) {
68 case kPremul_SkAlphaType:
69 break;
70 case kOpaque_SkAlphaType:
71 *flags |= SkGpuDevice::kIsOpaque_Flag;
72 break;
73 default: // If it is unpremul or unknown don't try to render
74 return false;
75 }
76 }
77 if (kClear_InitContents == init) {
78 *flags |= kNeedClear_Flag;
79 }
80 return true;
81}
82
Robert Phillips9fab7e92016-11-17 12:45:04 -050083sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context,
84 sk_sp<GrRenderTargetContext> renderTargetContext,
robertphillips15c42ca2016-08-04 08:45:02 -070085 int width, int height,
86 InitContents init) {
Robert Phillips920d4882019-03-04 15:16:44 -050087 if (!renderTargetContext || context->priv().abandoned()) {
robertphillipsca6eafc2016-05-17 09:57:46 -070088 return nullptr;
89 }
90 unsigned flags;
91 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
92 return nullptr;
93 }
Robert Phillips9fab7e92016-11-17 12:45:04 -050094 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
95 width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000096}
97
robertphillips24e91282016-04-29 06:46:36 -070098sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
99 const SkImageInfo& info, int sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400100 GrSurfaceOrigin origin, const SkSurfaceProps* props,
101 GrMipMapped mipMapped, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700102 unsigned flags;
103 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700105 }
106
Brian Osman11052242016-10-27 14:47:55 -0400107 sk_sp<GrRenderTargetContext> renderTargetContext(MakeRenderTargetContext(context, budgeted,
108 info, sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400109 origin, props,
110 mipMapped));
Brian Osman11052242016-10-27 14:47:55 -0400111 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700113 }
114
Robert Phillips9fab7e92016-11-17 12:45:04 -0500115 return sk_sp<SkGpuDevice>(new SkGpuDevice(context, std::move(renderTargetContext),
robertphillipsca6eafc2016-05-17 09:57:46 -0700116 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700117}
118
Brian Osman11052242016-10-27 14:47:55 -0400119static SkImageInfo make_info(GrRenderTargetContext* context, int w, int h, bool opaque) {
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400120 SkColorType colorType = GrColorTypeToSkColorType(context->colorSpaceInfo().colorType());
Brian Salomonf3569f02017-10-24 12:52:33 -0400121 return SkImageInfo::Make(w, h, colorType, opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType,
122 context->colorSpaceInfo().refColorSpace());
reed589a39e2016-08-20 07:59:19 -0700123}
124
Robert Phillips9fab7e92016-11-17 12:45:04 -0500125SkGpuDevice::SkGpuDevice(GrContext* context, sk_sp<GrRenderTargetContext> renderTargetContext,
126 int width, int height, unsigned flags)
Brian Osman11052242016-10-27 14:47:55 -0400127 : INHERITED(make_info(renderTargetContext.get(), width, height,
128 SkToBool(flags & kIsOpaque_Flag)), renderTargetContext->surfaceProps())
Robert Phillips9fab7e92016-11-17 12:45:04 -0500129 , fContext(SkRef(context))
Brian Osman11052242016-10-27 14:47:55 -0400130 , fRenderTargetContext(std::move(renderTargetContext))
reed589a39e2016-08-20 07:59:19 -0700131{
robertphillips1f3923e2016-07-21 07:17:54 -0700132 fSize.set(width, height);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133
bsalomone63ffef2016-02-05 07:17:34 -0800134 if (flags & kNeedClear_Flag) {
135 this->clearAll();
136 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137}
138
Brian Osman11052242016-10-27 14:47:55 -0400139sk_sp<GrRenderTargetContext> SkGpuDevice::MakeRenderTargetContext(
140 GrContext* context,
141 SkBudgeted budgeted,
142 const SkImageInfo& origInfo,
143 int sampleCount,
144 GrSurfaceOrigin origin,
Greg Daniele252f082017-10-23 16:05:23 -0400145 const SkSurfaceProps* surfaceProps,
146 GrMipMapped mipMapped) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000147 if (kUnknown_SkColorType == origInfo.colorType() ||
148 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700149 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000150 }
151
bsalomonafe30052015-01-16 07:32:33 -0800152 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700153 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800154 }
155
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400156 // This method is used to create SkGpuDevice's for SkSurface_Gpus. In this case
157 // they need to be exact.
Robert Phillips9da87e02019-02-04 13:26:26 -0500158 return context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400159 SkBackingFit::kExact, origInfo.width(), origInfo.height(),
Brian Salomond6287472019-06-24 15:50:07 -0400160 SkColorTypeToGrColorType(origInfo.colorType()), origInfo.refColorSpace(), sampleCount,
161 mipMapped, origin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800162}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000163
Mike Reeda1361362017-03-07 09:37:29 -0500164sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(SkSpecialImage* srcImg,
robertphillips970587b2016-07-14 14:12:55 -0700165 int left, int top,
166 SkIPoint* offset,
167 const SkImageFilter* filter) {
168 SkASSERT(srcImg->isTextureBacked());
169 SkASSERT(filter);
170
Mike Reeda1361362017-03-07 09:37:29 -0500171 SkMatrix matrix = this->ctm();
robertphillips970587b2016-07-14 14:12:55 -0700172 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
Mike Reeda1361362017-03-07 09:37:29 -0500173 const SkIRect clipBounds = this->devClipBounds().makeOffset(-left, -top);
Hal Canary144caf52016-11-07 17:57:18 -0500174 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400175 SkColorType colorType =
176 GrColorTypeToSkColorType(fRenderTargetContext->colorSpaceInfo().colorType());
177 if (colorType == kUnknown_SkColorType) {
178 colorType = kRGBA_8888_SkColorType;
Brian Osmana50205f2018-07-06 13:57:01 -0400179 }
Michael Ludwig03f9ca32019-08-14 14:35:15 -0400180 SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), colorType,
Michael Ludwige30a4852019-08-14 14:35:42 -0400181 fRenderTargetContext->colorSpaceInfo().colorSpace(), srcImg);
robertphillips970587b2016-07-14 14:12:55 -0700182
Michael Ludwige30a4852019-08-14 14:35:42 -0400183 return as_IFB(filter)->filterImage(ctx, offset);
robertphillips970587b2016-07-14 14:12:55 -0700184}
185
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000186///////////////////////////////////////////////////////////////////////////////
187
Mike Reed353196f2017-07-21 11:01:18 -0400188bool SkGpuDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800189 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000190
Mike Reed353196f2017-07-21 11:01:18 -0400191 if (!SkImageInfoValidConversion(pm.info(), this->imageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500192 return false;
193 }
194
Brian Salomon1d435302019-07-01 13:05:28 -0400195 return fRenderTargetContext->readPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), {x, y});
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000196}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000197
Mike Reed353196f2017-07-21 11:01:18 -0400198bool SkGpuDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800199 ASSERT_SINGLE_OWNER
robertphillips1da3ecd2016-08-31 14:54:15 -0700200
Mike Reed353196f2017-07-21 11:01:18 -0400201 if (!SkImageInfoValidConversion(this->imageInfo(), pm.info())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500202 return false;
203 }
204
Brian Salomon1d435302019-07-01 13:05:28 -0400205 return fRenderTargetContext->writePixels(pm.info(), pm.addr(), pm.rowBytes(), {x, y});
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000206}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207
reed41e010c2015-06-09 12:16:53 -0700208bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800209 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700210 return false;
211}
212
Brian Osman11052242016-10-27 14:47:55 -0400213GrRenderTargetContext* SkGpuDevice::accessRenderTargetContext() {
robertphillips175dd9b2016-04-28 14:32:04 -0700214 ASSERT_SINGLE_OWNER
Brian Osman11052242016-10-27 14:47:55 -0400215 return fRenderTargetContext.get();
robertphillips175dd9b2016-04-28 14:32:04 -0700216}
217
reed8eddfb52014-12-04 07:50:14 -0800218void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800219 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500220 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext.get());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500221
reed8eddfb52014-12-04 07:50:14 -0800222 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
Brian Osman9a9baae2018-11-05 15:06:26 -0500223 fRenderTargetContext->clear(&rect, SK_PMColor4fTRANSPARENT,
224 GrRenderTargetContext::CanClearFullscreen::kYes);
reed8eddfb52014-12-04 07:50:14 -0800225}
226
Brian Salomonaad83152019-05-24 10:16:35 -0400227void SkGpuDevice::replaceRenderTargetContext(sk_sp<GrRenderTargetContext> rtc,
228 bool shouldRetainContent) {
229 SkASSERT(rtc->width() == this->width());
230 SkASSERT(rtc->height() == this->height());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600231 SkASSERT(rtc->numSamples() == fRenderTargetContext->numSamples());
Brian Salomonaad83152019-05-24 10:16:35 -0400232 SkASSERT(rtc->asSurfaceProxy()->priv().isExact());
233 if (shouldRetainContent) {
234 if (this->context()->abandoned()) {
235 return;
236 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400237
238 SkASSERT(fRenderTargetContext->asTextureProxy());
239 SkAssertResult(rtc->blitTexture(fRenderTargetContext->asTextureProxy(),
240 SkIRect::MakeWH(this->width(), this->height()),
241 SkIPoint::Make(0,0)));
Brian Salomonaad83152019-05-24 10:16:35 -0400242 }
243
244 fRenderTargetContext = std::move(rtc);
245}
246
Brian Osman11052242016-10-27 14:47:55 -0400247void SkGpuDevice::replaceRenderTargetContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800248 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800249
Brian Osman693a5402016-10-27 15:13:22 -0400250 SkBudgeted budgeted = fRenderTargetContext->priv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800251
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400252 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
253 // kExact-backed render target context.
Brian Osman693a5402016-10-27 15:13:22 -0400254 sk_sp<GrRenderTargetContext> newRTC(MakeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400255 this->context(),
256 budgeted,
257 this->imageInfo(),
Chris Dalton6ce447a2019-06-23 18:07:38 -0600258 fRenderTargetContext->numSamples(),
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400259 fRenderTargetContext->origin(),
Greg Daniele252f082017-10-23 16:05:23 -0400260 &this->surfaceProps(),
261 fRenderTargetContext->mipMapped()));
Brian Osman693a5402016-10-27 15:13:22 -0400262 if (!newRTC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800263 return;
264 }
Brian Salomonaad83152019-05-24 10:16:35 -0400265 this->replaceRenderTargetContext(std::move(newRTC), shouldRetainContent);
kkinnunenabcfab42015-02-22 22:53:44 -0800266}
267
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268///////////////////////////////////////////////////////////////////////////////
269
Mike Reeda1361362017-03-07 09:37:29 -0500270void SkGpuDevice::drawPaint(const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800271 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500272 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273
274 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400275 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
276 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700277 return;
278 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279
Brian Salomon0166cfd2017-03-13 17:58:25 -0400280 fRenderTargetContext->drawPaint(this->clip(), std::move(grPaint), this->ctm());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281}
282
Brian Salomon1459ebd2017-12-19 16:12:44 -0500283static inline GrPrimitiveType point_mode_to_primitive_type(SkCanvas::PointMode mode) {
284 switch (mode) {
285 case SkCanvas::kPoints_PointMode:
286 return GrPrimitiveType::kPoints;
287 case SkCanvas::kLines_PointMode:
288 return GrPrimitiveType::kLines;
289 case SkCanvas::kPolygon_PointMode:
290 return GrPrimitiveType::kLineStrip;
291 }
292 SK_ABORT("Unexpected mode");
Brian Salomon1459ebd2017-12-19 16:12:44 -0500293}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294
Mike Reeda1361362017-03-07 09:37:29 -0500295void SkGpuDevice::drawPoints(SkCanvas::PointMode mode,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800297 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500298 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000299 SkScalar width = paint.getStrokeWidth();
300 if (width < 0) {
301 return;
302 }
303
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000304 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700305 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700306 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400307 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
308 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700309 return;
310 }
egdaniele61c4112014-06-12 10:24:21 -0700311 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700312 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700313 path.moveTo(pts[0]);
314 path.lineTo(pts[1]);
Chris Dalton3b51df12017-11-27 14:33:06 -0700315 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
316 this->ctm(), path, style);
egdaniele61c4112014-06-12 10:24:21 -0700317 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000318 }
319
bsalomon6ade6dd2016-09-12 12:07:17 -0700320 SkScalar scales[2];
Mike Reeda1361362017-03-07 09:37:29 -0500321 bool isHairline = (0 == width) || (1 == width && this->ctm().getMinMaxScales(scales) &&
bsalomon6ade6dd2016-09-12 12:07:17 -0700322 SkScalarNearlyEqual(scales[0], 1.f) &&
323 SkScalarNearlyEqual(scales[1], 1.f));
ethannicholas330bb952015-07-17 06:44:02 -0700324 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000325 // else we let the SkDraw call our drawPath()
Ethan Nicholas2d78bcd2017-06-08 10:11:53 -0400326 if (!isHairline || paint.getPathEffect() || paint.getMaskFilter() || paint.isAntiAlias()) {
Mike Reeda1361362017-03-07 09:37:29 -0500327 SkRasterClip rc(this->devClipBounds());
328 SkDraw draw;
329 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(this->width(), this->height()), nullptr, 0);
330 draw.fMatrix = &this->ctm();
331 draw.fRC = &rc;
Mike Reed99330ba2017-02-22 11:01:08 -0500332 draw.drawPoints(mode, count, pts, paint, this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000333 return;
334 }
335
Brian Salomon1459ebd2017-12-19 16:12:44 -0500336 GrPrimitiveType primitiveType = point_mode_to_primitive_type(mode);
bsalomon6ade6dd2016-09-12 12:07:17 -0700337
Mike Reeda1361362017-03-07 09:37:29 -0500338 const SkMatrix* viewMatrix = &this->ctm();
bsalomon6ade6dd2016-09-12 12:07:17 -0700339#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
340 // This offsetting in device space matches the expectations of the Android framework for non-AA
341 // points and lines.
342 SkMatrix tempMatrix;
Chris Dalton3809bab2017-06-13 10:55:06 -0600343 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon6ade6dd2016-09-12 12:07:17 -0700344 tempMatrix = *viewMatrix;
345 static const SkScalar kOffset = 0.063f; // Just greater than 1/16.
346 tempMatrix.postTranslate(kOffset, kOffset);
347 viewMatrix = &tempMatrix;
348 }
349#endif
350
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400352 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
353 *viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700354 return;
355 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356
Brian Osmanae0c50c2017-05-25 16:56:34 -0400357 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
358 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, SkToS32(count), pts, nullptr,
359 nullptr);
360
361 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), *viewMatrix,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400362 std::move(vertices), nullptr, 0, &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363}
364
365///////////////////////////////////////////////////////////////////////////////
366
Mike Reeda1361362017-03-07 09:37:29 -0500367void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800368 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500369 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400370
371 GrStyle style(paint);
372
bsalomona7d85ba2016-07-06 11:54:59 -0700373 // A couple reasons we might need to call drawPath.
374 if (paint.getMaskFilter() || paint.getPathEffect()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400375 GrShape shape(rect, style);
376
377 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
378 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379 return;
380 }
381
382 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400383 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
384 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700385 return;
386 }
Mike Klein744fb732014-06-23 15:13:26 -0400387
Chris Dalton3b51df12017-11-27 14:33:06 -0700388 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
389 this->ctm(), rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390}
391
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400392void SkGpuDevice::drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
393 SkCanvas::QuadAAFlags aaFlags, SkColor color, SkBlendMode mode) {
Michael Ludwig75451902019-01-23 11:14:29 -0500394 ASSERT_SINGLE_OWNER
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400395 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawEdgeAAQuad", fContext.get());
Michael Ludwig75451902019-01-23 11:14:29 -0500396
397 SkPMColor4f dstColor = SkColor4fPrepForDst(SkColor4f::FromColor(color),
Brian Osman8fa7ab42019-03-18 10:22:42 -0400398 fRenderTargetContext->colorSpaceInfo())
Michael Ludwig75451902019-01-23 11:14:29 -0500399 .premul();
400
401 GrPaint grPaint;
402 grPaint.setColor4f(dstColor);
403 if (mode != SkBlendMode::kSrcOver) {
404 grPaint.setXPFactory(SkBlendMode_AsXPFactory(mode));
405 }
406
Michael Ludwig136f45a2019-02-19 11:44:41 -0500407 // This is exclusively meant for tiling operations, so keep AA enabled to handle MSAA seaming
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500408 GrQuadAAFlags grAA = SkToGrQuadAAFlags(aaFlags);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400409 if (clip) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500410 // Use fillQuadWithEdgeAA
411 fRenderTargetContext->fillQuadWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
412 this->ctm(), clip, nullptr);
413 } else {
414 // Use fillRectWithEdgeAA to preserve mathematical properties of dst being rectangular
415 fRenderTargetContext->fillRectWithEdgeAA(this->clip(), std::move(grPaint), GrAA::kYes, grAA,
416 this->ctm(), rect);
417 }
Michael Ludwig75451902019-01-23 11:14:29 -0500418}
419
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000420///////////////////////////////////////////////////////////////////////////////
421
Mike Reeda1361362017-03-07 09:37:29 -0500422void SkGpuDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800423 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500424 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext.get());
Robert Phillips27927a52018-08-20 13:18:12 -0400425
Mike Reed80747ef2018-01-23 15:29:32 -0500426 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000427 if (mf) {
428 if (mf->hasFragmentProcessor()) {
429 mf = nullptr; // already handled in SkPaintToGrPaint
430 }
Robert Phillipsa29a9562016-10-20 09:40:55 -0400431 }
432
bsalomon6663acf2016-05-10 09:14:17 -0700433 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000434
Robert Phillipsa29a9562016-10-20 09:40:55 -0400435 if (mf || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800436 // A path effect will presumably transform this rrect into something else.
Robert Phillips27927a52018-08-20 13:18:12 -0400437 GrShape shape(rrect, style);
438
Robert Phillips27927a52018-08-20 13:18:12 -0400439 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(),
440 this->clip(), paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000441 return;
442 }
Mike Klein744fb732014-06-23 15:13:26 -0400443
bsalomon6663acf2016-05-10 09:14:17 -0700444 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800445
Robert Phillipsa522d662018-08-23 13:50:16 -0400446 GrPaint grPaint;
447 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
448 this->ctm(), &grPaint)) {
449 return;
450 }
451
Chris Dalton3b51df12017-11-27 14:33:06 -0700452 fRenderTargetContext->drawRRect(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
453 this->ctm(), rrect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000454}
455
robertphillipsd7706102016-02-25 09:28:08 -0800456
Robert Phillips20390c32018-08-17 11:01:03 -0400457void SkGpuDevice::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800458 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500459 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext.get());
robertphillipsd7706102016-02-25 09:28:08 -0800460 if (outer.isEmpty()) {
461 return;
462 }
463
464 if (inner.isEmpty()) {
Mike Reeda1361362017-03-07 09:37:29 -0500465 return this->drawRRect(outer, paint);
robertphillipsd7706102016-02-25 09:28:08 -0800466 }
467
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000468 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000469
robertphillips0e7029e2015-11-30 05:45:06 -0800470 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800471 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400472 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
473 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700474 return;
475 }
robertphillips00095892016-02-29 13:50:40 -0800476
Brian Salomon0166cfd2017-03-13 17:58:25 -0400477 fRenderTargetContext->drawDRRect(this->clip(), std::move(grPaint),
Chris Dalton3b51df12017-11-27 14:33:06 -0700478 GrAA(paint.isAntiAlias()), this->ctm(), outer, inner);
robertphillips00095892016-02-29 13:50:40 -0800479 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000480 }
481
482 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700483 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000484 path.addRRect(outer);
485 path.addRRect(inner);
486 path.setFillType(SkPath::kEvenOdd_FillType);
487
Robert Phillips27927a52018-08-20 13:18:12 -0400488 // TODO: We are losing the possible mutability of the path here but this should probably be
489 // fixed by upgrading GrShape to handle DRRects.
490 GrShape shape(path, paint);
491
492 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
493 paint, this->ctm(), shape);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000494}
495
496
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000497/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000498
Mike Reeda1361362017-03-07 09:37:29 -0500499void SkGpuDevice::drawRegion(const SkRegion& region, const SkPaint& paint) {
msarettcc319b92016-08-25 18:07:18 -0700500 if (paint.getMaskFilter()) {
501 SkPath path;
502 region.getBoundaryPath(&path);
Robert Phillips137ca522018-08-15 10:14:33 -0400503 path.setIsVolatile(true);
504 return this->drawPath(path, paint, true);
msarettcc319b92016-08-25 18:07:18 -0700505 }
506
507 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400508 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
509 this->ctm(), &grPaint)) {
msarettcc319b92016-08-25 18:07:18 -0700510 return;
511 }
512
Chris Dalton3b51df12017-11-27 14:33:06 -0700513 fRenderTargetContext->drawRegion(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
514 this->ctm(), region, GrStyle(paint));
msarettcc319b92016-08-25 18:07:18 -0700515}
516
Mike Reeda1361362017-03-07 09:37:29 -0500517void SkGpuDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800518 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500519 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext.get());
herb11a7f7f2015-11-24 12:41:00 -0800520
robertphillips514450c2015-11-24 05:36:02 -0800521 if (paint.getMaskFilter()) {
522 // The RRect path can handle special case blurring
523 SkRRect rr = SkRRect::MakeOval(oval);
Mike Reeda1361362017-03-07 09:37:29 -0500524 return this->drawRRect(rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525 }
526
527 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400528 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
529 this->ctm(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700530 return;
531 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000532
Chris Dalton3b51df12017-11-27 14:33:06 -0700533 fRenderTargetContext->drawOval(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
534 this->ctm(), oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000535}
536
Mike Reeda1361362017-03-07 09:37:29 -0500537void SkGpuDevice::drawArc(const SkRect& oval, SkScalar startAngle,
bsalomon4f3a0ca2016-08-22 13:14:26 -0700538 SkScalar sweepAngle, bool useCenter, const SkPaint& paint) {
539 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500540 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawArc", fContext.get());
bsalomon4f3a0ca2016-08-22 13:14:26 -0700541 if (paint.getMaskFilter()) {
Mike Reeda1361362017-03-07 09:37:29 -0500542 this->INHERITED::drawArc(oval, startAngle, sweepAngle, useCenter, paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -0700543 return;
544 }
545 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400546 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
547 this->ctm(), &grPaint)) {
bsalomon4f3a0ca2016-08-22 13:14:26 -0700548 return;
549 }
550
Chris Dalton3b51df12017-11-27 14:33:06 -0700551 fRenderTargetContext->drawArc(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
Mike Reeda1361362017-03-07 09:37:29 -0500552 this->ctm(), oval, startAngle, sweepAngle, useCenter,
Brian Salomon82f44312017-01-11 13:42:54 -0500553 GrStyle(paint));
bsalomon4f3a0ca2016-08-22 13:14:26 -0700554}
555
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500556#include "include/core/SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557
558///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700559void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
robertphillips0851d2d2016-06-02 05:21:34 -0700560 const SkPaint& origPaint) {
561 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500562 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext.get());
Brian Osman11052242016-10-27 14:47:55 -0400563 // Adding support for round capping would require a
564 // GrRenderTargetContext::fillRRectWithLocalMatrix entry point
robertphillips0851d2d2016-06-02 05:21:34 -0700565 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
566 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
567 SkASSERT(!origPaint.getPathEffect());
568 SkASSERT(!origPaint.getMaskFilter());
569
570 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
571 SkASSERT(halfWidth > 0);
572
573 SkVector v = points[1] - points[0];
574
575 SkScalar length = SkPoint::Normalize(&v);
576 if (!length) {
577 v.fX = 1.0f;
578 v.fY = 0.0f;
579 }
580
581 SkPaint newPaint(origPaint);
582 newPaint.setStyle(SkPaint::kFill_Style);
583
584 SkScalar xtraLength = 0.0f;
585 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
586 xtraLength = halfWidth;
587 }
588
589 SkPoint mid = points[0] + points[1];
590 mid.scale(0.5f);
591
592 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
593 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
594 SkMatrix m;
595 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
596
597 SkMatrix local = m;
598
Mike Reeda1361362017-03-07 09:37:29 -0500599 m.postConcat(this->ctm());
robertphillips0851d2d2016-06-02 05:21:34 -0700600
601 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400602 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), newPaint, m,
603 &grPaint)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700604 return;
605 }
606
Brian Salomon82f44312017-01-11 13:42:54 -0500607 fRenderTargetContext->fillRectWithLocalMatrix(
Chris Dalton3b51df12017-11-27 14:33:06 -0700608 this->clip(), std::move(grPaint), GrAA(newPaint.isAntiAlias()), m, rect, local);
robertphillips0851d2d2016-06-02 05:21:34 -0700609}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000610
Robert Phillips20390c32018-08-17 11:01:03 -0400611void SkGpuDevice::drawPath(const SkPath& origSrcPath, const SkPaint& paint, bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800612 ASSERT_SINGLE_OWNER
Robert Phillips137ca522018-08-15 10:14:33 -0400613 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect()) {
robertphillips0851d2d2016-06-02 05:21:34 -0700614 SkPoint points[2];
615 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
616 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
Mike Reeda1361362017-03-07 09:37:29 -0500617 this->ctm().preservesRightAngles() && origSrcPath.isLine(points)) {
robertphillips0851d2d2016-06-02 05:21:34 -0700618 // Path-based stroking looks better for thin rects
Mike Reeda1361362017-03-07 09:37:29 -0500619 SkScalar strokeWidth = this->ctm().getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700620 if (strokeWidth >= 1.0f) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500621 // Round capping support is currently disabled b.c. it would require a RRect
622 // GrDrawOp that takes a localMatrix.
Mike Reeda1361362017-03-07 09:37:29 -0500623 this->drawStrokedLine(points, paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700624 return;
625 }
626 }
robertphillipsff55b492015-11-24 07:56:59 -0800627 }
628
Hal Canary144caf52016-11-07 17:57:18 -0500629 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext.get());
Robert Phillips137ca522018-08-15 10:14:33 -0400630 if (!paint.getMaskFilter()) {
Brian Salomona3cf3652018-01-03 15:11:00 -0500631 GrPaint grPaint;
632 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
633 this->ctm(), &grPaint)) {
634 return;
635 }
636 fRenderTargetContext->drawPath(this->clip(), std::move(grPaint), GrAA(paint.isAntiAlias()),
637 this->ctm(), origSrcPath, GrStyle(paint));
638 return;
639 }
Robert Phillips27927a52018-08-20 13:18:12 -0400640
641 // TODO: losing possible mutability of 'origSrcPath' here
642 GrShape shape(origSrcPath, paint);
643
644 GrBlurUtils::drawShapeWithMaskFilter(fContext.get(), fRenderTargetContext.get(), this->clip(),
645 paint, this->ctm(), shape);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646}
647
648static const int kBmpSmallTileSize = 1 << 10;
649
650static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
651 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
652 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
653 return tilesX * tilesY;
654}
655
reed85d91782015-09-10 14:33:38 -0700656static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000657 if (maxTileSize <= kBmpSmallTileSize) {
658 return maxTileSize;
659 }
660
661 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
662 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
663
664 maxTileTotalTileSize *= maxTileSize * maxTileSize;
665 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
666
667 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
668 return kBmpSmallTileSize;
669 } else {
670 return maxTileSize;
671 }
672}
673
674// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
675// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700676static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800677 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800678 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700679 const SkMatrix& srcToDstRect,
reed85d91782015-09-10 14:33:38 -0700680 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 const SkRect* srcRectPtr,
682 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700683 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
bsalomone553b642016-08-17 09:02:09 -0700684 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
685 if (!inv.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686 clippedSrcIRect->setEmpty();
687 return;
688 }
689 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
690 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700691 if (srcRectPtr) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000692 if (!clippedSrcRect.intersect(*srcRectPtr)) {
693 clippedSrcIRect->setEmpty();
694 return;
695 }
696 }
697 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700698 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 if (!clippedSrcIRect->intersect(bmpBounds)) {
700 clippedSrcIRect->setEmpty();
701 }
702}
703
Robert Phillips920d4882019-03-04 15:16:44 -0500704const GrCaps* SkGpuDevice::caps() const {
705 return fContext->priv().caps();
706}
707
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400708bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
709 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700710 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700711 const SkMatrix& srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400712 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700713 const SkRect* srcRectPtr,
714 int maxTileSize,
715 int* tileSize,
716 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800717 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700718 // if it's larger than the max tile size, then we have no choice but tiling.
719 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
Brian Osman11052242016-10-27 14:47:55 -0400720 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
Brian Salomon0166cfd2017-03-13 17:58:25 -0400721 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
722 srcRectPtr, clippedSubset);
reed85d91782015-09-10 14:33:38 -0700723 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
724 return true;
725 }
726
bsalomon1a1d0b82015-10-16 07:49:42 -0700727 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700728 const size_t area = imageRect.width() * imageRect.height();
729 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
730 return false;
731 }
732
reed85d91782015-09-10 14:33:38 -0700733 // At this point we know we could do the draw by uploading the entire bitmap
734 // as a texture. However, if the texture would be large compared to the
735 // cache size and we don't require most of it for this draw then tile to
736 // reduce the amount of upload and cache spill.
737
738 // assumption here is that sw bitmap size is a good proxy for its size as
739 // a texture
740 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
741 size_t cacheSize;
742 fContext->getResourceCacheLimits(nullptr, &cacheSize);
743 if (bmpSize < cacheSize / 2) {
744 return false;
745 }
746
bsalomon1a1d0b82015-10-16 07:49:42 -0700747 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
748 // tiling memory savings would be < 50%.
Brian Salomon0166cfd2017-03-13 17:58:25 -0400749 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
750 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
Brian Osman11052242016-10-27 14:47:55 -0400751 clippedSubset);
reed85d91782015-09-10 14:33:38 -0700752 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
753 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
Brian Osmand05cdc32017-02-06 13:24:47 -0500754 kBmpSmallTileSize * kBmpSmallTileSize *
755 sizeof(SkPMColor); // assume 32bit pixels;
reed85d91782015-09-10 14:33:38 -0700756
Brian Osmand05cdc32017-02-06 13:24:47 -0500757 return usedTileBytes * 2 < bmpSize;
reed85d91782015-09-10 14:33:38 -0700758}
759
reed85d91782015-09-10 14:33:38 -0700760bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
761 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700762 const SkMatrix& viewMatrix,
763 const SkMatrix& srcToDstRect) const {
joshualittce894002016-01-11 13:29:31 -0800764 ASSERT_SINGLE_OWNER
Brian Salomon34169692017-08-28 15:32:01 -0400765 // If image is explicitly texture backed then we shouldn't get here.
766 SkASSERT(!image->isTextureBacked());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400768 GrSamplerState samplerState;
reed85d91782015-09-10 14:33:38 -0700769 bool doBicubic;
Brian Osmandb78cba2018-02-15 10:09:48 -0500770 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600771 image->width(), image->height(), quality, viewMatrix, srcToDstRect,
Robert Phillips9da87e02019-02-04 13:26:26 -0500772 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
reed85d91782015-09-10 14:33:38 -0700773
774 int tileFilterPad;
775 if (doBicubic) {
776 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400777 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
reed85d91782015-09-10 14:33:38 -0700778 tileFilterPad = 0;
779 } else {
780 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400782 samplerState.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400784 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785
reed85d91782015-09-10 14:33:38 -0700786 // these are output, which we safely ignore, as we just want to know the predicate
787 int outTileSize;
788 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789
bsalomone553b642016-08-17 09:02:09 -0700790 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400791 samplerState, srcRectPtr, maxTileSize, &outTileSize,
bsalomone553b642016-08-17 09:02:09 -0700792 &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793}
794
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000795// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
797// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000798static inline void clamped_outset_with_offset(SkIRect* iRect,
799 int outset,
800 SkPoint* offset,
801 const SkIRect& clamp) {
802 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000804 int leftClampDelta = clamp.fLeft - iRect->fLeft;
805 if (leftClampDelta > 0) {
806 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 iRect->fLeft = clamp.fLeft;
808 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000809 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000811
812 int topClampDelta = clamp.fTop - iRect->fTop;
813 if (topClampDelta > 0) {
814 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 iRect->fTop = clamp.fTop;
816 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000817 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 }
819
820 if (iRect->fRight > clamp.fRight) {
821 iRect->fRight = clamp.fRight;
822 }
823 if (iRect->fBottom > clamp.fBottom) {
824 iRect->fBottom = clamp.fBottom;
825 }
826}
827
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000828// Break 'bitmap' into several tiles to draw it since it has already
829// been determined to be too large to fit in VRAM
830void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800831 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700832 const SkMatrix& dstMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 const SkRect& srcRect,
834 const SkIRect& clippedSrcIRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400835 const GrSamplerState& params,
bsalomonc55271f2015-11-09 11:55:57 -0800836 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700837 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000838 int tileSize,
839 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800840 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800841
ericrk983294f2016-04-18 09:14:00 -0700842 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800843 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
Michael Ludwig3e2cc062019-02-19 12:11:40 -0500844 LogDrawScaleFactor(viewMatrix, SkMatrix::I(), origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800845
bsalomonc55271f2015-11-09 11:55:57 -0800846 const SkPaint* paint = &origPaint;
847 SkPaint tempPaint;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600848 if (origPaint.isAntiAlias() && fRenderTargetContext->numSamples() <= 1) {
bsalomonc55271f2015-11-09 11:55:57 -0800849 // Drop antialiasing to avoid seams at tile boundaries.
850 tempPaint = origPaint;
851 tempPaint.setAntiAlias(false);
852 paint = &tempPaint;
853 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
855
856 int nx = bitmap.width() / tileSize;
857 int ny = bitmap.height() / tileSize;
858 for (int x = 0; x <= nx; x++) {
859 for (int y = 0; y <= ny; y++) {
860 SkRect tileR;
861 tileR.set(SkIntToScalar(x * tileSize),
862 SkIntToScalar(y * tileSize),
863 SkIntToScalar((x + 1) * tileSize),
864 SkIntToScalar((y + 1) * tileSize));
865
866 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
867 continue;
868 }
869
870 if (!tileR.intersect(srcRect)) {
871 continue;
872 }
873
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874 SkIRect iTileR;
875 tileR.roundOut(&iTileR);
bsalomone553b642016-08-17 09:02:09 -0700876 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
877 SkIntToScalar(iTileR.fTop));
Brian Osmana950a862017-02-06 16:48:57 -0500878 SkRect rectToDraw = tileR;
bsalomone553b642016-08-17 09:02:09 -0700879 dstMatrix.mapRect(&rectToDraw);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400880 if (GrSamplerState::Filter::kNearest != params.filter() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881 SkIRect iClampRect;
882
reeda5517e22015-07-14 10:54:12 -0700883 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884 // In bleed mode we want to always expand the tile on all edges
885 // but stay within the bitmap bounds
886 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
887 } else {
888 // In texture-domain/clamp mode we only want to expand the
889 // tile on edges interior to "srcRect" (i.e., we want to
890 // not bleed across the original clamped edges)
891 srcRect.roundOut(&iClampRect);
892 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000893 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
894 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000895 }
896
bsalomone553b642016-08-17 09:02:09 -0700897 SkBitmap tmpB;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 if (bitmap.extractSubset(&tmpB, iTileR)) {
899 // now offset it to make it "local" to our tmp bitmap
900 tileR.offset(-offset.fX, -offset.fY);
bsalomonb1b01992015-11-18 10:56:08 -0800901 // de-optimized this determination
902 bool needsTextureDomain = true;
bsalomone553b642016-08-17 09:02:09 -0700903 this->drawBitmapTile(tmpB,
904 viewMatrix,
905 rectToDraw,
906 tileR,
Robert Phillipse14d3052017-02-15 13:18:21 -0500907 params,
bsalomone553b642016-08-17 09:02:09 -0700908 *paint,
909 constraint,
910 bicubic,
911 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 }
913 }
914 }
915}
916
bsalomone553b642016-08-17 09:02:09 -0700917void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
918 const SkMatrix& viewMatrix,
919 const SkRect& dstRect,
920 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400921 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700922 const SkPaint& paint,
923 SkCanvas::SrcRectConstraint constraint,
924 bool bicubic,
925 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -0800926 // We should have already handled bitmaps larger than the max texture size.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400927 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
928 bitmap.height() <= this->caps()->maxTextureSize());
reedc7ec7c92016-07-25 08:29:10 -0700929 // We should be respecting the max tile size by the time we get here.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400930 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
931 bitmap.height() <= this->caps()->maxTileSize());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400932 SkASSERT(!samplerState.isRepeated());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933
Brian Salomon2a943df2018-05-04 13:43:19 -0400934 SkScalar scales[2] = {1.f, 1.f};
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400935 sk_sp<GrTextureProxy> proxy =
Brian Salomon2a943df2018-05-04 13:43:19 -0400936 GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
Robert Phillips78075802017-03-23 11:11:59 -0400937 if (!proxy) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938 return;
939 }
bsalomone553b642016-08-17 09:02:09 -0700940
bsalomone553b642016-08-17 09:02:09 -0700941 // Compute a matrix that maps the rect we will draw to the src rect.
Brian Salomon2a943df2018-05-04 13:43:19 -0400942 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
943 texMatrix.postScale(scales[0], scales[1]);
joshualitt5f10b5c2015-07-09 10:24:35 -0700944
945 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
946 // the rest from the SkPaint.
Brian Salomonaff329b2017-08-11 09:40:37 -0400947 std::unique_ptr<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -0700948
reeda5517e22015-07-14 10:54:12 -0700949 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 // Use a constrained texture domain to avoid color bleeding
bsalomone553b642016-08-17 09:02:09 -0700951 SkRect domain;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 if (srcRect.width() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500953 domain.fLeft = srcRect.fLeft + 0.5f;
954 domain.fRight = srcRect.fRight - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500956 domain.fLeft = domain.fRight = srcRect.centerX();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957 }
958 if (srcRect.height() > SK_Scalar1) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500959 domain.fTop = srcRect.fTop + 0.5f;
960 domain.fBottom = srcRect.fBottom - 0.5f;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 } else {
Robert Phillipse98234f2017-01-09 14:23:59 -0500962 domain.fTop = domain.fBottom = srcRect.centerY();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963 }
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000964 if (bicubic) {
Brian Salomona86fc7a2019-05-28 20:42:58 -0400965 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomon1127c0b2019-06-13 20:22:10 +0000966 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, domain, kDir,
Brian Salomona86fc7a2019-05-28 20:42:58 -0400967 bitmap.alphaType());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000968 } else {
Brian Osman2240be92017-10-18 13:15:13 -0400969 fp = GrTextureDomainEffect::Make(std::move(proxy), texMatrix, domain,
Brian Osman5e341672017-10-18 10:23:18 -0400970 GrTextureDomain::kClamp_Mode, samplerState.filter());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +0000971 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000972 } else if (bicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400973 SkASSERT(GrSamplerState::Filter::kNearest == samplerState.filter());
974 GrSamplerState::WrapMode wrapMode[2] = {samplerState.wrapModeX(), samplerState.wrapModeY()};
Brian Salomona86fc7a2019-05-28 20:42:58 -0400975 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomon1127c0b2019-06-13 20:22:10 +0000976 fp = GrBicubicEffect::Make(std::move(proxy), texMatrix, wrapMode, kDir, bitmap.alphaType());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 } else {
Brian Osman2240be92017-10-18 13:15:13 -0400978 fp = GrSimpleTextureEffect::Make(std::move(proxy), texMatrix, samplerState);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 }
980
Brian Osman21fc5ce2018-08-27 20:36:19 +0000981 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400982 fRenderTargetContext->colorSpaceInfo().colorSpace());
joshualitt33a5fce2015-11-18 13:28:51 -0800983 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400984 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorSpaceInfo(), paint,
985 viewMatrix, std::move(fp),
986 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700987 return;
988 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000989
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500990 // Coverage-based AA would cause seams between tiles.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600991 GrAA aa = GrAA(paint.isAntiAlias() && fRenderTargetContext->numSamples() > 1);
Brian Salomon0166cfd2017-03-13 17:58:25 -0400992 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993}
994
Mike Reeda1361362017-03-07 09:37:29 -0500995void SkGpuDevice::drawSprite(const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800997 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -0500998 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999
Robert Phillips920d4882019-03-04 15:16:44 -05001000 if (fContext->priv().abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 return;
1002 }
1003
Robert Phillipse14d3052017-02-15 13:18:21 -05001004 sk_sp<SkSpecialImage> srcImg = this->makeSpecial(bitmap);
1005 if (!srcImg) {
1006 return;
joshualitt5f5a8d72015-02-25 14:09:45 -08001007 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008
Florin Malita53f77bd2017-04-28 13:48:37 -04001009 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
robertphillips970587b2016-07-14 14:12:55 -07001010}
1011
1012
Robert Phillipsc100d482018-07-10 10:11:01 -04001013void SkGpuDevice::drawSpecial(SkSpecialImage* special, int left, int top, const SkPaint& paint,
Robert Phillips213ce182018-04-25 09:13:28 -04001014 SkImage* clipImage, const SkMatrix& clipMatrix) {
robertphillips1b5f9682016-07-15 08:01:12 -07001015 ASSERT_SINGLE_OWNER
Hal Canary144caf52016-11-07 17:57:18 -05001016 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext.get());
robertphillips970587b2016-07-14 14:12:55 -07001017
robertphillips970587b2016-07-14 14:12:55 -07001018 sk_sp<SkSpecialImage> result;
1019 if (paint.getImageFilter()) {
Robert Phillipsc100d482018-07-10 10:11:01 -04001020 SkIPoint offset = { 0, 0 };
1021
1022 result = this->filterTexture(special, left, top, &offset, paint.getImageFilter());
robertphillips970587b2016-07-14 14:12:55 -07001023 if (!result) {
1024 return;
1025 }
Robert Phillipsc100d482018-07-10 10:11:01 -04001026
1027 left += offset.fX;
1028 top += offset.fY;
robertphillips970587b2016-07-14 14:12:55 -07001029 } else {
Robert Phillipsc100d482018-07-10 10:11:01 -04001030 result = sk_ref_sp(special);
robertphillips970587b2016-07-14 14:12:55 -07001031 }
1032
1033 SkASSERT(result->isTextureBacked());
Robert Phillips2c6d2bf2017-02-21 10:19:29 -05001034 sk_sp<GrTextureProxy> proxy = result->asTextureProxyRef(this->context());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001035 if (!proxy) {
Robert Phillips833dcf42016-11-18 08:44:13 -05001036 return;
1037 }
robertphillips970587b2016-07-14 14:12:55 -07001038
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001039 const GrPixelConfig config = proxy->config();
1040
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001041 SkMatrix ctm = this->ctm();
1042 ctm.postTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1043
robertphillips970587b2016-07-14 14:12:55 -07001044 SkPaint tmpUnfiltered(paint);
Mike Reed2179b782018-02-07 11:59:57 -05001045 if (tmpUnfiltered.getMaskFilter()) {
Florin Malitac6c5ead2018-04-11 15:33:40 -04001046 tmpUnfiltered.setMaskFilter(tmpUnfiltered.getMaskFilter()->makeWithMatrix(ctm));
Mike Reed2179b782018-02-07 11:59:57 -05001047 }
1048
robertphillips970587b2016-07-14 14:12:55 -07001049 tmpUnfiltered.setImageFilter(nullptr);
1050
Brian Osman2240be92017-10-18 13:15:13 -04001051 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I());
Brian Osman21fc5ce2018-08-27 20:36:19 +00001052 fp = GrColorSpaceXformEffect::Make(std::move(fp), result->getColorSpace(), result->alphaType(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001053 fRenderTargetContext->colorSpaceInfo().colorSpace());
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001054 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon22af73f2017-01-26 11:25:12 -05001055 fp = GrFragmentProcessor::MakeInputPremulAndMulByOutput(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001056 } else {
Brian Salomonc0d79e52019-04-10 15:02:11 -04001057 if (paint.getColor4f().isOpaque()) {
1058 fp = GrFragmentProcessor::OverrideInput(std::move(fp), SK_PMColor4fWHITE, false);
1059 } else {
1060 fp = GrFragmentProcessor::MulChildByInputAlpha(std::move(fp));
1061 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001062 }
Robert Phillips8e1c4e62017-02-19 12:27:01 -05001063
1064 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -04001065 if (!SkPaintToGrPaintReplaceShader(this->context(), fRenderTargetContext->colorSpaceInfo(),
1066 tmpUnfiltered, std::move(fp), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001067 return;
1068 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069
robertphillips970587b2016-07-14 14:12:55 -07001070 const SkIRect& subset = result->subset();
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001071 SkRect dstRect = SkRect::Make(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()));
1072 SkRect srcRect = SkRect::Make(subset);
1073 if (clipImage) {
1074 // Add the image as a simple texture effect applied to coverage. Accessing content outside
1075 // of the clip image should behave as if it were a decal (i.e. zero coverage). However, to
1076 // limit pixels touched and hardware checks, we draw the clip image geometry to get the
1077 // decal effect.
1078 GrSamplerState sampler = paint.getFilterQuality() > kNone_SkFilterQuality ?
1079 GrSamplerState::ClampBilerp() : GrSamplerState::ClampNearest();
1080 sk_sp<GrTextureProxy> clipProxy = as_IB(clipImage)->asTextureProxyRef(this->context(),
1081 sampler, nullptr);
1082 // Fold clip matrix into ctm
1083 ctm.preConcat(clipMatrix);
1084 SkMatrix inverseClipMatrix;
robertphillips970587b2016-07-14 14:12:55 -07001085
Michael Ludwigbeb7cd22019-04-08 11:11:42 -04001086 std::unique_ptr<GrFragmentProcessor> cfp;
1087 if (clipProxy && ctm.invert(&inverseClipMatrix)) {
1088 cfp = GrSimpleTextureEffect::Make(std::move(clipProxy), inverseClipMatrix, sampler);
1089 if (clipImage->colorType() != kAlpha_8_SkColorType) {
1090 cfp = GrFragmentProcessor::SwizzleOutput(std::move(cfp), GrSwizzle::AAAA());
1091 }
1092 }
1093
1094 if (cfp) {
1095 // If the grPaint already has coverage, this adds an additional stage that multiples
1096 // the image's alpha channel with the prior coverage.
1097 grPaint.addCoverageFragmentProcessor(std::move(cfp));
1098
1099 // Undo the offset that was needed for shader coord transforms to get the transform for
1100 // the actual drawn geometry.
1101 ctm.postTranslate(SkIntToScalar(left), SkIntToScalar(top));
1102 inverseClipMatrix.preTranslate(-SkIntToScalar(left), -SkIntToScalar(top));
1103 SkRect clipGeometry = SkRect::MakeWH(clipImage->width(), clipImage->height());
1104 if (!clipGeometry.contains(inverseClipMatrix.mapRect(dstRect))) {
1105 // Draw the clip geometry since it is smaller, using dstRect as an extra scissor
1106 SkClipStack clip(this->cs());
1107 clip.clipDevRect(SkIRect::MakeXYWH(left, top, subset.width(), subset.height()),
1108 SkClipOp::kIntersect);
1109 SkMatrix local = SkMatrix::Concat(SkMatrix::MakeRectToRect(
1110 dstRect, srcRect, SkMatrix::kFill_ScaleToFit), ctm);
1111 fRenderTargetContext->fillRectWithLocalMatrix(GrClipStackClip(&clip),
1112 std::move(grPaint), GrAA(paint.isAntiAlias()), ctm, clipGeometry, local);
1113 return;
1114 }
1115 // Else fall through and draw the subset since that is contained in the clip geometry
1116 }
1117 // Else some issue configuring the coverage FP, so just draw without the clip mask image
1118 }
1119 // Draw directly in screen space, possibly with an extra coverage processor
1120 fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint),
1121 GrAA(paint.isAntiAlias()), SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122}
1123
Mike Reeda1361362017-03-07 09:37:29 -05001124void SkGpuDevice::drawBitmapRect(const SkBitmap& bitmap,
bsalomonb1b01992015-11-18 10:56:08 -08001125 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001126 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001127 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001128 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1129 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1130 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1131 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1132 const SkRect* dst = &origDst;
1133 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001135 if (!src) {
1136 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001137 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001138
bsalomonb1b01992015-11-18 10:56:08 -08001139 SkMatrix srcToDstMatrix;
1140 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1141 return;
1142 }
1143 SkRect tmpSrc, tmpDst;
1144 if (src != &bmpBounds) {
1145 if (!bmpBounds.contains(*src)) {
1146 tmpSrc = *src;
1147 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 return; // nothing to draw
1149 }
bsalomonb1b01992015-11-18 10:56:08 -08001150 src = &tmpSrc;
1151 srcToDstMatrix.mapRect(&tmpDst, *src);
1152 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 }
1154 }
1155
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001156 int maxTileSize = this->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001157
bsalomonb1b01992015-11-18 10:56:08 -08001158 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1159 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
Chris Dalton6ce447a2019-06-23 18:07:38 -06001160 bool useCoverageAA = fRenderTargetContext->numSamples() <= 1 &&
Brian Salomon7c8460e2017-05-12 11:36:10 -04001161 paint.isAntiAlias() && bitmap.width() <= maxTileSize &&
1162 bitmap.height() <= maxTileSize;
bsalomonb1b01992015-11-18 10:56:08 -08001163
Brian Salomon7c8460e2017-05-12 11:36:10 -04001164 bool skipTileCheck = useCoverageAA || paint.getMaskFilter();
bsalomonb1b01992015-11-18 10:56:08 -08001165
1166 if (!skipTileCheck) {
1167 int tileSize;
1168 SkIRect clippedSrcRect;
1169
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001170 GrSamplerState sampleState;
bsalomonb1b01992015-11-18 10:56:08 -08001171 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001172 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -06001173 bitmap.width(), bitmap.height(), paint.getFilterQuality(), this->ctm(),
1174 srcToDstMatrix, fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001175
1176 int tileFilterPad;
1177
1178 if (doBicubic) {
1179 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001180 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
bsalomonb1b01992015-11-18 10:56:08 -08001181 tileFilterPad = 0;
1182 } else {
1183 tileFilterPad = 1;
1184 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001185 sampleState.setFilterMode(textureFilterMode);
bsalomonb1b01992015-11-18 10:56:08 -08001186
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001187 int maxTileSizeForFilter = this->caps()->maxTileSize() - 2 * tileFilterPad;
Mike Reeda1361362017-03-07 09:37:29 -05001188 if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), this->ctm(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001189 srcToDstMatrix, sampleState, src, maxTileSizeForFilter,
1190 &tileSize, &clippedSrcRect)) {
Mike Reeda1361362017-03-07 09:37:29 -05001191 this->drawTiledBitmap(bitmap, this->ctm(), srcToDstMatrix, *src, clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001192 sampleState, paint, constraint, tileSize, doBicubic);
bsalomonb1b01992015-11-18 10:56:08 -08001193 return;
1194 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001195 }
Hal Canary144caf52016-11-07 17:57:18 -05001196 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -04001197 this->drawTextureProducer(&maker, src, dst, constraint, this->ctm(), paint, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198}
1199
robertphillips6451a0c2016-07-18 08:31:31 -07001200sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
Robert Phillipse14d3052017-02-15 13:18:21 -05001201 // TODO: this makes a tight copy of 'bitmap' but it doesn't have to be (given SkSpecialImage's
1202 // semantics). Since this is cached we would have to bake the fit into the cache key though.
Robert Phillips9da87e02019-02-04 13:26:26 -05001203 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(fContext->priv().proxyProvider(),
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001204 bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -05001205 if (!proxy) {
robertphillips6451a0c2016-07-18 08:31:31 -07001206 return nullptr;
1207 }
1208
Robert Phillipse14d3052017-02-15 13:18:21 -05001209 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
robertphillips6451a0c2016-07-18 08:31:31 -07001210
Robert Phillipse14d3052017-02-15 13:18:21 -05001211 // GrMakeCachedBitmapProxy creates a tight copy of 'bitmap' so we don't have to subset
1212 // the special image
1213 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1214 rect,
1215 bitmap.getGenerationID(),
1216 std::move(proxy),
1217 bitmap.refColorSpace(),
1218 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001219}
1220
reede51c3562016-07-19 14:33:20 -07001221sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001222 SkPixmap pm;
1223 if (image->isTextureBacked()) {
Robert Phillips6603a172019-03-05 12:35:44 -05001224 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(this->context());
robertphillips6451a0c2016-07-18 08:31:31 -07001225
Robert Phillips6de99042017-01-31 11:31:39 -05001226 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1227 SkIRect::MakeWH(image->width(), image->height()),
1228 image->uniqueID(),
1229 std::move(proxy),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001230 image->refColorSpace(),
Robert Phillips6de99042017-01-31 11:31:39 -05001231 &this->surfaceProps());
robertphillips6451a0c2016-07-18 08:31:31 -07001232 } else if (image->peekPixels(&pm)) {
1233 SkBitmap bm;
1234
1235 bm.installPixels(pm);
1236 return this->makeSpecial(bm);
1237 } else {
1238 return nullptr;
1239 }
1240}
1241
1242sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
Greg Danielbe7fc462019-01-03 16:40:42 -05001243 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1244 // since it would require us to make a copy of the underlying VkImage which we don't have access
1245 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1246 // command buffer.
1247 if (this->accessRenderTargetContext()->wrapsVkSecondaryCB()) {
1248 return nullptr;
1249 }
1250
Robert Phillips63c67462017-02-15 14:19:01 -05001251 sk_sp<GrTextureProxy> proxy(this->accessRenderTargetContext()->asTextureProxyRef());
1252 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001253 // When the device doesn't have a texture, we create a temporary texture.
1254 // TODO: we should actually only copy the portion of the source needed to apply the image
1255 // filter
Robert Phillips63c67462017-02-15 14:19:01 -05001256 proxy = GrSurfaceProxy::Copy(fContext.get(),
1257 this->accessRenderTargetContext()->asSurfaceProxy(),
Greg Daniel65c7f662017-10-30 13:39:09 -04001258 GrMipMapped::kNo,
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001259 SkBackingFit::kApprox,
Robert Phillips63c67462017-02-15 14:19:01 -05001260 SkBudgeted::kYes);
1261 if (!proxy) {
robertphillips04d62182016-07-15 12:21:33 -07001262 return nullptr;
1263 }
robertphillips1b5f9682016-07-15 08:01:12 -07001264 }
1265
1266 const SkImageInfo ii = this->imageInfo();
1267 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1268
Robert Phillipse2f7d182016-12-15 09:23:05 -05001269 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1270 srcRect,
1271 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -05001272 std::move(proxy),
Robert Phillips70b49fd2017-01-13 11:21:36 -05001273 ii.refColorSpace(),
Robert Phillipse2f7d182016-12-15 09:23:05 -05001274 &this->surfaceProps());
robertphillips1b5f9682016-07-15 08:01:12 -07001275}
1276
Mike Reed148b7fd2018-12-18 17:38:18 -05001277sk_sp<SkSpecialImage> SkGpuDevice::snapBackImage(const SkIRect& subset) {
1278 GrRenderTargetContext* rtc = this->accessRenderTargetContext();
Greg Danielbe7fc462019-01-03 16:40:42 -05001279
1280 // If we are wrapping a vulkan secondary command buffer, then we can't snap off a special image
1281 // since it would require us to make a copy of the underlying VkImage which we don't have access
1282 // to. Additionaly we can't stop and start the render pass that is used with the secondary
1283 // command buffer.
1284 if (rtc->wrapsVkSecondaryCB()) {
Mike Reed148b7fd2018-12-18 17:38:18 -05001285 return nullptr;
1286 }
1287
Greg Danielbe7fc462019-01-03 16:40:42 -05001288
Mike Reed148b7fd2018-12-18 17:38:18 -05001289 GrContext* ctx = this->context();
Greg Danielbe7fc462019-01-03 16:40:42 -05001290 SkASSERT(rtc->asSurfaceProxy());
Mike Reed148b7fd2018-12-18 17:38:18 -05001291
Brian Salomonfee3f9b2018-12-19 12:34:12 -05001292 auto srcProxy =
1293 GrSurfaceProxy::Copy(ctx, rtc->asSurfaceProxy(), rtc->mipMapped(), subset,
1294 SkBackingFit::kApprox, rtc->asSurfaceProxy()->isBudgeted());
Mike Reed148b7fd2018-12-18 17:38:18 -05001295 if (!srcProxy) {
1296 return nullptr;
1297 }
1298
1299 // Note, can't move srcProxy since we also refer to this in the 2nd parameter
1300 return SkSpecialImage::MakeDeferredFromGpu(fContext.get(),
1301 SkIRect::MakeSize(srcProxy->isize()),
1302 kNeedNewImageUniqueID_SpecialImage,
1303 srcProxy,
1304 this->imageInfo().refColorSpace(),
1305 &this->surfaceProps());
1306}
1307
Mike Reeda1361362017-03-07 09:37:29 -05001308void SkGpuDevice::drawDevice(SkBaseDevice* device,
Mike Reed2179b782018-02-07 11:59:57 -05001309 int left, int top, const SkPaint& paint) {
1310 SkASSERT(!paint.getImageFilter());
reedcf5c8462016-07-20 12:28:40 -07001311
joshualittce894002016-01-11 13:29:31 -08001312 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001313 // clear of the source device must occur before CHECK_SHOULD_DRAW
Hal Canary144caf52016-11-07 17:57:18 -05001314 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext.get());
kkinnunen2e4414e2015-02-19 07:20:40 -08001315
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 // drawDevice is defined to be in device coords.
robertphillips1b5f9682016-07-15 08:01:12 -07001317 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001318 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001319 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 return;
1321 }
1322
Mike Reed2179b782018-02-07 11:59:57 -05001323 this->drawSpecial(srcImg.get(), left, top, paint, nullptr, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324}
1325
Brian Salomon34169692017-08-28 15:32:01 -04001326void SkGpuDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1327 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001328 ASSERT_SINGLE_OWNER
Michael Ludwig1433cfd2019-02-27 17:12:30 -05001329 GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -05001330 this->drawImageQuad(image, src, &dst, nullptr, GrAA(paint.isAntiAlias()), aaFlags, nullptr,
1331 paint, constraint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001332}
1333
Leon Scroggins III57e1f022018-04-20 14:53:00 -04001334// When drawing nine-patches or n-patches, cap the filter quality at kBilerp.
1335static GrSamplerState::Filter compute_lattice_filter_mode(const SkPaint& paint) {
1336 if (paint.getFilterQuality() == kNone_SkFilterQuality) {
1337 return GrSamplerState::Filter::kNearest;
1338 }
1339
1340 return GrSamplerState::Filter::kBilerp;
1341}
1342
Mike Reeda1361362017-03-07 09:37:29 -05001343void SkGpuDevice::drawImageNine(const SkImage* image,
bsalomon2bbd0c62015-12-09 12:50:56 -08001344 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001345 ASSERT_SINGLE_OWNER
reed2d5b7142016-08-17 11:12:33 -07001346 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001347 auto iter = skstd::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
Robert Phillips6603a172019-03-05 12:35:44 -05001348 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
1349 &pinnedUniqueID)) {
Brian Salomond6287472019-06-24 15:50:07 -04001350 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1351 SkColorTypeToGrColorType(image->colorType()), image->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001352 pinnedUniqueID, image->colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001353 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001354 } else {
1355 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001356 if (image->isLazyGenerated()) {
1357 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001358 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001359 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001360 GrBitmapTextureMaker maker(fContext.get(), bm);
1361 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001362 }
1363 }
1364}
1365
Mike Reeda1361362017-03-07 09:37:29 -05001366void SkGpuDevice::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
bsalomon2bbd0c62015-12-09 12:50:56 -08001367 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001368 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001369 auto iter = skstd::make_unique<SkLatticeIter>(bitmap.width(), bitmap.height(), center, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001370 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001371 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001372}
1373
Mike Reeda1361362017-03-07 09:37:29 -05001374void SkGpuDevice::drawProducerLattice(GrTextureProducer* producer,
Brian Salomon2a943df2018-05-04 13:43:19 -04001375 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst,
1376 const SkPaint& origPaint) {
Hal Canary144caf52016-11-07 17:57:18 -05001377 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerLattice", fContext.get());
Brian Salomon2a943df2018-05-04 13:43:19 -04001378 SkTCopyOnFirstWrite<SkPaint> paint(&origPaint);
msarett10e3d9b2016-08-18 15:46:03 -07001379
Brian Salomon2a943df2018-05-04 13:43:19 -04001380 if (!producer->isAlphaOnly() && (paint->getColor() & 0x00FFFFFF) != 0x00FFFFFF) {
1381 paint.writable()->setColor(SkColorSetARGB(origPaint.getAlpha(), 0xFF, 0xFF, 0xFF));
1382 }
msarett10e3d9b2016-08-18 15:46:03 -07001383 GrPaint grPaint;
Brian Salomon2a943df2018-05-04 13:43:19 -04001384 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), fRenderTargetContext->colorSpaceInfo(),
1385 *paint, &grPaint)) {
msarett10e3d9b2016-08-18 15:46:03 -07001386 return;
1387 }
1388
Brian Salomon2a943df2018-05-04 13:43:19 -04001389 auto dstColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
1390 const GrSamplerState::Filter filter = compute_lattice_filter_mode(*paint);
Michael Ludwigddeed372019-02-20 16:50:10 -05001391 auto proxy = producer->refTextureProxyForParams(&filter, nullptr);
Brian Salomona8daee82018-05-07 14:51:18 -04001392 if (!proxy) {
1393 return;
1394 }
Brian Osman6064e1c2018-10-19 14:27:54 -04001395 auto csxf = GrColorSpaceXform::Make(producer->colorSpace(), producer->alphaType(),
1396 dstColorSpace, kPremul_SkAlphaType);
Brian Salomon2a943df2018-05-04 13:43:19 -04001397
Brian Salomon0166cfd2017-03-13 17:58:25 -04001398 fRenderTargetContext->drawImageLattice(this->clip(), std::move(grPaint), this->ctm(),
Brian Salomon2a943df2018-05-04 13:43:19 -04001399 std::move(proxy), std::move(csxf), filter,
1400 std::move(iter), dst);
msarett10e3d9b2016-08-18 15:46:03 -07001401}
1402
Mike Reeda1361362017-03-07 09:37:29 -05001403void SkGpuDevice::drawImageLattice(const SkImage* image,
msarett10e3d9b2016-08-18 15:46:03 -07001404 const SkCanvas::Lattice& lattice, const SkRect& dst,
1405 const SkPaint& paint) {
1406 ASSERT_SINGLE_OWNER
1407 uint32_t pinnedUniqueID;
Brian Salomon2a943df2018-05-04 13:43:19 -04001408 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Robert Phillips6603a172019-03-05 12:35:44 -05001409 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
1410 &pinnedUniqueID)) {
Brian Salomond6287472019-06-24 15:50:07 -04001411 GrTextureAdjuster adjuster(this->context(), std::move(proxy),
1412 SkColorTypeToGrColorType(image->colorType()), image->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001413 pinnedUniqueID, image->colorSpace());
Brian Salomon2a943df2018-05-04 13:43:19 -04001414 this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001415 } else {
1416 SkBitmap bm;
Brian Osmandf7e0752017-04-26 16:20:28 -04001417 if (image->isLazyGenerated()) {
1418 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
Brian Salomon2a943df2018-05-04 13:43:19 -04001419 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
Brian Osmane50cdf02018-10-19 13:02:14 -04001420 } else if (as_IB(image)->getROPixels(&bm)) {
Brian Salomon2a943df2018-05-04 13:43:19 -04001421 GrBitmapTextureMaker maker(fContext.get(), bm);
1422 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001423 }
1424 }
1425}
1426
Mike Reeda1361362017-03-07 09:37:29 -05001427void SkGpuDevice::drawBitmapLattice(const SkBitmap& bitmap,
msarett10e3d9b2016-08-18 15:46:03 -07001428 const SkCanvas::Lattice& lattice, const SkRect& dst,
1429 const SkPaint& paint) {
1430 ASSERT_SINGLE_OWNER
Brian Salomon2a943df2018-05-04 13:43:19 -04001431 auto iter = skstd::make_unique<SkLatticeIter>(lattice, dst);
Hal Canary144caf52016-11-07 17:57:18 -05001432 GrBitmapTextureMaker maker(fContext.get(), bitmap);
Brian Salomon2a943df2018-05-04 13:43:19 -04001433 this->drawProducerLattice(&maker, std::move(iter), dst, paint);
msarett10e3d9b2016-08-18 15:46:03 -07001434}
1435
Brian Salomonf3569f02017-10-24 12:52:33 -04001436static bool init_vertices_paint(GrContext* context, const GrColorSpaceInfo& colorSpaceInfo,
1437 const SkPaint& skPaint, const SkMatrix& matrix, SkBlendMode bmode,
Robert Phillips26c90e02017-03-14 14:39:29 -04001438 bool hasTexs, bool hasColors, GrPaint* grPaint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001439 if (hasTexs && skPaint.getShader()) {
1440 if (hasColors) {
1441 // When there are texs and colors the shader and colors are combined using bmode.
Brian Salomonf3569f02017-10-24 12:52:33 -04001442 return SkPaintToGrPaintWithXfermode(context, colorSpaceInfo, skPaint, matrix, bmode,
1443 grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001444 } else {
1445 // We have a shader, but no colors to blend it against.
Brian Salomonf3569f02017-10-24 12:52:33 -04001446 return SkPaintToGrPaint(context, colorSpaceInfo, skPaint, matrix, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001447 }
1448 } else {
1449 if (hasColors) {
1450 // We have colors, but either have no shader or no texture coords (which implies that
1451 // we should ignore the shader).
Brian Salomonf3569f02017-10-24 12:52:33 -04001452 return SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001453 } else {
1454 // No colors and no shaders. Just draw with the paint color.
Brian Salomonf3569f02017-10-24 12:52:33 -04001455 return SkPaintToGrPaintNoShader(context, colorSpaceInfo, skPaint, grPaint);
Brian Salomon199fb872017-02-06 09:41:10 -05001456 }
1457 }
1458}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001459
Mike Reed887cdf12017-04-03 11:11:09 -04001460void SkGpuDevice::wireframeVertices(SkVertices::VertexMode vmode, int vertexCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001461 const SkPoint vertices[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001462 const SkVertices::Bone bones[], int boneCount,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001463 SkBlendMode bmode,
Mike Reed2f6b5a42017-03-19 15:04:17 -04001464 const uint16_t indices[], int indexCount,
1465 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001466 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001467 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "wireframeVertices", fContext.get());
mtklein533eb782014-08-27 10:39:42 -07001468
Mike Reed2f6b5a42017-03-19 15:04:17 -04001469 SkPaint copy(paint);
1470 copy.setStyle(SkPaint::kStroke_Style);
1471 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001472
Mike Reed2f6b5a42017-03-19 15:04:17 -04001473 GrPaint grPaint;
1474 // we ignore the shader since we have no texture coordinates.
Brian Salomonf3569f02017-10-24 12:52:33 -04001475 if (!SkPaintToGrPaintNoShader(this->context(), fRenderTargetContext->colorSpaceInfo(), copy,
1476 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001477 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001478 }
1479
Mike Reed2f6b5a42017-03-19 15:04:17 -04001480 int triangleCount = 0;
1481 int n = (nullptr == indices) ? vertexCount : indexCount;
1482 switch (vmode) {
Mike Reed887cdf12017-04-03 11:11:09 -04001483 case SkVertices::kTriangles_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001484 triangleCount = n / 3;
1485 break;
Mike Reed887cdf12017-04-03 11:11:09 -04001486 case SkVertices::kTriangleStrip_VertexMode:
Mike Reed2f6b5a42017-03-19 15:04:17 -04001487 triangleCount = n - 2;
1488 break;
Brian Salomoncccafe82018-04-28 16:13:08 -04001489 case SkVertices::kTriangleFan_VertexMode:
1490 SK_ABORT("Unexpected triangle fan.");
1491 break;
Mike Reed2f6b5a42017-03-19 15:04:17 -04001492 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493
Mike Reed2f6b5a42017-03-19 15:04:17 -04001494 VertState state(vertexCount, indices, indexCount);
1495 VertState::Proc vertProc = state.chooseProc(vmode);
1496
1497 //number of indices for lines per triangle with kLines
1498 indexCount = triangleCount * 6;
1499
Brian Osmanae0c50c2017-05-25 16:56:34 -04001500 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
1501 SkVertices::Builder builder(kIgnoredMode, vertexCount, indexCount, 0);
1502 memcpy(builder.positions(), vertices, vertexCount * sizeof(SkPoint));
1503
1504 uint16_t* lineIndices = builder.indices();
Mike Reed2f6b5a42017-03-19 15:04:17 -04001505 int i = 0;
1506 while (vertProc(&state)) {
1507 lineIndices[i] = state.f0;
1508 lineIndices[i + 1] = state.f1;
1509 lineIndices[i + 2] = state.f1;
1510 lineIndices[i + 3] = state.f2;
1511 lineIndices[i + 4] = state.f2;
1512 lineIndices[i + 5] = state.f0;
1513 i += 6;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001514 }
Brian Osmanae0c50c2017-05-25 16:56:34 -04001515
Chris Dalton3809bab2017-06-13 10:55:06 -06001516 GrPrimitiveType primitiveType = GrPrimitiveType::kLines;
Brian Salomon0166cfd2017-03-13 17:58:25 -04001517 fRenderTargetContext->drawVertices(this->clip(),
Brian Salomon82f44312017-01-11 13:42:54 -05001518 std::move(grPaint),
Mike Reeda1361362017-03-07 09:37:29 -05001519 this->ctm(),
Brian Osmanae0c50c2017-05-25 16:56:34 -04001520 builder.detach(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001521 bones,
1522 boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001523 &primitiveType);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524}
1525
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001526void SkGpuDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
1527 int boneCount, SkBlendMode mode, const SkPaint& paint) {
Brian Salomon199fb872017-02-06 09:41:10 -05001528 ASSERT_SINGLE_OWNER
Mike Reed2f6b5a42017-03-19 15:04:17 -04001529 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext.get());
Brian Salomon199fb872017-02-06 09:41:10 -05001530
1531 SkASSERT(vertices);
1532 GrPaint grPaint;
Mike Reed5fa66452017-03-16 09:06:34 -04001533 bool hasColors = vertices->hasColors();
1534 bool hasTexs = vertices->hasTexCoords();
Brian Osman0b403f82017-05-26 10:39:51 -04001535 if ((!hasTexs || !paint.getShader()) && !hasColors) {
Brian Salomon199fb872017-02-06 09:41:10 -05001536 // The dreaded wireframe mode. Fallback to drawVertices and go so slooooooow.
Mike Reed2f6b5a42017-03-19 15:04:17 -04001537 this->wireframeVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001538 bones, boneCount, mode, vertices->indices(), vertices->indexCount(),
1539 paint);
Brian Osman0b403f82017-05-26 10:39:51 -04001540 return;
Brian Salomon199fb872017-02-06 09:41:10 -05001541 }
Brian Salomonf3569f02017-10-24 12:52:33 -04001542 if (!init_vertices_paint(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
1543 this->ctm(), mode, hasTexs, hasColors, &grPaint)) {
Brian Salomon199fb872017-02-06 09:41:10 -05001544 return;
1545 }
Brian Salomon0166cfd2017-03-13 17:58:25 -04001546 fRenderTargetContext->drawVertices(this->clip(), std::move(grPaint), this->ctm(),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001547 sk_ref_sp(const_cast<SkVertices*>(vertices)),
1548 bones, boneCount);
Brian Salomon199fb872017-02-06 09:41:10 -05001549}
1550
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551///////////////////////////////////////////////////////////////////////////////
1552
Jim Van Verth3af1af92017-05-18 15:06:54 -04001553void SkGpuDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
1554
1555 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -04001556 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawShadow", fContext.get());
1557
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001558 if (!fRenderTargetContext->drawFastShadow(this->clip(), this->ctm(), path, rec)) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001559 // failed to find an accelerated case
1560 this->INHERITED::drawShadow(path, rec);
1561 }
1562}
1563
1564///////////////////////////////////////////////////////////////////////////////
1565
Brian Osman4d92b892019-03-24 00:53:23 +00001566void SkGpuDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
1567 const SkRect texRect[], const SkColor colors[], int count,
1568 SkBlendMode mode, const SkPaint& paint) {
1569 ASSERT_SINGLE_OWNER
Brian Osmanddba4e62019-03-25 09:52:17 -04001570 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawAtlas", fContext.get());
Brian Osman4d92b892019-03-24 00:53:23 +00001571
1572 SkPaint p(paint);
1573 p.setShader(atlas->makeShader());
1574
1575 GrPaint grPaint;
1576 if (colors) {
1577 if (!SkPaintToGrPaintWithXfermode(this->context(), fRenderTargetContext->colorSpaceInfo(),
1578 p, this->ctm(), (SkBlendMode)mode, &grPaint)) {
1579 return;
1580 }
1581 } else {
1582 if (!SkPaintToGrPaint(this->context(), fRenderTargetContext->colorSpaceInfo(), p,
1583 this->ctm(), &grPaint)) {
1584 return;
1585 }
1586 }
1587
1588 fRenderTargetContext->drawAtlas(
1589 this->clip(), std::move(grPaint), this->ctm(), count, xform, texRect, colors);
1590}
1591
1592///////////////////////////////////////////////////////////////////////////////
1593
Herb Derbyb935cf82018-07-26 16:54:18 -04001594void SkGpuDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
Herb Derbyb983e6b2018-07-13 13:26:29 -04001595 ASSERT_SINGLE_OWNER
1596 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawGlyphRunList", fContext.get());
Herb Derbyb983e6b2018-07-13 13:26:29 -04001597
Jim Van Verth87a30112018-09-24 16:13:58 -04001598 // Check for valid input
1599 const SkMatrix& ctm = this->ctm();
Mike Reed96345a22019-01-02 21:30:29 -05001600 if (!ctm.isFinite() || !glyphRunList.allFontsFinite()) {
Jim Van Verth87a30112018-09-24 16:13:58 -04001601 return;
1602 }
1603
1604 fRenderTargetContext->drawGlyphRunList(this->clip(), ctm, glyphRunList);
Herb Derbyb983e6b2018-07-13 13:26:29 -04001605}
1606
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607///////////////////////////////////////////////////////////////////////////////
1608
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001609void SkGpuDevice::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix, SkCanvas* canvas) {
Robert Phillips4217ea72019-01-30 13:08:28 -05001610 GrBackendApi api = this->context()->backend();
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001611 if (GrBackendApi::kVulkan == api) {
1612 const SkMatrix& ctm = canvas->getTotalMatrix();
1613 const SkMatrix& combinedMatrix = matrix ? SkMatrix::Concat(ctm, *matrix) : ctm;
1614 std::unique_ptr<SkDrawable::GpuDrawHandler> gpuDraw =
Derek Sollenbergere6fb76b2019-01-10 13:19:06 -05001615 drawable->snapGpuDrawHandler(api, combinedMatrix, canvas->getDeviceClipBounds(),
1616 this->imageInfo());
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001617 if (gpuDraw) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001618 fRenderTargetContext->drawDrawable(std::move(gpuDraw), drawable->getBounds());
1619 return;
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001620 }
1621 }
1622 this->INHERITED::drawDrawable(drawable, matrix, canvas);
1623}
1624
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001625
Greg Daniel9ed1a2c2018-10-18 12:43:25 -04001626///////////////////////////////////////////////////////////////////////////////
1627
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001628void SkGpuDevice::flush() {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001629 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo());
Greg Daniela5cb7812017-06-16 09:45:32 -04001630}
1631
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001632GrSemaphoresSubmitted SkGpuDevice::flush(SkSurface::BackendSurfaceAccess access,
1633 const GrFlushInfo& info) {
joshualittce894002016-01-11 13:29:31 -08001634 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001635
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001636 return fRenderTargetContext->flush(access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001637}
1638
Greg Danielc64ee462017-06-15 16:59:49 -04001639bool SkGpuDevice::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001640 ASSERT_SINGLE_OWNER
1641
Greg Danielc64ee462017-06-15 16:59:49 -04001642 return fRenderTargetContext->waitOnSemaphores(numSemaphores, waitSemaphores);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643}
1644
1645///////////////////////////////////////////////////////////////////////////////
1646
reed76033be2015-03-14 10:54:31 -07001647SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001648 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649
robertphillipsca6eafc2016-05-17 09:57:46 -07001650 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651
robertphillipsca6eafc2016-05-17 09:57:46 -07001652 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001653 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001654 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1655 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001656
Florin Malitaea795742019-07-14 14:21:23 +00001657 GrColorType colorType = fRenderTargetContext->colorSpaceInfo().colorType();
1658 if (colorType == GrColorType::kRGBA_1010102) {
1659 // If the original device is 1010102, fall back to 8888 so that we have a usable alpha
1660 // channel in the layer.
1661 colorType = GrColorType::kRGBA_8888;
1662 }
Brian Osman10fc6fd2018-03-02 11:01:10 -05001663
Robert Phillips9da87e02019-02-04 13:26:26 -05001664 sk_sp<GrRenderTargetContext> rtc(fContext->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -04001665 fit,
1666 cinfo.fInfo.width(),
1667 cinfo.fInfo.height(),
Florin Malitaea795742019-07-14 14:21:23 +00001668 colorType,
Brian Salomonf3569f02017-10-24 12:52:33 -04001669 fRenderTargetContext->colorSpaceInfo().refColorSpace(),
Brian Salomond6287472019-06-24 15:50:07 -04001670 fRenderTargetContext->numSamples(),
1671 GrMipMapped::kNo,
1672 kBottomLeft_GrSurfaceOrigin,
1673 &props,
1674 SkBudgeted::kYes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001675 fRenderTargetContext->asSurfaceProxy()->isProtected() ? GrProtected::kYes
1676 : GrProtected::kNo));
Brian Osman11052242016-10-27 14:47:55 -04001677 if (!rtc) {
Mike Kleine54c75f2016-10-13 14:18:09 -04001678 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001680
1681 // Skia's convention is to only clear a device if it is non-opaque.
1682 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1683
Robert Phillips9fab7e92016-11-17 12:45:04 -05001684 return SkGpuDevice::Make(fContext.get(), std::move(rtc),
1685 cinfo.fInfo.width(), cinfo.fInfo.height(), init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686}
1687
reede8f30622016-03-23 18:59:25 -07001688sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001689 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001690 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001691 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
Hal Canary144caf52016-11-07 17:57:18 -05001692 return SkSurface::MakeRenderTarget(fContext.get(), kBudgeted, info,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001693 fRenderTargetContext->numSamples(),
Brian Osman11052242016-10-27 14:47:55 -04001694 fRenderTargetContext->origin(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001695}
1696
senorblanco900c3672016-04-27 11:31:23 -07001697SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001698 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001699 // We always return a transient cache, so it is freed after each
1700 // filter traversal.
brianosman04a44d02016-09-21 09:46:57 -07001701 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001702}
reedf037e0b2014-10-30 11:34:15 -07001703