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