blob: 88b88a481cef55f0b48d5f8dc840c058ea621002 [file] [log] [blame]
bsalomonc55271f2015-11-09 11:55:57 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/SkGpuDevice.h"
Michael Ludwig1433cfd2019-02-27 17:12:30 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkYUVAIndex.h"
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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#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"
Brian Salomonb8f098d2020-01-07 11:15:44 -050025#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/image/SkImage_Base.h"
bsalomonc55271f2015-11-09 11:55:57 -080028
Michael Ludwig1433cfd2019-02-27 17:12:30 -050029namespace {
30
bsalomonc55271f2015-11-09 11:55:57 -080031static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
32 return textureIsAlphaOnly && paint.getShader();
33}
34
bsalomonb1b01992015-11-18 10:56:08 -080035//////////////////////////////////////////////////////////////////////////////
36// Helper functions for dropping src rect constraint in bilerp mode.
37
38static const SkScalar kColorBleedTolerance = 0.001f;
39
40static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
41 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050042 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
43 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
44 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080045 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
46 return true;
47 }
48 return false;
49}
50
51static bool may_color_bleed(const SkRect& srcRect,
52 const SkRect& transformedRect,
53 const SkMatrix& m,
Chris Dalton6ce447a2019-06-23 18:07:38 -060054 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080055 // 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);
Chris Dalton6ce447a2019-06-23 18:07:38 -060059 if (numSamples > 1) {
bsalomonb1b01992015-11-18 10:56:08 -080060 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
80static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
81 const SkRect& srcRect,
82 const SkMatrix& srcRectToDeviceSpace,
Chris Dalton6ce447a2019-06-23 18:07:38 -060083 int numSamples) {
bsalomonb1b01992015-11-18 10:56:08 -080084 if (srcRectToDeviceSpace.rectStaysRect()) {
85 // sampling is axis-aligned
86 SkRect transformedRect;
87 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
88
89 if (has_aligned_samples(srcRect, transformedRect) ||
Chris Dalton6ce447a2019-06-23 18:07:38 -060090 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, numSamples)) {
bsalomonb1b01992015-11-18 10:56:08 -080091 return true;
92 }
93 }
94 return false;
95}
96
Michael Ludwigdd86fb32020-03-10 09:55:35 -040097//////////////////////////////////////////////////////////////////////////////
98// Helper functions for tiling a large SkBitmap
99
100static const int kBmpSmallTileSize = 1 << 10;
101
102static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
103 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
104 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
105 return tilesX * tilesY;
106}
107
108static int determine_tile_size(const SkIRect& src, int maxTileSize) {
109 if (maxTileSize <= kBmpSmallTileSize) {
110 return maxTileSize;
111 }
112
113 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
114 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
115
116 maxTileTotalTileSize *= maxTileSize * maxTileSize;
117 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
118
119 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
120 return kBmpSmallTileSize;
121 } else {
122 return maxTileSize;
123 }
124}
125
126// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
127// pixels from the bitmap are necessary.
128static void determine_clipped_src_rect(int width, int height,
129 const GrClip& clip,
130 const SkMatrix& viewMatrix,
131 const SkMatrix& srcToDstRect,
132 const SkISize& imageDimensions,
133 const SkRect* srcRectPtr,
134 SkIRect* clippedSrcIRect) {
135 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
136 SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
137 if (!inv.invert(&inv)) {
138 clippedSrcIRect->setEmpty();
139 return;
140 }
141 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
142 inv.mapRect(&clippedSrcRect);
143 if (srcRectPtr) {
144 if (!clippedSrcRect.intersect(*srcRectPtr)) {
145 clippedSrcIRect->setEmpty();
146 return;
147 }
148 }
149 clippedSrcRect.roundOut(clippedSrcIRect);
150 SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions);
151 if (!clippedSrcIRect->intersect(bmpBounds)) {
152 clippedSrcIRect->setEmpty();
153 }
154}
155
156} // temporary anonymous namespace boundary before shouldTileImageID is removed from SkGPUDevice
157
158bool SkGpuDevice::shouldTileImageID(uint32_t imageID,
159 const SkIRect& imageRect,
160 const SkMatrix& viewMatrix,
161 const SkMatrix& srcToDstRect,
162 const SkRect* srcRectPtr,
163 int maxTileSize,
164 int* tileSize,
165 SkIRect* clippedSubset) const {
166 // if it's larger than the max tile size, then we have no choice but tiling.
167 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
168 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
169 this->clip(), viewMatrix, srcToDstRect, imageRect.size(),
170 srcRectPtr, clippedSubset);
171 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
172 return true;
173 }
174
175 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
176 const size_t area = imageRect.width() * imageRect.height();
177 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
178 return false;
179 }
180
181 // At this point we know we could do the draw by uploading the entire bitmap
182 // as a texture. However, if the texture would be large compared to the
183 // cache size and we don't require most of it for this draw then tile to
184 // reduce the amount of upload and cache spill.
185
186 // assumption here is that sw bitmap size is a good proxy for its size as
187 // a texture
188 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
189 size_t cacheSize = fContext->getResourceCacheLimit();
190 if (bmpSize < cacheSize / 2) {
191 return false;
192 }
193
194 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
195 // tiling memory savings would be < 50%.
196 determine_clipped_src_rect(fRenderTargetContext->width(), fRenderTargetContext->height(),
197 this->clip(), viewMatrix, srcToDstRect, imageRect.size(), srcRectPtr,
198 clippedSubset);
199 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
200 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
201 kBmpSmallTileSize * kBmpSmallTileSize *
202 sizeof(SkPMColor); // assume 32bit pixels;
203
204 return usedTileBytes * 2 < bmpSize;
205}
206
207bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
208 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
209 const SkMatrix& viewMatrix,
210 const SkMatrix& srcToDstRect) const {
211 // If image is explicitly texture backed then we shouldn't get here.
212 SkASSERT(!image->isTextureBacked());
213
214 bool doBicubic;
215 GrSamplerState::Filter textureFilterMode = GrSkFilterQualityToGrFilterMode(
216 image->width(), image->height(), quality, viewMatrix, srcToDstRect,
217 fContext->priv().options().fSharpenMipmappedTextures, &doBicubic);
218
219 int tileFilterPad;
220 if (doBicubic) {
221 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
222 } else if (GrSamplerState::Filter::kNearest == textureFilterMode) {
223 tileFilterPad = 0;
224 } else {
225 tileFilterPad = 1;
226 }
227
228 int maxTileSize = this->caps()->maxTileSize() - 2 * tileFilterPad;
229
230 // these are output, which we safely ignore, as we just want to know the predicate
231 int outTileSize;
232 SkIRect outClippedSrcRect;
233
234 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
235 srcRectPtr, maxTileSize, &outTileSize, &outClippedSrcRect);
236}
237
238namespace {
239
240// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
241// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
242// of 'iRect' for all possible outsets/clamps.
243static inline void clamped_outset_with_offset(SkIRect* iRect,
244 int outset,
245 SkPoint* offset,
246 const SkIRect& clamp) {
247 iRect->outset(outset, outset);
248
249 int leftClampDelta = clamp.fLeft - iRect->fLeft;
250 if (leftClampDelta > 0) {
251 offset->fX -= outset - leftClampDelta;
252 iRect->fLeft = clamp.fLeft;
253 } else {
254 offset->fX -= outset;
255 }
256
257 int topClampDelta = clamp.fTop - iRect->fTop;
258 if (topClampDelta > 0) {
259 offset->fY -= outset - topClampDelta;
260 iRect->fTop = clamp.fTop;
261 } else {
262 offset->fY -= outset;
263 }
264
265 if (iRect->fRight > clamp.fRight) {
266 iRect->fRight = clamp.fRight;
267 }
268 if (iRect->fBottom > clamp.fBottom) {
269 iRect->fBottom = clamp.fBottom;
270 }
271}
272
273//////////////////////////////////////////////////////////////////////////////
274// Helper functions for drawing an image with GrRenderTargetContext
275
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500276enum class ImageDrawMode {
277 // Src and dst have been restricted to the image content. May need to clamp, no need to decal.
278 kOptimized,
279 // Src and dst are their original sizes, requires use of a decal instead of plain clamping.
280 // This is used when a dst clip is provided and extends outside of the optimized dst rect.
281 kDecal,
282 // Src or dst are empty, or do not intersect the image content so don't draw anything.
283 kSkip
284};
285
286/**
287 * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that
288 * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect
289 * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in
290 * 'srcToDst'. Outputs are not always updated when kSkip is returned.
291 *
292 * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the
293 * original src rect. 'dstClip' should be null when there is no additional clipping.
294 */
295static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect,
296 const SkRect* origDstRect, const SkPoint dstClip[4],
297 SkRect* outSrcRect, SkRect* outDstRect,
298 SkMatrix* srcToDst) {
299 SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight);
300
301 SkRect src = origSrcRect ? *origSrcRect : srcBounds;
302 SkRect dst = origDstRect ? *origDstRect : src;
303
304 if (src.isEmpty() || dst.isEmpty()) {
305 return ImageDrawMode::kSkip;
306 }
307
308 if (outDstRect) {
309 srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
310 } else {
311 srcToDst->setIdentity();
312 }
313
314 if (origSrcRect && !srcBounds.contains(src)) {
315 if (!src.intersect(srcBounds)) {
316 return ImageDrawMode::kSkip;
317 }
318 srcToDst->mapRect(&dst, src);
319
320 // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still
321 // contained in dst, otherwise cannot optimize the sample area and must use a decal instead
322 if (dstClip) {
323 for (int i = 0; i < 4; ++i) {
324 if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) {
325 // Must resort to using a decal mode restricted to the clipped 'src', and
326 // use the original dst rect (filling in src bounds as needed)
327 *outSrcRect = src;
328 *outDstRect = (origDstRect ? *origDstRect
329 : (origSrcRect ? *origSrcRect : srcBounds));
330 return ImageDrawMode::kDecal;
331 }
332 }
333 }
334 }
335
336 // The original src and dst were fully contained in the image, or there was no dst clip to
337 // worry about, or the clip was still contained in the restricted dst rect.
338 *outSrcRect = src;
339 *outDstRect = dst;
340 return ImageDrawMode::kOptimized;
341}
342
Brian Salomon34169692017-08-28 15:32:01 -0400343/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400344 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
345 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400346 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400347static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400348 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500349 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400350}
351
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500352// Assumes srcRect and dstRect have already been optimized to fit the proxy
353static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm,
354 const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect,
355 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500356 SkCanvas::SrcRectConstraint constraint, GrSurfaceProxyView view,
Greg Daniela4828a12019-10-11 13:51:02 -0400357 const GrColorInfo& srcColorInfo) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400358 const GrColorInfo& dstInfo(rtc->colorInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400359 auto textureXform =
Greg Daniela4828a12019-10-11 13:51:02 -0400360 GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(),
Brian Osman3d139a42018-11-19 10:42:10 -0500361 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400362 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400363 switch (paint.getFilterQuality()) {
364 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400365 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400366 break;
367 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400368 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400369 break;
370 case kMedium_SkFilterQuality:
371 case kHigh_SkFilterQuality:
372 SK_ABORT("Quality level not allowed.");
373 }
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500374 GrSurfaceProxy* proxy = view.proxy();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500375 // Must specify the strict constraint when the proxy is not functionally exact and the src
376 // rect would access pixels outside the proxy's content area without the constraint.
Brian Salomon5c60b752019-12-13 15:03:43 -0500377 if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500378 // Conservative estimate of how much a coord could be outset from src rect:
379 // 1/2 pixel for AA and 1/2 pixel for bilerp
380 float buffer = 0.5f * (aa == GrAA::kYes) +
381 0.5f * (filter == GrSamplerState::Filter::kBilerp);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400382 SkRect safeBounds = proxy->getBoundsRect();
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500383 safeBounds.inset(buffer, buffer);
384 if (!safeBounds.contains(srcRect)) {
385 constraint = SkCanvas::kStrict_SrcRectConstraint;
386 }
387 }
Brian Osman3d139a42018-11-19 10:42:10 -0500388 SkPMColor4f color;
Greg Daniela4828a12019-10-11 13:51:02 -0400389 if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400390 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400391 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500392 float paintAlpha = paint.getColor4f().fA;
393 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400394 }
Brian Salomon34169692017-08-28 15:32:01 -0400395
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500396 if (dstClip) {
397 // Get source coords corresponding to dstClip
398 SkPoint srcQuad[4];
399 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000400
Greg Daniel2f3cd4f2020-02-07 11:07:25 -0500401 rtc->drawTextureQuad(clip, std::move(view), srcColorInfo.colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -0500402 srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad,
403 dstClip, aa, aaFlags,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500404 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
405 ctm, std::move(textureXform));
406 } else {
Greg Daniel40903af2020-01-30 14:55:05 -0500407 rtc->drawTexture(clip, std::move(view), srcColorInfo.alphaType(), filter,
408 paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint,
409 ctm, std::move(textureXform));
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000410 }
Michael Ludwig24adb3a2019-02-27 19:42:20 +0000411}
412
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500413// Assumes srcRect and dstRect have already been optimized to fit the proxy.
Brian Salomon777e1462020-02-28 21:10:31 -0500414static void draw_texture_producer(GrContext* context,
415 GrRenderTargetContext* rtc,
416 const GrClip& clip,
417 const SkMatrix& ctm,
418 const SkPaint& paint,
419 GrTextureProducer* producer,
420 const SkRect& src,
421 const SkRect& dst,
422 const SkPoint dstClip[4],
423 const SkMatrix& srcToDst,
424 GrAA aa,
425 GrQuadAAFlags aaFlags,
426 SkCanvas::SrcRectConstraint constraint,
427 GrSamplerState::WrapMode wm) {
428 if (wm == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() &&
429 can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400430 // We've done enough checks above to allow us to pass ClampNearest() and not check for
431 // scaling adjustments.
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500432 auto view = producer->view(GrMipMapped::kNo);
Greg Danielcc21d0c2020-02-05 16:58:40 -0500433 if (!view) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400434 return;
435 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500436
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500437 draw_texture(
438 rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint, std::move(view),
439 {producer->colorType(), producer->alphaType(), sk_ref_sp(producer->colorSpace())});
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400440 return;
441 }
442
bsalomonc55271f2015-11-09 11:55:57 -0800443 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500444
bsalomonc55271f2015-11-09 11:55:57 -0800445 // The shader expects proper local coords, so we can't replace local coords with texture coords
446 // if the shader will be used. If we have a mask filter we will change the underlying geometry
447 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800448 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800449
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500450 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500451 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500452 // FP. In the future this should be an opaque optimization enabled by the combination of
453 // GrDrawOp/GP and FP.
454 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
455 mf = nullptr;
456 }
bsalomonc55271f2015-11-09 11:55:57 -0800457 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400458 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
Chris Dalton309c6c02019-08-13 10:32:47 -0600459 producer->width(), producer->height(), paint.getFilterQuality(), ctm, srcToDst,
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500460 context->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400461 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800462
Brian Osmane8e54582016-11-28 10:06:27 -0500463 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800464 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
465 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
466 } else {
467 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
468 }
halcanary9d524f22016-03-29 09:03:52 -0700469
bsalomonc55271f2015-11-09 11:55:57 -0800470 // If we have to outset for AA then we will generate texture coords outside the src rect. The
471 // same happens for any mask filter that extends the bounds rendered in the dst.
472 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500473 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800474
bsalomonb1b01992015-11-18 10:56:08 -0800475 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400476 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400477 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect &&
Brian Salomon777e1462020-02-28 21:10:31 -0500478 !producer->isPlanar()) {
bsalomonb1b01992015-11-18 10:56:08 -0800479 SkMatrix combinedMatrix;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500480 combinedMatrix.setConcat(ctm, srcToDst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600481 if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) {
bsalomonb1b01992015-11-18 10:56:08 -0800482 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
483 }
484 }
485
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500486 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800487 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500488 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800489 } else {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500490 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800491 return;
492 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800493 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500494 auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
Brian Salomon777e1462020-02-28 21:10:31 -0500495 coordsAllInsideSrcRect, wm, wm, filterMode);
Brian Osman05c8f462018-10-22 17:13:36 -0400496 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400497 rtc->colorInfo().colorSpace());
bsalomonc55271f2015-11-09 11:55:57 -0800498 if (!fp) {
499 return;
500 }
joshualitt33a5fce2015-11-18 13:28:51 -0800501
bsalomonc55271f2015-11-09 11:55:57 -0800502 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400503 if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, ctm, std::move(fp),
504 producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800505 return;
506 }
507
508 if (!mf) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500509 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
510 if (dstClip) {
511 SkPoint srcClipPoints[4];
512 SkPoint* srcClip = nullptr;
513 if (canUseTextureCoordsAsLocalCoords) {
514 // Calculate texture coordinates that match the dst clip
515 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
516 srcClip = srcClipPoints;
517 }
518 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
519 } else {
520 // Provide explicit texture coords when possible, otherwise rely on texture matrix
521 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
522 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
523 }
524 } else {
525 // Must draw the mask filter as a GrShape. For now, this loses the per-edge AA information
526 // since it always draws with AA, but that is should not be noticeable since the mask filter
527 // is probably a blur.
528 GrShape shape;
529 if (dstClip) {
530 // Represent it as an SkPath formed from the dstClip
531 SkPath path;
532 path.addPoly(dstClip, 4, true);
533 shape = GrShape(path);
534 } else {
535 shape = GrShape(dst);
536 }
537
538 GrBlurUtils::drawShapeWithMaskFilter(
539 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
540 }
541}
542
543} // anonymous namespace
544
Michael Ludwigdd86fb32020-03-10 09:55:35 -0400545// Break 'bitmap' into several tiles to draw it since it has already
546// been determined to be too large to fit in VRAM
547void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
548 const SkMatrix& viewMatrix,
549 const SkMatrix& dstMatrix,
550 const SkRect& srcRect,
551 const SkIRect& clippedSrcIRect,
552 GrSamplerState::Filter filter,
553 const SkPaint& origPaint,
554 SkCanvas::SrcRectConstraint constraint,
555 int tileSize,
556 bool bicubic) {
557 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
558 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
559 LogDrawScaleFactor(viewMatrix, SkMatrix::I(), origPaint.getFilterQuality());
560
561 const SkPaint* paint = &origPaint;
562 SkPaint tempPaint;
563 if (origPaint.isAntiAlias() && fRenderTargetContext->numSamples() <= 1) {
564 // Drop antialiasing to avoid seams at tile boundaries.
565 tempPaint = origPaint;
566 tempPaint.setAntiAlias(false);
567 paint = &tempPaint;
568 }
569 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
570
571 int nx = bitmap.width() / tileSize;
572 int ny = bitmap.height() / tileSize;
573 for (int x = 0; x <= nx; x++) {
574 for (int y = 0; y <= ny; y++) {
575 SkRect tileR;
576 tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize),
577 SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize));
578
579 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
580 continue;
581 }
582
583 if (!tileR.intersect(srcRect)) {
584 continue;
585 }
586
587 SkIRect iTileR;
588 tileR.roundOut(&iTileR);
589 SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
590 SkIntToScalar(iTileR.fTop));
591 SkRect rectToDraw = tileR;
592 dstMatrix.mapRect(&rectToDraw);
593 if (filter != GrSamplerState::Filter::kNearest || bicubic) {
594 SkIRect iClampRect;
595
596 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
597 // In bleed mode we want to always expand the tile on all edges
598 // but stay within the bitmap bounds
599 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
600 } else {
601 // In texture-domain/clamp mode we only want to expand the
602 // tile on edges interior to "srcRect" (i.e., we want to
603 // not bleed across the original clamped edges)
604 srcRect.roundOut(&iClampRect);
605 }
606 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
607 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
608 }
609
610 SkBitmap tmpB;
611 if (bitmap.extractSubset(&tmpB, iTileR)) {
612 // now offset it to make it "local" to our tmp bitmap
613 tileR.offset(-offset.fX, -offset.fY);
614 // de-optimized this determination
615 bool needsTextureDomain = true;
616 this->drawBitmapTile(tmpB,
617 viewMatrix,
618 rectToDraw,
619 tileR,
620 filter,
621 *paint,
622 constraint,
623 bicubic,
624 needsTextureDomain);
625 }
626 }
627 }
628}
629
630void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
631 const SkMatrix& viewMatrix,
632 const SkRect& dstRect,
633 const SkRect& srcRect,
634 GrSamplerState::Filter filter,
635 const SkPaint& paint,
636 SkCanvas::SrcRectConstraint constraint,
637 bool bicubic,
638 bool needsTextureDomain) {
639 // We should have already handled bitmaps larger than the max texture size.
640 SkASSERT(bitmap.width() <= this->caps()->maxTextureSize() &&
641 bitmap.height() <= this->caps()->maxTextureSize());
642 // We should be respecting the max tile size by the time we get here.
643 SkASSERT(bitmap.width() <= this->caps()->maxTileSize() &&
644 bitmap.height() <= this->caps()->maxTileSize());
645
646 GrMipMapped mipMapped = filter == GrSamplerState::Filter::kMipMap ? GrMipMapped::kYes
647 : GrMipMapped::kNo;
648 GrSurfaceProxyView view = GrRefCachedBitmapView(fContext.get(), bitmap, mipMapped);
649 if (!view) {
650 return;
651 }
652
653 // Compute a matrix that maps the rect we will draw to the src rect.
654 SkMatrix texMatrix = SkMatrix::MakeRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
655
656 SkAlphaType srcAlphaType = bitmap.alphaType();
657
658 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
659 // the rest from the SkPaint.
660 std::unique_ptr<GrFragmentProcessor> fp;
661
662 const auto& caps = *this->caps();
663 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
664 if (bicubic) {
665 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
666 fp = GrBicubicEffect::MakeSubset(std::move(view), srcAlphaType, texMatrix,
667 GrSamplerState::WrapMode::kClamp,
668 GrSamplerState::WrapMode::kClamp, srcRect, kDir, caps);
669 } else {
670 fp = GrTextureEffect::MakeSubset(std::move(view), srcAlphaType, texMatrix, filter,
671 srcRect, caps);
672 }
673 } else if (bicubic) {
674 SkASSERT(GrSamplerState::Filter::kNearest == filter);
675 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
676 fp = GrBicubicEffect::Make(std::move(view), srcAlphaType, texMatrix, kDir);
677 } else {
678 fp = GrTextureEffect::Make(std::move(view), srcAlphaType, texMatrix, filter);
679 }
680
681 fp = GrColorSpaceXformEffect::Make(std::move(fp), bitmap.colorSpace(), bitmap.alphaType(),
682 fRenderTargetContext->colorInfo().colorSpace());
683 GrPaint grPaint;
684 if (!SkPaintToGrPaintWithTexture(this->context(), fRenderTargetContext->colorInfo(), paint,
685 viewMatrix, std::move(fp),
686 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
687 return;
688 }
689
690 // Coverage-based AA would cause seams between tiles.
691 GrAA aa = GrAA(paint.isAntiAlias() && fRenderTargetContext->numSamples() > 1);
692 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, dstRect);
693}
694
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500695//////////////////////////////////////////////////////////////////////////////
696
697void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
698 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500699 const SkMatrix* preViewMatrix, const SkPaint& paint,
700 SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500701 SkRect src;
702 SkRect dst;
703 SkMatrix srcToDst;
704 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
705 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
706 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800707 return;
708 }
709
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500710 if (src.contains(image->bounds())) {
711 constraint = SkCanvas::kFast_SrcRectConstraint;
712 }
713 // Depending on the nature of image, it can flow through more or less optimal pipelines
Brian Salomon777e1462020-02-28 21:10:31 -0500714 GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal
715 ? GrSamplerState::WrapMode::kClampToBorder
716 : GrSamplerState::WrapMode::kClamp;
Robert Phillips27927a52018-08-20 13:18:12 -0400717
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500718 // Get final CTM matrix
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400719 SkMatrix ctm = this->localToDevice();
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500720 if (preViewMatrix) {
721 ctm.preConcat(*preViewMatrix);
722 }
723
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500724 // YUVA images can be stored in multiple images with different plane resolutions, so this
725 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
726 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
727 if (as_IB(image)->isYUVA()) {
728 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500729 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500730
Brian Salomon777e1462020-02-28 21:10:31 -0500731 GrYUVAImageTextureMaker maker(fContext.get(), image);
732 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm, paint,
733 &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
734 wrapMode);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500735 return;
736 }
737
738 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
739 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
740 uint32_t pinnedUniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500741 if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500742 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500743 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500744
Greg Daniel82c6b102020-01-21 10:33:22 -0500745 GrColorInfo colorInfo;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500746 if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500747 SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
748 colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
749 image->imageInfo().refColorSpace());
750 } else {
751 colorInfo = GrColorInfo(image->imageInfo().colorInfo());
752 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500753
Brian Salomon777e1462020-02-28 21:10:31 -0500754 GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID);
755 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm, paint,
756 &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
757 wrapMode);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500758 return;
759 }
760
761 // Next up, try tiling the image
762 // TODO (michaelludwig): Implement this with per-edge AA flags to handle seaming properly
763 // instead of going through drawBitmapRect (which will be removed from SkDevice in the future)
764 SkBitmap bm;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500765 if (this->shouldTileImage(image, &src, constraint, paint.getFilterQuality(), ctm, srcToDst)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500766 // only support tiling as bitmap at the moment, so force raster-version
Jim Van Verth352f4f72019-10-29 15:26:00 -0400767 if (as_IB(image)->getROPixels(&bm)) {
768 this->drawBitmapRect(bm, &src, dst, paint, constraint);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500769 return;
770 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500771 }
772
773 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
774 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500775 LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500776
777 // Lazily generated images must get drawn as a texture producer that handles the final
778 // texture creation.
779 if (image->isLazyGenerated()) {
Brian Salomon777e1462020-02-28 21:10:31 -0500780 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint);
781 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm, paint,
782 &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
783 wrapMode);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500784 return;
785 }
786 if (as_IB(image)->getROPixels(&bm)) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500787 GrBitmapTextureMaker maker(fContext.get(), bm, GrBitmapTextureMaker::Cached::kYes,
Brian Salomon777e1462020-02-28 21:10:31 -0500788 SkBackingFit::kExact);
789 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm, paint,
790 &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
791 wrapMode);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500792 }
793
794 // Otherwise don't know how to draw it
795}
796
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400797void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count,
798 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
799 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500800 SkASSERT(count > 0);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400801 if (!can_use_draw_texture(paint)) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500802 // Send every entry through drawImageQuad() to handle the more complicated paint
803 int dstClipIndex = 0;
804 for (int i = 0; i < count; ++i) {
805 // Only no clip or quad clip are supported
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400806 SkASSERT(!set[i].fHasClip || dstClips);
807 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500808
Brian Salomondb151e02019-09-17 12:11:16 -0400809 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
810 if (set[i].fAlpha != 1.f) {
811 auto paintAlpha = paint.getAlphaf();
812 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
813 }
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500814 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
Brian Salomondb151e02019-09-17 12:11:16 -0400815 this->drawImageQuad(
816 set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
817 set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes,
818 SkToGrQuadAAFlags(set[i].fAAFlags),
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400819 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400820 *entryPaint, constraint);
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400821 dstClipIndex += 4 * set[i].fHasClip;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500822 }
823 return;
824 }
825
826 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
827 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
828 SkBlendMode mode = paint.getBlendMode();
829
830 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
831 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
Michael Ludwig379e4962019-12-06 13:21:26 -0500832 // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy
833 // switches that occur within the 'n' entries.
834 int base = 0, n = 0, p = 0;
835 auto draw = [&](int nextBase) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500836 if (n > 0) {
837 auto textureXform = GrColorSpaceXform::Make(
838 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400839 fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType);
Michael Ludwig379e4962019-12-06 13:21:26 -0500840 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p,
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400841 filter, mode, GrAA::kYes, constraint,
842 this->localToDevice(), std::move(textureXform));
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500843 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500844 base = nextBase;
845 n = 0;
846 p = 0;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500847 };
848 int dstClipIndex = 0;
849 for (int i = 0; i < count; ++i) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400850 SkASSERT(!set[i].fHasClip || dstClips);
851 SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices);
852
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500853 // Manage the dst clip pointer tracking before any continues are used so we don't lose
854 // our place in the dstClips array.
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400855 const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr;
856 dstClipIndex += 4 * set[i].fHasClip;
857
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500858 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
859 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
860 if (!set[i].fSrcRect.isSorted()) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500861 draw(i + 1);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500862 continue;
863 }
864
Greg Danielcc21d0c2020-02-05 16:58:40 -0500865 GrSurfaceProxyView view;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400866 const SkImage_Base* image = as_IB(set[i].fImage.get());
Greg Danielcc21d0c2020-02-05 16:58:40 -0500867 // Extract view from image, but skip YUV images so they get processed through
Michael Ludwigd9958f82019-03-21 13:08:36 -0400868 // drawImageQuad and the proper effect to dynamically sample their planes.
869 if (!image->isYUVA()) {
870 uint32_t uniqueID;
Greg Danielcc21d0c2020-02-05 16:58:40 -0500871 view = image->refPinnedView(this->context(), &uniqueID);
872 if (!view) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500873 view = image->refView(this->context(), GrMipMapped::kNo);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500874 }
875 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500876
Greg Danielcc21d0c2020-02-05 16:58:40 -0500877 if (!view) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400878 // This image can't go through the texture op, send through general image pipeline
879 // after flushing current batch.
Michael Ludwig379e4962019-12-06 13:21:26 -0500880 draw(i + 1);
Brian Salomondb151e02019-09-17 12:11:16 -0400881 SkTCopyOnFirstWrite<SkPaint> entryPaint(paint);
882 if (set[i].fAlpha != 1.f) {
883 auto paintAlpha = paint.getAlphaf();
884 entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha);
885 }
886 this->drawImageQuad(
887 image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes,
Michael Ludwigd9958f82019-03-21 13:08:36 -0400888 SkToGrQuadAAFlags(set[i].fAAFlags),
889 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex,
Brian Salomondb151e02019-09-17 12:11:16 -0400890 *entryPaint, constraint);
Michael Ludwigd9958f82019-03-21 13:08:36 -0400891 continue;
892 }
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500893
Greg Danielcc21d0c2020-02-05 16:58:40 -0500894 textures[i].fProxyView = std::move(view);
Brian Salomonfc118442019-11-22 19:09:27 -0500895 textures[i].fSrcAlphaType = image->alphaType();
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500896 textures[i].fSrcRect = set[i].fSrcRect;
897 textures[i].fDstRect = set[i].fDstRect;
898 textures[i].fDstClipQuad = clip;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400899 textures[i].fPreViewMatrix =
900 set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500901 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
902 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
903
904 if (n > 0 &&
Greg Daniel549325c2019-10-30 16:19:20 -0400905 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500906 textures[i].fProxyView.proxy(),
907 textures[base].fProxyView.proxy()) ||
Greg Daniel507736f2020-01-17 15:36:10 -0500908 textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() ||
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500909 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
910 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500911 draw(i);
912 }
913 // Whether or not we submitted a draw in the above if(), this ith entry is in the current
914 // set being accumulated so increment n, and increment p if proxies are different.
915 ++n;
916 if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) {
917 // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up
918 // to i - 1).
919 ++p;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500920 }
921 }
Michael Ludwig379e4962019-12-06 13:21:26 -0500922 draw(count);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500923}
924
925// TODO (michaelludwig) - to be removed when drawBitmapRect doesn't need it anymore
926void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
927 const SkRect* srcRect,
928 const SkRect* dstRect,
929 SkCanvas::SrcRectConstraint constraint,
930 const SkMatrix& viewMatrix,
Brian Salomon777e1462020-02-28 21:10:31 -0500931 const SkPaint& paint) {
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500932 // The texture refactor split the old logic of drawTextureProducer into the beginning of
933 // drawImageQuad() and into the static draw_texture_producer. Replicate necessary logic that
934 // drawImageQuad() handles.
935 SkRect src;
936 SkRect dst;
937 SkMatrix srcToDst;
Brian Salomon1a217eb2019-10-24 10:50:36 -0400938 ImageDrawMode mode = optimize_sample_area(producer->dimensions(), srcRect, dstRect, nullptr,
939 &src, &dst, &srcToDst);
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500940 if (mode == ImageDrawMode::kSkip) {
941 return;
942 }
943 // There's no dstClip to worry about and the producer is already made so we wouldn't be able
944 // to tell it to use decals if we had to
945 SkASSERT(mode != ImageDrawMode::kDecal);
946
947 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), viewMatrix,
948 paint, producer, src, dst, /* clip */ nullptr, srcToDst,
949 GrAA(paint.isAntiAlias()),
950 paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
Brian Salomon777e1462020-02-28 21:10:31 -0500951 constraint, GrSamplerState::WrapMode::kClamp);
bsalomonc55271f2015-11-09 11:55:57 -0800952}