blob: 7f597dbd1869cd26733af5d873b28b0db8cd6f94 [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
181 // At this point we know we could do the draw by uploading the entire bitmap
182 // as a texture. However, if the texture would be large compared to the
183 // cache size and we don't require most of it for this draw then tile to
184 // reduce the amount of upload and cache spill.
185
186 // assumption here is that sw bitmap size is a good proxy for its size as
187 // a texture
188 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Michael Ludwig47237242020-03-10 16:16:17 -0400189 size_t cacheSize = context->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400190 if (bmpSize < cacheSize / 2) {
191 return false;
192 }
193
194 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
195 // tiling memory savings would be < 50%.
Michael Ludwig47237242020-03-10 16:16:17 -0400196 determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst, imageSize, src,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400197 clippedSubset);
198 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
199 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
200 kBmpSmallTileSize * kBmpSmallTileSize *
201 sizeof(SkPMColor); // assume 32bit pixels;
202
203 return usedTileBytes * 2 < bmpSize;
204}
205
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400206// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
207// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
208// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400209static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400210 const SkIRect& clamp) {
211 iRect->outset(outset, outset);
212
213 int leftClampDelta = clamp.fLeft - iRect->fLeft;
214 if (leftClampDelta > 0) {
215 offset->fX -= outset - leftClampDelta;
216 iRect->fLeft = clamp.fLeft;
217 } else {
218 offset->fX -= outset;
219 }
220
221 int topClampDelta = clamp.fTop - iRect->fTop;
222 if (topClampDelta > 0) {
223 offset->fY -= outset - topClampDelta;
224 iRect->fTop = clamp.fTop;
225 } else {
226 offset->fY -= outset;
227 }
228
229 if (iRect->fRight > clamp.fRight) {
230 iRect->fRight = clamp.fRight;
231 }
232 if (iRect->fBottom > clamp.fBottom) {
233 iRect->fBottom = clamp.fBottom;
234 }
235}
236
237//////////////////////////////////////////////////////////////////////////////
238// Helper functions for drawing an image with GrRenderTargetContext
239
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500240enum class ImageDrawMode {
241 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
242 kOptimized,
243 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
244 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
245 kDecal,
246 // Src or dst are empty, or do not intersect the image content so don't draw anything.
247 kSkip
248};
249
250/**
251 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
252 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
253 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
254 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
255 *
256 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
257 * original src rect. 'dstClip' should be null when there is no additional clipping.
258 */
259static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
260 const SkRect* origDstRect, const SkPoint dstClip[4],
261 SkRect* outSrcRect, SkRect* outDstRect,
262 SkMatrix* srcToDst) {
263 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
264
265 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
266 SkRect dst = origDstRect ? *origDstRect : src;
267
268 if (src.isEmpty() || dst.isEmpty()) {
269 return ImageDrawMode::kSkip;
270 }
271
272 if (outDstRect) {
273 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
274 } else {
275 srcToDst->setIdentity();
276 }
277
278 if (origSrcRect && !srcBounds.contains(src)) {
279 if (!src.intersect(srcBounds)) {
280 return ImageDrawMode::kSkip;
281 }
282 srcToDst->mapRect(&dst, src);
283
284 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
285 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
286 if (dstClip) {
287 for (int i = 0; i < 4; ++i) {
288 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
289 // Must resort to using a decal mode restricted to the clipped 'src', and
290 // use the original dst rect (filling in src bounds as needed)
291 *outSrcRect = src;
292 *outDstRect = (origDstRect ? *origDstRect
293 : (origSrcRect ? *origSrcRect : srcBounds));
294 return ImageDrawMode::kDecal;
295 }
296 }
297 }
298 }
299
300 // The original src and dst were fully contained in the image, or there was no dst clip to
301 // worry about, or the clip was still contained in the restricted dst rect.
302 *outSrcRect = src;
303 *outDstRect = dst;
304 return ImageDrawMode::kOptimized;
305}
306
Brian Salomon34169692017-08-28 15:32:01 -0400307/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400308 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
309 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400310 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400311static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400312 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500313 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400314}
315
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500316// Assumes srcRect and dstRect have already been optimized to fit the proxy
317static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
318 const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
319 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500320 SkCanvas::SrcRectConstraint constraint, GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400321 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400322 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400323 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400324 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500325 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400326 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400327 switch (paint.getFilterQuality()) {
328 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400329 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400330 break;
331 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400332 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400333 break;
334 case kMedium_SkFilterQuality:
335 case kHigh_SkFilterQuality:
336 SK_ABORT("Quality level not allowed.");
337 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500338 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500339 // Must specify the strict constraint when the proxy is not functionally exact and the src
340 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500341 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500342 // Conservative estimate of how much a coord could be outset from src rect:
343 // 1/2 pixel for AA and 1/2 pixel for bilerp
344 float buffer = 0.5f * (aa == GrAA::kYes) +
345 0.5f * (filter == GrSamplerState::Filter::kBilerp);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400346 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500347 safeBounds.inset(buffer, buffer);
348 if (!safeBounds.contains(srcRect)) {
349 constraint = SkCanvas::kStrict_SrcRectConstraint;
350 }
351 }
Brian Osman3d139a42018-11-19 10:42:10 -0500352 SkPMColor4f color;
Greg Daniela4828a12019-10-11 13:51:02 -0400353 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400354 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400355 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500356 float paintAlpha = paint.getColor4f().fA;
357 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400358 }
Brian Salomon34169692017-08-28 15:32:01 -0400359
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500360 if (dstClip) {
361 // Get source coords corresponding to dstClip
362 SkPoint srcQuad[4];
363 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000364
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500365 rtc->drawTextureQuad(clip, std::move(view), srcColorInfo.colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -0500366 srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad,
367 dstClip, aa, aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500368 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
369 ctm, std::move(textureXform));
370 } else {
Greg Daniel40903af2020-01-30 14:55:05 -0500371 rtc->drawTexture(clip, std::move(view), srcColorInfo.alphaType(), filter,
372 paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint,
373 ctm, std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000374 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000375}
376
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500377// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Brian Salomon777e1462020-02-28 21:10:31 -0500378static void draw_texture_producer(GrContext* context,
379 GrRenderTargetContext* rtc,
380 const GrClip& clip,
Brian Osman449b1152020-04-15 16:43:00 -0400381 const SkMatrixProvider& matrixProvider,
Brian Salomon777e1462020-02-28 21:10:31 -0500382 const SkPaint& paint,
383 GrTextureProducer* producer,
384 const SkRect& src,
385 const SkRect& dst,
386 const SkPoint dstClip[4],
387 const SkMatrix& srcToDst,
388 GrAA aa,
389 GrQuadAAFlags aaFlags,
390 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400391 GrSamplerState::WrapMode wm,
392 GrSamplerState::Filter fm,
393 bool doBicubic) {
Brian Osman449b1152020-04-15 16:43:00 -0400394 const SkMatrix& ctm(matrixProvider.localToDevice());
Brian Salomon777e1462020-02-28 21:10:31 -0500395 if (wm == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
396 can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400397 // We've done enough checks above to allow us to pass ClampNearest() and not check for
398 // scaling adjustments.
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500399 auto view = producer->view(GrMipMapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500400 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400401 return;
402 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500403
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500404 draw_texture(
405 rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint, std::move(view),
406 {producer->colorType(), producer->alphaType(), sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400407 return;
408 }
409
bsalomonc55271f2015-11-09 11:55:57 -0800410 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500411
bsalomonc55271f2015-11-09 11:55:57 -0800412 // The shader expects proper local coords, so we can't replace local coords with texture coords
413 // if the shader will be used. If we have a mask filter we will change the underlying geometry
414 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800415 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800416
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500417 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500418 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500419 // FP. In the future this should be an opaque optimization enabled by the combination of
420 // GrDrawOp/GP and FP.
421 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
422 mf = nullptr;
423 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400424 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800425
Brian Osmane8e54582016-11-28 10:06:27 -0500426 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800427 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
428 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
429 } else {
430 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
431 }
halcanary9d524f22016-03-29 09:03:52 -0700432
bsalomonc55271f2015-11-09 11:55:57 -0800433 // If we have to outset for AA then we will generate texture coords outside the src rect. The
434 // same happens for any mask filter that extends the bounds rendered in the dst.
435 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500436 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800437
bsalomonb1b01992015-11-18 10:56:08 -0800438 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400439 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400440 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect &&
Brian Salomon777e1462020-02-28 21:10:31 -0500441 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800442 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500443 combinedMatrix.setConcat(ctm, srcToDst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600444 if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) {
bsalomonb1b01992015-11-18 10:56:08 -0800445 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
446 }
447 }
448
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500449 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800450 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500451 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800452 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500453 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800454 return;
455 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800456 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500457 auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
Brian Salomon777e1462020-02-28 21:10:31 -0500458 coordsAllInsideSrcRect, wm, wm, filterMode);
Brian Osman05c8f462018-10-22 17:13:36 -0400459 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400460 rtc->colorInfo().colorSpace());
bsalomonc55271f2015-11-09 11:55:57 -0800461 if (!fp) {
462 return;
463 }
joshualitt33a5fce2015-11-18 13:28:51 -0800464
bsalomonc55271f2015-11-09 11:55:57 -0800465 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400466 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
467 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800468 return;
469 }
470
471 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500472 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
473 if (dstClip) {
474 SkPoint srcClipPoints[4];
475 SkPoint* srcClip = nullptr;
476 if (canUseTextureCoordsAsLocalCoords) {
477 // Calculate texture coordinates that match the dst clip
478 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
479 srcClip = srcClipPoints;
480 }
481 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
482 } else {
483 // Provide explicit texture coords when possible, otherwise rely on texture matrix
484 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
485 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
486 }
487 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000488 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
489 // information since it always draws with AA, but that should not be noticeable since the
490 // mask filter is probably a blur.
491 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500492 if (dstClip) {
493 // Represent it as an SkPath formed from the dstClip
494 SkPath path;
495 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000496 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500497 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000498 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500499 }
500
501 GrBlurUtils::drawShapeWithMaskFilter(
502 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
503 }
504}
505
Michael Ludwig47237242020-03-10 16:16:17 -0400506void draw_tiled_bitmap(GrContext* context,
507 GrRenderTargetContext* rtc,
508 const GrClip& clip,
509 const SkBitmap& bitmap,
510 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400511 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400512 const SkMatrix& srcToDst,
513 const SkRect& srcRect,
514 const SkIRect& clippedSrcIRect,
515 const SkPaint& paint,
516 GrAA aa,
517 SkCanvas::SrcRectConstraint constraint,
518 GrSamplerState::WrapMode wm,
519 GrSamplerState::Filter fm,
520 bool doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400521 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
522
523 int nx = bitmap.width() / tileSize;
524 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400525
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400526 for (int x = 0; x <= nx; x++) {
527 for (int y = 0; y <= ny; y++) {
528 SkRect tileR;
529 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
530 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
531
532 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
533 continue;
534 }
535
536 if (!tileR.intersect(srcRect)) {
537 continue;
538 }
539
540 SkIRect iTileR;
541 tileR.roundOut(&iTileR);
542 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
543 SkIntToScalar(iTileR.fTop));
544 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400545 srcToDst.mapRect(&rectToDraw);
546 if (fm != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400547 SkIRect iClampRect;
548
549 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
550 // In bleed mode we want to always expand the tile on all edges
551 // but stay within the bitmap bounds
552 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
553 } else {
554 // In texture-domain/clamp mode we only want to expand the
555 // tile on edges interior to "srcRect" (i.e., we want to
556 // not bleed across the original clamped edges)
557 srcRect.roundOut(&iClampRect);
558 }
Michael Ludwig47237242020-03-10 16:16:17 -0400559 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400560 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
561 }
562
563 SkBitmap tmpB;
564 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400565 // We should have already handled bitmaps larger than the max texture size.
566 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
567 tmpB.height() <= context->priv().caps()->maxTextureSize());
568 // We should be respecting the max tile size by the time we get here.
569 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
570 tmpB.height() <= context->priv().caps()->maxTileSize());
571
Brian Salomonbc074a62020-03-18 10:06:13 -0400572 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400573
574 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
575 if (aa == GrAA::kYes) {
576 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
577 if (tileR.fLeft <= srcRect.fLeft) {
578 aaFlags |= GrQuadAAFlags::kLeft;
579 }
580 if (tileR.fRight >= srcRect.fRight) {
581 aaFlags |= GrQuadAAFlags::kRight;
582 }
583 if (tileR.fTop <= srcRect.fTop) {
584 aaFlags |= GrQuadAAFlags::kTop;
585 }
586 if (tileR.fBottom >= srcRect.fBottom) {
587 aaFlags |= GrQuadAAFlags::kBottom;
588 }
589 }
590
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400591 // now offset it to make it "local" to our tmp bitmap
592 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400593 SkMatrix offsetSrcToDst = srcToDst;
594 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
595
Brian Osman449b1152020-04-15 16:43:00 -0400596 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
597 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
598 constraint, wm, fm, doBicubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400599 }
600 }
601 }
602}
603
Michael Ludwig47237242020-03-10 16:16:17 -0400604} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400605
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500606//////////////////////////////////////////////////////////////////////////////
607
608void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
609 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500610 const SkMatrix* preViewMatrix, const SkPaint& paint,
611 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500612 SkRect src;
613 SkRect dst;
614 SkMatrix srcToDst;
615 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
616 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
617 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800618 return;
619 }
620
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500621 if (src.contains(image->bounds())) {
622 constraint = SkCanvas::kFast_SrcRectConstraint;
623 }
624 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500625 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
626 ? GrSamplerState::WrapMode::kClampToBorder
627 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400628
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500629 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400630 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
631 preViewMatrix ? *preViewMatrix : SkMatrix::I());
632 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500633
Michael Ludwig47237242020-03-10 16:16:17 -0400634 bool doBicubic;
635 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
636 image->width(), image->height(), paint.getFilterQuality(), ctm, srcToDst,
637 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
638
639 auto clip = this->clip();
640
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500641 // YUVA images can be stored in multiple images with different plane resolutions, so this
642 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
643 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
644 if (as_IB(image)->isYUVA()) {
645 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500646 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500647
Brian Salomon777e1462020-02-28 21:10:31 -0500648 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Osman449b1152020-04-15 16:43:00 -0400649 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
650 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400651 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500652 return;
653 }
654
655 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
656 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
657 uint32_t pinnedUniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500658 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500659 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500660 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500661
Greg Daniel82c6b102020-01-21 10:33:22 -0500662 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500663 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500664 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
665 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
666 image->imageInfo().refColorSpace());
667 } else {
668 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
669 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500670
Brian Salomon777e1462020-02-28 21:10:31 -0500671 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Osman449b1152020-04-15 16:43:00 -0400672 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
673 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
674 constraint, wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500675 return;
676 }
677
Michael Ludwig47237242020-03-10 16:16:17 -0400678 // Next up, determine if the image must be tiled
679 {
680 // If image is explicitly already texture backed then we shouldn't get here.
681 SkASSERT(!image->isTextureBacked());
682
683 int tileFilterPad;
684 if (doBicubic) {
685 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
686 } else if (GrSamplerState::Filter::kNearest == fm) {
687 tileFilterPad = 0;
688 } else {
689 tileFilterPad = 1;
690 }
691 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
692 int tileSize;
693 SkIRect clippedSubset;
694 if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(),
695 fRenderTargetContext->height()),
696 clip, image->unique(), image->dimensions(), ctm, srcToDst, &src,
697 maxTileSize, &tileSize, &clippedSubset)) {
698 // Extract pixels on the CPU, since we have to split into separate textures before
699 // sending to the GPU.
700 SkBitmap bm;
701 if (as_IB(image)->getROPixels(&bm)) {
702 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram
703 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
704 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
705 draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400706 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
707 constraint, wrapMode, fm, doBicubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400708 return;
709 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500710 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500711 }
712
713 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
714 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500715 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500716
717 // Lazily generated images must get drawn as a texture producer that handles the final
718 // texture creation.
719 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400720 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400721 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
722 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400723 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500724 return;
725 }
Michael Ludwig47237242020-03-10 16:16:17 -0400726
727 SkBitmap bm;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500728 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400729 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400730 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
731 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400732 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500733 }
734
735 // Otherwise don't know how to draw it
736}
737
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400738void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
739 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
740 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500741 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400742 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500743 // Send every entry through drawImageQuad() to handle the more complicated paint
744 int dstClipIndex = 0;
745 for (int i = 0; i < count; ++i) {
746 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400747 SkASSERT(!set[i].fHasClip || dstClips);
748 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500749
Brian Salomondb151e02019-09-17 12:11:16 -0400750 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
751 if (set[i].fAlpha != 1.f) {
752 auto paintAlpha = paint.getAlphaf();
753 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
754 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500755 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400756 this->drawImageQuad(
757 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
758 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
759 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400760 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400761 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400762 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500763 }
764 return;
765 }
766
767 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
768 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
769 SkBlendMode mode = paint.getBlendMode();
770
771 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
772 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500773 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
774 // switches that occur within the 'n' entries.
775 int base = 0, n = 0, p = 0;
776 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500777 if (n > 0) {
778 auto textureXform = GrColorSpaceXform::Make(
779 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400780 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Michael Ludwig379e4962019-12-06 13:21:26 -0500781 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400782 filter, mode, GrAA::kYes, constraint,
783 this->localToDevice(), std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500784 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500785 base = nextBase;
786 n = 0;
787 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500788 };
789 int dstClipIndex = 0;
790 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400791 SkASSERT(!set[i].fHasClip || dstClips);
792 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
793
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500794 // Manage the dst clip pointer tracking before any continues are used so we don't lose
795 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400796 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
797 dstClipIndex += 4 * set[i].fHasClip;
798
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500799 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
800 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
801 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500802 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500803 continue;
804 }
805
Greg Danielcc21d0c2020-02-05 16:58:40 -0500806 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400807 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500808 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400809 // drawImageQuad and the proper effect to dynamically sample their planes.
810 if (!image->isYUVA()) {
811 uint32_t uniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500812 view = image->refPinnedView(this->context(), &uniqueID);
813 if (!view) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500814 view = image->refView(this->context(), GrMipMapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500815 }
816 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500817
Greg Danielcc21d0c2020-02-05 16:58:40 -0500818 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400819 // This image can't go through the texture op, send through general image pipeline
820 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500821 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400822 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
823 if (set[i].fAlpha != 1.f) {
824 auto paintAlpha = paint.getAlphaf();
825 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
826 }
827 this->drawImageQuad(
828 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400829 SkToGrQuadAAFlags(set[i].fAAFlags),
830 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400831 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400832 continue;
833 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500834
Greg Danielcc21d0c2020-02-05 16:58:40 -0500835 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500836 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500837 textures[i].fSrcRect = set[i].fSrcRect;
838 textures[i].fDstRect = set[i].fDstRect;
839 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400840 textures[i].fPreViewMatrix =
841 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500842 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
843 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
844
845 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400846 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500847 textures[i].fProxyView.proxy(),
848 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500849 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500850 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
851 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500852 draw(i);
853 }
854 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
855 // set being accumulated so increment n, and increment p if proxies are different.
856 ++n;
857 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
858 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
859 // to i - 1).
860 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500861 }
862 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500863 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500864}