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