1 | /* |
2 | * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved. |
3 | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | * of this software and associated documentation files (the "Software"), to deal |
6 | * in the Software without restriction, including without limitation the rights |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8 | * copies of the Software, and to permit persons to whom the Software is |
9 | * furnished to do so, subject to the following conditions: |
10 | |
11 | * The above copyright notice and this permission notice shall be included in all |
12 | * copies or substantial portions of the Software. |
13 | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
20 | * SOFTWARE. |
21 | */ |
22 | |
23 | #include "tvgSwCommon.h" |
24 | |
25 | |
26 | /************************************************************************/ |
27 | /* Internal Class Implementation */ |
28 | /************************************************************************/ |
29 | |
30 | |
31 | /************************************************************************/ |
32 | /* External Class Implementation */ |
33 | /************************************************************************/ |
34 | |
35 | SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx) |
36 | { |
37 | return &mpool->outline[idx]; |
38 | } |
39 | |
40 | |
41 | void mpoolRetOutline(SwMpool* mpool, unsigned idx) |
42 | { |
43 | mpool->outline[idx].pts.clear(); |
44 | mpool->outline[idx].cntrs.clear(); |
45 | mpool->outline[idx].types.clear(); |
46 | mpool->outline[idx].closed.clear(); |
47 | } |
48 | |
49 | |
50 | SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx) |
51 | { |
52 | return &mpool->strokeOutline[idx]; |
53 | } |
54 | |
55 | |
56 | void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx) |
57 | { |
58 | mpool->strokeOutline[idx].pts.clear(); |
59 | mpool->strokeOutline[idx].cntrs.clear(); |
60 | mpool->strokeOutline[idx].types.clear(); |
61 | mpool->strokeOutline[idx].closed.clear(); |
62 | } |
63 | |
64 | |
65 | SwMpool* mpoolInit(unsigned threads) |
66 | { |
67 | auto allocSize = threads + 1; |
68 | |
69 | auto mpool = static_cast<SwMpool*>(calloc(sizeof(SwMpool), 1)); |
70 | mpool->outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * allocSize)); |
71 | if (!mpool->outline) goto err; |
72 | |
73 | mpool->strokeOutline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * allocSize)); |
74 | if (!mpool->strokeOutline) goto err; |
75 | |
76 | mpool->allocSize = allocSize; |
77 | |
78 | return mpool; |
79 | |
80 | err: |
81 | if (mpool->outline) { |
82 | free(mpool->outline); |
83 | mpool->outline = nullptr; |
84 | } |
85 | |
86 | if (mpool->strokeOutline) { |
87 | free(mpool->strokeOutline); |
88 | mpool->strokeOutline = nullptr; |
89 | } |
90 | free(mpool); |
91 | return nullptr; |
92 | } |
93 | |
94 | |
95 | bool mpoolClear(SwMpool* mpool) |
96 | { |
97 | SwOutline* p; |
98 | |
99 | for (unsigned i = 0; i < mpool->allocSize; ++i) { |
100 | //Outline |
101 | p = &mpool->outline[i]; |
102 | p->pts.reset(); |
103 | p->cntrs.reset(); |
104 | p->types.reset(); |
105 | p->closed.reset(); |
106 | |
107 | //StrokeOutline |
108 | p = &mpool->strokeOutline[i]; |
109 | p->pts.reset(); |
110 | p->cntrs.reset(); |
111 | p->types.reset(); |
112 | p->closed.reset(); |
113 | } |
114 | |
115 | return true; |
116 | } |
117 | |
118 | |
119 | bool mpoolTerm(SwMpool* mpool) |
120 | { |
121 | if (!mpool) return false; |
122 | |
123 | mpoolClear(mpool); |
124 | |
125 | if (mpool->outline) { |
126 | free(mpool->outline); |
127 | mpool->outline = nullptr; |
128 | } |
129 | |
130 | if (mpool->strokeOutline) { |
131 | free(mpool->strokeOutline); |
132 | mpool->strokeOutline = nullptr; |
133 | } |
134 | |
135 | free(mpool); |
136 | |
137 | return true; |
138 | } |
139 | |