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