ui_canvas.cpp 59.3 KB
Newer Older
M
mamingshuai 已提交
1
/*
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
 * 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.
 */

#include "components/ui_canvas.h"
17 18

#include "draw/clip_utils.h"
M
mamingshuai 已提交
19 20
#include "draw/draw_arc.h"
#include "draw/draw_image.h"
N
niulihua 已提交
21
#include "gfx_utils/graphic_log.h"
22 23 24
#include "render/render_buffer.h"
#include "render/render_pixfmt_rgba_blend.h"
#include "render/render_scanline.h"
25 26 27
#if ( (defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND) )
    #include "draw/draw_canvas.h"
#endif
28

M
mamingshuai 已提交
29
namespace OHOS {
30 31 32 33 34 35
UICanvas::UICanvasPath::~UICanvasPath()
{
    points_.Clear();
    cmd_.Clear();
    arcParam_.Clear();
}
36 37

BufferInfo* UICanvas::gfxMapBuffer_ = nullptr;
38

39
void RenderSolid(const Paint& paint,
40 41
                 RasterizerScanlineAntialias& rasterizer,
                 RenderBase& renBase,
42
                 const bool& isStroke);
M
mamingshuai 已提交
43 44 45

void UICanvas::BeginPath()
{
46
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
M
mamingshuai 已提交
47
    /* If the previous path is not added to the drawing linked list, it should be destroyed directly. */
48 49 50
    if (vertices_ != nullptr && vertices_->GetTotalVertices() == 0) {
        delete vertices_;
        vertices_ = nullptr;
M
mamingshuai 已提交
51 52
    }

53 54 55
    vertices_ = new UICanvasVertices();
    if (vertices_ == nullptr) {
        GRAPHIC_LOGE("new UICanvasVertices fail");
M
mamingshuai 已提交
56 57
        return;
    }
58 59 60 61 62 63 64 65 66 67 68 69
#else
    if (path_ != nullptr && path_->strokeCount_ == 0) {
        delete path_;
        path_ = nullptr;
    }

    path_ = new UICanvasPath();
    if (path_ == nullptr) {
        GRAPHIC_LOGE("new UICanvasPath fail");
        return;
    }
#endif
M
mamingshuai 已提交
70 71 72 73
}

void UICanvas::MoveTo(const Point& point)
{
74
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
75
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
76 77
        return;
    }
78
    vertices_->MoveTo(point.x, point.y);
79 80 81 82 83 84 85 86 87 88 89 90 91 92
#else
    if (path_ == nullptr) {
        return;
    }

    path_->startPos_ = point;
    /* If the previous command is also CMD_MOVE_TO, the previous command is overwritten. */
    if ((path_->cmd_.Size() != 0) && (path_->cmd_.Tail()->data_ == CMD_MOVE_TO)) {
        path_->points_.Tail()->data_ = point;
        return;
    }
    path_->points_.PushBack(point);
    path_->cmd_.PushBack(CMD_MOVE_TO);
#endif
M
mamingshuai 已提交
93 94 95 96
}

void UICanvas::LineTo(const Point& point)
{
97
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
98
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
99 100
        return;
    }
101
    vertices_->LineTo(point.x, point.y);
102 103 104 105 106 107 108 109 110 111 112 113 114
#else
    if (path_ == nullptr) {
        return;
    }

    path_->points_.PushBack(point);
    if (path_->cmd_.Size() == 0) {
        path_->startPos_ = point;
        path_->cmd_.PushBack(CMD_MOVE_TO);
    } else {
        path_->cmd_.PushBack(CMD_LINE_TO);
    }
#endif
M
mamingshuai 已提交
115 116 117 118
}

void UICanvas::ArcTo(const Point& center, uint16_t radius, int16_t startAngle, int16_t endAngle)
{
119
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
120
    if (vertices_ == nullptr || startAngle == endAngle) {
M
mamingshuai 已提交
121 122 123 124
        return;
    }
    float sinma = radius * Sin(startAngle);
    float cosma = radius * Sin(QUARTER_IN_DEGREE - startAngle);
125 126
    if (vertices_->GetTotalVertices() != 0) {
        vertices_->LineTo(float(center.x + sinma), float(center.y - cosma));
M
mamingshuai 已提交
127
    } else {
128
        vertices_->MoveTo(float(center.x + sinma), float(center.y - cosma));
M
mamingshuai 已提交
129 130 131 132 133
    }
    if (MATH_ABS(startAngle - endAngle) < CIRCLE_IN_DEGREE) {
        sinma = radius * Sin(endAngle);
        cosma = radius * Sin(QUARTER_IN_DEGREE - endAngle);
    }
134 135 136 137
    int16_t angle = endAngle - startAngle;
    bool largeArcFlag = false;
    if (angle > SEMICIRCLE_IN_DEGREE || angle <= 0) {
        largeArcFlag = true;
M
mamingshuai 已提交
138
    }
139
    vertices_->ArcTo(radius, radius, angle, largeArcFlag, 1, float(center.x + sinma), float(center.y - cosma));
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#else
    if (path_ == nullptr) {
        return;
    }

    /*
     * If there is no command before CMD_ARC, only the arc is drawn. If there is a command in front of
     * CMD_ARC, the start point of arc must be connected to the end point of the path.
     */
    float sinma = radius * Sin(startAngle);
    float cosma = radius * Sin(QUARTER_IN_DEGREE - startAngle);
    if (path_->cmd_.Size() != 0) {
        path_->points_.PushBack({MATH_ROUND(center.x + sinma), MATH_ROUND(center.y - cosma)});
        path_->cmd_.PushBack(CMD_LINE_TO);
    } else {
        path_->startPos_ = {MATH_ROUND(center.x + sinma), MATH_ROUND(center.y - cosma)};
    }

    /* If the ARC scan range exceeds 360 degrees, the end point of the path is the position of the start angle. */
    if (MATH_ABS(startAngle - endAngle) < CIRCLE_IN_DEGREE) {
        sinma = radius * Sin(endAngle);
        cosma = radius * Sin(QUARTER_IN_DEGREE - endAngle);
    }
    path_->points_.PushBack({MATH_ROUND(center.x + sinma), MATH_ROUND(center.y - cosma)});
    path_->cmd_.PushBack(CMD_ARC);

    int16_t start;
    int16_t end;
    if (startAngle > endAngle) {
        start = endAngle;
        end = startAngle;
    } else {
        start = startAngle;
        end = endAngle;
    }

    DrawArc::GetInstance()->GetDrawRange(start, end);
    ArcParam param;
    param.center = center;
    param.radius = radius;
    param.startAngle = start;
    param.endAngle = end;
    path_->arcParam_.PushBack(param);
#endif
M
mamingshuai 已提交
184 185 186 187
}

void UICanvas::AddRect(const Point& point, int16_t height, int16_t width)
{
188
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
189
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
190 191 192
        return;
    }

193 194 195 196 197 198 199 200 201 202 203 204
    int16_t right = static_cast<int16_t>(point.x + width);
    int16_t bottom = point.y + height;

    float fRight = static_cast<float>(width) + static_cast<float>(point.x);
    float fBottom = static_cast<float>(height) + static_cast<float>(point.y);
    const int16_t setup = 3;
    if (fRight > INT16_MAX) {
        right += setup;
    }
    if (fBottom > INT16_MAX) {
        bottom += setup;
    }
205

M
mamingshuai 已提交
206
    MoveTo(point);
207 208 209
    LineTo({right, point.y});
    LineTo({right, bottom});
    LineTo({point.x, bottom});
210 211 212 213 214 215 216 217 218 219 220
#else
    if (path_ == nullptr) {
        return;
    }

    MoveTo(point);
    LineTo({static_cast<int16_t>(point.x + width), point.y});
    LineTo({static_cast<int16_t>(point.x + width), static_cast<int16_t>(point.y + height)});
    LineTo({point.x, static_cast<int16_t>(point.y + height)});

#endif
M
mamingshuai 已提交
221 222 223 224 225
    ClosePath();
}

void UICanvas::ClosePath()
{
226
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
227
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
228 229
        return;
    }
230
    vertices_->ClosePolygon();
231 232 233 234 235 236 237 238
#else
    if ((path_ == nullptr) || (path_->cmd_.Size() == 0)) {
        return;
    }

    path_->points_.PushBack(path_->startPos_);
    path_->cmd_.PushBack(CMD_CLOSE);
#endif
M
mamingshuai 已提交
239 240 241 242
}

