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 if (!com::android::graphics::libgui::flags::edge_extension_shader()) {
62 return;
63 }
64 mResult = std::make_unique<SkRuntimeEffect::Result>(SkRuntimeEffect::MakeForShader(edgeShader));
65 LOG_ALWAYS_FATAL_IF(!mResult->errorText.isEmpty(),
66 "EdgeExtensionShaderFactory compilation "
67 "failed with an unexpected error: %s",
68 mResult->errorText.c_str());
69 }
70
createSkShader(const sk_sp<SkShader> & inputShader,const LayerSettings & layer,const SkRect & imageBounds) const71 sk_sp<SkShader> EdgeExtensionShaderFactory::createSkShader(const sk_sp<SkShader>& inputShader,
72 const LayerSettings& layer,
73 const SkRect& imageBounds) const {
74 LOG_ALWAYS_FATAL_IF(mResult == nullptr,
75 "EdgeExtensionShaderFactory did not initialize mResult. "
76 "This means that we unexpectedly applied the edge extension shader");
77
78 SkRuntimeShaderBuilder builder = SkRuntimeShaderBuilder(mResult->effect);
79
80 builder.child("uContentTexture") = inputShader;
81 if (imageBounds.isEmpty()) {
82 builder.uniform("uImgSize") = SkPoint{layer.geometry.boundaries.getWidth(),
83 layer.geometry.boundaries.getHeight()};
84 } else {
85 builder.uniform("uImgSize") = SkPoint{imageBounds.width(), imageBounds.height()};
86 }
87 return builder.makeShader();
88 }
89 } // namespace android::renderengine::skia