blob: 93491fcac48539e958a24cc8d27fa7ff5c4e5246 [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"
Robert Phillips4ef21ed2020-09-02 11:11:29 -040022#include "src/core/SkBlurPriv.h"
Brian Salomone7366842019-09-04 11:20:45 -040023#include "src/core/SkMathPriv.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050024#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrProxyProvider.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050026#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrShaderCaps.h"
Brian Salomon86b2f392020-06-16 16:01:07 -040028#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrFragmentProcessor.h"
John Stiles88183902020-06-10 16:40:38 -040031
Ethan Nicholas82399462017-10-16 12:35:44 -040032class GrRectBlurEffect : public GrFragmentProcessor {
33public:
Brian Salomon86b2f392020-06-16 16:01:07 -040034 static std::unique_ptr<GrFragmentProcessor> MakeIntegralFP(GrRecordingContext* context,
35 float sixSigma) {
Robert Phillips4ef21ed2020-09-02 11:11:29 -040036 int width = SkCreateIntegralTable(sixSigma, nullptr);
Brian Salomone7366842019-09-04 11:20:45 -040037
38 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
39 GrUniqueKey key;
40 GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask");
Brian Salomonb2d5d402019-09-10 10:11:52 -040041 builder[0] = width;
Brian Salomone7366842019-09-04 11:20:45 -040042 builder.finish();
43
Brian Salomon86b2f392020-06-16 16:01:07 -040044 SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f);
45
Greg Daniel6f5441a2020-01-28 17:02:49 -050046 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomondf1bd6d2020-03-26 20:37:01 -040047 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
Greg Daniel43956122020-02-11 15:49:27 -050048 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
49 GrColorType::kAlpha_8);
Brian Salomon86b2f392020-06-16 16:01:07 -040050 GrSurfaceProxyView view{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
51 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m,
Brian Salomona3b02f52020-07-15 16:02:01 -040052 GrSamplerState::Filter::kLinear);
Brian Salomone7366842019-09-04 11:20:45 -040053 }
Greg Daniel43956122020-02-11 15:49:27 -050054
55 SkBitmap bitmap;
Robert Phillips4ef21ed2020-09-02 11:11:29 -040056 if (!SkCreateIntegralTable(sixSigma, &bitmap)) {
Greg Daniel43956122020-02-11 15:49:27 -050057 return {};
58 }
Greg Daniel43956122020-02-11 15:49:27 -050059
Brian Salomonbc074a62020-03-18 10:06:13 -040060 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -040061 auto view = maker.view(GrMipmapped::kNo);
Greg Daniel43956122020-02-11 15:49:27 -050062 if (!view) {
63 return {};
64 }
65 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
66 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
Brian Salomon86b2f392020-06-16 16:01:07 -040067 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m,
Brian Salomona3b02f52020-07-15 16:02:01 -040068 GrSamplerState::Filter::kLinear);
Brian Salomone7366842019-09-04 11:20:45 -040069 }
70
John Stilese7b4d732020-06-10 12:46:40 -040071 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP,
72 GrRecordingContext* context,
Brian Osmanff0717f2020-03-27 11:08:38 -040073 const GrShaderCaps& caps,
74 const SkRect& rect,
Mike Kleind6ab77a2019-03-21 08:18:24 -050075 float sigma) {
Brian Salomonb2d5d402019-09-10 10:11:52 -040076 SkASSERT(rect.isSorted());
Brian Salomonff6a7cd2018-05-10 09:42:27 -040077 if (!caps.floatIs32Bits()) {
Brian Salomon2c596592019-08-13 20:05:04 -040078 // We promote the math that gets us into the Gaussian space to full float when the rect
79 // coords are large. If we don't have full float then fail. We could probably clip the
80 // rect to an outset device bounds instead.
Brian Salomonff6a7cd2018-05-10 09:42:27 -040081 if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f ||
Brian Salomon2c596592019-08-13 20:05:04 -040082 SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) {
Brian Salomonff6a7cd2018-05-10 09:42:27 -040083 return nullptr;
84 }
85 }
Ethan Nicholas82399462017-10-16 12:35:44 -040086
Brian Salomonb2d5d402019-09-10 10:11:52 -040087 const float sixSigma = 6 * sigma;
Brian Salomon86b2f392020-06-16 16:01:07 -040088 std::unique_ptr<GrFragmentProcessor> integral = MakeIntegralFP(context, sixSigma);
Brian Salomonb2d5d402019-09-10 10:11:52 -040089 if (!integral) {
Ethan Nicholas82399462017-10-16 12:35:44 -040090 return nullptr;
91 }
92
Brian Salomonb2d5d402019-09-10 10:11:52 -040093 // In the fast variant we think of the midpoint of the integral texture as aligning
94 // with the closest rect edge both in x and y. To simplify texture coord calculation we
95 // inset the rect so that the edge of the inset rect corresponds to t = 0 in the texture.
96 // It actually simplifies things a bit in the !isFast case, too.
97 float threeSigma = sixSigma / 2;
98 SkRect insetRect = {rect.fLeft + threeSigma, rect.fTop + threeSigma,
99 rect.fRight - threeSigma, rect.fBottom - threeSigma};
100
101 // In our fast variant we find the nearest horizontal and vertical edges and for each
102 // do a lookup in the integral texture for each and multiply them. When the rect is
103 // less than 6 sigma wide then things aren't so simple and we have to consider both the
104 // left and right edge of the rectangle (and similar in y).
105 bool isFast = insetRect.isSorted();
Brian Salomonb2d5d402019-09-10 10:11:52 -0400106 return std::unique_ptr<GrFragmentProcessor>(
Brian Salomon86b2f392020-06-16 16:01:07 -0400107 new GrRectBlurEffect(std::move(inputFP), insetRect, std::move(integral), isFast,
Brian Salomona3b02f52020-07-15 16:02:01 -0400108 GrSamplerState::Filter::kLinear));
Ethan Nicholas82399462017-10-16 12:35:44 -0400109 }
110 GrRectBlurEffect(const GrRectBlurEffect& src);
111 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500112 const char* name() const override { return "RectBlurEffect"; }
John Stiles735a5a72020-08-26 10:21:10 -0400113 bool usesExplicitReturn() const override;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400114 SkRect rect;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400115 bool isFast;
Ethan Nicholas82399462017-10-16 12:35:44 -0400116
117private:
John Stilese7b4d732020-06-10 12:46:40 -0400118 GrRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
119 SkRect rect,
Brian Salomon86b2f392020-06-16 16:01:07 -0400120 std::unique_ptr<GrFragmentProcessor> integral,
Brian Osmanff0717f2020-03-27 11:08:38 -0400121 bool isFast,
Brian Salomone7366842019-09-04 11:20:45 -0400122 GrSamplerState samplerParams)
Ethan Nicholas82399462017-10-16 12:35:44 -0400123 : INHERITED(kGrRectBlurEffect_ClassID,
John Stilese7b4d732020-06-10 12:46:40 -0400124 (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get())
125 : kAll_OptimizationFlags) &
126 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400127 , rect(rect)
Brian Salomonb2d5d402019-09-10 10:11:52 -0400128 , isFast(isFast) {
Brian Osman12c5d292020-07-13 16:11:35 -0400129 this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough());
Brian Salomon86b2f392020-06-16 16:01:07 -0400130 SkASSERT(integral);
Brian Osman12c5d292020-07-13 16:11:35 -0400131 this->registerChild(std::move(integral), SkSL::SampleUsage::Explicit());
Brian Salomone7366842019-09-04 11:20:45 -0400132 }
Ethan Nicholas82399462017-10-16 12:35:44 -0400133 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
134 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
135 bool onIsEqual(const GrFragmentProcessor&) const override;
John Stiles776c2842020-08-13 09:16:49 -0400136#if GR_TEST_UTILS
137 SkString onDumpInfo() const override;
138#endif
Ethan Nicholas82399462017-10-16 12:35:44 -0400139 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Ethan Nicholas82399462017-10-16 12:35:44 -0400140 typedef GrFragmentProcessor INHERITED;
141};
142#endif