blob: 6d5d0f0c07b852e4bfcb66e21114d5aa7d782eef [file] [log] [blame]
bsalomonc55271f2015-11-09 11:55:57 -08001/*
2 * Copyright 2015 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"
Michael Ludwig1433cfd2019-02-27 17:12:30 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkYUVAIndex.h"
11#include "src/core/SkDraw.h"
12#include "src/core/SkMaskFilterBase.h"
13#include "src/gpu/GrBitmapTextureMaker.h"
14#include "src/gpu/GrBlurUtils.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrColorSpaceXform.h"
17#include "src/gpu/GrImageTextureMaker.h"
18#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrStyle.h"
20#include "src/gpu/GrTextureAdjuster.h"
21#include "src/gpu/GrTextureMaker.h"
22#include "src/gpu/SkGr.h"
23#include "src/gpu/effects/GrBicubicEffect.h"
24#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050025#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080028
Michael Ludwig1433cfd2019-02-27 17:12:30 -050029namespace {
30
bsalomonc55271f2015-11-09 11:55:57 -080031static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
32 return textureIsAlphaOnly && paint.getShader();
33}
34
bsalomonb1b01992015-11-18 10:56:08 -080035//////////////////////////////////////////////////////////////////////////////
36// Helper functions for dropping src rect constraint in bilerp mode.
37
38static const SkScalar kColorBleedTolerance = 0.001f;
39
40static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
41 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050042 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
43 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
44 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080045 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
46 return true;
47 }
48 return false;
49}
50
51static bool may_color_bleed(const SkRect& srcRect,
52 const SkRect& transformedRect,
53 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060054 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080055 // Only gets called if has_aligned_samples returned false.
56 // So we can assume that sampling is axis aligned but not texel aligned.
57 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
58 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Chris Dalton6ce447a2019-06-23 18:07:38 -060059 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080060 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
61 } else {
62 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
63 }
64 m.mapRect(&innerTransformedRect, innerSrcRect);
65
66 // The gap between outerTransformedRect and innerTransformedRect
67 // represents the projection of the source border area, which is
68 // problematic for color bleeding. We must check whether any
69 // destination pixels sample the border area.
70 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
71 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
72 SkIRect outer, inner;
73 outerTransformedRect.round(&outer);
74 innerTransformedRect.round(&inner);
75 // If the inner and outer rects round to the same result, it means the
76 // border does not overlap any pixel centers. Yay!
77 return inner != outer;
78}
79
80static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
81 const SkRect& srcRect,
82 const SkMatrix& srcRectToDeviceSpace,
Chris Dalton6ce447a2019-06-23 18:07:38 -060083 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080084 if (srcRectToDeviceSpace.rectStaysRect()) {
85 // sampling is axis-aligned
86 SkRect transformedRect;
87 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
88
89 if (has_aligned_samples(srcRect, transformedRect) ||
Chris Dalton6ce447a2019-06-23 18:07:38 -060090 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080091 return true;
92 }
93 }
94 return false;
95}
96
Michael Ludwig1433cfd2019-02-27 17:12:30 -050097enum class ImageDrawMode {
98 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
99 kOptimized,
100 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
101 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
102 kDecal,
103 // Src or dst are empty, or do not intersect the image content so don't draw anything.
104 kSkip
105};
106
107/**
108 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
109 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
110 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
111 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
112 *
113 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
114 * original src rect. 'dstClip' should be null when there is no additional clipping.
115 */
116static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
117 const SkRect* origDstRect, const SkPoint dstClip[4],
118 SkRect* outSrcRect, SkRect* outDstRect,
119 SkMatrix* srcToDst) {
120 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
121
122 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
123 SkRect dst = origDstRect ? *origDstRect : src;
124
125 if (src.isEmpty() || dst.isEmpty()) {
126 return ImageDrawMode::kSkip;
127 }
128
129 if (outDstRect) {
130 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
131 } else {
132 srcToDst->setIdentity();
133 }
134
135 if (origSrcRect && !srcBounds.contains(src)) {
136 if (!src.intersect(srcBounds)) {
137 return ImageDrawMode::kSkip;
138 }
139 srcToDst->mapRect(&dst, src);
140
141 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
142 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
143 if (dstClip) {
144 for (int i = 0; i < 4; ++i) {
145 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
146 // Must resort to using a decal mode restricted to the clipped 'src', and
147 // use the original dst rect (filling in src bounds as needed)
148 *outSrcRect = src;
149 *outDstRect = (origDstRect ? *origDstRect
150 : (origSrcRect ? *origSrcRect : srcBounds));
151 return ImageDrawMode::kDecal;
152 }
153 }
154 }
155 }
156
157 // The original src and dst were fully contained in the image, or there was no dst clip to
158 // worry about, or the clip was still contained in the restricted dst rect.
159 *outSrcRect = src;
160 *outDstRect = dst;
161 return ImageDrawMode::kOptimized;
162}
163
Brian Salomon34169692017-08-28 15:32:01 -0400164/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400165 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
166 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400167 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400168static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400169 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500170 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400171}
172
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500173// Assumes srcRect and dstRect have already been optimized to fit the proxy
174static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
175 const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
176 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
177 SkCanvas::SrcRectConstraint constraint, sk_sp<GrTextureProxy> proxy,
Greg Daniela4828a12019-10-11 13:51:02 -0400178 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400179 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400180 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400181 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500182 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400183 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400184 switch (paint.getFilterQuality()) {
185 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400186 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400187 break;
188 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400189 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400190 break;
191 case kMedium_SkFilterQuality:
192 case kHigh_SkFilterQuality:
193 SK_ABORT("Quality level not allowed.");
194 }
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500195
196 // Must specify the strict constraint when the proxy is not functionally exact and the src
197 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500198 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500199 // Conservative estimate of how much a coord could be outset from src rect:
200 // 1/2 pixel for AA and 1/2 pixel for bilerp
201 float buffer = 0.5f * (aa == GrAA::kYes) +
202 0.5f * (filter == GrSamplerState::Filter::kBilerp);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400203 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500204 safeBounds.inset(buffer, buffer);
205 if (!safeBounds.contains(srcRect)) {
206 constraint = SkCanvas::kStrict_SrcRectConstraint;
207 }
208 }
Brian Osman3d139a42018-11-19 10:42:10 -0500209 SkPMColor4f color;
Greg Daniela4828a12019-10-11 13:51:02 -0400210 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400211 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400212 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500213 float paintAlpha = paint.getColor4f().fA;
214 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400215 }
Brian Salomon34169692017-08-28 15:32:01 -0400216
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500217 if (dstClip) {
218 // Get source coords corresponding to dstClip
219 SkPoint srcQuad[4];
220 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000221
Brian Salomonfc118442019-11-22 19:09:27 -0500222 rtc->drawTextureQuad(clip, std::move(proxy), srcColorInfo.colorType(),
223 srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad,
224 dstClip, aa, aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500225 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
226 ctm, std::move(textureXform));
227 } else {
Brian Salomonfc118442019-11-22 19:09:27 -0500228 rtc->drawTexture(clip, std::move(proxy), srcColorInfo.colorType(), srcColorInfo.alphaType(),
229 filter, paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags,
230 constraint, ctm, std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000231 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000232}
233
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500234// Assumes srcRect and dstRect have already been optimized to fit the proxy.
235static void draw_texture_producer(GrContext* context, GrRenderTargetContext* rtc,
236 const GrClip& clip, const SkMatrix& ctm,
237 const SkPaint& paint, GrTextureProducer* producer,
238 const SkRect& src, const SkRect& dst, const SkPoint dstClip[4],
239 const SkMatrix& srcToDst, GrAA aa, GrQuadAAFlags aaFlags,
240 SkCanvas::SrcRectConstraint constraint, bool attemptDrawTexture) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400241 if (attemptDrawTexture && can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400242 // We've done enough checks above to allow us to pass ClampNearest() and not check for
243 // scaling adjustments.
Greg Daniel82c6b102020-01-21 10:33:22 -0500244 auto [proxy, ct] = producer->refTextureProxy(GrMipMapped::kNo);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400245 if (!proxy) {
246 return;
247 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500248
249 draw_texture(rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint,
Greg Daniel82c6b102020-01-21 10:33:22 -0500250 std::move(proxy),
251 {ct, producer->alphaType(), sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400252 return;
253 }
254
bsalomonc55271f2015-11-09 11:55:57 -0800255 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500256
bsalomonc55271f2015-11-09 11:55:57 -0800257 // The shader expects proper local coords, so we can't replace local coords with texture coords
258 // if the shader will be used. If we have a mask filter we will change the underlying geometry
259 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800260 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800261
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500262 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500263 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500264 // FP. In the future this should be an opaque optimization enabled by the combination of
265 // GrDrawOp/GP and FP.
266 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
267 mf = nullptr;
268 }
bsalomonc55271f2015-11-09 11:55:57 -0800269 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400270 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600271 producer->width(), producer->height(), paint.getFilterQuality(), ctm, srcToDst,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500272 context->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400273 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800274
Brian Osmane8e54582016-11-28 10:06:27 -0500275 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800276 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
277 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
278 } else {
279 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
280 }
halcanary9d524f22016-03-29 09:03:52 -0700281
bsalomonc55271f2015-11-09 11:55:57 -0800282 // If we have to outset for AA then we will generate texture coords outside the src rect. The
283 // same happens for any mask filter that extends the bounds rendered in the dst.
284 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500285 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800286
bsalomonb1b01992015-11-18 10:56:08 -0800287 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400288 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400289 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect &&
290 !producer->hasMixedResolutions()) {
bsalomonb1b01992015-11-18 10:56:08 -0800291 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500292 combinedMatrix.setConcat(ctm, srcToDst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600293 if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) {
bsalomonb1b01992015-11-18 10:56:08 -0800294 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
295 }
296 }
297
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500298 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800299 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500300 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800301 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500302 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800303 return;
304 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800305 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500306 auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
Brian Osman05c8f462018-10-22 17:13:36 -0400307 coordsAllInsideSrcRect, filterMode);
308 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400309 rtc->colorInfo().colorSpace());
bsalomonc55271f2015-11-09 11:55:57 -0800310 if (!fp) {
311 return;
312 }
joshualitt33a5fce2015-11-18 13:28:51 -0800313
bsalomonc55271f2015-11-09 11:55:57 -0800314 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400315 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, ctm, std::move(fp),
316 producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800317 return;
318 }
319
320 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500321 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
322 if (dstClip) {
323 SkPoint srcClipPoints[4];
324 SkPoint* srcClip = nullptr;
325 if (canUseTextureCoordsAsLocalCoords) {
326 // Calculate texture coordinates that match the dst clip
327 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
328 srcClip = srcClipPoints;
329 }
330 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
331 } else {
332 // Provide explicit texture coords when possible, otherwise rely on texture matrix
333 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
334 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
335 }
336 } else {
337 // Must draw the mask filter as a GrShape. For now, this loses the per-edge AA information
338 // since it always draws with AA, but that is should not be noticeable since the mask filter
339 // is probably a blur.
340 GrShape shape;
341 if (dstClip) {
342 // Represent it as an SkPath formed from the dstClip
343 SkPath path;
344 path.addPoly(dstClip, 4, true);
345 shape = GrShape(path);
346 } else {
347 shape = GrShape(dst);
348 }
349
350 GrBlurUtils::drawShapeWithMaskFilter(
351 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
352 }
353}
354
355} // anonymous namespace
356
357//////////////////////////////////////////////////////////////////////////////
358
359void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
360 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500361 const SkMatrix* preViewMatrix, const SkPaint& paint,
362 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500363 SkRect src;
364 SkRect dst;
365 SkMatrix srcToDst;
366 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
367 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
368 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800369 return;
370 }
371
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500372 if (src.contains(image->bounds())) {
373 constraint = SkCanvas::kFast_SrcRectConstraint;
374 }
375 // Depending on the nature of image, it can flow through more or less optimal pipelines
376 bool useDecal = mode == ImageDrawMode::kDecal;
377 bool attemptDrawTexture = !useDecal; // rtc->drawTexture() only clamps
Robert Phillips27927a52018-08-20 13:18:12 -0400378
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500379 // Get final CTM matrix
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400380 SkMatrix ctm = this->localToDevice();
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500381 if (preViewMatrix) {
382 ctm.preConcat(*preViewMatrix);
383 }
384
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500385 // YUVA images can be stored in multiple images with different plane resolutions, so this
386 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
387 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
388 if (as_IB(image)->isYUVA()) {
389 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500390 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500391
392 GrYUVAImageTextureMaker maker(fContext.get(), image, useDecal);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500393 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500394 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
395 /* attempt draw texture */ false);
396 return;
397 }
398
399 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
400 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
401 uint32_t pinnedUniqueID;
Robert Phillips6603a172019-03-05 12:35:44 -0500402 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
403 &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500404 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500405 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500406
Greg Daniel82c6b102020-01-21 10:33:22 -0500407 GrColorInfo colorInfo;
408 if (fContext->priv().caps()->isFormatSRGB(proxy->backendFormat())) {
409 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
410 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
411 image->imageInfo().refColorSpace());
412 } else {
413 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
414 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500415
416 if (attemptDrawTexture && can_use_draw_texture(paint)) {
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500417 draw_texture(fRenderTargetContext.get(), this->clip(), ctm, paint, src, dst,
Greg Daniela4828a12019-10-11 13:51:02 -0400418 dstClip, aa, aaFlags, constraint, std::move(proxy), colorInfo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500419 return;
420 }
Greg Daniela4828a12019-10-11 13:51:02 -0400421 GrTextureAdjuster adjuster(fContext.get(), std::move(proxy), colorInfo, pinnedUniqueID,
422 useDecal);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500423 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500424 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
425 constraint, /* attempt draw_texture */ false);
426 return;
427 }
428
429 // Next up, try tiling the image
430 // TODO (michaelludwig): Implement this with per-edge AA flags to handle seaming properly
431 // instead of going through drawBitmapRect (which will be removed from SkDevice in the future)
432 SkBitmap bm;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500433 if (this->shouldTileImage(image, &src, constraint, paint.getFilterQuality(), ctm, srcToDst)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500434 // only support tiling as bitmap at the moment, so force raster-version
Jim Van Verth352f4f72019-10-29 15:26:00 -0400435 if (as_IB(image)->getROPixels(&bm)) {
436 this->drawBitmapRect(bm, &src, dst, paint, constraint);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500437 return;
438 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500439 }
440
441 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
442 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500443 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500444
445 // Lazily generated images must get drawn as a texture producer that handles the final
446 // texture creation.
447 if (image->isLazyGenerated()) {
448 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint, useDecal);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500449 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500450 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
451 attemptDrawTexture);
452 return;
453 }
454 if (as_IB(image)->getROPixels(&bm)) {
455 GrBitmapTextureMaker maker(fContext.get(), bm, useDecal);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500456 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500457 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
458 attemptDrawTexture);
459 }
460
461 // Otherwise don't know how to draw it
462}
463
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400464void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
465 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
466 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500467 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400468 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500469 // Send every entry through drawImageQuad() to handle the more complicated paint
470 int dstClipIndex = 0;
471 for (int i = 0; i < count; ++i) {
472 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400473 SkASSERT(!set[i].fHasClip || dstClips);
474 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500475
Brian Salomondb151e02019-09-17 12:11:16 -0400476 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
477 if (set[i].fAlpha != 1.f) {
478 auto paintAlpha = paint.getAlphaf();
479 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
480 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500481 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400482 this->drawImageQuad(
483 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
484 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
485 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400486 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400487 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400488 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500489 }
490 return;
491 }
492
493 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
494 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
495 SkBlendMode mode = paint.getBlendMode();
496
497 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
498 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500499 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
500 // switches that occur within the 'n' entries.
501 int base = 0, n = 0, p = 0;
502 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500503 if (n > 0) {
504 auto textureXform = GrColorSpaceXform::Make(
505 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400506 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Michael Ludwig379e4962019-12-06 13:21:26 -0500507 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400508 filter, mode, GrAA::kYes, constraint,
509 this->localToDevice(), std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500510 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500511 base = nextBase;
512 n = 0;
513 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500514 };
515 int dstClipIndex = 0;
516 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400517 SkASSERT(!set[i].fHasClip || dstClips);
518 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
519
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500520 // Manage the dst clip pointer tracking before any continues are used so we don't lose
521 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400522 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
523 dstClipIndex += 4 * set[i].fHasClip;
524
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500525 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
526 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
527 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500528 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500529 continue;
530 }
531
Michael Ludwigd9958f82019-03-21 13:08:36 -0400532 sk_sp<GrTextureProxy> proxy;
533 const SkImage_Base* image = as_IB(set[i].fImage.get());
534 // Extract proxy from image, but skip YUV images so they get processed through
535 // drawImageQuad and the proper effect to dynamically sample their planes.
536 if (!image->isYUVA()) {
537 uint32_t uniqueID;
538 proxy = image->refPinnedTextureProxy(this->context(), &uniqueID);
539 if (!proxy) {
Brian Salomonccb61422020-01-09 10:46:36 -0500540 proxy = image->asTextureProxyRef(this->context(), GrSamplerState::Filter::kBilerp,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400541 nullptr);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500542 }
543 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500544
Michael Ludwigd9958f82019-03-21 13:08:36 -0400545 if (!proxy) {
546 // This image can't go through the texture op, send through general image pipeline
547 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500548 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400549 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
550 if (set[i].fAlpha != 1.f) {
551 auto paintAlpha = paint.getAlphaf();
552 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
553 }
554 this->drawImageQuad(
555 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400556 SkToGrQuadAAFlags(set[i].fAAFlags),
557 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400558 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400559 continue;
560 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500561
Greg Daniel549325c2019-10-30 16:19:20 -0400562 // TODO: have refPinnedTextureProxy and asTextureProxyRef return GrSurfaceProxyViews.
563 GrSurfaceOrigin origin = proxy->origin();
564 const GrSwizzle& swizzle = proxy->textureSwizzle();
565 textures[i].fProxyView = {std::move(proxy), origin, swizzle};
Brian Salomonfc118442019-11-22 19:09:27 -0500566 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500567 textures[i].fSrcRect = set[i].fSrcRect;
568 textures[i].fDstRect = set[i].fDstRect;
569 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400570 textures[i].fPreViewMatrix =
571 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500572 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
573 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
574
575 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400576 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500577 textures[i].fProxyView.proxy(),
578 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500579 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500580 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
581 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500582 draw(i);
583 }
584 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
585 // set being accumulated so increment n, and increment p if proxies are different.
586 ++n;
587 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
588 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
589 // to i - 1).
590 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500591 }
592 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500593 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500594}
595
596// TODO (michaelludwig) - to be removed when drawBitmapRect doesn't need it anymore
597void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
598 const SkRect* srcRect,
599 const SkRect* dstRect,
600 SkCanvas::SrcRectConstraint constraint,
601 const SkMatrix& viewMatrix,
602 const SkPaint& paint,
603 bool attemptDrawTexture) {
604 // The texture refactor split the old logic of drawTextureProducer into the beginning of
605 // drawImageQuad() and into the static draw_texture_producer. Replicate necessary logic that
606 // drawImageQuad() handles.
607 SkRect src;
608 SkRect dst;
609 SkMatrix srcToDst;
Brian Salomon1a217eb2019-10-24 10:50:36 -0400610 ImageDrawMode mode = optimize_sample_area(producer->dimensions(), srcRect, dstRect, nullptr,
611 &src, &dst, &srcToDst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500612 if (mode == ImageDrawMode::kSkip) {
613 return;
614 }
615 // There's no dstClip to worry about and the producer is already made so we wouldn't be able
616 // to tell it to use decals if we had to
617 SkASSERT(mode != ImageDrawMode::kDecal);
618
619 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), viewMatrix,
620 paint, producer, src, dst, /* clip */ nullptr, srcToDst,
621 GrAA(paint.isAntiAlias()),
622 paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
623 constraint, attemptDrawTexture);
bsalomonc55271f2015-11-09 11:55:57 -0800624}