1 /*
2  * Copyright 2021 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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #include "BlurFilter.h"
19 #include <SkCanvas.h>
20 #include <SkPaint.h>
21 #include <SkRRect.h>
22 #include <SkRuntimeEffect.h>
23 #include <SkSize.h>
24 #include <SkString.h>
25 #include <SkSurface.h>
26 #include <log/log.h>
27 #include <utils/Trace.h>
28 
29 namespace android {
30 namespace renderengine {
31 namespace skia {
32 
createMixEffect()33 static sk_sp<SkRuntimeEffect> createMixEffect() {
34     SkString mixString(R"(
35         uniform shader blurredInput;
36         uniform shader originalInput;
37         uniform float mixFactor;
38 
39         half4 main(float2 xy) {
40             return half4(mix(originalInput.eval(xy), blurredInput.eval(xy), mixFactor)).rgb1;
41         }
42     )");
43 
44     auto [mixEffect, mixError] = SkRuntimeEffect::MakeForShader(mixString);
45     if (!mixEffect) {
46         LOG_ALWAYS_FATAL("RuntimeShader error: %s", mixError.c_str());
47     }
48     return mixEffect;
49 }
50 
getShaderTransform(const SkCanvas * canvas,const SkRect & blurRect,const float scale)51 static SkMatrix getShaderTransform(const SkCanvas* canvas, const SkRect& blurRect,
52                                    const float scale) {
53     // 1. Apply the blur shader matrix, which scales up the blurred surface to its real size
54     auto matrix = SkMatrix::Scale(scale, scale);
55     // 2. Since the blurred surface has the size of the layer, we align it with the
56     // top left corner of the layer position.
57     matrix.postConcat(SkMatrix::Translate(blurRect.fLeft, blurRect.fTop));
58     // 3. Finally, apply the inverse canvas matrix. The snapshot made in the BlurFilter is in the
59     // original surface orientation. The inverse matrix has to be applied to align the blur
60     // surface with the current orientation/position of the canvas.
61     SkMatrix drawInverse;
62     if (canvas != nullptr && canvas->getTotalMatrix().invert(&drawInverse)) {
63         matrix.postConcat(drawInverse);
64     }
65     return matrix;
66 }
67 
BlurFilter(const float maxCrossFadeRadius)68 BlurFilter::BlurFilter(const float maxCrossFadeRadius)
69       : mMaxCrossFadeRadius(maxCrossFadeRadius),
70         mMixEffect(maxCrossFadeRadius > 0 ? createMixEffect() : nullptr) {}
71 
getMaxCrossFadeRadius() const72 float BlurFilter::getMaxCrossFadeRadius() const {
73     return mMaxCrossFadeRadius;
74 }
75 
drawBlurRegion(SkCanvas * canvas,const SkRRect & effectRegion,const uint32_t blurRadius,const float blurAlpha,const SkRect & blurRect,sk_sp<SkImage> blurredImage,sk_sp<SkImage> input)76 void BlurFilter::drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion,
77                                 const uint32_t blurRadius, const float blurAlpha,
78                                 const SkRect& blurRect, sk_sp<SkImage> blurredImage,
79                                 sk_sp<SkImage> input) {
80     ATRACE_CALL();
81 
82     SkPaint paint;
83     paint.setAlphaf(blurAlpha);
84 
85     const auto blurMatrix = getShaderTransform(canvas, blurRect, kInverseInputScale);
86     SkSamplingOptions linearSampling(SkFilterMode::kLinear, SkMipmapMode::kNone);
87     const auto blurShader = blurredImage->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
88                                                      linearSampling, &blurMatrix);
89 
90     if (blurRadius < mMaxCrossFadeRadius) {
91         // For sampling Skia's API expects the inverse of what logically seems appropriate. In this
92         // case you might expect the matrix to simply be the canvas matrix.
93         SkMatrix inputMatrix;
94         if (!canvas->getTotalMatrix().invert(&inputMatrix)) {
95             ALOGE("matrix was unable to be inverted");
96         }
97 
98         SkRuntimeShaderBuilder blurBuilder(mMixEffect);
99         blurBuilder.child("blurredInput") = blurShader;
100         blurBuilder.child("originalInput") =
101                 input->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linearSampling,
102                                   inputMatrix);
103         blurBuilder.uniform("mixFactor") = blurRadius / mMaxCrossFadeRadius;
104 
105         paint.setShader(blurBuilder.makeShader());
106     } else {
107         paint.setShader(blurShader);
108     }
109 
110     if (effectRegion.isRect()) {
111         if (blurAlpha == 1.0f) {
112             paint.setBlendMode(SkBlendMode::kSrc);
113         }
114         canvas->drawRect(effectRegion.rect(), paint);
115     } else {
116         paint.setAntiAlias(true);
117         canvas->drawRRect(effectRegion, paint);
118     }
119 }
120 
121 } // namespace skia
122 } // namespace renderengine
123 } // namespace android
124