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