UICanvas::~UICanvas()
{
243 244 245 246
    if ((path_ != nullptr) && (path_->strokeCount_ == 0)) {
        delete path_;
        path_ = nullptr;
    }
M
mamingshuai 已提交
247 248 249 250 251 252 253 254
    void* param = nullptr;
    ListNode<DrawCmd>* curDraw = drawCmdList_.Begin();
    for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
        param = curDraw->data_.param;
        curDraw->data_.DeleteParam(param);
        curDraw->data_.param = nullptr;
    }
    drawCmdList_.Clear();
255 256 257 258
    if (vertices_ != nullptr) {
        delete vertices_;
        vertices_ = nullptr;
    }
259
    DestroyMapBufferInfo();
M
mamingshuai 已提交
260 261 262 263
}

void UICanvas::Clear()
{
264 265 266 267
    if ((path_ != nullptr) && (path_->strokeCount_ == 0)) {
        delete path_;
        path_ = nullptr;
    }
M
mamingshuai 已提交
268 269 270 271 272 273 274 275
    void* param = nullptr;
    ListNode<DrawCmd>* curDraw = drawCmdList_.Begin();
    for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
        param = curDraw->data_.param;
        curDraw->data_.DeleteParam(param);
        curDraw->data_.param = nullptr;
    }
    drawCmdList_.Clear();
276 277 278 279
    if (vertices_ != nullptr) {
        delete vertices_;
        vertices_ = nullptr;
    }
M
mamingshuai 已提交
280 281 282
    Invalidate();
}

283
void UICanvas::DeleteImageParam(void* param)
284 285 286 287 288 289 290 291 292 293
{
    ImageParam* imageParam = static_cast<ImageParam*>(param);
    if (imageParam->image != nullptr) {
        delete imageParam->image;
        imageParam->image = nullptr;
    }
    delete imageParam;
    imageParam = nullptr;
}

294
void UICanvas::DeletePathParam(void* param)
295 296
{
    PathParam* pathParam = static_cast<PathParam*>(param);
297
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
298 299 300 301 302 303 304
    if (pathParam->vertices != nullptr) {
        pathParam->vertices->FreeAll();
        pathParam->vertices = nullptr;
    }
    if (pathParam->imageParam != nullptr) {
        DeleteImageParam(pathParam->imageParam);
    }
305 306 307 308 309 310
#else
    pathParam->path->strokeCount_--;
    if (pathParam->path->strokeCount_ == 0) {
        delete pathParam->path;
    }
#endif
311 312 313 314
    delete pathParam;
    pathParam = nullptr;
}

M
mamingshuai 已提交
315 316 317 318 319 320 321 322 323
void UICanvas::DrawLine(const Point& endPoint, const Paint& paint)
{
    DrawLine(startPoint_, endPoint, paint);
}

void UICanvas::DrawLine(const Point& startPoint, const Point& endPoint, const Paint& paint)
{
    LineParam* lineParam = new LineParam;
    if (lineParam == nullptr) {
324
        GRAPHIC_LOGE("new LineParam fail");
M
mamingshuai 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        return;
    }
    lineParam->start = startPoint;
    lineParam->end = endPoint;

    DrawCmd cmd;
    cmd.paint = paint;
    cmd.param = lineParam;
    cmd.DeleteParam = DeleteLineParam;
    cmd.DrawGraphics = DoDrawLine;
    drawCmdList_.PushBack(cmd);

    Invalidate();
    SetStartPosition(endPoint);
}

void UICanvas::DrawCurve(const Point& control1, const Point& control2, const Point& endPoint, const Paint& paint)
{
    DrawCurve(startPoint_, control1, control2, endPoint, paint);
}

void UICanvas::DrawCurve(const Point& startPoint,
                         const Point& control1,
                         const Point& control2,
                         const Point& endPoint,
                         const Paint& paint)
{
    CurveParam* curveParam = new CurveParam;
    if (curveParam == nullptr) {
354
        GRAPHIC_LOGE("new CurveParam fail");
M
mamingshuai 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
        return;
    }
    curveParam->start = startPoint;
    curveParam->control1 = control1;
    curveParam->control2 = control2;
    curveParam->end = endPoint;

    DrawCmd cmd;
    cmd.paint = paint;
    if (paint.GetStrokeWidth() > MAX_CURVE_WIDTH) {
        cmd.paint.SetStrokeWidth(MAX_CURVE_WIDTH);
    }
    cmd.param = curveParam;
    cmd.DeleteParam = DeleteCurveParam;
    cmd.DrawGraphics = DoDrawCurve;
    drawCmdList_.PushBack(cmd);

    Invalidate();
    SetStartPosition(endPoint);
}

void UICanvas::DrawRect(const Point& startPoint, int16_t height, int16_t width, const Paint& paint)
{
378
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
379 380 381
    if (!paint.GetChangeFlag()) {
        if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
            DrawRectSetCmd(startPoint, height, width, paint, Paint::PaintStyle::STROKE_STYLE);
M
mamingshuai 已提交
382 383
        }

384 385 386 387 388 389 390 391 392 393 394 395 396
        if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
            DrawRectSetCmd(startPoint, height, width, paint, Paint::PaintStyle::FILL_STYLE);
        }
    } else {
        BeginPath();
        MoveTo(startPoint);
        LineTo({static_cast<int16_t>(startPoint.x + width), startPoint.y});
        LineTo({static_cast<int16_t>(startPoint.x + width), static_cast<int16_t>(startPoint.y + height)});
        LineTo({startPoint.x, static_cast<int16_t>(startPoint.y + height)});
        ClosePath();
        FillPath(paint);
        DrawPath(paint);
    }
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
#else
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
        RectParam* rectParam = new RectParam;
        if (rectParam == nullptr) {
            GRAPHIC_LOGE("new RectParam fail");
            return;
        }
        rectParam->start = startPoint;
        rectParam->height = height;
        rectParam->width = width;

        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = rectParam;
        cmd.DeleteParam = DeleteRectParam;
        cmd.DrawGraphics = DoDrawRect;
        drawCmdList_.PushBack(cmd);
    }

    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        RectParam* rectParam = new RectParam;
        if (rectParam == nullptr) {
            GRAPHIC_LOGE("new RectParam fail");
            return;
        }
        rectParam->start = startPoint;
        rectParam->height = height;
        rectParam->width = width;

        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = rectParam;
        cmd.DeleteParam = DeleteRectParam;
        cmd.DrawGraphics = DoFillRect;
        drawCmdList_.PushBack(cmd);
    }
#endif
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    Invalidate();
}

void UICanvas::DrawRectSetCmd(const Point& startPoint, int16_t height, int16_t width, const Paint& paint,
                              Paint::PaintStyle paintStyle)
{
    RectParam* rectParam = new RectParam;
    if (rectParam == nullptr) {
        GRAPHIC_LOGE("new RectParam fail");
        return;
    }
    rectParam->start = startPoint;
    rectParam->height = height;
    rectParam->width = width;

    DrawCmd cmd;
    cmd.paint = paint;
    cmd.param = rectParam;
    cmd.DeleteParam = DeleteRectParam;
    if (paintStyle == Paint::PaintStyle::STROKE_STYLE) {
M
mamingshuai 已提交
454
        cmd.DrawGraphics = DoDrawRect;
455 456
    } else if (paintStyle == Paint::PaintStyle::FILL_STYLE) {
        cmd.DrawGraphics = DoFillRect;
M
mamingshuai 已提交
457
    }
458 459
    drawCmdList_.PushBack(cmd);
}
M
mamingshuai 已提交
460

461
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
462 463 464
void UICanvas::StrokeRect(const Point& startPoint, int16_t height, int16_t width, const Paint& paint)
{
    if (!paint.GetChangeFlag()) {
M
mamingshuai 已提交
465 466
        RectParam* rectParam = new RectParam;
        if (rectParam == nullptr) {
467
            GRAPHIC_LOGE("new RectParam fail");
M
mamingshuai 已提交
468 469 470 471 472 473 474 475 476 477
            return;
        }
        rectParam->start = startPoint;
        rectParam->height = height;
        rectParam->width = width;

        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = rectParam;
        cmd.DeleteParam = DeleteRectParam;
478
        cmd.DrawGraphics = DoDrawRect;
M
mamingshuai 已提交
479
        drawCmdList_.PushBack(cmd);
480 481 482 483 484 485 486 487
    } else {
        BeginPath();
        MoveTo(startPoint);
        LineTo({static_cast<int16_t>(startPoint.x + width), startPoint.y});
        LineTo({static_cast<int16_t>(startPoint.x + width), static_cast<int16_t>(startPoint.y + height)});
        LineTo({startPoint.x, static_cast<int16_t>(startPoint.y + height)});
        ClosePath();
        DrawPath(paint);
M
mamingshuai 已提交
488
    }
489
    SetStartPosition(startPoint);
M
mamingshuai 已提交
490 491
}

