• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "EdgeExtensionShaderFactory.h"
18 #include <SkPoint.h>
19 #include <SkRuntimeEffect.h>
20 #include <SkStream.h>
21 #include <SkString.h>
22 #include <com_android_graphics_libgui_flags.h>
23 #include "log/log_main.h"
24 
25 namespace android::renderengine::skia {
26 
27 static const SkString edgeShader = SkString(R"(
28     uniform shader uContentTexture;
29     uniform vec2 uImgSize;
30 
31     // TODO(b/214232209) oobTolerance is temporary and will be removed when the scrollbar will be
32     // hidden during the animation
33     const float oobTolerance = 15;
34     const int blurRadius = 3;
35     const float blurArea = float((2 * blurRadius + 1) * (2 * blurRadius + 1));
36 
37     vec4 boxBlur(vec2 p) {
38         vec4 sumColors = vec4(0);
39 
40         for (int i = -blurRadius; i <= blurRadius; i++) {
41             for (int j = -blurRadius; j <= blurRadius; j++) {
42                 sumColors += uContentTexture.eval(p + vec2(i, j));
43             }
44         }
45         return sumColors / blurArea;
46     }
47 
48     vec4 main(vec2 coord) {
49         vec2 nearestTexturePoint = clamp(coord, vec2(0, 0), uImgSize);
50         if (coord == nearestTexturePoint) {
51             return uContentTexture.eval(coord);
52         } else {
53             vec2 samplePoint = nearestTexturePoint + oobTolerance * normalize(
54                                     nearestTexturePoint - coord);
55             return boxBlur(samplePoint);
56         }
57     }
58 )");
59 
EdgeExtensionShaderFactory()60 EdgeExtensionShaderFactory::EdgeExtensionShaderFactory() {
61     mResult = std::make_unique<SkRuntimeEffect::Result>(SkRuntimeEffect::MakeForShader(edgeShader));
62     LOG_ALWAYS_FATAL_IF(!mResult->errorText.isEmpty(),
63                         "EdgeExtensionShaderFactory compilation "
64                         "failed with an unexpected error: %s",
65                         mResult->errorText.c_str());
66 }
67 
createSkShader(const sk_sp<SkShader> & inputShader,const LayerSettings & layer,const SkRect & imageBounds) const68 sk_sp<SkShader> EdgeExtensionShaderFactory::createSkShader(const sk_sp<SkShader>& inputShader,
69                                                            const LayerSettings& layer,
70                                                            const SkRect& imageBounds) const {
71     LOG_ALWAYS_FATAL_IF(mResult == nullptr,
72                         "EdgeExtensionShaderFactory did not initialize mResult. "
73                         "This means that we unexpectedly applied the edge extension shader");
74 
75     SkRuntimeShaderBuilder builder = SkRuntimeShaderBuilder(mResult->effect);
76 
77     builder.child("uContentTexture") = inputShader;
78     if (imageBounds.isEmpty()) {
79         builder.uniform("uImgSize") = SkPoint{layer.geometry.boundaries.getWidth(),
80                                               layer.geometry.boundaries.getHeight()};
81     } else {
82         builder.uniform("uImgSize") = SkPoint{imageBounds.width(), imageBounds.height()};
83     }
84     return builder.makeShader();
85 }
86 } // namespace android::renderengine::skia