draw_utils.h 9.9 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * 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

Z
zhangguyuan 已提交
19
#include "gfx_utils/color.h"
M
mamingshuai 已提交
20
#include "common/text.h"
Z
zhangguyuan 已提交
21
#include "gfx_utils/geometry2d.h"
N
niulihua 已提交
22
#include "gfx_utils/graphic_buffer.h"
Z
zhangguyuan 已提交
23 24 25
#include "gfx_utils/graphic_types.h"
#include "gfx_utils/style.h"
#include "gfx_utils/transform.h"
M
mamingshuai 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38

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 已提交
39 40 41 42 43 44 45 46 47
// FixedPointed Related definition.
#define FIXED_NUM_1 32768
#define FO_TRANS_FLOAT_TO_FIXED(f) (static_cast<int32_t>((f) * FIXED_NUM_1))
#define FO_TRANS_INTEGER_TO_FIXED(f) ((static_cast<int32_t>(f)) << 15)
#define FO_DIV(n1, n2) ((static_cast<int64_t>(n1) << 15) / (n2))
#define FO_TO_INTEGER(n) ((n) >= 0 ? ((n) >> 15) : (((n) >> 15) + 1))
#define FO_DECIMAL(n) ((n) >= 0 ? ((n) & 32767) : ((n) | (-32768)))
#define FO_MUL(n1, n2) ((static_cast<int64_t>(n1) * (n2)) >> 15)

M
mamingshuai 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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;
    uint8_t fontId;
    uint8_t fontSize;
    uint8_t txtFlag;
    UITextLanguageDirect direct;
    uint32_t* codePoints;
};

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

    const uint32_t& letter;
    UITextLanguageDirect direct;
    uint8_t fontId;
    uint8_t shapingId;
    uint8_t fontSize;
};

struct TransformInitState {
L
liqiang 已提交
91 92 93 94 95 96 97 98
    // parameters below are Q15 fixed-point number
    int32_t verticalU;
    int32_t verticalV;
    int32_t duHorizon;
    int32_t dvHorizon;
    int32_t duVertical;
    int32_t dvVertical;
    // parameters above are Q15 fixed-point number
M
mamingshuai 已提交
99 100 101 102 103 104
};

struct TriangleEdge {
    TriangleEdge() {}
    TriangleEdge(int16_t x1, int16_t y1, int16_t duInt, int16_t dvInt);
    ~TriangleEdge();
L
liqiang 已提交
105 106 107 108 109 110
    // parameters below are Q15 fixed-point number
    int32_t curX = 0;
    int32_t curY = 0;
    int32_t du = 0;
    int32_t dv = 0;
    // parameters above are Q15 fixed-point number
M
mamingshuai 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
};

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;
};

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:
    static DrawUtils* GetInstance()
    {
        static DrawUtils instance;
        return &instance;
    }

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

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

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

N
niulihua 已提交
179
    void DrawLetter(BufferInfo& gfxDstBuffer, const LabelLetterInfo& letterInfo) const;
M
mamingshuai 已提交
180

N
niulihua 已提交
181 182 183
    void DrawImage(BufferInfo& gfxDstBuffer, const Rect& area, const Rect& mask,
                   const uint8_t* image, OpacityType opa, uint8_t pxBitSize,
                   ColorMode colorMode, LutColorMode lutColorMode = LUT_UNKNOW) const;
M
mamingshuai 已提交
184 185

    static void
L
liqiang 已提交
186
        GetXAxisErrForJunctionLine(bool ignoreJunctionPoint, bool isRightPart, int16_t& xMinErr, int16_t& xMaxErr);
M
mamingshuai 已提交
187 188 189 190 191 192

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

N
niulihua 已提交
193 194
    static void DrawTriangleTransform(BufferInfo& gfxDstBuffer,
                                      const Rect& mask,
M
mamingshuai 已提交
195 196 197 198 199 200
                                      const Point& position,
                                      const ColorType& color,
                                      OpacityType opaScale,
                                      const TransformMap& transMap,
                                      const TriangleTransformDataInfo& dataInfo);

