1 /* 2 * Copyright (c) 2020-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 #ifndef GRAPHIC_LITE_DRAW_UTILS_H 17 #define GRAPHIC_LITE_DRAW_UTILS_H 18 19 #include "common/text.h" 20 #include "font/ui_font_header.h" 21 #include "gfx_utils/color.h" 22 #include "gfx_utils/geometry2d.h" 23 #include "gfx_utils/graphic_buffer.h" 24 #include "gfx_utils/graphic_types.h" 25 #include "gfx_utils/list.h" 26 #include "gfx_utils/style.h" 27 #include "gfx_utils/transform.h" 28 29 namespace OHOS { 30 #define SWAP_INT16(x, y) \ 31 do { \ 32 int16_t temp = (x); \ 33 (x) = (y); \ 34 (y) = temp; \ 35 } while (0) 36 37 #define SWAP_POINTS(x1, x2, y1, y2) \ 38 SWAP_INT16(x1, x2); \ 39 SWAP_INT16(y1, y2); 40 41 // FixedPointed Related definition. 42 #define FIXED_NUM_1 1048576 43 #define FIXED_Q_NUM 20 44 #define FO_TRANS_FLOAT_TO_FIXED(f) (static_cast<int64_t>((f) * FIXED_NUM_1)) 45 #define FO_TRANS_INTEGER_TO_FIXED(f) ((static_cast<int64_t>(f)) << FIXED_Q_NUM) 46 #define FO_DIV(n1, n2) ((static_cast<int64_t>(n1) << FIXED_Q_NUM) / (n2)) 47 #define FO_TO_INTEGER(n) ((n) >= 0 ? ((n) >> FIXED_Q_NUM) : (((n) >> FIXED_Q_NUM) + 1)) 48 #define FO_DECIMAL(n) ((n) >= 0 ? ((n) & (FIXED_NUM_1 - 1)) : ((n) | (-FIXED_NUM_1))) 49 #define FO_MUL(n1, n2) ((static_cast<int64_t>(n1) * (n2)) >> FIXED_Q_NUM) 50 51 struct EdgeSides { 52 int16_t left; 53 int16_t right; 54 int16_t top; 55 int16_t bottom; 56 }; 57 58 struct LabelLineInfo { 59 Point& pos; 60 Point& offset; 61 const Rect& mask; 62 int16_t lineHeight; 63 uint16_t lineLength; 64 uint8_t shapingId; 65 uint8_t opaScale; 66 const Style& style; 67 68 const char* text; 69 uint16_t length; 70 uint16_t start; 71 uint16_t fontId; 72 uint8_t fontSize; 73 uint8_t txtFlag; 74 UITextLanguageDirect direct; 75 uint32_t* codePoints; 76 bool baseLine; 77 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING 78 TextStyle* textStyles; 79 #endif 80 List<BackgroundColor>* backgroundColor; 81 List<ForegroundColor>* foregroundColor; 82 List<LineBackgroundColor>* linebackgroundColor; 83 SizeSpan* sizeSpans; 84 uint16_t ellipsisOssetY; 85 }; 86 87 struct LabelLetterInfo { 88 const Point& pos; 89 Rect mask; 90 const ColorType& color; 91 OpacityType opa; 92 int8_t offsetX; 93 int8_t offsetY; 94 95 const uint32_t& letter; 96 UITextLanguageDirect direct; 97 uint16_t fontId; 98 uint8_t shapingId; 99 uint8_t fontSize; 100 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING 101 TextStyle textStyle; 102 #endif 103 bool baseLine; 104 int16_t letterSpace_; 105 int16_t lineSpace_; 106 bool havebackgroundColor; 107 ColorType backgroundColor; 108 }; 109 110 struct TransformInitState { 111 #if defined(ENABLE_FIXED_POINT) && ENABLE_FIXED_POINT 112 // parameters below are Q15 fixed-point number 113 int64_t verticalU; 114 int64_t verticalV; 115 int64_t duHorizon; 116 int64_t dvHorizon; 117 int64_t duVertical; 118 int64_t dvVertical; 119 // parameters above are Q15 fixed-point number 120 #else 121 float verticalU; 122 float verticalV; 123 float duHorizon; 124 float dvHorizon; 125 float duVertical; 126 float dvVertical; 127 #endif 128 }; 129 130 struct TriangleEdge { TriangleEdgeTriangleEdge131 TriangleEdge() {} 132 TriangleEdge(int16_t x1, int16_t y1, int16_t duInt, int16_t dvInt); 133 ~TriangleEdge(); 134 #if defined(ENABLE_FIXED_POINT) && ENABLE_FIXED_POINT 135 // parameters below are Q15 fixed-point number 136 int64_t curX = 0; 137 int64_t curY = 0; 138 int64_t du = 0; 139 int64_t dv = 0; 140 // parameters above are Q15 fixed-point number 141 #else 142 float curX = 0.0f; 143 float curY = 0.0f; 144 float du = 0.0f; 145 float dv = 0.0f; 146 #endif 147 }; 148 149 struct TriangleTransformDataInfo { 150 const TransformDataInfo& info; 151 Point p1; 152 Point p2; 153 Point p3; 154 bool isRightPart; 155 bool ignoreJunctionPoint; 156 }; 157 158 struct TriangleScanInfo { 159 int16_t yMin; 160 int16_t yMax; 161 TriangleEdge& edge1; 162 TriangleEdge& edge2; 163 uint8_t* screenBuffer; 164 uint8_t bufferPxSize; 165 const ColorType& color; 166 const OpacityType opaScale; 167 TransformInitState& init; 168 uint16_t screenBufferWidth; 169 uint8_t pixelSize; 170 const int32_t srcLineWidth; 171 const TransformDataInfo& info; 172 const Rect& mask; 173 bool isRightPart; 174 bool ignoreJunctionPoint; 175 Matrix3<float> matrix; 176 }; 177 178 struct TrianglePartInfo { 179 const Rect& mask; 180 const TransformMap& transMap; 181 const Point& position; 182 TriangleEdge& edge1; 183 TriangleEdge& edge2; 184 int16_t yMin; 185 int16_t yMax; 186 const TransformDataInfo& info; 187 const ColorType& color; 188 const OpacityType opaScale; 189 bool isRightPart; 190 bool ignoreJunctionPoint; 191 }; 192 193 enum { 194 IMG_SRC_VARIABLE, 195 IMG_SRC_FILE, 196 IMG_SRC_UNKNOWN, 197 }; 198 199 class DrawUtils : public HeapBase { 200 public: 201 static DrawUtils* GetInstance(); 202 203 void DrawColorArea(BufferInfo& gfxDstBuffer, const Rect& area, const Rect& mask, 204 const ColorType& color, OpacityType opa) const; 205 206 void DrawColorAreaBySides(BufferInfo& gfxDstBuffer, const Rect& mask, const ColorType& color, 207 OpacityType opa, const EdgeSides& sides) const; 208 209 void DrawPixel(BufferInfo& gfxDstBuffer, int16_t x, int16_t y, const Rect& mask, 210 const ColorType& color, OpacityType opa) const; 211 212 void DrawColorLetter(BufferInfo& gfxDstBuffer, 213 const LabelLetterInfo& letterInfo, 214 uint8_t* fontMap, 215 GlyphNode node, 216 uint8_t maxLetterSize) const; 217 void DrawNormalLetter(BufferInfo& gfxDstBuffer, 218 const LabelLetterInfo& letterInfo, 219 uint8_t* fontMap, 220 GlyphNode node, 221 uint8_t maxLetterSize) const; 222 223 void DrawLetter(BufferInfo& gfxDstBuffer, 224 const uint8_t* fontMap, 225 const Rect& fontRect, 226 const Rect& subRect, 227 const uint8_t fontWeight, 228 const ColorType& color, 229 const OpacityType opa) const; 230 231 void DrawImage(BufferInfo& gfxDstBuffer, const Rect& area, const Rect& mask, 232 const uint8_t* image, OpacityType opa, uint8_t pxBitSize, ColorMode colorMode) const; 233 234 static void 235 GetXAxisErrForJunctionLine(bool ignoreJunctionPoint, bool isRightPart, int16_t& xMinErr, int16_t& xMaxErr); 236 237 static void GetTransformInitState(const TransformMap& transMap, 238 const Point& position, 239 const Rect& trans, 240 TransformInitState& init); 241 242 static void DrawTriangleTransform(BufferInfo& gfxDstBuffer, 243 const Rect& mask, 244 const Point& position, 245 const ColorType& color, 246 OpacityType opaScale, 247 const TransformMap& transMap, 248 const TriangleTransformDataInfo& dataInfo); 249 250 void DrawTransform(BufferInfo& gfxDstBuffer, 251 const Rect& mask, 252 const Point& position, 253 const ColorType& color, 254 OpacityType opaScale, 255 const TransformMap& transMap, 256 const TransformDataInfo& dataInfo) const; 257 258 void DrawTranspantArea(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask); 259 260 void DrawWithBuffer(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask, const ColorType* colorBuf); 261 262 static uint8_t GetPxSizeByColorMode(uint8_t colorMode); 263 264 static uint8_t GetByteSizeByColorMode(uint8_t colorMode); 265 GetMixOpacity(OpacityType opa1,OpacityType opa2)266 static OpacityType GetMixOpacity(OpacityType opa1, OpacityType opa2) 267 { 268 // 8: Shift right 8 bits 269 OpacityType opaMix = (opa1 == OPA_OPAQUE) ? opa2 : ((static_cast<uint16_t>(opa1) * opa2) >> 8); 270 return opaMix; 271 } 272 273 void DrawAdjPixelInLine(BufferInfo& gfxDstBuffer, 274 int16_t x1, 275 int16_t y1, 276 int16_t x2, 277 int16_t y2, 278 const Rect& mask, 279 const ColorType& color, 280 OpacityType opa, 281 uint16_t w) const; 282 283 void DrawPixelInLine(BufferInfo& gfxDstBuffer, int16_t x, int16_t y, const Rect& mask, 284 const ColorType& color, OpacityType opa, uint16_t w) const; 285 286 void DrawVerPixelInLine(BufferInfo& gfxDstBuffer, 287 int16_t x, 288 int16_t y, 289 int8_t dir, 290 const Rect& mask, 291 const ColorType& color, 292 OpacityType opa, 293 uint16_t weight) const; 294 295 void DrawHorPixelInLine(BufferInfo& gfxDstBuffer, 296 int16_t x, 297 int16_t y, 298 int8_t dir, 299 const Rect& mask, 300 const ColorType& color, 301 OpacityType opa, 302 uint16_t weight) const; 303 304 void BlendWithSoftWare(const uint8_t* src1, 305 const Rect& srcRect, 306 uint32_t srcStride, 307 uint32_t srcLineNumber, 308 ColorMode srcMode, 309 uint32_t color, 310 OpacityType opa, 311 uint8_t* dst, 312 uint32_t destStride, 313 ColorMode destMode, 314 uint32_t x, 315 uint32_t y) const; 316 317 void FillAreaWithSoftWare(BufferInfo& gfxDstBuffer, 318 const Rect& fillArea, 319 const ColorType& color, 320 const OpacityType& opa) const; 321 #ifdef ARM_NEON_OPT 322 323 void BlendLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, 324 uint8_t alpha, uint8_t cover); 325 void BlendLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, 326 uint8_t alpha); 327 void BlendLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t srcCover); 328 void BlendLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t* srcCovers); 329 void BlendLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha, uint8_t* covers); 330 void BlendPreLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, 331 uint8_t alpha, uint8_t cover); 332 void BlendPreLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, 333 uint8_t alpha); 334 void BlendPreLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t srcCover); 335 void BlendPreLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t* srcCovers); 336 void BlendPreLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha, uint8_t* covers); 337 #endif 338 private: 339 using DrawTriangleTransformFuc = void (*)(const TriangleScanInfo& triangle, const ColorMode bufferMode); 340 341 static void DrawTriangleTrueColorNearest(const TriangleScanInfo& triangle, const ColorMode bufferMode); 342 343 static void DrawTriangleAlphaBilinear(const TriangleScanInfo& triangle, const ColorMode bufferMode); 344 345 static void DrawTriangleTrueColorBilinear565(const TriangleScanInfo& triangle, const ColorMode bufferMode); 346 347 static void DrawTriangleTrueColorBilinear888(const TriangleScanInfo& triangle, const ColorMode bufferMode); 348 349 static void Draw3DTriangleTrueColorBilinear8888(const TriangleScanInfo& triangle, const ColorMode bufferMode); 350 351 static void DrawTriangleTrueColorBilinear8888(const TriangleScanInfo& triangle, const ColorMode bufferMode); 352 353 inline static void StepToNextLine(TriangleEdge& edg1, TriangleEdge& edg2); 354 355 static void DrawTriangleTransformPart(BufferInfo& gfxDstBuffer, const TrianglePartInfo& part); 356 357 static OpacityType GetPxAlphaForAlphaImg(const TransformDataInfo& dataInfo, const Point& point); 358 359 static void AddBorderToImageData(TransformDataInfo& newDataInfo, ImageInfo& imageinfo); 360 361 static void UpdateTransMap(int16_t width, int16_t height, TransformMap& transMap); 362 363 void FillArea(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask, 364 bool isTransparent, const ColorType* colorBuf); 365 }; 366 } // namespace OHOS 367 #endif // GRAPHIC_LITE_DRAW_UTILS_H 368