1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <SkCanvas.h>
20 #include <SkImage.h>
21 #include <SkRuntimeEffect.h>
22 #include <SkSurface.h>
23 
24 using namespace std;
25 
26 namespace android {
27 namespace renderengine {
28 namespace skia {
29 
30 /**
31  * This is an implementation of a Kawase blur, as described in here:
32  * https://community.arm.com/cfs-file/__key/communityserver-blogs-components-weblogfiles/
33  * 00-00-00-20-66/siggraph2015_2D00_mmg_2D00_marius_2D00_notes.pdf
34  */
35 class BlurFilter {
36 public:
37     // Downsample FBO to improve performance
38     static constexpr float kInputScale = 0.25f;
39     // Downsample scale factor used to improve performance
40     static constexpr float kInverseInputScale = 1.0f / kInputScale;
41     // Maximum number of render passes
42     static constexpr uint32_t kMaxPasses = 4;
43     // To avoid downscaling artifacts, we interpolate the blurred fbo with the full composited
44     // image, up to this radius.
45     static constexpr float kMaxCrossFadeRadius = 10.0f;
46 
47     explicit BlurFilter();
~BlurFilter()48     virtual ~BlurFilter(){};
49 
50     // Execute blur, saving it to a texture
51     sk_sp<SkImage> generate(GrRecordingContext* context, const uint32_t radius,
52                             const sk_sp<SkImage> blurInput, const SkRect& blurRect) const;
53 
54     /**
55      * Draw the blurred content (from the generate method) into the canvas.
56      * @param canvas is the destination/output for the blur
57      * @param effectRegion the RoundRect in canvas coordinates that determines the blur coverage
58      * @param blurRadius radius of the blur used to determine the intensity of the crossfade effect
59      * @param blurAlpha alpha value applied to the effectRegion when the blur is drawn
60      * @param blurRect bounds of the blurredImage translated into canvas coordinates
61      * @param blurredImage down-sampled blurred content that was produced by the generate() method
62      * @param input original unblurred input that is used to crossfade with the blurredImage
63      */
64     void drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion, const uint32_t blurRadius,
65                         const float blurAlpha, const SkRect& blurRect, sk_sp<SkImage> blurredImage,
66                         sk_sp<SkImage> input);
67 
68 private:
69     sk_sp<SkRuntimeEffect> mBlurEffect;
70     sk_sp<SkRuntimeEffect> mMixEffect;
71 };
72 
73 } // namespace skia
74 } // namespace renderengine
75 } // namespace android
76