N
niulihua 已提交
201 202
    void DrawTransform(BufferInfo& gfxDstBuffer,
                       const Rect& mask,
M
mamingshuai 已提交
203 204 205 206 207 208
                       const Point& position,
                       const ColorType& color,
                       OpacityType opaScale,
                       const TransformMap& transMap,
                       const TransformDataInfo& dataInfo) const;

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

N
niulihua 已提交
211
    void DrawWithBuffer(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask, const ColorType* colorBuf);
M
mamingshuai 已提交
212 213 214 215 216

    static uint8_t GetPxSizeByColorMode(uint8_t colorMode);

    static uint8_t GetByteSizeByColorMode(uint8_t colorMode);

W
wanngtiantian 已提交
217
    static LutColorMode GetLutColorModeBySize(uint8_t size);
M
mamingshuai 已提交
218 219 220 221 222 223 224 225

    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 已提交
226 227
    void DrawAdjPixelInLine(BufferInfo& gfxDstBuffer,
                            int16_t x1,
M
mamingshuai 已提交
228 229 230 231 232 233 234 235
                            int16_t y1,
                            int16_t x2,
                            int16_t y2,
                            const Rect& mask,
                            const ColorType& color,
                            OpacityType opa,
                            uint16_t w) const;

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

N
niulihua 已提交
239 240
    void DrawVerPixelInLine(BufferInfo& gfxDstBuffer,
                            int16_t x,
M
mamingshuai 已提交
241 242 243 244 245 246 247
                            int16_t y,
                            int8_t dir,
                            const Rect& mask,
                            const ColorType& color,
                            OpacityType opa,
                            uint16_t weight) const;

N
niulihua 已提交
248 249
    void DrawHorPixelInLine(BufferInfo& gfxDstBuffer,
                            int16_t x,
M
mamingshuai 已提交
250 251 252 253 254 255 256
                            int16_t y,
                            int8_t dir,
                            const Rect& mask,
                            const ColorType& color,
                            OpacityType opa,
                            uint16_t weight) const;

N
niulihua 已提交
257 258
    void BlendWithSoftWare(const uint8_t* src1,
                           const Rect& srcRect,
M
mamingshuai 已提交
259
                           uint32_t srcStride,
N
niulihua 已提交
260
                           uint32_t srcLineNumber,
M
mamingshuai 已提交
261
                           ColorMode srcMode,
N
niulihua 已提交
262 263 264
                           uint32_t color,
                           OpacityType opa,
                           uint8_t* dst,
M
mamingshuai 已提交
265 266
                           uint32_t destStride,
                           ColorMode destMode,
N
niulihua 已提交
267 268 269 270 271 272 273 274 275
                           uint32_t x,
                           uint32_t y) const;

    void FillAreaWithSoftWare(BufferInfo& gfxDstBuffer,
                              const Rect& fillArea,
                              const ColorType& color,
                              const OpacityType& opa) const;
private:
    using DrawTriangleTransformFuc = void (*)(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
276

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

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

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

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

N
niulihua 已提交
285
    static void DrawTriangleTrueColorBilinear8888(const TriangleScanInfo& triangle, const ColorMode bufferMode);
M
mamingshuai 已提交
286 287 288

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

N
niulihua 已提交
289
    static void DrawTriangleTransformPart(BufferInfo& gfxDstBuffer, const TrianglePartInfo& part);
M
mamingshuai 已提交
290 291 292

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

N
niulihua 已提交
293 294
    void FillArea(BufferInfo& gfxDstBuffer, const Rect& rect, const Rect& mask,
                  bool isTransparent, const ColorType* colorBuf);
M
mamingshuai 已提交
295 296 297
};
} // namespace OHOS
#endif // GRAPHIC_LITE_DRAW_UTILS_H