492 493 494 495 496 497 498 499 500 501 502 503 504
void UICanvas::ClearRect(const Point& startPoint, int16_t height, int16_t width)
{
    Paint paint;
    paint.SetFillColor(this->style_->bgColor_);
    paint.SetStyle(Paint::FILL_STYLE);
    BeginPath();
    MoveTo(startPoint);
    LineTo({static_cast<int16_t>(startPoint.x + width), startPoint.y});
    LineTo({static_cast<int16_t>(startPoint.x + width), static_cast<int16_t>(startPoint.y + height)});
    LineTo({startPoint.x, static_cast<int16_t>(startPoint.y + height)});
    ClosePath();
    FillPath(paint);
}
505
#endif
506

M
mamingshuai 已提交
507 508
void UICanvas::DrawCircle(const Point& center, uint16_t radius, const Paint& paint)
{
509
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
510
    if (paint.GetChangeFlag()) {
Y
youbing 已提交
511
#if defined(GRAPHIC_ENABLE_BEZIER_ARC_FLAG) && GRAPHIC_ENABLE_BEZIER_ARC_FLAG
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        if (vertices_ == nullptr) {
            vertices_ = new UICanvasVertices();
        }
        vertices_->RemoveAll();
        BezierArc arc(center.x, center.y, radius, radius, 0, TWO_TIMES * PI);
        vertices_->ConcatPath(arc, 0);
        vertices_->ClosePolygon();
        if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
            DrawPath(paint);
        }
        if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
            FillPath(paint);
        }
#endif
    } else {
        CircleParam* circleParam = new CircleParam;
        if (circleParam == nullptr) {
            GRAPHIC_LOGE("new CircleParam fail");
            return;
        }
        circleParam->center = center;
        circleParam->radius = radius;
M
mamingshuai 已提交
534

535 536 537 538 539 540 541
        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = circleParam;
        cmd.DeleteParam = DeleteCircleParam;
        cmd.DrawGraphics = DoDrawCircle;
        drawCmdList_.PushBack(cmd);
    }
542 543 544 545 546 547 548 549
#else
    CircleParam* circleParam = new CircleParam;
    if (circleParam == nullptr) {
        GRAPHIC_LOGE("new CircleParam fail");
        return;
    }
    circleParam->center = center;
    circleParam->radius = radius;
M
mamingshuai 已提交
550

551 552 553 554 555 556 557
    DrawCmd cmd;
    cmd.paint = paint;
    cmd.param = circleParam;
    cmd.DeleteParam = DeleteCircleParam;
    cmd.DrawGraphics = DoDrawCircle;
    drawCmdList_.PushBack(cmd);
#endif
M
mamingshuai 已提交
558 559 560 561 562 563 564 565 566
    Invalidate();
}

void UICanvas::DrawSector(const Point& center,
                          uint16_t radius,
                          int16_t startAngle,
                          int16_t endAngle,
                          const Paint& paint)
{
567
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
568 569 570 571 572 573 574
    BeginPath();
    MoveTo(center);
    ArcTo(center, radius, startAngle, endAngle);
    ClosePath();
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
        DrawPath(paint);
    }
M
mamingshuai 已提交
575
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
576
        FillPath(paint);
M
mamingshuai 已提交
577
    }
578 579 580 581 582 583 584 585 586 587
#else
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        Paint innerPaint = paint;
        innerPaint.SetStyle(Paint::PaintStyle::STROKE_STYLE);
        innerPaint.SetStrokeWidth(radius);
        innerPaint.SetStrokeColor(paint.GetFillColor());
        radius >>= 1;
        DrawArc(center, radius, startAngle, endAngle, innerPaint);
    }
#endif
M
mamingshuai 已提交
588 589
}

590 591
void UICanvas::DrawArc(const Point& center, uint16_t radius, int16_t startAngle,
                       int16_t endAngle, const Paint& paint)
M
mamingshuai 已提交
592 593
{
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
594
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
595 596 597
        if (paint.GetChangeFlag()) {
            ArcTo(center, radius, startAngle, endAngle);
            DrawPath(paint);
598 599 600
        } else
#endif
        {
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
            ArcParam* arcParam = new ArcParam;
            if (arcParam == nullptr) {
                GRAPHIC_LOGE("new ArcParam fail");
                return;
            }
            arcParam->center = center;
            arcParam->radius = radius;

            int16_t start;
            int16_t end;
            if (startAngle > endAngle) {
                start = endAngle;
                end = startAngle;
            } else {
                start = startAngle;
                end = endAngle;
            }
M
mamingshuai 已提交
618

619 620 621
            DrawArc::GetInstance()->GetDrawRange(start, end);
            arcParam->startAngle = start;
            arcParam->endAngle = end;
M
mamingshuai 已提交
622

623 624 625 626 627 628 629
            DrawCmd cmd;
            cmd.paint = paint;
            cmd.param = arcParam;
            cmd.DeleteParam = DeleteArcParam;
            cmd.DrawGraphics = DoDrawArc;
            drawCmdList_.PushBack(cmd);
        }
M
mamingshuai 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
        Invalidate();
    }
}

void UICanvas::DrawLabel(const Point& startPoint,
                         const char* text,
                         uint16_t maxWidth,
                         const FontStyle& fontStyle,
                         const Paint& paint)
{
    if (text == nullptr) {
        return;
    }
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        UILabel* label = new UILabel();
        if (label == nullptr) {
646
            GRAPHIC_LOGE("new UILabel fail");
M
mamingshuai 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
            return;
        }
        label->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
        label->SetPosition(startPoint.x, startPoint.y);
        label->SetWidth(maxWidth);
        label->SetHeight(GetHeight());
        label->SetText(text);
        label->SetFont(fontStyle.fontName, fontStyle.fontSize);
        label->SetAlign(fontStyle.align);
        label->SetDirect(fontStyle.direct);
        label->SetStyle(STYLE_LETTER_SPACE, fontStyle.letterSpace);
        label->SetStyle(STYLE_TEXT_COLOR, paint.GetFillColor().full);
        label->SetStyle(STYLE_TEXT_OPA, paint.GetOpacity());

        DrawCmd cmd;
        cmd.param = label;
        cmd.DeleteParam = DeleteLabel;
        cmd.DrawGraphics = DoDrawLabel;
        drawCmdList_.PushBack(cmd);

        Invalidate();
    }
}
Y
youbing 已提交
670
#if defined(GRAPHIC_ENABLE_DRAW_IMAGE_FLAG) && GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
671
void UICanvas::DrawImage(const Point &startPoint, const char* image, const Paint& paint)
M
mamingshuai 已提交
672 673 674 675
{
    if (image == nullptr) {
        return;
    }
676 677 678 679 680 681 682 683 684
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        UIExtendImageView* imageView = new UIExtendImageView();
        if (imageView == nullptr) {
            GRAPHIC_LOGE("new UIImageView fail");
            return;
        }
        imageView->SetCanvas(this);
        imageView->SetPosition(startPoint.x, startPoint.y);
        imageView->SetSrc(image);
M
mamingshuai 已提交
685

686 687 688 689 690 691 692 693 694
        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = imageView;
        cmd.DeleteParam = DeleteImageView;
        cmd.DrawGraphics = DoDrawImage;
        drawCmdList_.PushBack(cmd);

        Invalidate();
        SetStartPosition(startPoint);
M
mamingshuai 已提交
695
    }
696 697 698 699 700 701
}

