1 /*
2  * Copyright (c) 2024 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 <securec.h>
17 #include "effect/blur_draw_looper.h"
18 
19 #include "utils/log.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
BlurDrawLooper(float blurRadius,scalar dx,scalar dy,const Color & color)24 BlurDrawLooper::BlurDrawLooper(float blurRadius, scalar dx, scalar dy, const Color& color) noexcept
25 {
26     blurDrawLooperPriv_.blurRadius = blurRadius;
27     blurDrawLooperPriv_.dx = dx;
28     blurDrawLooperPriv_.dy = dy;
29     blurDrawLooperPriv_.color = color;
30     blurMaskFilter_ = MaskFilter::CreateBlurMaskFilter(Drawing::BlurType::NORMAL,
31         ConvertRadiusToSigma(blurRadius), true);
32 }
33 
CreateBlurDrawLooper(float blurRadius,scalar dx,scalar dy,const Color & color)34 std::shared_ptr<BlurDrawLooper> BlurDrawLooper::CreateBlurDrawLooper(float blurRadius,
35     scalar dx, scalar dy, const Color& color)
36 {
37     if (blurRadius <= 0.f) {
38         return nullptr;
39     }
40     return std::make_shared<BlurDrawLooper>(blurRadius, dx, dy, color);
41 }
42 
operator ==(const BlurDrawLooper & lhs,const BlurDrawLooper & rhs)43 bool operator==(const BlurDrawLooper& lhs, const BlurDrawLooper& rhs)
44 {
45     return lhs.blurDrawLooperPriv_.color == rhs.blurDrawLooperPriv_.color &&
46         IsScalarAlmostEqual(lhs.blurDrawLooperPriv_.dx, rhs.blurDrawLooperPriv_.dx) &&
47         IsScalarAlmostEqual(lhs.blurDrawLooperPriv_.dy, rhs.blurDrawLooperPriv_.dy) &&
48         IsScalarAlmostEqual(lhs.blurDrawLooperPriv_.blurRadius, rhs.blurDrawLooperPriv_.blurRadius);
49 }
50 
operator !=(const BlurDrawLooper & lhs,const BlurDrawLooper & rhs)51 bool operator!=(const BlurDrawLooper& lhs, const BlurDrawLooper& rhs)
52 {
53     return !(lhs == rhs);
54 }
55 
Serialize() const56 std::shared_ptr<Data> BlurDrawLooper::Serialize() const
57 {
58     constexpr size_t len = sizeof(BlurDrawLooperPriv);
59     auto blurData = std::make_shared<Data>();
60     if (!blurData->BuildUninitialized(len)) {
61         return nullptr;
62     }
63     uint8_t* memory = static_cast<uint8_t*>(blurData->WritableData());
64     if (memcpy_s(memory, len, &blurDrawLooperPriv_, sizeof(BlurDrawLooperPriv)) != EOK) {
65         return nullptr;
66     }
67 
68     return blurData;
69 }
70 
Deserialize(std::shared_ptr<Data> data)71 std::shared_ptr<BlurDrawLooper> BlurDrawLooper::Deserialize(std::shared_ptr<Data> data)
72 {
73     constexpr size_t len = sizeof(BlurDrawLooperPriv);
74     if (data == nullptr || data->GetSize() != len) {
75         LOGD("BlurDrawLooper:blur data is valid!");
76         return nullptr;
77     }
78     uint8_t* memory = reinterpret_cast<uint8_t*>(data->WritableData());
79     if (memory == nullptr) {
80         return nullptr;
81     }
82     BlurDrawLooperPriv blurDrawLooperPriv;
83     if (memcpy_s(&blurDrawLooperPriv, sizeof(BlurDrawLooperPriv), memory, len) != EOK) {
84         return nullptr;
85     }
86 
87     return CreateBlurDrawLooper(blurDrawLooperPriv.blurRadius,
88         blurDrawLooperPriv.dx, blurDrawLooperPriv.dy, blurDrawLooperPriv.color);
89 }
90 } // namespace Drawing
91 } // namespace Rosen
92 } // namespace OHOS