blob: 3bad63d10da9651d4ecb704abe4c43c5616096d5 [file] [log] [blame]
bsalomonc55271f2015-11-09 11:55:57 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/SkGpuDevice.h"
Michael Ludwig1433cfd2019-02-27 17:12:30 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkYUVAIndex.h"
11#include "src/core/SkDraw.h"
12#include "src/core/SkMaskFilterBase.h"
13#include "src/gpu/GrBitmapTextureMaker.h"
14#include "src/gpu/GrBlurUtils.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrColorSpaceXform.h"
17#include "src/gpu/GrImageTextureMaker.h"
18#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrStyle.h"
20#include "src/gpu/GrTextureAdjuster.h"
21#include "src/gpu/GrTextureMaker.h"
22#include "src/gpu/SkGr.h"
23#include "src/gpu/effects/GrBicubicEffect.h"
24#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050025#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000026#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080028
Michael Ludwig1433cfd2019-02-27 17:12:30 -050029namespace {
30
bsalomonc55271f2015-11-09 11:55:57 -080031static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
32 return textureIsAlphaOnly && paint.getShader();
33}
34
bsalomonb1b01992015-11-18 10:56:08 -080035//////////////////////////////////////////////////////////////////////////////
36// Helper functions for dropping src rect constraint in bilerp mode.
37
38static const SkScalar kColorBleedTolerance = 0.001f;
39
40static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
41 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050042 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
43 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
44 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080045 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
46 return true;
47 }
48 return false;
49}
50
51static bool may_color_bleed(const SkRect& srcRect,
52 const SkRect& transformedRect,
53 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060054 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080055 // Only gets called if has_aligned_samples returned false.
56 // So we can assume that sampling is axis aligned but not texel aligned.
57 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
58 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Chris Dalton6ce447a2019-06-23 18:07:38 -060059 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080060 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
61 } else {
62 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
63 }
64 m.mapRect(&innerTransformedRect, innerSrcRect);
65
66 // The gap between outerTransformedRect and innerTransformedRect
67 // represents the projection of the source border area, which is
68 // problematic for color bleeding. We must check whether any
69 // destination pixels sample the border area.
70 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
71 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
72 SkIRect outer, inner;
73 outerTransformedRect.round(&outer);
74 innerTransformedRect.round(&inner);
75 // If the inner and outer rects round to the same result, it means the
76 // border does not overlap any pixel centers. Yay!
77 return inner != outer;
78}
79
80static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
81 const SkRect& srcRect,
82 const SkMatrix& srcRectToDeviceSpace,
Chris Dalton6ce447a2019-06-23 18:07:38 -060083 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080084 if (srcRectToDeviceSpace.rectStaysRect()) {
85 // sampling is axis-aligned
86 SkRect transformedRect;
87 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
88
89 if (has_aligned_samples(srcRect, transformedRect) ||
Chris Dalton6ce447a2019-06-23 18:07:38 -060090 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080091 return true;
92 }
93 }
94 return false;
95}
96
Michael Ludwigdd86fb32020-03-10 09:55:35 -040097//////////////////////////////////////////////////////////////////////////////
98// Helper functions for tiling a large SkBitmap
99
100static const int kBmpSmallTileSize = 1 << 10;
101
102static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
103 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
104 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
105 return tilesX * tilesY;
106}
107
108static int determine_tile_size(const SkIRect& src, int maxTileSize) {
109 if (maxTileSize <= kBmpSmallTileSize) {
110 return maxTileSize;
111 }
112
113 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
114 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
115
116 maxTileTotalTileSize *= maxTileSize * maxTileSize;
117 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
118
119 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
120 return kBmpSmallTileSize;
121 } else {
122 return maxTileSize;
123 }
124}
125
126// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
127// pixels from the bitmap are necessary.
128static void determine_clipped_src_rect(int width, int height,
129 const GrClip& clip,
130 const SkMatrix& viewMatrix,
131 const SkMatrix& srcToDstRect,
132 const SkISize& imageDimensions,
133 const SkRect* srcRectPtr,
134 SkIRect* clippedSrcIRect) {
135 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
136 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
137 if (!inv.invert(&inv)) {
138 clippedSrcIRect->setEmpty();
139 return;
140 }
141 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
142 inv.mapRect(&clippedSrcRect);
143 if (srcRectPtr) {
144 if (!clippedSrcRect.intersect(*srcRectPtr)) {
145 clippedSrcIRect->setEmpty();
146 return;
147 }
148 }
149 clippedSrcRect.roundOut(clippedSrcIRect);
150 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
151 if (!clippedSrcIRect->intersect(bmpBounds)) {
152 clippedSrcIRect->setEmpty();
153 }
154}
155
Michael Ludwig47237242020-03-10 16:16:17 -0400156// tileSize and clippedSubset are valid if true is returned
157static bool should_tile_image_id(GrContext* context,
158 SkISize rtSize,
159 const GrClip& clip,
160 uint32_t imageID,
161 const SkISize& imageSize,
162 const SkMatrix& ctm,
163 const SkMatrix& srcToDst,
164 const SkRect* src,
165 int maxTileSize,
166 int* tileSize,
167 SkIRect* clippedSubset) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400168 // if it's larger than the max tile size, then we have no choice but tiling.
Michael Ludwig47237242020-03-10 16:16:17 -0400169 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
170 determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst,
171 imageSize, src, clippedSubset);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400172 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
173 return true;
174 }
175
176 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
Michael Ludwig47237242020-03-10 16:16:17 -0400177 const size_t area = imageSize.width() * imageSize.height();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400178 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
179 return false;
180 }
181
182 // At this point we know we could do the draw by uploading the entire bitmap
183 // as a texture. However, if the texture would be large compared to the
184 // cache size and we don't require most of it for this draw then tile to
185 // reduce the amount of upload and cache spill.
186
187 // assumption here is that sw bitmap size is a good proxy for its size as
188 // a texture
189 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Michael Ludwig47237242020-03-10 16:16:17 -0400190 size_t cacheSize = context->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400191 if (bmpSize < cacheSize / 2) {
192 return false;
193 }
194
195 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
196 // tiling memory savings would be < 50%.
Michael Ludwig47237242020-03-10 16:16:17 -0400197 determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst, imageSize, src,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400198 clippedSubset);
199 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
200 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
201 kBmpSmallTileSize * kBmpSmallTileSize *
202 sizeof(SkPMColor); // assume 32bit pixels;
203
204 return usedTileBytes * 2 < bmpSize;
205}
206
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400207// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
208// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
209// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400210static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400211 const SkIRect& clamp) {
212 iRect->outset(outset, outset);
213
214 int leftClampDelta = clamp.fLeft - iRect->fLeft;
215 if (leftClampDelta > 0) {
216 offset->fX -= outset - leftClampDelta;
217 iRect->fLeft = clamp.fLeft;
218 } else {
219 offset->fX -= outset;
220 }
221
222 int topClampDelta = clamp.fTop - iRect->fTop;
223 if (topClampDelta > 0) {
224 offset->fY -= outset - topClampDelta;
225 iRect->fTop = clamp.fTop;
226 } else {
227 offset->fY -= outset;
228 }
229
230 if (iRect->fRight > clamp.fRight) {
231 iRect->fRight = clamp.fRight;
232 }
233 if (iRect->fBottom > clamp.fBottom) {
234 iRect->fBottom = clamp.fBottom;
235 }
236}
237
238//////////////////////////////////////////////////////////////////////////////
239// Helper functions for drawing an image with GrRenderTargetContext
240
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500241enum class ImageDrawMode {
242 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
243 kOptimized,
244 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
245 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
246 kDecal,
247 // Src or dst are empty, or do not intersect the image content so don't draw anything.
248 kSkip
249};
250
251/**
252 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
253 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
254 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
255 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
256 *
257 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
258 * original src rect. 'dstClip' should be null when there is no additional clipping.
259 */
260static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
261 const SkRect* origDstRect, const SkPoint dstClip[4],
262 SkRect* outSrcRect, SkRect* outDstRect,
263 SkMatrix* srcToDst) {
264 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
265
266 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
267 SkRect dst = origDstRect ? *origDstRect : src;
268
269 if (src.isEmpty() || dst.isEmpty()) {
270 return ImageDrawMode::kSkip;
271 }
272
273 if (outDstRect) {
274 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
275 } else {
276 srcToDst->setIdentity();
277 }
278
279 if (origSrcRect && !srcBounds.contains(src)) {
280 if (!src.intersect(srcBounds)) {
281 return ImageDrawMode::kSkip;
282 }
283 srcToDst->mapRect(&dst, src);
284
285 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
286 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
287 if (dstClip) {
288 for (int i = 0; i < 4; ++i) {
289 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
290 // Must resort to using a decal mode restricted to the clipped 'src', and
291 // use the original dst rect (filling in src bounds as needed)
292 *outSrcRect = src;
293 *outDstRect = (origDstRect ? *origDstRect
294 : (origSrcRect ? *origSrcRect : srcBounds));
295 return ImageDrawMode::kDecal;
296 }
297 }
298 }
299 }
300
301 // The original src and dst were fully contained in the image, or there was no dst clip to
302 // worry about, or the clip was still contained in the restricted dst rect.
303 *outSrcRect = src;
304 *outDstRect = dst;
305 return ImageDrawMode::kOptimized;
306}
307
Brian Salomon34169692017-08-28 15:32:01 -0400308/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400309 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
310 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400311 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400312static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400313 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500314 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400315}
316
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500317// Assumes srcRect and dstRect have already been optimized to fit the proxy
318static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
319 const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
320 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500321 SkCanvas::SrcRectConstraint constraint, GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400322 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400323 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400324 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400325 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500326 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400327 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400328 switch (paint.getFilterQuality()) {
329 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400330 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400331 break;
332 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400333 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400334 break;
335 case kMedium_SkFilterQuality:
336 case kHigh_SkFilterQuality:
337 SK_ABORT("Quality level not allowed.");
338 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500339 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500340 // Must specify the strict constraint when the proxy is not functionally exact and the src
341 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500342 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500343 // Conservative estimate of how much a coord could be outset from src rect:
344 // 1/2 pixel for AA and 1/2 pixel for bilerp
345 float buffer = 0.5f * (aa == GrAA::kYes) +
346 0.5f * (filter == GrSamplerState::Filter::kBilerp);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400347 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500348 safeBounds.inset(buffer, buffer);
349 if (!safeBounds.contains(srcRect)) {
350 constraint = SkCanvas::kStrict_SrcRectConstraint;
351 }
352 }
Brian Osman3d139a42018-11-19 10:42:10 -0500353 SkPMColor4f color;
Greg Daniela4828a12019-10-11 13:51:02 -0400354 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400355 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400356 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500357 float paintAlpha = paint.getColor4f().fA;
358 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400359 }
Brian Salomon34169692017-08-28 15:32:01 -0400360
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500361 if (dstClip) {
362 // Get source coords corresponding to dstClip
363 SkPoint srcQuad[4];
364 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000365
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500366 rtc->drawTextureQuad(clip, std::move(view), srcColorInfo.colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -0500367 srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad,
368 dstClip, aa, aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500369 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
370 ctm, std::move(textureXform));
371 } else {
Greg Daniel40903af2020-01-30 14:55:05 -0500372 rtc->drawTexture(clip, std::move(view), srcColorInfo.alphaType(), filter,
373 paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint,
374 ctm, std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000375 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000376}
377
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500378// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Brian Salomon777e1462020-02-28 21:10:31 -0500379static void draw_texture_producer(GrContext* context,
380 GrRenderTargetContext* rtc,
381 const GrClip& clip,
382 const SkMatrix& ctm,
383 const SkPaint& paint,
384 GrTextureProducer* producer,
385 const SkRect& src,
386 const SkRect& dst,
387 const SkPoint dstClip[4],
388 const SkMatrix& srcToDst,
389 GrAA aa,
390 GrQuadAAFlags aaFlags,
391 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400392 GrSamplerState::WrapMode wm,
393 GrSamplerState::Filter fm,
394 bool doBicubic) {
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 Salomon4bc0c1f2019-09-30 15:12:27 -0400466 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, ctm, std::move(fp),
467 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,
511 const SkMatrix& ctm,
512 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
596 draw_texture_producer(context, rtc, clip, ctm, paint, &tileProducer, tileR,
597 rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags, constraint,
598 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
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400630 SkMatrix ctm = this->localToDevice();
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500631 if (preViewMatrix) {
632 ctm.preConcat(*preViewMatrix);
633 }
634
Michael Ludwig47237242020-03-10 16:16:17 -0400635 bool doBicubic;
636 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
637 image->width(), image->height(), paint.getFilterQuality(), ctm, srcToDst,
638 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
639
640 auto clip = this->clip();
641
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500642 // YUVA images can be stored in multiple images with different plane resolutions, so this
643 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
644 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
645 if (as_IB(image)->isYUVA()) {
646 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500647 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500648
Brian Salomon777e1462020-02-28 21:10:31 -0500649 GrYUVAImageTextureMaker maker(fContext.get(), image);
Michael Ludwig47237242020-03-10 16:16:17 -0400650 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint,
Brian Salomon777e1462020-02-28 21:10:31 -0500651 &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400652 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500653 return;
654 }
655
656 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
657 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
658 uint32_t pinnedUniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500659 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500660 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500661 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500662
Greg Daniel82c6b102020-01-21 10:33:22 -0500663 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500664 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500665 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
666 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
667 image->imageInfo().refColorSpace());
668 } else {
669 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
670 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500671
Brian Salomon777e1462020-02-28 21:10:31 -0500672 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Michael Ludwig47237242020-03-10 16:16:17 -0400673 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint,
Brian Salomon777e1462020-02-28 21:10:31 -0500674 &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400675 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500676 return;
677 }
678
Michael Ludwig47237242020-03-10 16:16:17 -0400679 // Next up, determine if the image must be tiled
680 {
681 // If image is explicitly already texture backed then we shouldn't get here.
682 SkASSERT(!image->isTextureBacked());
683
684 int tileFilterPad;
685 if (doBicubic) {
686 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
687 } else if (GrSamplerState::Filter::kNearest == fm) {
688 tileFilterPad = 0;
689 } else {
690 tileFilterPad = 1;
691 }
692 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
693 int tileSize;
694 SkIRect clippedSubset;
695 if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(),
696 fRenderTargetContext->height()),
697 clip, image->unique(), image->dimensions(), ctm, srcToDst, &src,
698 maxTileSize, &tileSize, &clippedSubset)) {
699 // Extract pixels on the CPU, since we have to split into separate textures before
700 // sending to the GPU.
701 SkBitmap bm;
702 if (as_IB(image)->getROPixels(&bm)) {
703 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram
704 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
705 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
706 draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize,
707 ctm, srcToDst, src, clippedSubset, paint, aa, constraint,
708 wrapMode, fm, doBicubic);
709 return;
710 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500711 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500712 }
713
714 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
715 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500716 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500717
718 // Lazily generated images must get drawn as a texture producer that handles the final
719 // texture creation.
720 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400721 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400722 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint,
Brian Salomon777e1462020-02-28 21:10:31 -0500723 &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400724 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500725 return;
726 }
Michael Ludwig47237242020-03-10 16:16:17 -0400727
728 SkBitmap bm;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500729 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400730 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400731 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint,
Brian Salomon777e1462020-02-28 21:10:31 -0500732 &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Michael Ludwig47237242020-03-10 16:16:17 -0400733 wrapMode, fm, doBicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500734 }
735
736 // Otherwise don't know how to draw it
737}
738
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400739void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
740 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
741 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500742 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400743 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500744 // Send every entry through drawImageQuad() to handle the more complicated paint
745 int dstClipIndex = 0;
746 for (int i = 0; i < count; ++i) {
747 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400748 SkASSERT(!set[i].fHasClip || dstClips);
749 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500750
Brian Salomondb151e02019-09-17 12:11:16 -0400751 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
752 if (set[i].fAlpha != 1.f) {
753 auto paintAlpha = paint.getAlphaf();
754 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
755 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500756 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400757 this->drawImageQuad(
758 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
759 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
760 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400761 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400762 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400763 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500764 }
765 return;
766 }
767
768 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
769 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
770 SkBlendMode mode = paint.getBlendMode();
771
772 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
773 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500774 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
775 // switches that occur within the 'n' entries.
776 int base = 0, n = 0, p = 0;
777 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500778 if (n > 0) {
779 auto textureXform = GrColorSpaceXform::Make(
780 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400781 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Michael Ludwig379e4962019-12-06 13:21:26 -0500782 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400783 filter, mode, GrAA::kYes, constraint,
784 this->localToDevice(), std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500785 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500786 base = nextBase;
787 n = 0;
788 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500789 };
790 int dstClipIndex = 0;
791 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400792 SkASSERT(!set[i].fHasClip || dstClips);
793 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
794
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500795 // Manage the dst clip pointer tracking before any continues are used so we don't lose
796 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400797 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
798 dstClipIndex += 4 * set[i].fHasClip;
799
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500800 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
801 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
802 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500803 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500804 continue;
805 }
806
Greg Danielcc21d0c2020-02-05 16:58:40 -0500807 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400808 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500809 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400810 // drawImageQuad and the proper effect to dynamically sample their planes.
811 if (!image->isYUVA()) {
812 uint32_t uniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500813 view = image->refPinnedView(this->context(), &uniqueID);
814 if (!view) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500815 view = image->refView(this->context(), GrMipMapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500816 }
817 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500818
Greg Danielcc21d0c2020-02-05 16:58:40 -0500819 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400820 // This image can't go through the texture op, send through general image pipeline
821 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500822 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400823 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
824 if (set[i].fAlpha != 1.f) {
825 auto paintAlpha = paint.getAlphaf();
826 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
827 }
828 this->drawImageQuad(
829 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400830 SkToGrQuadAAFlags(set[i].fAAFlags),
831 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400832 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400833 continue;
834 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500835
Greg Danielcc21d0c2020-02-05 16:58:40 -0500836 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500837 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500838 textures[i].fSrcRect = set[i].fSrcRect;
839 textures[i].fDstRect = set[i].fDstRect;
840 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400841 textures[i].fPreViewMatrix =
842 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500843 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
844 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
845
846 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400847 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500848 textures[i].fProxyView.proxy(),
849 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500850 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500851 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
852 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500853 draw(i);
854 }
855 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
856 // set being accumulated so increment n, and increment p if proxies are different.
857 ++n;
858 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
859 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
860 // to i - 1).
861 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500862 }
863 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500864 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500865}