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