blob: 0aefd7c5625aeee3e028ced0cf1918b75e18f771 [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"
Robert Phillips16bf7d32020-07-07 10:20:27 -040011#include "include/gpu/GrDirectContext.h"
12#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkDraw.h"
14#include "src/core/SkMaskFilterBase.h"
15#include "src/gpu/GrBitmapTextureMaker.h"
16#include "src/gpu/GrBlurUtils.h"
17#include "src/gpu/GrCaps.h"
18#include "src/gpu/GrColorSpaceXform.h"
19#include "src/gpu/GrImageTextureMaker.h"
Robert Phillips16bf7d32020-07-07 10:20:27 -040020#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrStyle.h"
23#include "src/gpu/GrTextureAdjuster.h"
24#include "src/gpu/GrTextureMaker.h"
25#include "src/gpu/SkGr.h"
26#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -040027#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050028#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000029#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080031
Michael Ludwig1433cfd2019-02-27 17:12:30 -050032namespace {
33
bsalomonc55271f2015-11-09 11:55:57 -080034static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
35 return textureIsAlphaOnly && paint.getShader();
36}
37
bsalomonb1b01992015-11-18 10:56:08 -080038//////////////////////////////////////////////////////////////////////////////
Brian Salomona3b02f52020-07-15 16:02:01 -040039// Helper functions for dropping src rect subset with GrSamplerState::Filter::kLinear.
bsalomonb1b01992015-11-18 10:56:08 -080040
41static const SkScalar kColorBleedTolerance = 0.001f;
42
43static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
44 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050045 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
46 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
47 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080048 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
49 return true;
50 }
51 return false;
52}
53
54static bool may_color_bleed(const SkRect& srcRect,
55 const SkRect& transformedRect,
56 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060057 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080058 // Only gets called if has_aligned_samples returned false.
59 // So we can assume that sampling is axis aligned but not texel aligned.
60 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
61 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Chris Dalton6ce447a2019-06-23 18:07:38 -060062 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080063 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
64 } else {
65 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
66 }
67 m.mapRect(&innerTransformedRect, innerSrcRect);
68
69 // The gap between outerTransformedRect and innerTransformedRect
70 // represents the projection of the source border area, which is
71 // problematic for color bleeding. We must check whether any
72 // destination pixels sample the border area.
73 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
74 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
75 SkIRect outer, inner;
76 outerTransformedRect.round(&outer);
77 innerTransformedRect.round(&inner);
78 // If the inner and outer rects round to the same result, it means the
79 // border does not overlap any pixel centers. Yay!
80 return inner != outer;
81}
82
Brian Salomona3b02f52020-07-15 16:02:01 -040083static bool can_ignore_linear_filtering_subset(const GrTextureProducer& producer,
84 const SkRect& srcSubset,
85 const SkMatrix& srcRectToDeviceSpace,
86 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080087 if (srcRectToDeviceSpace.rectStaysRect()) {
88 // sampling is axis-aligned
89 SkRect transformedRect;
Brian Salomon8f32f132020-07-14 12:30:12 -040090 srcRectToDeviceSpace.mapRect(&transformedRect, srcSubset);
bsalomonb1b01992015-11-18 10:56:08 -080091
Brian Salomon8f32f132020-07-14 12:30:12 -040092 if (has_aligned_samples(srcSubset, transformedRect) ||
93 !may_color_bleed(srcSubset, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080094 return true;
95 }
96 }
97 return false;
98}
99
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400100//////////////////////////////////////////////////////////////////////////////
101// Helper functions for tiling a large SkBitmap
102
103static const int kBmpSmallTileSize = 1 << 10;
104
105static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
106 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
107 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
108 return tilesX * tilesY;
109}
110
111static int determine_tile_size(const SkIRect& src, int maxTileSize) {
112 if (maxTileSize <= kBmpSmallTileSize) {
113 return maxTileSize;
114 }
115
116 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
117 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
118
119 maxTileTotalTileSize *= maxTileSize * maxTileSize;
120 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
121
122 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
123 return kBmpSmallTileSize;
124 } else {
125 return maxTileSize;
126 }
127}
128
129// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
130// pixels from the bitmap are necessary.
Michael Ludwigc002d562020-05-13 14:17:57 -0400131static SkIRect determine_clipped_src_rect(int width, int height,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400132 const GrClip* clip,
Michael Ludwigc002d562020-05-13 14:17:57 -0400133 const SkMatrix& viewMatrix,
134 const SkMatrix& srcToDstRect,
135 const SkISize& imageDimensions,
136 const SkRect* srcRectPtr) {
Michael Ludwige06a8972020-06-11 10:29:00 -0400137 SkIRect clippedSrcIRect = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400138 : SkIRect::MakeWH(width, height);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400139 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
140 if (!inv.invert(&inv)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400141 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400142 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400143 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400144 inv.mapRect(&clippedSrcRect);
145 if (srcRectPtr) {
146 if (!clippedSrcRect.intersect(*srcRectPtr)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400147 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400148 }
149 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400150 clippedSrcRect.roundOut(&clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400151 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
Michael Ludwigc002d562020-05-13 14:17:57 -0400152 if (!clippedSrcIRect.intersect(bmpBounds)) {
153 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400154 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400155
156 return clippedSrcIRect;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400157}
158
Michael Ludwig47237242020-03-10 16:16:17 -0400159// tileSize and clippedSubset are valid if true is returned
Robert Phillips16bf7d32020-07-07 10:20:27 -0400160static bool should_tile_image_id(GrRecordingContext* context,
Michael Ludwig47237242020-03-10 16:16:17 -0400161 SkISize rtSize,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400162 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400163 uint32_t imageID,
164 const SkISize& imageSize,
165 const SkMatrix& ctm,
166 const SkMatrix& srcToDst,
167 const SkRect* src,
168 int maxTileSize,
169 int* tileSize,
170 SkIRect* clippedSubset) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400171 // if it's larger than the max tile size, then we have no choice but tiling.
Michael Ludwig47237242020-03-10 16:16:17 -0400172 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400173 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
174 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400175 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
176 return true;
177 }
178
179 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
Michael Ludwig47237242020-03-10 16:16:17 -0400180 const size_t area = imageSize.width() * imageSize.height();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400181 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
182 return false;
183 }
184
Michael Ludwig46d91382020-05-07 09:51:12 -0400185 // At this point we know we could do the draw by uploading the entire bitmap as a texture.
186 // However, if the texture would be large compared to the cache size and we don't require most
187 // of it for this draw then tile to reduce the amount of upload and cache spill.
188 // NOTE: if the context is not a direct context, it doesn't have access to the resource cache,
189 // and theoretically, the resource cache's limits could be being changed on another thread, so
190 // even having access to just the limit wouldn't be a reliable test during recording here.
191 // Instead, we will just upload the entire image to be on the safe side and not tile.
Robert Phillips16bf7d32020-07-07 10:20:27 -0400192 auto direct = context->asDirectContext();
193 if (!direct) {
Michael Ludwig46d91382020-05-07 09:51:12 -0400194 return false;
195 }
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400196
197 // assumption here is that sw bitmap size is a good proxy for its size as
198 // a texture
199 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Robert Phillips16bf7d32020-07-07 10:20:27 -0400200 size_t cacheSize = direct->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400201 if (bmpSize < cacheSize / 2) {
202 return false;
203 }
204
205 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
206 // tiling memory savings would be < 50%.
Michael Ludwigc002d562020-05-13 14:17:57 -0400207 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
208 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400209 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
210 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
211 kBmpSmallTileSize * kBmpSmallTileSize *
212 sizeof(SkPMColor); // assume 32bit pixels;
213
214 return usedTileBytes * 2 < bmpSize;
215}
216
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400217// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
218// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
219// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400220static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400221 const SkIRect& clamp) {
222 iRect->outset(outset, outset);
223
224 int leftClampDelta = clamp.fLeft - iRect->fLeft;
225 if (leftClampDelta > 0) {
226 offset->fX -= outset - leftClampDelta;
227 iRect->fLeft = clamp.fLeft;
228 } else {
229 offset->fX -= outset;
230 }
231
232 int topClampDelta = clamp.fTop - iRect->fTop;
233 if (topClampDelta > 0) {
234 offset->fY -= outset - topClampDelta;
235 iRect->fTop = clamp.fTop;
236 } else {
237 offset->fY -= outset;
238 }
239
240 if (iRect->fRight > clamp.fRight) {
241 iRect->fRight = clamp.fRight;
242 }
243 if (iRect->fBottom > clamp.fBottom) {
244 iRect->fBottom = clamp.fBottom;
245 }
246}
247
248//////////////////////////////////////////////////////////////////////////////
249// Helper functions for drawing an image with GrRenderTargetContext
250
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500251enum class ImageDrawMode {
252 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
253 kOptimized,
254 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
255 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
256 kDecal,
257 // Src or dst are empty, or do not intersect the image content so don't draw anything.
258 kSkip
259};
260
261/**
262 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
263 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
264 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
265 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
266 *
267 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
268 * original src rect. 'dstClip' should be null when there is no additional clipping.
269 */
270static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
271 const SkRect* origDstRect, const SkPoint dstClip[4],
272 SkRect* outSrcRect, SkRect* outDstRect,
273 SkMatrix* srcToDst) {
274 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
275
276 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
277 SkRect dst = origDstRect ? *origDstRect : src;
278
279 if (src.isEmpty() || dst.isEmpty()) {
280 return ImageDrawMode::kSkip;
281 }
282
283 if (outDstRect) {
284 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
285 } else {
286 srcToDst->setIdentity();
287 }
288
289 if (origSrcRect && !srcBounds.contains(src)) {
290 if (!src.intersect(srcBounds)) {
291 return ImageDrawMode::kSkip;
292 }
293 srcToDst->mapRect(&dst, src);
294
295 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
296 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
297 if (dstClip) {
298 for (int i = 0; i < 4; ++i) {
299 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
300 // Must resort to using a decal mode restricted to the clipped 'src', and
301 // use the original dst rect (filling in src bounds as needed)
302 *outSrcRect = src;
303 *outDstRect = (origDstRect ? *origDstRect
304 : (origSrcRect ? *origSrcRect : srcBounds));
305 return ImageDrawMode::kDecal;
306 }
307 }
308 }
309 }
310
311 // The original src and dst were fully contained in the image, or there was no dst clip to
312 // worry about, or the clip was still contained in the restricted dst rect.
313 *outSrcRect = src;
314 *outDstRect = dst;
315 return ImageDrawMode::kOptimized;
316}
317
Brian Salomon34169692017-08-28 15:32:01 -0400318/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400319 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
320 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400321 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400322static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400323 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500324 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400325}
326
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400327static SkPMColor4f texture_color(SkColor4f paintColor, float entryAlpha, GrColorType srcColorType,
328 const GrColorInfo& dstColorInfo) {
329 paintColor.fA *= entryAlpha;
330 if (GrColorTypeIsAlphaOnly(srcColorType)) {
331 return SkColor4fPrepForDst(paintColor, dstColorInfo).premul();
332 } else {
333 float paintAlpha = SkTPin(paintColor.fA, 0.f, 1.f);
334 return { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
335 }
336}
337
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500338// Assumes srcRect and dstRect have already been optimized to fit the proxy
Brian Salomone69b9ef2020-07-22 11:18:06 -0400339static void draw_texture(GrRenderTargetContext* rtc,
340 const GrClip* clip,
341 const SkMatrix& ctm,
342 const SkPaint& paint,
343 const SkRect& srcRect,
344 const SkRect& dstRect,
345 const SkPoint dstClip[4],
346 GrAA aa,
347 GrQuadAAFlags aaFlags,
348 SkCanvas::SrcRectConstraint constraint,
349 GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400350 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400351 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400352 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400353 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500354 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400355 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400356 switch (paint.getFilterQuality()) {
357 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400358 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400359 break;
360 case kLow_SkFilterQuality:
Brian Salomona3b02f52020-07-15 16:02:01 -0400361 filter = GrSamplerState::Filter::kLinear;
Brian Salomon34169692017-08-28 15:32:01 -0400362 break;
363 case kMedium_SkFilterQuality:
364 case kHigh_SkFilterQuality:
365 SK_ABORT("Quality level not allowed.");
366 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500367 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500368 // Must specify the strict constraint when the proxy is not functionally exact and the src
369 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500370 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500371 // Conservative estimate of how much a coord could be outset from src rect:
Brian Salomona3b02f52020-07-15 16:02:01 -0400372 // 1/2 pixel for AA and 1/2 pixel for linear filtering
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500373 float buffer = 0.5f * (aa == GrAA::kYes) +
Brian Salomona3b02f52020-07-15 16:02:01 -0400374 0.5f * (filter == GrSamplerState::Filter::kLinear);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400375 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500376 safeBounds.inset(buffer, buffer);
377 if (!safeBounds.contains(srcRect)) {
378 constraint = SkCanvas::kStrict_SrcRectConstraint;
379 }
380 }
Brian Salomon34169692017-08-28 15:32:01 -0400381
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400382 SkPMColor4f color = texture_color(paint.getColor4f(), 1.f, srcColorInfo.colorType(), dstInfo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500383 if (dstClip) {
384 // Get source coords corresponding to dstClip
385 SkPoint srcQuad[4];
386 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000387
Brian Salomone69b9ef2020-07-22 11:18:06 -0400388 rtc->drawTextureQuad(clip,
389 std::move(view),
390 srcColorInfo.colorType(),
391 srcColorInfo.alphaType(),
392 filter,
393 GrSamplerState::MipmapMode::kNone,
394 paint.getBlendMode(),
395 color,
396 srcQuad,
397 dstClip,
398 aa,
399 aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500400 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400401 ctm,
402 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500403 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400404 rtc->drawTexture(clip,
405 std::move(view),
406 srcColorInfo.alphaType(),
407 filter,
408 GrSamplerState::MipmapMode::kNone,
409 paint.getBlendMode(),
410 color,
411 srcRect,
412 dstRect,
413 aa,
414 aaFlags,
415 constraint,
416 ctm,
417 std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000418 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000419}
420
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500421// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Robert Phillips16bf7d32020-07-07 10:20:27 -0400422static void draw_texture_producer(GrRecordingContext* context,
Brian Salomon777e1462020-02-28 21:10:31 -0500423 GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400424 const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400425 const SkMatrixProvider& matrixProvider,
Brian Salomon777e1462020-02-28 21:10:31 -0500426 const SkPaint& paint,
427 GrTextureProducer* producer,
428 const SkRect& src,
429 const SkRect& dst,
430 const SkPoint dstClip[4],
431 const SkMatrix& srcToDst,
432 GrAA aa,
433 GrQuadAAFlags aaFlags,
434 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400435 GrSamplerState sampler,
Michael Ludwig47237242020-03-10 16:16:17 -0400436 bool doBicubic) {
Brian Osman449b1152020-04-15 16:43:00 -0400437 const SkMatrix& ctm(matrixProvider.localToDevice());
Brian Salomone69b9ef2020-07-22 11:18:06 -0400438 if (sampler.wrapModeX() == GrSamplerState::WrapMode::kClamp &&
439 sampler.wrapModeY() == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
Brian Salomon777e1462020-02-28 21:10:31 -0500440 can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400441 // We've done enough checks above to allow us to pass ClampNearest() and not check for
442 // scaling adjustments.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400443 auto view = producer->view(GrMipmapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500444 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400445 return;
446 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500447
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500448 draw_texture(
449 rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint, std::move(view),
450 {producer->colorType(), producer->alphaType(), sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400451 return;
452 }
453
bsalomonc55271f2015-11-09 11:55:57 -0800454 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500455
bsalomonc55271f2015-11-09 11:55:57 -0800456 // The shader expects proper local coords, so we can't replace local coords with texture coords
457 // if the shader will be used. If we have a mask filter we will change the underlying geometry
458 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800459 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800460
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500461 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500462 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500463 // FP. In the future this should be an opaque optimization enabled by the combination of
464 // GrDrawOp/GP and FP.
465 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
466 mf = nullptr;
467 }
bsalomonc55271f2015-11-09 11:55:57 -0800468
Brian Salomon0ea33072020-07-14 10:43:42 -0400469 bool restrictToSubset = SkCanvas::kStrict_SrcRectConstraint == constraint;
halcanary9d524f22016-03-29 09:03:52 -0700470
bsalomonc55271f2015-11-09 11:55:57 -0800471 // If we have to outset for AA then we will generate texture coords outside the src rect. The
472 // same happens for any mask filter that extends the bounds rendered in the dst.
473 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500474 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800475
Brian Salomona3b02f52020-07-15 16:02:01 -0400476 // Check for optimization to drop the src rect constraint when using linear filtering.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400477 if (!doBicubic && sampler.filter() == GrSamplerState::Filter::kLinear && restrictToSubset &&
478 sampler.mipmapped() == GrMipmapped::kNo && coordsAllInsideSrcRect &&
479 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800480 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500481 combinedMatrix.setConcat(ctm, srcToDst);
Brian Salomona3b02f52020-07-15 16:02:01 -0400482 if (can_ignore_linear_filtering_subset(*producer, src, combinedMatrix, rtc->numSamples())) {
Brian Salomon0ea33072020-07-14 10:43:42 -0400483 restrictToSubset = false;
bsalomonb1b01992015-11-18 10:56:08 -0800484 }
485 }
486
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500487 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800488 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500489 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800490 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500491 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800492 return;
493 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800494 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400495 const SkRect* subset = restrictToSubset ? &src : nullptr;
496 const SkRect* domain = coordsAllInsideSrcRect ? &src : nullptr;
497 std::unique_ptr<GrFragmentProcessor> fp;
498 if (doBicubic) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400499 fp = producer->createBicubicFragmentProcessor(textureMatrix, subset, domain,
500 sampler.wrapModeX(), sampler.wrapModeY());
Brian Salomon0ea33072020-07-14 10:43:42 -0400501 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400502 fp = producer->createFragmentProcessor(textureMatrix, subset, domain, sampler);
Brian Salomon0ea33072020-07-14 10:43:42 -0400503 }
bsalomonc55271f2015-11-09 11:55:57 -0800504 if (!fp) {
505 return;
506 }
Brian Osman958a3bb2020-07-30 14:13:23 -0400507 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
508 rtc->colorInfo().colorSpace(), kPremul_SkAlphaType);
509 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
joshualitt33a5fce2015-11-18 13:28:51 -0800510
bsalomonc55271f2015-11-09 11:55:57 -0800511 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400512 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
513 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800514 return;
515 }
516
517 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500518 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
519 if (dstClip) {
520 SkPoint srcClipPoints[4];
521 SkPoint* srcClip = nullptr;
522 if (canUseTextureCoordsAsLocalCoords) {
523 // Calculate texture coordinates that match the dst clip
524 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
525 srcClip = srcClipPoints;
526 }
527 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
528 } else {
529 // Provide explicit texture coords when possible, otherwise rely on texture matrix
530 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
531 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
532 }
533 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000534 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
535 // information since it always draws with AA, but that should not be noticeable since the
536 // mask filter is probably a blur.
537 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500538 if (dstClip) {
539 // Represent it as an SkPath formed from the dstClip
540 SkPath path;
541 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000542 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500543 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000544 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500545 }
546
547 GrBlurUtils::drawShapeWithMaskFilter(
548 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
549 }
550}
551
Robert Phillips16bf7d32020-07-07 10:20:27 -0400552void draw_tiled_bitmap(GrRecordingContext* context,
Michael Ludwig47237242020-03-10 16:16:17 -0400553 GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400554 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400555 const SkBitmap& bitmap,
556 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400557 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400558 const SkMatrix& srcToDst,
559 const SkRect& srcRect,
560 const SkIRect& clippedSrcIRect,
561 const SkPaint& paint,
562 GrAA aa,
563 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400564 GrSamplerState sampler,
Michael Ludwig47237242020-03-10 16:16:17 -0400565 bool doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400566 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
567
568 int nx = bitmap.width() / tileSize;
569 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400570
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400571 for (int x = 0; x <= nx; x++) {
572 for (int y = 0; y <= ny; y++) {
573 SkRect tileR;
574 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
575 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
576
577 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
578 continue;
579 }
580
581 if (!tileR.intersect(srcRect)) {
582 continue;
583 }
584
585 SkIRect iTileR;
586 tileR.roundOut(&iTileR);
587 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
588 SkIntToScalar(iTileR.fTop));
589 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400590 srcToDst.mapRect(&rectToDraw);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400591 if (sampler.filter() != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400592 SkIRect iClampRect;
593
594 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
595 // In bleed mode we want to always expand the tile on all edges
596 // but stay within the bitmap bounds
597 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
598 } else {
599 // In texture-domain/clamp mode we only want to expand the
600 // tile on edges interior to "srcRect" (i.e., we want to
601 // not bleed across the original clamped edges)
602 srcRect.roundOut(&iClampRect);
603 }
Michael Ludwig47237242020-03-10 16:16:17 -0400604 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400605 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
606 }
607
608 SkBitmap tmpB;
609 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400610 // We should have already handled bitmaps larger than the max texture size.
611 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
612 tmpB.height() <= context->priv().caps()->maxTextureSize());
613 // We should be respecting the max tile size by the time we get here.
614 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
615 tmpB.height() <= context->priv().caps()->maxTileSize());
616
Brian Salomonbc074a62020-03-18 10:06:13 -0400617 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400618
619 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
620 if (aa == GrAA::kYes) {
621 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
622 if (tileR.fLeft <= srcRect.fLeft) {
623 aaFlags |= GrQuadAAFlags::kLeft;
624 }
625 if (tileR.fRight >= srcRect.fRight) {
626 aaFlags |= GrQuadAAFlags::kRight;
627 }
628 if (tileR.fTop <= srcRect.fTop) {
629 aaFlags |= GrQuadAAFlags::kTop;
630 }
631 if (tileR.fBottom >= srcRect.fBottom) {
632 aaFlags |= GrQuadAAFlags::kBottom;
633 }
634 }
635
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400636 // now offset it to make it "local" to our tmp bitmap
637 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400638 SkMatrix offsetSrcToDst = srcToDst;
639 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
Brian Osman449b1152020-04-15 16:43:00 -0400640 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
641 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400642 constraint, sampler, doBicubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400643 }
644 }
645 }
646}
647
Michael Ludwig47237242020-03-10 16:16:17 -0400648} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400649
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500650//////////////////////////////////////////////////////////////////////////////
651
652void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
653 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500654 const SkMatrix* preViewMatrix, const SkPaint& paint,
655 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500656 SkRect src;
657 SkRect dst;
658 SkMatrix srcToDst;
659 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
660 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
661 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800662 return;
663 }
664
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500665 if (src.contains(image->bounds())) {
666 constraint = SkCanvas::kFast_SrcRectConstraint;
667 }
668 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500669 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
670 ? GrSamplerState::WrapMode::kClampToBorder
671 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400672
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500673 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400674 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
675 preViewMatrix ? *preViewMatrix : SkMatrix::I());
676 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500677
Brian Salomone69b9ef2020-07-22 11:18:06 -0400678 bool sharpenMM = fContext->priv().options().fSharpenMipmappedTextures;
679 auto [fm, mm, bicubic] = GrInterpretFilterQuality(image->dimensions(), paint.getFilterQuality(),
John Stilesf2f9b0d2020-08-25 13:33:45 -0400680 ctm, srcToDst, sharpenMM,
681 /*allowFilterQualityReduction=*/true);
Michael Ludwig47237242020-03-10 16:16:17 -0400682
683 auto clip = this->clip();
684
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500685 // YUVA images can be stored in multiple images with different plane resolutions, so this
686 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
687 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
688 if (as_IB(image)->isYUVA()) {
689 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500690 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500691
Brian Salomon777e1462020-02-28 21:10:31 -0500692 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Osman449b1152020-04-15 16:43:00 -0400693 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
694 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400695 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500696 return;
697 }
698
699 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
700 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
701 uint32_t pinnedUniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400702 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->recordingContext(),
703 &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500704 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500705 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500706
Greg Daniel82c6b102020-01-21 10:33:22 -0500707 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500708 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500709 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
710 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
711 image->imageInfo().refColorSpace());
712 } else {
713 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
714 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500715
Brian Salomon777e1462020-02-28 21:10:31 -0500716 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Osman449b1152020-04-15 16:43:00 -0400717 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
718 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400719 constraint, {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500720 return;
721 }
722
Michael Ludwig47237242020-03-10 16:16:17 -0400723 // Next up, determine if the image must be tiled
724 {
725 // If image is explicitly already texture backed then we shouldn't get here.
726 SkASSERT(!image->isTextureBacked());
727
728 int tileFilterPad;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400729 if (bicubic) {
Michael Ludwig47237242020-03-10 16:16:17 -0400730 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
731 } else if (GrSamplerState::Filter::kNearest == fm) {
732 tileFilterPad = 0;
733 } else {
734 tileFilterPad = 1;
735 }
736 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
737 int tileSize;
738 SkIRect clippedSubset;
739 if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(),
740 fRenderTargetContext->height()),
741 clip, image->unique(), image->dimensions(), ctm, srcToDst, &src,
742 maxTileSize, &tileSize, &clippedSubset)) {
743 // Extract pixels on the CPU, since we have to split into separate textures before
744 // sending to the GPU.
745 SkBitmap bm;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400746 if (as_IB(image)->getROPixels(nullptr, &bm)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400747 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram
748 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
749 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
750 draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400751 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400752 constraint, {wrapMode, fm, mm}, bicubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400753 return;
754 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500755 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500756 }
757
758 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
759 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500760 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500761
762 // Lazily generated images must get drawn as a texture producer that handles the final
763 // texture creation.
764 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400765 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400766 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
767 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400768 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500769 return;
770 }
Michael Ludwig47237242020-03-10 16:16:17 -0400771
772 SkBitmap bm;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400773 if (as_IB(image)->getROPixels(nullptr, &bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400774 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400775 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
776 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400777 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500778 }
779
780 // Otherwise don't know how to draw it
781}
782
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400783void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
784 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
785 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500786 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400787 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500788 // Send every entry through drawImageQuad() to handle the more complicated paint
789 int dstClipIndex = 0;
790 for (int i = 0; i < count; ++i) {
791 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400792 SkASSERT(!set[i].fHasClip || dstClips);
793 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500794
Brian Salomondb151e02019-09-17 12:11:16 -0400795 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
796 if (set[i].fAlpha != 1.f) {
797 auto paintAlpha = paint.getAlphaf();
798 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
799 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500800 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400801 this->drawImageQuad(
802 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
803 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
804 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400805 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400806 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400807 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500808 }
809 return;
810 }
811
Brian Salomona3b02f52020-07-15 16:02:01 -0400812 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality()
813 ? GrSamplerState::Filter::kNearest
814 : GrSamplerState::Filter::kLinear;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500815 SkBlendMode mode = paint.getBlendMode();
816
817 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
818 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500819 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
820 // switches that occur within the 'n' entries.
821 int base = 0, n = 0, p = 0;
822 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500823 if (n > 0) {
824 auto textureXform = GrColorSpaceXform::Make(
825 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400826 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400827 fRenderTargetContext->drawTextureSet(this->clip(),
828 textures.get() + base,
829 n,
830 p,
831 filter,
832 GrSamplerState::MipmapMode::kNone,
833 mode,
834 GrAA::kYes,
835 constraint,
836 this->localToDevice(),
837 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500838 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500839 base = nextBase;
840 n = 0;
841 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500842 };
843 int dstClipIndex = 0;
844 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400845 SkASSERT(!set[i].fHasClip || dstClips);
846 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
847
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500848 // Manage the dst clip pointer tracking before any continues are used so we don't lose
849 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400850 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
851 dstClipIndex += 4 * set[i].fHasClip;
852
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500853 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
854 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
855 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500856 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500857 continue;
858 }
859
Greg Danielcc21d0c2020-02-05 16:58:40 -0500860 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400861 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500862 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400863 // drawImageQuad and the proper effect to dynamically sample their planes.
864 if (!image->isYUVA()) {
865 uint32_t uniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400866 view = image->refPinnedView(this->recordingContext(), &uniqueID);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500867 if (!view) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400868 view = image->refView(this->recordingContext(), GrMipmapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500869 }
870 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500871
Greg Danielcc21d0c2020-02-05 16:58:40 -0500872 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400873 // This image can't go through the texture op, send through general image pipeline
874 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500875 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400876 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
877 if (set[i].fAlpha != 1.f) {
878 auto paintAlpha = paint.getAlphaf();
879 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
880 }
881 this->drawImageQuad(
882 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400883 SkToGrQuadAAFlags(set[i].fAAFlags),
884 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400885 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400886 continue;
887 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500888
Greg Danielcc21d0c2020-02-05 16:58:40 -0500889 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500890 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500891 textures[i].fSrcRect = set[i].fSrcRect;
892 textures[i].fDstRect = set[i].fDstRect;
893 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400894 textures[i].fPreViewMatrix =
895 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400896 textures[i].fColor = texture_color(paint.getColor4f(), set[i].fAlpha,
897 SkColorTypeToGrColorType(image->colorType()),
898 fRenderTargetContext->colorInfo());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500899 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
900
901 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400902 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500903 textures[i].fProxyView.proxy(),
904 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500905 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500906 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
907 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500908 draw(i);
909 }
910 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
911 // set being accumulated so increment n, and increment p if proxies are different.
912 ++n;
913 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
914 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
915 // to i - 1).
916 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500917 }
918 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500919 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500920}