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/SkGpuDevice.h"
9 
10 #include "include/core/SkYUVAIndex.h"
11 #include "src/core/SkDraw.h"
12 #include "src/core/SkMaskFilterBase.h"
13 #include "src/gpu/GrBitmapTextureMaker.h"
14 #include "src/gpu/GrBlurUtils.h"
15 #include "src/gpu/GrCaps.h"
16 #include "src/gpu/GrColorSpaceXform.h"
17 #include "src/gpu/GrImageTextureMaker.h"
18 #include "src/gpu/GrRenderTargetContext.h"
19 #include "src/gpu/GrStyle.h"
20 #include "src/gpu/GrTextureAdjuster.h"
21 #include "src/gpu/GrTextureMaker.h"
22 #include "src/gpu/SkGr.h"
23 #include "src/gpu/effects/GrBicubicEffect.h"
24 #include "src/gpu/effects/GrTextureDomain.h"
25 #include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
26 #include "src/gpu/geometry/GrShape.h"
27 #include "src/image/SkImage_Base.h"
28 
29 namespace {
30 
use_shader(bool textureIsAlphaOnly,const SkPaint & paint)31 static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
32     return textureIsAlphaOnly && paint.getShader();
33 }
34 
35 //////////////////////////////////////////////////////////////////////////////
36 //  Helper functions for dropping src rect constraint in bilerp mode.
37 
38 static const SkScalar kColorBleedTolerance = 0.001f;
39 
has_aligned_samples(const SkRect & srcRect,const SkRect & transformedRect)40 static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
41     // detect pixel disalignment
42     if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
43         SkScalarAbs(SkScalarRoundToScalar(transformedRect.top())  - transformedRect.top())  < kColorBleedTolerance &&
44         SkScalarAbs(transformedRect.width()  - srcRect.width())  < kColorBleedTolerance &&
45         SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
46         return true;
47     }
48     return false;
49 }
50 
may_color_bleed(const SkRect & srcRect,const SkRect & transformedRect,const SkMatrix & m,int numSamples)51 static bool may_color_bleed(const SkRect& srcRect,
52                             const SkRect& transformedRect,
53                             const SkMatrix& m,
54                             int numSamples) {
55     // Only gets called if has_aligned_samples returned false.
56     // So we can assume that sampling is axis aligned but not texel aligned.
57     SkASSERT(!has_aligned_samples(srcRect, transformedRect));
58     SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
59     if (numSamples > 1) {
60         innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
61     } else {
62         innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
63     }
64     m.mapRect(&innerTransformedRect, innerSrcRect);
65 
66     // The gap between outerTransformedRect and innerTransformedRect
67     // represents the projection of the source border area, which is
68     // problematic for color bleeding.  We must check whether any
69     // destination pixels sample the border area.
70     outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
71     innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
72     SkIRect outer, inner;
73     outerTransformedRect.round(&outer);
74     innerTransformedRect.round(&inner);
75     // If the inner and outer rects round to the same result, it means the
76     // border does not overlap any pixel centers. Yay!
77     return inner != outer;
78 }
79 
can_ignore_bilerp_constraint(const GrTextureProducer & producer,const SkRect & srcRect,const SkMatrix & srcRectToDeviceSpace,int numSamples)80 static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
81                                          const SkRect& srcRect,
82                                          const SkMatrix& srcRectToDeviceSpace,
83                                          int numSamples) {
84     if (srcRectToDeviceSpace.rectStaysRect()) {
85         // sampling is axis-aligned
86         SkRect transformedRect;
87         srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
88 
89         if (has_aligned_samples(srcRect, transformedRect) ||
90             !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, numSamples)) {
91             return true;
92         }
93     }
94     return false;
95 }
96 
97 enum class ImageDrawMode {
98     // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
99     kOptimized,
100     // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
101     // This is used when a dst clip is provided and extends outside of the optimized dst rect.
102     kDecal,
103     // Src or dst are empty, or do not intersect the image content so don't draw anything.
104     kSkip
105 };
106 
107 /**
108  * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
109  * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
110  * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
111  * 'srcToDst'. Outputs are not always updated when kSkip is returned.
112  *
113  * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
114  * original src rect. 'dstClip' should be null when there is no additional clipping.
115  */
optimize_sample_area(const SkISize & image,const SkRect * origSrcRect,const SkRect * origDstRect,const SkPoint dstClip[4],SkRect * outSrcRect,SkRect * outDstRect,SkMatrix * srcToDst)116 static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
117                                           const SkRect* origDstRect, const SkPoint dstClip[4],
118                                           SkRect* outSrcRect, SkRect* outDstRect,
119                                           SkMatrix* srcToDst) {
120     SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
121 
122     SkRect src = origSrcRect ? *origSrcRect : srcBounds;
123     SkRect dst = origDstRect ? *origDstRect : src;
124 
125     if (src.isEmpty() || dst.isEmpty()) {
126         return ImageDrawMode::kSkip;
127     }
128 
129     if (outDstRect) {
130         srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
131     } else {
132         srcToDst->setIdentity();
133     }
134 
135     if (origSrcRect && !srcBounds.contains(src)) {
136         if (!src.intersect(srcBounds)) {
137             return ImageDrawMode::kSkip;
138         }
139         srcToDst->mapRect(&dst, src);
140 
141         // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
142         // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
143         if (dstClip) {
144             for (int i = 0; i < 4; ++i) {
145                 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
146                     // Must resort to using a decal mode restricted to the clipped 'src', and
147                     // use the original dst rect (filling in src bounds as needed)
148                     *outSrcRect = src;
149                     *outDstRect = (origDstRect ? *origDstRect
150                                                : (origSrcRect ? *origSrcRect : srcBounds));
151                     return ImageDrawMode::kDecal;
152                 }
153             }
154         }
155     }
156 
157     // The original src and dst were fully contained in the image, or there was no dst clip to
158     // worry about, or the clip was still contained in the restricted dst rect.
159     *outSrcRect = src;
160     *outDstRect = dst;
161     return ImageDrawMode::kOptimized;
162 }
163 
164 /**
165  * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
166  * efficient than the GrTextureProducer general case.
167  */
can_use_draw_texture(const SkPaint & paint)168 static bool can_use_draw_texture(const SkPaint& paint) {
169     return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
170             !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
171 }
172 
173 // Assumes srcRect and dstRect have already been optimized to fit the proxy
draw_texture(GrRenderTargetContext * rtc,const GrClip & clip,const SkMatrix & ctm,const SkPaint & paint,const SkRect & srcRect,const SkRect & dstRect,const SkPoint dstClip[4],GrAA aa,GrQuadAAFlags aaFlags,SkCanvas::SrcRectConstraint constraint,sk_sp<GrTextureProxy> proxy,const GrColorInfo & srcColorInfo)174 static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
175                          const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
176                          const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
177                          SkCanvas::SrcRectConstraint constraint, sk_sp<GrTextureProxy> proxy,
178                          const GrColorInfo& srcColorInfo) {
179     const GrColorInfo& dstInfo(rtc->colorInfo());
180     auto textureXform =
181         GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
182                                 dstInfo.colorSpace(), kPremul_SkAlphaType);
183     GrSamplerState::Filter filter;
184     switch (paint.getFilterQuality()) {
185         case kNone_SkFilterQuality:
186             filter = GrSamplerState::Filter::kNearest;
187             break;
188         case kLow_SkFilterQuality:
189             filter = GrSamplerState::Filter::kBilerp;
190             break;
191         case kMedium_SkFilterQuality:
192         case kHigh_SkFilterQuality:
193             SK_ABORT("Quality level not allowed.");
194     }
195 
196     // Must specify the strict constraint when the proxy is not functionally exact and the src
197     // rect would access pixels outside the proxy's content area without the constraint.
198     if (constraint != SkCanvas::kStrict_SrcRectConstraint &&
199         !GrProxyProvider::IsFunctionallyExact(proxy.get())) {
200         // Conservative estimate of how much a coord could be outset from src rect:
201         // 1/2 pixel for AA and 1/2 pixel for bilerp
202         float buffer = 0.5f * (aa == GrAA::kYes) +
203                        0.5f * (filter == GrSamplerState::Filter::kBilerp);
204         SkRect safeBounds = SkRect::MakeWH(proxy->width(), proxy->height());
205         safeBounds.inset(buffer, buffer);
206         if (!safeBounds.contains(srcRect)) {
207             constraint = SkCanvas::kStrict_SrcRectConstraint;
208         }
209     }
210     SkPMColor4f color;
211     if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
212         color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
213     } else {
214         float paintAlpha = paint.getColor4f().fA;
215         color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
216     }
217 
218     if (dstClip) {
219         // Get source coords corresponding to dstClip
220         SkPoint srcQuad[4];
221         GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
222 
223         rtc->drawTextureQuad(clip, std::move(proxy), srcColorInfo.colorType(), filter,
224                              paint.getBlendMode(), color, srcQuad, dstClip, aa, aaFlags,
225                              constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
226                              ctm, std::move(textureXform));
227     } else {
228         rtc->drawTexture(clip, std::move(proxy), srcColorInfo.colorType(), filter,
229                          paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint,
230                          ctm, std::move(textureXform));
231     }
232 }
233 
234 // Assumes srcRect and dstRect have already been optimized to fit the proxy.
draw_texture_producer(GrContext * context,GrRenderTargetContext * rtc,const GrClip & clip,const SkMatrix & ctm,const SkPaint & paint,GrTextureProducer * producer,const SkRect & src,const SkRect & dst,const SkPoint dstClip[4],const SkMatrix & srcToDst,GrAA aa,GrQuadAAFlags aaFlags,SkCanvas::SrcRectConstraint constraint,bool attemptDrawTexture)235 static void draw_texture_producer(GrContext* context, GrRenderTargetContext* rtc,
236                                   const GrClip& clip, const SkMatrix& ctm,
237                                   const SkPaint& paint, GrTextureProducer* producer,
238                                   const SkRect& src, const SkRect& dst, const SkPoint dstClip[4],
239                                   const SkMatrix& srcToDst, GrAA aa, GrQuadAAFlags aaFlags,
240                                   SkCanvas::SrcRectConstraint constraint, bool attemptDrawTexture) {
241     if (attemptDrawTexture && can_use_draw_texture(paint)) {
242         // We've done enough checks above to allow us to pass ClampNearest() and not check for
243         // scaling adjustments.
244         auto proxy = producer->refTextureProxyForParams(GrSamplerState::ClampNearest(), nullptr);
245         if (!proxy) {
246             return;
247         }
248 
249         draw_texture(rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint,
250                      std::move(proxy), producer->colorInfo());
251         return;
252     }
253 
254     const SkMaskFilter* mf = paint.getMaskFilter();
255 
256     // The shader expects proper local coords, so we can't replace local coords with texture coords
257     // if the shader will be used. If we have a mask filter we will change the underlying geometry
258     // that is rendered.
259     bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
260 
261     // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
262     // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
263     // FP. In the future this should be an opaque optimization enabled by the combination of
264     // GrDrawOp/GP and FP.
265     if (mf && as_MFB(mf)->hasFragmentProcessor()) {
266         mf = nullptr;
267     }
268     bool doBicubic;
269     GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
270             producer->width(), producer->height(), paint.getFilterQuality(), ctm, srcToDst,
271             context->priv().options().fSharpenMipmappedTextures, &doBicubic);
272     const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
273 
274     GrTextureProducer::FilterConstraint constraintMode;
275     if (SkCanvas::kFast_SrcRectConstraint == constraint) {
276         constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
277     } else {
278         constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
279     }
280 
281     // If we have to outset for AA then we will generate texture coords outside the src rect. The
282     // same happens for any mask filter that extends the bounds rendered in the dst.
283     // This is conservative as a mask filter does not have to expand the bounds rendered.
284     bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
285 
286     // Check for optimization to drop the src rect constraint when on bilerp.
287     if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
288         GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect &&
289         !producer->hasMixedResolutions()) {
290         SkMatrix combinedMatrix;
291         combinedMatrix.setConcat(ctm, srcToDst);
292         if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) {
293             constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
294         }
295     }
296 
297     SkMatrix textureMatrix;
298     if (canUseTextureCoordsAsLocalCoords) {
299         textureMatrix = SkMatrix::I();
300     } else {
301         if (!srcToDst.invert(&textureMatrix)) {
302             return;
303         }
304     }
305     auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
306                                                 coordsAllInsideSrcRect, filterMode);
307     fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
308                                        rtc->colorInfo().colorSpace());
309     if (!fp) {
310         return;
311     }
312 
313     GrPaint grPaint;
314     if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, ctm, std::move(fp),
315                                      producer->isAlphaOnly(), &grPaint)) {
316         return;
317     }
318 
319     if (!mf) {
320         // Can draw the image directly (any mask filter on the paint was converted to an FP already)
321         if (dstClip) {
322             SkPoint srcClipPoints[4];
323             SkPoint* srcClip = nullptr;
324             if (canUseTextureCoordsAsLocalCoords) {
325                 // Calculate texture coordinates that match the dst clip
326                 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
327                 srcClip = srcClipPoints;
328             }
329             rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
330         } else {
331             // Provide explicit texture coords when possible, otherwise rely on texture matrix
332             rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
333                                     canUseTextureCoordsAsLocalCoords ? &src : nullptr);
334         }
335     } else {
336         // Must draw the mask filter as a GrShape. For now, this loses the per-edge AA information
337         // since it always draws with AA, but that is should not be noticeable since the mask filter
338         // is probably a blur.
339         GrShape shape;
340         if (dstClip) {
341             // Represent it as an SkPath formed from the dstClip
342             SkPath path;
343             path.addPoly(dstClip, 4, true);
344             shape = GrShape(path);
345         } else {
346             shape = GrShape(dst);
347         }
348 
349         GrBlurUtils::drawShapeWithMaskFilter(
350                 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
351     }
352 }
353 
354 } // anonymous namespace
355 
356 //////////////////////////////////////////////////////////////////////////////
357 
drawImageQuad(const SkImage * image,const SkRect * srcRect,const SkRect * dstRect,const SkPoint dstClip[4],GrAA aa,GrQuadAAFlags aaFlags,const SkMatrix * preViewMatrix,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)358 void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
359                                 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
360                                 const SkMatrix* preViewMatrix, const SkPaint& paint,
361                                 SkCanvas::SrcRectConstraint constraint) {
362     SkRect src;
363     SkRect dst;
364     SkMatrix srcToDst;
365     ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
366                                               srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
367     if (mode == ImageDrawMode::kSkip) {
368         return;
369     }
370 
371     if (src.contains(image->bounds())) {
372         constraint = SkCanvas::kFast_SrcRectConstraint;
373     }
374     // Depending on the nature of image, it can flow through more or less optimal pipelines
375     bool useDecal = mode == ImageDrawMode::kDecal;
376     bool attemptDrawTexture = !useDecal; // rtc->drawTexture() only clamps
377 
378     // Get final CTM matrix
379     SkMatrix ctm = this->ctm();
380     if (preViewMatrix) {
381         ctm.preConcat(*preViewMatrix);
382     }
383 
384     // YUVA images can be stored in multiple images with different plane resolutions, so this
385     // uses an effect to combine them dynamically on the GPU. This is done before requesting a
386     // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
387     if (as_IB(image)->isYUVA()) {
388         SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
389         LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
390 
391         GrYUVAImageTextureMaker maker(fContext.get(), image, useDecal);
392         draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
393                               paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
394                               /* attempt draw texture */ false);
395         return;
396     }
397 
398     // Pinned texture proxies can be rendered directly as textures, or with relatively simple
399     // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
400     uint32_t pinnedUniqueID;
401     if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
402                                                                           &pinnedUniqueID)) {
403         SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
404         LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
405 
406         GrColorInfo colorInfo(image->imageInfo().colorInfo());
407 
408         if (attemptDrawTexture && can_use_draw_texture(paint)) {
409             draw_texture(fRenderTargetContext.get(), this->clip(), ctm, paint, src,  dst,
410                          dstClip, aa, aaFlags, constraint, std::move(proxy), colorInfo);
411             return;
412         }
413         GrTextureAdjuster adjuster(fContext.get(), std::move(proxy), colorInfo, pinnedUniqueID,
414                                    useDecal);
415         draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
416                               paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
417                               constraint, /* attempt draw_texture */ false);
418         return;
419     }
420 
421     // Next up, try tiling the image
422     // TODO (michaelludwig): Implement this with per-edge AA flags to handle seaming properly
423     // instead of going through drawBitmapRect (which will be removed from SkDevice in the future)
424     SkBitmap bm;
425     if (this->shouldTileImage(image, &src, constraint, paint.getFilterQuality(), ctm, srcToDst)) {
426         // only support tiling as bitmap at the moment, so force raster-version
427         if (!as_IB(image)->getROPixels(&bm)) {
428             return;
429         }
430         this->drawBitmapRect(bm, &src, dst, paint, constraint);
431         return;
432     }
433 
434     // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
435     SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
436     LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
437 
438     // Lazily generated images must get drawn as a texture producer that handles the final
439     // texture creation.
440     if (image->isLazyGenerated()) {
441         GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint, useDecal);
442         draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
443                               paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
444                               attemptDrawTexture);
445         return;
446     }
447     if (as_IB(image)->getROPixels(&bm)) {
448         GrBitmapTextureMaker maker(fContext.get(), bm, useDecal);
449         draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
450                               paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
451                               attemptDrawTexture);
452     }
453 
454     // Otherwise don't know how to draw it
455 }
456 
drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)457 void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
458                                      const SkPoint dstClips[], const SkMatrix preViewMatrices[],
459                                      const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
460     SkASSERT(count > 0);
461     if (!can_use_draw_texture(paint)) {
462         // Send every entry through drawImageQuad() to handle the more complicated paint
463         int dstClipIndex = 0;
464         for (int i = 0; i < count; ++i) {
465             // Only no clip or quad clip are supported
466             SkASSERT(!set[i].fHasClip || dstClips);
467             SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
468 
469             SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
470             if (set[i].fAlpha != 1.f) {
471                 auto paintAlpha = paint.getAlphaf();
472                 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
473             }
474             // Always send GrAA::kYes to preserve seaming across tiling in MSAA
475             this->drawImageQuad(
476                     set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
477                     set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
478                     SkToGrQuadAAFlags(set[i].fAAFlags),
479                     set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
480                     *entryPaint, constraint);
481             dstClipIndex += 4 * set[i].fHasClip;
482         }
483         return;
484     }
485 
486     GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
487             GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
488     SkBlendMode mode = paint.getBlendMode();
489 
490     SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
491     // We accumulate compatible proxies until we find an an incompatible one or reach the end and
492     // issue the accumulated 'n' draws starting at 'base'.
493     int base = 0, n = 0;
494     auto draw = [&] {
495         if (n > 0) {
496             auto textureXform = GrColorSpaceXform::Make(
497                     set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
498                     fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
499             fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n,
500                                                  filter, mode, GrAA::kYes, constraint, this->ctm(),
501                                                  std::move(textureXform));
502         }
503     };
504     int dstClipIndex = 0;
505     for (int i = 0; i < count; ++i) {
506         SkASSERT(!set[i].fHasClip || dstClips);
507         SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
508 
509         // Manage the dst clip pointer tracking before any continues are used so we don't lose
510         // our place in the dstClips array.
511         const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
512         dstClipIndex += 4 * set[i].fHasClip;
513 
514         // The default SkBaseDevice implementation is based on drawImageRect which does not allow
515         // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
516         if (!set[i].fSrcRect.isSorted()) {
517             draw();
518             base = i + 1;
519             n = 0;
520             continue;
521         }
522 
523         sk_sp<GrTextureProxy> proxy;
524         const SkImage_Base* image = as_IB(set[i].fImage.get());
525         // Extract proxy from image, but skip YUV images so they get processed through
526         // drawImageQuad and the proper effect to dynamically sample their planes.
527         if (!image->isYUVA()) {
528             uint32_t uniqueID;
529             proxy = image->refPinnedTextureProxy(this->context(), &uniqueID);
530             if (!proxy) {
531                 proxy = image->asTextureProxyRef(this->context(), GrSamplerState::ClampBilerp(),
532                                                  nullptr);
533             }
534         }
535 
536         if (!proxy) {
537             // This image can't go through the texture op, send through general image pipeline
538             // after flushing current batch.
539             draw();
540             base = i + 1;
541             n = 0;
542             SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
543             if (set[i].fAlpha != 1.f) {
544                 auto paintAlpha = paint.getAlphaf();
545                 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
546             }
547             this->drawImageQuad(
548                     image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
549                     SkToGrQuadAAFlags(set[i].fAAFlags),
550                     set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
551                     *entryPaint, constraint);
552             continue;
553         }
554 
555         textures[i].fProxy = std::move(proxy);
556         textures[i].fSrcColorType = SkColorTypeToGrColorType(image->colorType());
557         textures[i].fSrcRect = set[i].fSrcRect;
558         textures[i].fDstRect = set[i].fDstRect;
559         textures[i].fDstClipQuad = clip;
560         textures[i].fPreViewMatrix =
561                 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
562         textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
563         textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
564 
565         if (n > 0 &&
566             (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(textures[i].fProxy.get(),
567                                                                  textures[base].fProxy.get()) ||
568              set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
569              !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
570             draw();
571             base = i;
572             n = 1;
573         } else {
574             ++n;
575         }
576     }
577     draw();
578 }
579 
580 // TODO (michaelludwig) - to be removed when drawBitmapRect doesn't need it anymore
drawTextureProducer(GrTextureProducer * producer,const SkRect * srcRect,const SkRect * dstRect,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,const SkPaint & paint,bool attemptDrawTexture)581 void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
582                                       const SkRect* srcRect,
583                                       const SkRect* dstRect,
584                                       SkCanvas::SrcRectConstraint constraint,
585                                       const SkMatrix& viewMatrix,
586                                       const SkPaint& paint,
587                                       bool attemptDrawTexture) {
588     // The texture refactor split the old logic of drawTextureProducer into the beginning of
589     // drawImageQuad() and into the static draw_texture_producer. Replicate necessary logic that
590     // drawImageQuad() handles.
591     SkRect src;
592     SkRect dst;
593     SkMatrix srcToDst;
594     ImageDrawMode mode = optimize_sample_area(SkISize::Make(producer->width(), producer->height()),
595                                               srcRect, dstRect, nullptr, &src, &dst, &srcToDst);
596     if (mode == ImageDrawMode::kSkip) {
597         return;
598     }
599     // There's no dstClip to worry about and the producer is already made so we wouldn't be able
600     // to tell it to use decals if we had to
601     SkASSERT(mode != ImageDrawMode::kDecal);
602 
603     draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), viewMatrix,
604                           paint, producer, src, dst, /* clip */ nullptr, srcToDst,
605                           GrAA(paint.isAntiAlias()),
606                           paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
607                           constraint, attemptDrawTexture);
608 }
609