blob: 6760b521a90bb6db15e35fe6a1df5891249fa9fa [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"
Brian Salomonb8f098d2020-01-07 11:15:44 -050027#include "src/gpu/effects/GrTextureEffect.h"
Brian Osmanf48f76e2020-07-15 16:04:17 -040028#include "src/gpu/effects/GrXfermodeFragmentProcessor.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 }
Brian Osmanf48f76e2020-07-15 16:04:17 -0400504 if (fp) {
505 fp = GrXfermodeFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
506 }
John Stiles3e627622020-06-29 12:47:04 -0400507 fp = GrColorSpaceXformEffect::Make(std::move(fp),
508 producer->colorSpace(), producer->alphaType(),
509 rtc->colorInfo().colorSpace(), kPremul_SkAlphaType);
bsalomonc55271f2015-11-09 11:55:57 -0800510 if (!fp) {
511 return;
512 }
joshualitt33a5fce2015-11-18 13:28:51 -0800513
bsalomonc55271f2015-11-09 11:55:57 -0800514 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400515 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
516 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800517 return;
518 }
519
520 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500521 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
522 if (dstClip) {
523 SkPoint srcClipPoints[4];
524 SkPoint* srcClip = nullptr;
525 if (canUseTextureCoordsAsLocalCoords) {
526 // Calculate texture coordinates that match the dst clip
527 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
528 srcClip = srcClipPoints;
529 }
530 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
531 } else {
532 // Provide explicit texture coords when possible, otherwise rely on texture matrix
533 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
534 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
535 }
536 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000537 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
538 // information since it always draws with AA, but that should not be noticeable since the
539 // mask filter is probably a blur.
540 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500541 if (dstClip) {
542 // Represent it as an SkPath formed from the dstClip
543 SkPath path;
544 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000545 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500546 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000547 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500548 }
549
550 GrBlurUtils::drawShapeWithMaskFilter(
551 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
552 }
553}
554
Robert Phillips16bf7d32020-07-07 10:20:27 -0400555void draw_tiled_bitmap(GrRecordingContext* context,
Michael Ludwig47237242020-03-10 16:16:17 -0400556 GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400557 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400558 const SkBitmap& bitmap,
559 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400560 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400561 const SkMatrix& srcToDst,
562 const SkRect& srcRect,
563 const SkIRect& clippedSrcIRect,
564 const SkPaint& paint,
565 GrAA aa,
566 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400567 GrSamplerState sampler,
Michael Ludwig47237242020-03-10 16:16:17 -0400568 bool doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400569 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
570
571 int nx = bitmap.width() / tileSize;
572 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400573
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400574 for (int x = 0; x <= nx; x++) {
575 for (int y = 0; y <= ny; y++) {
576 SkRect tileR;
577 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
578 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
579
580 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
581 continue;
582 }
583
584 if (!tileR.intersect(srcRect)) {
585 continue;
586 }
587
588 SkIRect iTileR;
589 tileR.roundOut(&iTileR);
590 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
591 SkIntToScalar(iTileR.fTop));
592 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400593 srcToDst.mapRect(&rectToDraw);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400594 if (sampler.filter() != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400595 SkIRect iClampRect;
596
597 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
598 // In bleed mode we want to always expand the tile on all edges
599 // but stay within the bitmap bounds
600 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
601 } else {
602 // In texture-domain/clamp mode we only want to expand the
603 // tile on edges interior to "srcRect" (i.e., we want to
604 // not bleed across the original clamped edges)
605 srcRect.roundOut(&iClampRect);
606 }
Michael Ludwig47237242020-03-10 16:16:17 -0400607 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400608 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
609 }
610
611 SkBitmap tmpB;
612 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400613 // We should have already handled bitmaps larger than the max texture size.
614 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
615 tmpB.height() <= context->priv().caps()->maxTextureSize());
616 // We should be respecting the max tile size by the time we get here.
617 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
618 tmpB.height() <= context->priv().caps()->maxTileSize());
619
Brian Salomonbc074a62020-03-18 10:06:13 -0400620 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400621
622 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
623 if (aa == GrAA::kYes) {
624 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
625 if (tileR.fLeft <= srcRect.fLeft) {
626 aaFlags |= GrQuadAAFlags::kLeft;
627 }
628 if (tileR.fRight >= srcRect.fRight) {
629 aaFlags |= GrQuadAAFlags::kRight;
630 }
631 if (tileR.fTop <= srcRect.fTop) {
632 aaFlags |= GrQuadAAFlags::kTop;
633 }
634 if (tileR.fBottom >= srcRect.fBottom) {
635 aaFlags |= GrQuadAAFlags::kBottom;
636 }
637 }
638
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400639 // now offset it to make it "local" to our tmp bitmap
640 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400641 SkMatrix offsetSrcToDst = srcToDst;
642 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
Brian Osman449b1152020-04-15 16:43:00 -0400643 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
644 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400645 constraint, sampler, doBicubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400646 }
647 }
648 }
649}
650
Michael Ludwig47237242020-03-10 16:16:17 -0400651} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400652
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500653//////////////////////////////////////////////////////////////////////////////
654
655void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
656 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500657 const SkMatrix* preViewMatrix, const SkPaint& paint,
658 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500659 SkRect src;
660 SkRect dst;
661 SkMatrix srcToDst;
662 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
663 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
664 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800665 return;
666 }
667
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500668 if (src.contains(image->bounds())) {
669 constraint = SkCanvas::kFast_SrcRectConstraint;
670 }
671 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500672 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
673 ? GrSamplerState::WrapMode::kClampToBorder
674 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400675
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500676 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400677 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
678 preViewMatrix ? *preViewMatrix : SkMatrix::I());
679 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500680
Brian Salomone69b9ef2020-07-22 11:18:06 -0400681 bool sharpenMM = fContext->priv().options().fSharpenMipmappedTextures;
682 auto [fm, mm, bicubic] = GrInterpretFilterQuality(image->dimensions(), paint.getFilterQuality(),
683 ctm, srcToDst, sharpenMM);
Michael Ludwig47237242020-03-10 16:16:17 -0400684
685 auto clip = this->clip();
686
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500687 // YUVA images can be stored in multiple images with different plane resolutions, so this
688 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
689 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
690 if (as_IB(image)->isYUVA()) {
691 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500692 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500693
Brian Salomon777e1462020-02-28 21:10:31 -0500694 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Osman449b1152020-04-15 16:43:00 -0400695 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
696 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400697 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500698 return;
699 }
700
701 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
702 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
703 uint32_t pinnedUniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400704 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->recordingContext(),
705 &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500706 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500707 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500708
Greg Daniel82c6b102020-01-21 10:33:22 -0500709 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500710 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500711 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
712 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
713 image->imageInfo().refColorSpace());
714 } else {
715 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
716 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500717
Brian Salomon777e1462020-02-28 21:10:31 -0500718 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Osman449b1152020-04-15 16:43:00 -0400719 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
720 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400721 constraint, {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500722 return;
723 }
724
Michael Ludwig47237242020-03-10 16:16:17 -0400725 // Next up, determine if the image must be tiled
726 {
727 // If image is explicitly already texture backed then we shouldn't get here.
728 SkASSERT(!image->isTextureBacked());
729
730 int tileFilterPad;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400731 if (bicubic) {
Michael Ludwig47237242020-03-10 16:16:17 -0400732 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
733 } else if (GrSamplerState::Filter::kNearest == fm) {
734 tileFilterPad = 0;
735 } else {
736 tileFilterPad = 1;
737 }
738 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
739 int tileSize;
740 SkIRect clippedSubset;
741 if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(),
742 fRenderTargetContext->height()),
743 clip, image->unique(), image->dimensions(), ctm, srcToDst, &src,
744 maxTileSize, &tileSize, &clippedSubset)) {
745 // Extract pixels on the CPU, since we have to split into separate textures before
746 // sending to the GPU.
747 SkBitmap bm;
748 if (as_IB(image)->getROPixels(&bm)) {
749 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram
750 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
751 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
752 draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400753 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400754 constraint, {wrapMode, fm, mm}, bicubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400755 return;
756 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500757 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500758 }
759
760 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
761 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500762 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500763
764 // Lazily generated images must get drawn as a texture producer that handles the final
765 // texture creation.
766 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400767 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400768 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
769 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400770 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500771 return;
772 }
Michael Ludwig47237242020-03-10 16:16:17 -0400773
774 SkBitmap bm;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500775 if (as_IB(image)->getROPixels(&bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400776 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Osman449b1152020-04-15 16:43:00 -0400777 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, matrixProvider,
778 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400779 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500780 }
781
782 // Otherwise don't know how to draw it
783}
784
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400785void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
786 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
787 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500788 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400789 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500790 // Send every entry through drawImageQuad() to handle the more complicated paint
791 int dstClipIndex = 0;
792 for (int i = 0; i < count; ++i) {
793 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400794 SkASSERT(!set[i].fHasClip || dstClips);
795 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500796
Brian Salomondb151e02019-09-17 12:11:16 -0400797 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
798 if (set[i].fAlpha != 1.f) {
799 auto paintAlpha = paint.getAlphaf();
800 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
801 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500802 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400803 this->drawImageQuad(
804 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
805 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
806 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400807 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400808 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400809 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500810 }
811 return;
812 }
813
Brian Salomona3b02f52020-07-15 16:02:01 -0400814 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality()
815 ? GrSamplerState::Filter::kNearest
816 : GrSamplerState::Filter::kLinear;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500817 SkBlendMode mode = paint.getBlendMode();
818
819 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
820 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500821 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
822 // switches that occur within the 'n' entries.
823 int base = 0, n = 0, p = 0;
824 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500825 if (n > 0) {
826 auto textureXform = GrColorSpaceXform::Make(
827 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400828 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400829 fRenderTargetContext->drawTextureSet(this->clip(),
830 textures.get() + base,
831 n,
832 p,
833 filter,
834 GrSamplerState::MipmapMode::kNone,
835 mode,
836 GrAA::kYes,
837 constraint,
838 this->localToDevice(),
839 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500840 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500841 base = nextBase;
842 n = 0;
843 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500844 };
845 int dstClipIndex = 0;
846 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400847 SkASSERT(!set[i].fHasClip || dstClips);
848 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
849
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500850 // Manage the dst clip pointer tracking before any continues are used so we don't lose
851 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400852 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
853 dstClipIndex += 4 * set[i].fHasClip;
854
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500855 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
856 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
857 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500858 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500859 continue;
860 }
861
Greg Danielcc21d0c2020-02-05 16:58:40 -0500862 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400863 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500864 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400865 // drawImageQuad and the proper effect to dynamically sample their planes.
866 if (!image->isYUVA()) {
867 uint32_t uniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400868 view = image->refPinnedView(this->recordingContext(), &uniqueID);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500869 if (!view) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400870 view = image->refView(this->recordingContext(), GrMipmapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500871 }
872 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500873
Greg Danielcc21d0c2020-02-05 16:58:40 -0500874 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400875 // This image can't go through the texture op, send through general image pipeline
876 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500877 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400878 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
879 if (set[i].fAlpha != 1.f) {
880 auto paintAlpha = paint.getAlphaf();
881 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
882 }
883 this->drawImageQuad(
884 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400885 SkToGrQuadAAFlags(set[i].fAAFlags),
886 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400887 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400888 continue;
889 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500890
Greg Danielcc21d0c2020-02-05 16:58:40 -0500891 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500892 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500893 textures[i].fSrcRect = set[i].fSrcRect;
894 textures[i].fDstRect = set[i].fDstRect;
895 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400896 textures[i].fPreViewMatrix =
897 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400898 textures[i].fColor = texture_color(paint.getColor4f(), set[i].fAlpha,
899 SkColorTypeToGrColorType(image->colorType()),
900 fRenderTargetContext->colorInfo());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500901 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
902
903 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400904 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500905 textures[i].fProxyView.proxy(),
906 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500907 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500908 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
909 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500910 draw(i);
911 }
912 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
913 // set being accumulated so increment n, and increment p if proxies are different.
914 ++n;
915 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
916 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
917 // to i - 1).
918 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500919 }
920 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500921 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500922}