You need to sign in or sign up before continuing.
ui_canvas.cpp 48.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
#include "draw/draw_arc.h"
20
#include "draw/draw_canvas.h"
M
mamingshuai 已提交
21
#include "draw/draw_image.h"
N
niulihua 已提交
22
#include "gfx_utils/graphic_log.h"
23 24 25
#include "render/render_buffer.h"
#include "render/render_pixfmt_rgba_blend.h"
#include "render/render_scanline.h"
26

M
mamingshuai 已提交
27
namespace OHOS {
28 29

BufferInfo* UICanvas::gfxMapBuffer_ = nullptr;
30

31
void RenderSolid(const Paint& paint,
32 33
                 RasterizerScanlineAntialias& rasterizer,
                 RenderBase& renBase,
34
                 const bool& isStroke);
M
mamingshuai 已提交
35 36 37 38

void UICanvas::BeginPath()
{
    /* If the previous path is not added to the drawing linked list, it should be destroyed directly. */
39 40 41
    if (vertices_ != nullptr && vertices_->GetTotalVertices() == 0) {
        delete vertices_;
        vertices_ = nullptr;
M
mamingshuai 已提交
42 43
    }

44 45 46
    vertices_ = new UICanvasVertices();
    if (vertices_ == nullptr) {
        GRAPHIC_LOGE("new UICanvasVertices fail");
M
mamingshuai 已提交
47 48 49 50 51 52
        return;
    }
}

void UICanvas::MoveTo(const Point& point)
{
53
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
54 55
        return;
    }
56
    vertices_->MoveTo(point.x, point.y);
M
mamingshuai 已提交
57 58 59 60
}

void UICanvas::LineTo(const Point& point)
{
61
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
62 63
        return;
    }
64
    vertices_->LineTo(point.x, point.y);
M
mamingshuai 已提交
65 66 67 68
}

void UICanvas::ArcTo(const Point& center, uint16_t radius, int16_t startAngle, int16_t endAngle)
{
69
    if (vertices_ == nullptr || startAngle == endAngle) {
M
mamingshuai 已提交
70 71 72 73
        return;
    }
    float sinma = radius * Sin(startAngle);
    float cosma = radius * Sin(QUARTER_IN_DEGREE - startAngle);
74 75
    if (vertices_->GetTotalVertices() != 0) {
        vertices_->LineTo(float(center.x + sinma), float(center.y - cosma));
M
mamingshuai 已提交
76
    } else {
77
        vertices_->MoveTo(float(center.x + sinma), float(center.y - cosma));
M
mamingshuai 已提交
78 79 80 81 82
    }
    if (MATH_ABS(startAngle - endAngle) < CIRCLE_IN_DEGREE) {
        sinma = radius * Sin(endAngle);
        cosma = radius * Sin(QUARTER_IN_DEGREE - endAngle);
    }
83 84 85 86
    int16_t angle = endAngle - startAngle;
    bool largeArcFlag = false;
    if (angle > SEMICIRCLE_IN_DEGREE || angle <= 0) {
        largeArcFlag = true;
M
mamingshuai 已提交
87
    }
88
    vertices_->ArcTo(radius, radius, angle, largeArcFlag, 1, float(center.x + sinma), float(center.y - cosma));
M
mamingshuai 已提交
89 90 91 92
}

void UICanvas::AddRect(const Point& point, int16_t height, int16_t width)
{
93
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
94 95 96
        return;
    }

97 98 99 100 101 102 103 104 105 106 107 108
    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;
    }
M
mamingshuai 已提交
109
    MoveTo(point);
110 111 112
    LineTo({right, point.y});
    LineTo({right, bottom});
    LineTo({point.x, bottom});
M
mamingshuai 已提交
113 114 115 116 117
    ClosePath();
}

void UICanvas::ClosePath()
{
118
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
119 120
        return;
    }
121
    vertices_->ClosePolygon();
M
mamingshuai 已提交
122 123 124 125 126 127 128 129 130 131 132 133
}

UICanvas::~UICanvas()
{
    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();
134 135 136 137
    if (vertices_ != nullptr) {
        delete vertices_;
        vertices_ = nullptr;
    }
138
    DestroyMapBufferInfo();
M
mamingshuai 已提交
139 140 141 142 143 144 145 146 147 148 149 150
}

