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 | |
8 | #include "src/gpu/SkGpuDevice.h" |
9 | |
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" |
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" |
25 | #include "src/gpu/effects/GrTextureEffect.h" |
26 | #include "src/gpu/geometry/GrShape.h" |
27 | #include "src/image/SkImage_Base.h" |
28 | |
29 | namespace { |
30 | |
31 | static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) { |
32 | return textureIsAlphaOnly && paint.getShader(); |
33 | } |
34 | |
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 |
42 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance && |
43 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance && |
44 | SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance && |
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, |
54 | int numSamples) { |
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); |
59 | if (numSamples > 1) { |
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, |
83 | int numSamples) { |
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) || |
90 | !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, numSamples)) { |
91 | return true; |
92 | } |
93 | } |
94 | return false; |
95 | } |
96 | |
97 | ////////////////////////////////////////////////////////////////////////////// |
98 | // Helper functions for tiling a large SkBitmap |
99 | |
100 | static const int kBmpSmallTileSize = 1 << 10; |
101 | |
102 | static inline int get_tile_count(const SkIRect& srcRect, int tileSize) { |
103 | int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1; |
104 | int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1; |
105 | return tilesX * tilesY; |
106 | } |
107 | |
108 | static int determine_tile_size(const SkIRect& src, int maxTileSize) { |
109 | if (maxTileSize <= kBmpSmallTileSize) { |
110 | return maxTileSize; |
111 | } |
112 | |
113 | size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize); |
114 | size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize); |
115 | |
116 | maxTileTotalTileSize *= maxTileSize * maxTileSize; |
117 | smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize; |
118 | |
119 | if (maxTileTotalTileSize > 2 * smallTotalTileSize) { |
120 | return kBmpSmallTileSize; |
121 | } else { |
122 | return maxTileSize; |
123 | } |
124 | } |
125 | |
126 | // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what |
127 | // pixels from the bitmap are necessary. |
128 | static void determine_clipped_src_rect(int width, int height, |
129 | const GrClip& clip, |
130 | const SkMatrix& viewMatrix, |
131 | const SkMatrix& srcToDstRect, |
132 | const SkISize& imageDimensions, |
133 | const SkRect* srcRectPtr, |
134 | SkIRect* clippedSrcIRect) { |
135 | clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr); |
136 | SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect); |
137 | if (!inv.invert(&inv)) { |
138 | clippedSrcIRect->setEmpty(); |
139 | return; |
140 | } |
141 | SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect); |
142 | inv.mapRect(&clippedSrcRect); |
143 | if (srcRectPtr) { |
144 | if (!clippedSrcRect.intersect(*srcRectPtr)) { |
145 | clippedSrcIRect->setEmpty(); |
146 | return; |
147 | } |
148 | } |
149 | clippedSrcRect.roundOut(clippedSrcIRect); |
150 | SkIRect bmpBounds = SkIRect::MakeSize(imageDimensions); |
151 | if (!clippedSrcIRect->intersect(bmpBounds)) { |
152 | clippedSrcIRect->setEmpty(); |
153 | } |
154 | } |
155 | |
156 | // tileSize and clippedSubset are valid if true is returned |
157 | static bool should_tile_image_id(GrContext* context, |
158 | SkISize rtSize, |
159 | const GrClip& clip, |
160 | uint32_t imageID, |
161 | const SkISize& imageSize, |
162 | const SkMatrix& ctm, |
163 | const SkMatrix& srcToDst, |
164 | const SkRect* src, |
165 | int maxTileSize, |
166 | int* tileSize, |
167 | SkIRect* clippedSubset) { |
168 | // if it's larger than the max tile size, then we have no choice but tiling. |
169 | if (imageSize.width() > maxTileSize || imageSize.height() > maxTileSize) { |
170 | determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst, |
171 | imageSize, src, clippedSubset); |
172 | *tileSize = determine_tile_size(*clippedSubset, maxTileSize); |
173 | return true; |
174 | } |
175 | |
176 | // If the image would only produce 4 tiles of the smaller size, don't bother tiling it. |
177 | const size_t area = imageSize.width() * imageSize.height(); |
178 | if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) { |
179 | return false; |
180 | } |
181 | |
182 | // At this point we know we could do the draw by uploading the entire bitmap |
183 | // as a texture. However, if the texture would be large compared to the |
184 | // cache size and we don't require most of it for this draw then tile to |
185 | // reduce the amount of upload and cache spill. |
186 | |
187 | // assumption here is that sw bitmap size is a good proxy for its size as |
188 | // a texture |
189 | size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels |
190 | size_t cacheSize = context->getResourceCacheLimit(); |
191 | if (bmpSize < cacheSize / 2) { |
192 | return false; |
193 | } |
194 | |
195 | // Figure out how much of the src we will need based on the src rect and clipping. Reject if |
196 | // tiling memory savings would be < 50%. |
197 | determine_clipped_src_rect(rtSize.width(), rtSize.height(), clip, ctm, srcToDst, imageSize, src, |
198 | clippedSubset); |
199 | *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile. |
200 | size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) * |
201 | kBmpSmallTileSize * kBmpSmallTileSize * |
202 | sizeof(SkPMColor); // assume 32bit pixels; |
203 | |
204 | return usedTileBytes * 2 < bmpSize; |
205 | } |
206 | |
207 | // This method outsets 'iRect' by 'outset' all around and then clamps its extents to |
208 | // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner |
209 | // of 'iRect' for all possible outsets/clamps. |
210 | static inline void clamped_outset_with_offset(SkIRect* iRect, int outset, SkPoint* offset, |
211 | const SkIRect& clamp) { |
212 | iRect->outset(outset, outset); |
213 | |
214 | int leftClampDelta = clamp.fLeft - iRect->fLeft; |
215 | if (leftClampDelta > 0) { |
216 | offset->fX -= outset - leftClampDelta; |
217 | iRect->fLeft = clamp.fLeft; |
218 | } else { |
219 | offset->fX -= outset; |
220 | } |
221 | |
222 | int topClampDelta = clamp.fTop - iRect->fTop; |
223 | if (topClampDelta > 0) { |
224 | offset->fY -= outset - topClampDelta; |
225 | iRect->fTop = clamp.fTop; |
226 | } else { |
227 | offset->fY -= outset; |
228 | } |
229 | |
230 | if (iRect->fRight > clamp.fRight) { |
231 | iRect->fRight = clamp.fRight; |
232 | } |
233 | if (iRect->fBottom > clamp.fBottom) { |
234 | iRect->fBottom = clamp.fBottom; |
235 | } |
236 | } |
237 | |
238 | ////////////////////////////////////////////////////////////////////////////// |
239 | // Helper functions for drawing an image with GrRenderTargetContext |
240 | |
241 | enum class ImageDrawMode { |
242 | // Src and dst have been restricted to the image content. May need to clamp, no need to decal. |
243 | kOptimized, |
244 | // Src and dst are their original sizes, requires use of a decal instead of plain clamping. |
245 | // This is used when a dst clip is provided and extends outside of the optimized dst rect. |
246 | kDecal, |
247 | // Src or dst are empty, or do not intersect the image content so don't draw anything. |
248 | kSkip |
249 | }; |
250 | |
251 | /** |
252 | * Optimize the src rect sampling area within an image (sized 'width' x 'height') such that |
253 | * 'outSrcRect' will be completely contained in the image's bounds. The corresponding rect |
254 | * to draw will be output to 'outDstRect'. The mapping between src and dst will be cached in |
255 | * 'srcToDst'. Outputs are not always updated when kSkip is returned. |
256 | * |
257 | * If 'origSrcRect' is null, implicitly use the image bounds. If 'origDstRect' is null, use the |
258 | * original src rect. 'dstClip' should be null when there is no additional clipping. |
259 | */ |
260 | static ImageDrawMode optimize_sample_area(const SkISize& image, const SkRect* origSrcRect, |
261 | const SkRect* origDstRect, const SkPoint dstClip[4], |
262 | SkRect* outSrcRect, SkRect* outDstRect, |
263 | SkMatrix* srcToDst) { |
264 | SkRect srcBounds = SkRect::MakeIWH(image.fWidth, image.fHeight); |
265 | |
266 | SkRect src = origSrcRect ? *origSrcRect : srcBounds; |
267 | SkRect dst = origDstRect ? *origDstRect : src; |
268 | |
269 | if (src.isEmpty() || dst.isEmpty()) { |
270 | return ImageDrawMode::kSkip; |
271 | } |
272 | |
273 | if (outDstRect) { |
274 | srcToDst->setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
275 | } else { |
276 | srcToDst->setIdentity(); |
277 | } |
278 | |
279 | if (origSrcRect && !srcBounds.contains(src)) { |
280 | if (!src.intersect(srcBounds)) { |
281 | return ImageDrawMode::kSkip; |
282 | } |
283 | srcToDst->mapRect(&dst, src); |
284 | |
285 | // Both src and dst have gotten smaller. If dstClip is provided, confirm it is still |
286 | // contained in dst, otherwise cannot optimize the sample area and must use a decal instead |
287 | if (dstClip) { |
288 | for (int i = 0; i < 4; ++i) { |
289 | if (!dst.contains(dstClip[i].fX, dstClip[i].fY)) { |
290 | // Must resort to using a decal mode restricted to the clipped 'src', and |
291 | // use the original dst rect (filling in src bounds as needed) |
292 | *outSrcRect = src; |
293 | *outDstRect = (origDstRect ? *origDstRect |
294 | : (origSrcRect ? *origSrcRect : srcBounds)); |
295 | return ImageDrawMode::kDecal; |
296 | } |
297 | } |
298 | } |
299 | } |
300 | |
301 | // The original src and dst were fully contained in the image, or there was no dst clip to |
302 | // worry about, or the clip was still contained in the restricted dst rect. |
303 | *outSrcRect = src; |
304 | *outDstRect = dst; |
305 | return ImageDrawMode::kOptimized; |
306 | } |
307 | |
308 | /** |
309 | * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more |
310 | * efficient than the GrTextureProducer general case. |
311 | */ |
312 | static bool can_use_draw_texture(const SkPaint& paint) { |
313 | return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() && |
314 | !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality); |
315 | } |
316 | |
317 | // Assumes srcRect and dstRect have already been optimized to fit the proxy |
318 | static void draw_texture(GrRenderTargetContext* rtc, const GrClip& clip, const SkMatrix& ctm, |
319 | const SkPaint& paint, const SkRect& srcRect, const SkRect& dstRect, |
320 | const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags, |
321 | SkCanvas::SrcRectConstraint constraint, GrSurfaceProxyView view, |
322 | const GrColorInfo& srcColorInfo) { |
323 | const GrColorInfo& dstInfo(rtc->colorInfo()); |
324 | auto textureXform = |
325 | GrColorSpaceXform::Make(srcColorInfo.colorSpace(), srcColorInfo.alphaType(), |
326 | dstInfo.colorSpace(), kPremul_SkAlphaType); |
327 | GrSamplerState::Filter filter; |
328 | switch (paint.getFilterQuality()) { |
329 | case kNone_SkFilterQuality: |
330 | filter = GrSamplerState::Filter::kNearest; |
331 | break; |
332 | case kLow_SkFilterQuality: |
333 | filter = GrSamplerState::Filter::kBilerp; |
334 | break; |
335 | case kMedium_SkFilterQuality: |
336 | case kHigh_SkFilterQuality: |
337 | SK_ABORT("Quality level not allowed." ); |
338 | } |
339 | GrSurfaceProxy* proxy = view.proxy(); |
340 | // Must specify the strict constraint when the proxy is not functionally exact and the src |
341 | // rect would access pixels outside the proxy's content area without the constraint. |
342 | if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) { |
343 | // Conservative estimate of how much a coord could be outset from src rect: |
344 | // 1/2 pixel for AA and 1/2 pixel for bilerp |
345 | float buffer = 0.5f * (aa == GrAA::kYes) + |
346 | 0.5f * (filter == GrSamplerState::Filter::kBilerp); |
347 | SkRect safeBounds = proxy->getBoundsRect(); |
348 | safeBounds.inset(buffer, buffer); |
349 | if (!safeBounds.contains(srcRect)) { |
350 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
351 | } |
352 | } |
353 | SkPMColor4f color; |
354 | if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) { |
355 | color = SkColor4fPrepForDst(paint.getColor4f(), dstInfo).premul(); |
356 | } else { |
357 | float paintAlpha = paint.getColor4f().fA; |
358 | color = { paintAlpha, paintAlpha, paintAlpha, paintAlpha }; |
359 | } |
360 | |
361 | if (dstClip) { |
362 | // Get source coords corresponding to dstClip |
363 | SkPoint srcQuad[4]; |
364 | GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4); |
365 | |
366 | rtc->drawTextureQuad(clip, std::move(view), srcColorInfo.colorType(), |
367 | srcColorInfo.alphaType(), filter, paint.getBlendMode(), color, srcQuad, |
368 | dstClip, aa, aaFlags, |
369 | constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr, |
370 | ctm, std::move(textureXform)); |
371 | } else { |
372 | rtc->drawTexture(clip, std::move(view), srcColorInfo.alphaType(), filter, |
373 | paint.getBlendMode(), color, srcRect, dstRect, aa, aaFlags, constraint, |
374 | ctm, std::move(textureXform)); |
375 | } |
376 | } |
377 | |
378 | // Assumes srcRect and dstRect have already been optimized to fit the proxy. |
379 | static void draw_texture_producer(GrContext* context, |
380 | GrRenderTargetContext* rtc, |
381 | const GrClip& clip, |
382 | const SkMatrix& ctm, |
383 | const SkPaint& paint, |
384 | GrTextureProducer* producer, |
385 | const SkRect& src, |
386 | const SkRect& dst, |
387 | const SkPoint dstClip[4], |
388 | const SkMatrix& srcToDst, |
389 | GrAA aa, |
390 | GrQuadAAFlags aaFlags, |
391 | SkCanvas::SrcRectConstraint constraint, |
392 | GrSamplerState::WrapMode wm, |
393 | GrSamplerState::Filter fm, |
394 | bool doBicubic) { |
395 | if (wm == GrSamplerState::WrapMode::kClamp && !producer->isPlanar() && |
396 | can_use_draw_texture(paint)) { |
397 | // We've done enough checks above to allow us to pass ClampNearest() and not check for |
398 | // scaling adjustments. |
399 | auto view = producer->view(GrMipMapped::kNo); |
400 | if (!view) { |
401 | return; |
402 | } |
403 | |
404 | draw_texture( |
405 | rtc, clip, ctm, paint, src, dst, dstClip, aa, aaFlags, constraint, std::move(view), |
406 | {producer->colorType(), producer->alphaType(), sk_ref_sp(producer->colorSpace())}); |
407 | return; |
408 | } |
409 | |
410 | const SkMaskFilter* mf = paint.getMaskFilter(); |
411 | |
412 | // The shader expects proper local coords, so we can't replace local coords with texture coords |
413 | // if the shader will be used. If we have a mask filter we will change the underlying geometry |
414 | // that is rendered. |
415 | bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf; |
416 | |
417 | // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp |
418 | // combining by not baking anything about the srcRect, dstRect, or ctm, into the texture |
419 | // FP. In the future this should be an opaque optimization enabled by the combination of |
420 | // GrDrawOp/GP and FP. |
421 | if (mf && as_MFB(mf)->hasFragmentProcessor()) { |
422 | mf = nullptr; |
423 | } |
424 | const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm; |
425 | |
426 | GrTextureProducer::FilterConstraint constraintMode; |
427 | if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
428 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
429 | } else { |
430 | constraintMode = GrTextureAdjuster::kYes_FilterConstraint; |
431 | } |
432 | |
433 | // If we have to outset for AA then we will generate texture coords outside the src rect. The |
434 | // same happens for any mask filter that extends the bounds rendered in the dst. |
435 | // This is conservative as a mask filter does not have to expand the bounds rendered. |
436 | bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf; |
437 | |
438 | // Check for optimization to drop the src rect constraint when on bilerp. |
439 | if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode && |
440 | GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect && |
441 | !producer->isPlanar()) { |
442 | SkMatrix combinedMatrix; |
443 | combinedMatrix.setConcat(ctm, srcToDst); |
444 | if (can_ignore_bilerp_constraint(*producer, src, combinedMatrix, rtc->numSamples())) { |
445 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
446 | } |
447 | } |
448 | |
449 | SkMatrix textureMatrix; |
450 | if (canUseTextureCoordsAsLocalCoords) { |
451 | textureMatrix = SkMatrix::I(); |
452 | } else { |
453 | if (!srcToDst.invert(&textureMatrix)) { |
454 | return; |
455 | } |
456 | } |
457 | auto fp = producer->createFragmentProcessor(textureMatrix, src, constraintMode, |
458 | coordsAllInsideSrcRect, wm, wm, filterMode); |
459 | fp = GrColorSpaceXformEffect::Make(std::move(fp), producer->colorSpace(), producer->alphaType(), |
460 | rtc->colorInfo().colorSpace()); |
461 | if (!fp) { |
462 | return; |
463 | } |
464 | |
465 | GrPaint grPaint; |
466 | if (!SkPaintToGrPaintWithTexture(context, rtc->colorInfo(), paint, ctm, std::move(fp), |
467 | producer->isAlphaOnly(), &grPaint)) { |
468 | return; |
469 | } |
470 | |
471 | if (!mf) { |
472 | // Can draw the image directly (any mask filter on the paint was converted to an FP already) |
473 | if (dstClip) { |
474 | SkPoint srcClipPoints[4]; |
475 | SkPoint* srcClip = nullptr; |
476 | if (canUseTextureCoordsAsLocalCoords) { |
477 | // Calculate texture coordinates that match the dst clip |
478 | GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4); |
479 | srcClip = srcClipPoints; |
480 | } |
481 | rtc->fillQuadWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dstClip, srcClip); |
482 | } else { |
483 | // Provide explicit texture coords when possible, otherwise rely on texture matrix |
484 | rtc->fillRectWithEdgeAA(clip, std::move(grPaint), aa, aaFlags, ctm, dst, |
485 | canUseTextureCoordsAsLocalCoords ? &src : nullptr); |
486 | } |
487 | } else { |
488 | // Must draw the mask filter as a GrShape. For now, this loses the per-edge AA information |
489 | // since it always draws with AA, but that is should not be noticeable since the mask filter |
490 | // is probably a blur. |
491 | GrShape shape; |
492 | if (dstClip) { |
493 | // Represent it as an SkPath formed from the dstClip |
494 | SkPath path; |
495 | path.addPoly(dstClip, 4, true); |
496 | shape = GrShape(path); |
497 | } else { |
498 | shape = GrShape(dst); |
499 | } |
500 | |
501 | GrBlurUtils::drawShapeWithMaskFilter( |
502 | context, rtc, clip, shape, std::move(grPaint), ctm, mf); |
503 | } |
504 | } |
505 | |
506 | void draw_tiled_bitmap(GrContext* context, |
507 | GrRenderTargetContext* rtc, |
508 | const GrClip& clip, |
509 | const SkBitmap& bitmap, |
510 | int tileSize, |
511 | const SkMatrix& ctm, |
512 | const SkMatrix& srcToDst, |
513 | const SkRect& srcRect, |
514 | const SkIRect& clippedSrcIRect, |
515 | const SkPaint& paint, |
516 | GrAA aa, |
517 | SkCanvas::SrcRectConstraint constraint, |
518 | GrSamplerState::WrapMode wm, |
519 | GrSamplerState::Filter fm, |
520 | bool doBicubic) { |
521 | SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect); |
522 | |
523 | int nx = bitmap.width() / tileSize; |
524 | int ny = bitmap.height() / tileSize; |
525 | |
526 | for (int x = 0; x <= nx; x++) { |
527 | for (int y = 0; y <= ny; y++) { |
528 | SkRect tileR; |
529 | tileR.setLTRB(SkIntToScalar(x * tileSize), SkIntToScalar(y * tileSize), |
530 | SkIntToScalar((x + 1) * tileSize), SkIntToScalar((y + 1) * tileSize)); |
531 | |
532 | if (!SkRect::Intersects(tileR, clippedSrcRect)) { |
533 | continue; |
534 | } |
535 | |
536 | if (!tileR.intersect(srcRect)) { |
537 | continue; |
538 | } |
539 | |
540 | SkIRect iTileR; |
541 | tileR.roundOut(&iTileR); |
542 | SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft), |
543 | SkIntToScalar(iTileR.fTop)); |
544 | SkRect rectToDraw = tileR; |
545 | srcToDst.mapRect(&rectToDraw); |
546 | if (fm != GrSamplerState::Filter::kNearest || doBicubic) { |
547 | SkIRect iClampRect; |
548 | |
549 | if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
550 | // In bleed mode we want to always expand the tile on all edges |
551 | // but stay within the bitmap bounds |
552 | iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
553 | } else { |
554 | // In texture-domain/clamp mode we only want to expand the |
555 | // tile on edges interior to "srcRect" (i.e., we want to |
556 | // not bleed across the original clamped edges) |
557 | srcRect.roundOut(&iClampRect); |
558 | } |
559 | int outset = doBicubic ? GrBicubicEffect::kFilterTexelPad : 1; |
560 | clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect); |
561 | } |
562 | |
563 | SkBitmap tmpB; |
564 | if (bitmap.extractSubset(&tmpB, iTileR)) { |
565 | // We should have already handled bitmaps larger than the max texture size. |
566 | SkASSERT(tmpB.width() <= context->priv().caps()->maxTextureSize() && |
567 | tmpB.height() <= context->priv().caps()->maxTextureSize()); |
568 | // We should be respecting the max tile size by the time we get here. |
569 | SkASSERT(tmpB.width() <= context->priv().caps()->maxTileSize() && |
570 | tmpB.height() <= context->priv().caps()->maxTileSize()); |
571 | |
572 | GrBitmapTextureMaker tileProducer(context, tmpB, GrImageTexGenPolicy::kDraw); |
573 | |
574 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
575 | if (aa == GrAA::kYes) { |
576 | // If the entire bitmap was anti-aliased, turn on AA for the outside tile edges. |
577 | if (tileR.fLeft <= srcRect.fLeft) { |
578 | aaFlags |= GrQuadAAFlags::kLeft; |
579 | } |
580 | if (tileR.fRight >= srcRect.fRight) { |
581 | aaFlags |= GrQuadAAFlags::kRight; |
582 | } |
583 | if (tileR.fTop <= srcRect.fTop) { |
584 | aaFlags |= GrQuadAAFlags::kTop; |
585 | } |
586 | if (tileR.fBottom >= srcRect.fBottom) { |
587 | aaFlags |= GrQuadAAFlags::kBottom; |
588 | } |
589 | } |
590 | |
591 | // now offset it to make it "local" to our tmp bitmap |
592 | tileR.offset(-offset.fX, -offset.fY); |
593 | SkMatrix offsetSrcToDst = srcToDst; |
594 | offsetSrcToDst.preTranslate(offset.fX, offset.fY); |
595 | |
596 | draw_texture_producer(context, rtc, clip, ctm, paint, &tileProducer, tileR, |
597 | rectToDraw, nullptr, offsetSrcToDst, aa, aaFlags, constraint, |
598 | wm, fm, doBicubic); |
599 | } |
600 | } |
601 | } |
602 | } |
603 | |
604 | } // anonymous namespace |
605 | |
606 | ////////////////////////////////////////////////////////////////////////////// |
607 | |
608 | void SkGpuDevice::drawImageQuad(const SkImage* image, const SkRect* srcRect, const SkRect* dstRect, |
609 | const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags, |
610 | const SkMatrix* preViewMatrix, const SkPaint& paint, |
611 | SkCanvas::SrcRectConstraint constraint) { |
612 | SkRect src; |
613 | SkRect dst; |
614 | SkMatrix srcToDst; |
615 | ImageDrawMode mode = optimize_sample_area(SkISize::Make(image->width(), image->height()), |
616 | srcRect, dstRect, dstClip, &src, &dst, &srcToDst); |
617 | if (mode == ImageDrawMode::kSkip) { |
618 | return; |
619 | } |
620 | |
621 | if (src.contains(image->bounds())) { |
622 | constraint = SkCanvas::kFast_SrcRectConstraint; |
623 | } |
624 | // Depending on the nature of image, it can flow through more or less optimal pipelines |
625 | GrSamplerState::WrapMode wrapMode = mode == ImageDrawMode::kDecal |
626 | ? GrSamplerState::WrapMode::kClampToBorder |
627 | : GrSamplerState::WrapMode::kClamp; |
628 | |
629 | // Get final CTM matrix |
630 | SkMatrix ctm = this->localToDevice(); |
631 | if (preViewMatrix) { |
632 | ctm.preConcat(*preViewMatrix); |
633 | } |
634 | |
635 | bool doBicubic; |
636 | GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode( |
637 | image->width(), image->height(), paint.getFilterQuality(), ctm, srcToDst, |
638 | fContext->priv().options().fSharpenMipmappedTextures, &doBicubic); |
639 | |
640 | auto clip = this->clip(); |
641 | |
642 | // YUVA images can be stored in multiple images with different plane resolutions, so this |
643 | // uses an effect to combine them dynamically on the GPU. This is done before requesting a |
644 | // pinned texture proxy because YUV images force-flatten to RGBA in that scenario. |
645 | if (as_IB(image)->isYUVA()) { |
646 | SK_HISTOGRAM_BOOLEAN("DrawTiled" , false); |
647 | LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality()); |
648 | |
649 | GrYUVAImageTextureMaker maker(fContext.get(), image); |
650 | draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint, |
651 | &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint, |
652 | wrapMode, fm, doBicubic); |
653 | return; |
654 | } |
655 | |
656 | // Pinned texture proxies can be rendered directly as textures, or with relatively simple |
657 | // adjustments applied to the image content (scaling, mipmaps, color space, etc.) |
658 | uint32_t pinnedUniqueID; |
659 | if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) { |
660 | SK_HISTOGRAM_BOOLEAN("DrawTiled" , false); |
661 | LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality()); |
662 | |
663 | GrColorInfo colorInfo; |
664 | if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) { |
665 | SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType); |
666 | colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(), |
667 | image->imageInfo().refColorSpace()); |
668 | } else { |
669 | colorInfo = GrColorInfo(image->imageInfo().colorInfo()); |
670 | } |
671 | |
672 | GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID); |
673 | draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint, |
674 | &adjuster, src, dst, dstClip, srcToDst, aa, aaFlags, constraint, |
675 | wrapMode, fm, doBicubic); |
676 | return; |
677 | } |
678 | |
679 | // Next up, determine if the image must be tiled |
680 | { |
681 | // If image is explicitly already texture backed then we shouldn't get here. |
682 | SkASSERT(!image->isTextureBacked()); |
683 | |
684 | int tileFilterPad; |
685 | if (doBicubic) { |
686 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
687 | } else if (GrSamplerState::Filter::kNearest == fm) { |
688 | tileFilterPad = 0; |
689 | } else { |
690 | tileFilterPad = 1; |
691 | } |
692 | int maxTileSize = fContext->priv().caps()->maxTileSize() - 2 * tileFilterPad; |
693 | int tileSize; |
694 | SkIRect clippedSubset; |
695 | if (should_tile_image_id(fContext.get(), SkISize::Make(fRenderTargetContext->width(), |
696 | fRenderTargetContext->height()), |
697 | clip, image->unique(), image->dimensions(), ctm, srcToDst, &src, |
698 | maxTileSize, &tileSize, &clippedSubset)) { |
699 | // Extract pixels on the CPU, since we have to split into separate textures before |
700 | // sending to the GPU. |
701 | SkBitmap bm; |
702 | if (as_IB(image)->getROPixels(&bm)) { |
703 | // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram |
704 | SK_HISTOGRAM_BOOLEAN("DrawTiled" , true); |
705 | LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality()); |
706 | draw_tiled_bitmap(fContext.get(), fRenderTargetContext.get(), clip, bm, tileSize, |
707 | ctm, srcToDst, src, clippedSubset, paint, aa, constraint, |
708 | wrapMode, fm, doBicubic); |
709 | return; |
710 | } |
711 | } |
712 | } |
713 | |
714 | // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry. |
715 | SK_HISTOGRAM_BOOLEAN("DrawTiled" , false); |
716 | LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality()); |
717 | |
718 | // Lazily generated images must get drawn as a texture producer that handles the final |
719 | // texture creation. |
720 | if (image->isLazyGenerated()) { |
721 | GrImageTextureMaker maker(fContext.get(), image, GrImageTexGenPolicy::kDraw); |
722 | draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint, |
723 | &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint, |
724 | wrapMode, fm, doBicubic); |
725 | return; |
726 | } |
727 | |
728 | SkBitmap bm; |
729 | if (as_IB(image)->getROPixels(&bm)) { |
730 | GrBitmapTextureMaker maker(fContext.get(), bm, GrImageTexGenPolicy::kDraw); |
731 | draw_texture_producer(fContext.get(), fRenderTargetContext.get(), clip, ctm, paint, |
732 | &maker, src, dst, dstClip, srcToDst, aa, aaFlags, constraint, |
733 | wrapMode, fm, doBicubic); |
734 | } |
735 | |
736 | // Otherwise don't know how to draw it |
737 | } |
738 | |
739 | void SkGpuDevice::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count, |
740 | const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
741 | const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) { |
742 | SkASSERT(count > 0); |
743 | if (!can_use_draw_texture(paint)) { |
744 | // Send every entry through drawImageQuad() to handle the more complicated paint |
745 | int dstClipIndex = 0; |
746 | for (int i = 0; i < count; ++i) { |
747 | // Only no clip or quad clip are supported |
748 | SkASSERT(!set[i].fHasClip || dstClips); |
749 | SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices); |
750 | |
751 | SkTCopyOnFirstWrite<SkPaint> entryPaint(paint); |
752 | if (set[i].fAlpha != 1.f) { |
753 | auto paintAlpha = paint.getAlphaf(); |
754 | entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha); |
755 | } |
756 | // Always send GrAA::kYes to preserve seaming across tiling in MSAA |
757 | this->drawImageQuad( |
758 | set[i].fImage.get(), &set[i].fSrcRect, &set[i].fDstRect, |
759 | set[i].fHasClip ? dstClips + dstClipIndex : nullptr, GrAA::kYes, |
760 | SkToGrQuadAAFlags(set[i].fAAFlags), |
761 | set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex, |
762 | *entryPaint, constraint); |
763 | dstClipIndex += 4 * set[i].fHasClip; |
764 | } |
765 | return; |
766 | } |
767 | |
768 | GrSamplerState::Filter filter = kNone_SkFilterQuality == paint.getFilterQuality() ? |
769 | GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp; |
770 | SkBlendMode mode = paint.getBlendMode(); |
771 | |
772 | SkAutoTArray<GrRenderTargetContext::TextureSetEntry> textures(count); |
773 | // We accumulate compatible proxies until we find an an incompatible one or reach the end and |
774 | // issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy |
775 | // switches that occur within the 'n' entries. |
776 | int base = 0, n = 0, p = 0; |
777 | auto draw = [&](int nextBase) { |
778 | if (n > 0) { |
779 | auto textureXform = GrColorSpaceXform::Make( |
780 | set[base].fImage->colorSpace(), set[base].fImage->alphaType(), |
781 | fRenderTargetContext->colorInfo().colorSpace(), kPremul_SkAlphaType); |
782 | fRenderTargetContext->drawTextureSet(this->clip(), textures.get() + base, n, p, |
783 | filter, mode, GrAA::kYes, constraint, |
784 | this->localToDevice(), std::move(textureXform)); |
785 | } |
786 | base = nextBase; |
787 | n = 0; |
788 | p = 0; |
789 | }; |
790 | int dstClipIndex = 0; |
791 | for (int i = 0; i < count; ++i) { |
792 | SkASSERT(!set[i].fHasClip || dstClips); |
793 | SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices); |
794 | |
795 | // Manage the dst clip pointer tracking before any continues are used so we don't lose |
796 | // our place in the dstClips array. |
797 | const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr; |
798 | dstClipIndex += 4 * set[i].fHasClip; |
799 | |
800 | // The default SkBaseDevice implementation is based on drawImageRect which does not allow |
801 | // non-sorted src rects. TODO: Decide this is OK or make sure we handle it. |
802 | if (!set[i].fSrcRect.isSorted()) { |
803 | draw(i + 1); |
804 | continue; |
805 | } |
806 | |
807 | GrSurfaceProxyView view; |
808 | const SkImage_Base* image = as_IB(set[i].fImage.get()); |
809 | // Extract view from image, but skip YUV images so they get processed through |
810 | // drawImageQuad and the proper effect to dynamically sample their planes. |
811 | if (!image->isYUVA()) { |
812 | uint32_t uniqueID; |
813 | view = image->refPinnedView(this->context(), &uniqueID); |
814 | if (!view) { |
815 | view = image->refView(this->context(), GrMipMapped::kNo); |
816 | } |
817 | } |
818 | |
819 | if (!view) { |
820 | // This image can't go through the texture op, send through general image pipeline |
821 | // after flushing current batch. |
822 | draw(i + 1); |
823 | SkTCopyOnFirstWrite<SkPaint> entryPaint(paint); |
824 | if (set[i].fAlpha != 1.f) { |
825 | auto paintAlpha = paint.getAlphaf(); |
826 | entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha); |
827 | } |
828 | this->drawImageQuad( |
829 | image, &set[i].fSrcRect, &set[i].fDstRect, clip, GrAA::kYes, |
830 | SkToGrQuadAAFlags(set[i].fAAFlags), |
831 | set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex, |
832 | *entryPaint, constraint); |
833 | continue; |
834 | } |
835 | |
836 | textures[i].fProxyView = std::move(view); |
837 | textures[i].fSrcAlphaType = image->alphaType(); |
838 | textures[i].fSrcRect = set[i].fSrcRect; |
839 | textures[i].fDstRect = set[i].fDstRect; |
840 | textures[i].fDstClipQuad = clip; |
841 | textures[i].fPreViewMatrix = |
842 | set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex; |
843 | textures[i].fAlpha = set[i].fAlpha * paint.getAlphaf(); |
844 | textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags); |
845 | |
846 | if (n > 0 && |
847 | (!GrTextureProxy::ProxiesAreCompatibleAsDynamicState( |
848 | textures[i].fProxyView.proxy(), |
849 | textures[base].fProxyView.proxy()) || |
850 | textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() || |
851 | set[i].fImage->alphaType() != set[base].fImage->alphaType() || |
852 | !SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) { |
853 | draw(i); |
854 | } |
855 | // Whether or not we submitted a draw in the above if(), this ith entry is in the current |
856 | // set being accumulated so increment n, and increment p if proxies are different. |
857 | ++n; |
858 | if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) { |
859 | // First proxy or a different proxy (that is compatible, otherwise we'd have drawn up |
860 | // to i - 1). |
861 | ++p; |
862 | } |
863 | } |
864 | draw(count); |
865 | } |
866 | |