blob: da5382e1d80595945e6c93b505753a8903e38b00 [file] [log] [blame]
Ethan Nicholas82399462017-10-16 12:35:44 -04001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas82399462017-10-16 12:35:44 -04003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas82399462017-10-16 12:35:44 -040011#ifndef GrRectBlurEffect_DEFINED
12#define GrRectBlurEffect_DEFINED
John Stiles88183902020-06-10 16:40:38 -040013
Mike Reed46f5c5f2020-02-20 15:42:29 -050014#include "include/core/SkM44.h"
John Stiles88183902020-06-10 16:40:38 -040015#include "include/core/SkTypes.h"
Ethan Nicholas82399462017-10-16 12:35:44 -040016
Brian Salomonb2d5d402019-09-10 10:11:52 -040017#include <cmath>
18#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkScalar.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040020#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkBlurMask.h"
Brian Salomone7366842019-09-04 11:20:45 -040022#include "src/core/SkMathPriv.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050023#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrProxyProvider.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050025#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrShaderCaps.h"
Brian Salomon86b2f392020-06-16 16:01:07 -040027#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrFragmentProcessor.h"
John Stiles88183902020-06-10 16:40:38 -040030
Ethan Nicholas82399462017-10-16 12:35:44 -040031class GrRectBlurEffect : public GrFragmentProcessor {
32public:
Brian Salomon86b2f392020-06-16 16:01:07 -040033 static std::unique_ptr<GrFragmentProcessor> MakeIntegralFP(GrRecordingContext* context,
34 float sixSigma) {
Brian Salomonb2d5d402019-09-10 10:11:52 -040035 // The texture we're producing represents the integral of a normal distribution over a
36 // six-sigma range centered at zero. We want enough resolution so that the linear
37 // interpolation done in texture lookup doesn't introduce noticeable artifacts. We
38 // conservatively choose to have 2 texels for each dst pixel.
39 int minWidth = 2 * sk_float_ceil2int(sixSigma);
40 // Bin by powers of 2 with a minimum so we get good profile reuse.
Brian Osman788b9162020-02-07 10:36:46 -050041 int width = std::max(SkNextPow2(minWidth), 32);
Brian Salomone7366842019-09-04 11:20:45 -040042
43 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
44 GrUniqueKey key;
45 GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask");
Brian Salomonb2d5d402019-09-10 10:11:52 -040046 builder[0] = width;
Brian Salomone7366842019-09-04 11:20:45 -040047 builder.finish();
48
Brian Salomon86b2f392020-06-16 16:01:07 -040049 SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f);
50
Greg Daniel6f5441a2020-01-28 17:02:49 -050051 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomondf1bd6d2020-03-26 20:37:01 -040052 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
Greg Daniel43956122020-02-11 15:49:27 -050053 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
54 GrColorType::kAlpha_8);
Brian Salomon86b2f392020-06-16 16:01:07 -040055 GrSurfaceProxyView view{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
56 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m,
Brian Salomona3b02f52020-07-15 16:02:01 -040057 GrSamplerState::Filter::kLinear);
Brian Salomone7366842019-09-04 11:20:45 -040058 }
Greg Daniel43956122020-02-11 15:49:27 -050059
60 SkBitmap bitmap;
61 if (!bitmap.tryAllocPixels(SkImageInfo::MakeA8(width, 1))) {
62 return {};
63 }
64 *bitmap.getAddr8(0, 0) = 255;
65 const float invWidth = 1.f / width;
66 for (int i = 1; i < width - 1; ++i) {
67 float x = (i + 0.5f) * invWidth;
68 x = (-6 * x + 3) * SK_ScalarRoot2Over2;
69 float integral = 0.5f * (std::erf(x) + 1.f);
70 *bitmap.getAddr8(i, 0) = SkToU8(sk_float_round2int(255.f * integral));
71 }
72 *bitmap.getAddr8(width - 1, 0) = 0;
73 bitmap.setImmutable();
74
Brian Salomonbc074a62020-03-18 10:06:13 -040075 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -040076 auto view = maker.view(GrMipmapped::kNo);
Greg Daniel43956122020-02-11 15:49:27 -050077 if (!view) {
78 return {};
79 }
80 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
81 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Brian Salomon86b2f392020-06-16 16:01:07 -040082 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m,
Brian Salomona3b02f52020-07-15 16:02:01 -040083 GrSamplerState::Filter::kLinear);
Brian Salomone7366842019-09-04 11:20:45 -040084 }
85
John Stilese7b4d732020-06-10 12:46:40 -040086 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP,
87 GrRecordingContext* context,
Brian Osmanff0717f2020-03-27 11:08:38 -040088 const GrShaderCaps& caps,
89 const SkRect& rect,
Mike Kleind6ab77a2019-03-21 08:18:24 -050090 float sigma) {
Brian Salomonb2d5d402019-09-10 10:11:52 -040091 SkASSERT(rect.isSorted());
Brian Salomonff6a7cd2018-05-10 09:42:27 -040092 if (!caps.floatIs32Bits()) {
Brian Salomon2c596592019-08-13 20:05:04 -040093 // We promote the math that gets us into the Gaussian space to full float when the rect
94 // coords are large. If we don't have full float then fail. We could probably clip the
95 // rect to an outset device bounds instead.
Brian Salomonff6a7cd2018-05-10 09:42:27 -040096 if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f ||
Brian Salomon2c596592019-08-13 20:05:04 -040097 SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) {
Brian Salomonff6a7cd2018-05-10 09:42:27 -040098 return nullptr;
99 }
100 }
Ethan Nicholas82399462017-10-16 12:35:44 -0400101
Brian Salomonb2d5d402019-09-10 10:11:52 -0400102 const float sixSigma = 6 * sigma;
Brian Salomon86b2f392020-06-16 16:01:07 -0400103 std::unique_ptr<GrFragmentProcessor> integral = MakeIntegralFP(context, sixSigma);
Brian Salomonb2d5d402019-09-10 10:11:52 -0400104 if (!integral) {
Ethan Nicholas82399462017-10-16 12:35:44 -0400105 return nullptr;
106 }
107
Brian Salomonb2d5d402019-09-10 10:11:52 -0400108 // In the fast variant we think of the midpoint of the integral texture as aligning
109 // with the closest rect edge both in x and y. To simplify texture coord calculation we
110 // inset the rect so that the edge of the inset rect corresponds to t = 0 in the texture.
111 // It actually simplifies things a bit in the !isFast case, too.
112 float threeSigma = sixSigma / 2;
113 SkRect insetRect = {rect.fLeft + threeSigma, rect.fTop + threeSigma,
114 rect.fRight - threeSigma, rect.fBottom - threeSigma};
115
116 // In our fast variant we find the nearest horizontal and vertical edges and for each
117 // do a lookup in the integral texture for each and multiply them. When the rect is
118 // less than 6 sigma wide then things aren't so simple and we have to consider both the
119 // left and right edge of the rectangle (and similar in y).
120 bool isFast = insetRect.isSorted();
Brian Salomonb2d5d402019-09-10 10:11:52 -0400121 return std::unique_ptr<GrFragmentProcessor>(
Brian Salomon86b2f392020-06-16 16:01:07 -0400122 new GrRectBlurEffect(std::move(inputFP), insetRect, std::move(integral), isFast,
Brian Salomona3b02f52020-07-15 16:02:01 -0400123 GrSamplerState::Filter::kLinear));
Ethan Nicholas82399462017-10-16 12:35:44 -0400124 }
125 GrRectBlurEffect(const GrRectBlurEffect& src);
126 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500127 const char* name() const override { return "RectBlurEffect"; }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400128 SkRect rect;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400129 bool isFast;
Ethan Nicholas82399462017-10-16 12:35:44 -0400130
131private:
John Stilese7b4d732020-06-10 12:46:40 -0400132 GrRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
133 SkRect rect,
Brian Salomon86b2f392020-06-16 16:01:07 -0400134 std::unique_ptr<GrFragmentProcessor> integral,
Brian Osmanff0717f2020-03-27 11:08:38 -0400135 bool isFast,
Brian Salomone7366842019-09-04 11:20:45 -0400136 GrSamplerState samplerParams)
Ethan Nicholas82399462017-10-16 12:35:44 -0400137 : INHERITED(kGrRectBlurEffect_ClassID,
John Stilese7b4d732020-06-10 12:46:40 -0400138 (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get())
139 : kAll_OptimizationFlags) &
140 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400141 , rect(rect)
Brian Salomonb2d5d402019-09-10 10:11:52 -0400142 , isFast(isFast) {
Brian Osman12c5d292020-07-13 16:11:35 -0400143 this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough());
Brian Salomon86b2f392020-06-16 16:01:07 -0400144 SkASSERT(integral);
Brian Osman12c5d292020-07-13 16:11:35 -0400145 this->registerChild(std::move(integral), SkSL::SampleUsage::Explicit());
Brian Salomone7366842019-09-04 11:20:45 -0400146 }
Ethan Nicholas82399462017-10-16 12:35:44 -0400147 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
148 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
149 bool onIsEqual(const GrFragmentProcessor&) const override;
150 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Ethan Nicholas82399462017-10-16 12:35:44 -0400151 typedef GrFragmentProcessor INHERITED;
152};
153#endif