void UICanvas::Clear()
{
    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();
151 152 153 154
    if (vertices_ != nullptr) {
        delete vertices_;
        vertices_ = nullptr;
    }
M
mamingshuai 已提交
155 156 157
    Invalidate();
}

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
void DeleteImageParam(void* param)
{
    ImageParam* imageParam = static_cast<ImageParam*>(param);
    if (imageParam->image != nullptr) {
        delete imageParam->image;
        imageParam->image = nullptr;
    }
    delete imageParam;
    imageParam = nullptr;
}

void DeletePathParam(void* param)
{
    PathParam* pathParam = static_cast<PathParam*>(param);
    if (pathParam->vertices != nullptr) {
        pathParam->vertices->FreeAll();
        pathParam->vertices = nullptr;
    }
    if (pathParam->imageParam != nullptr) {
        DeleteImageParam(pathParam->imageParam);
    }
    delete pathParam;
    pathParam = nullptr;
}

M
mamingshuai 已提交
183 184 185 186 187 188 189 190 191
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) {
192
        GRAPHIC_LOGE("new LineParam fail");
M
mamingshuai 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        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) {
222
        GRAPHIC_LOGE("new CurveParam fail");
M
mamingshuai 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        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)
{
246 247 248
    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 已提交
249 250
        }

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        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);
    }
    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 已提交
284
        cmd.DrawGraphics = DoDrawRect;
285 286
    } else if (paintStyle == Paint::PaintStyle::FILL_STYLE) {
        cmd.DrawGraphics = DoFillRect;
M
mamingshuai 已提交
287
    }
288 289
    drawCmdList_.PushBack(cmd);
}
M
mamingshuai 已提交
290

291 292 293
void UICanvas::StrokeRect(const Point& startPoint, int16_t height, int16_t width, const Paint& paint)
{
    if (!paint.GetChangeFlag()) {
M
mamingshuai 已提交
294 295
        RectParam* rectParam = new RectParam;
        if (rectParam == nullptr) {
296
            GRAPHIC_LOGE("new RectParam fail");
M
mamingshuai 已提交
297 298 299 300 301 302 303 304 305 306
            return;
        }
        rectParam->start = startPoint;
        rectParam->height = height;
        rectParam->width = width;

        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = rectParam;
        cmd.DeleteParam = DeleteRectParam;
307
        cmd.DrawGraphics = DoDrawRect;
M
mamingshuai 已提交
308
        drawCmdList_.PushBack(cmd);
309 310 311 312 313 314 315 316
    } 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 已提交
317
    }
318
    SetStartPosition(startPoint);
M
mamingshuai 已提交
319 320
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334
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);
}

M
mamingshuai 已提交
335 336
void UICanvas::DrawCircle(const Point& center, uint16_t radius, const Paint& paint)
{
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    if (paint.GetChangeFlag()) {
#if GRAPHIC_ENABLE_BEZIER_ARC_FLAG
        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 已提交
361

362 363 364 365 366 367 368
        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = circleParam;
        cmd.DeleteParam = DeleteCircleParam;
        cmd.DrawGraphics = DoDrawCircle;
        drawCmdList_.PushBack(cmd);
    }
M
mamingshuai 已提交
369 370 371 372 373 374 375 376 377 378

    Invalidate();
}

void UICanvas::DrawSector(const Point& center,
                          uint16_t radius,
                          int16_t startAngle,
                          int16_t endAngle,
                          const Paint& paint)
{
379 380 381 382 383 384 385
    BeginPath();
    MoveTo(center);
    ArcTo(center, radius, startAngle, endAngle);
    ClosePath();
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
        DrawPath(paint);
    }
M
mamingshuai 已提交
386
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::FILL_STYLE) {
387
        FillPath(paint);
M
mamingshuai 已提交
388 389 390
    }
}

391 392
void UICanvas::DrawArc(const Point& center, uint16_t radius, int16_t startAngle,
                       int16_t endAngle, const Paint& paint)
