blob: 0b1d3f1f08af81010827de44d4486471169214fa [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"
9
10#include "GrBlurUtils.h"
11#include "GrCaps.h"
12#include "GrDrawContext.h"
13#include "GrStrokeInfo.h"
14#include "GrTextureParamsAdjuster.h"
15#include "SkDraw.h"
16#include "SkGrPriv.h"
17#include "SkMaskFilter.h"
18#include "effects/GrBicubicEffect.h"
19#include "effects/GrSimpleTextureEffect.h"
20#include "effects/GrTextureDomain.h"
21
22static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
23 return textureIsAlphaOnly && paint.getShader();
24}
25
bsalomonb1b01992015-11-18 10:56:08 -080026//////////////////////////////////////////////////////////////////////////////
27// Helper functions for dropping src rect constraint in bilerp mode.
28
29static const SkScalar kColorBleedTolerance = 0.001f;
30
31static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
32 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050033 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
34 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
35 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080036 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
37 return true;
38 }
39 return false;
40}
41
42static bool may_color_bleed(const SkRect& srcRect,
43 const SkRect& transformedRect,
44 const SkMatrix& m,
45 bool isMSAA) {
46 // Only gets called if has_aligned_samples returned false.
47 // So we can assume that sampling is axis aligned but not texel aligned.
48 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
49 SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect);
50 if (isMSAA) {
51 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
52 } else {
53 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
54 }
55 m.mapRect(&innerTransformedRect, innerSrcRect);
56
57 // The gap between outerTransformedRect and innerTransformedRect
58 // represents the projection of the source border area, which is
59 // problematic for color bleeding. We must check whether any
60 // destination pixels sample the border area.
61 outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance);
62 innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance);
63 SkIRect outer, inner;
64 outerTransformedRect.round(&outer);
65 innerTransformedRect.round(&inner);
66 // If the inner and outer rects round to the same result, it means the
67 // border does not overlap any pixel centers. Yay!
68 return inner != outer;
69}
70
71static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
72 const SkRect& srcRect,
73 const SkMatrix& srcRectToDeviceSpace,
74 bool isMSAA) {
75 if (srcRectToDeviceSpace.rectStaysRect()) {
76 // sampling is axis-aligned
77 SkRect transformedRect;
78 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
79
80 if (has_aligned_samples(srcRect, transformedRect) ||
81 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, isMSAA)) {
82 return true;
83 }
84 }
85 return false;
86}
87
88//////////////////////////////////////////////////////////////////////////////
89
90void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
bsalomonc55271f2015-11-09 11:55:57 -080091 bool alphaOnly,
92 const SkRect* srcRect,
93 const SkRect* dstRect,
94 SkCanvas::SrcRectConstraint constraint,
95 const SkMatrix& viewMatrix,
96 const GrClip& clip,
97 const SkPaint& paint) {
98 // Figure out the actual dst and src rect by clipping the src rect to the bounds of the
99 // adjuster. If the src rect is clipped then the dst rect must be recomputed. Also determine
100 // the matrix that maps the src rect to the dst rect.
101 SkRect clippedSrcRect;
102 SkRect clippedDstRect;
bsalomonb1b01992015-11-18 10:56:08 -0800103 const SkRect srcBounds = SkRect::MakeIWH(producer->width(), producer->height());
bsalomonc55271f2015-11-09 11:55:57 -0800104 SkMatrix srcToDstMatrix;
105 if (srcRect) {
106 if (!dstRect) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800107 dstRect = &srcBounds;
bsalomonc55271f2015-11-09 11:55:57 -0800108 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800109 if (!srcBounds.contains(*srcRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800110 clippedSrcRect = *srcRect;
bsalomon3aa5fce2015-11-12 09:59:44 -0800111 if (!clippedSrcRect.intersect(srcBounds)) {
bsalomonc55271f2015-11-09 11:55:57 -0800112 return;
113 }
114 if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) {
115 return;
116 }
117 srcToDstMatrix.mapRect(&clippedDstRect, clippedSrcRect);
118 } else {
119 clippedSrcRect = *srcRect;
120 clippedDstRect = *dstRect;
121 if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) {
122 return;
123 }
124 }
125 } else {
bsalomon3aa5fce2015-11-12 09:59:44 -0800126 clippedSrcRect = srcBounds;
bsalomonc55271f2015-11-09 11:55:57 -0800127 if (dstRect) {
128 clippedDstRect = *dstRect;
bsalomon3aa5fce2015-11-12 09:59:44 -0800129 if (!srcToDstMatrix.setRectToRect(srcBounds, *dstRect, SkMatrix::kFill_ScaleToFit)) {
bsalomonc55271f2015-11-09 11:55:57 -0800130 return;
131 }
132 } else {
bsalomon3aa5fce2015-11-12 09:59:44 -0800133 clippedDstRect = srcBounds;
bsalomonc55271f2015-11-09 11:55:57 -0800134 srcToDstMatrix.reset();
135 }
136 }
137
bsalomonb1b01992015-11-18 10:56:08 -0800138 this->drawTextureProducerImpl(producer, alphaOnly, clippedSrcRect, clippedDstRect, constraint,
bsalomonc55271f2015-11-09 11:55:57 -0800139 viewMatrix, srcToDstMatrix, clip, paint);
140}
141
bsalomonb1b01992015-11-18 10:56:08 -0800142void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer,
bsalomonc55271f2015-11-09 11:55:57 -0800143 bool alphaTexture,
144 const SkRect& clippedSrcRect,
145 const SkRect& clippedDstRect,
146 SkCanvas::SrcRectConstraint constraint,
147 const SkMatrix& viewMatrix,
148 const SkMatrix& srcToDstMatrix,
149 const GrClip& clip,
150 const SkPaint& paint) {
151 // Specifying the texture coords as local coordinates is an attempt to enable more batching
152 // by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture FP. In
153 // the future this should be an opaque optimization enabled by the combination of batch/GP and
154 // FP.
bsalomonc55271f2015-11-09 11:55:57 -0800155 const SkMaskFilter* mf = paint.getMaskFilter();
bsalomonc55271f2015-11-09 11:55:57 -0800156 // The shader expects proper local coords, so we can't replace local coords with texture coords
157 // if the shader will be used. If we have a mask filter we will change the underlying geometry
158 // that is rendered.
159 bool canUseTextureCoordsAsLocalCoords = !use_shader(alphaTexture, paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800160
161 bool doBicubic;
162 GrTextureParams::FilterMode fm =
163 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, srcToDstMatrix,
164 &doBicubic);
165 const GrTextureParams::FilterMode* filterMode = doBicubic ? nullptr : &fm;
166
167 GrTextureAdjuster::FilterConstraint constraintMode;
168 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
169 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
170 } else {
171 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
172 }
173
174 // If we have to outset for AA then we will generate texture coords outside the src rect. The
175 // same happens for any mask filter that extends the bounds rendered in the dst.
176 // This is conservative as a mask filter does not have to expand the bounds rendered.
177 bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf;
178
bsalomonb1b01992015-11-18 10:56:08 -0800179 // Check for optimization to drop the src rect constraint when on bilerp.
180 if (filterMode && GrTextureParams::kBilerp_FilterMode == *filterMode &&
181 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) {
182 SkMatrix combinedMatrix;
183 combinedMatrix.setConcat(viewMatrix, srcToDstMatrix);
184 if (can_ignore_bilerp_constraint(*producer, clippedSrcRect, combinedMatrix,
185 fRenderTarget->isUnifiedMultisampled())) {
186 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
187 }
188 }
189
bsalomon3aa5fce2015-11-12 09:59:44 -0800190 const SkMatrix* textureMatrix;
191 SkMatrix tempMatrix;
192 if (canUseTextureCoordsAsLocalCoords) {
193 textureMatrix = &SkMatrix::I();
194 } else {
195 if (!srcToDstMatrix.invert(&tempMatrix)) {
196 return;
197 }
198 textureMatrix = &tempMatrix;
199 }
bsalomonb1b01992015-11-18 10:56:08 -0800200 SkAutoTUnref<const GrFragmentProcessor> fp(producer->createFragmentProcessor(
bsalomon3aa5fce2015-11-12 09:59:44 -0800201 *textureMatrix, clippedSrcRect, constraintMode, coordsAllInsideSrcRect, filterMode));
bsalomonc55271f2015-11-09 11:55:57 -0800202 if (!fp) {
203 return;
204 }
joshualitt33a5fce2015-11-18 13:28:51 -0800205
bsalomonc55271f2015-11-09 11:55:57 -0800206 GrPaint grPaint;
joshualitt33a5fce2015-11-18 13:28:51 -0800207 if (!SkPaintToGrPaintWithTexture(fContext, paint, viewMatrix, fp, alphaTexture, &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800208 return;
209 }
210
211 if (canUseTextureCoordsAsLocalCoords) {
bsalomon3877ab82015-11-11 16:52:03 -0800212 fDrawContext->fillRectToRect(clip, grPaint, viewMatrix, clippedDstRect, clippedSrcRect);
bsalomonc55271f2015-11-09 11:55:57 -0800213 return;
214 }
215
216 if (!mf) {
217 fDrawContext->drawRect(clip, grPaint, viewMatrix, clippedDstRect);
218 return;
219 }
220
221 // First see if we can do the draw + mask filter direct to the dst.
222 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
223 SkRRect rrect;
224 rrect.setRect(clippedDstRect);
225 if (mf->directFilterRRectMaskGPU(fContext->textureProvider(),
226 fDrawContext,
227 &grPaint,
228 clip,
229 viewMatrix,
230 rec,
231 rrect)) {
232 return;
233 }
234 SkPath rectPath;
235 rectPath.addRect(clippedDstRect);
robertphillips0e7029e2015-11-30 05:45:06 -0800236 rectPath.setIsVolatile(true);
robertphillips7bceedc2015-12-01 12:51:26 -0800237 GrBlurUtils::drawPathWithMaskFilter(this->context(), fDrawContext, fClip,
bsalomonc55271f2015-11-09 11:55:57 -0800238 rectPath, &grPaint, viewMatrix, mf, paint.getPathEffect(),
robertphillips0e7029e2015-11-30 05:45:06 -0800239 GrStrokeInfo::FillInfo(), true);
bsalomonc55271f2015-11-09 11:55:57 -0800240}