blob: 9a547c0d953a7fe6ae53ca7b8dc53cdcbf29345d [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 Klein8aa0edf2020-10-16 11:04:18 -050013#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkDraw.h"
15#include "src/core/SkMaskFilterBase.h"
Michael Ludwig16d5b0a2020-08-31 13:07:16 -040016#include "src/core/SkSpecialImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrBitmapTextureMaker.h"
18#include "src/gpu/GrBlurUtils.h"
19#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrColorSpaceXform.h"
21#include "src/gpu/GrImageTextureMaker.h"
Robert Phillips16bf7d32020-07-07 10:20:27 -040022#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050024#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrTextureAdjuster.h"
26#include "src/gpu/GrTextureMaker.h"
27#include "src/gpu/SkGr.h"
28#include "src/gpu/effects/GrBicubicEffect.h"
John Stilesf743d4e2020-07-23 11:35:08 -040029#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050030#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000031#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080033
Michael Ludwig1433cfd2019-02-27 17:12:30 -050034namespace {
35
bsalomonc55271f2015-11-09 11:55:57 -080036static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
37 return textureIsAlphaOnly && paint.getShader();
38}
39
bsalomonb1b01992015-11-18 10:56:08 -080040//////////////////////////////////////////////////////////////////////////////
Brian Salomona3b02f52020-07-15 16:02:01 -040041// Helper functions for dropping src rect subset with GrSamplerState::Filter::kLinear.
bsalomonb1b01992015-11-18 10:56:08 -080042
43static const SkScalar kColorBleedTolerance = 0.001f;
44
45static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
46 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050047 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
48 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
49 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080050 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
51 return true;
52 }
53 return false;
54}
55
56static bool may_color_bleed(const SkRect& srcRect,
57 const SkRect& transformedRect,
58 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060059 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080060 // Only gets called if has_aligned_samples returned false.
61 // So we can assume that sampling is axis aligned but not texel aligned.
62 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
63 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Chris Dalton6ce447a2019-06-23 18:07:38 -060064 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080065 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
66 } else {
67 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
68 }
69 m.mapRect(&innerTransformedRect, innerSrcRect);
70
71 // The gap between outerTransformedRect and innerTransformedRect
72 // represents the projection of the source border area, which is
73 // problematic for color bleeding. We must check whether any
74 // destination pixels sample the border area.
75 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
76 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
77 SkIRect outer, inner;
78 outerTransformedRect.round(&outer);
79 innerTransformedRect.round(&inner);
80 // If the inner and outer rects round to the same result, it means the
81 // border does not overlap any pixel centers. Yay!
82 return inner != outer;
83}
84
Brian Salomona3b02f52020-07-15 16:02:01 -040085static bool can_ignore_linear_filtering_subset(const GrTextureProducer& producer,
86 const SkRect& srcSubset,
87 const SkMatrix& srcRectToDeviceSpace,
88 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080089 if (srcRectToDeviceSpace.rectStaysRect()) {
90 // sampling is axis-aligned
91 SkRect transformedRect;
Brian Salomon8f32f132020-07-14 12:30:12 -040092 srcRectToDeviceSpace.mapRect(&transformedRect, srcSubset);
bsalomonb1b01992015-11-18 10:56:08 -080093
Brian Salomon8f32f132020-07-14 12:30:12 -040094 if (has_aligned_samples(srcSubset, transformedRect) ||
95 !may_color_bleed(srcSubset, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080096 return true;
97 }
98 }
99 return false;
100}
101
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400102//////////////////////////////////////////////////////////////////////////////
103// Helper functions for tiling a large SkBitmap
104
105static const int kBmpSmallTileSize = 1 << 10;
106
107static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
108 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
109 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
110 return tilesX * tilesY;
111}
112
113static int determine_tile_size(const SkIRect& src, int maxTileSize) {
114 if (maxTileSize <= kBmpSmallTileSize) {
115 return maxTileSize;
116 }
117
118 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
119 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
120
121 maxTileTotalTileSize *= maxTileSize * maxTileSize;
122 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
123
124 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
125 return kBmpSmallTileSize;
126 } else {
127 return maxTileSize;
128 }
129}
130
131// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
132// pixels from the bitmap are necessary.
Michael Ludwigc002d562020-05-13 14:17:57 -0400133static SkIRect determine_clipped_src_rect(int width, int height,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400134 const GrClip* clip,
Michael Ludwigc002d562020-05-13 14:17:57 -0400135 const SkMatrix& viewMatrix,
136 const SkMatrix& srcToDstRect,
137 const SkISize& imageDimensions,
138 const SkRect* srcRectPtr) {
Michael Ludwige06a8972020-06-11 10:29:00 -0400139 SkIRect clippedSrcIRect = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400140 : SkIRect::MakeWH(width, height);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400141 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
142 if (!inv.invert(&inv)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400143 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400144 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400145 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400146 inv.mapRect(&clippedSrcRect);
147 if (srcRectPtr) {
148 if (!clippedSrcRect.intersect(*srcRectPtr)) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400149 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400150 }
151 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400152 clippedSrcRect.roundOut(&clippedSrcIRect);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400153 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
Michael Ludwigc002d562020-05-13 14:17:57 -0400154 if (!clippedSrcIRect.intersect(bmpBounds)) {
155 return SkIRect::MakeEmpty();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400156 }
Michael Ludwigc002d562020-05-13 14:17:57 -0400157
158 return clippedSrcIRect;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400159}
160
Michael Ludwig47237242020-03-10 16:16:17 -0400161// tileSize and clippedSubset are valid if true is returned
Robert Phillips16bf7d32020-07-07 10:20:27 -0400162static bool should_tile_image_id(GrRecordingContext* context,
Michael Ludwig47237242020-03-10 16:16:17 -0400163 SkISize rtSize,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400164 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400165 uint32_t imageID,
166 const SkISize& imageSize,
167 const SkMatrix& ctm,
168 const SkMatrix& srcToDst,
169 const SkRect* src,
170 int maxTileSize,
171 int* tileSize,
172 SkIRect* clippedSubset) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400173 // if it's larger than the max tile size, then we have no choice but tiling.
Michael Ludwig47237242020-03-10 16:16:17 -0400174 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
Michael Ludwigc002d562020-05-13 14:17:57 -0400175 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
176 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400177 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
178 return true;
179 }
180
181 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
Michael Ludwig47237242020-03-10 16:16:17 -0400182 const size_t area = imageSize.width() * imageSize.height();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400183 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
184 return false;
185 }
186
Michael Ludwig46d91382020-05-07 09:51:12 -0400187 // At this point we know we could do the draw by uploading the entire bitmap as a texture.
188 // However, if the texture would be large compared to the cache size and we don't require most
189 // of it for this draw then tile to reduce the amount of upload and cache spill.
190 // NOTE: if the context is not a direct context, it doesn't have access to the resource cache,
191 // and theoretically, the resource cache's limits could be being changed on another thread, so
192 // even having access to just the limit wouldn't be a reliable test during recording here.
193 // Instead, we will just upload the entire image to be on the safe side and not tile.
Robert Phillips16bf7d32020-07-07 10:20:27 -0400194 auto direct = context->asDirectContext();
195 if (!direct) {
Michael Ludwig46d91382020-05-07 09:51:12 -0400196 return false;
197 }
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400198
199 // assumption here is that sw bitmap size is a good proxy for its size as
200 // a texture
201 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
Robert Phillips16bf7d32020-07-07 10:20:27 -0400202 size_t cacheSize = direct->getResourceCacheLimit();
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400203 if (bmpSize < cacheSize / 2) {
204 return false;
205 }
206
207 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
208 // tiling memory savings would be < 50%.
Michael Ludwigc002d562020-05-13 14:17:57 -0400209 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
210 srcToDst, imageSize, src);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400211 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
212 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
213 kBmpSmallTileSize * kBmpSmallTileSize *
214 sizeof(SkPMColor); // assume 32bit pixels;
215
216 return usedTileBytes * 2 < bmpSize;
217}
218
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400219// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
220// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
221// of 'iRect' for all possible outsets/clamps.
Michael Ludwig47237242020-03-10 16:16:17 -0400222static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400223 const SkIRect& clamp) {
224 iRect->outset(outset, outset);
225
226 int leftClampDelta = clamp.fLeft - iRect->fLeft;
227 if (leftClampDelta > 0) {
228 offset->fX -= outset - leftClampDelta;
229 iRect->fLeft = clamp.fLeft;
230 } else {
231 offset->fX -= outset;
232 }
233
234 int topClampDelta = clamp.fTop - iRect->fTop;
235 if (topClampDelta > 0) {
236 offset->fY -= outset - topClampDelta;
237 iRect->fTop = clamp.fTop;
238 } else {
239 offset->fY -= outset;
240 }
241
242 if (iRect->fRight > clamp.fRight) {
243 iRect->fRight = clamp.fRight;
244 }
245 if (iRect->fBottom > clamp.fBottom) {
246 iRect->fBottom = clamp.fBottom;
247 }
248}
249
250//////////////////////////////////////////////////////////////////////////////
Brian Salomoneebe7352020-12-09 16:37:04 -0500251// Helper functions for drawing an image with GrSurfaceDrawContext
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400252
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500253enum class ImageDrawMode {
254 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
255 kOptimized,
256 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
257 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
258 kDecal,
259 // Src or dst are empty, or do not intersect the image content so don't draw anything.
260 kSkip
261};
262
263/**
264 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
265 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
266 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
267 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
268 *
269 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
270 * original src rect. 'dstClip' should be null when there is no additional clipping.
271 */
272static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
273 const SkRect* origDstRect, const SkPoint dstClip[4],
274 SkRect* outSrcRect, SkRect* outDstRect,
275 SkMatrix* srcToDst) {
276 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
277
278 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
279 SkRect dst = origDstRect ? *origDstRect : src;
280
281 if (src.isEmpty() || dst.isEmpty()) {
282 return ImageDrawMode::kSkip;
283 }
284
285 if (outDstRect) {
286 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
287 } else {
288 srcToDst->setIdentity();
289 }
290
291 if (origSrcRect && !srcBounds.contains(src)) {
292 if (!src.intersect(srcBounds)) {
293 return ImageDrawMode::kSkip;
294 }
295 srcToDst->mapRect(&dst, src);
296
297 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
298 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
299 if (dstClip) {
300 for (int i = 0; i < 4; ++i) {
301 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
302 // Must resort to using a decal mode restricted to the clipped 'src', and
303 // use the original dst rect (filling in src bounds as needed)
304 *outSrcRect = src;
305 *outDstRect = (origDstRect ? *origDstRect
306 : (origSrcRect ? *origSrcRect : srcBounds));
307 return ImageDrawMode::kDecal;
308 }
309 }
310 }
311 }
312
313 // The original src and dst were fully contained in the image, or there was no dst clip to
314 // worry about, or the clip was still contained in the restricted dst rect.
315 *outSrcRect = src;
316 *outDstRect = dst;
317 return ImageDrawMode::kOptimized;
318}
319
Brian Salomon34169692017-08-28 15:32:01 -0400320/**
Brian Salomoneebe7352020-12-09 16:37:04 -0500321 * Checks whether the paint is compatible with using GrSurfaceDrawContext::drawTexture. It is more
Brian Salomonb80ffee2018-05-23 16:39:39 -0400322 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400323 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400324static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400325 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500326 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400327}
328
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400329static SkPMColor4f texture_color(SkColor4f paintColor, float entryAlpha, GrColorType srcColorType,
330 const GrColorInfo& dstColorInfo) {
331 paintColor.fA *= entryAlpha;
332 if (GrColorTypeIsAlphaOnly(srcColorType)) {
333 return SkColor4fPrepForDst(paintColor, dstColorInfo).premul();
334 } else {
335 float paintAlpha = SkTPin(paintColor.fA, 0.f, 1.f);
336 return { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
337 }
338}
339
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500340// Assumes srcRect and dstRect have already been optimized to fit the proxy
Brian Salomoneebe7352020-12-09 16:37:04 -0500341static void draw_texture(GrSurfaceDrawContext* rtc,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400342 const GrClip* clip,
343 const SkMatrix& ctm,
344 const SkPaint& paint,
345 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 Salomon16d86132020-12-28 11:38:35 -0500353 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
354 view.concatSwizzle(GrSwizzle("aaaa"));
355 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400356 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400357 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400358 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500359 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400360 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400361 switch (paint.getFilterQuality()) {
362 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400363 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400364 break;
365 case kLow_SkFilterQuality:
Brian Salomona3b02f52020-07-15 16:02:01 -0400366 filter = GrSamplerState::Filter::kLinear;
Brian Salomon34169692017-08-28 15:32:01 -0400367 break;
368 case kMedium_SkFilterQuality:
369 case kHigh_SkFilterQuality:
370 SK_ABORT("Quality level not allowed.");
371 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500372 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500373 // Must specify the strict constraint when the proxy is not functionally exact and the src
374 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500375 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500376 // Conservative estimate of how much a coord could be outset from src rect:
Brian Salomona3b02f52020-07-15 16:02:01 -0400377 // 1/2 pixel for AA and 1/2 pixel for linear filtering
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500378 float buffer = 0.5f * (aa == GrAA::kYes) +
Brian Salomona3b02f52020-07-15 16:02:01 -0400379 0.5f * (filter == GrSamplerState::Filter::kLinear);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400380 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500381 safeBounds.inset(buffer, buffer);
382 if (!safeBounds.contains(srcRect)) {
383 constraint = SkCanvas::kStrict_SrcRectConstraint;
384 }
385 }
Brian Salomon34169692017-08-28 15:32:01 -0400386
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400387 SkPMColor4f color = texture_color(paint.getColor4f(), 1.f, srcColorInfo.colorType(), dstInfo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500388 if (dstClip) {
389 // Get source coords corresponding to dstClip
390 SkPoint srcQuad[4];
391 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000392
Brian Salomone69b9ef2020-07-22 11:18:06 -0400393 rtc->drawTextureQuad(clip,
394 std::move(view),
395 srcColorInfo.colorType(),
396 srcColorInfo.alphaType(),
397 filter,
398 GrSamplerState::MipmapMode::kNone,
399 paint.getBlendMode(),
400 color,
401 srcQuad,
402 dstClip,
403 aa,
404 aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500405 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400406 ctm,
407 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500408 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400409 rtc->drawTexture(clip,
410 std::move(view),
411 srcColorInfo.alphaType(),
412 filter,
413 GrSamplerState::MipmapMode::kNone,
414 paint.getBlendMode(),
415 color,
416 srcRect,
417 dstRect,
418 aa,
419 aaFlags,
420 constraint,
421 ctm,
422 std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000423 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000424}
425
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500426// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Robert Phillips16bf7d32020-07-07 10:20:27 -0400427static void draw_texture_producer(GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -0500428 GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400429 const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400430 const SkMatrixProvider& matrixProvider,
Brian Salomon777e1462020-02-28 21:10:31 -0500431 const SkPaint& paint,
432 GrTextureProducer* producer,
433 const SkRect& src,
434 const SkRect& dst,
435 const SkPoint dstClip[4],
436 const SkMatrix& srcToDst,
437 GrAA aa,
438 GrQuadAAFlags aaFlags,
439 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400440 GrSamplerState sampler,
Michael Ludwig47237242020-03-10 16:16:17 -0400441 bool doBicubic) {
Brian Osman449b1152020-04-15 16:43:00 -0400442 const SkMatrix& ctm(matrixProvider.localToDevice());
Brian Salomone69b9ef2020-07-22 11:18:06 -0400443 if (sampler.wrapModeX() == GrSamplerState::WrapMode::kClamp &&
444 sampler.wrapModeY() == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
Brian Salomon777e1462020-02-28 21:10:31 -0500445 can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400446 // We've done enough checks above to allow us to pass ClampNearest() and not check for
447 // scaling adjustments.
Brian Salomon7e67dca2020-07-21 09:27:25 -0400448 auto view = producer->view(GrMipmapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500449 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400450 return;
451 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500452
Brian Salomon16d86132020-12-28 11:38:35 -0500453 draw_texture(rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint,
454 std::move(view), producer->colorInfo());
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400455 return;
456 }
457
bsalomonc55271f2015-11-09 11:55:57 -0800458 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500459
bsalomonc55271f2015-11-09 11:55:57 -0800460 // The shader expects proper local coords, so we can't replace local coords with texture coords
461 // if the shader will be used. If we have a mask filter we will change the underlying geometry
462 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800463 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800464
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500465 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500466 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500467 // FP. In the future this should be an opaque optimization enabled by the combination of
468 // GrDrawOp/GP and FP.
469 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
470 mf = nullptr;
471 }
bsalomonc55271f2015-11-09 11:55:57 -0800472
Brian Salomon0ea33072020-07-14 10:43:42 -0400473 bool restrictToSubset = SkCanvas::kStrict_SrcRectConstraint == constraint;
halcanary9d524f22016-03-29 09:03:52 -0700474
bsalomonc55271f2015-11-09 11:55:57 -0800475 // If we have to outset for AA then we will generate texture coords outside the src rect. The
476 // same happens for any mask filter that extends the bounds rendered in the dst.
477 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500478 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800479
Brian Salomona3b02f52020-07-15 16:02:01 -0400480 // Check for optimization to drop the src rect constraint when using linear filtering.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400481 if (!doBicubic && sampler.filter() == GrSamplerState::Filter::kLinear && restrictToSubset &&
482 sampler.mipmapped() == GrMipmapped::kNo && coordsAllInsideSrcRect &&
483 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800484 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500485 combinedMatrix.setConcat(ctm, srcToDst);
Brian Salomona3b02f52020-07-15 16:02:01 -0400486 if (can_ignore_linear_filtering_subset(*producer, src, combinedMatrix, rtc->numSamples())) {
Brian Salomon0ea33072020-07-14 10:43:42 -0400487 restrictToSubset = false;
bsalomonb1b01992015-11-18 10:56:08 -0800488 }
489 }
490
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500491 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800492 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500493 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800494 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500495 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800496 return;
497 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800498 }
Brian Salomon0ea33072020-07-14 10:43:42 -0400499 const SkRect* subset = restrictToSubset ? &src : nullptr;
500 const SkRect* domain = coordsAllInsideSrcRect ? &src : nullptr;
501 std::unique_ptr<GrFragmentProcessor> fp;
502 if (doBicubic) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400503 fp = producer->createBicubicFragmentProcessor(textureMatrix, subset, domain,
Mike Reed3867c702020-09-01 13:28:10 -0400504 sampler.wrapModeX(), sampler.wrapModeY(),
505 GrBicubicEffect::gMitchell);
Brian Salomon0ea33072020-07-14 10:43:42 -0400506 } else {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400507 fp = producer->createFragmentProcessor(textureMatrix, subset, domain, sampler);
Brian Salomon0ea33072020-07-14 10:43:42 -0400508 }
bsalomonc55271f2015-11-09 11:55:57 -0800509 if (!fp) {
510 return;
511 }
Brian Osman958a3bb2020-07-30 14:13:23 -0400512 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
513 rtc->colorInfo().colorSpace(), kPremul_SkAlphaType);
Brian Salomon16d86132020-12-28 11:38:35 -0500514 if (producer->isAlphaOnly()) {
515 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kDstIn);
516 } else {
517 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kSrcIn);
518 }
joshualitt33a5fce2015-11-18 13:28:51 -0800519
bsalomonc55271f2015-11-09 11:55:57 -0800520 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400521 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, matrixProvider,
522 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800523 return;
524 }
525
526 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500527 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
528 if (dstClip) {
529 SkPoint srcClipPoints[4];
530 SkPoint* srcClip = nullptr;
531 if (canUseTextureCoordsAsLocalCoords) {
532 // Calculate texture coordinates that match the dst clip
533 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
534 srcClip = srcClipPoints;
535 }
536 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
537 } else {
538 // Provide explicit texture coords when possible, otherwise rely on texture matrix
539 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
540 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
541 }
542 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000543 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
544 // information since it always draws with AA, but that should not be noticeable since the
545 // mask filter is probably a blur.
546 GrStyledShape shape;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500547 if (dstClip) {
548 // Represent it as an SkPath formed from the dstClip
549 SkPath path;
550 path.addPoly(dstClip, 4, true);
Michael Ludwig2686d692020-04-17 20:21:37 +0000551 shape = GrStyledShape(path);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500552 } else {
Michael Ludwig2686d692020-04-17 20:21:37 +0000553 shape = GrStyledShape(dst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500554 }
555
556 GrBlurUtils::drawShapeWithMaskFilter(
557 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
558 }
559}
560
Robert Phillips16bf7d32020-07-07 10:20:27 -0400561void draw_tiled_bitmap(GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -0500562 GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400563 const GrClip* clip,
Michael Ludwig47237242020-03-10 16:16:17 -0400564 const SkBitmap& bitmap,
565 int tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400566 const SkMatrixProvider& matrixProvider,
Michael Ludwig47237242020-03-10 16:16:17 -0400567 const SkMatrix& srcToDst,
568 const SkRect& srcRect,
569 const SkIRect& clippedSrcIRect,
570 const SkPaint& paint,
571 GrAA aa,
572 SkCanvas::SrcRectConstraint constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400573 GrSamplerState sampler,
Michael Ludwig47237242020-03-10 16:16:17 -0400574 bool doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400575 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
576
577 int nx = bitmap.width() / tileSize;
578 int ny = bitmap.height() / tileSize;
Michael Ludwig47237242020-03-10 16:16:17 -0400579
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400580 for (int x = 0; x <= nx; x++) {
581 for (int y = 0; y <= ny; y++) {
582 SkRect tileR;
583 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
584 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
585
586 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
587 continue;
588 }
589
590 if (!tileR.intersect(srcRect)) {
591 continue;
592 }
593
594 SkIRect iTileR;
595 tileR.roundOut(&iTileR);
596 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
597 SkIntToScalar(iTileR.fTop));
598 SkRect rectToDraw = tileR;
Michael Ludwig47237242020-03-10 16:16:17 -0400599 srcToDst.mapRect(&rectToDraw);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400600 if (sampler.filter() != GrSamplerState::Filter::kNearest || doBicubic) {
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400601 SkIRect iClampRect;
602
603 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
604 // In bleed mode we want to always expand the tile on all edges
605 // but stay within the bitmap bounds
606 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
607 } else {
608 // In texture-domain/clamp mode we only want to expand the
609 // tile on edges interior to "srcRect" (i.e., we want to
610 // not bleed across the original clamped edges)
611 srcRect.roundOut(&iClampRect);
612 }
Michael Ludwig47237242020-03-10 16:16:17 -0400613 int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1;
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400614 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
615 }
616
617 SkBitmap tmpB;
618 if (bitmap.extractSubset(&tmpB, iTileR)) {
Michael Ludwig47237242020-03-10 16:16:17 -0400619 // We should have already handled bitmaps larger than the max texture size.
620 SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() &&
621 tmpB.height() <= context->priv().caps()->maxTextureSize());
622 // We should be respecting the max tile size by the time we get here.
623 SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() &&
624 tmpB.height() <= context->priv().caps()->maxTileSize());
625
Brian Salomonbc074a62020-03-18 10:06:13 -0400626 GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw);
Michael Ludwig47237242020-03-10 16:16:17 -0400627
628 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
629 if (aa == GrAA::kYes) {
630 // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges.
631 if (tileR.fLeft <= srcRect.fLeft) {
632 aaFlags |= GrQuadAAFlags::kLeft;
633 }
634 if (tileR.fRight >= srcRect.fRight) {
635 aaFlags |= GrQuadAAFlags::kRight;
636 }
637 if (tileR.fTop <= srcRect.fTop) {
638 aaFlags |= GrQuadAAFlags::kTop;
639 }
640 if (tileR.fBottom >= srcRect.fBottom) {
641 aaFlags |= GrQuadAAFlags::kBottom;
642 }
643 }
644
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400645 // now offset it to make it "local" to our tmp bitmap
646 tileR.offset(-offset.fX, -offset.fY);
Michael Ludwig47237242020-03-10 16:16:17 -0400647 SkMatrix offsetSrcToDst = srcToDst;
648 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
Brian Osman449b1152020-04-15 16:43:00 -0400649 draw_texture_producer(context, rtc, clip, matrixProvider, paint, &tileProducer,
650 tileR, rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400651 constraint, sampler, doBicubic);
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400652 }
653 }
654 }
655}
656
Michael Ludwig47237242020-03-10 16:16:17 -0400657} // anonymous namespace
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400658
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500659//////////////////////////////////////////////////////////////////////////////
660
Michael Ludwig278263d2020-10-12 19:49:24 +0000661void SkGpuDevice::drawSpecial(SkSpecialImage* special, const SkMatrix& localToDevice,
662 const SkPaint& paint) {
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400663 SkASSERT(!paint.getMaskFilter() && !paint.getImageFilter());
664 SkASSERT(special->isTextureBacked());
665
666 SkRect src = SkRect::Make(special->subset());
Michael Ludwig278263d2020-10-12 19:49:24 +0000667 SkRect dst = SkRect::MakeWH(special->width(), special->height());
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400668 SkMatrix srcToDst = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
669
Michael Ludwig278263d2020-10-12 19:49:24 +0000670 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp,
671 paint.getFilterQuality() >= kLow_SkFilterQuality ?
672 GrSamplerState::Filter::kLinear : GrSamplerState::Filter::kNearest);
673 GrAA aa = paint.isAntiAlias() ? GrAA::kYes : GrAA::kNo;
674 GrQuadAAFlags aaFlags = paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400675
676 GrColorInfo colorInfo(SkColorTypeToGrColorType(special->colorType()),
677 special->alphaType(), sk_ref_sp(special->getColorSpace()));
678
679 GrSurfaceProxyView view = special->view(this->recordingContext());
680 GrTextureAdjuster texture(fContext.get(), std::move(view), colorInfo, special->uniqueID());
681 // In most cases this ought to hit draw_texture since there won't be a color filter,
682 // alpha-only texture+shader, or a high filter quality.
Michael Ludwig278263d2020-10-12 19:49:24 +0000683 SkOverrideDeviceMatrixProvider matrixProvider(this->asMatrixProvider(), localToDevice);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500684 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), this->clip(), matrixProvider,
685 paint, &texture, src, dst, nullptr, srcToDst, aa, aaFlags,
686 SkCanvas::kStrict_SrcRectConstraint, sampler, false);
Michael Ludwig16d5b0a2020-08-31 13:07:16 -0400687}
688
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500689void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
690 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500691 const SkMatrix* preViewMatrix, const SkPaint& paint,
692 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500693 SkRect src;
694 SkRect dst;
695 SkMatrix srcToDst;
696 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
697 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
698 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800699 return;
700 }
701
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500702 if (src.contains(image->bounds())) {
703 constraint = SkCanvas::kFast_SrcRectConstraint;
704 }
705 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500706 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
707 ? GrSamplerState::WrapMode::kClampToBorder
708 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400709
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500710 // Get final CTM matrix
Brian Osman449b1152020-04-15 16:43:00 -0400711 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
712 preViewMatrix ? *preViewMatrix : SkMatrix::I());
713 const SkMatrix& ctm(matrixProvider.localToDevice());
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500714
Brian Salomone69b9ef2020-07-22 11:18:06 -0400715 bool sharpenMM = fContext->priv().options().fSharpenMipmappedTextures;
716 auto [fm, mm, bicubic] = GrInterpretFilterQuality(image->dimensions(), paint.getFilterQuality(),
John Stilesf2f9b0d2020-08-25 13:33:45 -0400717 ctm, srcToDst, sharpenMM,
718 /*allowFilterQualityReduction=*/true);
Michael Ludwig47237242020-03-10 16:16:17 -0400719
720 auto clip = this->clip();
721
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500722 // YUVA images can be stored in multiple images with different plane resolutions, so this
723 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
724 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
725 if (as_IB(image)->isYUVA()) {
Brian Salomon777e1462020-02-28 21:10:31 -0500726 GrYUVAImageTextureMaker maker(fContext.get(), image);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500727 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400728 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400729 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500730 return;
731 }
732
733 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
734 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
735 uint32_t pinnedUniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400736 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->recordingContext(),
737 &pinnedUniqueID)) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500738 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500739 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500740 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
741 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
742 image->imageInfo().refColorSpace());
743 } else {
744 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
745 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500746
Brian Salomon777e1462020-02-28 21:10:31 -0500747 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500748 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400749 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400750 constraint, {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500751 return;
752 }
753
Michael Ludwig47237242020-03-10 16:16:17 -0400754 // Next up, determine if the image must be tiled
755 {
756 // If image is explicitly already texture backed then we shouldn't get here.
757 SkASSERT(!image->isTextureBacked());
758
759 int tileFilterPad;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400760 if (bicubic) {
Michael Ludwig47237242020-03-10 16:16:17 -0400761 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
762 } else if (GrSamplerState::Filter::kNearest == fm) {
763 tileFilterPad = 0;
764 } else {
765 tileFilterPad = 1;
766 }
767 int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad;
768 int tileSize;
769 SkIRect clippedSubset;
Brian Salomon8f7d9532020-12-23 09:16:59 -0500770 if (should_tile_image_id(fContext.get(), fSurfaceDrawContext->dimensions(), clip,
771 image->unique(), image->dimensions(), ctm, srcToDst, &src,
Michael Ludwig47237242020-03-10 16:16:17 -0400772 maxTileSize, &tileSize, &clippedSubset)) {
773 // Extract pixels on the CPU, since we have to split into separate textures before
774 // sending to the GPU.
775 SkBitmap bm;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400776 if (as_IB(image)->getROPixels(nullptr, &bm)) {
Greg Daniel5c53ae52020-10-28 13:22:30 -0400777 // This is the funnel for all paths that draw tiled bitmaps/images.
Brian Salomon8f7d9532020-12-23 09:16:59 -0500778 draw_tiled_bitmap(fContext.get(), fSurfaceDrawContext.get(), clip, bm, tileSize,
Brian Osman449b1152020-04-15 16:43:00 -0400779 matrixProvider, srcToDst, src, clippedSubset, paint, aa,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400780 constraint, {wrapMode, fm, mm}, bicubic);
Michael Ludwig47237242020-03-10 16:16:17 -0400781 return;
782 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500783 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500784 }
785
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500786 // Lazily generated images must get drawn as a texture producer that handles the final
787 // texture creation.
788 if (image->isLazyGenerated()) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400789 GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500790 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400791 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400792 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500793 return;
794 }
Michael Ludwig47237242020-03-10 16:16:17 -0400795
796 SkBitmap bm;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400797 if (as_IB(image)->getROPixels(nullptr, &bm)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400798 GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw);
Brian Salomon8f7d9532020-12-23 09:16:59 -0500799 draw_texture_producer(fContext.get(), fSurfaceDrawContext.get(), clip, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400800 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400801 {wrapMode, fm, mm}, bicubic);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500802 }
803
804 // Otherwise don't know how to draw it
805}
806
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400807void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
808 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
809 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500810 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400811 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500812 // Send every entry through drawImageQuad() to handle the more complicated paint
813 int dstClipIndex = 0;
814 for (int i = 0; i < count; ++i) {
815 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400816 SkASSERT(!set[i].fHasClip || dstClips);
817 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500818
Brian Salomondb151e02019-09-17 12:11:16 -0400819 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
820 if (set[i].fAlpha != 1.f) {
821 auto paintAlpha = paint.getAlphaf();
822 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
823 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500824 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400825 this->drawImageQuad(
826 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
827 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
828 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400829 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400830 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400831 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500832 }
833 return;
834 }
835
Brian Salomona3b02f52020-07-15 16:02:01 -0400836 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality()
837 ? GrSamplerState::Filter::kNearest
838 : GrSamplerState::Filter::kLinear;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500839 SkBlendMode mode = paint.getBlendMode();
840
Brian Salomoneebe7352020-12-09 16:37:04 -0500841 SkAutoTArray<GrSurfaceDrawContext::TextureSetEntry> textures(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500842 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500843 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
844 // switches that occur within the 'n' entries.
845 int base = 0, n = 0, p = 0;
846 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500847 if (n > 0) {
848 auto textureXform = GrColorSpaceXform::Make(
849 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon8f7d9532020-12-23 09:16:59 -0500850 fSurfaceDrawContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
851 fSurfaceDrawContext->drawTextureSet(this->clip(),
852 textures.get() + base,
853 n,
854 p,
855 filter,
856 GrSamplerState::MipmapMode::kNone,
857 mode,
858 GrAA::kYes,
859 constraint,
860 this->localToDevice(),
861 std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500862 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500863 base = nextBase;
864 n = 0;
865 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500866 };
867 int dstClipIndex = 0;
868 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400869 SkASSERT(!set[i].fHasClip || dstClips);
870 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
871
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500872 // Manage the dst clip pointer tracking before any continues are used so we don't lose
873 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400874 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
875 dstClipIndex += 4 * set[i].fHasClip;
876
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500877 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
878 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
879 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500880 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500881 continue;
882 }
883
Greg Danielcc21d0c2020-02-05 16:58:40 -0500884 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400885 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500886 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400887 // drawImageQuad and the proper effect to dynamically sample their planes.
888 if (!image->isYUVA()) {
889 uint32_t uniqueID;
Robert Phillipsbedaef32020-06-29 10:37:57 -0400890 view = image->refPinnedView(this->recordingContext(), &uniqueID);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500891 if (!view) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400892 view = image->refView(this->recordingContext(), GrMipmapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500893 }
Brian Salomon16d86132020-12-28 11:38:35 -0500894 if (image->isAlphaOnly()) {
895 GrSwizzle swizzle = GrSwizzle::Concat(view.swizzle(), GrSwizzle("aaaa"));
896 view = {view.detachProxy(), view.origin(), swizzle};
897 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500898 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500899
Greg Danielcc21d0c2020-02-05 16:58:40 -0500900 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400901 // This image can't go through the texture op, send through general image pipeline
902 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500903 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400904 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
905 if (set[i].fAlpha != 1.f) {
906 auto paintAlpha = paint.getAlphaf();
907 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
908 }
909 this->drawImageQuad(
910 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400911 SkToGrQuadAAFlags(set[i].fAAFlags),
912 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400913 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400914 continue;
915 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500916
Greg Danielcc21d0c2020-02-05 16:58:40 -0500917 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500918 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500919 textures[i].fSrcRect = set[i].fSrcRect;
920 textures[i].fDstRect = set[i].fDstRect;
921 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400922 textures[i].fPreViewMatrix =
923 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400924 textures[i].fColor = texture_color(paint.getColor4f(), set[i].fAlpha,
925 SkColorTypeToGrColorType(image->colorType()),
Brian Salomon8f7d9532020-12-23 09:16:59 -0500926 fSurfaceDrawContext->colorInfo());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500927 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
928
929 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400930 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500931 textures[i].fProxyView.proxy(),
932 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500933 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500934 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
935 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500936 draw(i);
937 }
938 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
939 // set being accumulated so increment n, and increment p if proxies are different.
940 ++n;
941 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
942 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
943 // to i - 1).
944 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500945 }
946 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500947 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500948}