• 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 "effect_test_utils.h"
17 
18 #include "effect/image_filter.h"
19 #include "effect/shader_effect.h"
20 
21 #ifdef ROSEN_OHOS
22 #include "transaction/rs_marshalling_helper.h"
23 #include "utils/object_helper.h"
24 #endif
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 
30 #ifdef ROSEN_OHOS
31 std::function<bool(Parcel&, std::shared_ptr<Data>)> EffectTestUtils::originalMarshallingCallback_;
32 std::function<std::shared_ptr<Data>(Parcel&)> EffectTestUtils::originalUnmarshallingCallback_;
33 #endif
34 
CompareImageFilter(const std::shared_ptr<ImageFilter> & filter1,const std::shared_ptr<ImageFilter> & filter2)35 bool EffectTestUtils::CompareImageFilter(const std::shared_ptr<ImageFilter>& filter1,
36                                          const std::shared_ptr<ImageFilter>& filter2)
37 {
38     // Handle null filters - both null is equal
39     if (filter1 == nullptr && filter2 == nullptr) {
40         return true;
41     }
42     if (filter1 == nullptr || filter2 == nullptr) {
43         return false;
44     }
45 
46     // Serialize both filters
47     auto data1 = filter1->Serialize();
48     auto data2 = filter2->Serialize();
49     // Handle null serialization data - both null is equal
50     if (data1 == nullptr && data2 == nullptr) {
51         return true;
52     }
53     if (data1 == nullptr || data2 == nullptr) {
54         return false;
55     }
56 
57     // Handle empty data - both empty is equal
58     if (data1->GetSize() == 0 && data2->GetSize() == 0) {
59         return true;
60     }
61     if (data1->GetSize() == 0 || data2->GetSize() == 0 ||
62         data1->GetSize() != data2->GetSize()) {
63         return false;
64     }
65 
66     // Compare serialized content byte by byte
67     const uint8_t* bytes1 = static_cast<const uint8_t*>(data1->GetData());
68     const uint8_t* bytes2 = static_cast<const uint8_t*>(data2->GetData());
69     if (bytes1 == nullptr || bytes2 == nullptr) {
70         return false;
71     }
72 
73     int memResult = memcmp(bytes1, bytes2, data1->GetSize());
74     return memResult == 0;
75 }
76 
CompareShaderEffect(const std::shared_ptr<ShaderEffect> & shader1,const std::shared_ptr<ShaderEffect> & shader2)77 bool EffectTestUtils::CompareShaderEffect(const std::shared_ptr<ShaderEffect>& shader1,
78                                           const std::shared_ptr<ShaderEffect>& shader2)
79 {
80     // Handle null shaders - both null is equal
81     if (shader1 == nullptr && shader2 == nullptr) {
82         return true;
83     }
84     if (shader1 == nullptr || shader2 == nullptr) {
85         return false;
86     }
87 
88     // Serialize both shaders
89     auto data1 = shader1->Serialize();
90     auto data2 = shader2->Serialize();
91     // Handle null serialization data - both null is equal
92     if (data1 == nullptr && data2 == nullptr) {
93         return true;
94     }
95     if (data1 == nullptr || data2 == nullptr) {
96         return false;
97     }
98 
99     // Handle empty data - both empty is equal
100     if (data1->GetSize() == 0 && data2->GetSize() == 0) {
101         return true;
102     }
103     if (data1->GetSize() == 0 || data2->GetSize() == 0 ||
104         data1->GetSize() != data2->GetSize()) {
105         return false;
106     }
107 
108     // Compare serialized content byte by byte
109     const uint8_t* bytes1 = static_cast<const uint8_t*>(data1->GetData());
110     const uint8_t* bytes2 = static_cast<const uint8_t*>(data2->GetData());
111     if (bytes1 == nullptr || bytes2 == nullptr) {
112         return false;
113     }
114 
115     int memResult = memcmp(bytes1, bytes2, data1->GetSize());
116     return memResult == 0;
117 }
118 
119 #ifdef ROSEN_OHOS
SetupMarshallingCallbacks()120 void EffectTestUtils::SetupMarshallingCallbacks()
121 {
122     // Save original callbacks
123     originalMarshallingCallback_ = ObjectHelper::Instance().GetDataMarshallingCallback();
124     originalUnmarshallingCallback_ = ObjectHelper::Instance().GetDataUnmarshallingCallback();
125 
126     // Register Data marshalling/unmarshalling callbacks
127     ObjectHelper::Instance().SetDataMarshallingCallback(
128         [](Parcel& parcel, std::shared_ptr<Data> data) -> bool {
129             return RSMarshallingHelper::Marshalling(parcel, data);
130         });
131     ObjectHelper::Instance().SetDataUnmarshallingCallback(
132         [](Parcel& parcel) -> std::shared_ptr<Data> {
133             std::shared_ptr<Data> data;
134             return RSMarshallingHelper::Unmarshalling(parcel, data) ? data : nullptr;
135         });
136 }
137 
RestoreMarshallingCallbacks()138 void EffectTestUtils::RestoreMarshallingCallbacks()
139 {
140     // Restore original callbacks
141     ObjectHelper::Instance().SetDataMarshallingCallback(originalMarshallingCallback_);
142     ObjectHelper::Instance().SetDataUnmarshallingCallback(originalUnmarshallingCallback_);
143 }
144 
ClearMarshallingCallback()145 std::function<bool(Parcel&, std::shared_ptr<Data>)> EffectTestUtils::ClearMarshallingCallback()
146 {
147     auto savedCallback = ObjectHelper::Instance().GetDataMarshallingCallback();
148     ObjectHelper::Instance().SetDataMarshallingCallback(nullptr);
149     return savedCallback;
150 }
151 
RestoreMarshallingCallback(const std::function<bool (Parcel &,std::shared_ptr<Data>)> & callback)152 void EffectTestUtils::RestoreMarshallingCallback(
153     const std::function<bool(Parcel&, std::shared_ptr<Data>)>& callback)
154 {
155     ObjectHelper::Instance().SetDataMarshallingCallback(callback);
156 }
157 #endif
158 
159 } // namespace Drawing
160 } // namespace Rosen
161 } // namespace OHOS