M
mamingshuai 已提交
393 394
{
    if (static_cast<uint8_t>(paint.GetStyle()) & Paint::PaintStyle::STROKE_STYLE) {
395 396 397
        if (paint.GetChangeFlag()) {
            ArcTo(center, radius, startAngle, endAngle);
            DrawPath(paint);
M
mamingshuai 已提交
398
        } else {
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
            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 已提交
416

417 418 419
            DrawArc::GetInstance()->GetDrawRange(start, end);
            arcParam->startAngle = start;
            arcParam->endAngle = end;
M
mamingshuai 已提交
420

421 422 423 424 425 426 427
            DrawCmd cmd;
            cmd.paint = paint;
            cmd.param = arcParam;
            cmd.DeleteParam = DeleteArcParam;
            cmd.DrawGraphics = DoDrawArc;
            drawCmdList_.PushBack(cmd);
        }
M
mamingshuai 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        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) {
444
            GRAPHIC_LOGE("new UILabel fail");
M
mamingshuai 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
            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();
    }
}
468 469
#if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
void UICanvas::DrawImage(const Point &startPoint, const char* image, const Paint& paint)
M
mamingshuai 已提交
470 471 472 473
{
    if (image == nullptr) {
        return;
    }
474 475 476 477 478 479 480 481 482
    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 已提交
483

484 485 486 487 488 489 490 491 492
        DrawCmd cmd;
        cmd.paint = paint;
        cmd.param = imageView;
        cmd.DeleteParam = DeleteImageView;
        cmd.DrawGraphics = DoDrawImage;
        drawCmdList_.PushBack(cmd);

        Invalidate();
        SetStartPosition(startPoint);
M
mamingshuai 已提交
493
    }
494 495 496 497 498 499
}

void UICanvas::DrawImage(const Point& startPoint, const char* image,
                         const Paint& paint, int16_t width, int16_t height)
{
    if (image == nullptr) {
M
mamingshuai 已提交
500 501
        return;
    }
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    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 已提交
526

527 528 529
        Invalidate();
        SetStartPosition(startPoint);
    }
M
mamingshuai 已提交
530 531

    Invalidate();
532
    SetStartPosition(startPoint);
M
mamingshuai 已提交
533
}
534
#endif
M
mamingshuai 已提交
535 536 537

void UICanvas::DrawPath(const Paint& paint)
{
538
    if (vertices_ == nullptr) {
M
mamingshuai 已提交
539 540 541
        return;
    }

542 543 544
    PathParam* pathParam = new PathParam;
    if (pathParam == nullptr) {
        GRAPHIC_LOGE("new LineParam fail");
M
mamingshuai 已提交
545 546 547
        return;
    }

548 549
    pathParam->vertices = vertices_;
    pathParam->isStroke = true;
550 551 552 553 554 555 556
#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
    if (paint.GetStyle() == Paint::PATTERN) {
        ImageParam* imageParam = new ImageParam;
        if (imageParam == nullptr) {
            GRAPHIC_LOGE("new ImageParam fail");
            return;
        }
557

558 559 560 561 562 563 564 565 566 567 568 569 570
        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;
571

572 573 574
        pathParam->imageParam = imageParam;
    }
#endif
M
mamingshuai 已提交
575 576
    DrawCmd cmd;
    cmd.paint = paint;
577
    cmd.param = pathParam;
M
mamingshuai 已提交
578 579 580 581 582 583
    cmd.DeleteParam = DeletePathParam;
    cmd.DrawGraphics = DoDrawPath;
    drawCmdList_.PushBack(cmd);
    Invalidate();
}

584 585 586 587 588 589 590 591 592 593 594 595 596 597
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;
598 599 600 601 602 603 604
#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
    if (paint.GetStyle() == Paint::PATTERN) {
        ImageParam* imageParam = new ImageParam;
        if (imageParam == nullptr) {
            GRAPHIC_LOGE("new ImageParam fail");
            return;
        }
605

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
        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
622 623 624 625 626 627 628 629 630
    DrawCmd cmd;
    cmd.paint = paint;
    cmd.param = pathParam;
    cmd.DeleteParam = DeletePathParam;
    cmd.DrawGraphics = DoFillPath;
    drawCmdList_.PushBack(cmd);
    Invalidate();
}

