draw_utils.h 12.7 KB
Newer Older
M
mamingshuai 已提交
1
/*
X
xucheng57@huawei.com 已提交
2
 * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
M
mamingshuai 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GRAPHIC_LITE_DRAW_UTILS_H
#define GRAPHIC_LITE_DRAW_UTILS_H

#include "common/text.h"
X
xucheng57@huawei.com 已提交
20
#include "font/ui_font_header.h"
21
#include "gfx_utils/color.h"
Z
zhangguyuan 已提交
22
#include "gfx_utils/geometry2d.h"
N
niulihua 已提交
23
#include "gfx_utils/graphic_buffer.h"
Z
zhangguyuan 已提交
24
#include "gfx_utils/graphic_types.h"
X
xucheng57@huawei.com 已提交
25
#include "gfx_utils/list.h"
Z
zhangguyuan 已提交
26 27
#include "gfx_utils/style.h"
#include "gfx_utils/transform.h"
M
mamingshuai 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40

namespace OHOS {
#define SWAP_INT16(x, y)    \
    do {                    \
        int16_t temp = (x); \
        (x) = (y);          \
        (y) = temp;         \
    } while (0)

#define SWAP_POINTS(x1, x2, y1, y2) \
    SWAP_INT16(x1, x2);             \
    SWAP_INT16(y1, y2);

L
liqiang 已提交
41
// FixedPointed Related definition.
L
liqiang 已提交
42 43
#define FIXED_NUM_1 1048576
#define FIXED_Q_NUM 20
L
liqiang 已提交
44
#define FO_TRANS_FLOAT_TO_FIXED(f) (static_cast<int64_t>((f) * FIXED_NUM_1))
L
liqiang 已提交
45 46 47 48 49
#define FO_TRANS_INTEGER_TO_FIXED(f) ((static_cast<int64_t>(f)) << FIXED_Q_NUM)
#define FO_DIV(n1, n2) ((static_cast<int64_t>(n1) << FIXED_Q_NUM) / (n2))
#define FO_TO_INTEGER(n) ((n) >= 0 ? ((n) >> FIXED_Q_NUM) : (((n) >> FIXED_Q_NUM) + 1))
#define FO_DECIMAL(n) ((n) >= 0 ? ((n) & (FIXED_NUM_1 - 1)) : ((n) | (-FIXED_NUM_1)))
#define FO_MUL(n1, n2) ((static_cast<int64_t>(n1) * (n2)) >> FIXED_Q_NUM)
L
liqiang 已提交
50

M
mamingshuai 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
struct EdgeSides {
    int16_t left;
    int16_t right;
    int16_t top;
    int16_t bottom;
};

struct LabelLineInfo {
    Point& pos;
    Point& offset;
    const Rect& mask;
    int16_t lineHeight;
    uint16_t lineLength;
    uint8_t shapingId;
    uint8_t opaScale;
    const Style& style;

    const char* text;
    uint16_t length;
    uint16_t start;
71
    uint16_t fontId;
M
mamingshuai 已提交
72 73 74 75
    uint8_t fontSize;
    uint8_t txtFlag;
    UITextLanguageDirect direct;
    uint32_t* codePoints;
76
    bool baseLine;
L
Lizhiqi 已提交
77
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
78 79 80 81 82 83 84
    TextStyle* textStyles;
#endif
    List<BackgroundColor>*  backgroundColor;
    List<ForegroundColor>*  foregroundColor;
    List<LineBackgroundColor>*  linebackgroundColor;
    SizeSpan* sizeSpans;
    uint16_t ellipsisOssetY;
M
mamingshuai 已提交
85 86 87 88 89 90 91 92 93 94 95 96
};

struct LabelLetterInfo {
    const Point& pos;
    Rect mask;
    const ColorType& color;
    OpacityType opa;
    int8_t offsetX;
    int8_t offsetY;

    const uint32_t& letter;
    UITextLanguageDirect direct;
97
    uint16_t fontId;
M
mamingshuai 已提交
98 99
    uint8_t shapingId;
    uint8_t fontSize;
L
Lizhiqi 已提交
100
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
101 102
    TextStyle textStyle;
#endif
103
    bool baseLine;
X
xucheng57@huawei.com 已提交
104 105 106 107
    int16_t letterSpace_;
    int16_t lineSpace_;
    bool havebackgroundColor;
    ColorType backgroundColor;
M
mamingshuai 已提交
108 109 110
};

struct TransformInitState {
L
liqiang 已提交
111
#if ENABLE_FIXED_POINT
L
liqiang 已提交
112
    // parameters below are Q15 fixed-point number
L
liqiang 已提交
113 114 115 116 117 118
    int64_t verticalU;
    int64_t verticalV;
    int64_t duHorizon;
    int64_t dvHorizon;
    int64_t duVertical;
    int64_t dvVertical;
L
liqiang 已提交
119
    // parameters above are Q15 fixed-point number
L
liqiang 已提交
120 121 122 123 124 125 126 127
#else
    float verticalU;
    float verticalV;
    float duHorizon;
    float dvHorizon;
    float duVertical;
    float dvVertical;
#endif
M
mamingshuai 已提交
128 129 130 131 132 133
};

struct TriangleEdge {
    TriangleEdge() {}
    TriangleEdge(int16_t x1, int16_t y1, int16_t duInt, int16_t dvInt);
    ~TriangleEdge();
L
liqiang 已提交
134
#if ENABLE_FIXED_POINT
L
liqiang 已提交
135
    // parameters below are Q15 fixed-point number
L
liqiang 已提交
136 137 138 139
    int64_t curX = 0;
    int64_t curY = 0;
    int64_t du = 0;
    int64_t dv = 0;
L
liqiang 已提交
140
    // parameters above are Q15 fixed-point number
L
liqiang 已提交
141 142 143 144 145 146
#else
    float curX = 0.0f;
    float curY = 0.0f;
    float du = 0.0f;
    float dv = 0.0f;
#endif
M
mamingshuai 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
};

struct TriangleTransformDataInfo {
    const TransformDataInfo& info;
    Point p1;
    Point p2;
    Point p3;
    bool isRightPart;
    bool ignoreJunctionPoint;
};

struct TriangleScanInfo {
    int16_t yMin;
    int16_t yMax;
    TriangleEdge& edge1;
    TriangleEdge& edge2;
    uint8_t* screenBuffer;
    uint8_t bufferPxSize;
    const ColorType& color;
    const OpacityType opaScale;
    TransformInitState& init;
    uint16_t screenBufferWidth;
    uint8_t pixelSize;
    const int32_t srcLineWidth;
    const TransformDataInfo& info;
    const Rect& mask;
    bool isRightPart;
    bool ignoreJunctionPoint;
L
liqiang 已提交
175
    Matrix3<float> matrix;
M
mamingshuai 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
};

struct TrianglePartInfo {
    const Rect& mask;
    const TransformMap& transMap;
    const Point& position;
    TriangleEdge& edge1;
    TriangleEdge& edge2;
    int16_t yMin;
    int16_t yMax;
    const TransformDataInfo& info;
    const ColorType& color;
    const OpacityType opaScale;
    bool isRightPart;
    bool ignoreJunctionPoint;
};

enum {
    IMG_SRC_VARIABLE,
    IMG_SRC_FILE,
    IMG_SRC_UNKNOWN,
};

class DrawUtils : public HeapBase {
public:
Z
zhdengc 已提交
201
    static DrawUtils* GetInstance();
M
mamingshuai 已提交
202

N
niulihua 已提交
203 204
    void DrawColorArea(BufferInfo& gfxDstBuffer, const Rect& area, const Rect& mask,
                       const ColorType& color, OpacityType opa) const;
M
mamingshuai 已提交
205

N
niulihua 已提交
206 207
    void DrawColorAreaBySides(BufferInfo& gfxDstBuffer, const Rect& mask, const ColorType& color,
                              OpacityType opa, const EdgeSides& sides) const;
M
mamingshuai 已提交
208

N
niulihua 已提交
209 210
    void DrawPixel(BufferInfo& gfxDstBuffer, int16_t x, int16_t y, const Rect& mask,
                   const ColorType& color, OpacityType opa) const;
M
mamingshuai 已提交
211

212 213 214 215 216 217 218 219 220
    void DrawColorLetter(BufferInfo& gfxDstBuffer,
                         const LabelLetterInfo& letterInfo,
                         uint8_t* fontMap,
                         GlyphNode node) const;
    void DrawNormalLetter(BufferInfo& gfxDstBuffer,
                          const LabelLetterInfo& letterInfo,
                          uint8_t* fontMap,
                          GlyphNode node,
                          uint8_t maxLetterSize) const;
M
mamingshuai 已提交
221

222 223 224 225 226 227 228 229
    void DrawLetter(BufferInfo& gfxDstBuffer,
                    const uint8_t* fontMap,
                    const Rect& fontRect,
                    const Rect& subRect,
                    const uint8_t fontWeight,
                    const ColorType& color,
                    const OpacityType opa) const;

N
niulihua 已提交
230
    void DrawImage(BufferInfo& gfxDstBuffer, const Rect& area, const Rect& mask,
W
wangtiantian 已提交
231
                   const uint8_t* image, OpacityType opa, uint8_t pxBitSize, ColorMode colorMode) const;
M
mamingshuai 已提交
232 233

    static void
L
liqiang 已提交
234
        GetXAxisErrForJunctionLine(bool ignoreJunctionPoint, bool isRightPart, int16_t& xMinErr, int16_t& xMaxErr);
M
mamingshuai 已提交
235 236 237 238 239 240

    static void GetTransformInitState(const TransformMap& transMap,
                                      const Point& position,
                                      const Rect& trans,
                                      TransformInitState& init);

N
niulihua 已提交
241 242
    static void DrawTriangleTransform(BufferInfo& gfxDstBuffer,
                                      const Rect& mask,
M
mamingshuai 已提交
243 244 245 246 247 248
                                      const Point& position,
                                      const ColorType& color,
                                      OpacityType opaScale,
                                      const TransformMap& transMap,
                                      const TriangleTransformDataInfo& dataInfo);

N
niulihua 已提交
249 250
    void DrawTransform(BufferInfo& gfxDstBuffer,
                       const Rect& mask,
M
mamingshuai 已提交
251 252 253 254 255 256
                       const Point& position,
                       const ColorType& color,
                       OpacityType opaScale,
                       const TransformMap& transMap,
                       const TransformDataInfo& dataInfo) const;

N
niulihua 已提交
257
    void DrawTranspantArea(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask);
M
mamingshuai 已提交
258

N
niulihua 已提交
259
    void DrawWithBuffer(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask, const ColorType* colorBuf);
M
mamingshuai 已提交
260 261 262 263 264 265 266 267 268 269 270 271

    static uint8_t GetPxSizeByColorMode(uint8_t colorMode);

    static uint8_t GetByteSizeByColorMode(uint8_t colorMode);

    static OpacityType GetMixOpacity(OpacityType opa1, OpacityType opa2)
    {
        // 8: Shift right 8 bits
        OpacityType opaMix = (opa1 == OPA_OPAQUE) ? opa2 : ((static_cast<uint16_t>(opa1) * opa2) >> 8);
        return opaMix;
    }

N
niulihua 已提交
272 273
    void DrawAdjPixelInLine(BufferInfo& gfxDstBuffer,
                            int16_t x1,
M
mamingshuai 已提交
274 275 276 277 278 279 280 281
                            int16_t y1,
                            int16_t x2,
                            int16_t y2,
                            const Rect& mask,
                            const ColorType& color,
                            OpacityType opa,
                            uint16_t w) const;

N
niulihua 已提交
282 283
    void DrawPixelInLine(BufferInfo& gfxDstBuffer, int16_t x, int16_t y, const Rect& mask,
                         const ColorType& color, OpacityType opa, uint16_t w) const;
M
mamingshuai 已提交
284

N
niulihua 已提交
285 286
    void DrawVerPixelInLine(BufferInfo& gfxDstBuffer,
                            int16_t x,
M
mamingshuai 已提交
287 288 289 290 291 292 293
                            int16_t y,
                            int8_t dir,
                            const Rect& mask,
                            const ColorType& color,
                            OpacityType opa,
                            uint16_t weight) const;

N
niulihua 已提交
294 295
    void DrawHorPixelInLine(BufferInfo& gfxDstBuffer,
                            int16_t x,
M
mamingshuai 已提交
296 297 298 299 300 301 302
                            int16_t y,
                            int8_t dir,
                            const Rect& mask,
                            const ColorType& color,
                            OpacityType opa,
                            uint16_t weight) const;

N
niulihua 已提交
303 304
    void BlendWithSoftWare(const uint8_t* src1,
                           const Rect& srcRect,
M
mamingshuai 已提交
305
                           uint32_t srcStride,
N
niulihua 已提交
306
                           uint32_t srcLineNumber,
M
mamingshuai 已提交
307
                           ColorMode srcMode,
N
niulihua 已提交
308 309 310
                           uint32_t color,
                           OpacityType opa,
                           uint8_t* dst,
M
mamingshuai 已提交
311 312
                           uint32_t destStride,
                           ColorMode destMode,
N
niulihua 已提交
313 314
                           uint32_t x,
                           uint32_t y) const;
M
mamingshuai 已提交
315

N
niulihua 已提交
316 317 318 319
    void FillAreaWithSoftWare(BufferInfo& gfxDstBuffer,
                              const Rect& fillArea,
                              const ColorType& color,
                              const OpacityType& opa) const;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#ifdef ARM_NEON_OPT

    void BlendLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue,
                      uint8_t alpha, uint8_t cover);
    void BlendLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue,
                      uint8_t alpha);
    void BlendLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t srcCover);
    void BlendLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t* srcCovers);
    void BlendLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha, uint8_t* covers);
    void BlendPreLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue,
                         uint8_t alpha, uint8_t cover);
    void BlendPreLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue,
                         uint8_t alpha);
    void BlendPreLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t srcCover);
    void BlendPreLerpPix(uint8_t* dstColors, uint8_t* srcColors, uint8_t* srcCovers);
    void BlendPreLerpPix(uint8_t* color, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha, uint8_t* covers);
#endif
N
niulihua 已提交
337 338
private:
    using DrawTriangleTransformFuc = void (*)(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
339

N
niulihua 已提交
340
    static void DrawTriangleTrueColorNearest(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
341

N
niulihua 已提交
342
    static void DrawTriangleAlphaBilinear(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
343

N
niulihua 已提交
344
    static void DrawTriangleTrueColorBilinear565(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
345

N
niulihua 已提交
346
    static void DrawTriangleTrueColorBilinear888(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
347

L
liqiang 已提交
348 349
    static void Draw3DTriangleTrueColorBilinear8888(const TriangleScanInfo& triangle, const ColorMode bufferMode);

N
niulihua 已提交
350
    static void DrawTriangleTrueColorBilinear8888(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
351 352 353

    inline static void StepToNextLine(TriangleEdge& edg1, TriangleEdge& edg2);

N
niulihua 已提交
354
    static void DrawTriangleTransformPart(BufferInfo& gfxDstBuffer, const TrianglePartInfo& part);
M
mamingshuai 已提交
355 356 357

    static OpacityType GetPxAlphaForAlphaImg(const TransformDataInfo& dataInfo, const Point& point);

358 359 360 361
    static void AddBorderToImageData(TransformDataInfo& newDataInfo);

    static void UpdateTransMap(int16_t width, int16_t height, TransformMap& transMap);

N
niulihua 已提交
362 363
    void FillArea(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask,
                  bool isTransparent, const ColorType* colorBuf);
M
mamingshuai 已提交
364 365 366
};
} // namespace OHOS
#endif // GRAPHIC_LITE_DRAW_UTILS_H