void UICanvas::DrawImage(const Point& startPoint, const char* image,
                         const Paint& paint, int16_t width, int16_t height)
{
    if (image == nullptr) {
M
mamingshuai 已提交
702 703
        return;
    }
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        UIExtendImageView* imageView = new UIExtendImageView();
        if (imageView == nullptr) {
            GRAPHIC_LOGE("new UIImageView fail");
            return;
        }
        imageView->SetCanvas(this);
        imageView->SetPosition(startPoint.x, startPoint.y);
        imageView->SetSrc(image);
        float scaleX = 1.0;
        float scaleY = 1.0;
        if (width > 0 && imageView->GetWidth() > 0) {
            scaleX = (float)width / (float)imageView->GetWidth();
        }
        if (height > 0 && imageView->GetHeight() > 0) {
            scaleY = (float)height / (float)imageView->GetHeight();
        }
        DrawCmd cmd;
        cmd.paint = paint;
        cmd.paint.Scale(scaleX, scaleY);
        cmd.param = imageView;
        cmd.DeleteParam = DeleteImageView;
        cmd.DrawGraphics = DoDrawImage;
        drawCmdList_.PushBack(cmd);
M
mamingshuai 已提交
728

729 730 731
        Invalidate();
        SetStartPosition(startPoint);
    }
M
mamingshuai 已提交
732 733

    Invalidate();
734
    SetStartPosition(startPoint);
M
mamingshuai 已提交
735
}
736
#endif
M
mamingshuai 已提交
737 738 739

void UICanvas::DrawPath(const Paint& paint)
{
740
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
741
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
742 743 744
        return;
    }

745 746 747
    PathParam* pathParam = new PathParam;
    if (pathParam == nullptr) {
        GRAPHIC_LOGE("new LineParam fail");
M
mamingshuai 已提交
748 749 750
        return;
    }

751 752
    pathParam->vertices = vertices_;
    pathParam->isStroke = true;
Y
youbing 已提交
753
#if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
754 755 756 757 758 759
    if (paint.GetStyle() == Paint::PATTERN) {
        ImageParam* imageParam = new ImageParam;
        if (imageParam == nullptr) {
            GRAPHIC_LOGE("new ImageParam fail");
            return;
        }
760

761 762 763 764 765 766 767 768 769 770 771 772 773
        imageParam->image = new Image();
        if (imageParam->image == nullptr) {
            delete imageParam;
            imageParam = nullptr;
            return;
        }

        imageParam->image->SetSrc(paint.GetPatternImage());
        ImageHeader header = {0};
        imageParam->image->GetHeader(header);
        imageParam->start = {0, 0};
        imageParam->height = header.height;
        imageParam->width = header.width;
774

775 776
        pathParam->imageParam = imageParam;
    }
777 778 779 780 781 782 783 784 785 786 787 788 789 790
#endif
#else
    if ((path_ == nullptr) || (path_->cmd_.Size() == 0)) {
        return;
    }

    path_->strokeCount_++;
    PathParam* pathParam = new PathParam;
    if (pathParam == nullptr) {
        GRAPHIC_LOGE("new PathParam fail");
        return;
    }
    pathParam->path = path_;
    pathParam->count = path_->cmd_.Size();
791
#endif
M
mamingshuai 已提交
792 793
    DrawCmd cmd;
    cmd.paint = paint;
794
    cmd.param = pathParam;
M
mamingshuai 已提交
795 796 797 798 799 800
    cmd.DeleteParam = DeletePathParam;
    cmd.DrawGraphics = DoDrawPath;
    drawCmdList_.PushBack(cmd);
    Invalidate();
}

801
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
802 803 804 805 806 807 808 809 810 811 812 813 814 815
void UICanvas::FillPath(const Paint& paint)
{
    if (vertices_ == nullptr) {
        return;
    }

    PathParam* pathParam = new PathParam;
    if (pathParam == nullptr) {
        GRAPHIC_LOGE("new LineParam fail");
        return;
    }

    pathParam->vertices = vertices_;
    pathParam->isStroke = false;
Y
youbing 已提交
816
#if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
817 818 819 820 821 822
    if (paint.GetStyle() == Paint::PATTERN) {
        ImageParam* imageParam = new ImageParam;
        if (imageParam == nullptr) {
            GRAPHIC_LOGE("new ImageParam fail");
            return;
        }
823

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
        imageParam->image = new Image();
        if (imageParam->image == nullptr) {
            delete imageParam;
            imageParam = nullptr;
            return;
        }

        imageParam->image->SetSrc(paint.GetPatternImage());
        ImageHeader header = {0};
        imageParam->image->GetHeader(header);
        imageParam->start = {0, 0};
        imageParam->height = header.height;
        imageParam->width = header.width;
        pathParam->imageParam = imageParam;
    }
#endif
840 841 842 843 844 845 846 847
    DrawCmd cmd;
    cmd.paint = paint;
    cmd.param = pathParam;
    cmd.DeleteParam = DeletePathParam;
    cmd.DrawGraphics = DoFillPath;
    drawCmdList_.PushBack(cmd);
    Invalidate();
}
848
#endif
849

N
niulihua 已提交
850
void UICanvas::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
851 852
{
    Rect rect = GetOrigRect();
N
niulihua 已提交
853
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, rect, invalidatedArea, *style_, opaScale_);
M
mamingshuai 已提交
854 855 856 857 858

    void* param = nullptr;
    ListNode<DrawCmd>* curDraw = drawCmdList_.Begin();
    Rect coords = GetOrigRect();
    Rect trunc = invalidatedArea;
859 860 861
    if (!trunc.Intersect(trunc, coords)) {
        return;
    }
862
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
863 864 865 866 867 868 869 870 871 872
    bool haveComposite = false;
    for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
        if (curDraw->data_.paint.HaveComposite()) {
            haveComposite = true;
            break;
        }
    }

    if (haveComposite) {
        OnBlendDraw(gfxDstBuffer, trunc);
873 874 875
    } else
#endif
    {
876
        curDraw = drawCmdList_.Begin();
M
mamingshuai 已提交
877 878
        for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
            param = curDraw->data_.param;
N
niulihua 已提交
879
            curDraw->data_.DrawGraphics(gfxDstBuffer, param, curDraw->data_.paint, rect, trunc, *style_);
M
mamingshuai 已提交
880 881 882 883
        }
    }
}

884
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
885 886 887 888 889 890 891 892 893 894
void OnBlendDrawPattern(ListNode<UICanvas::DrawCmd>* curDraw,
                        UICanvas::DrawCmd& drawCmd,
                        Rect& rect,
                        const Rect& trunc,
                        RasterizerScanlineAntialias& blendRasterizer,
                        RasterizerScanlineAntialias& rasterizer,
                        RenderBase& renBase,
                        TransAffine& transform,
                        PathParam* pathParamBlend)
{
Y
youbing 已提交
895
#if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    if (curDraw->data_.paint.GetStyle() == Paint::PATTERN) {
        if (curDraw->data_.param == nullptr) {
            return;
        }
        PathParam* pathParam = static_cast<PathParam*>(curDraw->data_.param);
        ImageParam* imageParam = static_cast<ImageParam*>(pathParam->imageParam);
        if (imageParam->image == nullptr) {
            return;
        }
        FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), curDraw->data_.paint.GetPatternRepeatMode(),
                                    rect.GetLeft(), rect.GetTop());
        UICanvas::BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform,
                              spanPattern, trunc, pathParamBlend->isStroke);
    }
#endif
}

void OnBlendDrawGradient(ListNode<UICanvas::DrawCmd>* curDraw,
                         UICanvas::DrawCmd& drawCmd,
                         const Rect& trunc,
                         RasterizerScanlineAntialias& blendRasterizer,
                         RasterizerScanlineAntialias& rasterizer,
                         RenderBase& renBase,
                         TransAffine& transform,
                         PathParam* pathParamBlend)
{
Y
youbing 已提交
922
#if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
923 924 925 926 927 928 929 930 931 932
    if (curDraw->data_.paint.GetStyle() == Paint::GRADIENT) {
        TransAffine gradientMatrix;
        FillInterpolator interpolatorType(gradientMatrix);
        FillGradientLut gradientColorMode;
        DrawCanvas::BuildGradientColor(curDraw->data_.paint, gradientColorMode);
        if (curDraw->data_.paint.GetGradient() == Paint::Linear) {
            float distance = 0;
            DrawCanvas::BuildLineGradientMatrix(drawCmd.paint, gradientMatrix, transform, distance);
            GradientLinearCalculate gradientLinearCalculate;
            FillGradient span(interpolatorType, gradientLinearCalculate, gradientColorMode, 0, distance);
Y
youbing 已提交
933 934
            UICanvas::BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase,
                                  transform, span, trunc, pathParamBlend->isStroke);
935 936 937 938 939 940 941 942 943
        }
        if (curDraw->data_.paint.GetGradient() == Paint::Radial) {
            Paint::RadialGradientPoint radialPoint = drawCmd.paint.GetRadialGradientPoint();
            float startRadius = 0;
            float endRadius = 0;
            DrawCanvas::BuildRadialGradientMatrix(drawCmd.paint, gradientMatrix, transform, startRadius, endRadius);
            GradientRadialCalculate gradientRadialCalculate(endRadius, radialPoint.x0 - radialPoint.x1,
                                                            radialPoint.y0 - radialPoint.y1);
            FillGradient span(interpolatorType, gradientRadialCalculate, gradientColorMode, startRadius, endRadius);
Y
youbing 已提交
944 945
            UICanvas::BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase,
                                  transform, span, trunc, pathParamBlend->isStroke);
946 947 948 949 950
        }
    }
