1 /*
2  * Copyright (c) 2021-2023 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 "skia_path_effect.h"
17 #include "skia_helper.h"
18 
19 #include <memory>
20 
21 #include "include/core/SkPathEffect.h"
22 #include "include/effects/Sk1DPathEffect.h"
23 #include "include/effects/SkCornerPathEffect.h"
24 #include "include/effects/SkDashPathEffect.h"
25 #include "include/effects/SkDiscretePathEffect.h"
26 #include "skia_path.h"
27 
28 #include "effect/path_effect.h"
29 #include "utils/data.h"
30 #include "utils/log.h"
31 
32 namespace OHOS {
33 namespace Rosen {
34 namespace Drawing {
SkiaPathEffect()35 SkiaPathEffect::SkiaPathEffect() noexcept : pathEffect_(nullptr) {}
36 
InitWithDash(const scalar intervals[],int count,scalar phase)37 void SkiaPathEffect::InitWithDash(const scalar intervals[], int count, scalar phase)
38 {
39     pathEffect_ = SkDashPathEffect::Make(intervals, count, phase);
40 }
41 
InitWithPathDash(const Path & path,scalar advance,scalar phase,PathDashStyle style)42 void SkiaPathEffect::InitWithPathDash(const Path& path, scalar advance, scalar phase, PathDashStyle style)
43 {
44     auto p = path.GetImpl<SkiaPath>();
45     if (p != nullptr) {
46         pathEffect_ =
47             SkPath1DPathEffect::Make(p->GetPath(), advance, phase, static_cast<SkPath1DPathEffect::Style>(style));
48     }
49 }
50 
InitWithCorner(scalar radius)51 void SkiaPathEffect::InitWithCorner(scalar radius)
52 {
53     pathEffect_ = SkCornerPathEffect::Make(radius);
54 }
55 
InitWithDiscrete(scalar segLength,scalar dev,uint32_t seedAssist)56 void SkiaPathEffect::InitWithDiscrete(scalar segLength, scalar dev, uint32_t seedAssist)
57 {
58     pathEffect_ = SkDiscretePathEffect::Make(segLength, dev, seedAssist);
59 }
60 
InitWithSum(const PathEffect & e1,const PathEffect & e2)61 void SkiaPathEffect::InitWithSum(const PathEffect& e1, const PathEffect& e2)
62 {
63     auto first = e1.GetImpl<SkiaPathEffect>();
64     auto second = e2.GetImpl<SkiaPathEffect>();
65     if (first != nullptr && second != nullptr) {
66         pathEffect_ = SkPathEffect::MakeSum(first->GetPathEffect(), second->GetPathEffect());
67     }
68 }
69 
InitWithCompose(const PathEffect & e1,const PathEffect & e2)70 void SkiaPathEffect::InitWithCompose(const PathEffect& e1, const PathEffect& e2)
71 {
72     auto outer = e1.GetImpl<SkiaPathEffect>();
73     auto inner = e2.GetImpl<SkiaPathEffect>();
74     if (outer != nullptr && inner != nullptr) {
75         pathEffect_ = SkPathEffect::MakeCompose(outer->GetPathEffect(), inner->GetPathEffect());
76     }
77 }
78 
GetPathEffect() const79 sk_sp<SkPathEffect> SkiaPathEffect::GetPathEffect() const
80 {
81     return pathEffect_;
82 }
83 
SetSkPathEffect(const sk_sp<SkPathEffect> & pathEffect)84 void SkiaPathEffect::SetSkPathEffect(const sk_sp<SkPathEffect>& pathEffect)
85 {
86     pathEffect_ = pathEffect;
87 }
88 
Serialize() const89 std::shared_ptr<Data> SkiaPathEffect::Serialize() const
90 {
91     if (pathEffect_ == nullptr) {
92         LOGD("SkiaPathEffect::Serialize, pathEffect_ is nullptr!");
93         return nullptr;
94     }
95 
96     return SkiaHelper::FlattenableSerialize(pathEffect_.get());
97 }
98 
Deserialize(std::shared_ptr<Data> data)99 bool SkiaPathEffect::Deserialize(std::shared_ptr<Data> data)
100 {
101     if (data == nullptr) {
102         LOGD("SkiaPathEffect::Deserialize, data is invalid!");
103         return false;
104     }
105 
106     pathEffect_ = SkiaHelper::FlattenableDeserialize<SkPathEffect>(data);
107     return true;
108 }
109 
110 } // namespace Drawing
111 } // namespace Rosen
112 } // namespace OHOS
113