blob: a891c03d4adfb677b93b38daba5f19662701adc1 [file] [log] [blame]
bsalomonc55271f2015-11-09 11:55:57 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
bsalomonc55271f2015-11-09 11:55:57 -08009#include "GrBlurUtils.h"
10#include "GrCaps.h"
Brian Osman1cb41712017-10-19 12:54:52 -040011#include "GrColorSpaceXform.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
Robert Phillips27927a52018-08-20 13:18:12 -040013#include "GrShape.h"
bsalomon6663acf2016-05-10 09:14:17 -070014#include "GrStyle.h"
Brian Osmane8e54582016-11-28 10:06:27 -050015#include "GrTextureAdjuster.h"
Brian Salomon34169692017-08-28 15:32:01 -040016#include "GrTextureMaker.h"
bsalomonc55271f2015-11-09 11:55:57 -080017#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050018#include "SkGr.h"
Mike Reed80747ef2018-01-23 15:29:32 -050019#include "SkMaskFilterBase.h"
bsalomonc55271f2015-11-09 11:55:57 -080020#include "effects/GrBicubicEffect.h"
21#include "effects/GrSimpleTextureEffect.h"
22#include "effects/GrTextureDomain.h"
23
24static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
25 return textureIsAlphaOnly && paint.getShader();
26}
27
bsalomonb1b01992015-11-18 10:56:08 -080028//////////////////////////////////////////////////////////////////////////////
29// Helper functions for dropping src rect constraint in bilerp mode.
30
31static const SkScalar kColorBleedTolerance = 0.001f;
32
33static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
34 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050035 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
36 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
37 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080038 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
39 return true;
40 }
41 return false;
42}
43
44static bool may_color_bleed(const SkRect& srcRect,
45 const SkRect& transformedRect,
46 const SkMatrix& m,
Brian Salomon7c8460e2017-05-12 11:36:10 -040047 GrFSAAType fsaaType) {
bsalomonb1b01992015-11-18 10:56:08 -080048 // Only gets called if has_aligned_samples returned false.
49 // So we can assume that sampling is axis aligned but not texel aligned.
50 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
51 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
Brian Salomon7c8460e2017-05-12 11:36:10 -040052 if (GrFSAAType::kUnifiedMSAA == fsaaType) {
bsalomonb1b01992015-11-18 10:56:08 -080053 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
54 } else {
55 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
56 }
57 m.mapRect(&innerTransformedRect, innerSrcRect);
58
59 // The gap between outerTransformedRect and innerTransformedRect
60 // represents the projection of the source border area, which is
61 // problematic for color bleeding. We must check whether any
62 // destination pixels sample the border area.
63 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
64 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
65 SkIRect outer, inner;
66 outerTransformedRect.round(&outer);
67 innerTransformedRect.round(&inner);
68 // If the inner and outer rects round to the same result, it means the
69 // border does not overlap any pixel centers. Yay!
70 return inner != outer;
71}
72
73static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
74 const SkRect& srcRect,
75 const SkMatrix& srcRectToDeviceSpace,
Brian Salomon7c8460e2017-05-12 11:36:10 -040076 GrFSAAType fsaaType) {
bsalomonb1b01992015-11-18 10:56:08 -080077 if (srcRectToDeviceSpace.rectStaysRect()) {
78 // sampling is axis-aligned
79 SkRect transformedRect;
80 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
81
82 if (has_aligned_samples(srcRect, transformedRect) ||
Brian Salomon7c8460e2017-05-12 11:36:10 -040083 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, fsaaType)) {
bsalomonb1b01992015-11-18 10:56:08 -080084 return true;
85 }
86 }
87 return false;
88}
89
Brian Salomon34169692017-08-28 15:32:01 -040090/**
Brian Salomonb80ffee2018-05-23 16:39:39 -040091 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
92 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -040093 */
Brian Salomonb80ffee2018-05-23 16:39:39 -040094static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -040095 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Brian Salomonb5ef1f92018-01-11 11:46:21 -050096 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality &&
Brian Salomonb80ffee2018-05-23 16:39:39 -040097 paint.getBlendMode() == SkBlendMode::kSrcOver);
Brian Salomon34169692017-08-28 15:32:01 -040098}
99
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400100static void draw_texture(const SkPaint& paint, const SkMatrix& ctm, const SkRect* src,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400101 const SkRect* dst, GrAA aa, SkCanvas::SrcRectConstraint constraint,
Brian Osman21fc5ce2018-08-27 20:36:19 +0000102 sk_sp<GrTextureProxy> proxy, SkAlphaType alphaType,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400103 SkColorSpace* colorSpace, const GrClip& clip, GrRenderTargetContext* rtc) {
Brian Salomon34169692017-08-28 15:32:01 -0400104 SkASSERT(!(SkToBool(src) && !SkToBool(dst)));
105 SkRect srcRect = src ? *src : SkRect::MakeWH(proxy->width(), proxy->height());
106 SkRect dstRect = dst ? *dst : srcRect;
107 if (src && !SkRect::MakeIWH(proxy->width(), proxy->height()).contains(srcRect)) {
108 // Shrink the src rect to be within bounds and proportionately shrink the dst rect.
109 SkMatrix srcToDst;
110 srcToDst.setRectToRect(srcRect, dstRect, SkMatrix::kFill_ScaleToFit);
111 SkAssertResult(srcRect.intersect(SkRect::MakeIWH(proxy->width(), proxy->height())));
112 srcToDst.mapRect(&dstRect, srcRect);
113 }
Brian Osman3d139a42018-11-19 10:42:10 -0500114 const GrColorSpaceInfo& dstInfo(rtc->colorSpaceInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400115 auto textureXform =
Brian Osman3d139a42018-11-19 10:42:10 -0500116 GrColorSpaceXform::Make(colorSpace , alphaType,
117 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400118 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400119 switch (paint.getFilterQuality()) {
120 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400121 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400122 break;
123 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400125 break;
126 case kMedium_SkFilterQuality:
127 case kHigh_SkFilterQuality:
128 SK_ABORT("Quality level not allowed.");
129 }
Brian Osman3d139a42018-11-19 10:42:10 -0500130 SkPMColor4f color;
Brian Osman3ebd3542018-07-30 14:36:53 -0400131 if (GrPixelConfigIsAlphaOnly(proxy->config())) {
Brian Osman3d139a42018-11-19 10:42:10 -0500132 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo, *rtc->caps()).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400133 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500134 float paintAlpha = paint.getColor4f().fA;
135 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400136 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400137 GrQuadAAFlags aaFlags = aa == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
138 rtc->drawTexture(clip, std::move(proxy), filter, color, srcRect, dstRect, aaFlags, constraint,
Brian Osman3d139a42018-11-19 10:42:10 -0500139 ctm, std::move(textureXform));
Brian Salomon34169692017-08-28 15:32:01 -0400140}
141
bsalomonb1b01992015-11-18 10:56:08 -0800142//////////////////////////////////////////////////////////////////////////////
143
Brian Salomon34169692017-08-28 15:32:01 -0400144void SkGpuDevice::drawPinnedTextureProxy(sk_sp<GrTextureProxy> proxy, uint32_t pinnedUniqueID,
145 SkColorSpace* colorSpace, SkAlphaType alphaType,
146 const SkRect* srcRect, const SkRect* dstRect,
147 SkCanvas::SrcRectConstraint constraint,
148 const SkMatrix& viewMatrix, const SkPaint& paint) {
Brian Salomon4e6cf912018-01-17 09:27:35 -0500149 GrAA aa = GrAA(paint.isAntiAlias());
Brian Salomonb80ffee2018-05-23 16:39:39 -0400150 if (can_use_draw_texture(paint)) {
151 draw_texture(paint, viewMatrix, srcRect, dstRect, aa, constraint, std::move(proxy),
Brian Osman21fc5ce2018-08-27 20:36:19 +0000152 alphaType, colorSpace, this->clip(), fRenderTargetContext.get());
Brian Salomon34169692017-08-28 15:32:01 -0400153 return;
154 }
Greg Danielc77085d2017-11-01 16:38:48 -0400155 GrTextureAdjuster adjuster(this->context(), std::move(proxy), alphaType, pinnedUniqueID,
156 colorSpace);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400157 this->drawTextureProducer(&adjuster, srcRect, dstRect, constraint, viewMatrix, paint, false);
Brian Salomon34169692017-08-28 15:32:01 -0400158}
159
bsalomonb1b01992015-11-18 10:56:08 -0800160void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
bsalomonc55271f2015-11-09 11:55:57 -0800161 const SkRect* srcRect,
162 const SkRect* dstRect,
163 SkCanvas::SrcRectConstraint constraint,
164 const SkMatrix& viewMatrix,
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400165 const SkPaint& paint,
166 bool attemptDrawTexture) {
167 if (attemptDrawTexture && can_use_draw_texture(paint)) {
168 GrAA aa = GrAA(paint.isAntiAlias());
169 // We've done enough checks above to allow us to pass ClampNearest() and not check for
170 // scaling adjustments.
171 auto proxy = producer->refTextureProxyForParams(GrSamplerState::ClampNearest(), nullptr);
172 if (!proxy) {
173 return;
174 }
175 draw_texture(paint, viewMatrix, srcRect, dstRect, aa, constraint, std::move(proxy),
176 producer->alphaType(), producer->colorSpace(), this->clip(),
177 fRenderTargetContext.get());
178 return;
179 }
180
ericrk369e9372016-02-05 15:32:36 -0800181 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
182 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
183
bsalomonc55271f2015-11-09 11:55:57 -0800184 // Figure out the actual dst and src rect by clipping the src rect to the bounds of the
185 // adjuster. If the src rect is clipped then the dst rect must be recomputed. Also determine
186 // the matrix that maps the src rect to the dst rect.
187 SkRect clippedSrcRect;
188 SkRect clippedDstRect;
bsalomonb1b01992015-11-18 10:56:08 -0800189 const SkRect srcBounds = SkRect::MakeIWH(producer->width(), producer->height());
bsalomonc55271f2015-11-09 11:55:57 -0800190 SkMatrix srcToDstMatrix;
191 if (srcRect) {
192 if (!dstRect) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800193 dstRect = &srcBounds;
bsalomonc55271f2015-11-09 11:55:57 -0800194 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800195 if (!srcBounds.contains(*srcRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800196 clippedSrcRect = *srcRect;
bsalomon3aa5fce2015-11-12 09:59:44 -0800197 if (!clippedSrcRect.intersect(srcBounds)) {
bsalomonc55271f2015-11-09 11:55:57 -0800198 return;
199 }
200 if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) {
201 return;
202 }
203 srcToDstMatrix.mapRect(&clippedDstRect, clippedSrcRect);
204 } else {
205 clippedSrcRect = *srcRect;
206 clippedDstRect = *dstRect;
207 if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) {
208 return;
209 }
210 }
211 } else {
bsalomon3aa5fce2015-11-12 09:59:44 -0800212 clippedSrcRect = srcBounds;
bsalomonc55271f2015-11-09 11:55:57 -0800213 if (dstRect) {
214 clippedDstRect = *dstRect;
bsalomon3aa5fce2015-11-12 09:59:44 -0800215 if (!srcToDstMatrix.setRectToRect(srcBounds, *dstRect, SkMatrix::kFill_ScaleToFit)) {
bsalomonc55271f2015-11-09 11:55:57 -0800216 return;
217 }
218 } else {
bsalomon3aa5fce2015-11-12 09:59:44 -0800219 clippedDstRect = srcBounds;
bsalomonc55271f2015-11-09 11:55:57 -0800220 srcToDstMatrix.reset();
221 }
222 }
223
ericrk983294f2016-04-18 09:14:00 -0700224 // Now that we have both the view and srcToDst matrices, log our scale factor.
225 LogDrawScaleFactor(SkMatrix::Concat(viewMatrix, srcToDstMatrix), paint.getFilterQuality());
226
bsalomonf1ecd212015-12-09 17:06:02 -0800227 this->drawTextureProducerImpl(producer, clippedSrcRect, clippedDstRect, constraint, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400228 srcToDstMatrix, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800229}
230
bsalomonb1b01992015-11-18 10:56:08 -0800231void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer,
bsalomonc55271f2015-11-09 11:55:57 -0800232 const SkRect& clippedSrcRect,
233 const SkRect& clippedDstRect,
234 SkCanvas::SrcRectConstraint constraint,
235 const SkMatrix& viewMatrix,
236 const SkMatrix& srcToDstMatrix,
bsalomonc55271f2015-11-09 11:55:57 -0800237 const SkPaint& paint) {
Brian Salomon09d994e2016-12-21 11:14:46 -0500238 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
239 // combining by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture
240 // FP. In the future this should be an opaque optimization enabled by the combination of
241 // GrDrawOp/GP and FP.
bsalomonc55271f2015-11-09 11:55:57 -0800242 const SkMaskFilter* mf = paint.getMaskFilter();
Mike Reed547c8592018-02-05 15:59:23 -0500243 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
244 mf = nullptr;
245 }
bsalomonc55271f2015-11-09 11:55:57 -0800246 // The shader expects proper local coords, so we can't replace local coords with texture coords
247 // if the shader will be used. If we have a mask filter we will change the underlying geometry
248 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800249 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800250
251 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400252 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
Brian Osmandb78cba2018-02-15 10:09:48 -0500253 paint.getFilterQuality(), viewMatrix, srcToDstMatrix,
254 fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400255 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800256
Brian Osmane8e54582016-11-28 10:06:27 -0500257 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800258 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
259 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
260 } else {
261 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
262 }
halcanary9d524f22016-03-29 09:03:52 -0700263
bsalomonc55271f2015-11-09 11:55:57 -0800264 // If we have to outset for AA then we will generate texture coords outside the src rect. The
265 // same happens for any mask filter that extends the bounds rendered in the dst.
266 // This is conservative as a mask filter does not have to expand the bounds rendered.
267 bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf;
268
bsalomonb1b01992015-11-18 10:56:08 -0800269 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400270 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
bsalomonb1b01992015-11-18 10:56:08 -0800271 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) {
272 SkMatrix combinedMatrix;
273 combinedMatrix.setConcat(viewMatrix, srcToDstMatrix);
274 if (can_ignore_bilerp_constraint(*producer, clippedSrcRect, combinedMatrix,
Brian Salomon7c8460e2017-05-12 11:36:10 -0400275 fRenderTargetContext->fsaaType())) {
bsalomonb1b01992015-11-18 10:56:08 -0800276 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
277 }
278 }
279
bsalomon3aa5fce2015-11-12 09:59:44 -0800280 const SkMatrix* textureMatrix;
281 SkMatrix tempMatrix;
282 if (canUseTextureCoordsAsLocalCoords) {
283 textureMatrix = &SkMatrix::I();
284 } else {
285 if (!srcToDstMatrix.invert(&tempMatrix)) {
286 return;
287 }
288 textureMatrix = &tempMatrix;
289 }
Brian Osman05c8f462018-10-22 17:13:36 -0400290 auto fp = producer->createFragmentProcessor(*textureMatrix, clippedSrcRect, constraintMode,
291 coordsAllInsideSrcRect, filterMode);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500292 SkColorSpace* rtColorSpace = fRenderTargetContext->colorSpaceInfo().colorSpace();
293 SkColorSpace* targetColorSpace = producer->targetColorSpace();
294 SkColorSpace* dstColorSpace = SkToBool(rtColorSpace) ? rtColorSpace : targetColorSpace;
Brian Osman05c8f462018-10-22 17:13:36 -0400295 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500296 dstColorSpace);
bsalomonc55271f2015-11-09 11:55:57 -0800297 if (!fp) {
298 return;
299 }
joshualitt33a5fce2015-11-18 13:28:51 -0800300
bsalomonc55271f2015-11-09 11:55:57 -0800301 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400302 if (!SkPaintToGrPaintWithTexture(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint,
303 viewMatrix, std::move(fp), producer->isAlphaOnly(),
304 &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800305 return;
306 }
Chris Dalton3b51df12017-11-27 14:33:06 -0700307 GrAA aa = GrAA(paint.isAntiAlias());
bsalomonc55271f2015-11-09 11:55:57 -0800308 if (canUseTextureCoordsAsLocalCoords) {
Brian Salomon34169692017-08-28 15:32:01 -0400309 fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint), aa, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500310 clippedDstRect, clippedSrcRect);
bsalomonc55271f2015-11-09 11:55:57 -0800311 return;
312 }
313
314 if (!mf) {
Brian Salomon34169692017-08-28 15:32:01 -0400315 fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix,
316 clippedDstRect);
bsalomonc55271f2015-11-09 11:55:57 -0800317 return;
318 }
319
Robert Phillips27927a52018-08-20 13:18:12 -0400320 GrShape shape(clippedDstRect, GrStyle::SimpleFill());
321
322 GrBlurUtils::drawShapeWithMaskFilter(this->context(), fRenderTargetContext.get(), this->clip(),
Robert Phillips793324c2018-08-24 13:53:56 -0400323 shape, std::move(grPaint), viewMatrix, mf);
bsalomonc55271f2015-11-09 11:55:57 -0800324}