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_antialias.h 18 * @brief Defines Rasterization and scanline process stage processing 19 * @since 1.0 20 * @version 1.0 21 */ 22 23 #ifndef GRAPHIC_LITE_RASTERIZER_SCANLINE_ANTIALIAS_H 24 #define GRAPHIC_LITE_RASTERIZER_SCANLINE_ANTIALIAS_H 25 26 #include "rasterizer_cells_antialias.h" 27 #include "rasterizer_scanline_clip.h" 28 #include "gfx_utils/diagram/scanline/geometry_scanline.h" 29 namespace OHOS { 30 /** 31 * @class RasterizerScanlineAntiAlias 32 * @brief Polygon rasterization is used for high-quality rendering of filled polygons, 33 * The int32_t coordinates of this class include the format of 24.8, 34 * and 24 bits are used for the int32_t part of the coordinates, 35 * 8 bits are used for the shift offset part of the sub-pixel, and then move_ to(x, y) / line_ to(x, y) 36 * When constructing polygons, the scanline is rasterized 37 * @since 1.0 38 * @version 1.0 39 */ 40 class RasterizerScanlineAntialias { 41 /** 42 * @brief Build scanline status enumeration 43 * @since 1.0 44 * @version 1.0 45 */ 46 enum RasterizerStatus { 47 STATUS_INITIAL, 48 STATUS_MOVE_TO, 49 STATUS_LINE_TO, 50 STATUS_CLOSED 51 }; 52 53 public: 54 enum AntialiasScale { 55 AA_SHIFT = 8, 56 AA_SCALE = 1 << AA_SHIFT, 57 AA_MASK = AA_SCALE - 1, 58 AA_SCALE2 = AA_SCALE * 2, 59 AA_MASK2 = AA_SCALE2 - 1 60 }; 61 62 /** 63 * Construction of rasterized scanline antialiasing constructor 64 * @brief It mainly includes the allocation quota of cell block and cutter 65 * Filling rules, automatic closing, starting position, etc. 66 * @since 1.0 67 * @version 1.0 68 */ 69 RasterizerScanlineAntialias(uint32_t cell_block_limit = (1 << (AA_SHIFT + 2))) outline_(cell_block_limit)70 : outline_(cell_block_limit), 71 clipper_(), 72 fillingRule_(FILL_NON_ZERO), 73 autoClose_(true), 74 startX_(0), 75 startY_(0), 76 status_(STATUS_INITIAL), 77 scanY_(0) 78 { 79 for (int32_t coverIndex = 0; coverIndex < AA_SCALE; coverIndex++) { 80 gammar_[coverIndex] = coverIndex; 81 } 82 } 83 84 /** 85 * @brief Reset the cell array for building contour lines, 86 * Reset the scan line status value, etc. 87 * @since 1.0 88 * @version 1.0 89 */ 90 void Reset(); 91 92 /** 93 * @brief Reset the clipping range and clipping flag of the clipper. 94 * @since 1.0 95 * @version 1.0 96 */ 97 void ResetClipping(); 98 void ClipBox(float x1, float y1, float x2, float y2); 99 AutoClose(bool flag)100 void AutoClose(bool flag) 101 { 102 autoClose_ = flag; 103 } 104 105 /** 106 * @brief Set the starting position of the element according to the of 1 / 256 pixel unit. 107 * @since 1.0 108 * @version 1.0 109 */ 110 void MoveTo(int32_t x, int32_t y); 111 /** 112 * @brief The moving position of the element is set according to the 1 / 256 pixel unit. 113 * @since 1.0 114 * @version 1.0 115 */ 116 void LineTo(int32_t x, int32_t y); 117 /** 118 * @brief Set the starting position of the element according to the of 1 pixel unit. 119 * @since 1.0 120 * @version 1.0 121 */ 122 void MoveToByfloat(float x, float y); 123 /** 124 * @brief Set the moving position of the element according to the pixel unit. 125 * @since 1.0 126 * @version 1.0 127 */ 128 void LineToByfloat(float x, float y); 129 130 /** 131 * @brief Closed polygon processing. 132 * @since 1.0 133 * @version 1.0 134 */ 135 void ClosePolygon(); 136 void AddVertex(float x, float y, uint32_t cmd); 137 138 /** 139 * @brief Obtain the vertex information coordinates from the vertex source and follow the scanning process 140 * Sets the procedure for adding an array of cells. 141 * @since 1.0 142 * @version 1.0 143 */ 144 template <typename VertexSource> 145 void AddPath(VertexSource& vs, uint32_t pathId = 0) 146 { 147 float x; 148 float y; 149 150 uint32_t cmd; 151 vs.Rewind(pathId); 152 if (outline_.GetSorted()) { 153 Reset(); 154 } 155 while (!IsStop(cmd = vs.GenerateVertex(&x, &y))) { 156 AddVertex(x, y, cmd); 157 } 158 } 159 160 /** 161 * @brief The range boundary value of the contour line. 162 * @since 1.0 163 * @version 1.0 164 */ GetMinX()165 int32_t GetMinX() const 166 { 167 return outline_.GetMinX(); 168 } GetMinY()169 int32_t GetMinY() const 170 { 171 return outline_.GetMinY(); 172 } GetMaxX()173 int32_t GetMaxX() const 174 { 175 return outline_.GetMaxX(); 176 } GetMaxY()177 int32_t GetMaxY() const 178 { 179 return outline_.GetMaxY(); 180 } 181 182 /** 183 * @brief The cells in the contour line are sorted from left to right and from top to bottom. 184 * @since 1.0 185 * @version 1.0 186 */ 187 void Sort(); 188 bool RewindScanlines(); 189 190 /** 191 * @brief Convert area cover to gamma cover value to calculate alpha. 192 * @since 1.0 193 * @version 1.0 194 */ 195 uint32_t CalculateAlpha(int32_t area) const; 196 /** 197 * @brief Scan line that gets a y value from rasterizer stage 198 * And iterate the cell array of the current scan line to obtain area->cover, 199 * Use both to calculate delta area as area cover and convert it into gamma cover 200 * The color information is obtained successfully, 201 * and then the alpha information of color is calculated by gamma function 202 * Fill in the new scanline and have subsequent render. 203 * @since 1.0 204 * @version 1.0 205 */ 206 bool SweepScanline(GeometryScanline& sl); 207 208 private: 209 // Disable copying 210 RasterizerScanlineAntialias(const RasterizerScanlineAntialias&); 211 const RasterizerScanlineAntialias& operator=(const RasterizerScanlineAntialias&); 212 213 RasterizerCellsAntiAlias outline_; 214 RasterizerScanlineClip clipper_; 215 int32_t gammar_[AA_SCALE]; 216 FillingRule fillingRule_; 217 bool autoClose_; 218 int32_t startX_; 219 int32_t startY_; 220 uint32_t status_; 221 int32_t scanY_; 222 }; 223 } // namespace OHOS 224 #endif 225