N
niulihua 已提交
631
void UICanvas::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
632 633
{
    Rect rect = GetOrigRect();
N
niulihua 已提交
634
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, rect, invalidatedArea, *style_, opaScale_);
M
mamingshuai 已提交
635 636 637 638 639

    void* param = nullptr;
    ListNode<DrawCmd>* curDraw = drawCmdList_.Begin();
    Rect coords = GetOrigRect();
    Rect trunc = invalidatedArea;
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    if (!trunc.Intersect(trunc, coords)) {
        return;
    }
    bool haveComposite = false;
    for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
        if (curDraw->data_.paint.HaveComposite()) {
            haveComposite = true;
            break;
        }
    }

    if (haveComposite) {
        OnBlendDraw(gfxDstBuffer, trunc);
    } else {
        curDraw = drawCmdList_.Begin();
M
mamingshuai 已提交
655 656
        for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) {
            param = curDraw->data_.param;
N
niulihua 已提交
657
            curDraw->data_.DrawGraphics(gfxDstBuffer, param, curDraw->data_.paint, rect, trunc, *style_);
M
mamingshuai 已提交
658 659 660 661
        }
    }
}

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
void OnBlendDrawPattern(ListNode<UICanvas::DrawCmd>* curDraw,
                        UICanvas::DrawCmd& drawCmd,
                        Rect& rect,
                        const Rect& trunc,
                        RasterizerScanlineAntialias& blendRasterizer,
                        RasterizerScanlineAntialias& rasterizer,
                        RenderBase& renBase,
                        TransAffine& transform,
                        PathParam* pathParamBlend)
{
#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
    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)
{
#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
    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);
            UICanvas::BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform, span, trunc,
                        pathParamBlend->isStroke);
        }
        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);
            UICanvas::BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform, span, trunc,
                        pathParamBlend->isStroke);
        }
    }
#endif
}

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
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();
748 749 750
    DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint);
    DrawCanvas::SetRasterizer(*pathParamBlend->vertices, drawCmd.paint, blendRasterizer, transform,
                              pathParamBlend->isStroke);
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    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);
#if GRAPHIC_ENABLE_BLUR_EFFECT_FLAG
        if (curDraw->data_.paint.HaveShadow()) {
771 772
            DrawCanvas::DoDrawShadow(gfxDstBuffer, curDraw->data_.param, curDraw->data_.paint, rect, trunc, *style_,
                                     pathParam->isStroke);
773 774
        }
#endif
775
        DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint);
776
        rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height);
777 778
        DrawCanvas::SetRasterizer(*pathParam->vertices, curDraw->data_.paint, rasterizer, transform,
                                  pathParam->isStroke);
779 780
        if (IsSoild(curDraw->data_.paint)) {
            Rgba8T color;
781
            DrawCanvas::RenderBlendSolid(curDraw->data_.paint, color, pathParam->isStroke);
782 783 784 785 786 787 788 789 790 791 792 793 794
            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);
    }
}

M
mamingshuai 已提交
795 796 797 798 799 800
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 已提交
801 802
void UICanvas::DoDrawLine(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816
                          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 已提交
817 818
    BaseGfxEngine::GetInstance()->DrawLine(gfxDstBuffer, start, end, invalidatedArea, paint.GetStrokeWidth(),
                                           paint.GetStrokeColor(), paint.GetOpacity());
M
mamingshuai 已提交
819 820
}

N
niulihua 已提交
821 822
void UICanvas::DoDrawCurve(BufferInfo& gfxDstBuffer,
                           void* param,
M
mamingshuai 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
                           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);

841 842 843 844 845 846 847 848 849
    BaseGfxEngine::GetInstance()->DrawCubicBezier(gfxDstBuffer,
                                                  start,
                                                  control1,
                                                  control2,
                                                  end,
                                                  invalidatedArea,
                                                  paint.GetStrokeWidth(),
                                                  paint.GetStrokeColor(),
                                                  paint.GetOpacity());
M
mamingshuai 已提交
850 851
}

N
niulihua 已提交
852 853
void UICanvas::DoDrawRect(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
                          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 已提交
879
        BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
880 881 882 883 884 885
        return;
    }

    coords.SetPosition(x, y);
    coords.SetHeight(lineWidth);
    coords.SetWidth(rectParam->width);
N
niulihua 已提交
886
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
887 888 889 890

    coords.SetPosition(x + rectParam->width, y);
    coords.SetHeight(rectParam->height);
    coords.SetWidth(lineWidth);
