1 /*
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
8 #include "src/gpu/ganesh/Device_v1.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/gpu/GrDirectContext.h"
13 #include "include/gpu/GrRecordingContext.h"
14 #include "include/private/base/SkTPin.h"
15 #include "src/core/SkDraw.h"
16 #include "src/core/SkImagePriv.h"
17 #include "src/core/SkMaskFilterBase.h"
18 #include "src/core/SkSamplingPriv.h"
19 #include "src/core/SkSpecialImage.h"
20 #include "src/gpu/ganesh/GrBlurUtils.h"
21 #include "src/gpu/ganesh/GrCaps.h"
22 #include "src/gpu/ganesh/GrColorSpaceXform.h"
23 #include "src/gpu/ganesh/GrFPArgs.h"
24 #include "src/gpu/ganesh/GrOpsTypes.h"
25 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
26 #include "src/gpu/ganesh/GrStyle.h"
27 #include "src/gpu/ganesh/SkGr.h"
28 #include "src/gpu/ganesh/SurfaceDrawContext.h"
29 #include "src/gpu/ganesh/effects/GrBicubicEffect.h"
30 #include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h"
31 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
32 #include "src/gpu/ganesh/geometry/GrRect.h"
33 #include "src/gpu/ganesh/geometry/GrStyledShape.h"
34 #include "src/image/SkImage_Base.h"
35 #include "src/image/SkImage_Gpu.h"
36
37 using namespace skia_private;
38
39 namespace {
40
use_shader(bool textureIsAlphaOnly,const SkPaint & paint)41 inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
42 return textureIsAlphaOnly && paint.getShader();
43 }
44
45 //////////////////////////////////////////////////////////////////////////////
46 // Helper functions for dropping src rect subset with GrSamplerState::Filter::kLinear.
47
48 static const SkScalar kColorBleedTolerance = 0.001f;
49
has_aligned_samples(const SkRect & srcRect,const SkRect & transformedRect)50 bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
51 // detect pixel disalignment
52 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
53 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
54 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
55 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
56 return true;
57 }
58 return false;
59 }
60
may_color_bleed(const SkRect & srcRect,const SkRect & transformedRect,const SkMatrix & m,int numSamples)61 bool may_color_bleed(const SkRect& srcRect,
62 const SkRect& transformedRect,
63 const SkMatrix& m,
64 int numSamples) {
65 // Only gets called if has_aligned_samples returned false.
66 // So we can assume that sampling is axis aligned but not texel aligned.
67 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
68 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
69 if (numSamples > 1) {
70 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
71 } else {
72 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
73 }
74 m.mapRect(&innerTransformedRect, innerSrcRect);
75
76 // The gap between outerTransformedRect and innerTransformedRect
77 // represents the projection of the source border area, which is
78 // problematic for color bleeding. We must check whether any
79 // destination pixels sample the border area.
80 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
81 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
82 SkIRect outer, inner;
83 outerTransformedRect.round(&outer);
84 innerTransformedRect.round(&inner);
85 // If the inner and outer rects round to the same result, it means the
86 // border does not overlap any pixel centers. Yay!
87 return inner != outer;
88 }
89
can_ignore_linear_filtering_subset(const SkRect & srcSubset,const SkMatrix & srcRectToDeviceSpace,int numSamples)90 bool can_ignore_linear_filtering_subset(const SkRect& srcSubset,
91 const SkMatrix& srcRectToDeviceSpace,
92 int numSamples) {
93 if (srcRectToDeviceSpace.rectStaysRect()) {
94 // sampling is axis-aligned
95 SkRect transformedRect;
96 srcRectToDeviceSpace.mapRect(&transformedRect, srcSubset);
97
98 if (has_aligned_samples(srcSubset, transformedRect) ||
99 !may_color_bleed(srcSubset, transformedRect, srcRectToDeviceSpace, numSamples)) {
100 return true;
101 }
102 }
103 return false;
104 }
105
106 //////////////////////////////////////////////////////////////////////////////
107 // Helper functions for tiling a large SkBitmap
108
109 static const int kBmpSmallTileSize = 1 << 10;
110
get_tile_count(const SkIRect & srcRect,int tileSize)111 inline size_t get_tile_count(const SkIRect& srcRect, int tileSize) {
112 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
113 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
114 // We calculate expected tile count before we read the bitmap's pixels, so hypothetically we can
115 // have lazy images with excessive dimensions that would cause (tilesX*tilesY) to overflow int.
116 // In these situations we also later fail to allocate a bitmap to store the lazy image, so there
117 // isn't really a performance concern around one image turning into millions of tiles.
118 return SkSafeMath::Mul(tilesX, tilesY);
119 }
120
determine_tile_size(const SkIRect & src,int maxTileSize)121 int determine_tile_size(const SkIRect& src, int maxTileSize) {
122 if (maxTileSize <= kBmpSmallTileSize) {
123 return maxTileSize;
124 }
125
126 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
127 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
128
129 maxTileTotalTileSize *= maxTileSize * maxTileSize;
130 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
131
132 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
133 return kBmpSmallTileSize;
134 } else {
135 return maxTileSize;
136 }
137 }
138
139 // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
140 // pixels from the bitmap are necessary.
determine_clipped_src_rect(int width,int height,const GrClip * clip,const SkMatrix & viewMatrix,const SkMatrix & srcToDstRect,const SkISize & imageDimensions,const SkRect * srcRectPtr)141 SkIRect determine_clipped_src_rect(int width, int height,
142 const GrClip* clip,
143 const SkMatrix& viewMatrix,
144 const SkMatrix& srcToDstRect,
145 const SkISize& imageDimensions,
146 const SkRect* srcRectPtr) {
147 SkIRect clippedSrcIRect = clip ? clip->getConservativeBounds()
148 : SkIRect::MakeWH(width, height);
149 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
150 if (!inv.invert(&inv)) {
151 return SkIRect::MakeEmpty();
152 }
153 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
154 inv.mapRect(&clippedSrcRect);
155 if (srcRectPtr) {
156 if (!clippedSrcRect.intersect(*srcRectPtr)) {
157 return SkIRect::MakeEmpty();
158 }
159 }
160 clippedSrcRect.roundOut(&clippedSrcIRect);
161 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
162 if (!clippedSrcIRect.intersect(bmpBounds)) {
163 return SkIRect::MakeEmpty();
164 }
165
166 return clippedSrcIRect;
167 }
168
169 // tileSize and clippedSubset are valid if true is returned
should_tile_image_id(GrRecordingContext * context,SkISize rtSize,const GrClip * clip,uint32_t imageID,const SkISize & imageSize,const SkMatrix & ctm,const SkMatrix & srcToDst,const SkRect * src,int maxTileSize,int * tileSize,SkIRect * clippedSubset)170 bool should_tile_image_id(GrRecordingContext* context,
171 SkISize rtSize,
172 const GrClip* clip,
173 uint32_t imageID,
174 const SkISize& imageSize,
175 const SkMatrix& ctm,
176 const SkMatrix& srcToDst,
177 const SkRect* src,
178 int maxTileSize,
179 int* tileSize,
180 SkIRect* clippedSubset) {
181 // if it's larger than the max tile size, then we have no choice but tiling.
182 if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) {
183 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
184 srcToDst, imageSize, src);
185 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
186 return true;
187 }
188
189 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
190 const size_t area = imageSize.width() * imageSize.height();
191 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
192 return false;
193 }
194
195 // At this point we know we could do the draw by uploading the entire bitmap as a texture.
196 // However, if the texture would be large compared to the cache size and we don't require most
197 // of it for this draw then tile to reduce the amount of upload and cache spill.
198 // NOTE: if the context is not a direct context, it doesn't have access to the resource cache,
199 // and theoretically, the resource cache's limits could be being changed on another thread, so
200 // even having access to just the limit wouldn't be a reliable test during recording here.
201 // Instead, we will just upload the entire image to be on the safe side and not tile.
202 auto direct = context->asDirectContext();
203 if (!direct) {
204 return false;
205 }
206
207 // assumption here is that sw bitmap size is a good proxy for its size as
208 // a texture
209 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
210 size_t cacheSize = direct->getResourceCacheLimit();
211 if (bmpSize < cacheSize / 2) {
212 return false;
213 }
214
215 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
216 // tiling memory savings would be < 50%.
217 *clippedSubset = determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm,
218 srcToDst, imageSize, src);
219 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
220 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
221 kBmpSmallTileSize * kBmpSmallTileSize *
222 sizeof(SkPMColor); // assume 32bit pixels;
223
224 return usedTileBytes * 2 < bmpSize;
225 }
226
227 // This method outsets 'iRect' by 'outset' all around and then clamps its extents to
228 // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
229 // of 'iRect' for all possible outsets/clamps.
clamped_outset_with_offset(SkIRect * iRect,int outset,SkPoint * offset,const SkIRect & clamp)230 inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset,
231 const SkIRect& clamp) {
232 iRect->outset(outset, outset);
233
234 int leftClampDelta = clamp.fLeft - iRect->fLeft;
235 if (leftClampDelta > 0) {
236 offset->fX -= outset - leftClampDelta;
237 iRect->fLeft = clamp.fLeft;
238 } else {
239 offset->fX -= outset;
240 }
241
242 int topClampDelta = clamp.fTop - iRect->fTop;
243 if (topClampDelta > 0) {
244 offset->fY -= outset - topClampDelta;
245 iRect->fTop = clamp.fTop;
246 } else {
247 offset->fY -= outset;
248 }
249
250 if (iRect->fRight > clamp.fRight) {
251 iRect->fRight = clamp.fRight;
252 }
253 if (iRect->fBottom > clamp.fBottom) {
254 iRect->fBottom = clamp.fBottom;
255 }
256 }
257
258 //////////////////////////////////////////////////////////////////////////////
259 // Helper functions for drawing an image with v1::SurfaceDrawContext
260
261 enum class ImageDrawMode {
262 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
263 kOptimized,
264 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
265 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
266 kDecal,
267 // Src or dst are empty, or do not intersect the image content so don't draw anything.
268 kSkip
269 };
270
271 /**
272 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
273 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
274 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
275 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
276 *
277 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
278 * original src rect. 'dstClip' should be null when there is no additional clipping.
279 */
optimize_sample_area(const SkISize & image,const SkRect * origSrcRect,const SkRect * origDstRect,const SkPoint dstClip[4],SkRect * outSrcRect,SkRect * outDstRect,SkMatrix * srcToDst)280 ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
281 const SkRect* origDstRect, const SkPoint dstClip[4],
282 SkRect* outSrcRect, SkRect* outDstRect,
283 SkMatrix* srcToDst) {
284 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
285
286 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
287 SkRect dst = origDstRect ? *origDstRect : src;
288
289 if (src.isEmpty() || dst.isEmpty()) {
290 return ImageDrawMode::kSkip;
291 }
292
293 if (outDstRect) {
294 *srcToDst = SkMatrix::RectToRect(src, dst);
295 } else {
296 srcToDst->setIdentity();
297 }
298
299 if (origSrcRect && !srcBounds.contains(src)) {
300 if (!src.intersect(srcBounds)) {
301 return ImageDrawMode::kSkip;
302 }
303 srcToDst->mapRect(&dst, src);
304
305 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
306 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
307 if (dstClip) {
308 for (int i = 0; i < 4; ++i) {
309 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
310 // Must resort to using a decal mode restricted to the clipped 'src', and
311 // use the original dst rect (filling in src bounds as needed)
312 *outSrcRect = src;
313 *outDstRect = (origDstRect ? *origDstRect
314 : (origSrcRect ? *origSrcRect : srcBounds));
315 return ImageDrawMode::kDecal;
316 }
317 }
318 }
319 }
320
321 // The original src and dst were fully contained in the image, or there was no dst clip to
322 // worry about, or the clip was still contained in the restricted dst rect.
323 *outSrcRect = src;
324 *outDstRect = dst;
325 return ImageDrawMode::kOptimized;
326 }
327
328 /**
329 * Checks whether the paint is compatible with using SurfaceDrawContext::drawTexture. It is more
330 * efficient than the SkImage general case.
331 */
can_use_draw_texture(const SkPaint & paint,const SkSamplingOptions & sampling)332 bool can_use_draw_texture(const SkPaint& paint, const SkSamplingOptions& sampling) {
333 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
334 !paint.getImageFilter() && !paint.getBlender() && !sampling.isAniso() &&
335 !sampling.useCubic && sampling.mipmap == SkMipmapMode::kNone);
336 }
337
texture_color(SkColor4f paintColor,float entryAlpha,GrColorType srcColorType,const GrColorInfo & dstColorInfo)338 SkPMColor4f texture_color(SkColor4f paintColor, float entryAlpha, GrColorType srcColorType,
339 const GrColorInfo& dstColorInfo) {
340 paintColor.fA *= entryAlpha;
341 if (GrColorTypeIsAlphaOnly(srcColorType)) {
342 return SkColor4fPrepForDst(paintColor, dstColorInfo).premul();
343 } else {
344 float paintAlpha = SkTPin(paintColor.fA, 0.f, 1.f);
345 return { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
346 }
347 }
348
349 // Assumes srcRect and dstRect have already been optimized to fit the proxy
draw_texture(skgpu::v1::SurfaceDrawContext * sdc,const GrClip * clip,const SkMatrix & ctm,const SkPaint & paint,GrSamplerState::Filter filter,const SkRect & srcRect,const SkRect & dstRect,const SkPoint dstClip[4],GrQuadAAFlags aaFlags,SkCanvas::SrcRectConstraint constraint,GrSurfaceProxyView view,const GrColorInfo & srcColorInfo)350 void draw_texture(skgpu::v1::SurfaceDrawContext* sdc,
351 const GrClip* clip,
352 const SkMatrix& ctm,
353 const SkPaint& paint,
354 GrSamplerState::Filter filter,
355 const SkRect& srcRect,
356 const SkRect& dstRect,
357 const SkPoint dstClip[4],
358 GrQuadAAFlags aaFlags,
359 SkCanvas::SrcRectConstraint constraint,
360 GrSurfaceProxyView view,
361 const GrColorInfo& srcColorInfo) {
362 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
363 view.concatSwizzle(skgpu::Swizzle("aaaa"));
364 }
365 const GrColorInfo& dstInfo = sdc->colorInfo();
366 auto textureXform = GrColorSpaceXform::Make(srcColorInfo, sdc->colorInfo());
367 GrSurfaceProxy* proxy = view.proxy();
368 // 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.
370 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
371 // Conservative estimate of how much a coord could be outset from src rect:
372 // 1/2 pixel for AA and 1/2 pixel for linear filtering
373 float buffer = 0.5f * (aaFlags != GrQuadAAFlags::kNone) +
374 0.5f * (filter == GrSamplerState::Filter::kLinear);
375 SkRect safeBounds = proxy->getBoundsRect();
376 safeBounds.inset(buffer, buffer);
377 if (!safeBounds.contains(srcRect)) {
378 constraint = SkCanvas::kStrict_SrcRectConstraint;
379 }
380 }
381
382 SkPMColor4f color = texture_color(paint.getColor4f(), 1.f, srcColorInfo.colorType(), dstInfo);
383 if (dstClip) {
384 // Get source coords corresponding to dstClip
385 SkPoint srcQuad[4];
386 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
387
388 sdc->drawTextureQuad(clip,
389 std::move(view),
390 srcColorInfo.colorType(),
391 srcColorInfo.alphaType(),
392 filter,
393 GrSamplerState::MipmapMode::kNone,
394 paint.getBlendMode_or(SkBlendMode::kSrcOver),
395 color,
396 srcQuad,
397 dstClip,
398 aaFlags,
399 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
400 ctm,
401 std::move(textureXform));
402 } else {
403 sdc->drawTexture(clip,
404 std::move(view),
405 srcColorInfo.alphaType(),
406 filter,
407 GrSamplerState::MipmapMode::kNone,
408 paint.getBlendMode_or(SkBlendMode::kSrcOver),
409 color,
410 srcRect,
411 dstRect,
412 aaFlags,
413 constraint,
414 ctm,
415 std::move(textureXform));
416 }
417 }
418
419 // Assumes srcRect and dstRect have already been optimized to fit the proxy.
draw_image(GrRecordingContext * rContext,skgpu::v1::SurfaceDrawContext * sdc,const GrClip * clip,const SkMatrixProvider & matrixProvider,const SkPaint & paint,const SkImage_Base & image,const SkRect & src,const SkRect & dst,const SkPoint dstClip[4],const SkMatrix & srcToDst,GrQuadAAFlags aaFlags,SkCanvas::SrcRectConstraint constraint,SkSamplingOptions sampling,SkTileMode tm=SkTileMode::kClamp)420 void draw_image(GrRecordingContext* rContext,
421 skgpu::v1::SurfaceDrawContext* sdc,
422 const GrClip* clip,
423 const SkMatrixProvider& matrixProvider,
424 const SkPaint& paint,
425 const SkImage_Base& image,
426 const SkRect& src,
427 const SkRect& dst,
428 const SkPoint dstClip[4],
429 const SkMatrix& srcToDst,
430 GrQuadAAFlags aaFlags,
431 SkCanvas::SrcRectConstraint constraint,
432 SkSamplingOptions sampling,
433 SkTileMode tm = SkTileMode::kClamp) {
434 const SkMatrix& ctm(matrixProvider.localToDevice());
435 if (tm == SkTileMode::kClamp && !image.isYUVA() && can_use_draw_texture(paint, sampling)) {
436 // We've done enough checks above to allow us to pass ClampNearest() and not check for
437 // scaling adjustments.
438 auto [view, ct] = image.asView(rContext, GrMipmapped::kNo);
439 if (!view) {
440 return;
441 }
442 GrColorInfo info(image.imageInfo().colorInfo());
443 info = info.makeColorType(ct);
444 draw_texture(sdc,
445 clip,
446 ctm,
447 paint,
448 sampling.filter,
449 src,
450 dst,
451 dstClip,
452 aaFlags,
453 constraint,
454 std::move(view),
455 info);
456 return;
457 }
458
459 const SkMaskFilter* mf = paint.getMaskFilter();
460
461 // The shader expects proper local coords, so we can't replace local coords with texture coords
462 // if the shader will be used. If we have a mask filter we will change the underlying geometry
463 // that is rendered.
464 bool canUseTextureCoordsAsLocalCoords = !use_shader(image.isAlphaOnly(), paint) && !mf;
465
466 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
467 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
468 // FP. In the future this should be an opaque optimization enabled by the combination of
469 // GrDrawOp/GP and FP.
470 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
471 mf = nullptr;
472 }
473
474 bool restrictToSubset = SkCanvas::kStrict_SrcRectConstraint == constraint;
475
476 // If we have to outset for AA then we will generate texture coords outside the src rect. The
477 // same happens for any mask filter that extends the bounds rendered in the dst.
478 // This is conservative as a mask filter does not have to expand the bounds rendered.
479 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
480
481 // Check for optimization to drop the src rect constraint when using linear filtering.
482 // TODO: Just rely on image to handle this.
483 if (sampling.isAniso() &&
484 !sampling.useCubic &&
485 sampling.filter == SkFilterMode::kLinear &&
486 restrictToSubset &&
487 sampling.mipmap == SkMipmapMode::kNone &&
488 coordsAllInsideSrcRect &&
489 !image.isYUVA()) {
490 SkMatrix combinedMatrix;
491 combinedMatrix.setConcat(ctm, srcToDst);
492 if (can_ignore_linear_filtering_subset(src, combinedMatrix, sdc->numSamples())) {
493 restrictToSubset = false;
494 }
495 }
496
497 SkMatrix textureMatrix;
498 if (canUseTextureCoordsAsLocalCoords) {
499 textureMatrix = SkMatrix::I();
500 } else {
501 if (!srcToDst.invert(&textureMatrix)) {
502 return;
503 }
504 }
505 const SkRect* subset = restrictToSubset ? &src : nullptr;
506 const SkRect* domain = coordsAllInsideSrcRect ? &src : nullptr;
507 SkTileMode tileModes[] = {tm, tm};
508 std::unique_ptr<GrFragmentProcessor> fp = image.asFragmentProcessor(rContext,
509 sampling,
510 tileModes,
511 textureMatrix,
512 subset,
513 domain);
514 fp = GrColorSpaceXformEffect::Make(std::move(fp),
515 image.imageInfo().colorInfo(),
516 sdc->colorInfo());
517 if (image.isAlphaOnly()) {
518 if (const auto* shader = as_SB(paint.getShader())) {
519 auto shaderFP = shader->asRootFragmentProcessor(
520 GrFPArgs(rContext, &sdc->colorInfo(), sdc->surfaceProps()),
521 matrixProvider.localToDevice());
522 if (!shaderFP) {
523 return;
524 }
525 fp = GrBlendFragmentProcessor::Make<SkBlendMode::kDstIn>(std::move(fp),
526 std::move(shaderFP));
527 } else {
528 // Multiply the input (paint) color by the texture (alpha)
529 fp = GrFragmentProcessor::MulInputByChildAlpha(std::move(fp));
530 }
531 }
532
533 GrPaint grPaint;
534 if (!SkPaintToGrPaintReplaceShader(rContext,
535 sdc->colorInfo(),
536 paint,
537 ctm,
538 std::move(fp),
539 sdc->surfaceProps(),
540 &grPaint)) {
541 return;
542 }
543
544 if (!mf) {
545 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
546 if (dstClip) {
547 SkPoint srcClipPoints[4];
548 SkPoint* srcClip = nullptr;
549 if (canUseTextureCoordsAsLocalCoords) {
550 // Calculate texture coordinates that match the dst clip
551 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
552 srcClip = srcClipPoints;
553 }
554 sdc->fillQuadWithEdgeAA(clip, std::move(grPaint), aaFlags, ctm, dstClip, srcClip);
555 } else {
556 // Provide explicit texture coords when possible, otherwise rely on texture matrix
557 sdc->fillRectWithEdgeAA(clip, std::move(grPaint), aaFlags, ctm, dst,
558 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
559 }
560 } else {
561 // Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA
562 // information since it always draws with AA, but that should not be noticeable since the
563 // mask filter is probably a blur.
564 GrStyledShape shape;
565 if (dstClip) {
566 // Represent it as an SkPath formed from the dstClip
567 SkPath path;
568 path.addPoly(dstClip, 4, true);
569 shape = GrStyledShape(path);
570 } else {
571 shape = GrStyledShape(dst);
572 }
573
574 GrBlurUtils::drawShapeWithMaskFilter(
575 rContext, sdc, clip, shape, std::move(grPaint), ctm, mf);
576 }
577 }
578
draw_tiled_bitmap(GrRecordingContext * rContext,skgpu::v1::SurfaceDrawContext * sdc,const GrClip * clip,const SkBitmap & bitmap,int tileSize,const SkMatrixProvider & matrixProvider,const SkMatrix & srcToDst,const SkRect & srcRect,const SkIRect & clippedSrcIRect,const SkPaint & paint,GrQuadAAFlags origAAFlags,SkCanvas::SrcRectConstraint constraint,SkSamplingOptions sampling,SkTileMode tileMode)579 void draw_tiled_bitmap(GrRecordingContext* rContext,
580 skgpu::v1::SurfaceDrawContext* sdc,
581 const GrClip* clip,
582 const SkBitmap& bitmap,
583 int tileSize,
584 const SkMatrixProvider& matrixProvider,
585 const SkMatrix& srcToDst,
586 const SkRect& srcRect,
587 const SkIRect& clippedSrcIRect,
588 const SkPaint& paint,
589 GrQuadAAFlags origAAFlags,
590 SkCanvas::SrcRectConstraint constraint,
591 SkSamplingOptions sampling,
592 SkTileMode tileMode) {
593 if (sampling.isAniso()) {
594 sampling = SkSamplingPriv::AnisoFallback(/*imageIsMipped=*/false);
595 }
596 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
597
598 int nx = bitmap.width() / tileSize;
599 int ny = bitmap.height() / tileSize;
600
601 for (int x = 0; x <= nx; x++) {
602 for (int y = 0; y <= ny; y++) {
603 SkRect tileR;
604 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
605 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
606
607 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
608 continue;
609 }
610
611 if (!tileR.intersect(srcRect)) {
612 continue;
613 }
614
615 SkIRect iTileR;
616 tileR.roundOut(&iTileR);
617 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
618 SkIntToScalar(iTileR.fTop));
619 SkRect rectToDraw = tileR;
620 srcToDst.mapRect(&rectToDraw);
621 if (sampling.filter != SkFilterMode::kNearest || sampling.useCubic) {
622 SkIRect iClampRect;
623
624 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
625 // In bleed mode we want to always expand the tile on all edges
626 // but stay within the bitmap bounds
627 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
628 } else {
629 // In texture-domain/clamp mode we only want to expand the
630 // tile on edges interior to "srcRect" (i.e., we want to
631 // not bleed across the original clamped edges)
632 srcRect.roundOut(&iClampRect);
633 }
634 int outset = sampling.useCubic ? GrBicubicEffect::kFilterTexelPad : 1;
635 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
636 }
637
638 // We must subset as a bitmap and then turn into an SkImage if we want caching to work.
639 // Image subsets always make a copy of the pixels and lose the association with the
640 // original's SkPixelRef.
641 if (SkBitmap subsetBmp; bitmap.extractSubset(&subsetBmp, iTileR)) {
642 auto image = SkMakeImageFromRasterBitmap(subsetBmp, kNever_SkCopyPixelsMode);
643 // We should have already handled bitmaps larger than the max texture size.
644 SkASSERT(image->width() <= rContext->priv().caps()->maxTextureSize() &&
645 image->height() <= rContext->priv().caps()->maxTextureSize());
646
647 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
648 // Preserve the original edge AA flags for the exterior tile edges.
649 if (tileR.fLeft <= srcRect.fLeft && (origAAFlags & GrQuadAAFlags::kLeft)) {
650 aaFlags |= GrQuadAAFlags::kLeft;
651 }
652 if (tileR.fRight >= srcRect.fRight && (origAAFlags & GrQuadAAFlags::kRight)) {
653 aaFlags |= GrQuadAAFlags::kRight;
654 }
655 if (tileR.fTop <= srcRect.fTop && (origAAFlags & GrQuadAAFlags::kTop)) {
656 aaFlags |= GrQuadAAFlags::kTop;
657 }
658 if (tileR.fBottom >= srcRect.fBottom && (origAAFlags & GrQuadAAFlags::kBottom)) {
659 aaFlags |= GrQuadAAFlags::kBottom;
660 }
661
662 // now offset it to make it "local" to our tmp bitmap
663 tileR.offset(-offset.fX, -offset.fY);
664 SkMatrix offsetSrcToDst = srcToDst;
665 offsetSrcToDst.preTranslate(offset.fX, offset.fY);
666 draw_image(rContext,
667 sdc,
668 clip,
669 matrixProvider,
670 paint,
671 *as_IB(image.get()),
672 tileR,
673 rectToDraw,
674 nullptr,
675 offsetSrcToDst,
676 aaFlags,
677 constraint,
678 sampling,
679 tileMode);
680 }
681 }
682 }
683 }
684
downgrade_to_filter(const SkSamplingOptions & sampling)685 SkFilterMode downgrade_to_filter(const SkSamplingOptions& sampling) {
686 SkFilterMode filter = sampling.filter;
687 if (sampling.isAniso() || sampling.useCubic || sampling.mipmap != SkMipmapMode::kNone) {
688 // if we were "fancier" than just bilerp, only do bilerp
689 filter = SkFilterMode::kLinear;
690 }
691 return filter;
692 }
693
can_disable_mipmap(const SkMatrix & viewM,const SkMatrix & localM)694 bool can_disable_mipmap(const SkMatrix& viewM, const SkMatrix& localM) {
695 SkMatrix matrix;
696 matrix.setConcat(viewM, localM);
697 // We bias mipmap lookups by -0.5. That means our final LOD is >= 0 until
698 // the computed LOD is >= 0.5. At what scale factor does a texture get an LOD of
699 // 0.5?
700 //
701 // Want: 0 = log2(1/s) - 0.5
702 // 0.5 = log2(1/s)
703 // 2^0.5 = 1/s
704 // 1/2^0.5 = s
705 // 2^0.5/2 = s
706 return matrix.getMinScale() >= SK_ScalarRoot2Over2;
707 }
708
709 } // anonymous namespace
710
711 //////////////////////////////////////////////////////////////////////////////
712
713 namespace skgpu::v1 {
714
drawSpecial(SkSpecialImage * special,const SkMatrix & localToDevice,const SkSamplingOptions & origSampling,const SkPaint & paint)715 void Device::drawSpecial(SkSpecialImage* special,
716 const SkMatrix& localToDevice,
717 const SkSamplingOptions& origSampling,
718 const SkPaint& paint) {
719 SkASSERT(!paint.getMaskFilter() && !paint.getImageFilter());
720 SkASSERT(special->isTextureBacked());
721
722 SkRect src = SkRect::Make(special->subset());
723 SkRect dst = SkRect::MakeWH(special->width(), special->height());
724 SkMatrix srcToDst = SkMatrix::RectToRect(src, dst);
725
726 SkSamplingOptions sampling = SkSamplingOptions(downgrade_to_filter(origSampling));
727 GrAA aa = fSurfaceDrawContext->chooseAA(paint);
728 GrQuadAAFlags aaFlags = (aa == GrAA::kYes) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
729
730 GrSurfaceProxyView view = special->view(this->recordingContext());
731 SkImage_Gpu image(sk_ref_sp(special->getContext()),
732 special->uniqueID(),
733 std::move(view),
734 special->colorInfo());
735 // In most cases this ought to hit draw_texture since there won't be a color filter,
736 // alpha-only texture+shader, or a high filter quality.
737 SkMatrixProvider matrixProvider(localToDevice);
738 draw_image(fContext.get(),
739 fSurfaceDrawContext.get(),
740 this->clip(),
741 matrixProvider,
742 paint,
743 image,
744 src,
745 dst,
746 nullptr,
747 srcToDst,
748 aaFlags,
749 SkCanvas::kStrict_SrcRectConstraint,
750 sampling);
751 }
752
drawImageQuad(const SkImage * image,const SkRect * srcRect,const SkRect * dstRect,const SkPoint dstClip[4],GrQuadAAFlags aaFlags,const SkMatrix * preViewMatrix,const SkSamplingOptions & origSampling,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)753 void Device::drawImageQuad(const SkImage* image,
754 const SkRect* srcRect,
755 const SkRect* dstRect,
756 const SkPoint dstClip[4],
757 GrQuadAAFlags aaFlags,
758 const SkMatrix* preViewMatrix,
759 const SkSamplingOptions& origSampling,
760 const SkPaint& paint,
761 SkCanvas::SrcRectConstraint constraint) {
762 SkRect src;
763 SkRect dst;
764 SkMatrix srcToDst;
765 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
766 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
767 if (mode == ImageDrawMode::kSkip) {
768 return;
769 }
770
771 if (src.contains(image->bounds())) {
772 constraint = SkCanvas::kFast_SrcRectConstraint;
773 }
774 // Depending on the nature of image, it can flow through more or less optimal pipelines
775 SkTileMode tileMode = mode == ImageDrawMode::kDecal ? SkTileMode::kDecal : SkTileMode::kClamp;
776
777 // Get final CTM matrix
778 SkPreConcatMatrixProvider matrixProvider(this->asMatrixProvider(),
779 preViewMatrix ? *preViewMatrix : SkMatrix::I());
780 const SkMatrix& ctm(matrixProvider.localToDevice());
781
782 SkSamplingOptions sampling = origSampling;
783 if (sampling.mipmap != SkMipmapMode::kNone && can_disable_mipmap(ctm, srcToDst)) {
784 sampling = SkSamplingOptions(sampling.filter);
785 }
786 auto clip = this->clip();
787
788 if (!image->isTextureBacked() && !as_IB(image)->isPinnedOnContext(fContext.get())) {
789 int tileFilterPad;
790 if (sampling.useCubic) {
791 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
792 } else if (sampling.filter == SkFilterMode::kLinear || sampling.isAniso()) {
793 // Aniso will fallback to linear filtering in the tiling case.
794 tileFilterPad = 1;
795 } else {
796 tileFilterPad = 0;
797 }
798 int maxTileSize = fContext->priv().caps()->maxTextureSize() - 2*tileFilterPad;
799 int tileSize;
800 SkIRect clippedSubset;
801 if (should_tile_image_id(fContext.get(),
802 fSurfaceDrawContext->dimensions(),
803 clip,
804 image->unique(),
805 image->dimensions(),
806 ctm,
807 srcToDst,
808 &src,
809 maxTileSize,
810 &tileSize,
811 &clippedSubset)) {
812 // Extract pixels on the CPU, since we have to split into separate textures before
813 // sending to the GPU if tiling.
814 if (SkBitmap bm; as_IB(image)->getROPixels(nullptr, &bm)) {
815 // This is the funnel for all paths that draw tiled bitmaps/images.
816 draw_tiled_bitmap(fContext.get(),
817 fSurfaceDrawContext.get(),
818 clip,
819 bm,
820 tileSize,
821 matrixProvider,
822 srcToDst,
823 src,
824 clippedSubset,
825 paint,
826 aaFlags,
827 constraint,
828 sampling,
829 tileMode);
830 return;
831 }
832 }
833 }
834
835 draw_image(fContext.get(),
836 fSurfaceDrawContext.get(),
837 clip,
838 matrixProvider,
839 paint,
840 *as_IB(image),
841 src,
842 dst,
843 dstClip,
844 srcToDst,
845 aaFlags,
846 constraint,
847 sampling);
848 return;
849 }
850
drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)851 void Device::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
852 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
853 const SkSamplingOptions& sampling, const SkPaint& paint,
854 SkCanvas::SrcRectConstraint constraint) {
855 SkASSERT(count > 0);
856 if (!can_use_draw_texture(paint, sampling)) {
857 // Send every entry through drawImageQuad() to handle the more complicated paint
858 int dstClipIndex = 0;
859 for (int i = 0; i < count; ++i) {
860 // Only no clip or quad clip are supported
861 SkASSERT(!set[i].fHasClip || dstClips);
862 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
863
864 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
865 if (set[i].fAlpha != 1.f) {
866 auto paintAlpha = paint.getAlphaf();
867 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
868 }
869 this->drawImageQuad(
870 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
871 set[i].fHasClip ? dstClips + dstClipIndex : nullptr,
872 SkToGrQuadAAFlags(set[i].fAAFlags),
873 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
874 sampling, *entryPaint, constraint);
875 dstClipIndex += 4 * set[i].fHasClip;
876 }
877 return;
878 }
879
880 GrSamplerState::Filter filter = sampling.filter == SkFilterMode::kNearest
881 ? GrSamplerState::Filter::kNearest
882 : GrSamplerState::Filter::kLinear;
883 SkBlendMode mode = paint.getBlendMode_or(SkBlendMode::kSrcOver);
884
885 AutoTArray<GrTextureSetEntry> textures(count);
886 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
887 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
888 // switches that occur within the 'n' entries.
889 int base = 0, n = 0, p = 0;
890 auto draw = [&](int nextBase) {
891 if (n > 0) {
892 auto textureXform = GrColorSpaceXform::Make(set[base].fImage->imageInfo().colorInfo(),
893 fSurfaceDrawContext->colorInfo());
894 fSurfaceDrawContext->drawTextureSet(this->clip(),
895 textures.get() + base,
896 n,
897 p,
898 filter,
899 GrSamplerState::MipmapMode::kNone,
900 mode,
901 constraint,
902 this->localToDevice(),
903 std::move(textureXform));
904 }
905 base = nextBase;
906 n = 0;
907 p = 0;
908 };
909 int dstClipIndex = 0;
910 for (int i = 0; i < count; ++i) {
911 SkASSERT(!set[i].fHasClip || dstClips);
912 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
913
914 // Manage the dst clip pointer tracking before any continues are used so we don't lose
915 // our place in the dstClips array.
916 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
917 dstClipIndex += 4 * set[i].fHasClip;
918
919 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
920 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
921 if (!set[i].fSrcRect.isSorted()) {
922 draw(i + 1);
923 continue;
924 }
925
926 GrSurfaceProxyView view;
927 const SkImage_Base* image = as_IB(set[i].fImage.get());
928 // Extract view from image, but skip YUV images so they get processed through
929 // drawImageQuad and the proper effect to dynamically sample their planes.
930 if (!image->isYUVA()) {
931 std::tie(view, std::ignore) = image->asView(this->recordingContext(), GrMipmapped::kNo);
932 if (image->isAlphaOnly()) {
933 skgpu::Swizzle swizzle = skgpu::Swizzle::Concat(view.swizzle(),
934 skgpu::Swizzle("aaaa"));
935 view = {view.detachProxy(), view.origin(), swizzle};
936 }
937 }
938
939 if (!view) {
940 // This image can't go through the texture op, send through general image pipeline
941 // after flushing current batch.
942 draw(i + 1);
943 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
944 if (set[i].fAlpha != 1.f) {
945 auto paintAlpha = paint.getAlphaf();
946 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
947 }
948 this->drawImageQuad(
949 image, &set[i].fSrcRect, &set[i].fDstRect, clip,
950 SkToGrQuadAAFlags(set[i].fAAFlags),
951 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
952 sampling, *entryPaint, constraint);
953 continue;
954 }
955
956 textures[i].fProxyView = std::move(view);
957 textures[i].fSrcAlphaType = image->alphaType();
958 textures[i].fSrcRect = set[i].fSrcRect;
959 textures[i].fDstRect = set[i].fDstRect;
960 textures[i].fDstClipQuad = clip;
961 textures[i].fPreViewMatrix =
962 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
963 textures[i].fColor = texture_color(paint.getColor4f(), set[i].fAlpha,
964 SkColorTypeToGrColorType(image->colorType()),
965 fSurfaceDrawContext->colorInfo());
966 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
967
968 if (n > 0 &&
969 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
970 textures[i].fProxyView.proxy(),
971 textures[base].fProxyView.proxy()) ||
972 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
973 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
974 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
975 draw(i);
976 }
977 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
978 // set being accumulated so increment n, and increment p if proxies are different.
979 ++n;
980 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
981 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
982 // to i - 1).
983 ++p;
984 }
985 }
986 draw(count);
987 }
988
989 } // namespace skgpu::v1
990