#endif
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
void UICanvas::OnBlendDraw(BufferInfo& gfxDstBuffer, const Rect& trunc)
{
    Rect rect = GetOrigRect();
    RenderBuffer renderBuffer;
    TransAffine transform;
    ListNode<DrawCmd>* curDrawEnd = drawCmdList_.Begin();
    RasterizerScanlineAntialias blendRasterizer;
    DrawCmd drawCmd;
    int count = 0;
    for (; curDrawEnd != drawCmdList_.End(); curDrawEnd = curDrawEnd->next_) {
        if (curDrawEnd->data_.paint.HaveComposite()) {
            drawCmd = curDrawEnd->data_;
            count++;
        }
    }
    if (drawCmd.param == nullptr) {
        return;
    }
    PathParam* pathParamBlend = static_cast<PathParam*>(drawCmd.param);
    ListNode<DrawCmd>* curDraw = drawCmdList_.Begin();
971 972 973
    DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint);
    DrawCanvas::SetRasterizer(*pathParamBlend->vertices, drawCmd.paint, blendRasterizer, transform,
                              pathParamBlend->isStroke);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    RasterizerScanlineAntialias scanline;
    RenderPixfmtRgbaBlend pixFormat(renderBuffer);
    RenderBase renBase(pixFormat);
    renBase.ResetClipping(true);
    renBase.ClipBox(trunc.GetLeft(), trunc.GetTop(), trunc.GetRight(), trunc.GetBottom());
    for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
        if (curDraw->data_.paint.HaveComposite()) {
            drawCmd = curDraw->data_;
            count--;
        }
        if (count <= 0) {
            continue;
        }
        RasterizerScanlineAntialias rasterizer;
        if (curDraw->data_.param == nullptr) {
            continue;
        }
        PathParam* pathParam = static_cast<PathParam*>(curDraw->data_.param);
Y
youbing 已提交
992
#if defined(GRAPHIC_ENABLE_BLUR_EFFECT_FLAG) && GRAPHIC_ENABLE_BLUR_EFFECT_FLAG
993
        if (curDraw->data_.paint.HaveShadow()) {
994 995
            DrawCanvas::DoDrawShadow(gfxDstBuffer, curDraw->data_.param, curDraw->data_.paint, rect, trunc, *style_,
                                     pathParam->isStroke);
996 997
        }
#endif
998
        DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint);
999
        rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height);
1000 1001
        DrawCanvas::SetRasterizer(*pathParam->vertices, curDraw->data_.paint, rasterizer, transform,
                                  pathParam->isStroke);
1002 1003
        if (IsSoild(curDraw->data_.paint)) {
            Rgba8T color;
1004
            DrawCanvas::RenderBlendSolid(curDraw->data_.paint, color, pathParam->isStroke);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
            SpanSoildColor spanSoildColor(color);
            BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform,
                        spanSoildColor, rect, pathParamBlend->isStroke);
        }

        OnBlendDrawGradient(curDraw, drawCmd, trunc, blendRasterizer,
                            rasterizer, renBase, transform, pathParamBlend);

        OnBlendDrawPattern(curDraw, drawCmd, rect, trunc, blendRasterizer,
                           rasterizer, renBase, transform, pathParamBlend);
    }
}
1017
#endif
1018

M
mamingshuai 已提交
1019 1020 1021 1022 1023 1024
void UICanvas::GetAbsolutePosition(const Point& prePoint, const Rect& rect, const Style& style, Point& point)
{
    point.x = prePoint.x + rect.GetLeft() + style.paddingLeft_ + style.borderWidth_;
    point.y = prePoint.y + rect.GetTop() + style.paddingTop_ + style.borderWidth_;
}

N
niulihua 已提交
1025 1026
void UICanvas::DoDrawLine(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
{
    if (param == nullptr) {
        return;
    }
    LineParam* lineParam = static_cast<LineParam*>(param);
    Point start;
    Point end;
    GetAbsolutePosition(lineParam->start, rect, style, start);
    GetAbsolutePosition(lineParam->end, rect, style, end);

N
niulihua 已提交
1041 1042
    BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start, end, invalidatedArea, paint.GetStrokeWidth(),
                                           paint.GetStrokeColor(), paint.GetOpacity());
M
mamingshuai 已提交
1043 1044
}

N
niulihua 已提交
1045 1046
void UICanvas::DoDrawCurve(BufferInfo& gfxDstBuffer,
                           void* param,
M
mamingshuai 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
                           const Paint& paint,
                           const Rect& rect,
                           const Rect& invalidatedArea,
                           const Style& style)
{
    if (param == nullptr) {
        return;
    }
    CurveParam* curveParam = static_cast<CurveParam*>(param);
    Point start;
    Point end;
    Point control1;
    Point control2;
    GetAbsolutePosition(curveParam->start, rect, style, start);
    GetAbsolutePosition(curveParam->end, rect, style, end);
    GetAbsolutePosition(curveParam->control1, rect, style, control1);
    GetAbsolutePosition(curveParam->control2, rect, style, control2);

1065 1066 1067 1068 1069 1070 1071 1072 1073
    BaseGfxEngine::GetInstance()->DrawCubicBezier(gfxDstBuffer,
                                                  start,
                                                  control1,
                                                  control2,
                                                  end,
                                                  invalidatedArea,
                                                  paint.GetStrokeWidth(),
                                                  paint.GetStrokeColor(),
                                                  paint.GetOpacity());
M
mamingshuai 已提交
1074 1075
}