N
niulihua 已提交
891
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
892 893 894 895

    coords.SetPosition(x, y + lineWidth);
    coords.SetHeight(rectParam->height);
    coords.SetWidth(lineWidth);
N
niulihua 已提交
896
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
897 898 899 900

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

N
niulihua 已提交
904 905
void UICanvas::DoFillRect(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
                          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 已提交
932
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, coords, invalidatedArea, drawStyle, OPA_OPAQUE);
M
mamingshuai 已提交
933 934
}

N
niulihua 已提交
935 936
void UICanvas::DoDrawCircle(BufferInfo& gfxDstBuffer,
                            void* param,
M
mamingshuai 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950
                            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}};
951
    arcInfo.imgPos = {0, 0};
M
mamingshuai 已提交
952 953 954 955 956 957 958 959 960
    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 已提交
961 962
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, drawStyle, OPA_OPAQUE,
                                              CapType::CAP_NONE);
M
mamingshuai 已提交
963 964 965 966 967 968
    }

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

N
niulihua 已提交
974 975
void UICanvas::DoDrawArc(BufferInfo& gfxDstBuffer,
                         void* param,
M
mamingshuai 已提交
976 977 978 979 980 981 982 983 984 985 986
                         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}};
987
    arcInfo.imgPos = {0, 0};
M
mamingshuai 已提交
988 989 990 991 992 993 994 995 996
    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 已提交
997 998
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, drawStyle, OPA_OPAQUE,
                                          CapType::CAP_NONE);
M
mamingshuai 已提交
999
}
1000
#if GRAPHIC_ENABLE_DRAW_IMAGE_FLAG
N
niulihua 已提交
1001 1002
void UICanvas::DoDrawImage(BufferInfo& gfxDstBuffer,
                           void* param,
M
mamingshuai 已提交
1003 1004 1005 1006 1007 1008 1009 1010
                           const Paint& paint,
                           const Rect& rect,
                           const Rect& invalidatedArea,
                           const Style& style)
{
    if (param == nullptr) {
        return;
    }
1011 1012
    UIImageView* imageView = static_cast<UIImageView*>(param);
    Point startPos = {imageView->GetX(), imageView->GetY()};
M
mamingshuai 已提交
1013
    Point start;
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    GetAbsolutePosition({startPos.x, startPos.y}, rect, style, start);
    imageView->SetPosition(start.x, start.y);
    if (!paint.GetTransAffine().IsIdentity()) {
        float angle = paint.GetRotateAngle();
        imageView->Rotate(MATH_ROUND(angle),Vector2<float>(0, 0));
        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 已提交
1025
}
1026
#endif
M
mamingshuai 已提交
1027

N
niulihua 已提交
1028 1029
void UICanvas::DoDrawLabel(BufferInfo& gfxDstBuffer,
                           void* param,
M
mamingshuai 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
                           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 已提交
1043
    label->OnDraw(gfxDstBuffer, invalidatedArea);
M
mamingshuai 已提交
1044 1045 1046
    label->SetPosition(startPos.x, startPos.y);
}

N
niulihua 已提交
1047 1048 1049 1050
void UICanvas::DoDrawLineJoin(BufferInfo& gfxDstBuffer,
                              const Point& center,
                              const Rect& invalidatedArea,
                              const Paint& paint)
M
mamingshuai 已提交
1051 1052 1053
{
    ArcInfo arcinfo = {{0}};
    arcinfo.center = center;
1054
    arcinfo.imgPos = {0, 0};
M
mamingshuai 已提交
1055 1056 1057 1058 1059 1060 1061 1062
    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;
1063 1064
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcinfo, invalidatedArea,
                                          style, OPA_OPAQUE, CapType::CAP_NONE);
M
mamingshuai 已提交
1065 1066
}

N
niulihua 已提交
1067 1068
void UICanvas::DoDrawPath(BufferInfo& gfxDstBuffer,
                          void* param,
M
mamingshuai 已提交
1069 1070 1071 1072
                          const Paint& paint,
                          const Rect& rect,
                          const Rect& invalidatedArea,
                          const Style& style)
1073
{
1074
    BaseGfxEngine::GetInstance()->DrawPath(gfxDstBuffer, param, paint, rect, invalidatedArea, style);
1075 1076 1077 1078 1079 1080 1081 1082 1083
}

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

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 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 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
#if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
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);
    }
}

