• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "common/rs_common_def.h"
17 #include "common/rs_vector4.h"
18 #include "effect/shader_effect.h"
19 #include "ge_log.h"
20 #include "ge_pixel_map_shader_mask.h"
21 #include "ge_shader_filter_params.h"
22 #include "platform/common/rs_log.h"
23 #include "utils/matrix.h"
24 #include "utils/rect.h"
25 #include "utils/sampling_options.h"
26 #include "utils/scalar.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace Drawing {
31 
GEPixelMapShaderMask(const GEPixelMapMaskParams & param)32 GEPixelMapShaderMask::GEPixelMapShaderMask(const GEPixelMapMaskParams& param) : param_(param) {
33     for (size_t i = 0; i < Vector4f::V4SIZE; ++i) {
34         param_.fillColor.data_[i] = std::clamp(param_.fillColor.data_[i], 0.f, 1.f);
35     }
36 }
37 
IsValid() const38 bool GEPixelMapShaderMask::IsValid() const
39 {
40     if (param_.image == nullptr) {
41         LOGE("GEPixelMapShaderMask::IsValid image is nullptr");
42         return false;
43     }
44 
45     if (!param_.src.IsValid()) {
46         LOGE("GEPixelMapShaderMask::IsValid src is invalid");
47         return false;
48     }
49 
50     if (ROSEN_LE(param_.src.GetWidth() * param_.image->GetWidth(), 0.f)) {
51         LOGE("GEPixelMapShaderMask::IsValid src width is zero");
52         return false;
53     }
54 
55     if (ROSEN_LE(param_.src.GetHeight() * param_.image->GetHeight(), 0.f)) {
56         LOGE("GEPixelMapShaderMask::IsValid src height is zero");
57         return false;
58     }
59 
60     return true;
61 }
62 
GenerateDrawingShader(float width,float height) const63 std::shared_ptr<ShaderEffect> GEPixelMapShaderMask::GenerateDrawingShader(float width, float height) const
64 {
65     if (!IsValid()) {
66         return nullptr;
67     }
68     std::shared_ptr<Drawing::RuntimeShaderBuilder> builder = nullptr;
69     builder = GetPixelMapShaderMaskBuilder();
70     if (builder == nullptr) {
71         LOGE("GEPixelMapShaderMask::GenerateDrawingShaderHas get builder error");
72         return nullptr;
73     }
74     static const Drawing::SamplingOptions option(Drawing::FilterMode::LINEAR, Drawing::MipmapMode::NONE);
75     Drawing::Matrix matrix;
76     auto sx = param_.dst.GetWidth() * width / (param_.src.GetWidth() * param_.image->GetWidth());
77     auto sy = param_.dst.GetHeight() * height / (param_.src.GetHeight() * param_.image->GetHeight());
78     auto tx = param_.dst.left_ * width - param_.src.left_ * param_.image->GetWidth() * sx;
79     auto ty = param_.dst.top_ * height - param_.src.top_ * param_.image->GetHeight() * sy;
80     matrix.SetScaleTranslate(sx, sy, tx, ty);
81     builder->SetChild("image", Drawing::ShaderEffect::CreateImageShader(*param_.image,
82         Drawing::TileMode::CLAMP, Drawing::TileMode::CLAMP, option, matrix));
83     builder->SetUniformVec4("dst",
84         param_.dst.left_ * width, param_.dst.top_ * height, param_.dst.right_ * width, param_.dst.bottom_ * height);
85     builder->SetUniformVec4("fillPixel",
86         param_.fillColor.x_, param_.fillColor.y_, param_.fillColor.z_, param_.fillColor.w_);
87 
88     auto pixelMapMaskEffectShader = builder->MakeShader(nullptr, false);
89     if (pixelMapMaskEffectShader == nullptr) {
90         LOGE("GEPixelMapShaderMask::GenerateDrawingShader make shader failed");
91         return nullptr;
92     }
93     return pixelMapMaskEffectShader;
94 }
95 
GetPixelMapShaderMaskBuilder() const96 std::shared_ptr<Drawing::RuntimeShaderBuilder> GEPixelMapShaderMask::GetPixelMapShaderMaskBuilder() const
97 {
98     thread_local std::shared_ptr<Drawing::RuntimeShaderBuilder> pixelMapShaderMaskBuilder = nullptr;
99     if (pixelMapShaderMaskBuilder != nullptr) {
100         return pixelMapShaderMaskBuilder;
101     }
102 
103     static constexpr char prog[] = R"(
104         uniform shader image;
105         uniform vec4 dst;
106         uniform vec4 fillPixel;
107 
108         vec4 main(float2 fragCoord)
109         {
110             if (dst.x < fragCoord.x && fragCoord.x < dst.z &&
111                 dst.y < fragCoord.y && fragCoord.y < dst.w) {
112                 return image.eval(fragCoord);
113             }
114 
115             return fillPixel;
116         }
117     )";
118 
119     auto pixelMapShaderMaskEffect = Drawing::RuntimeEffect::CreateForShader(prog);
120     if (pixelMapShaderMaskEffect == nullptr) {
121         LOGE("GEPixelMapShaderMask::GetPixelMapShaderMaskBuilder effect error");
122         return nullptr;
123     }
124 
125     pixelMapShaderMaskBuilder = std::make_shared<Drawing::RuntimeShaderBuilder>(pixelMapShaderMaskEffect);
126     return pixelMapShaderMaskBuilder;
127 }
128 
GenerateDrawingShaderHasNormal(float width,float height) const129 std::shared_ptr<ShaderEffect> GEPixelMapShaderMask::GenerateDrawingShaderHasNormal(float width, float height) const
130 {
131     return GenerateDrawingShader(width, height);
132 }
133 
134 
GetPixelMapShaderNormalMaskBuilder() const135 std::shared_ptr<Drawing::RuntimeShaderBuilder> GEPixelMapShaderMask::GetPixelMapShaderNormalMaskBuilder() const
136 {
137     return GetPixelMapShaderMaskBuilder();
138 }
139 
140 } // namespace Drawing
141 } // namespace Rosen
142 } // namespace OHOS