N
niulihua 已提交
1076 1077
void UICanvas::DoDrawRect(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
{
    if (param == nullptr) {
        return;
    }
    RectParam* rectParam = static_cast<RectParam*>(param);
    Style drawStyle = StyleDefault::GetDefaultStyle();
    drawStyle.bgColor_ = paint.GetStrokeColor();
    drawStyle.bgOpa_ = paint.GetOpacity();
    drawStyle.borderRadius_ = 0;

    int16_t lineWidth = static_cast<int16_t>(paint.GetStrokeWidth());
    Point start;
    GetAbsolutePosition(rectParam->start, rect, style, start);

    int16_t x = start.x - lineWidth / 2; // 2: half
    int16_t y = start.y - lineWidth / 2; // 2: half
    Rect coords;
    if ((rectParam->height <= lineWidth) || (rectParam->width <= lineWidth)) {
        coords.SetPosition(x, y);
        coords.SetHeight(rectParam->height + lineWidth);
        coords.SetWidth(rectParam->width + lineWidth);
N
niulihua 已提交
1103
        BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
1104 1105 1106 1107 1108 1109
        return;
    }

    coords.SetPosition(x, y);
    coords.SetHeight(lineWidth);
    coords.SetWidth(rectParam->width);
N
niulihua 已提交
1110
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
1111 1112 1113 1114

    coords.SetPosition(x + rectParam->width, y);
    coords.SetHeight(rectParam->height);
    coords.SetWidth(lineWidth);
N
niulihua 已提交
1115
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
1116 1117 1118 1119

    coords.SetPosition(x, y + lineWidth);
    coords.SetHeight(rectParam->height);
    coords.SetWidth(lineWidth);
N
niulihua 已提交
1120
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
1121 1122 1123 1124

    coords.SetPosition(x + lineWidth, y + rectParam->height);
    coords.SetHeight(lineWidth);
    coords.SetWidth(rectParam->width);
N
niulihua 已提交
1125
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
1126 1127
}

N
niulihua 已提交
1128 1129
void UICanvas::DoFillRect(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
{
    if (param == nullptr) {
        return;
    }
    RectParam* rectParam = static_cast<RectParam*>(param);
    uint8_t enableStroke = static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE;
    int16_t lineWidth = enableStroke ? paint.GetStrokeWidth() : 0;
    if ((rectParam->height <= lineWidth) || (rectParam->width <= lineWidth)) {
        return;
    }
    Point start;
    GetAbsolutePosition(rectParam->start, rect, style, start);

    Rect coords;
    coords.SetPosition(start.x + (lineWidth + 1) / 2, start.y + (lineWidth + 1) / 2); // 2: half
    coords.SetHeight(rectParam->height - lineWidth);
    coords.SetWidth(rectParam->width - lineWidth);

    Style drawStyle = StyleDefault::GetDefaultStyle();
    drawStyle.bgColor_ = paint.GetFillColor();
    drawStyle.bgOpa_ = paint.GetOpacity();
    drawStyle.borderRadius_ = 0;
N
niulihua 已提交
1156
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
1157 1158
}

N
niulihua 已提交
1159 1160
void UICanvas::DoDrawCircle(BufferInfo& gfxDstBuffer,
                            void* param,
M
mamingshuai 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
                            const Paint& paint,
                            const Rect& rect,
                            const Rect& invalidatedArea,
                            const Style& style)
{
    if (param == nullptr) {
        return;
    }
    CircleParam* circleParam = static_cast<CircleParam*>(param);

    Style drawStyle = StyleDefault::GetDefaultStyle();
    drawStyle.lineOpa_ = paint.GetOpacity();

    ArcInfo arcInfo = {{0}};
1175
    arcInfo.imgPos = {0, 0};
M
mamingshuai 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184
    arcInfo.startAngle = 0;
    arcInfo.endAngle = CIRCLE_IN_DEGREE;
    GetAbsolutePosition(circleParam->center, rect, style, arcInfo.center);
    uint8_t enableStroke = static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE;
    uint16_t halfLineWidth = enableStroke ? (paint.GetStrokeWidth() >> 1) : 0;
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        arcInfo.radius = circleParam->radius - halfLineWidth;
        drawStyle.lineWidth_ = arcInfo.radius;
        drawStyle.lineColor_ = paint.GetFillColor();
N
niulihua 已提交
1185 1186
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, drawStyle, OPA_OPAQUE,
                                              CapType::CAP_NONE);
M
mamingshuai 已提交
1187 1188 1189 1190 1191 1192
    }

    if (enableStroke) {
        arcInfo.radius = circleParam->radius + halfLineWidth - 1;
        drawStyle.lineWidth_ = static_cast<int16_t>(paint.GetStrokeWidth());
        drawStyle.lineColor_ = paint.GetStrokeColor();
N
niulihua 已提交
1193 1194
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, drawStyle, OPA_OPAQUE,
                                              CapType::CAP_NONE);
M
mamingshuai 已提交
1195 1196 1197
    }
}

N
niulihua 已提交
1198 1199
void UICanvas::DoDrawArc(BufferInfo& gfxDstBuffer,
                         void* param,
M
mamingshuai 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
                         const Paint& paint,
                         const Rect& rect,
                         const Rect& invalidatedArea,
                         const Style& style)
{
    if (param == nullptr) {
        return;
    }
    ArcParam* arcParam = static_cast<ArcParam*>(param);

    ArcInfo arcInfo = {{0}};
1211
    arcInfo.imgPos = {0, 0};
M
mamingshuai 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220
    arcInfo.startAngle = arcParam->startAngle;
    arcInfo.endAngle = arcParam->endAngle;
    Style drawStyle = StyleDefault::GetDefaultStyle();
    drawStyle.lineWidth_ = static_cast<int16_t>(paint.GetStrokeWidth());
    drawStyle.lineColor_ = paint.GetStrokeColor();
    drawStyle.lineOpa_ = paint.GetOpacity();
    arcInfo.radius = arcParam->radius + ((paint.GetStrokeWidth() + 1) >> 1);

    GetAbsolutePosition(arcParam->center, rect, style, arcInfo.center);
N
niulihua 已提交
1221 1222
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, drawStyle, OPA_OPAQUE,
                                          CapType::CAP_NONE);
M
mamingshuai 已提交
1223
}
Y
youbing 已提交
1224
#if defined(GRAPHIC_ENABLE_DRAW_IMAGE_FLAG) && GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
N
niulihua 已提交
1225 1226
void UICanvas::DoDrawImage(BufferInfo& gfxDstBuffer,
                           void* param,
M
mamingshuai 已提交
1227 1228 1229 1230 1231 1232 1233 1234
                           const Paint& paint,
                           const Rect& rect,
                           const Rect& invalidatedArea,
                           const Style& style)
{
    if (param == nullptr) {
        return;
    }
1235 1236
    UIImageView* imageView = static_cast<UIImageView*>(param);
    Point startPos = {imageView->GetX(), imageView->GetY()};
M
mamingshuai 已提交
1237
    Point start;
1238 1239 1240 1241
    GetAbsolutePosition({startPos.x, startPos.y}, rect, style, start);
    imageView->SetPosition(start.x, start.y);
    if (!paint.GetTransAffine().IsIdentity()) {
        float angle = paint.GetRotateAngle();
Y
youbing 已提交
1242
        imageView->Rotate(MATH_ROUND(angle), Vector2<float>(0, 0));
1243 1244 1245 1246 1247 1248
        imageView->Translate(Vector3<int16_t>(paint.GetTranslateX(), paint.GetTranslateY(), 1));
        Vector2<float> scale(static_cast<float>(paint.GetScaleX()), static_cast<float>(paint.GetScaleY()));
        imageView->Scale(scale, Vector2<float>(0, 0));
    }
    imageView->OnDraw(gfxDstBuffer, invalidatedArea);
    imageView->SetPosition(startPos.x, startPos.y);
M
mamingshuai 已提交
1249
}
1250
#endif
M
mamingshuai 已提交
1251

N
niulihua 已提交
1252 1253
void UICanvas::DoDrawLabel(BufferInfo& gfxDstBuffer,
                           void* param,
M
mamingshuai 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
                           const Paint& paint,
                           const Rect& rect,
                           const Rect& invalidatedArea,
                           const Style& style)
{
    if (param == nullptr) {
        return;
    }
    UILabel* label = static_cast<UILabel*>(param);
    Point startPos = {label->GetX(), label->GetY()};
    Point start;
    GetAbsolutePosition({startPos.x, startPos.y}, rect, style, start);
    label->SetPosition(start.x, start.y);
N
niulihua 已提交
1267
    label->OnDraw(gfxDstBuffer, invalidatedArea);
M
mamingshuai 已提交
1268 1269 1270
    label->SetPosition(startPos.x, startPos.y);
}

N
niulihua 已提交
1271 1272 1273 1274
void UICanvas::DoDrawLineJoin(BufferInfo& gfxDstBuffer,
                              const Point& center,
                              const Rect& invalidatedArea,
                              const Paint& paint)
M
mamingshuai 已提交
1275 1276 1277
{
    ArcInfo arcinfo = {{0}};
    arcinfo.center = center;
1278
    arcinfo.imgPos = {0, 0};
M
mamingshuai 已提交
1279 1280 1281 1282 1283 1284 1285 1286
    arcinfo.radius = (paint.GetStrokeWidth() + 1) >> 1;
    arcinfo.startAngle = 0;
    arcinfo.endAngle = CIRCLE_IN_DEGREE;

    Style style;
    style.lineColor_ = paint.GetStrokeColor();
    style.lineWidth_ = static_cast<int16_t>(paint.GetStrokeWidth());
    style.lineOpa_ = OPA_OPAQUE;
1287 1288
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcinfo, invalidatedArea,
                                          style, OPA_OPAQUE, CapType::CAP_NONE);
M
mamingshuai 已提交
1289 1290
}

