blob: c75a5c548eb88092aafe578541b43f803015ec0d [file] [log] [blame]
Ethan Nicholas297d6ef2017-12-20 12:00:11 -05001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas297d6ef2017-12-20 12:00:11 -05003 *
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 GrRRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050011#ifndef GrRRectBlurEffect_DEFINED
12#define GrRRectBlurEffect_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 Nicholas297d6ef2017-12-20 12:00:11 -050016
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040017#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkBlurPriv.h"
19#include "src/core/SkGpuBlurUtils.h"
20#include "src/core/SkRRectPriv.h"
21#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrPaint.h"
23#include "src/gpu/GrProxyProvider.h"
24#include "src/gpu/GrRecordingContextPriv.h"
25#include "src/gpu/GrRenderTargetContext.h"
26#include "src/gpu/GrStyle.h"
Brian Salomon99b04702020-06-18 10:12:47 -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 Nicholas297d6ef2017-12-20 12:00:11 -050031class GrRRectBlurEffect : public GrFragmentProcessor {
32public:
Brian Salomon71f6cfd2020-06-17 17:14:12 -040033 static std::unique_ptr<GrFragmentProcessor> find_or_create_rrect_blur_mask_fp(
34 GrRecordingContext* context,
35 const SkRRect& rrectToDraw,
36 const SkISize& dimensions,
37 float xformedSigma) {
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050038 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Mike Kleind6ab77a2019-03-21 08:18:24 -050039 GrUniqueKey key;
40 GrUniqueKey::Builder builder(&key, kDomain, 9, "RoundRect Blur Mask");
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050041 builder[0] = SkScalarCeilToInt(xformedSigma - 1 / 6.0f);
42
43 int index = 1;
Mike Kleind6ab77a2019-03-21 08:18:24 -050044 for (auto c : {SkRRect::kUpperLeft_Corner, SkRRect::kUpperRight_Corner,
45 SkRRect::kLowerRight_Corner, SkRRect::kLowerLeft_Corner}) {
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050046 SkASSERT(SkScalarIsInt(rrectToDraw.radii(c).fX) &&
47 SkScalarIsInt(rrectToDraw.radii(c).fY));
48 builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fX);
49 builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fY);
50 }
51 builder.finish();
52
Brian Salomon71f6cfd2020-06-17 17:14:12 -040053 // It seems like we could omit this matrix and modify the shader code to not normalize
54 // the coords used to sample the texture effect. However, the "proxyDims" value in the
55 // shader is not always the actual the proxy dimensions. This is because 'dimensions' here
56 // was computed using integer corner radii as determined in
57 // SkComputeBlurredRRectParams whereas the shader code uses the float radius to compute
58 // 'proxyDims'. Why it draws correctly with these unequal values is a mystery for the ages.
59 auto m = SkMatrix::Scale(dimensions.width(), dimensions.height());
Brian Salomond005b692020-04-01 15:47:05 -040060 static constexpr auto kMaskOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillips9da87e02019-02-04 13:26:26 -050061 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050062
Brian Salomond005b692020-04-01 15:47:05 -040063 if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback(
Brian Salomon0029db02020-04-03 10:41:24 -040064 key, kMaskOrigin, GrColorType::kAlpha_8, 1)) {
Brian Salomon71f6cfd2020-06-17 17:14:12 -040065 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050066 }
67
Brian Salomond005b692020-04-01 15:47:05 -040068 auto rtc = GrRenderTargetContext::MakeWithFallback(
69 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -040070 GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin);
Greg Daniel43956122020-02-11 15:49:27 -050071 if (!rtc) {
Brian Salomon71f6cfd2020-06-17 17:14:12 -040072 return nullptr;
Greg Daniel43956122020-02-11 15:49:27 -050073 }
74
75 GrPaint paint;
76
Michael Ludwig81d41722020-05-26 16:57:38 -040077 rtc->clear(SK_PMColor4fTRANSPARENT);
Michael Ludwig7c12e282020-05-29 09:54:07 -040078 rtc->drawRRect(nullptr, std::move(paint), GrAA::kYes, SkMatrix::I(), rrectToDraw,
Greg Daniel43956122020-02-11 15:49:27 -050079 GrStyle::SimpleFill());
80
81 GrSurfaceProxyView srcView = rtc->readSurfaceView();
82 if (!srcView) {
Brian Salomon71f6cfd2020-06-17 17:14:12 -040083 return nullptr;
Greg Daniel43956122020-02-11 15:49:27 -050084 }
85 SkASSERT(srcView.asTextureProxy());
86 auto rtc2 = SkGpuBlurUtils::GaussianBlur(context,
87 std::move(srcView),
88 rtc->colorInfo().colorType(),
89 rtc->colorInfo().alphaType(),
90 nullptr,
91 SkIRect::MakeSize(dimensions),
92 SkIRect::MakeSize(dimensions),
93 xformedSigma,
94 xformedSigma,
95 SkTileMode::kClamp,
96 SkBackingFit::kExact);
97 if (!rtc2) {
Brian Salomon71f6cfd2020-06-17 17:14:12 -040098 return nullptr;
Greg Daniel43956122020-02-11 15:49:27 -050099 }
100
101 GrSurfaceProxyView mask = rtc2->readSurfaceView();
102 if (!mask) {
Brian Salomon71f6cfd2020-06-17 17:14:12 -0400103 return nullptr;
Greg Daniel43956122020-02-11 15:49:27 -0500104 }
105 SkASSERT(mask.asTextureProxy());
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400106 SkASSERT(mask.origin() == kMaskOrigin);
Greg Daniel43956122020-02-11 15:49:27 -0500107 proxyProvider->assignUniqueKeyToProxy(key, mask.asTextureProxy());
Brian Salomon71f6cfd2020-06-17 17:14:12 -0400108 return GrTextureEffect::Make(std::move(mask), kPremul_SkAlphaType, m);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500109 }
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500110
John Stiles4ca88842020-06-08 17:20:01 -0400111 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP,
112 GrRecordingContext* context,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500113 float sigma,
114 float xformedSigma,
115 const SkRRect& srcRRect,
116 const SkRRect& devRRect);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500117 GrRRectBlurEffect(const GrRRectBlurEffect& src);
118 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500119 const char* name() const override { return "RRectBlurEffect"; }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400120 float sigma;
121 SkRect rect;
122 float cornerRadius;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500123
124private:
John Stiles4ca88842020-06-08 17:20:01 -0400125 GrRRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
126 float sigma,
Brian Osmanff0717f2020-03-27 11:08:38 -0400127 SkRect rect,
128 float cornerRadius,
Brian Salomon71f6cfd2020-06-17 17:14:12 -0400129 std::unique_ptr<GrFragmentProcessor> ninePatchFP)
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500130 : INHERITED(kGrRRectBlurEffect_ClassID,
John Stiles4ca88842020-06-08 17:20:01 -0400131 (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get())
132 : kAll_OptimizationFlags) &
133 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400134 , sigma(sigma)
135 , rect(rect)
Brian Salomon71f6cfd2020-06-17 17:14:12 -0400136 , cornerRadius(cornerRadius) {
Brian Osman12c5d292020-07-13 16:11:35 -0400137 this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough());
Brian Salomon71f6cfd2020-06-17 17:14:12 -0400138 SkASSERT(ninePatchFP);
Brian Osman12c5d292020-07-13 16:11:35 -0400139 this->registerChild(std::move(ninePatchFP), SkSL::SampleUsage::Explicit());
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500140 }
141 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
142 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
143 bool onIsEqual(const GrFragmentProcessor&) const override;
144 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500145 typedef GrFragmentProcessor INHERITED;
146};
147#endif