blob: 45a9a25b16b9e308b83f6336e9e982058316ce7b [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
Robert Phillips16bf7d32020-07-07 10:20:27 -040010#include "include/gpu/GrDirectContext.h"
11#include "include/gpu/GrRecordingContext.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050012#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkDraw.h"
14#include "src/core/SkMaskFilterBase.h"
Michael Ludwig16d5b0a2020-08-31 13:07:16 -040015#include "src/core/SkSpecialImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrBitmapTextureMaker.h"
17#include "src/gpu/GrBlurUtils.h"
18#include "src/gpu/GrCaps.h"
19#include "src/gpu/GrColorSpaceXform.h"
20#include "src/gpu/GrImageTextureMaker.h"
Robert Phillips16bf7d32020-07-07 10:20:27 -040021#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050023#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrTextureAdjuster.h"
25#include "src/gpu/GrTextureMaker.h"
26#include "src/gpu/SkGr.h"
27#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -040028#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050029#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000030#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080032
Michael Ludwig1433cfd2019-02-27 17:12:30 -050033namespace {
34
bsalomonc55271f2015-11-09 11:55:57 -080035static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
36 return textureIsAlphaOnly && paint.getShader();
37}
38
bsalomonb1b01992015-11-18 10:56:08 -080039//////////////////////////////////////////////////////////////////////////////
Brian Salomona3b02f52020-07-15 16:02:01 -040040// Helper functions for dropping src rect subset with GrSamplerState::Filter::kLinear.
bsalomonb1b01992015-11-18 10:56:08 -080041
42static const SkScalar kColorBleedTolerance = 0.001f;
43
44static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
45 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050046 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
47 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
48 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080049 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
50 return true;
51 }
52 return false;
53}
54
55static bool may_color_bleed(const SkRect& srcRect,
56 const SkRect& transformedRect,
57 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060058 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080059 // Only gets called if has_aligned_samples returned false.
60 // So we can assume that sampling is axis aligned but not texel aligned.
61 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
62 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Chris Dalton6ce447a2019-06-23 18:07:38 -060063 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080064 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
65 } else {
66 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
67 }
68 m.mapRect(&innerTransformedRect, innerSrcRect);
69
70 // The gap between outerTransformedRect and innerTransformedRect
71 // represents the projection of the source border area, which is
72 // problematic for color bleeding. We must check whether any
73 // destination pixels sample the border area.
74 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
75 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
76 SkIRect outer, inner;
77 outerTransformedRect.round(&outer);
78 innerTransformedRect.round(&inner);
79 // If the inner and outer rects round to the same result, it means the
80 // border does not overlap any pixel centers. Yay!
81 return inner != outer;
82}
83
Brian Salomona3b02f52020-07-15 16:02:01 -040084static bool can_ignore_linear_filtering_subset(const GrTextureProducer& producer,
85 const SkRect& srcSubset,
86 const SkMatrix& srcRectToDeviceSpace,
87 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080088 if (srcRectToDeviceSpace.rectStaysRect()) {
89 // sampling is axis-aligned
90 SkRect transformedRect;
Brian Salomon8f32f132020-07-14 12:30:12 -040091 srcRectToDeviceSpace.mapRect(&transformedRect, srcSubset);
bsalomonb1b01992015-11-18 10:56:08 -080092
Brian Salomon8f32f132020-07-14 12:30:12 -040093 if (has_aligned_samples(srcSubset, transformedRect) ||
94 !may_color_bleed(srcSubset, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080095 return true;
96 }
97 }
98 return false;
99}
100
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400101//////////////////////////////////////////////////////////////////////////////
102// Helper functions for tiling a large SkBitmap
103
104static const int kBmpSmallTileSize = 1 << 10;
105
106static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
107 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
108 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
109 return tilesX * tilesY;
110}
111
112static int determine_tile_size(const SkIRect& src, int maxTileSize) {
113 if (maxTileSize <= kBmpSmallTileSize) {
114 return maxTileSize;
115 }
116
117 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
118 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
119
120 maxTileTotalTileSize *= maxTileSize * maxTileSize;
121 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
122
123 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
124 return kBmpSmallTileSize;
125 } else {
126 return maxTileSize;
127 }
128}
129
130// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
131// pixels from the bitmap are necessary.
Michael Ludwigc002d562020-05-13 14:17:57 -0400132static SkIRect determine_clipped_src_rect(int width, int height,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400133 const GrClip* clip,
Michael Ludwigc002d562020-05-13 14:17:57 -0400134 const SkMatrix& viewMatrix,
135 const SkMatrix& srcToDstRect,
136 const SkISize& imageDimensions,
137 const SkRect* srcRectPtr) {
Michael Ludwige06a8972020-06-11 10:29:00 -0400138 SkIRect clippedSrcIRect = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400139 : SkIRect::MakeWH(width, height);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400140 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
141 if (!inv.invert(&inv)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400142 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400143 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400144 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400145 inv.mapRect(&clippedSrcRect);
146 if (srcRectPtr) {
147 if (!clippedSrcRect.intersect(*srcRectPtr)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400148 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400149 }
150 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400151 clippedSrcRect.roundOut(&clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400152 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
Michael Ludwigc002d562020-05-13 14:17:57 -0400153 if (!clippedSrcIRect.intersect(bmpBounds)) {
154 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400155 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400156
157 return clippedSrcIRect;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400158}
159
Michael Ludwig47237242020-03-10 16:16:17 -0400160// tileSize and clippedSubset are valid if true is returned
Robert Phillips16bf7d32020-07-07 10:20:27 -0400161static bool should_tile_image_id(GrRecordingContext* context,
Michael Ludwig47237242020-03-10 16:16:17 -0400162 SkISize rtSize,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400163 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400164 uint32_t imageID,
165 const SkISize& imageSize,
166 const SkMatrix& ctm,
167 const SkMatrix& srcToDst,
168 const SkRect* src,
169 int maxTileSize,
170 int* tileSize,
171 SkIRect* clippedSubset) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400172 // if it's larger than the max tile size, then we have no choice but tiling.
Michael Ludwig47237242020-03-10 16:16:17 -0400173 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400174 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
175 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400176 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
177 return true;
178 }
179
180 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
Michael Ludwig47237242020-03-10 16:16:17 -0400181 const size_t area = imageSize.width() * imageSize.height();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400182 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
183 return false;
184 }
185
Michael Ludwig46d91382020-05-07 09:51:12 -0400186 // At this point we know we could do the draw by uploading the entire bitmap as a texture.
187 // However, if the texture would be large compared to the cache size and we don't require most
188 // of it for this draw then tile to reduce the amount of upload and cache spill.
189 // NOTE: if the context is not a direct context, it doesn't have access to the resource cache,
190 // and theoretically, the resource cache's limits could be being changed on another thread, so
191 // even having access to just the limit wouldn't be a reliable test during recording here.
192 // Instead, we will just upload the entire image to be on the safe side and not tile.
Robert Phillips16bf7d32020-07-07 10:20:27 -0400193 auto direct = context->asDirectContext();
194 if (!direct) {
Michael Ludwig46d91382020-05-07 09:51:12 -0400195 return false;
196 }
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400197
198 // assumption here is that sw bitmap size is a good proxy for its size as
199 // a texture
200 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Robert Phillips16bf7d32020-07-07 10:20:27 -0400201 size_t cacheSize = direct->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400202 if (bmpSize < cacheSize / 2) {
203 return false;
204 }
205
206 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
207 // tiling memory savings would be < 50%.
Michael Ludwigc002d562020-05-13 14:17:57 -0400208 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
209 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400210 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
211 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
212 kBmpSmallTileSize * kBmpSmallTileSize *
213 sizeof(SkPMColor); // assume 32bit pixels;
214
215 return usedTileBytes * 2 < bmpSize;
216}
217
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400218// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
219// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
220// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400221static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400222 const SkIRect& clamp) {
223 iRect->outset(outset, outset);
224
225 int leftClampDelta = clamp.fLeft - iRect->fLeft;
226 if (leftClampDelta > 0) {
227 offset->fX -= outset - leftClampDelta;
228 iRect->fLeft = clamp.fLeft;
229 } else {
230 offset->fX -= outset;
231 }
232
233 int topClampDelta = clamp.fTop - iRect->fTop;
234 if (topClampDelta > 0) {
235 offset->fY -= outset - topClampDelta;
236 iRect->fTop = clamp.fTop;
237 } else {
238 offset->fY -= outset;
239 }
240
241 if (iRect->fRight > clamp.fRight) {
242 iRect->fRight = clamp.fRight;
243 }
244 if (iRect->fBottom > clamp.fBottom) {
245 iRect->fBottom = clamp.fBottom;
246 }
247}
248
249//////////////////////////////////////////////////////////////////////////////
Brian Salomoneebe7352020-12-09 16:37:04 -0500250// Helper functions for drawing an image with GrSurfaceDrawContext
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400251
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500252enum class ImageDrawMode {
253 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
254 kOptimized,
255 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
256 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
257 kDecal,
258 // Src or dst are empty, or do not intersect the image content so don't draw anything.
259 kSkip
260};
261
262/**
263 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
264 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
265 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
266 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
267 *
268 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
269 * original src rect. 'dstClip' should be null when there is no additional clipping.
270 */
271static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
272 const SkRect* origDstRect, const SkPoint dstClip[4],
273 SkRect* outSrcRect, SkRect* outDstRect,
274 SkMatrix* srcToDst) {
275 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
276
277 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
278 SkRect dst = origDstRect ? *origDstRect : src;
279
280 if (src.isEmpty() || dst.isEmpty()) {
281 return ImageDrawMode::kSkip;
282 }
283
284 if (outDstRect) {
Mike Reed2ac6ce82021-01-15 12:26:22 -0500285 *srcToDst = SkMatrix::RectToRect(src, dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500286 } else {
287 srcToDst->setIdentity();
288 }
289
290 if (origSrcRect && !srcBounds.contains(src)) {
291 if (!src.intersect(srcBounds)) {
292 return ImageDrawMode::kSkip;
293 }
294 srcToDst->mapRect(&dst, src);
295
296 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
297 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
298 if (dstClip) {
299 for (int i = 0; i < 4; ++i) {
300 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
301 // Must resort to using a decal mode restricted to the clipped 'src', and
302 // use the original dst rect (filling in src bounds as needed)
303 *outSrcRect = src;
304 *outDstRect = (origDstRect ? *origDstRect
305 : (origSrcRect ? *origSrcRect : srcBounds));
306 return ImageDrawMode::kDecal;
307 }
308 }
309 }
310 }
311
312 // The original src and dst were fully contained in the image, or there was no dst clip to
313 // worry about, or the clip was still contained in the restricted dst rect.
314 *outSrcRect = src;
315 *outDstRect = dst;
316 return ImageDrawMode::kOptimized;
317}
318
Brian Salomon34169692017-08-28 15:32:01 -0400319/**
Brian Salomoneebe7352020-12-09 16:37:04 -0500320 * Checks whether the paint is compatible with using GrSurfaceDrawContext::drawTexture. It is more
Brian Salomonb80ffee2018-05-23 16:39:39 -0400321 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400322 */
Mike Reed4f23dec2020-12-30 14:22:42 +0000323static bool can_use_draw_texture(const SkPaint& paint, bool useCubicResampler, SkMipmapMode mm) {
Brian Salomon34169692017-08-28 15:32:01 -0400324 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Mike Reed4f23dec2020-12-30 14:22:42 +0000325 !paint.getImageFilter() && !useCubicResampler && mm == SkMipmapMode::kNone);
Brian Salomon34169692017-08-28 15:32:01 -0400326}
327
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400328static SkPMColor4f texture_color(SkColor4f paintColor, float entryAlpha, GrColorType srcColorType,
329 const GrColorInfo& dstColorInfo) {
330 paintColor.fA *= entryAlpha;
331 if (GrColorTypeIsAlphaOnly(srcColorType)) {
332 return SkColor4fPrepForDst(paintColor, dstColorInfo).premul();
333 } else {
334 float paintAlpha = SkTPin(paintColor.fA, 0.f, 1.f);
335 return { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
336 }
337}
338
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500339// Assumes srcRect and dstRect have already been optimized to fit the proxy
Brian Salomoneebe7352020-12-09 16:37:04 -0500340static void draw_texture(GrSurfaceDrawContext* rtc,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400341 const GrClip* clip,
342 const SkMatrix& ctm,
343 const SkPaint& paint,
Mike Reed869a7172020-12-28 14:45:28 -0500344 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400345 const SkRect& srcRect,
346 const SkRect& dstRect,
347 const SkPoint dstClip[4],
348 GrAA aa,
349 GrQuadAAFlags aaFlags,
350 SkCanvas::SrcRectConstraint constraint,
351 GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400352 const GrColorInfo& srcColorInfo) {
Brian Salomonb43d6992021-01-05 14:37:40 -0500353 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
354 view.concatSwizzle(GrSwizzle("aaaa"));
355 }
Brian Salomon7f296c42021-01-13 14:56:55 +0000356 const GrColorInfo& dstInfo(rtc->colorInfo());
357 auto textureXform =
358 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
359 dstInfo.colorSpace(), kPremul_SkAlphaType);
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500360 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500361 // Must specify the strict constraint when the proxy is not functionally exact and the src
362 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500363 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500364 // Conservative estimate of how much a coord could be outset from src rect:
Brian Salomona3b02f52020-07-15 16:02:01 -0400365 // 1/2 pixel for AA and 1/2 pixel for linear filtering
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500366 float buffer = 0.5f * (aa == GrAA::kYes) +
Brian Salomona3b02f52020-07-15 16:02:01 -0400367 0.5f * (filter == GrSamplerState::Filter::kLinear);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400368 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500369 safeBounds.inset(buffer, buffer);
370 if (!safeBounds.contains(srcRect)) {
371 constraint = SkCanvas::kStrict_SrcRectConstraint;
372 }
373 }
Brian Salomon34169692017-08-28 15:32:01 -0400374
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400375 SkPMColor4f color = texture_color(paint.getColor4f(), 1.f, srcColorInfo.colorType(), dstInfo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500376 if (dstClip) {
377 // Get source coords corresponding to dstClip
378 SkPoint srcQuad[4];
379 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000380
Brian Salomone69b9ef2020-07-22 11:18:06 -0400381 rtc->drawTextureQuad(clip,
382 std::move(view),
383 srcColorInfo.colorType(),
384 srcColorInfo.alphaType(),
385 filter,
386 GrSamplerState::MipmapMode::kNone,
387 paint.getBlendMode(),
388 color,
389 srcQuad,
390 dstClip,
391 aa,
392 aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500393 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400394 ctm,
395 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500396 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400397 rtc->drawTexture(clip,
398 std::move(view),
399 srcColorInfo.alphaType(),
400 filter,
401 GrSamplerState::MipmapMode::kNone,
402 paint.getBlendMode(),
403 color,
404 srcRect,
405 dstRect,
406 aa,
407 aaFlags,
408 constraint,
409 ctm,
410 std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000411 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000412}
413
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500414// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Robert Phillips16bf7d32020-07-07 10:20:27 -0400415static void draw_texture_producer(GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -0500416 GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400417 const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400418 const SkMatrixProvider& matrixProvider,
Brian Salomon777e1462020-02-28 21:10:31 -0500419 const SkPaint& paint,
420 GrTextureProducer* producer,
421 const SkRect& src,
422 const SkRect& dst,
423 const SkPoint dstClip[4],
424 const SkMatrix& srcToDst,
425 GrAA aa,
426 GrQuadAAFlags aaFlags,
427 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400428 GrSamplerState sampler,
Mike Reed52130b02020-12-28 15:33:13 -0500429 SkCubicResampler cubic) {
Brian Osman449b1152020-04-15 16:43:00 -0400430 const SkMatrix& ctm(matrixProvider.localToDevice());
Brian Salomone69b9ef2020-07-22 11:18:06 -0400431 if (sampler.wrapModeX() == GrSamplerState::WrapMode::kClamp &&
432 sampler.wrapModeY() == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
Mike Reed4f23dec2020-12-30 14:22:42 +0000433 can_use_draw_texture(paint, GrValidCubicResampler(cubic), sampler.mipmapMode())) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400434 // We've done enough checks above to allow us to pass ClampNearest() and not check for
435 // scaling adjustments.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400436 auto view = producer->view(GrMipmapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500437 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400438 return;
439 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500440
Mike Reed869a7172020-12-28 14:45:28 -0500441 draw_texture(rtc,
442 clip,
443 ctm,
444 paint,
445 sampler.filter(),
446 src,
447 dst,
448 dstClip,
449 aa,
450 aaFlags,
451 constraint,
452 std::move(view),
Brian Salomonb43d6992021-01-05 14:37:40 -0500453 {producer->colorType(),
454 producer->alphaType(),
455 sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400456 return;
457 }
458
bsalomonc55271f2015-11-09 11:55:57 -0800459 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500460
bsalomonc55271f2015-11-09 11:55:57 -0800461 // The shader expects proper local coords, so we can't replace local coords with texture coords
462 // if the shader will be used. If we have a mask filter we will change the underlying geometry
463 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800464 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800465
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500466 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500467 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500468 // FP. In the future this should be an opaque optimization enabled by the combination of
469 // GrDrawOp/GP and FP.
470 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
471 mf = nullptr;
472 }
bsalomonc55271f2015-11-09 11:55:57 -0800473
Brian Salomon0ea33072020-07-14 10:43:42 -0400474 bool restrictToSubset = SkCanvas::kStrict_SrcRectConstraint == constraint;
halcanary9d524f22016-03-29 09:03:52 -0700475
bsalomonc55271f2015-11-09 11:55:57 -0800476 // If we have to outset for AA then we will generate texture coords outside the src rect. The
477 // same happens for any mask filter that extends the bounds rendered in the dst.
478 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500479 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800480
Brian Salomona3b02f52020-07-15 16:02:01 -0400481 // Check for optimization to drop the src rect constraint when using linear filtering.
Mike Reed52130b02020-12-28 15:33:13 -0500482 if (!GrValidCubicResampler(cubic) &&
483 sampler.filter() == GrSamplerState::Filter::kLinear && restrictToSubset &&
Brian Salomone69b9ef2020-07-22 11:18:06 -0400484 sampler.mipmapped() == GrMipmapped::kNo && coordsAllInsideSrcRect &&
485 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800486 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500487 combinedMatrix.setConcat(ctm, srcToDst);
Brian Salomona3b02f52020-07-15 16:02:01 -0400488 if (can_ignore_linear_filtering_subset(*producer, src, combinedMatrix, rtc->numSamples())) {
Brian Salomon0ea33072020-07-14 10:43:42 -0400489 restrictToSubset = false;
bsalomonb1b01992015-11-18 10:56:08 -0800490 }
491 }
492
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500493 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800494 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500495 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800496 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500497 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800498 return;
499 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800500 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400501 const SkRect* subset = restrictToSubset ? &src : nullptr;
502 const SkRect* domain = coordsAllInsideSrcRect ? &src : nullptr;
503 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed52130b02020-12-28 15:33:13 -0500504 if (GrValidCubicResampler(cubic)) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400505 fp = producer->createBicubicFragmentProcessor(textureMatrix, subset, domain,
Mike Reed3867c702020-09-01 13:28:10 -0400506 sampler.wrapModeX(), sampler.wrapModeY(),
Mike Reed52130b02020-12-28 15:33:13 -0500507 cubic);
Brian Salomon0ea33072020-07-14 10:43:42 -0400508 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400509 fp = producer->createFragmentProcessor(textureMatrix, subset, domain, sampler);
Brian Salomon0ea33072020-07-14 10:43:42 -0400510 }
bsalomonc55271f2015-11-09 11:55:57 -0800511 if (!fp) {
512 return;
513 }
Brian Osman958a3bb2020-07-30 14:13:23 -0400514 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
515 rtc->colorInfo().colorSpace(), kPremul_SkAlphaType);
Brian Salomonb43d6992021-01-05 14:37:40 -0500516 if (producer->isAlphaOnly()) {
517 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kDstIn);
518 } else {
519 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kSrcIn);
520 }
joshualitt33a5fce2015-11-18 13:28:51 -0800521
bsalomonc55271f2015-11-09 11:55:57 -0800522 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400523 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
524 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800525 return;
526 }
527
528 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500529 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
530 if (dstClip) {
531 SkPoint srcClipPoints[4];
532 SkPoint* srcClip = nullptr;
533 if (canUseTextureCoordsAsLocalCoords) {
534 // Calculate texture coordinates that match the dst clip
535 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
536 srcClip = srcClipPoints;
537 }
538 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
539 } else {
540 // Provide explicit texture coords when possible, otherwise rely on texture matrix
541 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
542 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
543 }
544 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000545 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
546 // information since it always draws with AA, but that should not be noticeable since the
547 // mask filter is probably a blur.
548 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500549 if (dstClip) {
550 // Represent it as an SkPath formed from the dstClip
551 SkPath path;
552 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000553 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500554 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000555 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500556 }
557
558 GrBlurUtils::drawShapeWithMaskFilter(
559 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
560 }
561}
562
Robert Phillips16bf7d32020-07-07 10:20:27 -0400563void draw_tiled_bitmap(GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -0500564 GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400565 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400566 const SkBitmap& bitmap,
567 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400568 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400569 const SkMatrix& srcToDst,
570 const SkRect& srcRect,
571 const SkIRect& clippedSrcIRect,
572 const SkPaint& paint,
573 GrAA aa,
574 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400575 GrSamplerState sampler,
Mike Reed52130b02020-12-28 15:33:13 -0500576 SkCubicResampler cubic) {
577 const bool doBicubic = GrValidCubicResampler(cubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400578 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
579
580 int nx = bitmap.width() / tileSize;
581 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400582
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400583 for (int x = 0; x <= nx; x++) {
584 for (int y = 0; y <= ny; y++) {
585 SkRect tileR;
586 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
587 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
588
589 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
590 continue;
591 }
592
593 if (!tileR.intersect(srcRect)) {
594 continue;
595 }
596
597 SkIRect iTileR;
598 tileR.roundOut(&iTileR);
599 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
600 SkIntToScalar(iTileR.fTop));
601 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400602 srcToDst.mapRect(&rectToDraw);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400603 if (sampler.filter() != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400604 SkIRect iClampRect;
605
606 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
607 // In bleed mode we want to always expand the tile on all edges
608 // but stay within the bitmap bounds
609 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
610 } else {
611 // In texture-domain/clamp mode we only want to expand the
612 // tile on edges interior to "srcRect" (i.e., we want to
613 // not bleed across the original clamped edges)
614 srcRect.roundOut(&iClampRect);
615 }
Michael Ludwig47237242020-03-10 16:16:17 -0400616 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400617 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
618 }
619
620 SkBitmap tmpB;
621 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400622 // We should have already handled bitmaps larger than the max texture size.
623 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
624 tmpB.height() <= context->priv().caps()->maxTextureSize());
625 // We should be respecting the max tile size by the time we get here.
626 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
627 tmpB.height() <= context->priv().caps()->maxTileSize());
628
Brian Salomonbc074a62020-03-18 10:06:13 -0400629 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400630
631 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
632 if (aa == GrAA::kYes) {
633 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
634 if (tileR.fLeft <= srcRect.fLeft) {
635 aaFlags |= GrQuadAAFlags::kLeft;
636 }
637 if (tileR.fRight >= srcRect.fRight) {
638 aaFlags |= GrQuadAAFlags::kRight;
639 }
640 if (tileR.fTop <= srcRect.fTop) {
641 aaFlags |= GrQuadAAFlags::kTop;
642 }
643 if (tileR.fBottom >= srcRect.fBottom) {
644 aaFlags |= GrQuadAAFlags::kBottom;
645 }
646 }
647
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400648 // now offset it to make it "local" to our tmp bitmap
649 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400650 SkMatrix offsetSrcToDst = srcToDst;
651 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
Brian Osman449b1152020-04-15 16:43:00 -0400652 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
653 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
Mike Reed52130b02020-12-28 15:33:13 -0500654 constraint, sampler, cubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400655 }
656 }
657 }
658}
659
Michael Ludwig47237242020-03-10 16:16:17 -0400660} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400661
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500662//////////////////////////////////////////////////////////////////////////////
663
Mike Reedd8ff3132021-01-02 12:57:04 -0500664static SkFilterMode downgrade_to_filter(const SkSamplingOptions& sampling) {
665 SkFilterMode filter = sampling.filter;
666 if (sampling.useCubic || sampling.mipmap != SkMipmapMode::kNone) {
667 // if we were "fancier" than just bilerp, only do bilerp
668 filter = SkFilterMode::kLinear;
669 }
670 return filter;
671}
672
Michael Ludwig278263d2020-10-12 19:49:24 +0000673void SkGpuDevice::drawSpecial(SkSpecialImage* special, const SkMatrix& localToDevice,
Mike Reedd8ff3132021-01-02 12:57:04 -0500674 const SkSamplingOptions& sampling, const SkPaint& paint) {
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400675 SkASSERT(!paint.getMaskFilter() && !paint.getImageFilter());
676 SkASSERT(special->isTextureBacked());
677
678 SkRect src = SkRect::Make(special->subset());
Michael Ludwig278263d2020-10-12 19:49:24 +0000679 SkRect dst = SkRect::MakeWH(special->width(), special->height());
Mike Reed2ac6ce82021-01-15 12:26:22 -0500680 SkMatrix srcToDst = SkMatrix::RectToRect(src, dst);
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400681
Mike Reedd8ff3132021-01-02 12:57:04 -0500682 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, downgrade_to_filter(sampling));
Michael Ludwig278263d2020-10-12 19:49:24 +0000683 GrAA aa = paint.isAntiAlias() ? GrAA::kYes : GrAA::kNo;
684 GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400685
686 GrColorInfo colorInfo(SkColorTypeToGrColorType(special->colorType()),
687 special->alphaType(), sk_ref_sp(special->getColorSpace()));
688
689 GrSurfaceProxyView view = special->view(this->recordingContext());
690 GrTextureAdjuster texture(fContext.get(), std::move(view), colorInfo, special->uniqueID());
691 // In most cases this ought to hit draw_texture since there won't be a color filter,
692 // alpha-only texture+shader, or a high filter quality.
Michael Ludwig278263d2020-10-12 19:49:24 +0000693 SkOverrideDeviceMatrixProvider matrixProvider(this->asMatrixProvider(), localToDevice);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500694 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), this->clip(), matrixProvider,
695 paint, &texture, src, dst, nullptr, srcToDst, aa, aaFlags,
Mike Reed52130b02020-12-28 15:33:13 -0500696 SkCanvas::kStrict_SrcRectConstraint, sampler, kInvalidCubicResampler);
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400697}
698
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500699void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
700 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Mike Reed4f23dec2020-12-30 14:22:42 +0000701 const SkMatrix* preViewMatrix, const SkSamplingOptions& sampling,
702 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500703 SkRect src;
704 SkRect dst;
705 SkMatrix srcToDst;
706 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
707 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
708 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800709 return;
710 }
711
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500712 if (src.contains(image->bounds())) {
713 constraint = SkCanvas::kFast_SrcRectConstraint;
714 }
715 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500716 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
717 ? GrSamplerState::WrapMode::kClampToBorder
718 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400719
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500720 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400721 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
722 preViewMatrix ? *preViewMatrix : SkMatrix::I());
723 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500724
Brian Salomone69b9ef2020-07-22 11:18:06 -0400725 bool sharpenMM = fContext->priv().options().fSharpenMipmappedTextures;
Mike Reed52130b02020-12-28 15:33:13 -0500726 auto [fm, mm, cubic] = GrInterpretSamplingOptions(image->dimensions(), sampling,
John Stilesf2f9b0d2020-08-25 13:33:45 -0400727 ctm, srcToDst, sharpenMM,
728 /*allowFilterQualityReduction=*/true);
Michael Ludwig47237242020-03-10 16:16:17 -0400729
730 auto clip = this->clip();
731
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500732 // YUVA images can be stored in multiple images with different plane resolutions, so this
733 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
734 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
735 if (as_IB(image)->isYUVA()) {
Brian Salomon777e1462020-02-28 21:10:31 -0500736 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500737 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400738 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Mike Reed52130b02020-12-28 15:33:13 -0500739 {wrapMode, fm, mm}, cubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500740 return;
741 }
742
743 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
744 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
745 uint32_t pinnedUniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400746 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->recordingContext(),
747 &pinnedUniqueID)) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500748 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500749 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500750 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
751 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
752 image->imageInfo().refColorSpace());
753 } else {
754 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
755 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500756
Brian Salomon777e1462020-02-28 21:10:31 -0500757 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500758 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400759 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
Mike Reed52130b02020-12-28 15:33:13 -0500760 constraint, {wrapMode, fm, mm}, cubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500761 return;
762 }
763
Michael Ludwig47237242020-03-10 16:16:17 -0400764 // Next up, determine if the image must be tiled
765 {
766 // If image is explicitly already texture backed then we shouldn't get here.
767 SkASSERT(!image->isTextureBacked());
768
769 int tileFilterPad;
Mike Reed52130b02020-12-28 15:33:13 -0500770 if (GrValidCubicResampler(cubic)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400771 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
772 } else if (GrSamplerState::Filter::kNearest == fm) {
773 tileFilterPad = 0;
774 } else {
775 tileFilterPad = 1;
776 }
777 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
778 int tileSize;
779 SkIRect clippedSubset;
Brian Salomon8f7d9532020-12-23 09:16:59 -0500780 if (should_tile_image_id(fContext.get(), fSurfaceDrawContext->dimensions(), clip,
781 image->unique(), image->dimensions(), ctm, srcToDst, &src,
Michael Ludwig47237242020-03-10 16:16:17 -0400782 maxTileSize, &tileSize, &clippedSubset)) {
783 // Extract pixels on the CPU, since we have to split into separate textures before
784 // sending to the GPU.
785 SkBitmap bm;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400786 if (as_IB(image)->getROPixels(nullptr, &bm)) {
Greg Daniel5c53ae52020-10-28 13:22:30 -0400787 // This is the funnel for all paths that draw tiled bitmaps/images.
Brian Salomon8f7d9532020-12-23 09:16:59 -0500788 draw_tiled_bitmap(fContext.get(), fSurfaceDrawContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400789 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
Mike Reed52130b02020-12-28 15:33:13 -0500790 constraint, {wrapMode, fm, mm}, cubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400791 return;
792 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500793 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500794 }
795
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500796 // Lazily generated images must get drawn as a texture producer that handles the final
797 // texture creation.
798 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400799 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500800 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400801 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Mike Reed52130b02020-12-28 15:33:13 -0500802 {wrapMode, fm, mm}, cubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500803 return;
804 }
Michael Ludwig47237242020-03-10 16:16:17 -0400805
806 SkBitmap bm;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400807 if (as_IB(image)->getROPixels(nullptr, &bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400808 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500809 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400810 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Mike Reed52130b02020-12-28 15:33:13 -0500811 {wrapMode, fm, mm}, cubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500812 }
813
814 // Otherwise don't know how to draw it
815}
816
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400817void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
818 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
Mike Reed4f509342021-01-05 12:03:47 -0500819 const SkSamplingOptions& sampling, const SkPaint& paint,
820 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500821 SkASSERT(count > 0);
Mike Reed4f23dec2020-12-30 14:22:42 +0000822 if (!can_use_draw_texture(paint, sampling.useCubic, sampling.mipmap)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500823 // Send every entry through drawImageQuad() to handle the more complicated paint
824 int dstClipIndex = 0;
825 for (int i = 0; i < count; ++i) {
826 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400827 SkASSERT(!set[i].fHasClip || dstClips);
828 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500829
Brian Salomondb151e02019-09-17 12:11:16 -0400830 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
831 if (set[i].fAlpha != 1.f) {
832 auto paintAlpha = paint.getAlphaf();
833 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
834 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500835 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400836 this->drawImageQuad(
837 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
838 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
839 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400840 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Mike Reed4f23dec2020-12-30 14:22:42 +0000841 sampling, *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400842 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500843 }
844 return;
845 }
846
Mike Reed4f23dec2020-12-30 14:22:42 +0000847 GrSamplerState::Filter filter = sampling.filter == SkFilterMode::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -0400848 ? GrSamplerState::Filter::kNearest
849 : GrSamplerState::Filter::kLinear;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500850 SkBlendMode mode = paint.getBlendMode();
851
Brian Salomoneebe7352020-12-09 16:37:04 -0500852 SkAutoTArray<GrSurfaceDrawContext::TextureSetEntry> textures(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500853 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500854 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
855 // switches that occur within the 'n' entries.
856 int base = 0, n = 0, p = 0;
857 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500858 if (n > 0) {
Brian Salomon7f296c42021-01-13 14:56:55 +0000859 auto textureXform = GrColorSpaceXform::Make(
860 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
861 fSurfaceDrawContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500862 fSurfaceDrawContext->drawTextureSet(this->clip(),
863 textures.get() + base,
864 n,
865 p,
866 filter,
867 GrSamplerState::MipmapMode::kNone,
868 mode,
869 GrAA::kYes,
870 constraint,
871 this->localToDevice(),
872 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500873 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500874 base = nextBase;
875 n = 0;
876 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500877 };
878 int dstClipIndex = 0;
879 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400880 SkASSERT(!set[i].fHasClip || dstClips);
881 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
882
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500883 // Manage the dst clip pointer tracking before any continues are used so we don't lose
884 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400885 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
886 dstClipIndex += 4 * set[i].fHasClip;
887
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500888 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
889 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
890 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500891 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500892 continue;
893 }
894
Greg Danielcc21d0c2020-02-05 16:58:40 -0500895 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400896 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500897 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400898 // drawImageQuad and the proper effect to dynamically sample their planes.
899 if (!image->isYUVA()) {
900 uint32_t uniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400901 view = image->refPinnedView(this->recordingContext(), &uniqueID);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500902 if (!view) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400903 view = image->refView(this->recordingContext(), GrMipmapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500904 }
Brian Salomonb43d6992021-01-05 14:37:40 -0500905 if (image->isAlphaOnly()) {
906 GrSwizzle swizzle = GrSwizzle::Concat(view.swizzle(), GrSwizzle("aaaa"));
907 view = {view.detachProxy(), view.origin(), swizzle};
908 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500909 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500910
Greg Danielcc21d0c2020-02-05 16:58:40 -0500911 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400912 // This image can't go through the texture op, send through general image pipeline
913 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500914 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400915 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
916 if (set[i].fAlpha != 1.f) {
917 auto paintAlpha = paint.getAlphaf();
918 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
919 }
920 this->drawImageQuad(
921 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400922 SkToGrQuadAAFlags(set[i].fAAFlags),
923 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Mike Reed4f23dec2020-12-30 14:22:42 +0000924 sampling, *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400925 continue;
926 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500927
Greg Danielcc21d0c2020-02-05 16:58:40 -0500928 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500929 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500930 textures[i].fSrcRect = set[i].fSrcRect;
931 textures[i].fDstRect = set[i].fDstRect;
932 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400933 textures[i].fPreViewMatrix =
934 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400935 textures[i].fColor = texture_color(paint.getColor4f(), set[i].fAlpha,
936 SkColorTypeToGrColorType(image->colorType()),
Brian Salomon8f7d9532020-12-23 09:16:59 -0500937 fSurfaceDrawContext->colorInfo());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500938 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
939
940 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400941 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500942 textures[i].fProxyView.proxy(),
943 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500944 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500945 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
946 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500947 draw(i);
948 }
949 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
950 // set being accumulated so increment n, and increment p if proxies are different.
951 ++n;
952 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
953 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
954 // to i - 1).
955 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500956 }
957 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500958 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500959}