#if GRAPHIC_ENABLE_DRAW_TEXT_FLAG
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());
1193
        BufferInfo* mapBufferInfo = UpdateMapBufferInfo(gfxDstBuffer, textImageRect);
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
        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
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
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;
1222
    gfxMapBuffer_->virAddr = BaseGfxEngine::GetInstance()->AllocBuffer(buffSize, BUFFER_MAP_SURFACE);
1223 1224 1225 1226
    gfxMapBuffer_->phyAddr = gfxMapBuffer_->virAddr;

    errno_t err = memset_s(gfxMapBuffer_->virAddr, buffSize, 0, buffSize);
    if (err != EOK) {
1227
        BaseGfxEngine::GetInstance()->FreeBuffer((uint8_t*)gfxMapBuffer_->virAddr, BUFFER_MAP_SURFACE);
1228 1229 1230 1231
        GRAPHIC_LOGE("memset_s gfxMapBuffer_ fail");
        return;
    }
}
1232

1233 1234 1235 1236 1237 1238
BufferInfo* UICanvas::UpdateMapBufferInfo(const BufferInfo& srcBuff, const Rect& rect)
{
    if (gfxMapBuffer_ == nullptr) {
        InitGfxMapBuffer(srcBuff, rect);
        return gfxMapBuffer_;
    }
M
mamingshuai 已提交
1239

1240 1241 1242 1243 1244 1245 1246 1247 1248
    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) {
1249
            BaseGfxEngine::GetInstance()->FreeBuffer((uint8_t*)gfxMapBuffer_->virAddr, BUFFER_MAP_SURFACE);
1250 1251 1252 1253 1254
            GRAPHIC_LOGE("memset_s gfxMapBuffer_ fail");
        }
    }
    return gfxMapBuffer_;
}
1255

1256 1257 1258
void UICanvas::DestroyMapBufferInfo()
{
    if (gfxMapBuffer_ != nullptr) {
1259
        BaseGfxEngine::GetInstance()->FreeBuffer(static_cast<uint8_t*>(gfxMapBuffer_->virAddr), BUFFER_MAP_SURFACE);
1260 1261 1262 1263 1264 1265
        gfxMapBuffer_->virAddr = nullptr;
        gfxMapBuffer_->phyAddr = nullptr;
        delete gfxMapBuffer_;
        gfxMapBuffer_ = nullptr;
    }
}
1266

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
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;
1284
        DrawCanvas::RenderBlendSolid(paint, blendColor, isStroke);
1285 1286 1287 1288 1289 1290 1291 1292
        SpanSoildColor spanBlendSoildColor(blendColor);
        BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer,
                      scanline1, scanline2, renBase, allocator1, spanBlendSoildColor, spanGen);
    }
#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
    FillInterpolator interpolatorTypeBlend(gradientMatrixBlend);
    FillGradientLut gradientColorModeBlend;
    if (paint.GetStyle() == Paint::GRADIENT) {
1293
        DrawCanvas::BuildGradientColor(paint, gradientColorModeBlend);
1294 1295
        if (paint.GetGradient() == Paint::Linear) {
            float distance = 0;
1296
            DrawCanvas::BuildLineGradientMatrix(paint, gradientMatrixBlend, transform, distance);
1297 1298 1299 1300 1301 1302 1303 1304 1305
            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;
1306
            DrawCanvas::BuildRadialGradientMatrix(paint, gradientMatrixBlend, transform, startRadius, endRadius);
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
            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
#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG
    if (paint.GetStyle() == Paint::PATTERN) {
        if (param == nullptr) {
            return;
        }
1321

1322 1323 1324 1325 1326 1327 1328
        PathParam* pathParam = static_cast<PathParam*>(param);

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

        if (imageParam->image == nullptr) {
            return;
        }
1329 1330
        FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), paint.GetPatternRepeatMode(), rect.GetLeft(),
                                    rect.GetTop());
1331 1332
        BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer,
                      scanline1, scanline2, renBase, allocator1, spanPattern, spanGen);
M
mamingshuai 已提交
1333
    }
1334
#endif
M
mamingshuai 已提交
1335
}
1336
} // namespace OHOS