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_dot_matrix_shader.h"
19 #include "render/rs_flow_light_sweep_shader.h"
20 #include "ge_visual_effect_impl.h"
21
22 namespace OHOS {
23 namespace Rosen {
CreateRSShader()24 std::shared_ptr<RSShader> RSShader::CreateRSShader()
25 {
26 return std::make_shared<RSShader>();
27 }
28
CreateRSShader(const std::shared_ptr<Drawing::ShaderEffect> & drShader)29 std::shared_ptr<RSShader> RSShader::CreateRSShader(const std::shared_ptr<Drawing::ShaderEffect>& drShader)
30 {
31 auto rsShader = std::make_shared<RSShader>();
32 rsShader->SetDrawingShader(drShader);
33 return rsShader;
34 }
35
CreateRSShader(const ShaderType & type)36 std::shared_ptr<RSShader> RSShader::CreateRSShader(const ShaderType& type)
37 {
38 std::shared_ptr<RSShader> shader = nullptr;
39 switch (type) {
40 case ShaderType::DOT_MATRIX: {
41 shader = std::make_shared<RSDotMatrixShader>();
42 break;
43 }
44 case ShaderType::FLOW_LIGHT_SWEEP: {
45 shader = std::make_shared<RSFlowLightSweepShader>();
46 break;
47 }
48 case ShaderType::DRAWING: {
49 shader = std::make_shared<RSShader>();
50 break;
51 }
52 default:
53 break;
54 }
55 if (shader == nullptr) {
56 ROSEN_LOGE("RSShader::CreateRSShader shader is nullptr");
57 return nullptr;
58 }
59
60 return shader;
61 }
62
SetDrawingShader(const std::shared_ptr<Drawing::ShaderEffect> & drShader)63 void RSShader::SetDrawingShader(const std::shared_ptr<Drawing::ShaderEffect>& drShader)
64 {
65 drShader_ = drShader;
66 }
67
GetDrawingShader() const68 const std::shared_ptr<Drawing::ShaderEffect>& RSShader::GetDrawingShader() const
69 {
70 return drShader_;
71 }
72
Marshalling(Parcel & parcel)73 bool RSShader::Marshalling(Parcel& parcel)
74 {
75 auto& shaderEffect = GetDrawingShader();
76 if (!shaderEffect) {
77 ROSEN_LOGD("RSShader::Marshalling Drawing Shader is nullptr");
78 return parcel.WriteInt32(-1);
79 }
80
81 int32_t type = static_cast<int32_t>(shaderEffect->GetType());
82 std::shared_ptr<Drawing::Data> data = shaderEffect->Serialize();
83 if (!data) {
84 ROSEN_LOGE("RSShader::Marshalling, data is nullptr");
85 return parcel.WriteInt32(-1);
86 }
87 bool flag = parcel.WriteInt32(type) && RSMarshallingHelper::Marshalling(parcel, data);
88 if (!flag) {
89 ROSEN_LOGE("unirender: RSShader::Marshalling WriteInt32 or RSMarshallingHelper::Marshalling failed");
90 }
91 return flag;
92 }
93
Unmarshalling(Parcel & parcel,bool & needReset)94 bool RSShader::Unmarshalling(Parcel& parcel, bool& needReset)
95 {
96 needReset = false;
97 int32_t type{0};
98 if (!parcel.ReadInt32(type)) {
99 ROSEN_LOGE("RSShader::Unmarshalling Read type failed");
100 return false;
101 }
102 if (type == -1) {
103 needReset = true;
104 return true;
105 }
106 std::shared_ptr<Drawing::Data> data;
107 if (!RSMarshallingHelper::Unmarshalling(parcel, data) || !data) {
108 ROSEN_LOGE("RSShader::Unmarshalling, data is nullptr");
109 return false;
110 }
111 Drawing::ShaderEffect::ShaderEffectType shaderEffectType = Drawing::ShaderEffect::ShaderEffectType::NO_TYPE;
112 if (type >= static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::COLOR_SHADER) &&
113 type <= static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::EXTEND_SHADER)) {
114 shaderEffectType = static_cast<Drawing::ShaderEffect::ShaderEffectType>(type);
115 }
116 auto shaderEffect = std::make_shared<Drawing::ShaderEffect>(shaderEffectType);
117 if (!shaderEffect->Deserialize(data)) {
118 ROSEN_LOGE("unirender: RSMarshallingHelper::Unmarshalling RSShader, Deserialize failed");
119 return false;
120 }
121 SetDrawingShader(shaderEffect);
122 return true;
123 }
124 } // namespace Rosen
125 } // namespace OHOS