blob: da2c8878185332a81a29b24e8b4a142469569a2f [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"
Michael Ludwig339e1cc2019-02-27 11:26:27 -05009
10#include "GrBitmapTextureMaker.h"
bsalomonc55271f2015-11-09 11:55:57 -080011#include "GrBlurUtils.h"
12#include "GrCaps.h"
Brian Osman1cb41712017-10-19 12:54:52 -040013#include "GrColorSpaceXform.h"
Michael Ludwig339e1cc2019-02-27 11:26:27 -050014#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Robert Phillips27927a52018-08-20 13:18:12 -040016#include "GrShape.h"
bsalomon6663acf2016-05-10 09:14:17 -070017#include "GrStyle.h"
Brian Osmane8e54582016-11-28 10:06:27 -050018#include "GrTextureAdjuster.h"
Brian Salomon34169692017-08-28 15:32:01 -040019#include "GrTextureMaker.h"
bsalomonc55271f2015-11-09 11:55:57 -080020#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050021#include "SkGr.h"
Michael Ludwig339e1cc2019-02-27 11:26:27 -050022#include "SkImage_Base.h"
Mike Reed80747ef2018-01-23 15:29:32 -050023#include "SkMaskFilterBase.h"
Michael Ludwig339e1cc2019-02-27 11:26:27 -050024#include "SkYUVAIndex.h"
bsalomonc55271f2015-11-09 11:55:57 -080025#include "effects/GrBicubicEffect.h"
26#include "effects/GrSimpleTextureEffect.h"
27#include "effects/GrTextureDomain.h"
28
Michael Ludwig339e1cc2019-02-27 11:26:27 -050029namespace {
30
bsalomonc55271f2015-11-09 11:55:57 -080031static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) {
32 return textureIsAlphaOnly && paint.getShader();
33}
34
bsalomonb1b01992015-11-18 10:56:08 -080035//////////////////////////////////////////////////////////////////////////////
36// Helper functions for dropping src rect constraint in bilerp mode.
37
38static const SkScalar kColorBleedTolerance = 0.001f;
39
40static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) {
41 // detect pixel disalignment
Brian Salomona911f8f2015-11-18 15:19:57 -050042 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance &&
43 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance &&
44 SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance &&
bsalomonb1b01992015-11-18 10:56:08 -080045 SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) {
46 return true;
47 }
48 return false;
49}
50
51static bool may_color_bleed(const SkRect& srcRect,
52 const SkRect& transformedRect,
53 const SkMatrix& m,
Brian Salomon7c8460e2017-05-12 11:36:10 -040054 GrFSAAType fsaaType) {
bsalomonb1b01992015-11-18 10:56:08 -080055 // 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);
Brian Salomon7c8460e2017-05-12 11:36:10 -040059 if (GrFSAAType::kUnifiedMSAA == fsaaType) {
bsalomonb1b01992015-11-18 10:56:08 -080060 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
80static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer,
81 const SkRect& srcRect,
82 const SkMatrix& srcRectToDeviceSpace,
Brian Salomon7c8460e2017-05-12 11:36:10 -040083 GrFSAAType fsaaType) {
bsalomonb1b01992015-11-18 10:56:08 -080084 if (srcRectToDeviceSpace.rectStaysRect()) {
85 // sampling is axis-aligned
86 SkRect transformedRect;
87 srcRectToDeviceSpace.mapRect(&transformedRect, srcRect);
88
89 if (has_aligned_samples(srcRect, transformedRect) ||
Brian Salomon7c8460e2017-05-12 11:36:10 -040090 !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, fsaaType)) {
bsalomonb1b01992015-11-18 10:56:08 -080091 return true;
92 }
93 }
94 return false;
95}
96
Michael Ludwig339e1cc2019-02-27 11:26:27 -050097enum 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 */
116static 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 Salomon34169692017-08-28 15:32:01 -0400164/**
Brian Salomonb80ffee2018-05-23 16:39:39 -0400165 * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more
166 * efficient than the GrTextureProducer general case.
Brian Salomon34169692017-08-28 15:32:01 -0400167 */
Brian Salomonb80ffee2018-05-23 16:39:39 -0400168static bool can_use_draw_texture(const SkPaint& paint) {
Brian Salomon34169692017-08-28 15:32:01 -0400169 return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() &&
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500170 !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality);
Brian Salomon34169692017-08-28 15:32:01 -0400171}
172
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500173// Assumes srcRect and dstRect have already been optimized to fit the proxy
174static 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,
177 SkCanvas::SrcRectConstraint constraint, sk_sp<GrTextureProxy> proxy,
178 SkAlphaType alphaType, SkColorSpace* colorSpace) {
Brian Osman3d139a42018-11-19 10:42:10 -0500179 const GrColorSpaceInfo& dstInfo(rtc->colorSpaceInfo());
Mike Kleine03a1762018-08-22 11:52:16 -0400180 auto textureXform =
Brian Osman3d139a42018-11-19 10:42:10 -0500181 GrColorSpaceXform::Make(colorSpace , alphaType,
182 dstInfo.colorSpace(), kPremul_SkAlphaType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400183 GrSamplerState::Filter filter;
Brian Salomon34169692017-08-28 15:32:01 -0400184 switch (paint.getFilterQuality()) {
185 case kNone_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400186 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400187 break;
188 case kLow_SkFilterQuality:
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400189 filter = GrSamplerState::Filter::kBilerp;
Brian Salomon34169692017-08-28 15:32:01 -0400190 break;
191 case kMedium_SkFilterQuality:
192 case kHigh_SkFilterQuality:
193 SK_ABORT("Quality level not allowed.");
194 }
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500195
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.
198 if (constraint != SkCanvas::kStrict_SrcRectConstraint &&
199 !GrProxyProvider::IsFunctionallyExact(proxy.get())) {
200 // Conservative estimate of how much a coord could be outset from src rect:
201 // 1/2 pixel for AA and 1/2 pixel for bilerp
202 float buffer = 0.5f * (aa == GrAA::kYes) +
203 0.5f * (filter == GrSamplerState::Filter::kBilerp);
204 SkRect safeBounds = SkRect::MakeWH(proxy->width(), proxy->height());
205 safeBounds.inset(buffer, buffer);
206 if (!safeBounds.contains(srcRect)) {
207 constraint = SkCanvas::kStrict_SrcRectConstraint;
208 }
209 }
Brian Osman3d139a42018-11-19 10:42:10 -0500210 SkPMColor4f color;
Brian Osman3ebd3542018-07-30 14:36:53 -0400211 if (GrPixelConfigIsAlphaOnly(proxy->config())) {
Brian Osman3d139a42018-11-19 10:42:10 -0500212 color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo, *rtc->caps()).premul();
Brian Osman3ebd3542018-07-30 14:36:53 -0400213 } else {
Brian Osman3d139a42018-11-19 10:42:10 -0500214 float paintAlpha = paint.getColor4f().fA;
215 color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha };
Brian Osman3ebd3542018-07-30 14:36:53 -0400216 }
Brian Salomon34169692017-08-28 15:32:01 -0400217
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500218 if (dstClip) {
219 // Get source coords corresponding to dstClip
220 SkPoint srcQuad[4];
221 GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4);
bsalomonb1b01992015-11-18 10:56:08 -0800222
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500223 rtc->drawTextureQuad(clip, std::move(proxy), filter, paint.getBlendMode(), color,
224 srcQuad, dstClip, aa, aaFlags,
225 constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr,
226 ctm, std::move(textureXform));
227 } else {
228 rtc->drawTexture(clip, std::move(proxy), filter, paint.getBlendMode(), color, srcRect,
229 dstRect, aa, aaFlags, constraint, ctm, std::move(textureXform));
Brian Salomon34169692017-08-28 15:32:01 -0400230 }
Brian Salomon34169692017-08-28 15:32:01 -0400231}
232
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500233// Assumes srcRect and dstRect have already been optimized to fit the proxy.
234static void draw_texture_producer(GrContext* context, GrRenderTargetContext* rtc,
235 const GrClip& clip, const SkMatrix& ctm,
236 const SkPaint& paint, GrTextureProducer* producer,
237 const SkRect& src, const SkRect& dst, const SkPoint dstClip[4],
238 const SkMatrix& srcToDst, GrAA aa, GrQuadAAFlags aaFlags,
239 SkCanvas::SrcRectConstraint constraint, bool attemptDrawTexture) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400240 if (attemptDrawTexture && can_use_draw_texture(paint)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400241 // We've done enough checks above to allow us to pass ClampNearest() and not check for
242 // scaling adjustments.
243 auto proxy = producer->refTextureProxyForParams(GrSamplerState::ClampNearest(), nullptr);
244 if (!proxy) {
245 return;
246 }
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500247
248 draw_texture(rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint,
249 std::move(proxy), producer->alphaType(), producer->colorSpace());
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400250 return;
251 }
252
bsalomonc55271f2015-11-09 11:55:57 -0800253 const SkMaskFilter* mf = paint.getMaskFilter();
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500254
bsalomonc55271f2015-11-09 11:55:57 -0800255 // The shader expects proper local coords, so we can't replace local coords with texture coords
256 // if the shader will be used. If we have a mask filter we will change the underlying geometry
257 // that is rendered.
bsalomonf1ecd212015-12-09 17:06:02 -0800258 bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800259
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500260 // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500261 // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture
Michael Ludwigb7d64b92019-02-11 11:09:15 -0500262 // FP. In the future this should be an opaque optimization enabled by the combination of
263 // GrDrawOp/GP and FP.
264 if (mf && as_MFB(mf)->hasFragmentProcessor()) {
265 mf = nullptr;
266 }
bsalomonc55271f2015-11-09 11:55:57 -0800267 bool doBicubic;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400268 GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode(
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500269 paint.getFilterQuality(), ctm, srcToDst,
270 context->priv().options().fSharpenMipmappedTextures, &doBicubic);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400271 const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm;
bsalomonc55271f2015-11-09 11:55:57 -0800272
Brian Osmane8e54582016-11-28 10:06:27 -0500273 GrTextureProducer::FilterConstraint constraintMode;
bsalomonc55271f2015-11-09 11:55:57 -0800274 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
275 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
276 } else {
277 constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
278 }
halcanary9d524f22016-03-29 09:03:52 -0700279
bsalomonc55271f2015-11-09 11:55:57 -0800280 // If we have to outset for AA then we will generate texture coords outside the src rect. The
281 // same happens for any mask filter that extends the bounds rendered in the dst.
282 // This is conservative as a mask filter does not have to expand the bounds rendered.
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500283 bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf;
bsalomonc55271f2015-11-09 11:55:57 -0800284
bsalomonb1b01992015-11-18 10:56:08 -0800285 // Check for optimization to drop the src rect constraint when on bilerp.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400286 if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode &&
bsalomonb1b01992015-11-18 10:56:08 -0800287 GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) {
288 SkMatrix combinedMatrix;
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500289 combinedMatrix.setConcat(ctm, srcToDst);
290 if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->fsaaType())) {
bsalomonb1b01992015-11-18 10:56:08 -0800291 constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
292 }
293 }
294
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500295 SkMatrix textureMatrix;
bsalomon3aa5fce2015-11-12 09:59:44 -0800296 if (canUseTextureCoordsAsLocalCoords) {
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500297 textureMatrix = SkMatrix::I();
bsalomon3aa5fce2015-11-12 09:59:44 -0800298 } else {
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500299 if (!srcToDst.invert(&textureMatrix)) {
bsalomon3aa5fce2015-11-12 09:59:44 -0800300 return;
301 }
bsalomon3aa5fce2015-11-12 09:59:44 -0800302 }
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500303 auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode,
Brian Osman05c8f462018-10-22 17:13:36 -0400304 coordsAllInsideSrcRect, filterMode);
305 fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(),
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500306 rtc->colorSpaceInfo().colorSpace());
bsalomonc55271f2015-11-09 11:55:57 -0800307 if (!fp) {
308 return;
309 }
joshualitt33a5fce2015-11-18 13:28:51 -0800310
bsalomonc55271f2015-11-09 11:55:57 -0800311 GrPaint grPaint;
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500312 if (!SkPaintToGrPaintWithTexture(context, rtc->colorSpaceInfo(), paint, ctm,
313 std::move(fp), producer->isAlphaOnly(), &grPaint)) {
bsalomonc55271f2015-11-09 11:55:57 -0800314 return;
315 }
316
317 if (!mf) {
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500318 // Can draw the image directly (any mask filter on the paint was converted to an FP already)
319 if (dstClip) {
320 SkPoint srcClipPoints[4];
321 SkPoint* srcClip = nullptr;
322 if (canUseTextureCoordsAsLocalCoords) {
323 // Calculate texture coordinates that match the dst clip
324 GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4);
325 srcClip = srcClipPoints;
326 }
327 rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip);
328 } else {
329 // Provide explicit texture coords when possible, otherwise rely on texture matrix
330 rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst,
331 canUseTextureCoordsAsLocalCoords ? &src : nullptr);
332 }
333 } else {
334 // Must draw the mask filter as a GrShape. For now, this loses the per-edge AA information
335 // since it always draws with AA, but that is should not be noticeable since the mask filter
336 // is probably a blur.
337 GrShape shape;
338 if (dstClip) {
339 // Represent it as an SkPath formed from the dstClip
340 SkPath path;
341 path.addPoly(dstClip, 4, true);
342 shape = GrShape(path);
343 } else {
344 shape = GrShape(dst);
345 }
346
347 GrBlurUtils::drawShapeWithMaskFilter(
348 context, rtc, clip, shape, std::move(grPaint), ctm, mf);
349 }
350}
351
352} // anonymous namespace
353
354//////////////////////////////////////////////////////////////////////////////
355
356void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect,
357 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
358 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
359 SkRect src;
360 SkRect dst;
361 SkMatrix srcToDst;
362 ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()),
363 srcRect, dstRect, dstClip, &src, &dst, &srcToDst);
364 if (mode == ImageDrawMode::kSkip) {
bsalomonc55271f2015-11-09 11:55:57 -0800365 return;
366 }
367
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500368 if (src.contains(image->bounds())) {
369 constraint = SkCanvas::kFast_SrcRectConstraint;
370 }
371 // Depending on the nature of image, it can flow through more or less optimal pipelines
372 bool useDecal = mode == ImageDrawMode::kDecal;
373 bool attemptDrawTexture = !useDecal; // rtc->drawTexture() only clamps
Robert Phillips27927a52018-08-20 13:18:12 -0400374
Michael Ludwig339e1cc2019-02-27 11:26:27 -0500375 // YUVA images can be stored in multiple images with different plane resolutions, so this
376 // uses an effect to combine them dynamically on the GPU. This is done before requesting a
377 // pinned texture proxy because YUV images force-flatten to RGBA in that scenario.
378 if (as_IB(image)->isYUVA()) {
379 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
380 LogDrawScaleFactor(this->ctm(), srcToDst, paint.getFilterQuality());
381
382 GrYUVAImageTextureMaker maker(fContext.get(), image, useDecal);
383 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), this->ctm(),
384 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
385 /* attempt draw texture */ false);
386 return;
387 }
388
389 // Pinned texture proxies can be rendered directly as textures, or with relatively simple
390 // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
391 uint32_t pinnedUniqueID;
392 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(&pinnedUniqueID)) {
393 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
394 LogDrawScaleFactor(this->ctm(), srcToDst, paint.getFilterQuality());
395
396 SkAlphaType alphaType = image->alphaType();
397 SkColorSpace* colorSpace = as_IB(image)->colorSpace();
398
399 if (attemptDrawTexture && can_use_draw_texture(paint)) {
400 draw_texture(fRenderTargetContext.get(), this->clip(), this->ctm(), paint, src, dst,
401 dstClip, aa, aaFlags, constraint, std::move(proxy), alphaType, colorSpace);
402 return;
403 }
404
405 GrTextureAdjuster adjuster(fContext.get(), std::move(proxy), alphaType, pinnedUniqueID,
406 colorSpace, useDecal);
407 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), this->ctm(),
408 paint, &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags,
409 constraint, /* attempt draw_texture */ false);
410 return;
411 }
412
413 // Next up, try tiling the image
414 // TODO (michaelludwig): Implement this with per-edge AA flags to handle seaming properly
415 // instead of going through drawBitmapRect (which will be removed from SkDevice in the future)
416 SkBitmap bm;
417 if (this->shouldTileImage(image, &src, constraint, paint.getFilterQuality(), this->ctm(),
418 srcToDst)) {
419 // only support tiling as bitmap at the moment, so force raster-version
420 if (!as_IB(image)->getROPixels(&bm)) {
421 return;
422 }
423 this->drawBitmapRect(bm, &src, dst, paint, constraint);
424 return;
425 }
426
427 // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry.
428 SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
429 LogDrawScaleFactor(this->ctm(), srcToDst, paint.getFilterQuality());
430
431 // Lazily generated images must get drawn as a texture producer that handles the final
432 // texture creation.
433 if (image->isLazyGenerated()) {
434 GrImageTextureMaker maker(fContext.get(), image, SkImage::kAllow_CachingHint, useDecal);
435 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), this->ctm(),
436 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
437 attemptDrawTexture);
438 return;
439 }
440 if (as_IB(image)->getROPixels(&bm)) {
441 GrBitmapTextureMaker maker(fContext.get(), bm, useDecal);
442 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), this->ctm(),
443 paint, &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint,
444 attemptDrawTexture);
445 }
446
447 // Otherwise don't know how to draw it
448}
449
450// For ease-of-use, the temporary API treats null dstClipCounts as if it were the proper sized
451// array, filled with all 0s (so dstClips can be null too)
452void SkGpuDevice::tmp_drawImageSetV2(const SkCanvas::ImageSetEntry set[], int dstClipCounts[],
453 int count, const SkPoint dstClips[], const SkPaint& paint,
454 SkCanvas::SrcRectConstraint constraint) {
455 SkASSERT(count > 0);
456
457 if (!can_use_draw_texture(paint)) {
458 // Send every entry through drawImageQuad() to handle the more complicated paint
459 int dstClipIndex = 0;
460 for (int i = 0; i < count; ++i) {
461 // Only no clip or quad clip are supported
462 SkASSERT(!dstClipCounts || dstClipCounts[i] == 0 || dstClipCounts[i] == 4);
463 // Always send GrAA::kYes to preserve seaming across tiling in MSAA
464 this->drawImageQuad(set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect,
465 (dstClipCounts && dstClipCounts[i] > 0) ? dstClips + dstClipIndex : nullptr,
466 GrAA::kYes, SkToGrQuadAAFlags(set[i].fAAFlags), paint, constraint);
467 if (dstClipCounts) {
468 dstClipIndex += dstClipCounts[i];
469 }
470 }
471 return;
472 }
473
474 GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ?
475 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
476 SkBlendMode mode = paint.getBlendMode();
477
478 SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count);
479 // We accumulate compatible proxies until we find an an incompatible one or reach the end and
480 // issue the accumulated 'n' draws starting at 'base'.
481 int base = 0, n = 0;
482 auto draw = [&] {
483 if (n > 0) {
484 auto textureXform = GrColorSpaceXform::Make(
485 set[base].fImage->colorSpace(), set[base].fImage->alphaType(),
486 fRenderTargetContext->colorSpaceInfo().colorSpace(), kPremul_SkAlphaType);
487 fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n,
488 filter, mode, GrAA::kYes, this->ctm(),
489 std::move(textureXform));
490 }
491 };
492 int dstClipIndex = 0;
493 for (int i = 0; i < count; ++i) {
494 // Manage the dst clip pointer tracking before any continues are used so we don't lose
495 // our place in the dstClips array.
496 int clipCount = (dstClipCounts ? dstClipCounts[i] : 0);
497 SkASSERT(clipCount == 0 || (dstClipCounts[i] == 4 && dstClips));
498 const SkPoint* clip = clipCount > 0 ? dstClips + dstClipIndex : nullptr;
499 if (dstClipCounts) {
500 dstClipIndex += dstClipCounts[i];
501 }
502 // The default SkBaseDevice implementation is based on drawImageRect which does not allow
503 // non-sorted src rects. TODO: Decide this is OK or make sure we handle it.
504 if (!set[i].fSrcRect.isSorted()) {
505 draw();
506 base = i + 1;
507 n = 0;
508 continue;
509 }
510
511 uint32_t uniqueID;
512 textures[i].fProxy = as_IB(set[i].fImage.get())->refPinnedTextureProxy(&uniqueID);
513 if (!textures[i].fProxy) {
514 // FIXME(michaelludwig) - If asTextureProxyRef fails, does going through drawImageQuad
515 // make sense? Does that catch the lazy-image cases then?
516 // FIXME(michaelludwig) - Both refPinnedTextureProxy and asTextureProxyRef for YUVA
517 // images force flatten the planes. It would be nice to detect a YUVA image entry and
518 // send it to drawImageQuad (which uses a special effect for YUV)
519 textures[i].fProxy =
520 as_IB(set[i].fImage.get())
521 ->asTextureProxyRef(fContext.get(), GrSamplerState::ClampBilerp(),
522 nullptr);
523 // If we failed to make a proxy then flush the accumulated set and reset for the next
524 // image.
525 if (!textures[i].fProxy) {
526 draw();
527 base = i + 1;
528 n = 0;
529 continue;
530 }
531 }
532 textures[i].fSrcRect = set[i].fSrcRect;
533 textures[i].fDstRect = set[i].fDstRect;
534 textures[i].fDstClipQuad = clip;
535 textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf();
536 textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags);
537
538 if (n > 0 &&
539 (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState(textures[i].fProxy.get(),
540 textures[base].fProxy.get()) ||
541 set[i].fImage->alphaType() != set[base].fImage->alphaType() ||
542 !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) {
543 draw();
544 base = i;
545 n = 1;
546 } else {
547 ++n;
548 }
549 }
550 draw();
551}
552
553// TODO (michaelludwig) - to be removed when drawBitmapRect doesn't need it anymore
554void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
555 const SkRect* srcRect,
556 const SkRect* dstRect,
557 SkCanvas::SrcRectConstraint constraint,
558 const SkMatrix& viewMatrix,
559 const SkPaint& paint,
560 bool attemptDrawTexture) {
561 // The texture refactor split the old logic of drawTextureProducer into the beginning of
562 // drawImageQuad() and into the static draw_texture_producer. Replicate necessary logic that
563 // drawImageQuad() handles.
564 SkRect src;
565 SkRect dst;
566 SkMatrix srcToDst;
567 ImageDrawMode mode = optimize_sample_area(SkISize::Make(producer->width(), producer->height()),
568 srcRect, dstRect, nullptr, &src, &dst, &srcToDst);
569 if (mode == ImageDrawMode::kSkip) {
570 return;
571 }
572 // There's no dstClip to worry about and the producer is already made so we wouldn't be able
573 // to tell it to use decals if we had to
574 SkASSERT(mode != ImageDrawMode::kDecal);
575
576 draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), viewMatrix,
577 paint, producer, src, dst, /* clip */ nullptr, srcToDst,
578 GrAA(paint.isAntiAlias()),
579 paint.isAntiAlias() ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
580 constraint, attemptDrawTexture);
bsalomonc55271f2015-11-09 11:55:57 -0800581}