blob: 3534b6cd844ad24441a56e5b52410938d27fea3b [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"
Brian Salomonb8f098d2020-01-07 11:15:44 -050024#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000025#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080027
Michael Ludwig1433cfd2019-02-27 17:12:30 -050028namespace {
29
bsalomonc55271f2015-11-09 11:55:57 -080030static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
31 return textureIsAlphaOnly && paint.getShader();
32}
33
bsalomonb1b01992015-11-18 10:56:08 -080034//////////////////////////////////////////////////////////////////////////////
35// Helper functions for dropping src rect constraint in bilerp mode.
36
37static const SkScalar kColorBleedTolerance = 0.001f;
38
39static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
40 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050041 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
42 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
43 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080044 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
45 return true;
46 }
47 return false;
48}
49
50static bool may_color_bleed(const SkRect& srcRect,
51 const SkRect& transformedRect,
52 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060053 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080054 // Only gets called if has_aligned_samples returned false.
55 // So we can assume that sampling is axis aligned but not texel aligned.
56 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
57 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Chris Dalton6ce447a2019-06-23 18:07:38 -060058 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080059 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
60 } else {
61 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
62 }
63 m.mapRect(&innerTransformedRect, innerSrcRect);
64
65 // The gap between outerTransformedRect and innerTransformedRect
66 // represents the projection of the source border area, which is
67 // problematic for color bleeding. We must check whether any
68 // destination pixels sample the border area.
69 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
70 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
71 SkIRect outer, inner;
72 outerTransformedRect.round(&outer);
73 innerTransformedRect.round(&inner);
74 // If the inner and outer rects round to the same result, it means the
75 // border does not overlap any pixel centers. Yay!
76 return inner != outer;
77}
78
79static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
80 const SkRect& srcRect,
81 const SkMatrix& srcRectToDeviceSpace,
Chris Dalton6ce447a2019-06-23 18:07:38 -060082 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080083 if (srcRectToDeviceSpace.rectStaysRect()) {
84 // sampling is axis-aligned
85 SkRect transformedRect;
86 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
87
88 if (has_aligned_samples(srcRect, transformedRect) ||
Chris Dalton6ce447a2019-06-23 18:07:38 -060089 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080090 return true;
91 }
92 }
93 return false;
94}
95
Michael Ludwigdd86fb32020-03-10 09:55:35 -040096//////////////////////////////////////////////////////////////////////////////
97// Helper functions for tiling a large SkBitmap
98
99static const int kBmpSmallTileSize = 1 << 10;
100
101static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
102 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
103 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
104 return tilesX * tilesY;
105}
106
107static int determine_tile_size(const SkIRect& src, int maxTileSize) {
108 if (maxTileSize <= kBmpSmallTileSize) {
109 return maxTileSize;
110 }
111
112 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
113 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
114
115 maxTileTotalTileSize *= maxTileSize * maxTileSize;
116 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
117
118 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
119 return kBmpSmallTileSize;
120 } else {
121 return maxTileSize;
122 }
123}
124
125// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
126// pixels from the bitmap are necessary.
Michael Ludwigc002d562020-05-13 14:17:57 -0400127static SkIRect determine_clipped_src_rect(int width, int height,
128 const GrClip& clip,
129 const SkMatrix& viewMatrix,
130 const SkMatrix& srcToDstRect,
131 const SkISize& imageDimensions,
132 const SkRect* srcRectPtr) {
133 SkIRect clippedSrcIRect = clip.getConservativeBounds(width, height);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400134 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
135 if (!inv.invert(&inv)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400136 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400137 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400138 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400139 inv.mapRect(&clippedSrcRect);
140 if (srcRectPtr) {
141 if (!clippedSrcRect.intersect(*srcRectPtr)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400142 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400143 }
144 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400145 clippedSrcRect.roundOut(&clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400146 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
Michael Ludwigc002d562020-05-13 14:17:57 -0400147 if (!clippedSrcIRect.intersect(bmpBounds)) {
148 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400149 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400150
151 return clippedSrcIRect;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400152}
153
Michael Ludwig47237242020-03-10 16:16:17 -0400154// tileSize and clippedSubset are valid if true is returned
155static bool should_tile_image_id(GrContext* context,
156 SkISize rtSize,
157 const GrClip& clip,
158 uint32_t imageID,
159 const SkISize& imageSize,
160 const SkMatrix& ctm,
161 const SkMatrix& srcToDst,
162 const SkRect* src,
163 int maxTileSize,
164 int* tileSize,
165 SkIRect* clippedSubset) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400166 // if it's larger than the max tile size, then we have no choice but tiling.
Michael Ludwig47237242020-03-10 16:16:17 -0400167 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400168 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
169 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400170 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
171 return true;
172 }
173
174 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
Michael Ludwig47237242020-03-10 16:16:17 -0400175 const size_t area = imageSize.width() * imageSize.height();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400176 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
177 return false;
178 }
179
Michael Ludwig46d91382020-05-07 09:51:12 -0400180 // At this point we know we could do the draw by uploading the entire bitmap as a texture.
181 // However, if the texture would be large compared to the cache size and we don't require most
182 // of it for this draw then tile to reduce the amount of upload and cache spill.
183 // NOTE: if the context is not a direct context, it doesn't have access to the resource cache,
184 // and theoretically, the resource cache's limits could be being changed on another thread, so
185 // even having access to just the limit wouldn't be a reliable test during recording here.
186 // Instead, we will just upload the entire image to be on the safe side and not tile.
187 if (!context->priv().asDirectContext()) {
188 return false;
189 }
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400190
191 // assumption here is that sw bitmap size is a good proxy for its size as
192 // a texture
193 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Michael Ludwig47237242020-03-10 16:16:17 -0400194 size_t cacheSize = context->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400195 if (bmpSize < cacheSize / 2) {
196 return false;
197 }
198
199 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
200 // tiling memory savings would be < 50%.
Michael Ludwigc002d562020-05-13 14:17:57 -0400201 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
202 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400203 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
204 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
205 kBmpSmallTileSize * kBmpSmallTileSize *
206 sizeof(SkPMColor); // assume 32bit pixels;
207
208 return usedTileBytes * 2 < bmpSize;
209}
210
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400211// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
212// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
213// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400214static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400215 const SkIRect& clamp) {
216 iRect->outset(outset, outset);
217
218 int leftClampDelta = clamp.fLeft - iRect->fLeft;
219 if (leftClampDelta > 0) {
220 offset->fX -= outset - leftClampDelta;
221 iRect->fLeft = clamp.fLeft;
222 } else {
223 offset->fX -= outset;
224 }
225
226 int topClampDelta = clamp.fTop - iRect->fTop;
227 if (topClampDelta > 0) {
228 offset->fY -= outset - topClampDelta;
229 iRect->fTop = clamp.fTop;
230 } else {
231 offset->fY -= outset;
232 }
233
234 if (iRect->fRight > clamp.fRight) {
235 iRect->fRight = clamp.fRight;
236 }
237 if (iRect->fBottom > clamp.fBottom) {
238 iRect->fBottom = clamp.fBottom;
239 }
240}
241
242//////////////////////////////////////////////////////////////////////////////
243// Helper functions for drawing an image with GrRenderTargetContext
244
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500245enum class ImageDrawMode {
246 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
247 kOptimized,
248 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
249 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
250 kDecal,
251 // Src or dst are empty, or do not intersect the image content so don't draw anything.
252 kSkip
253};
254
255/**
256 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
257 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
258 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
259 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
260 *
261 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
262 * original src rect. 'dstClip' should be null when there is no additional clipping.
263 */
264static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
265 const SkRect* origDstRect, const SkPoint dstClip[4],
266 SkRect* outSrcRect, SkRect* outDstRect,
267 SkMatrix* srcToDst) {
268 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
269
270 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
271 SkRect dst = origDstRect ? *origDstRect : src;
272
273 if (src.isEmpty() || dst.isEmpty()) {
274 return ImageDrawMode::kSkip;
275 }
276
277 if (outDstRect) {
278 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
279 } else {
280 srcToDst->setIdentity();
281 }
282
283 if (origSrcRect && !srcBounds.contains(src)) {
284 if (!src.intersect(srcBounds)) {
285 return ImageDrawMode::kSkip;
286 }
287 srcToDst->mapRect(&dst, src);
288
289 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
290 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
291 if (dstClip) {
292 for (int i = 0; i < 4; ++i) {
293 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
294 // Must resort to using a decal mode restricted to the clipped 'src', and
295 // use the original dst rect (filling in src bounds as needed)
296 *outSrcRect = src;
297 *outDstRect = (origDstRect ? *origDstRect
298 : (origSrcRect ? *origSrcRect : srcBounds));
299 return ImageDrawMode::kDecal;
300 }
301 }
302 }
303 }
304
305 // The original src and dst were fully contained in the image, or there was no dst clip to
306 // worry about, or the clip was still contained in the restricted dst rect.
307 *outSrcRect = src;
308 *outDstRect = dst;
309 return ImageDrawMode::kOptimized;
310}
311
Brian Salomon34169692017-08-28 15:32:01 -0400312/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400313 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
314 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400315 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400316static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400317 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500318 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400319}
320
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500321// Assumes srcRect and dstRect have already been optimized to fit the proxy
322static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
323 const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
324 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500325 SkCanvas::SrcRectConstraint constraint, GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400326 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400327 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400328 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400329 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500330 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400331 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400332 switch (paint.getFilterQuality()) {
333 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400334 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400335 break;
336 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400337 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400338 break;
339 case kMedium_SkFilterQuality:
340 case kHigh_SkFilterQuality:
341 SK_ABORT("Quality level not allowed.");
342 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500343 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500344 // Must specify the strict constraint when the proxy is not functionally exact and the src
345 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500346 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500347 // Conservative estimate of how much a coord could be outset from src rect:
348 // 1/2 pixel for AA and 1/2 pixel for bilerp
349 float buffer = 0.5f * (aa == GrAA::kYes) +
350 0.5f * (filter == GrSamplerState::Filter::kBilerp);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400351 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500352 safeBounds.inset(buffer, buffer);
353 if (!safeBounds.contains(srcRect)) {
354 constraint = SkCanvas::kStrict_SrcRectConstraint;
355 }
356 }
Brian Osman3d139a42018-11-19 10:42:10 -0500357 SkPMColor4f color;
Greg Daniela4828a12019-10-11 13:51:02 -0400358 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400359 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400360 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500361 float paintAlpha = paint.getColor4f().fA;
362 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400363 }
Brian Salomon34169692017-08-28 15:32:01 -0400364
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500365 if (dstClip) {
366 // Get source coords corresponding to dstClip
367 SkPoint srcQuad[4];
368 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000369
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500370 rtc->drawTextureQuad(clip, std::move(view), srcColorInfo.colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -0500371 srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad,
372 dstClip, aa, aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500373 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
374 ctm, std::move(textureXform));
375 } else {
Greg Daniel40903af2020-01-30 14:55:05 -0500376 rtc->drawTexture(clip, std::move(view), srcColorInfo.alphaType(), filter,
377 paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint,
378 ctm, std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000379 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000380}
381
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500382// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Brian Salomon777e1462020-02-28 21:10:31 -0500383static void draw_texture_producer(GrContext* context,
384 GrRenderTargetContext* rtc,
385 const GrClip& clip,
Brian Osman449b1152020-04-15 16:43:00 -0400386 const SkMatrixProvider& matrixProvider,
Brian Salomon777e1462020-02-28 21:10:31 -0500387 const SkPaint& paint,
388 GrTextureProducer* producer,
389 const SkRect& src,
390 const SkRect& dst,
391 const SkPoint dstClip[4],
392 const SkMatrix& srcToDst,
393 GrAA aa,
394 GrQuadAAFlags aaFlags,
395 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400396 GrSamplerState::WrapMode wm,
397 GrSamplerState::Filter fm,
398 bool doBicubic) {
Brian Osman449b1152020-04-15 16:43:00 -0400399 const SkMatrix& ctm(matrixProvider.localToDevice());
Brian Salomon777e1462020-02-28 21:10:31 -0500400 if (wm == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
401 can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400402 // We've done enough checks above to allow us to pass ClampNearest() and not check for
403 // scaling adjustments.
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500404 auto view = producer->view(GrMipMapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500405 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400406 return;
407 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500408
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500409 draw_texture(
410 rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint, std::move(view),
411 {producer->colorType(), producer->alphaType(), sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400412 return;
413 }
414
bsalomonc55271f2015-11-09 11:55:57 -0800415 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500416
bsalomonc55271f2015-11-09 11:55:57 -0800417 // The shader expects proper local coords, so we can't replace local coords with texture coords
418 // if the shader will be used. If we have a mask filter we will change the underlying geometry
419 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800420 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800421
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500422 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500423 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500424 // FP. In the future this should be an opaque optimization enabled by the combination of
425 // GrDrawOp/GP and FP.
426 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
427 mf = nullptr;
428 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400429 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800430
Brian Osmane8e54582016-11-28 10:06:27 -0500431 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800432 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
433 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
434 } else {
435 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
436 }
halcanary9d524f22016-03-29 09:03:52 -0700437
bsalomonc55271f2015-11-09 11:55:57 -0800438 // If we have to outset for AA then we will generate texture coords outside the src rect. The
439 // same happens for any mask filter that extends the bounds rendered in the dst.
440 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500441 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800442
bsalomonb1b01992015-11-18 10:56:08 -0800443 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400444 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400445 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect &&
Brian Salomon777e1462020-02-28 21:10:31 -0500446 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800447 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500448 combinedMatrix.setConcat(ctm, srcToDst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600449 if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) {
bsalomonb1b01992015-11-18 10:56:08 -0800450 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
451 }
452 }
453
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500454 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800455 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500456 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800457 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500458 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800459 return;
460 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800461 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500462 auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
Brian Salomon777e1462020-02-28 21:10:31 -0500463 coordsAllInsideSrcRect, wm, wm, filterMode);
Brian Osman05c8f462018-10-22 17:13:36 -0400464 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400465 rtc->colorInfo().colorSpace());
bsalomonc55271f2015-11-09 11:55:57 -0800466 if (!fp) {
467 return;
468 }
joshualitt33a5fce2015-11-18 13:28:51 -0800469
bsalomonc55271f2015-11-09 11:55:57 -0800470 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400471 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
472 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800473 return;
474 }
475
476 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500477 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
478 if (dstClip) {
479 SkPoint srcClipPoints[4];
480 SkPoint* srcClip = nullptr;
481 if (canUseTextureCoordsAsLocalCoords) {
482 // Calculate texture coordinates that match the dst clip
483 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
484 srcClip = srcClipPoints;
485 }
486 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
487 } else {
488 // Provide explicit texture coords when possible, otherwise rely on texture matrix
489 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
490 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
491 }
492 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000493 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
494 // information since it always draws with AA, but that should not be noticeable since the
495 // mask filter is probably a blur.
496 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500497 if (dstClip) {
498 // Represent it as an SkPath formed from the dstClip
499 SkPath path;
500 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000501 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500502 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000503 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500504 }
505
506 GrBlurUtils::drawShapeWithMaskFilter(
507 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
508 }
509}
510
Michael Ludwig47237242020-03-10 16:16:17 -0400511void draw_tiled_bitmap(GrContext* context,
512 GrRenderTargetContext* rtc,
513 const GrClip& clip,
514 const SkBitmap& bitmap,
515 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400516 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400517 const SkMatrix& srcToDst,
518 const SkRect& srcRect,
519 const SkIRect& clippedSrcIRect,
520 const SkPaint& paint,
521 GrAA aa,
522 SkCanvas::SrcRectConstraint constraint,
523 GrSamplerState::WrapMode wm,
524 GrSamplerState::Filter fm,
525 bool doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400526 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
527
528 int nx = bitmap.width() / tileSize;
529 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400530
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400531 for (int x = 0; x <= nx; x++) {
532 for (int y = 0; y <= ny; y++) {
533 SkRect tileR;
534 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
535 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
536
537 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
538 continue;
539 }
540
541 if (!tileR.intersect(srcRect)) {
542 continue;
543 }
544
545 SkIRect iTileR;
546 tileR.roundOut(&iTileR);
547 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
548 SkIntToScalar(iTileR.fTop));
549 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400550 srcToDst.mapRect(&rectToDraw);
551 if (fm != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400552 SkIRect iClampRect;
553
554 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
555 // In bleed mode we want to always expand the tile on all edges
556 // but stay within the bitmap bounds
557 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
558 } else {
559 // In texture-domain/clamp mode we only want to expand the
560 // tile on edges interior to "srcRect" (i.e., we want to
561 // not bleed across the original clamped edges)
562 srcRect.roundOut(&iClampRect);
563 }
Michael Ludwig47237242020-03-10 16:16:17 -0400564 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400565 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
566 }
567
568 SkBitmap tmpB;
569 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400570 // We should have already handled bitmaps larger than the max texture size.
571 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
572 tmpB.height() <= context->priv().caps()->maxTextureSize());
573 // We should be respecting the max tile size by the time we get here.
574 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
575 tmpB.height() <= context->priv().caps()->maxTileSize());
576
Brian Salomonbc074a62020-03-18 10:06:13 -0400577 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400578
579 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
580 if (aa == GrAA::kYes) {
581 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
582 if (tileR.fLeft <= srcRect.fLeft) {
583 aaFlags |= GrQuadAAFlags::kLeft;
584 }
585 if (tileR.fRight >= srcRect.fRight) {
586 aaFlags |= GrQuadAAFlags::kRight;
587 }
588 if (tileR.fTop <= srcRect.fTop) {
589 aaFlags |= GrQuadAAFlags::kTop;
590 }
591 if (tileR.fBottom >= srcRect.fBottom) {
592 aaFlags |= GrQuadAAFlags::kBottom;
593 }
594 }
595
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400596 // now offset it to make it "local" to our tmp bitmap
597 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400598 SkMatrix offsetSrcToDst = srcToDst;
599 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
600
Brian Osman449b1152020-04-15 16:43:00 -0400601 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
602 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
603 constraint, wm, fm, doBicubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400604 }
605 }
606 }
607}
608
Michael Ludwig47237242020-03-10 16:16:17 -0400609} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400610
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500611//////////////////////////////////////////////////////////////////////////////
612
613void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
614 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500615 const SkMatrix* preViewMatrix, const SkPaint& paint,
616 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500617 SkRect src;
618 SkRect dst;
619 SkMatrix srcToDst;
620 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
621 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
622 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800623 return;
624 }
625
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500626 if (src.contains(image->bounds())) {
627 constraint = SkCanvas::kFast_SrcRectConstraint;
628 }
629 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500630 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
631 ? GrSamplerState::WrapMode::kClampToBorder
632 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400633
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500634 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400635 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
636 preViewMatrix ? *preViewMatrix : SkMatrix::I());
637 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500638
Michael Ludwig47237242020-03-10 16:16:17 -0400639 bool doBicubic;
640 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
641 image->width(), image->height(), paint.getFilterQuality(), ctm, srcToDst,
642 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
643
644 auto clip = this->clip();
645
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500646 // YUVA images can be stored in multiple images with different plane resolutions, so this
647 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
648 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
649 if (as_IB(image)->isYUVA()) {
650 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500651 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500652
Brian Salomon777e1462020-02-28 21:10:31 -0500653 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Osman449b1152020-04-15 16:43:00 -0400654 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
655 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400656 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500657 return;
658 }
659
660 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
661 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
662 uint32_t pinnedUniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500663 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500664 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500665 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500666
Greg Daniel82c6b102020-01-21 10:33:22 -0500667 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500668 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500669 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
670 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
671 image->imageInfo().refColorSpace());
672 } else {
673 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
674 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500675
Brian Salomon777e1462020-02-28 21:10:31 -0500676 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Osman449b1152020-04-15 16:43:00 -0400677 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
678 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
679 constraint, wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500680 return;
681 }
682
Michael Ludwig47237242020-03-10 16:16:17 -0400683 // Next up, determine if the image must be tiled
684 {
685 // If image is explicitly already texture backed then we shouldn't get here.
686 SkASSERT(!image->isTextureBacked());
687
688 int tileFilterPad;
689 if (doBicubic) {
690 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
691 } else if (GrSamplerState::Filter::kNearest == fm) {
692 tileFilterPad = 0;
693 } else {
694 tileFilterPad = 1;
695 }
696 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
697 int tileSize;
698 SkIRect clippedSubset;
699 if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(),
700 fRenderTargetContext->height()),
701 clip, image->unique(), image->dimensions(), ctm, srcToDst, &src,
702 maxTileSize, &tileSize, &clippedSubset)) {
703 // Extract pixels on the CPU, since we have to split into separate textures before
704 // sending to the GPU.
705 SkBitmap bm;
706 if (as_IB(image)->getROPixels(&bm)) {
707 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram
708 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
709 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
710 draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400711 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
712 constraint, wrapMode, fm, doBicubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400713 return;
714 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500715 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500716 }
717
718 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
719 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500720 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500721
722 // Lazily generated images must get drawn as a texture producer that handles the final
723 // texture creation.
724 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400725 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400726 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
727 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400728 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500729 return;
730 }
Michael Ludwig47237242020-03-10 16:16:17 -0400731
732 SkBitmap bm;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500733 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400734 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400735 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
736 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400737 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500738 }
739
740 // Otherwise don't know how to draw it
741}
742
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400743void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
744 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
745 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500746 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400747 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500748 // Send every entry through drawImageQuad() to handle the more complicated paint
749 int dstClipIndex = 0;
750 for (int i = 0; i < count; ++i) {
751 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400752 SkASSERT(!set[i].fHasClip || dstClips);
753 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500754
Brian Salomondb151e02019-09-17 12:11:16 -0400755 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
756 if (set[i].fAlpha != 1.f) {
757 auto paintAlpha = paint.getAlphaf();
758 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
759 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500760 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400761 this->drawImageQuad(
762 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
763 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
764 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400765 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400766 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400767 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500768 }
769 return;
770 }
771
772 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
773 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
774 SkBlendMode mode = paint.getBlendMode();
775
776 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
777 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500778 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
779 // switches that occur within the 'n' entries.
780 int base = 0, n = 0, p = 0;
781 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500782 if (n > 0) {
783 auto textureXform = GrColorSpaceXform::Make(
784 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400785 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Michael Ludwig379e4962019-12-06 13:21:26 -0500786 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400787 filter, mode, GrAA::kYes, constraint,
788 this->localToDevice(), std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500789 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500790 base = nextBase;
791 n = 0;
792 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500793 };
794 int dstClipIndex = 0;
795 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400796 SkASSERT(!set[i].fHasClip || dstClips);
797 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
798
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500799 // Manage the dst clip pointer tracking before any continues are used so we don't lose
800 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400801 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
802 dstClipIndex += 4 * set[i].fHasClip;
803
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500804 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
805 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
806 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500807 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500808 continue;
809 }
810
Greg Danielcc21d0c2020-02-05 16:58:40 -0500811 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400812 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500813 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400814 // drawImageQuad and the proper effect to dynamically sample their planes.
815 if (!image->isYUVA()) {
816 uint32_t uniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500817 view = image->refPinnedView(this->context(), &uniqueID);
818 if (!view) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500819 view = image->refView(this->context(), GrMipMapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500820 }
821 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500822
Greg Danielcc21d0c2020-02-05 16:58:40 -0500823 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400824 // This image can't go through the texture op, send through general image pipeline
825 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500826 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400827 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
828 if (set[i].fAlpha != 1.f) {
829 auto paintAlpha = paint.getAlphaf();
830 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
831 }
832 this->drawImageQuad(
833 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400834 SkToGrQuadAAFlags(set[i].fAAFlags),
835 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400836 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400837 continue;
838 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500839
Greg Danielcc21d0c2020-02-05 16:58:40 -0500840 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500841 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500842 textures[i].fSrcRect = set[i].fSrcRect;
843 textures[i].fDstRect = set[i].fDstRect;
844 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400845 textures[i].fPreViewMatrix =
846 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500847 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
848 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
849
850 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400851 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500852 textures[i].fProxyView.proxy(),
853 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500854 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500855 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
856 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500857 draw(i);
858 }
859 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
860 // set being accumulated so increment n, and increment p if proxies are different.
861 ++n;
862 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
863 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
864 // to i - 1).
865 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500866 }
867 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500868 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500869}