• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class BlurFilter {
31 public:
32     // Downsample FBO to improve performance
33     static constexpr float kInputScale = 0.25f;
34     // Downsample scale factor used to improve performance
35     static constexpr float kInverseInputScale = 1.0f / kInputScale;
36 
37     explicit BlurFilter(float maxCrossFadeRadius = 10.0f);
~BlurFilter()38     virtual ~BlurFilter(){}
39 
40     // Execute blur, saving it to a texture
41     virtual sk_sp<SkImage> generate(GrRecordingContext* context, const uint32_t radius,
42                             const sk_sp<SkImage> blurInput, const SkRect& blurRect) const = 0;
43 
44     /**
45      * Draw the blurred content (from the generate method) into the canvas.
46      * @param canvas is the destination/output for the blur
47      * @param effectRegion the RoundRect in canvas coordinates that determines the blur coverage
48      * @param blurRadius radius of the blur used to determine the intensity of the crossfade effect
49      * @param blurAlpha alpha value applied to the effectRegion when the blur is drawn
50      * @param blurRect bounds of the blurredImage translated into canvas coordinates
51      * @param blurredImage down-sampled blurred content that was produced by the generate() method
52      * @param input original unblurred input that is used to crossfade with the blurredImage
53      */
54     void drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion,
55                                 const uint32_t blurRadius, const float blurAlpha,
56                                 const SkRect& blurRect, sk_sp<SkImage> blurredImage,
57                                 sk_sp<SkImage> input);
58 
59     float getMaxCrossFadeRadius() const;
60 
61 private:
62     // To avoid downscaling artifacts, we interpolate the blurred fbo with the full composited
63     // image, up to this radius.
64     const float mMaxCrossFadeRadius;
65 
66     // Optional blend used for crossfade only if mMaxCrossFadeRadius > 0
67     const sk_sp<SkRuntimeEffect> mMixEffect;
68 };
69 
70 } // namespace skia
71 } // namespace renderengine
72 } // namespace android
73