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 #ifndef MASK_CMD_LIST_H
17 #define MASK_CMD_LIST_H
18 
19 #include "recording/cmd_list.h"
20 #include "draw_cmd.h"
21 #include "recording/recording_handle.h"
22 #include "draw/path.h"
23 #include "draw/brush.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 
29 
30 /* OpItem */
31 /**
32  * @brief  Helper class for mask playback.
33  *         Contains the playback context and a static mapping table: { OpItemType, OpItemPlaybackFunc }.
34  */
35 class MaskPlayer {
36 public:
37     MaskPlayer(std::shared_ptr<Path>& path, Brush& brush, const CmdList& cmdList);
38     MaskPlayer(std::shared_ptr<Path>& path, Brush& brush, Pen& pen, const CmdList& cmdList);
39     ~MaskPlayer() = default;
40 
41     /**
42      * @brief  Obtain the corresponding func according to the type lookup mapping table
43      *         and then invoke the func to plays opItem back to mask which in context.
44      */
45     bool Playback(uint32_t type, const void* opItem, size_t leftOpAllocatorSize);
46 
47     std::shared_ptr<Path>& path_;
48     Brush& brush_;
49     Pen& pen_;
50     const CmdList& cmdList_;
51 
52     using MaskPlaybackFunc = void(*)(MaskPlayer& palyer, const void* opItem, size_t leftOpAllocatorSize);
53 private:
54     static std::unordered_map<uint32_t, MaskPlaybackFunc> opPlaybackFuncLUT_;
55 };
56 
57 class DRAWING_API MaskCmdList : public CmdList {
58 public:
59     MaskCmdList() = default;
60     ~MaskCmdList() override = default;
61 
GetType()62     uint32_t GetType() const override
63     {
64         return Type::MASK_CMD_LIST;
65     }
66 
67     /**
68      * @brief         Creates a MaskCmdList with contiguous buffers.
69      * @param data    A contiguous buffers.
70      * @param isCopy  Whether to copy data or not.
71      */
72     static std::shared_ptr<MaskCmdList> CreateFromData(const CmdListData& data, bool isCopy = false);
73 
74     /**
75      * @brief  Calls the corresponding operations of all opitems in MaskCmdList to the mask.
76      */
77     bool Playback(std::shared_ptr<Path>& path, Brush& brush) const;
78 
79     bool Playback(std::shared_ptr<Path>& path, Pen& pen, Brush& brush) const;
80 
81 private:
82     bool Playback(MaskPlayer &player) const;
83 };
84 
85 class MaskOpItem : public OpItem {
86 public:
MaskOpItem(uint32_t type)87     MaskOpItem(uint32_t type) : OpItem(type) {}
88     ~MaskOpItem() = default;
89 
90     enum Type : uint32_t {
91         OPITEM_HEAD,
92         MASK_BRUSH_OPITEM,
93         MASK_PATH_OPITEM,
94         MASK_PEN_OPITEM
95     };
96 };
97 
98 class MaskBrushOpItem : public MaskOpItem {
99 public:
100     explicit MaskBrushOpItem(const BrushHandle& brushHandle);
101     ~MaskBrushOpItem() = default;
102 
103     static void Playback(MaskPlayer& player, const void* opItem, size_t leftOpAllocatorSize);
104 
105     void Playback(Brush& brush, const CmdList& cmdList) const;
106 private:
107     BrushHandle brushHandle_;
108 };
109 
110 class MaskPenOpItem : public MaskOpItem {
111 public:
112     explicit MaskPenOpItem(const PenHandle& penHandle);
113     ~MaskPenOpItem() = default;
114 
115     static void Playback(MaskPlayer& player, const void* opItem, size_t leftOpAllocatorSize);
116 
117     void Playback(Pen& pen, const CmdList& cmdList) const;
118 private:
119     PenHandle penHandle_;
120 };
121 
122 class MaskPathOpItem : public MaskOpItem {
123 public:
124     explicit MaskPathOpItem(const OpDataHandle& pathHandle);
125     ~MaskPathOpItem() = default;
126 
127     static void Playback(MaskPlayer& player, const void* opItem, size_t leftOpAllocatorSize);
128 
129     void Playback(std::shared_ptr<Path>& path, const CmdList& cmdList) const;
130 private:
131     OpDataHandle pathHandle_;
132 };
133 } // namespace Drawing
134 } // namespace Rosen
135 } // namespace OHOS
136 
137 #endif
138