• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "recording/path_effect_cmd_list.h"
17 
18 #include "recording/cmd_list_helper.h"
19 #include "recording/path_cmd_list.h"
20 #include "utils/log.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
CreateFromData(const CmdListData & data,bool isCopy)25 std::shared_ptr<PathEffectCmdList> PathEffectCmdList::CreateFromData(const CmdListData& data, bool isCopy)
26 {
27     auto cmdList = std::make_shared<PathEffectCmdList>();
28     if (isCopy) {
29         cmdList->opAllocator_.BuildFromDataWithCopy(data.first, data.second);
30     } else {
31         cmdList->opAllocator_.BuildFromData(data.first, data.second);
32     }
33     return cmdList;
34 }
35 
Playback() const36 std::shared_ptr<PathEffect> PathEffectCmdList::Playback() const
37 {
38     if (opAllocator_.GetSize() == 0) {
39         return nullptr;
40     }
41 
42     uint32_t offset = 0;
43     std::shared_ptr<PathEffect> pe = nullptr;
44     do {
45         OpItem* itemPtr = static_cast<OpItem*>(opAllocator_.OffsetToAddr(offset));
46         if (itemPtr == nullptr) {
47             LOGE("PathEffectCmdList Playback failed!");
48             break;
49         }
50 
51         switch (itemPtr->GetType()) {
52             case PathEffectOpItem::OPITEM_HEAD:
53                 break;
54             case PathEffectOpItem::CREATE_DASH:
55                 pe = static_cast<CreateDashPathEffectOpItem*>(itemPtr)->Playback(*this);
56                 break;
57             case PathEffectOpItem::CREATE_PATH_DASH:
58                 pe = static_cast<CreatePathDashEffectOpItem*>(itemPtr)->Playback(*this);
59                 break;
60             case PathEffectOpItem::CREATE_CORNER:
61                 pe = static_cast<CreateCornerPathEffectOpItem*>(itemPtr)->Playback();
62                 break;
63             case PathEffectOpItem::CREATE_SUM:
64                 pe = static_cast<CreateSumPathEffectOpItem*>(itemPtr)->Playback(*this);
65                 break;
66             case PathEffectOpItem::CREATE_COMPOSE:
67                 pe = static_cast<CreateComposePathEffectOpItem*>(itemPtr)->Playback(*this);
68                 break;
69             default:
70                 LOGE("PathEffectCmdList unknown OpItem type!");
71                 break;
72         }
73         offset = itemPtr->GetNextOpItemOffset();
74     } while (offset != 0);
75 
76     return pe;
77 }
78 
79 /* OpItem */
CreateDashPathEffectOpItem(const std::pair<uint32_t,size_t> & intervals,scalar phase)80 CreateDashPathEffectOpItem::CreateDashPathEffectOpItem(const std::pair<uint32_t, size_t>& intervals, scalar phase)
81     : PathEffectOpItem(CREATE_DASH), intervals_(intervals), phase_(phase) {}
82 
Playback(const CmdList & cmdList) const83 std::shared_ptr<PathEffect> CreateDashPathEffectOpItem::Playback(const CmdList& cmdList) const
84 {
85     auto intervals = CmdListHelper::GetVectorFromCmdList<scalar>(cmdList, intervals_);
86     int32_t count = intervals_.second / sizeof(scalar);
87     return PathEffect::CreateDashPathEffect(intervals.data(), count, phase_);
88 }
89 
CreatePathDashEffectOpItem(const CmdListHandle & path,scalar advance,scalar phase,PathDashStyle style)90 CreatePathDashEffectOpItem::CreatePathDashEffectOpItem(
91     const CmdListHandle& path, scalar advance, scalar phase, PathDashStyle style)
92     : PathEffectOpItem(CREATE_PATH_DASH), path_(path), advance_(advance), phase_(phase), style_(style) {}
93 
Playback(const CmdList & cmdList) const94 std::shared_ptr<PathEffect> CreatePathDashEffectOpItem::Playback(const CmdList& cmdList) const
95 {
96     auto path = CmdListHelper::GetFromCmdList<PathCmdList, Path>(cmdList, path_);
97     if (path == nullptr) {
98         return nullptr;
99     }
100     return PathEffect::CreatePathDashEffect(*path, advance_, phase_, style_);
101 }
102 
CreateCornerPathEffectOpItem(scalar radius)103 CreateCornerPathEffectOpItem::CreateCornerPathEffectOpItem(scalar radius)
104     : PathEffectOpItem(CREATE_CORNER), radius_(radius) {}
105 
Playback() const106 std::shared_ptr<PathEffect> CreateCornerPathEffectOpItem::Playback() const
107 {
108     return PathEffect::CreateCornerPathEffect(radius_);
109 }
110 
CreateSumPathEffectOpItem(const CmdListHandle & effect1,const CmdListHandle & effect2)111 CreateSumPathEffectOpItem::CreateSumPathEffectOpItem(const CmdListHandle& effect1, const CmdListHandle& effect2)
112     : PathEffectOpItem(CREATE_SUM), effect1_(effect1), effect2_(effect2) {}
113 
Playback(const CmdList & cmdList) const114 std::shared_ptr<PathEffect> CreateSumPathEffectOpItem::Playback(const CmdList& cmdList) const
115 {
116     auto effect1 = CmdListHelper::GetFromCmdList<PathEffectCmdList, PathEffect>(cmdList, effect1_);
117     auto effect2 = CmdListHelper::GetFromCmdList<PathEffectCmdList, PathEffect>(cmdList, effect2_);
118     if (effect1 == nullptr || effect2 == nullptr) {
119         return nullptr;
120     }
121 
122     return PathEffect::CreateSumPathEffect(*effect1, *effect2);
123 }
124 
CreateComposePathEffectOpItem(const CmdListHandle & effect1,const CmdListHandle & effect2)125 CreateComposePathEffectOpItem::CreateComposePathEffectOpItem(
126     const CmdListHandle& effect1, const CmdListHandle& effect2)
127     : PathEffectOpItem(CREATE_COMPOSE), effect1_(effect1), effect2_(effect2) {}
128 
Playback(const CmdList & cmdList) const129 std::shared_ptr<PathEffect> CreateComposePathEffectOpItem::Playback(const CmdList& cmdList) const
130 {
131     auto effect1 = CmdListHelper::GetFromCmdList<PathEffectCmdList, PathEffect>(cmdList, effect1_);
132     auto effect2 = CmdListHelper::GetFromCmdList<PathEffectCmdList, PathEffect>(cmdList, effect2_);
133     if (effect1 == nullptr || effect2 == nullptr) {
134         return nullptr;
135     }
136 
137     return PathEffect::CreateComposePathEffect(*effect1, *effect2);
138 }
139 } // namespace Drawing
140 } // namespace Rosen
141 } // namespace OHOS
142