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