• 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/mask_cmd_list.h"
17 
18 #include "recording/cmd_list_helper.h"
19 #include "utils/log.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
24 namespace {
25 Pen defaultPen;
26 }
27 
CreateFromData(const CmdListData & data,bool isCopy)28 std::shared_ptr<MaskCmdList> MaskCmdList::CreateFromData(const CmdListData& data, bool isCopy)
29 {
30     auto cmdList = std::make_shared<MaskCmdList>();
31     if (isCopy) {
32         cmdList->opAllocator_.BuildFromDataWithCopy(data.first, data.second);
33     } else {
34         cmdList->opAllocator_.BuildFromData(data.first, data.second);
35     }
36     return cmdList;
37 }
38 
Playback(std::shared_ptr<Path> & path,Brush & brush) const39 bool MaskCmdList::Playback(std::shared_ptr<Path>& path, Brush& brush) const
40 {
41     uint32_t offset = 0;
42     MaskPlayer player(path, brush, *this);
43     do {
44         void* itemPtr = opAllocator_.OffsetToAddr(offset);
45         OpItem* curOpItemPtr = static_cast<OpItem*>(itemPtr);
46         if (curOpItemPtr != nullptr) {
47             if (!player.Playback(curOpItemPtr->GetType(), itemPtr)) {
48                 LOGD("MaskCmdList::Playback failed!");
49                 break;
50             }
51 
52             offset = curOpItemPtr->GetNextOpItemOffset();
53         } else {
54             LOGE("MaskCmdList::Playback failed, opItem is nullptr");
55             break;
56         }
57     } while (offset != 0);
58 
59     return true;
60 }
61 
Playback(std::shared_ptr<Path> & path,Pen & pen,Brush & brush) const62 bool MaskCmdList::Playback(std::shared_ptr<Path>& path, Pen& pen, Brush& brush) const
63 {
64     uint32_t offset = 0;
65     MaskPlayer player(path, brush, pen, *this);
66     do {
67         void* itemPtr = opAllocator_.OffsetToAddr(offset);
68         OpItem* curOpItemPtr = static_cast<OpItem*>(itemPtr);
69         if (curOpItemPtr != nullptr) {
70             if (!player.Playback(curOpItemPtr->GetType(), itemPtr)) {
71                 LOGE("MaskCmdList::Playback failed!");
72                 break;
73             }
74 
75             offset = curOpItemPtr->GetNextOpItemOffset();
76         } else {
77             LOGE("MaskCmdList::Playback failed, opItem is nullptr");
78             break;
79         }
80     } while (offset != 0);
81 
82     return true;
83 }
84 
85 /* OpItem */
86 std::unordered_map<uint32_t, MaskPlayer::MaskPlaybackFunc> MaskPlayer::opPlaybackFuncLUT_ = {
87     { MaskOpItem::MASK_BRUSH_OPITEM,          MaskBrushOpItem::Playback },
88     { MaskOpItem::MASK_PATH_OPITEM,           MaskPathOpItem::Playback },
89     { MaskOpItem::MASK_PEN_OPITEM,           MaskPenOpItem::Playback },
90 };
91 
MaskPlayer(std::shared_ptr<Path> & path,Brush & brush,const CmdList & cmdList)92 MaskPlayer::MaskPlayer(std::shared_ptr<Path>& path, Brush& brush, const CmdList& cmdList)
93     : path_(path), brush_(brush), pen_(defaultPen), cmdList_(cmdList) {}
94 
MaskPlayer(std::shared_ptr<Path> & path,Brush & brush,Pen & pen,const CmdList & cmdList)95 MaskPlayer::MaskPlayer(std::shared_ptr<Path>& path, Brush& brush, Pen& pen, const CmdList& cmdList)
96     : path_(path), brush_(brush), pen_(pen), cmdList_(cmdList) {}
97 
Playback(uint32_t type,const void * opItem)98 bool MaskPlayer::Playback(uint32_t type, const void* opItem)
99 {
100     if (type == MaskOpItem::OPITEM_HEAD) {
101         return true;
102     }
103 
104     auto it = opPlaybackFuncLUT_.find(type);
105     if (it == opPlaybackFuncLUT_.end() || it->second == nullptr) {
106         return false;
107     }
108 
109     auto func = it->second;
110     (*func)(*this, opItem);
111 
112     return true;
113 }
114 
MaskBrushOpItem(const BrushHandle & brushHandle)115 MaskBrushOpItem::MaskBrushOpItem(const BrushHandle& brushHandle)
116     : MaskOpItem(MASK_BRUSH_OPITEM), brushHandle_(brushHandle) {}
117 
Playback(MaskPlayer & player,const void * opItem)118 void MaskBrushOpItem::Playback(MaskPlayer& player, const void* opItem)
119 {
120     if (opItem != nullptr) {
121         const auto* op = static_cast<const MaskBrushOpItem*>(opItem);
122         op->Playback(player.brush_, player.cmdList_);
123     }
124 }
125 
Playback(Brush & brush,const CmdList & cmdList) const126 void MaskBrushOpItem::Playback(Brush& brush, const CmdList& cmdList) const
127 {
128     auto colorSpace = CmdListHelper::GetColorSpaceFromCmdList(
129         cmdList, brushHandle_.colorSpaceHandle);
130     auto shaderEffect = CmdListHelper::GetShaderEffectFromCmdList(
131         cmdList, brushHandle_.shaderEffectHandle);
132     auto colorFilter = CmdListHelper::GetColorFilterFromCmdList(
133         cmdList, brushHandle_.colorFilterHandle);
134     auto imageFilter = CmdListHelper::GetImageFilterFromCmdList(
135         cmdList, brushHandle_.imageFilterHandle);
136     auto maskFilter = CmdListHelper::GetMaskFilterFromCmdList(
137         cmdList, brushHandle_.maskFilterHandle);
138 
139     Filter filter;
140     filter.SetColorFilter(colorFilter);
141     filter.SetImageFilter(imageFilter);
142     filter.SetMaskFilter(maskFilter);
143     filter.SetFilterQuality(brushHandle_.filterQuality);
144 
145     const Color4f color4f = { brushHandle_.color.GetRedF(), brushHandle_.color.GetGreenF(),
146         brushHandle_.color.GetBlueF(), brushHandle_.color.GetAlphaF() };
147 
148     brush.SetColor(color4f, colorSpace);
149     brush.SetShaderEffect(shaderEffect);
150     brush.SetBlendMode(brushHandle_.mode);
151     brush.SetAntiAlias(brushHandle_.isAntiAlias);
152     brush.SetFilter(filter);
153 }
154 
MaskPathOpItem(const OpDataHandle & pathHandle)155 MaskPathOpItem::MaskPathOpItem(const OpDataHandle& pathHandle)
156     : MaskOpItem(MASK_PATH_OPITEM), pathHandle_(pathHandle) {}
157 
Playback(MaskPlayer & player,const void * opItem)158 void MaskPathOpItem::Playback(MaskPlayer& player, const void* opItem)
159 {
160     if (opItem != nullptr) {
161         const auto* op = static_cast<const MaskPathOpItem*>(opItem);
162         op->Playback(player.path_, player.cmdList_);
163     }
164 }
165 
Playback(std::shared_ptr<Path> & path,const CmdList & cmdList) const166 void MaskPathOpItem::Playback(std::shared_ptr<Path>& path, const CmdList& cmdList) const
167 {
168     auto readPath = CmdListHelper::GetPathFromCmdList(cmdList, pathHandle_);
169     path = readPath;
170 }
171 
MaskPenOpItem(const PenHandle & penHandle)172 MaskPenOpItem::MaskPenOpItem(const PenHandle& penHandle)
173     : MaskOpItem(MASK_PEN_OPITEM), penHandle_(penHandle) {}
174 
Playback(MaskPlayer & player,const void * opItem)175 void MaskPenOpItem::Playback(MaskPlayer &player, const void *opItem)
176 {
177     if (opItem != nullptr) {
178         const auto* op = static_cast<const MaskPenOpItem*>(opItem);
179         op->Playback(player.pen_, player.cmdList_);
180     }
181 }
182 
Playback(Pen & pen,const CmdList & cmdList) const183 void MaskPenOpItem::Playback(Pen& pen, const CmdList& cmdList) const
184 {
185     pen.SetWidth(penHandle_.width);
186     pen.SetMiterLimit(penHandle_.miterLimit);
187     pen.SetJoinStyle(penHandle_.joinStyle);
188     pen.SetCapStyle(penHandle_.capStyle);
189 
190     auto pathEffect = CmdListHelper::GetPathEffectFromCmdList(cmdList, penHandle_.pathEffectHandle);
191     pen.SetPathEffect(pathEffect);
192     pen.SetColor(penHandle_.color);
193 }
194 } // namespace Drawing
195 } // namespace Rosen
196 } // namespace OHOS
197