1 /*
2 * Copyright (c) 2021 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 "render/rs_shader.h"
17 #include "platform/common/rs_log.h"
18 #include "render/rs_border_light_shader.h"
19 #include "render/rs_complex_shader.h"
20 #include "render/rs_dot_matrix_shader.h"
21 #include "render/rs_flow_light_sweep_shader.h"
22 #include "ge_visual_effect_impl.h"
23 #include "effect/shader_effect_lazy.h"
24
25 namespace OHOS {
26 namespace Rosen {
CreateRSShader()27 std::shared_ptr<RSShader> RSShader::CreateRSShader()
28 {
29 return std::make_shared<RSShader>();
30 }
31
CreateRSShader(const std::shared_ptr<Drawing::ShaderEffect> & drShader)32 std::shared_ptr<RSShader> RSShader::CreateRSShader(const std::shared_ptr<Drawing::ShaderEffect>& drShader)
33 {
34 auto rsShader = std::make_shared<RSShader>();
35 rsShader->SetDrawingShader(drShader);
36 return rsShader;
37 }
38
CreateRSShader(const ShaderType & type)39 std::shared_ptr<RSShader> RSShader::CreateRSShader(const ShaderType& type)
40 {
41 std::shared_ptr<RSShader> shader = nullptr;
42 switch (type) {
43 case ShaderType::DOT_MATRIX: {
44 shader = std::make_shared<RSDotMatrixShader>();
45 break;
46 }
47 case ShaderType::FLOW_LIGHT_SWEEP: {
48 shader = std::make_shared<RSFlowLightSweepShader>();
49 break;
50 }
51 case ShaderType::DRAWING: {
52 shader = std::make_shared<RSShader>();
53 break;
54 }
55 case ShaderType::COMPLEX: {
56 shader = std::make_shared<RSComplexShader>();
57 break;
58 }
59 case ShaderType::BORDER_LIGHT: {
60 shader = std::make_shared<RSBorderLightShader>();
61 break;
62 }
63 default:
64 break;
65 }
66 if (shader == nullptr) {
67 ROSEN_LOGE("RSShader::CreateRSShader shader is nullptr");
68 return nullptr;
69 }
70
71 return shader;
72 }
73
SetDrawingShader(const std::shared_ptr<Drawing::ShaderEffect> & drShader)74 void RSShader::SetDrawingShader(const std::shared_ptr<Drawing::ShaderEffect>& drShader)
75 {
76 // Materialize Lazy ShaderEffect objects immediately for RSShader compatibility
77 if (drShader && drShader->IsLazy()) {
78 auto lazyShader = std::static_pointer_cast<Drawing::ShaderEffectLazy>(drShader);
79 auto materializedShader = lazyShader->Materialize();
80 drShader_ = materializedShader;
81 ROSEN_LOGD("RSShader::SetDrawingShader materialized Lazy ShaderEffect");
82 return;
83 }
84 drShader_ = drShader;
85 }
86
GetDrawingShader() const87 const std::shared_ptr<Drawing::ShaderEffect>& RSShader::GetDrawingShader() const
88 {
89 return drShader_;
90 }
91
Marshalling(Parcel & parcel)92 bool RSShader::Marshalling(Parcel& parcel)
93 {
94 auto& shaderEffect = GetDrawingShader();
95 if (!shaderEffect) {
96 ROSEN_LOGD("RSShader::Marshalling Drawing Shader is nullptr");
97 return parcel.WriteInt32(-1);
98 }
99
100 int32_t type = static_cast<int32_t>(shaderEffect->GetType());
101 std::shared_ptr<Drawing::Data> data = shaderEffect->Serialize();
102 if (!data) {
103 ROSEN_LOGE("RSShader::Marshalling, data is nullptr");
104 return parcel.WriteInt32(-1);
105 }
106 bool flag = parcel.WriteInt32(type) && RSMarshallingHelper::Marshalling(parcel, data);
107 if (!flag) {
108 ROSEN_LOGE("unirender: RSShader::Marshalling WriteInt32 or RSMarshallingHelper::Marshalling failed");
109 }
110 return flag;
111 }
112
Unmarshalling(Parcel & parcel,bool & needReset)113 bool RSShader::Unmarshalling(Parcel& parcel, bool& needReset)
114 {
115 needReset = false;
116 int32_t type{0};
117 if (!parcel.ReadInt32(type)) {
118 ROSEN_LOGE("RSShader::Unmarshalling Read type failed");
119 return false;
120 }
121 if (type == -1) {
122 needReset = true;
123 return true;
124 }
125 std::shared_ptr<Drawing::Data> data;
126 if (!RSMarshallingHelper::Unmarshalling(parcel, data) || !data) {
127 ROSEN_LOGE("RSShader::Unmarshalling, data is nullptr");
128 return false;
129 }
130 Drawing::ShaderEffect::ShaderEffectType shaderEffectType = Drawing::ShaderEffect::ShaderEffectType::NO_TYPE;
131 if (type >= static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::COLOR_SHADER) &&
132 type <= static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::EXTEND_SHADER)) {
133 shaderEffectType = static_cast<Drawing::ShaderEffect::ShaderEffectType>(type);
134 }
135 auto shaderEffect = std::make_shared<Drawing::ShaderEffect>(shaderEffectType);
136 if (!shaderEffect->Deserialize(data)) {
137 ROSEN_LOGE("unirender: RSMarshallingHelper::Unmarshalling RSShader, Deserialize failed");
138 return false;
139 }
140 SetDrawingShader(shaderEffect);
141 return true;
142 }
143 } // namespace Rosen
144 } // namespace OHOS