N
niulihua 已提交
1291 1292
void UICanvas::DoDrawPath(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
1293 1294 1295 1296
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
1297
{
1298
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
1299
    BaseGfxEngine::GetInstance()->DrawPath(gfxDstBuffer, param, paint, rect, invalidatedArea, style);
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
#else
    if (param == nullptr) {
        return;
    }
    PathParam* pathParam = static_cast<PathParam*>(param);
    const UICanvasPath* path = pathParam->path;
    if (path == nullptr) {
        return;
    }
    Point pathEnd = {COORD_MIN, COORD_MIN};

    ListNode<Point>* pointIter = path->points_.Begin();
    ListNode<ArcParam>* arcIter = path->arcParam_.Begin();
    ListNode<PathCmd>* iter = path->cmd_.Begin();
    for (uint16_t i = 0; (i < pathParam->count) && (iter != path->cmd_.End()); i++, iter = iter->next_) {
        switch (iter->data_) {
            case CMD_MOVE_TO: {
                pointIter = pointIter->next_;
                break;
            }
            case CMD_LINE_TO: {
                Point start = pointIter->prev_->data_;
                Point end = pointIter->data_;
                pointIter = pointIter->next_;
                if ((start.x == end.x) && (start.y == end.y)) {
                    break;
                }

                GetAbsolutePosition(start, rect, style, start);
                GetAbsolutePosition(end, rect, style, end);
                BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start, end, invalidatedArea,
                                                       paint.GetStrokeWidth(), paint.GetStrokeColor(), OPA_OPAQUE);
                if ((pathEnd.x == start.x) && (pathEnd.y == start.y)) {
                    DoDrawLineJoin(gfxDstBuffer, start, invalidatedArea, paint);
                }
                pathEnd = end;
                break;
            }
            case CMD_ARC: {
                ArcInfo arcInfo = {{0}};
                arcInfo.imgPos = Point{0, 0};
                arcInfo.startAngle = arcIter->data_.startAngle;
                arcInfo.endAngle = arcIter->data_.endAngle;
                Style drawStyle = StyleDefault::GetDefaultStyle();
                drawStyle.lineWidth_ = static_cast<int16_t>(paint.GetStrokeWidth());
                drawStyle.lineColor_ = paint.GetStrokeColor();
                drawStyle.lineOpa_ = OPA_OPAQUE;
                arcInfo.radius = arcIter->data_.radius + ((paint.GetStrokeWidth() + 1) >> 1);

                GetAbsolutePosition(arcIter->data_.center, rect, style, arcInfo.center);
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, drawStyle, OPA_OPAQUE,
                                                      CapType::CAP_NONE);
                if (pointIter != path->points_.Begin()) {
                    DoDrawLineJoin(gfxDstBuffer, pathEnd, invalidatedArea, paint);
                }

                GetAbsolutePosition(pointIter->data_, rect, style, pathEnd);
                pointIter = pointIter->next_;
                arcIter = arcIter->next_;
                break;
            }
            case CMD_CLOSE: {
                Point start = pointIter->prev_->data_;
                Point end = pointIter->data_;
                GetAbsolutePosition(start, rect, style, start);
                GetAbsolutePosition(end, rect, style, end);
                if ((start.x != end.x) || (start.y != end.y)) {
                    BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start, end, invalidatedArea,
                                                           paint.GetStrokeWidth(), paint.GetStrokeColor(), OPA_OPAQUE);
                    if ((pathEnd.x == start.x) && (pathEnd.y == start.y)) {
                        DoDrawLineJoin(gfxDstBuffer, start, invalidatedArea, paint);
                    }
                    pathEnd = end;
                }

                if ((pathEnd.x == end.x) && (pathEnd.y == end.y)) {
                    DoDrawLineJoin(gfxDstBuffer, end, invalidatedArea, paint);
                }
                pointIter = pointIter->next_;
                break;
            }
            default:
                break;
        }
    }
#endif
1386 1387 1388 1389 1390 1391 1392 1393 1394
}

void UICanvas::DoFillPath(BufferInfo& gfxDstBuffer,
                          void* param,
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
{
1395
    BaseGfxEngine::GetInstance()->FillPath(gfxDstBuffer, param, paint, rect, invalidatedArea, style);
1396 1397
}

Y
youbing 已提交
1398
#if defined(GRAPHIC_ENABLE_DRAW_TEXT_FLAG) && GRAPHIC_ENABLE_DRAW_TEXT_FLAG
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
void UICanvas::StrokeText(const char* text, const Point& point, const FontStyle& fontStyle, const Paint& paint)
{
    if (text == nullptr) {
        return;
    }
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
        TextParam* textParam = new TextParam;
        if (textParam == nullptr) {
            GRAPHIC_LOGE("new TextParam fail");
            return;
        }
        textParam->text = text;
        textParam->fontStyle = fontStyle;
        textParam->fontOpa = paint.GetOpacity();
        textParam->fontColor = paint.GetFillColor();
        textParam->position = point;
        DrawCmd cmd;
        cmd.param = textParam;
        cmd.DeleteParam = DeleteTextParam;
        cmd.DrawGraphics = DoDrawText;
        cmd.paint = paint;
        drawCmdList_.PushBack(cmd);
        Invalidate();
        SetStartPosition(point);
    }
}
#endif

Point UICanvas::MeasureText(const char* text, const FontStyle& fontStyle)
{
    Text* textCompent = new Text;
    textCompent->SetText(text);
    textCompent->SetFont(fontStyle.fontName, fontStyle.fontSize);
    textCompent->SetDirect(static_cast<UITextLanguageDirect>(fontStyle.direct));
    textCompent->SetAlign(static_cast<UITextLanguageAlignment>(fontStyle.align));
    Style drawStyle;
    drawStyle.SetStyle(STYLE_LETTER_SPACE, fontStyle.letterSpace);
    textCompent->ReMeasureTextSize(this->GetRect(), drawStyle);
    Point textSize = textCompent->GetTextSize();
    delete textCompent;
    return textSize;
}

void UICanvas::BlitMapBuffer(BufferInfo &gfxDstBuffer, BufferInfo& gfxMapBuffer, Rect& textRect,
                             TransformMap& transMap, const Rect& invalidatedArea)
{
    Rect invalidRect = textRect;
    transMap.SetTransMapRect(textRect);
    if (invalidRect.Intersect(invalidRect, transMap.GetBoxRect())) {
        uint8_t pxSize = DrawUtils::GetPxSizeByColorMode(gfxDstBuffer.mode);
        ImageInfo imageInfo;
        imageInfo.header.colorMode = gfxDstBuffer.mode;
        imageInfo.dataSize = gfxMapBuffer.width * gfxMapBuffer.height *
                DrawUtils::GetByteSizeByColorMode(gfxDstBuffer.mode);
        imageInfo.header.width = gfxMapBuffer.width;
        imageInfo.header.height = gfxMapBuffer.height;
        imageInfo.header.reserved = 0;
        uint8_t* addr = reinterpret_cast<uint8_t*>(gfxMapBuffer.virAddr);
        imageInfo.data = addr;
        TransformDataInfo imageTranDataInfo = {imageInfo.header, imageInfo.data, pxSize,
                                               BlurLevel::LEVEL0, TransformAlgorithm::BILINEAR};
        BaseGfxEngine::GetInstance()->DrawTransform(gfxDstBuffer, invalidatedArea, {0, 0}, Color::Black(),
                                                    OPA_OPAQUE, transMap, imageTranDataInfo);
    }
}

