1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkGpuBlurUtils.h"
#include "SkRect.h"
#if SK_SUPPORT_GPU
#include "GrCaps.h"
#include "GrContext.h"
#include "GrFixedClip.h"
#include "GrRenderTargetContext.h"
#include "GrRenderTargetContextPriv.h"
#include "effects/GrGaussianConvolutionFragmentProcessor.h"
#include "effects/GrMatrixConvolutionEffect.h"
#define MAX_BLUR_SIGMA 4.0f
using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
static void scale_irect_roundout(SkIRect* rect, float xScale, float yScale) {
rect->fLeft = SkScalarFloorToInt(rect->fLeft * xScale);
rect->fTop = SkScalarFloorToInt(rect->fTop * yScale);
rect->fRight = SkScalarCeilToInt(rect->fRight * xScale);
rect->fBottom = SkScalarCeilToInt(rect->fBottom * yScale);
}
static void scale_irect(SkIRect* rect, int xScale, int yScale) {
rect->fLeft *= xScale;
rect->fTop *= yScale;
rect->fRight *= xScale;
rect->fBottom *= yScale;
}
#ifdef SK_DEBUG
static inline int is_even(int x) { return !(x & 1); }
#endif
static void shrink_irect_by_2(SkIRect* rect, bool xAxis, bool yAxis) {
if (xAxis) {
SkASSERT(is_even(rect->fLeft) && is_even(rect->fRight));
rect->fLeft /= 2;
rect->fRight /= 2;
}
if (yAxis) {
SkASSERT(is_even(rect->fTop) && is_even(rect->fBottom));
rect->fTop /= 2;
rect->fBottom /= 2;
}
}
static float adjust_sigma(float sigma, int maxTextureSize, int *scaleFactor, int *radius) {
*scaleFactor = 1;
while (sigma > MAX_BLUR_SIGMA) {
*scaleFactor *= 2;
sigma *= 0.5f;
if (*scaleFactor > maxTextureSize) {
*scaleFactor = maxTextureSize;
sigma = MAX_BLUR_SIGMA;
}
}
*radius = static_cast<int>(ceilf(sigma * 3.0f));
SkASSERT(*radius <= GrGaussianConvolutionFragmentProcessor::kMaxKernelRadius);
return sigma;
}
static void convolve_gaussian_1d(GrRenderTargetContext* renderTargetContext,
const GrClip& clip,
const SkIRect& dstRect,
const SkIPoint& srcOffset,
sk_sp<GrTextureProxy> proxy,
Direction direction,
int radius,
float sigma,
GrTextureDomain::Mode mode,
int bounds[2]) {
GrPaint paint;
paint.setGammaCorrect(renderTargetContext->colorSpaceInfo().isGammaCorrect());
std::unique_ptr<GrFragmentProcessor> conv(GrGaussianConvolutionFragmentProcessor::Make(
std::move(proxy), direction, radius, sigma, mode, bounds));
paint.addColorFragmentProcessor(std::move(conv));
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
-SkIntToScalar(srcOffset.y()));
renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
SkRect::Make(dstRect), localMatrix);
}
static void convolve_gaussian_2d(GrRenderTargetContext* renderTargetContext,
const GrClip& clip,
const SkIRect& dstRect,
const SkIPoint& srcOffset,
sk_sp<GrTextureProxy> proxy,
int radiusX,
int radiusY,
SkScalar sigmaX,
SkScalar sigmaY,
const SkIRect& srcBounds,
GrTextureDomain::Mode mode) {
SkMatrix localMatrix = SkMatrix::MakeTrans(-SkIntToScalar(srcOffset.x()),
-SkIntToScalar(srcOffset.y()));
SkISize size = SkISize::Make(2 * radiusX + 1, 2 * radiusY + 1);
SkIPoint kernelOffset = SkIPoint::Make(radiusX, radiusY);
GrPaint paint;
paint.setGammaCorrect(renderTargetContext->colorSpaceInfo().isGammaCorrect());
auto conv = GrMatrixConvolutionEffect::MakeGaussian(std::move(proxy), srcBounds, size, 1.0, 0.0,
kernelOffset, mode, true, sigmaX, sigmaY);
paint.addColorFragmentProcessor(std::move(conv));
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
SkRect::Make(dstRect), localMatrix);
}
static void convolve_gaussian(GrRenderTargetContext* renderTargetContext,
const GrClip& clip,
const SkIRect& srcRect,
sk_sp<GrTextureProxy> proxy,
Direction direction,
int radius,
float sigma,
const SkIRect& srcBounds,
const SkIPoint& srcOffset,
GrTextureDomain::Mode mode) {
int bounds[2] = { 0, 0 };
SkIRect dstRect = SkIRect::MakeWH(srcRect.width(), srcRect.height());
if (GrTextureDomain::kIgnore_Mode == mode) {
convolve_gaussian_1d(renderTargetContext, clip, dstRect, srcOffset,
std::move(proxy), direction, radius, sigma,
GrTextureDomain::kIgnore_Mode, bounds);
return;
}
SkIRect midRect = srcBounds, leftRect, rightRect;
midRect.offset(srcOffset);
SkIRect topRect, bottomRect;
if (Direction::kX == direction) {
bounds[0] = srcBounds.left();
bounds[1] = srcBounds.right();
topRect = SkIRect::MakeLTRB(0, 0, dstRect.right(), midRect.top());
bottomRect = SkIRect::MakeLTRB(0, midRect.bottom(), dstRect.right(), dstRect.bottom());
midRect.inset(radius, 0);
leftRect = SkIRect::MakeLTRB(0, midRect.top(), midRect.left(), midRect.bottom());
rightRect =
SkIRect::MakeLTRB(midRect.right(), midRect.top(), dstRect.width(), midRect.bottom());
dstRect.fTop = midRect.top();
dstRect.fBottom = midRect.bottom();
} else {
bounds[0] = srcBounds.top();
bounds[1] = srcBounds.bottom();
topRect = SkIRect::MakeLTRB(0, 0, midRect.left(), dstRect.bottom());
bottomRect = SkIRect::MakeLTRB(midRect.right(), 0, dstRect.right(), dstRect.bottom());
midRect.inset(0, radius);
leftRect = SkIRect::MakeLTRB(midRect.left(), 0, midRect.right(), midRect.top());
rightRect =
SkIRect::MakeLTRB(midRect.left(), midRect.bottom(), midRect.right(), dstRect.height());
dstRect.fLeft = midRect.left();
dstRect.fRight = midRect.right();
}
if (!topRect.isEmpty()) {
renderTargetContext->clear(&topRect, 0, false);
}
if (!bottomRect.isEmpty()) {
renderTargetContext->clear(&bottomRect, 0, false);
}
if (midRect.isEmpty()) {
// Blur radius covers srcBounds; use bounds over entire draw
convolve_gaussian_1d(renderTargetContext, clip, dstRect, srcOffset,
std::move(proxy), direction, radius, sigma, mode, bounds);
} else {
// Draw right and left margins with bounds; middle without.
convolve_gaussian_1d(renderTargetContext, clip, leftRect, srcOffset,
proxy, direction, radius, sigma, mode, bounds);
convolve_gaussian_1d(renderTargetContext, clip, rightRect, srcOffset,
proxy, direction, radius, sigma, mode, bounds);
convolve_gaussian_1d(renderTargetContext, clip, midRect, srcOffset,
std::move(proxy), direction, radius, sigma,
GrTextureDomain::kIgnore_Mode, bounds);
}
}
namespace SkGpuBlurUtils {
sk_sp<GrRenderTargetContext> GaussianBlur(GrContext* context,
sk_sp<GrTextureProxy> srcProxy,
sk_sp<SkColorSpace> colorSpace,
const SkIRect& dstBounds,
const SkIRect& srcBounds,
float sigmaX,
float sigmaY,
GrTextureDomain::Mode mode,
SkBackingFit fit) {
SkASSERT(context);
SkIRect clearRect;
int scaleFactorX, radiusX;
int scaleFactorY, radiusY;
int maxTextureSize = context->caps()->maxTextureSize();
sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX);
sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY);
SkASSERT(sigmaX || sigmaY);
SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y());
SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height());
SkIRect localSrcBounds;
SkIRect srcRect;
if (GrTextureDomain::kIgnore_Mode == mode) {
srcRect = localDstBounds;
} else {
srcRect = localSrcBounds = srcBounds;
srcRect.offset(srcOffset);
}
scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
scale_irect(&srcRect, scaleFactorX, scaleFactorY);
// setup new clip
GrFixedClip clip(localDstBounds);
GrPixelConfig config = srcProxy->config();
if (GrPixelConfigIsSRGB(config) && !colorSpace) {
// If the context doesn't have sRGB write control, and we make an sRGB RTC, we won't be
// able to suppress the linear -> sRGB conversion out of the shader. Not all GL drivers
// have that feature, and Vulkan is missing it entirely. To keep things simple, switch to
// a non-sRGB destination, to ensure correct blurring behavior.
config = kRGBA_8888_GrPixelConfig;
}
SkASSERT(kBGRA_8888_GrPixelConfig == config || kRGBA_8888_GrPixelConfig == config ||
kRGBA_4444_GrPixelConfig == config || kRGB_565_GrPixelConfig == config ||
kSRGBA_8888_GrPixelConfig == config || kSBGRA_8888_GrPixelConfig == config ||
kRGBA_half_GrPixelConfig == config || kAlpha_8_GrPixelConfig == config);
const int width = dstBounds.width();
const int height = dstBounds.height();
sk_sp<GrRenderTargetContext> dstRenderTargetContext;
// For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just
// launch a single non separable kernel vs two launches
if (sigmaX > 0.0f && sigmaY > 0.0f &&
(2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) {
// We shouldn't be scaling because this is a small size blur
SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY));
dstRenderTargetContext = context->makeDeferredRenderTargetContext(fit, width, height,
config, colorSpace);
if (!dstRenderTargetContext) {
return nullptr;
}
convolve_gaussian_2d(dstRenderTargetContext.get(),
clip, localDstBounds, srcOffset, std::move(srcProxy),
radiusX, radiusY, sigmaX, sigmaY, srcBounds, mode);
return dstRenderTargetContext;
}
SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY));
// GrTextureDomainEffect does not support kRepeat_Mode with GrSamplerState::Filter.
GrTextureDomain::Mode modeForScaling =
GrTextureDomain::kRepeat_Mode == mode ? GrTextureDomain::kDecal_Mode : mode;
for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
SkIRect dstRect(srcRect);
shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY);
dstRenderTargetContext = context->makeDeferredRenderTargetContext(
fit,
SkTMin(dstRect.fRight, width),
SkTMin(dstRect.fBottom, height),
config, colorSpace);
if (!dstRenderTargetContext) {
return nullptr;
}
GrPaint paint;
paint.setGammaCorrect(dstRenderTargetContext->colorSpaceInfo().isGammaCorrect());
if (GrTextureDomain::kIgnore_Mode != mode && i == 1) {
SkRect domain = SkRect::Make(localSrcBounds);
domain.inset((i < scaleFactorX) ? SK_ScalarHalf : 0.0f,
(i < scaleFactorY) ? SK_ScalarHalf : 0.0f);
auto fp = GrTextureDomainEffect::Make(std::move(srcProxy),
SkMatrix::I(),
domain,
modeForScaling,
GrSamplerState::Filter::kBilerp);
paint.addColorFragmentProcessor(std::move(fp));
srcRect.offset(-srcOffset);
srcOffset.set(0, 0);
} else {
paint.addColorTextureProcessor(std::move(srcProxy), SkMatrix::I(),
GrSamplerState::ClampBilerp());
}
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
dstRenderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
SkRect::Make(dstRect), SkRect::Make(srcRect));
srcProxy = dstRenderTargetContext->asTextureProxyRef();
if (!srcProxy) {
return nullptr;
}
srcRect = dstRect;
localSrcBounds = srcRect;
}
srcRect = localDstBounds;
scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
if (sigmaX > 0.0f) {
if (scaleFactorX > 1) {
SkASSERT(dstRenderTargetContext);
// Clear out a radius to the right of the srcRect to prevent the
// X convolution from reading garbage.
clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop,
radiusX, srcRect.height());
dstRenderTargetContext->priv().absClear(&clearRect, 0x0);
}
SkASSERT(srcRect.width() <= width && srcRect.height() <= height);
dstRenderTargetContext = context->makeDeferredRenderTargetContext(fit, srcRect.width(),
srcRect.height(),
config, colorSpace);
if (!dstRenderTargetContext) {
return nullptr;
}
convolve_gaussian(dstRenderTargetContext.get(), clip, srcRect, std::move(srcProxy),
Direction::kX, radiusX, sigmaX, localSrcBounds, srcOffset, mode);
srcProxy = dstRenderTargetContext->asTextureProxyRef();
if (!srcProxy) {
return nullptr;
}
srcRect.offsetTo(0, 0);
localSrcBounds = srcRect;
if (GrTextureDomain::kClamp_Mode == mode) {
// We need to adjust bounds because we only fill part of the srcRect in x-pass.
localSrcBounds.inset(0, radiusY);
}
srcOffset.set(0, 0);
}
if (sigmaY > 0.0f) {
if (scaleFactorY > 1 || sigmaX > 0.0f) {
SkASSERT(dstRenderTargetContext);
// Clear out a radius below the srcRect to prevent the Y
// convolution from reading garbage.
clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom,
srcRect.width(), radiusY);
dstRenderTargetContext->priv().absClear(&clearRect, 0x0);
}
SkASSERT(srcRect.width() <= width && srcRect.height() <= height);
dstRenderTargetContext = context->makeDeferredRenderTargetContext(fit, srcRect.width(),
srcRect.height(),
config, colorSpace);
if (!dstRenderTargetContext) {
return nullptr;
}
convolve_gaussian(dstRenderTargetContext.get(), clip, srcRect, std::move(srcProxy),
Direction::kY, radiusY, sigmaY, localSrcBounds, srcOffset, mode);
srcProxy = dstRenderTargetContext->asTextureProxyRef();
if (!srcProxy) {
return nullptr;
}
srcRect.offsetTo(0, 0);
}
SkASSERT(dstRenderTargetContext);
SkASSERT(srcProxy.get() == dstRenderTargetContext->asTextureProxy());
if (scaleFactorX > 1 || scaleFactorY > 1) {
// Clear one pixel to the right and below, to accommodate bilinear upsampling.
// TODO: it seems like we should actually be clamping here rather than darkening
// the bottom right edges.
clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
dstRenderTargetContext->priv().absClear(&clearRect, 0x0);
clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height());
dstRenderTargetContext->priv().absClear(&clearRect, 0x0);
SkIRect dstRect(srcRect);
scale_irect(&dstRect, scaleFactorX, scaleFactorY);
dstRenderTargetContext = context->makeDeferredRenderTargetContext(
fit, SkTMin(dstRect.width(), width),
SkTMin(dstRect.height(), height),
config, colorSpace);
if (!dstRenderTargetContext) {
return nullptr;
}
GrPaint paint;
paint.setGammaCorrect(dstRenderTargetContext->colorSpaceInfo().isGammaCorrect());
if (GrTextureDomain::kIgnore_Mode != mode) {
SkRect domain = SkRect::Make(localSrcBounds);
auto fp = GrTextureDomainEffect::Make(std::move(srcProxy),
SkMatrix::I(),
domain,
modeForScaling,
GrSamplerState::Filter::kBilerp);
paint.addColorFragmentProcessor(std::move(fp));
} else {
// FIXME: this should be mitchell, not bilinear.
paint.addColorTextureProcessor(std::move(srcProxy), SkMatrix::I(),
GrSamplerState::ClampBilerp());
}
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
dstRenderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
SkRect::Make(dstRect), SkRect::Make(srcRect));
srcProxy = dstRenderTargetContext->asTextureProxyRef();
if (!srcProxy) {
return nullptr;
}
srcRect = dstRect;
}
return dstRenderTargetContext;
}
}
#endif
|