1 /*
2 * Copyright (c) 2022 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 /**
17 * @file rasterizer_cells_antialias.h
18 * @brief Defines Grating cell (anti aliasing)
19 * @since 1.0
20 * @version 1.0
21 */
22
23 #ifndef GRAPHIC_LITE_RASTERIZER_CELLS_ANTIALIAS_H
24 #define GRAPHIC_LITE_RASTERIZER_CELLS_ANTIALIAS_H
25
26 #include "gfx_utils/diagram/common/common_math.h"
27 #include "gfx_utils/vector.h"
28
29 namespace OHOS {
30 // There is no constructor defined for pixel cells,
31 // which is to avoid the additional overhead of allocating cell arrays
32 struct CellBuildAntiAlias {
33 int32_t x;
34 int32_t y;
35 int32_t cover;
36 int32_t area;
37
InitialCellBuildAntiAlias38 void Initial()
39 {
40 x = INT32_MAX;
41 y = INT32_MAX;
42 cover = 0;
43 area = 0;
44 }
45
StyleCellBuildAntiAlias46 void Style(const CellBuildAntiAlias&) {}
47
NotEqualCellBuildAntiAlias48 int32_t NotEqual(int32_t ex, int32_t ey, const CellBuildAntiAlias&) const
49 {
50 return (static_cast<uint32_t>(ex) - static_cast<uint32_t>(x))
51 | (static_cast<uint32_t>(ey) - static_cast<uint32_t>(y));
52 }
53 };
54
55 class RasterizerCellsAntiAlias {
56 struct SortedYLevel {
57 uint32_t start;
58 uint32_t num;
59 };
60
61 /**
62 * @brief Build the offset of 'cell unit', mask mask, cell pool capacity, etc
63 * @since 1.0
64 * @version 1.0
65 */
66 enum CellBlockScale {
67 CELL_BLOCK_SHIFT = 5,
68 CELL_BLOCK_SIZE = 1 << CELL_BLOCK_SHIFT,
69 CELL_BLOCK_MASK = CELL_BLOCK_SIZE - 1,
70 CELL_BLOCK_POOL = 256
71 };
72
73 enum DxLimit {
74 DX_LIMIT = CONSTITUTION << POLY_SUBPIXEL_SHIFT
75 };
76
77 public:
78 ~RasterizerCellsAntiAlias();
79
80 /**
81 * @brief RasterizerCellsAntiAlias Class constructor
82 * initialization numBlocks_,maxBlocks_,currBlock_ etc
83 * @since 1.0
84 * @version 1.0
85 */
86 RasterizerCellsAntiAlias(uint32_t cellBlockLimit = 1024);
87
88 /**
89 * Reinitialize settings numBlocks_,maxBlocks_,currBlock_ etc。
90 * @since 1.0
91 * @version 1.0
92 */
93 void Reset();
94 void SetStyle(const CellBuildAntiAlias& styleCell);
95
96 /**
97 * @brief According to the incoming 2 coordinate points (both with sub pixels),
98 * The process of constructing rasterized cell points is from y to X.
99 * @since 1.0
100 * @version 1.0
101 */
102 void LineOperate(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
103
104 void LineOperateVerticalLine();
105
106 /**
107 * @brief The rasterization process builds the coordinate range of the entity.
108 * @since 1.0
109 * @version 1.0
110 */
GetMinX()111 int32_t GetMinX() const
112 {
113 return minX_;
114 }
GetMinY()115 int32_t GetMinY() const
116 {
117 return minY_;
118 }
GetMaxX()119 int32_t GetMaxX() const
120 {
121 return maxX_;
122 }
GetMaxY()123 int32_t GetMaxY() const
124 {
125 return maxY_;
126 }
127
128 /**
129 * @brief In the rasterization process, all cells are rasterized according to
130 * Sort from left to right and from top to bottom.
131 * @since 1.0
132 * @version 1.0
133 */
134 void SortAllCells();
135
GetTotalCells()136 uint32_t GetTotalCells() const
137 {
138 return numCells_;
139 }
140
141 /**
142 * @brief In the process of rasterization, it is calculated according to the coordinate height of Y
143 * Total number of cells.
144 * @since 1.0
145 * @version 1.0
146 */
GetScanlineNumCells(uint32_t yLevel)147 uint32_t GetScanlineNumCells(uint32_t yLevel)
148 {
149 return sortedY_[yLevel - minY_].num;
150 }
151
152 /**
153 * @brief In the process of rasterization, it is calculated according to the coordinate height of Y
154 * The first address of the sorted cells array.
155 * @since 1.0
156 * @version 1.0
157 */
GetScanlineCells(uint32_t yLevel)158 const CellBuildAntiAlias * const *GetScanlineCells(uint32_t yLevel)
159 {
160 return sortedCells_ + sortedY_[yLevel - minY_].start;
161 }
162
GetSorted()163 bool GetSorted() const
164 {
165 return sorted_;
166 }
167
168 private:
169 RasterizerCellsAntiAlias(const CellBuildAntiAlias&);
170 const CellBuildAntiAlias& operator=(const CellBuildAntiAlias&);
171
172 /**
173 * @brief Set the current cell during rasterization.
174 * @since 1.0
175 * @version 1.0
176 */
177 void SetCurrentCell(int32_t x, int32_t y);
178
179 void OutLineLegal(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
180
181 /**
182 * @brief Add the current cell during rasterization.
183 * @since 1.0
184 * @version 1.0
185 */
186 void AddCurrentCell();
187
188 /**
189 * @brief n the rasterization process, the horizontal direction is
190 * from x1 to x2 according to the coordinate height value of ey,
191 * The filling process of cell cells longitudinally from sub-pixel mask y1 to sub-pixel mask y2.
192 * @since 1.0
193 * @version 1.0
194 */
195 void RenderHorizonline(int32_t ey, int32_t x1, int32_t submaskFlagsY1, int32_t x2, int32_t submaskFlagsY2);
196
197 void RenderVerticalLine(int32_t& x1, int32_t& ex1, int64_t& dy, int32_t& first,
198 int32_t& increase, int32_t& xFrom, int32_t& submaskFlagsY1,
199 int32_t& submaskFlagsY2, int32_t& ey1, int32_t& ey2, int32_t& delta);
200
201 void RenderObliqueLine(int64_t& dx, int64_t& dy, int32_t& first,
202 int32_t& increase, int32_t& xFrom, int64_t& deltaxMask,
203 int32_t& ey1, int32_t& ey2, int32_t& delta);
204
205 /**
206 * @brief Allocate array space for cells during rasterization.
207 * @since 1.0
208 * @version 1.0
209 */
210 void AllocateBlock();
211
212 private:
213 uint32_t numBlocks_;
214 uint32_t maxBlocks_;
215 uint32_t currBlock_;
216 uint32_t numCells_;
217 uint32_t cellBlockLimit_;
218 CellBuildAntiAlias** cells_;
219 CellBuildAntiAlias* currCellPtr_;
220 CellBuildAntiAlias** sortedCells_;
221 SortedYLevel* sortedY_;
222 CellBuildAntiAlias currCell_;
223 CellBuildAntiAlias styleCell_;
224 int32_t minX_;
225 int32_t minY_;
226 int32_t maxX_;
227 int32_t maxY_;
228 bool sorted_;
229 };
230
231 class ScanlineHitRegionMeasure {
232 public:
ScanlineHitRegionMeasure(int32_t x)233 ScanlineHitRegionMeasure(int32_t x) : xCoordinate_(x), hitMeasureFlags_(false) {}
234
AddCellInContainer(int32_t x,int32_t)235 void AddCellInContainer(int32_t x, int32_t)
236 {
237 if (xCoordinate_ == x) {
238 hitMeasureFlags_ = true;
239 }
240 }
AddSpanInContainer(int32_t x,int32_t len,int32_t)241 void AddSpanInContainer(int32_t x, int32_t len, int32_t)
242 {
243 if (xCoordinate_ >= x && xCoordinate_ < x + len) {
244 hitMeasureFlags_ = true;
245 }
246 }
GetNumberSpans()247 uint32_t GetNumberSpans() const
248 {
249 return 1;
250 }
GetHitMeasure()251 bool GetHitMeasure() const
252 {
253 return hitMeasureFlags_;
254 }
255
256 private:
257 int32_t xCoordinate_;
258 bool hitMeasureFlags_;
259 };
260
261 /**
262 * @brief The exchange of cells in the rasterization process.
263 * @since 1.0
264 * @version 1.0
265 */
266 template <class T>
SwapCells(T * oneCells,T * twoCells)267 inline void SwapCells(T* oneCells, T* twoCells)
268 {
269 T tempCells = *oneCells;
270 *oneCells = *twoCells;
271 *twoCells = tempCells;
272 }
273
274 void QsortCellsSweep(CellBuildAntiAlias*** base, CellBuildAntiAlias*** iIndex, CellBuildAntiAlias*** jIndex);
275
276 /**
277 * @brief In the rasterization process, all cells are sorted quickly.
278 * @since 1.0
279 * @version 1.0
280 */
281 void QsortCells(CellBuildAntiAlias** start, uint32_t num);
282
283 void QsortCellsFor(CellBuildAntiAlias*** iIndex, CellBuildAntiAlias*** jIndex,
284 CellBuildAntiAlias*** limit, CellBuildAntiAlias*** base);
285 } // namespace OHOS
286 #endif
287