Y
youbing 已提交
1465
#if defined(GRAPHIC_ENABLE_DRAW_TEXT_FLAG) && GRAPHIC_ENABLE_DRAW_TEXT_FLAG
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
void UICanvas::DoDrawText(BufferInfo& gfxDstBuffer,
                          void* param,
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
{
    TextParam* textParam = static_cast<TextParam*>(param);
    if (textParam == nullptr) {
        return;
    }
    if (textParam->fontStyle.fontSize <= 0) {
        return;
    }
    Text* text = textParam->textComment;
    text->SetText(textParam->text);
    text->SetFont(textParam->fontStyle.fontName, textParam->fontStyle.fontSize);
    text->SetDirect(static_cast<UITextLanguageDirect>(textParam->fontStyle.direct));
    text->SetAlign(static_cast<UITextLanguageAlignment>(textParam->fontStyle.align));

    Point start;
    Rect textRect = invalidatedArea;
    GetAbsolutePosition(textParam->position, rect, style, start);
    textRect.SetPosition(start.x, start.y);
    Style drawStyle = style;
    drawStyle.textColor_ = textParam->fontColor;
    drawStyle.lineColor_ = textParam->fontColor;
    drawStyle.bgColor_ = textParam->fontColor;
    drawStyle.SetStyle(STYLE_LETTER_SPACE, textParam->fontStyle.letterSpace);
    text->ReMeasureTextSize(textRect, drawStyle);
    if (text->GetTextSize().x == 0 || text->GetTextSize().y == 0) {
        return;
    }
    textRect.SetWidth(text->GetTextSize().x + 1);
    textRect.SetHeight(text->GetTextSize().y + 1);
    OpacityType opa = DrawUtils::GetMixOpacity(textParam->fontOpa, style.bgOpa_);
    if (!paint.GetTransAffine().IsIdentity()) {
        Rect textImageRect(0, 0, textRect.GetWidth(), textRect.GetHeight());
1504
        BufferInfo* mapBufferInfo = UpdateMapBufferInfo(gfxDstBuffer, textImageRect);
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
        text->OnDraw(*mapBufferInfo, textImageRect, textImageRect, textImageRect, 0, drawStyle,
                     Text::TEXT_ELLIPSIS_END_INV, opa);
        TransformMap trans;
        trans.SetTransMapRect(textRect);
        trans.Scale(Vector2<float>(static_cast<float>(paint.GetScaleX()), static_cast<float>(paint.GetScaleY())),
                    Vector2<float>(0, 0));
        float angle = paint.GetRotateAngle();
        trans.Rotate(MATH_ROUND(angle),  Vector2<float>(0, 0));
        trans.Translate(Vector2<int16_t>(paint.GetTranslateX(), paint.GetTranslateY()));
        BlitMapBuffer(gfxDstBuffer, *mapBufferInfo, textRect, trans, invalidatedArea);
    } else {
        text->OnDraw(gfxDstBuffer, invalidatedArea, textRect, textRect, 0,
                     drawStyle, Text::TEXT_ELLIPSIS_END_INV, opa);
    }
}
#endif
1521

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
void UICanvas::InitGfxMapBuffer(const BufferInfo& srcBuff, const Rect& rect)
{
    gfxMapBuffer_ = new BufferInfo();
    gfxMapBuffer_->rect = rect;
    gfxMapBuffer_->mode = srcBuff.mode;
    gfxMapBuffer_->color = srcBuff.color;
    gfxMapBuffer_->width = static_cast<uint16_t>(rect.GetWidth());
    gfxMapBuffer_->height = static_cast<uint16_t>(rect.GetHeight());
    uint8_t destByteSize = DrawUtils::GetByteSizeByColorMode(srcBuff.mode);
    gfxMapBuffer_->stride = static_cast<int32_t>(gfxMapBuffer_->width) * static_cast<int32_t>(destByteSize);
    uint32_t buffSize = gfxMapBuffer_->height * gfxMapBuffer_->stride;
1533
    gfxMapBuffer_->virAddr = BaseGfxEngine::GetInstance()->AllocBuffer(buffSize, BUFFER_MAP_SURFACE);
1534 1535 1536 1537
    gfxMapBuffer_->phyAddr = gfxMapBuffer_->virAddr;

    errno_t err = memset_s(gfxMapBuffer_->virAddr, buffSize, 0, buffSize);
    if (err != EOK) {
L
leihonglin 已提交
1538
        BaseGfxEngine::GetInstance()->FreeBuffer(static_cast<uint8_t*>(gfxMapBuffer_->virAddr), BUFFER_MAP_SURFACE);
1539 1540 1541 1542
        GRAPHIC_LOGE("memset_s gfxMapBuffer_ fail");
        return;
    }
}
1543

1544 1545 1546 1547 1548 1549
BufferInfo* UICanvas::UpdateMapBufferInfo(const BufferInfo& srcBuff, const Rect& rect)
{
    if (gfxMapBuffer_ == nullptr) {
        InitGfxMapBuffer(srcBuff, rect);
        return gfxMapBuffer_;
    }
M
mamingshuai 已提交
1550

1551 1552 1553 1554 1555 1556 1557 1558 1559
    if (rect.GetWidth() != gfxMapBuffer_->width ||
        rect.GetHeight() != gfxMapBuffer_->height ||
        srcBuff.mode != gfxMapBuffer_->mode) {
        DestroyMapBufferInfo();
        InitGfxMapBuffer(srcBuff, rect);
    } else {
        uint32_t buffSize = gfxMapBuffer_->height * gfxMapBuffer_->stride;
        errno_t err = memset_s(gfxMapBuffer_->virAddr, buffSize, 0, buffSize);
        if (err != EOK) {
L
leihonglin 已提交
1560
            BaseGfxEngine::GetInstance()->FreeBuffer(static_cast<uint8_t*>(gfxMapBuffer_->virAddr), BUFFER_MAP_SURFACE);
1561 1562 1563 1564 1565
            GRAPHIC_LOGE("memset_s gfxMapBuffer_ fail");
        }
    }
    return gfxMapBuffer_;
}
1566

1567 1568 1569
void UICanvas::DestroyMapBufferInfo()
{
    if (gfxMapBuffer_ != nullptr) {
1570
        BaseGfxEngine::GetInstance()->FreeBuffer(static_cast<uint8_t*>(gfxMapBuffer_->virAddr), BUFFER_MAP_SURFACE);
1571 1572 1573 1574 1575 1576
        gfxMapBuffer_->virAddr = nullptr;
        gfxMapBuffer_->phyAddr = nullptr;
        delete gfxMapBuffer_;
        gfxMapBuffer_ = nullptr;
    }
}
1577

1578
#if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
void UICanvas::BlendRaster(const Paint& paint,
                           void* param,
                           RasterizerScanlineAntialias& blendRasterizer,
                           RasterizerScanlineAntialias& rasterizer,
                           RenderBase& renBase,
                           TransAffine& transform,
                           SpanBase& spanGen,
                           const Rect& rect,
                           bool isStroke)
{
    TransAffine gradientMatrixBlend;
    GeometryScanline scanline1;
    GeometryScanline scanline2;
    FillBase allocator1;

    if (IsSoild(paint)) {
        Rgba8T blendColor;
1596
        DrawCanvas::RenderBlendSolid(paint, blendColor, isStroke);
1597 1598 1599 1600
        SpanSoildColor spanBlendSoildColor(blendColor);
        BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer,
                      scanline1, scanline2, renBase, allocator1, spanBlendSoildColor, spanGen);
    }
Y
youbing 已提交
1601
#if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
1602 1603 1604
    FillInterpolator interpolatorTypeBlend(gradientMatrixBlend);
    FillGradientLut gradientColorModeBlend;
    if (paint.GetStyle() == Paint::GRADIENT) {
1605
        DrawCanvas::BuildGradientColor(paint, gradientColorModeBlend);
1606 1607
        if (paint.GetGradient() == Paint::Linear) {
            float distance = 0;
1608
            DrawCanvas::BuildLineGradientMatrix(paint, gradientMatrixBlend, transform, distance);
1609 1610 1611 1612 1613 1614 1615 1616 1617
            GradientLinearCalculate gradientLinearCalculate;
            FillGradient span(interpolatorTypeBlend, gradientLinearCalculate,
                                    gradientColorModeBlend, 0, distance);
            BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer,
                          scanline1, scanline2, renBase, allocator1, span, spanGen);
        } else if (paint.GetGradient() == Paint::Radial) {
            Paint::RadialGradientPoint radialPoint = paint.GetRadialGradientPoint();
            float startRadius = 0;
            float endRadius = 0;
1618
            DrawCanvas::BuildRadialGradientMatrix(paint, gradientMatrixBlend, transform, startRadius, endRadius);
1619 1620 1621 1622 1623 1624 1625 1626 1627
            GradientRadialCalculate gradientRadialCalculate(endRadius, radialPoint.x0 - radialPoint.x1,
                                                            radialPoint.y0 - radialPoint.y1);
            FillGradient span(interpolatorTypeBlend, gradientRadialCalculate, gradientColorModeBlend,
                                    startRadius, endRadius);
            BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer,
                          scanline1, scanline2, renBase, allocator1, span, spanGen);
        }
    }
#endif
Y
youbing 已提交
1628
#if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
1629 1630 1631 1632
    if (paint.GetStyle() == Paint::PATTERN) {
        if (param == nullptr) {
            return;
        }
1633

1634 1635 1636 1637 1638 1639 1640
        PathParam* pathParam = static_cast<PathParam*>(param);

        ImageParam* imageParam = static_cast<ImageParam*>(pathParam->imageParam);

        if (imageParam->image == nullptr) {
            return;
        }
1641 1642
        FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), paint.GetPatternRepeatMode(), rect.GetLeft(),
                                    rect.GetTop());
1643 1644
        BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer,
                      scanline1, scanline2, renBase, allocator1, spanPattern, spanGen);
M
mamingshuai 已提交
1645
    }
1646
#endif
M
mamingshuai 已提交
1647
}
1648
#endif // ENABLE_CANVAS_EXTEND
1649
} // namespace OHOS