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_scanline_clip.h
18  * @brief Defines Light fence clipping
19  * @since 1.0
20  * @version 1.0
21  */
22 
23 #ifndef GRAPHIC_LITE_RASTERIZER_SCANLINE_CLIP_H
24 #define GRAPHIC_LITE_RASTERIZER_SCANLINE_CLIP_H
25 
26 #include "gfx_utils/diagram/common/common_clip_operate.h"
27 #include "gfx_utils/graphic_math.h"
28 #include "gfx_utils/diagram/rasterizer/rasterizer_cells_antialias.h"
29 namespace OHOS {
30 /**
31  * The PolyMaxCoord enumeration type
32  * Defines the value of the maximum coordinate of the polygon
33  * @since 1.0
34  * @version 1.0
35  */
36 enum PolyMaxCoord {
37     POLY_MAX_COORD = (1 << 30) - 1
38 };
39 
40 /**
41  * @struct RasterDepictInt
42  * @brief The RasterDepictInt structure is within the target range
43  * When clipping coordinates, for 3 values similar to coordinate spacing, or
44  * Upper sampling and lower sampling are processed.
45  * @since 1.0
46  * @version 1.0
47  */
48 struct RasterDepictInt {
49     /**
50      * @brief This function is mainly for the input coordinate values
51      * Do sampling processing.
52      * @since 1.0
53      * @version 1.0
54      */
UpScaleRasterDepictInt55     static int32_t UpScale(float vUpscale)
56     {
57         return MATH_ROUND32(vUpscale * POLY_SUBPIXEL_SCALE);
58     }
59 
60     /**
61      * @brief This function is mainly for the input coordinate values
62      * Do the next sampling.
63      * @since 1.0
64      * @version 1.0
65      */
DownScaleRasterDepictInt66     static int32_t DownScale(int32_t vDownscale)
67     {
68         return vDownscale;
69     }
70 
MultDivRasterDepictInt71     static inline int32_t MultDiv(float deltaA, float deltaB, float dealtaC)
72     {
73         if (dealtaC != 0) {
74             return MATH_ROUND32(deltaA * deltaB / dealtaC);
75         }
76         return 0;
77     }
78 
GetXCoordinateValueRasterDepictInt79     static int32_t GetXCoordinateValue(int32_t xValue)
80     {
81         return xValue;
82     }
GetYCoordinateValueRasterDepictInt83     static int32_t GetYCoordinateValue(int32_t yValue)
84     {
85         return yValue;
86     }
87 };
88 
89 /**
90  * @class RasterizerScanlineClip
91  * @brief Defines In the rasterization stage, when exchanging scan line processing, for
92  * Coordinate cutting and processing
93  * @since 1.0
94  * @version 1.0
95  */
96 class RasterizerScanlineClip {
97 public:
98     /**
99      * @brief RasterizerScanlineClip Class constructor
100      * Initialize clipping range, clipping flag, etc.
101      * @since 1.0
102      * @version 1.0
103      */
RasterizerScanlineClip()104     RasterizerScanlineClip()
105         : clipBox_(0, 0, 0, 0),
106           x1_(0),
107           y1_(0),
108           clippingFlags_(0),
109           clipping_(false) {}
110 
ResetClipping()111     void ResetClipping()
112     {
113         clipping_ = false;
114     }
115 
116     /**
117      * @brief Sets the clipping range.
118      * @since 1.0
119      * @version 1.0
120      */
ClipBox(int32_t left,int32_t top,int32_t right,int32_t bottom)121     void ClipBox(int32_t left, int32_t top, int32_t right, int32_t bottom)
122     {
123         clipBox_ = Rect32(left, top, right, bottom);
124         clipBox_.Normalize();
125         clipping_ = true;
126     }
127 
128     /**
129      * @brief In the RASTERIZER process, the starting point of setting is added,
130      * And set the flag of clippingFlags_
131      * @since 1.0
132      * @version 1.0
133      */
MoveTo(int32_t x1,int32_t y1)134     void MoveTo(int32_t x1, int32_t y1)
135     {
136         x1_ = x1;
137         y1_ = y1;
138         if (clipping_) {
139             clippingFlags_ = ClippingFlags(x1, y1, clipBox_);
140         }
141     }
142     /**
143      * @brief In the RASTERIZER process, add the set sampling point
144      * And set the sampling point, set the related cover and area attributes, etc
145      *         |        |
146      *   0110  |  0010  | 0011
147      *         |        |
148      *  -------+--------+-------- clip_box.y2
149      *         |        |
150      *   0100  |  0000  | 0001
151      *         |        |
152      *  -------+--------+-------- clip_box.y1
153      *         |        |
154      *   1100  |  1000  | 1001
155      *         |        |
156      *   clip_box.x1  clip_box.x2
157      * @since 1.0
158      * @version 1.0
159      */
160     void LineTo(RasterizerCellsAntiAlias& ras, int32_t x2, int32_t y2);
161 
162 private:
163     /**
164      * @brief In the RASTERIZER process,Judge the mark according to the last clipping range
165      * And the cutting range judgment flag this time,
166      * add the actual sampling points and set relevant attributes
167      * @since 1.0
168      * @version 1.0
169      */
170     inline void LineClipY(RasterizerCellsAntiAlias& ras,
171                           int32_t x1, int32_t y1,
172                           int32_t x2, int32_t y2,
173                           uint32_t clipFlagsOne, uint32_t clipFlagsTwo) const;
174     Rect32 clipBox_;
175     int32_t x1_;
176     int32_t y1_;
177     uint32_t clippingFlags_;
178     bool clipping_;
179 };
180 } // namespace OHOS
181 #endif
182