blob: a1de1e63aecb6a2a89de1a0f3949a00f28a66068 [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.
127static void 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) {
134 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
135 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
136 if (!inv.invert(&inv)) {
137 clippedSrcIRect->setEmpty();
138 return;
139 }
140 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
141 inv.mapRect(&clippedSrcRect);
142 if (srcRectPtr) {
143 if (!clippedSrcRect.intersect(*srcRectPtr)) {
144 clippedSrcIRect->setEmpty();
145 return;
146 }
147 }
148 clippedSrcRect.roundOut(clippedSrcIRect);
149 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
150 if (!clippedSrcIRect->intersect(bmpBounds)) {
151 clippedSrcIRect->setEmpty();
152 }
153}
154
Michael Ludwig47237242020-03-10 16:16:17 -0400155// tileSize and clippedSubset are valid if true is returned
156static bool should_tile_image_id(GrContext* context,
157 SkISize rtSize,
158 const GrClip& clip,
159 uint32_t imageID,
160 const SkISize& imageSize,
161 const SkMatrix& ctm,
162 const SkMatrix& srcToDst,
163 const SkRect* src,
164 int maxTileSize,
165 int* tileSize,
166 SkIRect* clippedSubset) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400167 // if it's larger than the max tile size, then we have no choice but tiling.
Michael Ludwig47237242020-03-10 16:16:17 -0400168 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
169 determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst,
170 imageSize, src, clippedSubset);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400171 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
172 return true;
173 }
174
175 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
Michael Ludwig47237242020-03-10 16:16:17 -0400176 const size_t area = imageSize.width() * imageSize.height();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400177 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
178 return false;
179 }
180
Michael Ludwig46d91382020-05-07 09:51:12 -0400181 // At this point we know we could do the draw by uploading the entire bitmap as a texture.
182 // However, if the texture would be large compared to the cache size and we don't require most
183 // of it for this draw then tile to reduce the amount of upload and cache spill.
184 // NOTE: if the context is not a direct context, it doesn't have access to the resource cache,
185 // and theoretically, the resource cache's limits could be being changed on another thread, so
186 // even having access to just the limit wouldn't be a reliable test during recording here.
187 // Instead, we will just upload the entire image to be on the safe side and not tile.
188 if (!context->priv().asDirectContext()) {
189 return false;
190 }
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400191
192 // assumption here is that sw bitmap size is a good proxy for its size as
193 // a texture
194 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Michael Ludwig47237242020-03-10 16:16:17 -0400195 size_t cacheSize = context->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400196 if (bmpSize < cacheSize / 2) {
197 return false;
198 }
199
200 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
201 // tiling memory savings would be < 50%.
Michael Ludwig47237242020-03-10 16:16:17 -0400202 determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst, imageSize, src,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400203 clippedSubset);
204 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
205 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
206 kBmpSmallTileSize * kBmpSmallTileSize *
207 sizeof(SkPMColor); // assume 32bit pixels;
208
209 return usedTileBytes * 2 < bmpSize;
210}
211
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400212// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
213// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
214// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400215static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400216 const SkIRect& clamp) {
217 iRect->outset(outset, outset);
218
219 int leftClampDelta = clamp.fLeft - iRect->fLeft;
220 if (leftClampDelta > 0) {
221 offset->fX -= outset - leftClampDelta;
222 iRect->fLeft = clamp.fLeft;
223 } else {
224 offset->fX -= outset;
225 }
226
227 int topClampDelta = clamp.fTop - iRect->fTop;
228 if (topClampDelta > 0) {
229 offset->fY -= outset - topClampDelta;
230 iRect->fTop = clamp.fTop;
231 } else {
232 offset->fY -= outset;
233 }
234
235 if (iRect->fRight > clamp.fRight) {
236 iRect->fRight = clamp.fRight;
237 }
238 if (iRect->fBottom > clamp.fBottom) {
239 iRect->fBottom = clamp.fBottom;
240 }
241}
242
243//////////////////////////////////////////////////////////////////////////////
244// Helper functions for drawing an image with GrRenderTargetContext
245
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500246enum class ImageDrawMode {
247 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
248 kOptimized,
249 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
250 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
251 kDecal,
252 // Src or dst are empty, or do not intersect the image content so don't draw anything.
253 kSkip
254};
255
256/**
257 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
258 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
259 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
260 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
261 *
262 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
263 * original src rect. 'dstClip' should be null when there is no additional clipping.
264 */
265static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
266 const SkRect* origDstRect, const SkPoint dstClip[4],
267 SkRect* outSrcRect, SkRect* outDstRect,
268 SkMatrix* srcToDst) {
269 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
270
271 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
272 SkRect dst = origDstRect ? *origDstRect : src;
273
274 if (src.isEmpty() || dst.isEmpty()) {
275 return ImageDrawMode::kSkip;
276 }
277
278 if (outDstRect) {
279 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
280 } else {
281 srcToDst->setIdentity();
282 }
283
284 if (origSrcRect && !srcBounds.contains(src)) {
285 if (!src.intersect(srcBounds)) {
286 return ImageDrawMode::kSkip;
287 }
288 srcToDst->mapRect(&dst, src);
289
290 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
291 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
292 if (dstClip) {
293 for (int i = 0; i < 4; ++i) {
294 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
295 // Must resort to using a decal mode restricted to the clipped 'src', and
296 // use the original dst rect (filling in src bounds as needed)
297 *outSrcRect = src;
298 *outDstRect = (origDstRect ? *origDstRect
299 : (origSrcRect ? *origSrcRect : srcBounds));
300 return ImageDrawMode::kDecal;
301 }
302 }
303 }
304 }
305
306 // The original src and dst were fully contained in the image, or there was no dst clip to
307 // worry about, or the clip was still contained in the restricted dst rect.
308 *outSrcRect = src;
309 *outDstRect = dst;
310 return ImageDrawMode::kOptimized;
311}
312
Brian Salomon34169692017-08-28 15:32:01 -0400313/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400314 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
315 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400316 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400317static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400318 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500319 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400320}
321
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500322// Assumes srcRect and dstRect have already been optimized to fit the proxy
323static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
324 const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
325 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500326 SkCanvas::SrcRectConstraint constraint, GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400327 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400328 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400329 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400330 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500331 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400332 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400333 switch (paint.getFilterQuality()) {
334 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400335 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400336 break;
337 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400338 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400339 break;
340 case kMedium_SkFilterQuality:
341 case kHigh_SkFilterQuality:
342 SK_ABORT("Quality level not allowed.");
343 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500344 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500345 // Must specify the strict constraint when the proxy is not functionally exact and the src
346 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500347 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500348 // Conservative estimate of how much a coord could be outset from src rect:
349 // 1/2 pixel for AA and 1/2 pixel for bilerp
350 float buffer = 0.5f * (aa == GrAA::kYes) +
351 0.5f * (filter == GrSamplerState::Filter::kBilerp);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400352 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500353 safeBounds.inset(buffer, buffer);
354 if (!safeBounds.contains(srcRect)) {
355 constraint = SkCanvas::kStrict_SrcRectConstraint;
356 }
357 }
Brian Osman3d139a42018-11-19 10:42:10 -0500358 SkPMColor4f color;
Greg Daniela4828a12019-10-11 13:51:02 -0400359 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400360 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400361 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500362 float paintAlpha = paint.getColor4f().fA;
363 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400364 }
Brian Salomon34169692017-08-28 15:32:01 -0400365
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500366 if (dstClip) {
367 // Get source coords corresponding to dstClip
368 SkPoint srcQuad[4];
369 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000370
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500371 rtc->drawTextureQuad(clip, std::move(view), srcColorInfo.colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -0500372 srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad,
373 dstClip, aa, aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500374 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
375 ctm, std::move(textureXform));
376 } else {
Greg Daniel40903af2020-01-30 14:55:05 -0500377 rtc->drawTexture(clip, std::move(view), srcColorInfo.alphaType(), filter,
378 paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint,
379 ctm, std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000380 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000381}
382
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500383// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Brian Salomon777e1462020-02-28 21:10:31 -0500384static void draw_texture_producer(GrContext* context,
385 GrRenderTargetContext* rtc,
386 const GrClip& clip,
Brian Osman449b1152020-04-15 16:43:00 -0400387 const SkMatrixProvider& matrixProvider,
Brian Salomon777e1462020-02-28 21:10:31 -0500388 const SkPaint& paint,
389 GrTextureProducer* producer,
390 const SkRect& src,
391 const SkRect& dst,
392 const SkPoint dstClip[4],
393 const SkMatrix& srcToDst,
394 GrAA aa,
395 GrQuadAAFlags aaFlags,
396 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400397 GrSamplerState::WrapMode wm,
398 GrSamplerState::Filter fm,
399 bool doBicubic) {
Brian Osman449b1152020-04-15 16:43:00 -0400400 const SkMatrix& ctm(matrixProvider.localToDevice());
Brian Salomon777e1462020-02-28 21:10:31 -0500401 if (wm == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
402 can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400403 // We've done enough checks above to allow us to pass ClampNearest() and not check for
404 // scaling adjustments.
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500405 auto view = producer->view(GrMipMapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500406 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400407 return;
408 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500409
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500410 draw_texture(
411 rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint, std::move(view),
412 {producer->colorType(), producer->alphaType(), sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400413 return;
414 }
415
bsalomonc55271f2015-11-09 11:55:57 -0800416 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500417
bsalomonc55271f2015-11-09 11:55:57 -0800418 // The shader expects proper local coords, so we can't replace local coords with texture coords
419 // if the shader will be used. If we have a mask filter we will change the underlying geometry
420 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800421 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800422
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500423 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500424 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500425 // FP. In the future this should be an opaque optimization enabled by the combination of
426 // GrDrawOp/GP and FP.
427 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
428 mf = nullptr;
429 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400430 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800431
Brian Osmane8e54582016-11-28 10:06:27 -0500432 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800433 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
434 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
435 } else {
436 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
437 }
halcanary9d524f22016-03-29 09:03:52 -0700438
bsalomonc55271f2015-11-09 11:55:57 -0800439 // If we have to outset for AA then we will generate texture coords outside the src rect. The
440 // same happens for any mask filter that extends the bounds rendered in the dst.
441 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500442 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800443
bsalomonb1b01992015-11-18 10:56:08 -0800444 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400445 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400446 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect &&
Brian Salomon777e1462020-02-28 21:10:31 -0500447 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800448 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500449 combinedMatrix.setConcat(ctm, srcToDst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600450 if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) {
bsalomonb1b01992015-11-18 10:56:08 -0800451 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
452 }
453 }
454
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500455 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800456 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500457 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800458 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500459 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800460 return;
461 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800462 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500463 auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
Brian Salomon777e1462020-02-28 21:10:31 -0500464 coordsAllInsideSrcRect, wm, wm, filterMode);
Brian Osman05c8f462018-10-22 17:13:36 -0400465 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400466 rtc->colorInfo().colorSpace());
bsalomonc55271f2015-11-09 11:55:57 -0800467 if (!fp) {
468 return;
469 }
joshualitt33a5fce2015-11-18 13:28:51 -0800470
bsalomonc55271f2015-11-09 11:55:57 -0800471 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400472 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
473 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800474 return;
475 }
476
477 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500478 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
479 if (dstClip) {
480 SkPoint srcClipPoints[4];
481 SkPoint* srcClip = nullptr;
482 if (canUseTextureCoordsAsLocalCoords) {
483 // Calculate texture coordinates that match the dst clip
484 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
485 srcClip = srcClipPoints;
486 }
487 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
488 } else {
489 // Provide explicit texture coords when possible, otherwise rely on texture matrix
490 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
491 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
492 }
493 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000494 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
495 // information since it always draws with AA, but that should not be noticeable since the
496 // mask filter is probably a blur.
497 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500498 if (dstClip) {
499 // Represent it as an SkPath formed from the dstClip
500 SkPath path;
501 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000502 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500503 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000504 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500505 }
506
507 GrBlurUtils::drawShapeWithMaskFilter(
508 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
509 }
510}
511
Michael Ludwig47237242020-03-10 16:16:17 -0400512void draw_tiled_bitmap(GrContext* context,
513 GrRenderTargetContext* rtc,
514 const GrClip& clip,
515 const SkBitmap& bitmap,
516 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400517 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400518 const SkMatrix& srcToDst,
519 const SkRect& srcRect,
520 const SkIRect& clippedSrcIRect,
521 const SkPaint& paint,
522 GrAA aa,
523 SkCanvas::SrcRectConstraint constraint,
524 GrSamplerState::WrapMode wm,
525 GrSamplerState::Filter fm,
526 bool doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400527 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
528
529 int nx = bitmap.width() / tileSize;
530 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400531
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400532 for (int x = 0; x <= nx; x++) {
533 for (int y = 0; y <= ny; y++) {
534 SkRect tileR;
535 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
536 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
537
538 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
539 continue;
540 }
541
542 if (!tileR.intersect(srcRect)) {
543 continue;
544 }
545
546 SkIRect iTileR;
547 tileR.roundOut(&iTileR);
548 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
549 SkIntToScalar(iTileR.fTop));
550 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400551 srcToDst.mapRect(&rectToDraw);
552 if (fm != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400553 SkIRect iClampRect;
554
555 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
556 // In bleed mode we want to always expand the tile on all edges
557 // but stay within the bitmap bounds
558 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
559 } else {
560 // In texture-domain/clamp mode we only want to expand the
561 // tile on edges interior to "srcRect" (i.e., we want to
562 // not bleed across the original clamped edges)
563 srcRect.roundOut(&iClampRect);
564 }
Michael Ludwig47237242020-03-10 16:16:17 -0400565 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400566 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
567 }
568
569 SkBitmap tmpB;
570 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400571 // We should have already handled bitmaps larger than the max texture size.
572 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
573 tmpB.height() <= context->priv().caps()->maxTextureSize());
574 // We should be respecting the max tile size by the time we get here.
575 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
576 tmpB.height() <= context->priv().caps()->maxTileSize());
577
Brian Salomonbc074a62020-03-18 10:06:13 -0400578 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400579
580 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
581 if (aa == GrAA::kYes) {
582 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
583 if (tileR.fLeft <= srcRect.fLeft) {
584 aaFlags |= GrQuadAAFlags::kLeft;
585 }
586 if (tileR.fRight >= srcRect.fRight) {
587 aaFlags |= GrQuadAAFlags::kRight;
588 }
589 if (tileR.fTop <= srcRect.fTop) {
590 aaFlags |= GrQuadAAFlags::kTop;
591 }
592 if (tileR.fBottom >= srcRect.fBottom) {
593 aaFlags |= GrQuadAAFlags::kBottom;
594 }
595 }
596
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400597 // now offset it to make it "local" to our tmp bitmap
598 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400599 SkMatrix offsetSrcToDst = srcToDst;
600 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
601
Brian Osman449b1152020-04-15 16:43:00 -0400602 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
603 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
604 constraint, wm, fm, doBicubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400605 }
606 }
607 }
608}
609
Michael Ludwig47237242020-03-10 16:16:17 -0400610} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400611
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500612//////////////////////////////////////////////////////////////////////////////
613
614void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
615 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500616 const SkMatrix* preViewMatrix, const SkPaint& paint,
617 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500618 SkRect src;
619 SkRect dst;
620 SkMatrix srcToDst;
621 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
622 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
623 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800624 return;
625 }
626
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500627 if (src.contains(image->bounds())) {
628 constraint = SkCanvas::kFast_SrcRectConstraint;
629 }
630 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500631 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
632 ? GrSamplerState::WrapMode::kClampToBorder
633 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400634
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500635 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400636 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
637 preViewMatrix ? *preViewMatrix : SkMatrix::I());
638 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500639
Michael Ludwig47237242020-03-10 16:16:17 -0400640 bool doBicubic;
641 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
642 image->width(), image->height(), paint.getFilterQuality(), ctm, srcToDst,
643 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
644
645 auto clip = this->clip();
646
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500647 // YUVA images can be stored in multiple images with different plane resolutions, so this
648 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
649 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
650 if (as_IB(image)->isYUVA()) {
651 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500652 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500653
Brian Salomon777e1462020-02-28 21:10:31 -0500654 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Osman449b1152020-04-15 16:43:00 -0400655 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
656 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400657 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500658 return;
659 }
660
661 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
662 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
663 uint32_t pinnedUniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500664 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500665 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500666 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500667
Greg Daniel82c6b102020-01-21 10:33:22 -0500668 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500669 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500670 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
671 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
672 image->imageInfo().refColorSpace());
673 } else {
674 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
675 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500676
Brian Salomon777e1462020-02-28 21:10:31 -0500677 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Osman449b1152020-04-15 16:43:00 -0400678 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
679 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
680 constraint, wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500681 return;
682 }
683
Michael Ludwig47237242020-03-10 16:16:17 -0400684 // Next up, determine if the image must be tiled
685 {
686 // If image is explicitly already texture backed then we shouldn't get here.
687 SkASSERT(!image->isTextureBacked());
688
689 int tileFilterPad;
690 if (doBicubic) {
691 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
692 } else if (GrSamplerState::Filter::kNearest == fm) {
693 tileFilterPad = 0;
694 } else {
695 tileFilterPad = 1;
696 }
697 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
698 int tileSize;
699 SkIRect clippedSubset;
700 if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(),
701 fRenderTargetContext->height()),
702 clip, image->unique(), image->dimensions(), ctm, srcToDst, &src,
703 maxTileSize, &tileSize, &clippedSubset)) {
704 // Extract pixels on the CPU, since we have to split into separate textures before
705 // sending to the GPU.
706 SkBitmap bm;
707 if (as_IB(image)->getROPixels(&bm)) {
708 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram
709 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
710 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
711 draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400712 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
713 constraint, wrapMode, fm, doBicubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400714 return;
715 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500716 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500717 }
718
719 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
720 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500721 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500722
723 // Lazily generated images must get drawn as a texture producer that handles the final
724 // texture creation.
725 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400726 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400727 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
728 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400729 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500730 return;
731 }
Michael Ludwig47237242020-03-10 16:16:17 -0400732
733 SkBitmap bm;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500734 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400735 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400736 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
737 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400738 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500739 }
740
741 // Otherwise don't know how to draw it
742}
743
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400744void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
745 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
746 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500747 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400748 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500749 // Send every entry through drawImageQuad() to handle the more complicated paint
750 int dstClipIndex = 0;
751 for (int i = 0; i < count; ++i) {
752 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400753 SkASSERT(!set[i].fHasClip || dstClips);
754 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500755
Brian Salomondb151e02019-09-17 12:11:16 -0400756 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
757 if (set[i].fAlpha != 1.f) {
758 auto paintAlpha = paint.getAlphaf();
759 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
760 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500761 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400762 this->drawImageQuad(
763 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
764 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
765 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400766 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400767 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400768 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500769 }
770 return;
771 }
772
773 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
774 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
775 SkBlendMode mode = paint.getBlendMode();
776
777 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
778 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500779 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
780 // switches that occur within the 'n' entries.
781 int base = 0, n = 0, p = 0;
782 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500783 if (n > 0) {
784 auto textureXform = GrColorSpaceXform::Make(
785 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400786 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Michael Ludwig379e4962019-12-06 13:21:26 -0500787 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400788 filter, mode, GrAA::kYes, constraint,
789 this->localToDevice(), std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500790 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500791 base = nextBase;
792 n = 0;
793 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500794 };
795 int dstClipIndex = 0;
796 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400797 SkASSERT(!set[i].fHasClip || dstClips);
798 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
799
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500800 // Manage the dst clip pointer tracking before any continues are used so we don't lose
801 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400802 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
803 dstClipIndex += 4 * set[i].fHasClip;
804
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500805 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
806 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
807 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500808 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500809 continue;
810 }
811
Greg Danielcc21d0c2020-02-05 16:58:40 -0500812 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400813 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500814 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400815 // drawImageQuad and the proper effect to dynamically sample their planes.
816 if (!image->isYUVA()) {
817 uint32_t uniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500818 view = image->refPinnedView(this->context(), &uniqueID);
819 if (!view) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500820 view = image->refView(this->context(), GrMipMapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500821 }
822 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500823
Greg Danielcc21d0c2020-02-05 16:58:40 -0500824 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400825 // This image can't go through the texture op, send through general image pipeline
826 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500827 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400828 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
829 if (set[i].fAlpha != 1.f) {
830 auto paintAlpha = paint.getAlphaf();
831 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
832 }
833 this->drawImageQuad(
834 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400835 SkToGrQuadAAFlags(set[i].fAAFlags),
836 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400837 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400838 continue;
839 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500840
Greg Danielcc21d0c2020-02-05 16:58:40 -0500841 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500842 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500843 textures[i].fSrcRect = set[i].fSrcRect;
844 textures[i].fDstRect = set[i].fDstRect;
845 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400846 textures[i].fPreViewMatrix =
847 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500848 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
849 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
850
851 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400852 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500853 textures[i].fProxyView.proxy(),
854 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500855 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500856 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
857 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500858 draw(i);
859 }
860 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
861 // set being accumulated so increment n, and increment p if proxies are different.
862 ++n;
863 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
864 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
865 // to i - 1).
866 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500867 }
868 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500869 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500870}