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