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