diff --git a/BUILD.gn b/BUILD.gn index b957f23ee0fc90efd7bece30dc9e3ef2fa145754..d805d6f6045808912092bd310f383217dc251080 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -167,6 +167,7 @@ lite_library("ui") { "frameworks/dock/virtual_input_device.cpp", "frameworks/draw/clip_utils.cpp", "frameworks/draw/draw_arc.cpp", + "frameworks/draw/draw_canvas.cpp", "frameworks/draw/draw_curve.cpp", "frameworks/draw/draw_image.cpp", "frameworks/draw/draw_label.cpp", diff --git a/frameworks/components/root_view.cpp b/frameworks/components/root_view.cpp index 817970cea3eef18ff2787bfe64c642d8bfa2c60b..2914563765ed074339844736921d971c4a01ad37 100644 --- a/frameworks/components/root_view.cpp +++ b/frameworks/components/root_view.cpp @@ -824,7 +824,7 @@ void RootView::InitMapBufferInfo(BufferInfo* bufferInfo) void RootView::DestroyMapBufferInfo() { if (dc_.mapBufferInfo != nullptr) { - BaseGfxEngine::GetInstance()->FreeBuffer(static_cast(dc_.mapBufferInfo->virAddr)); + BaseGfxEngine::GetInstance()->FreeBuffer(static_cast(dc_.mapBufferInfo->virAddr), BUFFER_MAP_SURFACE); dc_.mapBufferInfo->virAddr = dc_.mapBufferInfo->phyAddr = nullptr; delete dc_.mapBufferInfo; dc_.mapBufferInfo = nullptr; diff --git a/frameworks/components/ui_canvas.cpp b/frameworks/components/ui_canvas.cpp index be5e2f515a87d399416b8c185718e2ae9fd9ffa8..1f60d7faa4eec890ecd4cc1de79eeab73999c54b 100644 --- a/frameworks/components/ui_canvas.cpp +++ b/frameworks/components/ui_canvas.cpp @@ -17,20 +17,16 @@ #include "draw/clip_utils.h" #include "draw/draw_arc.h" +#include "draw/draw_canvas.h" #include "draw/draw_image.h" -#include "draw/draw_utils.h" #include "gfx_utils/graphic_log.h" #include "render/render_buffer.h" #include "render/render_pixfmt_rgba_blend.h" #include "render/render_scanline.h" namespace OHOS { -void InitRenderAndTransform(BufferInfo& gfxDstBuffer, - RenderBuffer& renderBuffer, - const Rect& rect, - TransAffine& transform, - const Style& style, - const Paint& paint); + +BufferInfo* UICanvas::gfxMapBuffer_ = nullptr; void RenderSolid(const Paint& paint, RasterizerScanlineAntialias& rasterizer, @@ -159,6 +155,31 @@ void UICanvas::Clear() Invalidate(); } +void DeleteImageParam(void* param) +{ + ImageParam* imageParam = static_cast(param); + if (imageParam->image != nullptr) { + delete imageParam->image; + imageParam->image = nullptr; + } + delete imageParam; + imageParam = nullptr; +} + +void DeletePathParam(void* param) +{ + PathParam* pathParam = static_cast(param); + if (pathParam->vertices != nullptr) { + pathParam->vertices->FreeAll(); + pathParam->vertices = nullptr; + } + if (pathParam->imageParam != nullptr) { + DeleteImageParam(pathParam->imageParam); + } + delete pathParam; + pathParam = nullptr; +} + void UICanvas::DrawLine(const Point& endPoint, const Paint& paint) { DrawLine(startPoint_, endPoint, paint); @@ -533,6 +554,7 @@ void UICanvas::DrawPath(const Paint& paint) GRAPHIC_LOGE("new ImageParam fail"); return; } + imageParam->image = new Image(); if (imageParam->image == nullptr) { delete imageParam; @@ -546,6 +568,7 @@ void UICanvas::DrawPath(const Paint& paint) imageParam->start = {0, 0}; imageParam->height = header.height; imageParam->width = header.width; + pathParam->imageParam = imageParam; } #endif @@ -579,6 +602,7 @@ void UICanvas::FillPath(const Paint& paint) GRAPHIC_LOGE("new ImageParam fail"); return; } + imageParam->image = new Image(); if (imageParam->image == nullptr) { delete imageParam; @@ -630,16 +654,77 @@ void UICanvas::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) curDraw = drawCmdList_.Begin(); for (; curDraw != drawCmdList_.End(); curDraw = curDraw->next_) { param = curDraw->data_.param; -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - if (curDraw->data_.paint.HaveShadow()) { - curDraw->data_.paint.SetUICanvas(this); - } -#endif curDraw->data_.DrawGraphics(gfxDstBuffer, param, curDraw->data_.paint, rect, trunc, *style_); } } } +void OnBlendDrawPattern(ListNode* 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(curDraw->data_.param); + ImageParam* imageParam = static_cast(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* 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 +} + void UICanvas::OnBlendDraw(BufferInfo& gfxDstBuffer, const Rect& trunc) { Rect rect = GetOrigRect(); @@ -660,8 +745,9 @@ void UICanvas::OnBlendDraw(BufferInfo& gfxDstBuffer, const Rect& trunc) } PathParam* pathParamBlend = static_cast(drawCmd.param); ListNode* curDraw = drawCmdList_.Begin(); - InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint); - SetRasterizer(*pathParamBlend->vertices, drawCmd.paint, blendRasterizer, transform, pathParamBlend->isStroke); + DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint); + DrawCanvas::SetRasterizer(*pathParamBlend->vertices, drawCmd.paint, blendRasterizer, transform, + pathParamBlend->isStroke); RasterizerScanlineAntialias scanline; RenderPixfmtRgbaBlend pixFormat(renderBuffer); RenderBase renBase(pixFormat); @@ -682,17 +768,17 @@ void UICanvas::OnBlendDraw(BufferInfo& gfxDstBuffer, const Rect& trunc) PathParam* pathParam = static_cast(curDraw->data_.param); #if GRAPHIC_ENABLE_BLUR_EFFECT_FLAG if (curDraw->data_.paint.HaveShadow()) { - curDraw->data_.paint.SetUICanvas(this); - DoDrawShadow(gfxDstBuffer, curDraw->data_.param, curDraw->data_.paint, rect, - trunc, *style_, pathParam->isStroke); + DrawCanvas::DoDrawShadow(gfxDstBuffer, curDraw->data_.param, curDraw->data_.paint, rect, trunc, *style_, + pathParam->isStroke); } #endif - InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint); + DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, *style_, curDraw->data_.paint); rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height); - SetRasterizer(*pathParam->vertices, curDraw->data_.paint, rasterizer, transform, pathParam->isStroke); + DrawCanvas::SetRasterizer(*pathParam->vertices, curDraw->data_.paint, rasterizer, transform, + pathParam->isStroke); if (IsSoild(curDraw->data_.paint)) { Rgba8T color; - RenderBlendSolid(curDraw->data_.paint, color, pathParam->isStroke); + DrawCanvas::RenderBlendSolid(curDraw->data_.paint, color, pathParam->isStroke); SpanSoildColor spanSoildColor(color); BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform, spanSoildColor, rect, pathParamBlend->isStroke); @@ -706,70 +792,6 @@ void UICanvas::OnBlendDraw(BufferInfo& gfxDstBuffer, const Rect& trunc) } } -void UICanvas::OnBlendDrawGradient(ListNode* curDraw, 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; - BuildGradientColor(curDraw->data_.paint, gradientColorMode); - if (curDraw->data_.paint.GetGradient() == Paint::Linear) { - float distance = 0; - BuildLineGradientMatrix(drawCmd.paint, gradientMatrix, transform, distance); - GradientLinearCalculate gradientLinearCalculate; - FillGradient span(interpolatorType, gradientLinearCalculate, gradientColorMode, 0, distance); - 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; - 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); - BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform, - span, trunc, pathParamBlend->isStroke); - } - } -#endif -} - -void UICanvas::OnBlendDrawPattern(ListNode* curDraw, 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(curDraw->data_.param); - ImageParam* imageParam = static_cast(pathParam->imageParam); - if (imageParam->image == nullptr) { - return; - } - FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), - curDraw->data_.paint.GetPatternRepeatMode(), rect.GetLeft(), rect.GetTop()); - BlendRaster(drawCmd.paint, drawCmd.param, blendRasterizer, rasterizer, renBase, transform, - spanPattern, trunc, pathParamBlend->isStroke); - } -#endif -} - void UICanvas::GetAbsolutePosition(const Point& prePoint, const Rect& rect, const Style& style, Point& point) { point.x = prePoint.x + rect.GetLeft() + style.paddingLeft_ + style.borderWidth_; @@ -1049,7 +1071,7 @@ void UICanvas::DoDrawPath(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, const Style& style) { - DoRender(gfxDstBuffer, param, paint, rect, invalidatedArea, style, true); + BaseGfxEngine::GetInstance()->DrawPath(gfxDstBuffer, param, paint, rect, invalidatedArea, style); } void UICanvas::DoFillPath(BufferInfo& gfxDstBuffer, @@ -1059,186 +1081,7 @@ void UICanvas::DoFillPath(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, const Style& style) { - DoRender(gfxDstBuffer, param, paint, rect, invalidatedArea, style, false); -} - -void UICanvas::SetRasterizer(UICanvasVertices& vertices, - const Paint& paint, - RasterizerScanlineAntialias& rasterizer, - TransAffine& transform, - const bool& isStroke) -{ - DepictCurve canvasPath(vertices); - if (isStroke) { -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - if (paint.IsLineDash()) { - using DashStyle = DepictDash; - using StrokeDashStyle = DepictStroke; - using StrokeDashTransform = DepictTransform; - DashStyle dashStyle(canvasPath); - LineDashStyleCalc(dashStyle, paint); - StrokeDashStyle strokeDashStyle(dashStyle); - LineStyleCalc(strokeDashStyle, paint); - StrokeDashTransform strokeDashTransform(strokeDashStyle, transform); - rasterizer.Reset(); - rasterizer.AddPath(strokeDashTransform); - return; - } -#endif - using StrokeLineStyle = DepictStroke; - StrokeLineStyle strokeLineStyle(canvasPath); - LineStyleCalc(strokeLineStyle, paint); - - DepictTransform strokeTransform(strokeLineStyle, transform); - rasterizer.Reset(); - rasterizer.AddPath(strokeTransform); - } else { - DepictTransform pathTransform(canvasPath, transform); - rasterizer.Reset(); - rasterizer.AddPath(pathTransform); - } -} - -void UICanvas::DoRender(BufferInfo& gfxDstBuffer, - void* param, - const Paint& paint, - const Rect& rect, - const Rect& invalidatedArea, - const Style& style, - const bool& isStroke) -{ - if (param == nullptr) { - return; - } -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - if (paint.HaveShadow()) { - DoDrawShadow(gfxDstBuffer, param, paint, rect, invalidatedArea, style, isStroke); - } -#endif - TransAffine transform; - RenderBuffer renderBuffer; - InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, style, paint); - - RasterizerScanlineAntialias rasterizer; - GeometryScanline scanline; - - PathParam* pathParam = static_cast(param); - rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height); - SetRasterizer(*pathParam->vertices, paint, rasterizer, transform, isStroke); - - RenderPixfmtRgbaBlend pixFormat(renderBuffer); - RenderBase renBase(pixFormat); - FillBase allocator; - - renBase.ResetClipping(true); - renBase.ClipBox(invalidatedArea.GetLeft(), invalidatedArea.GetTop(), invalidatedArea.GetRight(), - invalidatedArea.GetBottom()); - - if (paint.GetStyle() == Paint::STROKE_STYLE || paint.GetStyle() == Paint::FILL_STYLE || - paint.GetStyle() == Paint::STROKE_FILL_STYLE) { - RenderSolid(paint, rasterizer, renBase, isStroke); - } - -#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG - if (paint.GetStyle() == Paint::GRADIENT) { - RenderGradient(paint, rasterizer, transform, renBase, renderBuffer, allocator, invalidatedArea); - } -#endif -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG - if (paint.GetStyle() == Paint::PATTERN) { - RenderPattern(paint, pathParam->imageParam, rasterizer, renBase, allocator, rect); - } -#endif -} - -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG -void UICanvas::DoDrawShadow(BufferInfo& gfxDstBuffer, - void* param, - const Paint& paint, - const Rect& rect, - const Rect& invalidatedArea, - const Style& style, - const bool& isStroke) -{ - if (param == nullptr) { - return; - } - -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - TransAffine transform; - RenderBuffer renderBuffer; - InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, style, paint); - - transform.Translate(paint.GetShadowOffsetX(), paint.GetShadowOffsetY()); - - RasterizerScanlineAntialias rasterizer; - GeometryScanline scanline; - PathParam* pathParam = static_cast(param); - rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height); - SetRasterizer(*pathParam->vertices, paint, rasterizer, transform, isStroke); - Rect bbox(rasterizer.GetMinX(), rasterizer.GetMinY(), rasterizer.GetMaxX(), rasterizer.GetMaxY()); - - RenderPixfmtRgbaBlend pixFormat(renderBuffer); - RenderBase renBase(pixFormat); - FillBase allocator; - - renBase.ResetClipping(true); - renBase.ClipBox(invalidatedArea.GetLeft(), invalidatedArea.GetTop(), invalidatedArea.GetRight(), - invalidatedArea.GetBottom()); - - Rgba8T shadowColor; - ChangeColor(shadowColor, paint.GetShadowColor(), paint.GetShadowColor().alpha * paint.GetGlobalAlpha()); - - RenderScanlinesAntiAliasSolid(rasterizer, scanline, renBase, shadowColor); -#if GRAPHIC_ENABLE_BLUR_EFFECT_FLAG - bbox.SetLeft(bbox.GetLeft() - paint.GetShadowBlur()); - bbox.SetTop(bbox.GetTop() - paint.GetShadowBlur()); - bbox.SetRight(bbox.GetRight() + paint.GetShadowBlur()); - bbox.SetBottom(bbox.GetBottom() + paint.GetShadowBlur()); - RenderBuffer shadowBuffer; - RenderPixfmtRgbaBlend pixf2(shadowBuffer); - Rect shadowRect = {int16_t(bbox.GetLeft()), int16_t(bbox.GetTop()), - int16_t(bbox.GetRight()), int16_t(bbox.GetBottom())}; - shadowRect.Intersect(shadowRect, invalidatedArea); - pixf2.Attach(pixFormat, shadowRect.GetLeft(), shadowRect.GetTop(), - shadowRect.GetRight(), shadowRect.GetBottom()); - uint8_t pixelByteSize = DrawUtils::GetPxSizeByColorMode(gfxDstBuffer.mode) >> 3; // 3: Shift right 3 bits - - paint.GetUICanvas()->GetDrawBoxBlur().BoxBlur(pixf2, MATH_UROUND(paint.GetShadowBlur()), - pixelByteSize, gfxDstBuffer.stride); - -#endif -#endif -} -#endif -/** - * Renders monochrome polygon paths and fills - */ -void RenderSolid(const Paint& paint, - RasterizerScanlineAntialias& rasterizer, - RenderBase& renBase, - const bool& isStroke) -{ - GeometryScanline scanline; - Rgba8T color; - UICanvas::RenderBlendSolid(paint, color, isStroke); - RenderScanlinesAntiAliasSolid(rasterizer, scanline, renBase, color); -} - -void InitRenderAndTransform(BufferInfo& gfxDstBuffer, - RenderBuffer& renderBuffer, - const Rect& rect, - TransAffine& transform, - const Style& style, - const Paint& paint) -{ - int16_t realLeft = rect.GetLeft() + style.paddingLeft_ + style.borderWidth_; - int16_t realTop = rect.GetTop() + style.paddingTop_ + style.borderWidth_; - transform.Reset(); - transform *= paint.GetTransAffine(); - transform.Translate(realLeft, realTop); - renderBuffer.Attach(static_cast(gfxDstBuffer.virAddr), gfxDstBuffer.width, gfxDstBuffer.height, - gfxDstBuffer.stride); + BaseGfxEngine::GetInstance()->FillPath(gfxDstBuffer, param, paint, rect, invalidatedArea, style); } #if GRAPHIC_ENABLE_DRAW_TEXT_FLAG @@ -1263,7 +1106,6 @@ void UICanvas::StrokeText(const char* text, const Point& point, const FontStyle& cmd.DeleteParam = DeleteTextParam; cmd.DrawGraphics = DoDrawText; cmd.paint = paint; - cmd.paint.SetUICanvas(this); drawCmdList_.PushBack(cmd); Invalidate(); SetStartPosition(point); @@ -1348,10 +1190,7 @@ void UICanvas::DoDrawText(BufferInfo& gfxDstBuffer, OpacityType opa = DrawUtils::GetMixOpacity(textParam->fontOpa, style.bgOpa_); if (!paint.GetTransAffine().IsIdentity()) { Rect textImageRect(0, 0, textRect.GetWidth(), textRect.GetHeight()); - if (paint.GetUICanvas() == nullptr) { - return; - } - BufferInfo* mapBufferInfo = paint.GetUICanvas()->UpdateMapBufferInfo(gfxDstBuffer, textImageRect); + BufferInfo* mapBufferInfo = UpdateMapBufferInfo(gfxDstBuffer, textImageRect); text->OnDraw(*mapBufferInfo, textImageRect, textImageRect, textImageRect, 0, drawStyle, Text::TEXT_ELLIPSIS_END_INV, opa); TransformMap trans; @@ -1368,93 +1207,6 @@ void UICanvas::DoDrawText(BufferInfo& gfxDstBuffer, } } #endif -#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG -void UICanvas::RenderGradient(const Paint &paint, RasterizerScanlineAntialias &rasterizer, - TransAffine &transform, RenderBase &renBase, - RenderBuffer &renderBuffer, FillBase &allocator, - const Rect &invalidatedArea) -{ - GeometryScanline scanline; - - RenderPixfmtRgbaBlend pixFormatComp(renderBuffer); - RenderBase m_renBaseComp(pixFormatComp); - - m_renBaseComp.ResetClipping(true); - m_renBaseComp.ClipBox(invalidatedArea.GetLeft(), invalidatedArea.GetTop(), - invalidatedArea.GetRight(), invalidatedArea.GetBottom()); - TransAffine gradientMatrix; - FillInterpolator interpolatorType(gradientMatrix); - FillGradientLut gradientColorMode; - BuildGradientColor(paint, gradientColorMode); - if (paint.GetGradient() == Paint::Linear) { - float distance = 0; - BuildLineGradientMatrix(paint, gradientMatrix, transform, distance); - GradientLinearCalculate gradientLinearCalculate; - FillGradient span(interpolatorType, gradientLinearCalculate, gradientColorMode, 0, distance); - RenderScanlinesAntiAlias(rasterizer, scanline, renBase, allocator, span); - } - - if (paint.GetGradient() == Paint::Radial) { - Paint::RadialGradientPoint radialPoint = paint.GetRadialGradientPoint(); - float startRadius = 0; - float endRadius = 0; - BuildRadialGradientMatrix(paint, gradientMatrix, transform, startRadius, endRadius); - GradientRadialCalculate gradientRadialCalculate(radialPoint.r1, radialPoint.x0 - radialPoint.x1, - radialPoint.y0 - radialPoint.y1); - FillGradient span(interpolatorType, gradientRadialCalculate, gradientColorMode, - startRadius, endRadius); - RenderScanlinesAntiAlias(rasterizer, scanline, renBase, allocator, span); - } -} - -void UICanvas::BuildGradientColor(const Paint &paint, FillGradientLut &gradientColorMode) -{ - gradientColorMode.RemoveAll(); - ListNode* iter = paint.getStopAndColor().Begin(); - uint16_t count = 0; - for (; count < paint.getStopAndColor().Size(); count++) { - ColorType stopColor = iter->data_.color; - Rgba8T sRgba8; - ChangeColor(sRgba8, stopColor, stopColor.alpha * paint.GetGlobalAlpha()); - gradientColorMode.AddColor(iter->data_.stop, sRgba8); - iter = iter->next_; - } - gradientColorMode.BuildLut(); -} - -void UICanvas::BuildRadialGradientMatrix(const Paint &paint, TransAffine &gradientMatrix, - TransAffine &transform, float &startRadius, - float &endRadius) -{ - Paint::RadialGradientPoint radialPoint = paint.GetRadialGradientPoint(); - gradientMatrix.Reset(); - gradientMatrix *= TransAffine::TransAffineTranslation(radialPoint.x1, radialPoint.y1); - gradientMatrix *= transform; - gradientMatrix.Invert(); - startRadius = radialPoint.r0; - endRadius = radialPoint.r1; -} -#endif - -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG -void UICanvas::RenderPattern(const Paint &paint, void *param, - RasterizerScanlineAntialias &rasterizer, - RenderBase &renBase, FillBase &allocator, - const Rect &rect) -{ - if (param == nullptr) { - return; - } - ImageParam* imageParam = static_cast(param); - if (imageParam->image == nullptr) { - return; - } - GeometryScanline scanline; - FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), - paint.GetPatternRepeatMode(), rect.GetLeft(), rect.GetTop()); - RenderScanlinesAntiAlias(rasterizer, scanline, renBase, allocator, spanPattern); -} -#endif void UICanvas::InitGfxMapBuffer(const BufferInfo& srcBuff, const Rect& rect) { @@ -1467,12 +1219,12 @@ void UICanvas::InitGfxMapBuffer(const BufferInfo& srcBuff, const Rect& rect) uint8_t destByteSize = DrawUtils::GetByteSizeByColorMode(srcBuff.mode); gfxMapBuffer_->stride = static_cast(gfxMapBuffer_->width) * static_cast(destByteSize); uint32_t buffSize = gfxMapBuffer_->height * gfxMapBuffer_->stride; - gfxMapBuffer_->virAddr = UIMalloc(buffSize); + gfxMapBuffer_->virAddr = BaseGfxEngine::GetInstance()->AllocBuffer(buffSize, BUFFER_MAP_SURFACE); gfxMapBuffer_->phyAddr = gfxMapBuffer_->virAddr; errno_t err = memset_s(gfxMapBuffer_->virAddr, buffSize, 0, buffSize); if (err != EOK) { - BaseGfxEngine::GetInstance()->FreeBuffer((uint8_t*)gfxMapBuffer_->virAddr); + BaseGfxEngine::GetInstance()->FreeBuffer((uint8_t*)gfxMapBuffer_->virAddr, BUFFER_MAP_SURFACE); GRAPHIC_LOGE("memset_s gfxMapBuffer_ fail"); return; } @@ -1494,7 +1246,7 @@ BufferInfo* UICanvas::UpdateMapBufferInfo(const BufferInfo& srcBuff, const Rect& uint32_t buffSize = gfxMapBuffer_->height * gfxMapBuffer_->stride; errno_t err = memset_s(gfxMapBuffer_->virAddr, buffSize, 0, buffSize); if (err != EOK) { - BaseGfxEngine::GetInstance()->FreeBuffer((uint8_t*)gfxMapBuffer_->virAddr); + BaseGfxEngine::GetInstance()->FreeBuffer((uint8_t*)gfxMapBuffer_->virAddr, BUFFER_MAP_SURFACE); GRAPHIC_LOGE("memset_s gfxMapBuffer_ fail"); } } @@ -1504,7 +1256,7 @@ BufferInfo* UICanvas::UpdateMapBufferInfo(const BufferInfo& srcBuff, const Rect& void UICanvas::DestroyMapBufferInfo() { if (gfxMapBuffer_ != nullptr) { - BaseGfxEngine::GetInstance()->FreeBuffer(static_cast(gfxMapBuffer_->virAddr)); + BaseGfxEngine::GetInstance()->FreeBuffer(static_cast(gfxMapBuffer_->virAddr), BUFFER_MAP_SURFACE); gfxMapBuffer_->virAddr = nullptr; gfxMapBuffer_->phyAddr = nullptr; delete gfxMapBuffer_; @@ -1529,7 +1281,7 @@ void UICanvas::BlendRaster(const Paint& paint, if (IsSoild(paint)) { Rgba8T blendColor; - RenderBlendSolid(paint, blendColor, isStroke); + DrawCanvas::RenderBlendSolid(paint, blendColor, isStroke); SpanSoildColor spanBlendSoildColor(blendColor); BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer, scanline1, scanline2, renBase, allocator1, spanBlendSoildColor, spanGen); @@ -1538,10 +1290,10 @@ void UICanvas::BlendRaster(const Paint& paint, FillInterpolator interpolatorTypeBlend(gradientMatrixBlend); FillGradientLut gradientColorModeBlend; if (paint.GetStyle() == Paint::GRADIENT) { - BuildGradientColor(paint, gradientColorModeBlend); + DrawCanvas::BuildGradientColor(paint, gradientColorModeBlend); if (paint.GetGradient() == Paint::Linear) { float distance = 0; - BuildLineGradientMatrix(paint, gradientMatrixBlend, transform, distance); + DrawCanvas::BuildLineGradientMatrix(paint, gradientMatrixBlend, transform, distance); GradientLinearCalculate gradientLinearCalculate; FillGradient span(interpolatorTypeBlend, gradientLinearCalculate, gradientColorModeBlend, 0, distance); @@ -1551,7 +1303,7 @@ void UICanvas::BlendRaster(const Paint& paint, Paint::RadialGradientPoint radialPoint = paint.GetRadialGradientPoint(); float startRadius = 0; float endRadius = 0; - BuildRadialGradientMatrix(paint, gradientMatrixBlend, transform, startRadius, endRadius); + DrawCanvas::BuildRadialGradientMatrix(paint, gradientMatrixBlend, transform, startRadius, endRadius); GradientRadialCalculate gradientRadialCalculate(endRadius, radialPoint.x0 - radialPoint.x1, radialPoint.y0 - radialPoint.y1); FillGradient span(interpolatorTypeBlend, gradientRadialCalculate, gradientColorModeBlend, @@ -1574,8 +1326,8 @@ void UICanvas::BlendRaster(const Paint& paint, if (imageParam->image == nullptr) { return; } - FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), - paint.GetPatternRepeatMode(), rect.GetLeft(), rect.GetTop()); + FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), paint.GetPatternRepeatMode(), rect.GetLeft(), + rect.GetTop()); BlendScanLine(paint.GetGlobalCompositeOperation(), blendRasterizer, rasterizer, scanline1, scanline2, renBase, allocator1, spanPattern, spanGen); } diff --git a/frameworks/draw/draw_canvas.cpp b/frameworks/draw/draw_canvas.cpp new file mode 100644 index 0000000000000000000000000000000000000000..985191ce8fe41765e9d77c7fe32b4c8468accb7c --- /dev/null +++ b/frameworks/draw/draw_canvas.cpp @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "draw/draw_canvas.h" +#include "common/typed_text.h" +#include "draw/clip_utils.h" +#include "gfx_utils/diagram/depiction/depict_curve.h" +#include "gfx_utils/diagram/spancolorfill/fill_gradient.h" +#include "gfx_utils/diagram/spancolorfill/fill_interpolator.h" + +namespace OHOS { +/** + * Renders monochrome polygon paths and fills + */ +void RenderSolid(const Paint& paint, RasterizerScanlineAntialias& rasterizer, RenderBase& renBase, const bool& isStroke) +{ + GeometryScanline scanline; + Rgba8T color; + DrawCanvas::RenderBlendSolid(paint, color, isStroke); + RenderScanlinesAntiAliasSolid(rasterizer, scanline, renBase, color); +} + +void DrawCanvas::DoRender(BufferInfo& gfxDstBuffer, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style, + const bool& isStroke) +{ + if (param == nullptr) { + return; + } +#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG + if (paint.HaveShadow()) { + DrawCanvas::DoDrawShadow(gfxDstBuffer, param, paint, rect, invalidatedArea, style, isStroke); + } +#endif + TransAffine transform; + RenderBuffer renderBuffer; + InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, style, paint); + + RasterizerScanlineAntialias rasterizer; + GeometryScanline scanline; + + PathParam* pathParam = static_cast(param); + rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height); + SetRasterizer(*pathParam->vertices, paint, rasterizer, transform, isStroke); + + RenderPixfmtRgbaBlend pixFormat(renderBuffer); + RenderBase renBase(pixFormat); + FillBase allocator; + + renBase.ResetClipping(true); + renBase.ClipBox(invalidatedArea.GetLeft(), invalidatedArea.GetTop(), invalidatedArea.GetRight(), + invalidatedArea.GetBottom()); + + if (paint.GetStyle() == Paint::STROKE_STYLE || paint.GetStyle() == Paint::FILL_STYLE || + paint.GetStyle() == Paint::STROKE_FILL_STYLE) { + RenderSolid(paint, rasterizer, renBase, isStroke); + } + +#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG + if (paint.GetStyle() == Paint::GRADIENT) { + RenderGradient(paint, rasterizer, transform, renBase, renderBuffer, allocator, invalidatedArea); + } +#endif +#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG + if (paint.GetStyle() == Paint::PATTERN) { + RenderPattern(paint, pathParam->imageParam, rasterizer, renBase, allocator, rect); + } +#endif +} + +#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG +void DrawCanvas::DoDrawShadow(BufferInfo& gfxDstBuffer, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style, + const bool& isStroke) +{ + if (param == nullptr) { + return; + } + + TransAffine transform; + RenderBuffer renderBuffer; + DrawCanvas::InitRenderAndTransform(gfxDstBuffer, renderBuffer, rect, transform, style, paint); + + transform.Translate(paint.GetShadowOffsetX(), paint.GetShadowOffsetY()); + + RasterizerScanlineAntialias rasterizer; + GeometryScanline scanline; + PathParam* pathParam = static_cast(param); + rasterizer.ClipBox(0, 0, gfxDstBuffer.width, gfxDstBuffer.height); + DrawCanvas::SetRasterizer(*pathParam->vertices, paint, rasterizer, transform, isStroke); + Rect bbox(rasterizer.GetMinX(), rasterizer.GetMinY(), rasterizer.GetMaxX(), rasterizer.GetMaxY()); + + RenderPixfmtRgbaBlend pixFormat(renderBuffer); + RenderBase renBase(pixFormat); + FillBase allocator; + + renBase.ResetClipping(true); + renBase.ClipBox(invalidatedArea.GetLeft(), invalidatedArea.GetTop(), invalidatedArea.GetRight(), + invalidatedArea.GetBottom()); + + Rgba8T shadowColor; + DrawCanvas::ChangeColor(shadowColor, paint.GetShadowColor(), paint.GetShadowColor().alpha * paint.GetGlobalAlpha()); + + RenderScanlinesAntiAliasSolid(rasterizer, scanline, renBase, shadowColor); +#if GRAPHIC_ENABLE_BLUR_EFFECT_FLAG + bbox.SetLeft(bbox.GetLeft() - paint.GetShadowBlur()); + bbox.SetTop(bbox.GetTop() - paint.GetShadowBlur()); + bbox.SetRight(bbox.GetRight() + paint.GetShadowBlur()); + bbox.SetBottom(bbox.GetBottom() + paint.GetShadowBlur()); + RenderBuffer shadowBuffer; + RenderPixfmtRgbaBlend pixf2(shadowBuffer); + Rect shadowRect = {int16_t(bbox.GetLeft()), int16_t(bbox.GetTop()), int16_t(bbox.GetRight()), + int16_t(bbox.GetBottom())}; + shadowRect.Intersect(shadowRect, invalidatedArea); + pixf2.Attach(pixFormat, shadowRect.GetLeft(), shadowRect.GetTop(), shadowRect.GetRight(), shadowRect.GetBottom()); + uint8_t pixelByteSize = DrawUtils::GetPxSizeByColorMode(gfxDstBuffer.mode) >> 3; // 3: Shift right 3 bits + + paint.GetDrawBoxBlur().BoxBlur(pixf2, MATH_UROUND(paint.GetShadowBlur()), pixelByteSize, gfxDstBuffer.stride); + +#endif // GRAPHIC_ENABLE_BLUR_EFFECT_FLAG +} +#endif // GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG + +void DrawCanvas::InitRenderAndTransform(BufferInfo& gfxDstBuffer, + RenderBuffer& renderBuffer, + const Rect& rect, + TransAffine& transform, + const Style& style, + const Paint& paint) +{ + int16_t realLeft = rect.GetLeft() + style.paddingLeft_ + style.borderWidth_; + int16_t realTop = rect.GetTop() + style.paddingTop_ + style.borderWidth_; + transform.Reset(); + transform *= paint.GetTransAffine(); + transform.Translate(realLeft, realTop); + renderBuffer.Attach(static_cast(gfxDstBuffer.virAddr), gfxDstBuffer.width, gfxDstBuffer.height, + gfxDstBuffer.stride); +} + +void DrawCanvas::SetRasterizer(UICanvasVertices& vertices, + const Paint& paint, + RasterizerScanlineAntialias& rasterizer, + TransAffine& transform, + const bool& isStroke) +{ + DepictCurve canvasPath(vertices); + if (isStroke) { +#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG + if (paint.IsLineDash()) { + using DashStyle = DepictDash; + using StrokeDashStyle = DepictStroke; + using StrokeDashTransform = DepictTransform; + DashStyle dashStyle(canvasPath); + LineDashStyleCalc(dashStyle, paint); + StrokeDashStyle strokeDashStyle(dashStyle); + LineStyleCalc(strokeDashStyle, paint); + StrokeDashTransform strokeDashTransform(strokeDashStyle, transform); + rasterizer.Reset(); + rasterizer.AddPath(strokeDashTransform); + return; + } +#endif + using StrokeLineStyle = DepictStroke; + StrokeLineStyle strokeLineStyle(canvasPath); + LineStyleCalc(strokeLineStyle, paint); + + DepictTransform strokeTransform(strokeLineStyle, transform); + rasterizer.Reset(); + rasterizer.AddPath(strokeTransform); + } else { + DepictTransform pathTransform(canvasPath, transform); + rasterizer.Reset(); + rasterizer.AddPath(pathTransform); + } +} + +#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG +void DrawCanvas::RenderGradient(const Paint& paint, + RasterizerScanlineAntialias& rasterizer, + TransAffine& transform, + RenderBase& renBase, + RenderBuffer& renderBuffer, + FillBase& allocator, + const Rect& invalidatedArea) +{ + GeometryScanline scanline; + + RenderPixfmtRgbaBlend pixFormatComp(renderBuffer); + RenderBase m_renBaseComp(pixFormatComp); + + m_renBaseComp.ResetClipping(true); + m_renBaseComp.ClipBox(invalidatedArea.GetLeft(), invalidatedArea.GetTop(), invalidatedArea.GetRight(), + invalidatedArea.GetBottom()); + TransAffine gradientMatrix; + FillInterpolator interpolatorType(gradientMatrix); + FillGradientLut gradientColorMode; + BuildGradientColor(paint, gradientColorMode); + if (paint.GetGradient() == Paint::Linear) { + float distance = 0; + BuildLineGradientMatrix(paint, gradientMatrix, transform, distance); + GradientLinearCalculate gradientLinearCalculate; + FillGradient span(interpolatorType, gradientLinearCalculate, gradientColorMode, 0, distance); + RenderScanlinesAntiAlias(rasterizer, scanline, renBase, allocator, span); + } + + if (paint.GetGradient() == Paint::Radial) { + Paint::RadialGradientPoint radialPoint = paint.GetRadialGradientPoint(); + float startRadius = 0; + float endRadius = 0; + BuildRadialGradientMatrix(paint, gradientMatrix, transform, startRadius, endRadius); + GradientRadialCalculate gradientRadialCalculate(radialPoint.r1, radialPoint.x0 - radialPoint.x1, + radialPoint.y0 - radialPoint.y1); + FillGradient span(interpolatorType, gradientRadialCalculate, gradientColorMode, startRadius, endRadius); + RenderScanlinesAntiAlias(rasterizer, scanline, renBase, allocator, span); + } +} + +void DrawCanvas::BuildGradientColor(const Paint& paint, FillGradientLut& gradientColorMode) +{ + gradientColorMode.RemoveAll(); + ListNode* iter = paint.getStopAndColor().Begin(); + uint16_t count = 0; + for (; count < paint.getStopAndColor().Size(); count++) { + ColorType stopColor = iter->data_.color; + Rgba8T sRgba8; + ChangeColor(sRgba8, stopColor, stopColor.alpha * paint.GetGlobalAlpha()); + gradientColorMode.AddColor(iter->data_.stop, sRgba8); + iter = iter->next_; + } + gradientColorMode.BuildLut(); +} + +void DrawCanvas::BuildRadialGradientMatrix(const Paint& paint, + TransAffine& gradientMatrix, + TransAffine& transform, + float& startRadius, + float& endRadius) +{ + Paint::RadialGradientPoint radialPoint = paint.GetRadialGradientPoint(); + gradientMatrix.Reset(); + gradientMatrix *= TransAffine::TransAffineTranslation(radialPoint.x1, radialPoint.y1); + gradientMatrix *= transform; + gradientMatrix.Invert(); + startRadius = radialPoint.r0; + endRadius = radialPoint.r1; +} +#endif // GRAPHIC_ENABLE_GRADIENT_FILL_FLAG + +#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG +void DrawCanvas::RenderPattern(const Paint& paint, + void* param, + RasterizerScanlineAntialias& rasterizer, + RenderBase& renBase, + FillBase& allocator, + const Rect& rect) +{ + if (param == nullptr) { + return; + } + ImageParam* imageParam = static_cast(param); + if (imageParam->image == nullptr) { + return; + } + GeometryScanline scanline; + FillPatternRgba spanPattern(imageParam->image->GetImageInfo(), paint.GetPatternRepeatMode(), rect.GetLeft(), + rect.GetTop()); + RenderScanlinesAntiAlias(rasterizer, scanline, renBase, allocator, spanPattern); +} +#endif // GRAPHIC_ENABLE_PATTERN_FILL_FLAG + +} // namespace OHOS diff --git a/frameworks/draw/draw_canvas.h b/frameworks/draw/draw_canvas.h new file mode 100644 index 0000000000000000000000000000000000000000..e78924569e5813775641a3fe6942d330c22c7963 --- /dev/null +++ b/frameworks/draw/draw_canvas.h @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GRAPHIC_LITE_DRAW_CANVAS_H +#define GRAPHIC_LITE_DRAW_CANVAS_H + +#include "common/image.h" +#include "gfx_utils/diagram/common/paint.h" +#include "gfx_utils/diagram/depiction/depict_dash.h" +#include "gfx_utils/diagram/depiction/depict_stroke.h" +#include "gfx_utils/diagram/rasterizer/rasterizer_scanline_antialias.h" +#include "gfx_utils/diagram/spancolorfill/fill_gradient_lut.h" + + +namespace OHOS { + +struct ImageParam : public HeapBase { + Point start; + uint16_t height; + uint16_t width; + int16_t newWidth; + int16_t newHeight; + Image* image; +}; + +struct PathParam : public HeapBase { + UICanvasVertices* vertices; + ImageParam* imageParam = nullptr; + bool isStroke; +}; + +class RenderBuffer; +class RenderBase; +class DrawCanvas : public HeapBase { +public: + static void DoRender(BufferInfo& gfxDstBuffer, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style, + const bool& isStroke); + +#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG + static void DoDrawShadow(BufferInfo& gfxDstBuffer, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style, + const bool& isStroke); +#endif + + static void InitRenderAndTransform(BufferInfo& gfxDstBuffer, + RenderBuffer& renderBuffer, + const Rect& rect, + TransAffine& transform, + const Style& style, + const Paint& paint); + + static void SetRasterizer(UICanvasVertices& vertices, + const Paint& paint, + RasterizerScanlineAntialias& rasterizer, + TransAffine& transform, + const bool& isStroke); + +#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG + /** + * Render gradient + */ + static void RenderGradient(const Paint& paint, + RasterizerScanlineAntialias& rasterizer, + TransAffine& transform, + RenderBase& renBase, + RenderBuffer& renderBuffer, + FillBase& allocator, + const Rect& invalidatedArea); + + static void BuildGradientColor(const Paint& paint, FillGradientLut& gradientColorMode); + + static void BuildLineGradientMatrix(const Paint& paint, + TransAffine& gradientMatrix, + TransAffine& transform, + float& distance) + { + Paint::LinearGradientPoint linearPoint = paint.GetLinearGradientPoint(); + float angle = FastAtan2F(linearPoint.y1 - linearPoint.y0, linearPoint.x1 - linearPoint.x0); + gradientMatrix.Reset(); + gradientMatrix *= TransAffine::TransAffineRotation(angle); + gradientMatrix *= TransAffine::TransAffineTranslation(linearPoint.x0, linearPoint.y0); + gradientMatrix *= transform; + gradientMatrix.Invert(); + distance = Sqrt((linearPoint.x1 - linearPoint.x0) * (linearPoint.x1 - linearPoint.x0) + + (linearPoint.y1 - linearPoint.y0) * (linearPoint.y1 - linearPoint.y0)); + } + + static void BuildRadialGradientMatrix(const Paint& paint, + TransAffine& gradientMatrix, + TransAffine& transform, + float& startRadius, + float& endRadius); +#endif // GRAPHIC_ENABLE_GRADIENT_FILL_FLAG + +#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG + /** + * Render pattern mode + */ + static void RenderPattern(const Paint& paint, + void* param, + RasterizerScanlineAntialias& rasterizer, + RenderBase& renBase, + FillBase& allocator, + const Rect& rect); +#endif // GRAPHIC_ENABLE_PATTERN_FILL_FLAG + + static void ChangeColor(Rgba8T& color, ColorType colorType, uint8_t alpha) + { + color.red = colorType.red; + color.green = colorType.green; + color.blue = colorType.blue; + color.alpha = alpha; + } + + static void RenderBlendSolid(const Paint& paint, Rgba8T& color, const bool& isStroke) + { + if (isStroke) { + if (paint.GetStyle() == Paint::STROKE_STYLE || paint.GetStyle() == Paint::STROKE_FILL_STYLE) { + ChangeColor(color, paint.GetStrokeColor(), + static_cast(paint.GetStrokeColor().alpha * paint.GetGlobalAlpha())); + } + } else { + if (paint.GetStyle() == Paint::FILL_STYLE || paint.GetStyle() == Paint::STROKE_FILL_STYLE) { + ChangeColor(color, paint.GetFillColor(), + static_cast(paint.GetFillColor().alpha * paint.GetGlobalAlpha())); + } + } + } + + /** + * Assembly parameter setting lineweight,LineCap,LineJoin + */ + template static void LineStyleCalc(DepictStroke& strokeLineStyle, const Paint& paint) + { + strokeLineStyle.SetWidth(paint.GetStrokeWidth()); // Line style related +#if GRAPHIC_ENABLE_LINECAP_FLAG + strokeLineStyle.SetLineCap(paint.GetLineCap()); +#endif +#if GRAPHIC_ENABLE_LINEJOIN_FLAG + strokeLineStyle.SetLineJoin(paint.GetLineJoin()); + if (paint.GetMiterLimit() > 0) { + strokeLineStyle.SetMiterLimit(paint.GetMiterLimit()); + } +#endif + }; + +#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG + /** + * Set linedash style + */ + static void LineDashStyleCalc(DepictDash& dashStyle, const Paint& paint) + { + for (uint32_t i = 0; i < paint.GetLineDashCount(); i += TWO_STEP) { + dashStyle.AddDash(paint.GetLineDash()[i], paint.GetLineDash()[i + 1]); + } + dashStyle.DashStart(paint.GetLineDashOffset()); + }; +#endif +}; +} // namespace OHOS +#endif // GRAPHIC_LITE_DRAW_CANVAS_H diff --git a/frameworks/engines/gfx/gfx_engine_manager.cpp b/frameworks/engines/gfx/gfx_engine_manager.cpp index ffe045a2c2ae6dd536a53b3ce2fe6c04661c034b..a97d0ec19ee91dc217bcc1848a9bc374411fa7f5 100644 --- a/frameworks/engines/gfx/gfx_engine_manager.cpp +++ b/frameworks/engines/gfx/gfx_engine_manager.cpp @@ -19,10 +19,12 @@ #include "draw/clip_utils.h" #include "draw/draw_arc.h" +#include "draw/draw_canvas.h" #include "draw/draw_curve.h" #include "draw/draw_line.h" #include "draw/draw_rect.h" + namespace OHOS { BaseGfxEngine* BaseGfxEngine::baseEngine_ = nullptr; void BaseGfxEngine::DrawArc(BufferInfo& dst, @@ -115,12 +117,32 @@ void BaseGfxEngine::Fill(BufferInfo& dst, const Rect& fillArea, const ColorType DrawUtils::GetInstance()->FillAreaWithSoftWare(dst, fillArea, color, opacity); } +void BaseGfxEngine::DrawPath(BufferInfo& dst, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style) +{ + DrawCanvas::DoRender(dst, param, paint, rect, invalidatedArea, style, true); +} + +void BaseGfxEngine::FillPath(BufferInfo& dst, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style) +{ + DrawCanvas::DoRender(dst, param, paint, rect, invalidatedArea, style, false); +} + uint8_t* BaseGfxEngine::AllocBuffer(uint32_t size, uint32_t usage) { return static_cast(malloc(size)); } -void BaseGfxEngine::FreeBuffer(uint8_t* buffer) +void BaseGfxEngine::FreeBuffer(uint8_t* buffer, uint32_t usage) { free(buffer); } diff --git a/interfaces/innerkits/engines/gfx/gfx_engine_manager.h b/interfaces/innerkits/engines/gfx/gfx_engine_manager.h index ff0ee64f794aae844cffb19987551f3d79f6f268..eb0adc46f1d0d0e01d6aae5eb31552ab9eafe2c9 100644 --- a/interfaces/innerkits/engines/gfx/gfx_engine_manager.h +++ b/interfaces/innerkits/engines/gfx/gfx_engine_manager.h @@ -16,6 +16,7 @@ #ifndef GRAPHIC_LITE_GFX_ENGINE_MANAGER_H #define GRAPHIC_LITE_GFX_ENGINE_MANAGER_H +#include "gfx_utils/diagram/common/paint.h" #include "gfx_utils/geometry2d.h" #include "gfx_utils/graphic_buffer.h" #include "gfx_utils/graphic_math.h" @@ -128,9 +129,23 @@ public: const ColorType color, const OpacityType opacity); + virtual void DrawPath(BufferInfo& dst, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style); + + virtual void FillPath(BufferInfo& dst, + void* param, + const Paint& paint, + const Rect& rect, + const Rect& invalidatedArea, + const Style& style); + virtual uint8_t* AllocBuffer(uint32_t size, uint32_t usage); - virtual void FreeBuffer(uint8_t* buffer); + virtual void FreeBuffer(uint8_t* buffer, uint32_t usage); virtual BufferInfo* GetFBBufferInfo() { diff --git a/interfaces/kits/components/ui_canvas.h b/interfaces/kits/components/ui_canvas.h index 2125e5a7c70776a54bcb6460f8ab378a72b7c5a3..44cfe304a3481e1d79ea7264ecb92f9a795448ec 100644 --- a/interfaces/kits/components/ui_canvas.h +++ b/interfaces/kits/components/ui_canvas.h @@ -53,1015 +53,10 @@ #include "ui_extend_image_view.h" #include "gfx_utils/file.h" #include "gfx_utils/list.h" +#include "gfx_utils/diagram/common/paint.h" namespace OHOS { -const uint16_t DEFAULT_STROKE_WIDTH = 2; -class UICanvas; class RenderBase; -class RenderBuffer; -/** - * @brief Defines the basic styles of graphs drawn on canvases. - * - * @since 1.0 - * @version 1.0 - */ -class Paint : public HeapBase { -public: - /** - * @brief A constructor used to create a Paint instance. - * - * @since 1.0 - * @version 1.0 - */ - Paint() - : style_(PaintStyle::STROKE_FILL_STYLE), - fillColor_(Color::Black()), - strokeColor_(Color::White()), - opacity_(OPA_OPAQUE), - strokeWidth_(DEFAULT_STROKE_WIDTH), - changeFlag_(false), -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - lineJoin_(LineJoin::ROUND_JOIN), -#endif -#if GRAPHIC_ENABLE_LINECAP_FLAG - lineCap_(LineCap::BUTT_CAP), -#endif -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - isDashMode_(false), - dashOffset_(0), - dashArray_(nullptr), - ndashes_(0), -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - miterLimit_(0), -#endif -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - shadowColor_(Color::Black()), - haveShadow_(false), -#endif - globalAlpha_(1.0), - globalCompositeOperation_(SOURCE_OVER), - rotateAngle_(0), - scaleRadioX_(1.0f), - scaleRadioY_(1.0f), - translationX_(0), - translationY_(0), - haveComposite_(false), - uiCanvas_(nullptr) {} - - Paint(const Paint& paint) - { - Init(paint); - } - void InitDash(const Paint& paint) - { -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - if (isDashMode_ && ndashes_ > 0) { - dashArray_ = new float[ndashes_]; - if (dashArray_) { - if (memset_s(dashArray_, ndashes_ * sizeof(float), 0, ndashes_ * sizeof(float)) != EOF) { - } - for (uint32_t i = 0; i < ndashes_; i++) { - dashArray_[i] = paint.dashArray_[i]; - } - } else { - ndashes_ = 0; - dashOffset_ = 0; - isDashMode_ = false; - } - } else { - dashArray_ = nullptr; - } -#endif - } - /* - * Initialize data members. - * style_: paint style. - * fillColor_: Sets the fill color of the pen. - * strokeColor_: Sets the line color of the pen. - * opacity_: Set transparency. - * strokeWidth_: Set lineweight. - * lineCap_: Set pen cap. - * lineJoin_: Sets the style at the path connection of the pen. - * miterLimit_: Sets the spacing limit for sharp corners at path connections. - * dashOffset: dash Point offset. - * isDrawDash: Whether to draw dotted line. - * dashArray: dash Point group. - * ndashes: Number of dotted lines. - * globalAlpha: Set element Global alpha. - * shadowBlurRadius: Sets the shadow blur radius. - * shadowOffsetX: Sets the abscissa offset of the shadow. - * shadowOffsetY: Sets the shadow ordinate offset. - * shadowColor: Set shadow color. - */ - void Init(const Paint& paint) - { - style_ = paint.style_; - fillColor_ = paint.fillColor_; - strokeColor_ = paint.strokeColor_; - strokeWidth_ = paint.strokeWidth_; - opacity_ = paint.opacity_; -#if GRAPHIC_ENABLE_LINECAP_FLAG - lineCap_ = paint.lineCap_; -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - lineJoin_ = paint.lineJoin_; -#endif -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - isDashMode_ = paint.isDashMode_; - dashOffset_ = paint.dashOffset_; - dashArray_ = paint.dashArray_; - ndashes_ = paint.ndashes_; -#endif - changeFlag_ = paint.changeFlag_; - scaleRadioX_= paint.scaleRadioX_; - scaleRadioY_= paint.scaleRadioY_; - translationX_= paint.translationX_; - translationY_= paint.translationY_; - InitDash(paint); -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - miterLimit_ = paint.miterLimit_; -#endif -#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG - linearGradientPoint_ = paint.linearGradientPoint_; - radialGradientPoint_ = paint.radialGradientPoint_; - stopAndColors_ = paint.stopAndColors_; - gradientflag_ = paint.gradientflag_; -#endif -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG - patternRepeat_ = paint.patternRepeat_; -#endif -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - shadowBlurRadius_ = paint.shadowBlurRadius_; - shadowOffsetX_ = paint.shadowOffsetX_; - shadowOffsetY_ = paint.shadowOffsetY_; - shadowColor_ = paint.shadowColor_; - haveShadow_ = paint.haveShadow_; -#endif - globalAlpha_ = paint.globalAlpha_; - globalCompositeOperation_ = paint.globalCompositeOperation_; - rotateAngle_ = paint.rotateAngle_; - transfrom_ = paint.transfrom_; - haveComposite_ = paint.haveComposite_; - uiCanvas_ = paint.uiCanvas_; - } - - /** - * @brief A destructor used to delete the Paint instance. - * - * @since 1.0 - * @version 1.0 - */ - virtual ~Paint() {} - - const Paint& operator=(const Paint& paint) - { - Init(paint); - return *this; - } - /** - * @brief Enumerates paint styles of a closed graph. The styles are invalid for non-closed graphs. - */ - enum PaintStyle { - /** Stroke only */ - STROKE_STYLE = 1, - /** Fill only */ - FILL_STYLE, - /** Stroke and fill */ - STROKE_FILL_STYLE, - /** Gradual change */ - GRADIENT, - /** Image mode */ - PATTERN - }; - - struct LinearGradientPoint { - /** Start point coordinate x */ - float x0; - /** Start point coordinate y */ - float y0; - /** End point coordinate x */ - float x1; - /** End point coordinate y */ - float y1; - }; - - struct RadialGradientPoint { - /** Start dot coordinate x */ - float x0; - /** Start dot coordinate y */ - float y0; - /** Start circle radius r0 */ - float r0; - /** End dot coordinates x */ - float x1; - /** End dot coordinates y */ - float y1; - /** Start circle radius r0 */ - float r1; - }; - - struct StopAndColor { - /** Values between 0.0 and 1.0 represent the position between the beginning and end of the ramp. */ - float stop; - /** The color value displayed at the end */ - ColorType color; - }; - enum Gradient { - Linear, - Radial - }; - - /** - * @brief Sets the paint style of a closed graph. - * - * @param style Indicates the paint style. Stroke and fill are set by default. - * For details, see {@link PaintStyle}. - * @see GetStyle - * @since 1.0 - * @version 1.0 - */ - void SetStyle(PaintStyle style) - { - style_ = style; - } - - /** - * @brief Sets the paint style. - * - * @param color value. - * @since 1.0 - * @version 1.0 - */ - void SetStrokeStyle(ColorType color) - { - SetStyle(Paint::STROKE_STYLE); - SetStrokeColor(color); - } - - /** - * @brief Sets fill style. - * - * @param color value. - * @since 1.0 - * @version 1.0 - */ - void SetFillStyle(ColorType color) - { - SetStyle(Paint::FILL_STYLE); - SetFillColor(color); - } - - /** - * @brief Sets the paint stroke style of a closed graph. - * - * @param style Indicates the paint style. Stroke and fill are set by default. - * @since 1.0 - * @version 1.0 - */ - void SetStrokeStyle(PaintStyle style) - { - SetStyle(style); - } - - /** - * @brief Sets the paint fill style of a closed graph. - * - * @param style Indicates the paint style. Stroke and fill are set by default. - * @since 1.0 - * @version 1.0 - */ - void SetFillStyle(PaintStyle style) - { - SetStyle(style); - } - - /** - * @brief Obtains the paint style of a closed graph. - * - * @return Returns the paint style. For details, see {@link PaintStyle}. - * @see SetStyle - * @since 1.0 - * @version 1.0 - */ - PaintStyle GetStyle() const - { - return style_; - } - - /** - * @brief Sets the width of a line or border. - * - * @param width Indicates the line width when a line is drawn or the border width when a closed graph is drawn. - * The width is extended to both sides. - * @see GetStrokeWidth - * @since 1.0 - * @version 1.0 - */ - void SetStrokeWidth(uint16_t width) - { - strokeWidth_ = width; - } - - /** - * @brief Obtains the width of a line or border. - * - * @return Returns the line width if a line is drawn or the border width if a closed graph is drawn. - * @see SetStrokeWidth - * @since 1.0 - * @version 1.0 - */ - uint16_t GetStrokeWidth() const - { - return strokeWidth_; - } - - /** - * @brief Sets the color of a line or border. - * - * @param color Indicates the line color when a line is drawn or the border color when a closed graph is drawn. - * @see GetStrokeColor - * @since 1.0 - * @version 1.0 - */ - void SetStrokeColor(ColorType color) - { - strokeColor_ = color; - changeFlag_ = true; - } - - /** - * @brief Obtains the color of a line or border. - * - * @return Returns the line color if a line is drawn or the border color if a closed graph is drawn. - * @see SetStrokeWidth - * @since 1.0 - * @version 1.0 - */ - ColorType GetStrokeColor() const - { - return strokeColor_; - } - - /** - * @brief Sets fill color. - * - * This function is valid only for closed graphs. - * - * @param color Indicates the fill color to set. - * @see GetFillColor - * @since 1.0 - * @version 1.0 - */ - void SetFillColor(ColorType color) - { - fillColor_ = color; - changeFlag_ = true; - } - - /** - * @brief Obtains the fill color. - * - * @return Returns the fill color. - * @see SetFillColor - * @since 1.0 - * @version 1.0 - */ - ColorType GetFillColor() const - { - return fillColor_; - } - - /** - * @brief Sets the opacity. - * - * The setting takes effect for the entire graph, including the border, line color, and fill color. - * - * @param opacity Indicates the opacity. The value range is [0, 255]. - * @see GetOpacity - * @since 1.0 - * @version 1.0 - */ - void SetOpacity(uint8_t opacity) - { - opacity_ = opacity; - } - - /** - * @brief Obtains the opacity. - * - * @return Returns the opacity. - * @see SetOpacity - * @since 1.0 - * @version 1.0 - */ - uint8_t GetOpacity() const - { - return opacity_; - } - - bool GetChangeFlag() const - { - return changeFlag_; - } -#if GRAPHIC_ENABLE_LINECAP_FLAG - /** - * @brief Sets the cap type. - * @see GetLineCap - * @since 1.0 - * @version 1.0 - */ - void SetLineCap(LineCap lineCap) - { - lineCap_ = lineCap; - changeFlag_ = true; - } -#endif - -#if GRAPHIC_ENABLE_LINECAP_FLAG - /** - * @brief Gets the cap type. - * @see SetLineCap - * @since 1.0 - * @version 1.0 - */ - LineCap GetLineCap() const - { - return lineCap_; - } -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - /** - * @brief Sets the style at the path connection of the pen. - * @see GetLineJoin - * @since 1.0 - * @version 1.0 - */ - void SetLineJoin(LineJoin lineJoin) - { - lineJoin_ = lineJoin; - changeFlag_ = true; - } -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - /** - * @brief Sets the spacing limit for sharp corners at path connections. - * @see GetMiterLimit - * @since 1.0 - * @version 1.0 - */ - void SetMiterLimit(float miterLimit) - { - miterLimit_ = miterLimit; - changeFlag_ = true; - } -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - float GetMiterLimit() const - { - return miterLimit_; - } -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - /** - * @brief Gets the style at the path connection of the pen. - * @see SetLineJoin - * @since 1.0 - * @version 1.0 - */ - LineJoin GetLineJoin() const - { - return lineJoin_; - } -#endif -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - bool IsLineDash() const - { - return isDashMode_; - } - - /** - * @brief Sets the array and number of dashes. - * @param lineDashs Represents an array of dotted lines,ndash Indicates the number of dotted lines - * @since 1.0 - * @version 1.0 - */ - void SetLineDash(float* lineDashs, const uint32_t ndash) - { - ClearLineDash(); - if (lineDashs == nullptr || ndash == 0) { - return; - } - ndashes_ = ndash; - isDashMode_ = true; - dashArray_ = new float[ndashes_]; - if (dashArray_) { - if (memset_s(dashArray_, ndashes_ * sizeof(float), 0, ndashes_ * sizeof(float)) != EOF) { - } - for (uint32_t iIndex = 0; iIndex < ndashes_; iIndex++) { - dashArray_[iIndex] = lineDashs[iIndex]; - } - } else { - ndashes_ = 0; - dashOffset_ = 0; - isDashMode_ = false; - } - changeFlag_ = true; - } - - /** - * @brief Get dash array - * @return - */ - float* GetLineDash() const - { - return dashArray_; - } - - float GetLineDashOffset() const - { - return dashOffset_; - } - - /** - * @brief Sets the offset of the dash mode start point - * @see GetLineDashOffset - * @since 1.0 - * @version 1.0 - */ - void SetLineDashOffset(float offset) - { - dashOffset_ = offset; - changeFlag_ = true; - isDashMode_ = true; - } - - /** - * @brief Get dash array length - * @return - */ - uint32_t GetLineDashCount() const - { - return ndashes_; - } - - /** - * @brief Empty the dotted line and draw it instead. - * @since 1.0 - * @version 1.0 - */ - void ClearLineDash(void) - { - dashOffset_ = 0; - ndashes_ = 0; - isDashMode_ = false; - if (dashArray_ != nullptr) { - delete[] dashArray_; - dashArray_ = nullptr; - } - } -#endif -#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG - void createLinearGradient(float startx, float starty, float endx, float endy) - { - gradientflag_ = Linear; - linearGradientPoint_.x0 = startx; - linearGradientPoint_.y0 = starty; - linearGradientPoint_.x1 = endx; - linearGradientPoint_.y1 = endy; - changeFlag_ = true; - } - - void addColorStop(float stop, ColorType color) - { - StopAndColor stopAndColor; - stopAndColor.stop = stop; - stopAndColor.color = color; - stopAndColors_.PushBack(stopAndColor); - } - - void createRadialGradient(float start_x, float start_y, float start_r, float end_x, float end_y, - float end_r) - { - gradientflag_ = Radial; - radialGradientPoint_.x0 = start_x; - radialGradientPoint_.y0 = start_y; - radialGradientPoint_.r0 = start_r; - radialGradientPoint_.x1 = end_x; - radialGradientPoint_.y1 = end_y; - radialGradientPoint_.r1 = end_r; - changeFlag_ = true; - } - - List getStopAndColor() const - { - return stopAndColors_; - } - - LinearGradientPoint GetLinearGradientPoint() const - { - return linearGradientPoint_; - } - - RadialGradientPoint GetRadialGradientPoint() const - { - return radialGradientPoint_; - } - - Gradient GetGradient() const - { - return gradientflag_; - } -#endif -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG - /* - * Set hatch patterns for elements - * @param img Represents the pattern of the hatch,text Represents a fill pattern - */ - void CreatePattern(const char* img, PatternRepeatMode patternRepeat) - { - image_ = img; - patternRepeat_ = patternRepeat; - changeFlag_ = true; - } - - const char* GetPatternImage() const - { - return image_; - } - - PatternRepeatMode GetPatternRepeatMode() const - { - return patternRepeat_; - } -#endif - -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - /** - * @brief Sets the shadow blur level. - * @since 1.0 - * @version 1.0 - */ - void SetShadowBlur(uint16_t radius) - { - shadowBlurRadius_ = radius; - changeFlag_ = true; - } - - /** - * @brief Gets the shadow blur level. - * @since 1.0 - * @version 1.0 - */ - uint16_t GetShadowBlur() const - { - return shadowBlurRadius_; - } - - /** - * @brief Gets the abscissa offset of the shadow. - * @since 1.0 - * @version 1.0 - */ - float GetShadowOffsetX() const - { - return shadowOffsetX_; - } - /** - * @brief Sets the abscissa offset of the shadow. - * @since 1.0 - * @version 1.0 - */ - void SetShadowOffsetX(float offset) - { - shadowOffsetX_ = offset; - changeFlag_ = true; - } - /** - * @brief Gets the shadow ordinate offset. - * @since 1.0 - * @version 1.0 - */ - float GetShadowOffsetY() const - { - return shadowOffsetY_; - } - /** - * @brief Sets the shadow ordinate offset. - * @since 1.0 - * @version 1.0 - */ - void SetShadowOffsetY(float offset) - { - shadowOffsetY_ = offset; - changeFlag_ = true; - } - /** - * @brief Gets the color value of the shadow. - * @since 1.0 - * @version 1.0 - */ - ColorType GetShadowColor() const - { - return shadowColor_; - } - /** - * @brief Sets the color value of the shadow. - * @since 1.0 - * @version 1.0 - */ - void SetShadowColor(ColorType color) - { - shadowColor_ = color; - changeFlag_ = true; - haveShadow_ = true; - } - bool HaveShadow() const - { - return haveShadow_; - } -#endif - /** - * @brief Sets the alpha of the current drawing. - */ - void SetGlobalAlpha(float alphaPercentage) - { - if (alphaPercentage > 1) { - globalAlpha_ = 1.0; - return; - } - if (alphaPercentage < 0) { - globalAlpha_ = 0.0; - return; - } - globalAlpha_ = alphaPercentage; - changeFlag_ = true; - } - - /** - * @brief get the alpha of the current drawing - * @return Returns the alpha of the current drawing - * @since 1.0 - * @version 1.0 - */ - float GetGlobalAlpha() const - { - return globalAlpha_; - } - - /** - * @brief Set blend mode - */ - void SetGlobalCompositeOperation(GlobalCompositeOperation globalCompositeOperation) - { - globalCompositeOperation_ = globalCompositeOperation; - changeFlag_ = true; - if (globalCompositeOperation != SOURCE_OVER) { - haveComposite_ = true; - } - } - - /** - * @brief Get blend mode - */ - GlobalCompositeOperation GetGlobalCompositeOperation() const - { - return globalCompositeOperation_; - } - - /* Zooms the current drawing to a larger or smaller size */ - void Scale(float scaleX, float scaleY) - { - this->scaleRadioX_ *= scaleX; - this->scaleRadioY_ *= scaleX; - if (rotateAngle_ > 0.0 || rotateAngle_ < 0) { - transfrom_.Rotate(-rotateAngle_ * PI / BOXER); - transfrom_.Scale(scaleX, scaleY); - transfrom_.Rotate(rotateAngle_ * PI / BOXER); - } else { - transfrom_.Scale(scaleX, scaleY); - } - changeFlag_ = true; - } - - /** - * @brief get the x coordinate scale value - * @since 1.0 - * @version 1.0 - */ - float GetScaleX() const - { - return this->scaleRadioX_; - } - - /** - * @brief get the y coordinate scale value - * @since 1.0 - * @version 1.0 - */ - float GetScaleY() const - { - return this->scaleRadioY_; - } - - /** - * @brief Rotate current drawing - * @param angle rotate angle value. - * @since 1.0 - * @version 1.0 - */ - void Rotate(float angle) - { - changeFlag_ = true; - transfrom_.Rotate(angle * PI / BOXER); - rotateAngle_ += angle; - } - - /** - * @brief Rotate current drawing - * @param angle rotate angle value. - * @param x translate x coordinate. - * @param y translate y coordinate. - * @since 1.0 - * @version 1.0 - */ - void Rotate(float angle, int16_t x, int16_t y) - { - transfrom_.Translate(-x, -y); - transfrom_.Rotate(angle * PI / BOXER); - rotateAngle_ += angle; - transfrom_.Translate(x, y); - changeFlag_ = true; - } - - /** - * @brief Remap the (x, y) position on the canvas - * @param x translate x coordinate. - * @param y translate y coordinate. - * @since 1.0 - * @version 1.0 - */ - void Translate(int16_t x, int16_t y) - { - changeFlag_ = true; - transfrom_.Translate(x, y); - this->translationX_ += x; - this->translationY_ += y; - } - - /** - * @brief Gets the x position on the remapping canvas - * @since 1.0 - * @version 1.0 - */ - int16_t GetTranslateX() const - { - return this->translationX_; - } - - /** - * @brief Gets the Y position on the remapping canvas - * @since 1.0 - * @version 1.0 - */ - int16_t GetTranslateY() const - { - return this->translationY_; - } - - /** - * @brief Resets the current conversion to the identity matrix. Then run transform () - * @param scaleX scale x value. - * @param shearX shear x value. - * @param shearY shear y value. - * @param scaleY scale y value - * @param transLateX translate x coordinate. - * @param transLateY translate y coordinate. - * @since 1.0 - * @version 1.0 - */ - void SetTransform(float scaleX, float shearX, float shearY, float scaleY, - int16_t transLateX, int16_t transLateY) - { - transfrom_.Reset(); - rotateAngle_ = 0; - Transform(scaleX, shearX, shearY, scaleY, transLateX, transLateY); - changeFlag_ = true; - } - - /** - * @brief Resets the current conversion to the identity matrix. Then run transform () - * @param scaleX scale x value. - * @param shearX shear x value. - * @param shearY shear y value. - * @param scaleY scale y value - * @param transLateX translate x coordinate. - * @param transLateY translate y coordinate. - * @since 1.0 - * @version 1.0 - */ - void Transform(float scaleX, float shearX, float shearY, float scaleY, int16_t transLateX, int16_t transLateY) - { - changeFlag_ = true; - this->translationX_ += transLateX; - this->translationY_ += transLateY; - transLateX += transfrom_.GetData()[2]; - transLateY += transfrom_.GetData()[5]; - transfrom_.Translate(-transfrom_.GetData()[2], -transfrom_.GetData()[5]); - Scale(scaleX, scaleY); - transfrom_.Translate(transLateX, transLateY); - transfrom_.SetData(1, transfrom_.GetData()[1] + shearX); - transfrom_.SetData(3, transfrom_.GetData()[3] + shearY); - } - - /** - * @brief Gets the Trans Affine - * @since 1.0 - * @version 1.0 - */ - TransAffine GetTransAffine() const - { - return transfrom_; - } - - /** - * @brief Gets the Rotate Angle - * @since 1.0 - * @version 1.0 - */ - float GetRotateAngle() const - { - return rotateAngle_; - } - - void SetUICanvas(UICanvas* uiCanvas) - { - this->uiCanvas_ = uiCanvas; - } - - UICanvas* GetUICanvas() const - { - return uiCanvas_; - } - - bool HaveComposite() const - { - return haveComposite_; - } - -private: - PaintStyle style_; - ColorType fillColor_; - ColorType strokeColor_; - uint8_t opacity_; - uint16_t strokeWidth_; - bool changeFlag_; -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - LineJoin lineJoin_; -#endif - -#if GRAPHIC_ENABLE_LINECAP_FLAG - LineCap lineCap_; -#endif -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - bool isDashMode_; // Is it a dash mode segment. - float dashOffset_; // dash Point offset. - float* dashArray_; // dash Point array. - uint32_t ndashes_; // Length of dasharray -#endif -#if GRAPHIC_ENABLE_LINEJOIN_FLAG - float miterLimit_; // Sets the spacing limit for sharp corners at path connections -#endif -#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG - LinearGradientPoint linearGradientPoint_; - RadialGradientPoint radialGradientPoint_; - List stopAndColors_; - Gradient gradientflag_; -#endif -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG - PatternRepeatMode patternRepeat_; -#endif -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG - const char* image_; -#endif -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - uint16_t shadowBlurRadius_; // Sets the shadow blur radius. - float shadowOffsetX_; // Sets the abscissa offset of the shadow. - float shadowOffsetY_; // Sets the shadow ordinate offset. - ColorType shadowColor_; // Set shadow color. - bool haveShadow_; // Is there a shadow currently. -#endif - float globalAlpha_; // The transparency of the current drawing is 0-1 percent - GlobalCompositeOperation globalCompositeOperation_; // Mixed image mode - float rotateAngle_; // Rotation angle in degrees - float scaleRadioX_; - float scaleRadioY_; - int32_t translationX_; - int32_t translationY_; - TransAffine transfrom_; // matrix. - bool haveComposite_; - UICanvas* uiCanvas_; -}; /** * @brief Defines a canvas, which is used to draw multiple types of 2D graphs. @@ -1077,7 +72,7 @@ public: * @since 1.0 * @version 1.0 */ - UICanvas() : startPoint_({0, 0}), vertices_(nullptr), gfxMapBuffer_(nullptr) {} + UICanvas() : startPoint_({0, 0}), vertices_(nullptr){} /** * @brief A destructor used to delete the UICanvas instance. @@ -1254,7 +249,7 @@ public: * Only stroke is supported. \n * When the start angle is smaller than the end angle, the sector is drawn clockwise. * Otherwise, the sector is drawn counterclockwise. \n - * + * * @param center Indicates the coordinates of the arc's center. * @param radius Indicates the radius of the arc. * @param startAngle Indicates the start angle of the arc. Value 0 indicates the 12-o'clock direction, @@ -1284,7 +279,7 @@ public: /** * @brief Defines the font style. */ - struct FontStyle { + struct FontStyle : public HeapBase { /** Text direction. For details, see {@link UITextLanguageDirect}. */ UITextLanguageDirect direct; /** Text alignment mode. For details, see {@link UITextLanguageAlignment}. */ @@ -1297,13 +292,11 @@ public: const char* fontName; }; - struct ImageParam : public HeapBase { - Point start; - uint16_t height; - uint16_t width; - int16_t newWidth; - int16_t newHeight; - Image* image = nullptr; + struct DrawCmd : public HeapBase { + Paint paint; + void* param; + void (*DrawGraphics)(BufferInfo&, void*, const Paint&, const Rect&, const Rect&, const Style&); + void (*DeleteParam)(void*); }; /** @@ -1440,24 +433,15 @@ public: void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; - static void RenderBlendSolid(const Paint& paint, - Rgba8T& color, - const bool& isStroke) - { - if (isStroke) { - if (paint.GetStyle() == Paint::STROKE_STYLE || - paint.GetStyle() == Paint::STROKE_FILL_STYLE) { - ChangeColor(color, paint.GetStrokeColor(), - static_cast(paint.GetStrokeColor().alpha * paint.GetGlobalAlpha())); - } - } else { - if (paint.GetStyle() == Paint::FILL_STYLE || - paint.GetStyle() == Paint::STROKE_FILL_STYLE) { - ChangeColor(color, paint.GetFillColor(), - static_cast(paint.GetFillColor().alpha * paint.GetGlobalAlpha())); - } - } - } + static void BlendRaster(const Paint& paint, + void* param, + RasterizerScanlineAntialias& blendRasterizer, + RasterizerScanlineAntialias& rasterizer, + RenderBase& renBase, + TransAffine& transform, + SpanBase& spanGen, + const Rect& rect, + bool isStroke); protected: constexpr static uint8_t MAX_CURVE_WIDTH = 3; @@ -1491,12 +475,6 @@ protected: int16_t endAngle; }; - struct PathParam : public HeapBase { - UICanvasVertices* vertices; - ImageParam* imageParam = nullptr; - bool isStroke; - }; - struct TextParam : public HeapBase { const char* text; Point position; @@ -1518,26 +496,13 @@ protected: }; }; - struct DrawCmd : public HeapBase { - Paint paint; - void* param; - void (*DrawGraphics)(BufferInfo&, void*, const Paint&, const Rect&, const Rect&, const Style&); - void (*DeleteParam)(void*); - }; - Point startPoint_; UICanvasVertices* vertices_; List drawCmdList_; // Save historical modification information of paint List paintStack_; - BufferInfo* gfxMapBuffer_; -#if GRAPHIC_ENABLE_BLUR_EFFECT_FLAG - Filterblur drawBlur; - Filterblur GetDrawBoxBlur() const - { - return drawBlur; - } -#endif + static BufferInfo* gfxMapBuffer_; + static void DeleteLineParam(void* param) { LineParam* lineParam = static_cast(param); @@ -1568,16 +533,6 @@ protected: delete arcParam; } - static void DeleteImageParam(void* param) - { - ImageParam* imageParam = static_cast(param); - if (imageParam->image != nullptr) { - delete imageParam->image; - imageParam->image = nullptr; - } - delete imageParam; - } - static void DeleteLabel(void* param) { UILabel* label = static_cast(param); @@ -1590,20 +545,6 @@ protected: delete imageView; } - static void DeletePathParam(void* param) - { - PathParam* pathParam = static_cast(param); - if (pathParam->vertices != nullptr) { - pathParam->vertices->FreeAll(); - pathParam->vertices = nullptr; - } - if (pathParam->imageParam != nullptr) { - DeleteImageParam(pathParam->imageParam); - } - delete pathParam; - pathParam = nullptr; - } - static void DeleteTextParam(void* param) { TextParam* textParam = static_cast(param); @@ -1680,28 +621,6 @@ protected: const Rect& invalidatedArea, const Style& style); - static void SetRasterizer(UICanvasVertices& vertices, - const Paint& paint, - RasterizerScanlineAntialias& rasterizer, - TransAffine& transform, - const bool& isStroke); - - static void DoRender(BufferInfo& gfxDstBuffer, - void* param, - const Paint& paint, - const Rect& rect, - const Rect& invalidatedArea, - const Style& style, - const bool& isStroke); -#if GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG - static void DoDrawShadow(BufferInfo& gfxDstBuffer, - void* param, - const Paint& paint, - const Rect& rect, - const Rect& invalidatedArea, - const Style& style, - const bool& isStroke); -#endif static void BlitMapBuffer(BufferInfo &gfxDstBuffer, BufferInfo& gfxMapBuffer, Rect& textRect, TransformMap& transMap, const Rect& invalidatedArea); @@ -1726,18 +645,6 @@ protected: } #endif }; -#if GRAPHIC_ENABLE_DASH_GENERATE_FLAG - /** - * Set linedash style - */ - static void LineDashStyleCalc(DepictDash& dashStyle, const Paint& paint) - { - for (uint32_t i = 0; i < paint.GetLineDashCount(); i += TWO_STEP) { - dashStyle.AddDash(paint.GetLineDash()[i], paint.GetLineDash()[i + 1]); - } - dashStyle.DashStart(paint.GetLineDashOffset()); - }; -#endif static bool IsSoild(const Paint& paint) { @@ -1749,94 +656,11 @@ protected: return false; } -#if GRAPHIC_ENABLE_GRADIENT_FILL_FLAG - /** - * Render gradient - */ - static void RenderGradient(const Paint& paint, - RasterizerScanlineAntialias& rasterizer, - TransAffine& transform, - RenderBase& renBase, - RenderBuffer& renderBuffer, - FillBase& allocator, - const Rect& invalidatedArea); - - static void BuildGradientColor(const Paint& paint, FillGradientLut& gradientColorMode); - - static void BuildLineGradientMatrix(const Paint& paint, - TransAffine& gradientMatrix, - TransAffine& transform, - float& distance) - { - Paint::LinearGradientPoint linearPoint = paint.GetLinearGradientPoint(); - float angle = FastAtan2F(linearPoint.y1 - linearPoint.y0, linearPoint.x1 - linearPoint.x0); - gradientMatrix.Reset(); - gradientMatrix *= TransAffine::TransAffineRotation(angle); - gradientMatrix *= TransAffine::TransAffineTranslation(linearPoint.x0, linearPoint.y0); - gradientMatrix *= transform; - gradientMatrix.Invert(); - distance = Sqrt((linearPoint.x1 - linearPoint.x0) * (linearPoint.x1 - linearPoint.x0) + - (linearPoint.y1 - linearPoint.y0) * (linearPoint.y1 - linearPoint.y0)); - } - - static void BuildRadialGradientMatrix(const Paint& paint, - TransAffine& gradientMatrix, - TransAffine& transform, - float& startRadius, - float& endRadius); -#endif - - static void BlendRaster(const Paint& paint, - void* param, - RasterizerScanlineAntialias& blendRasterizer, - RasterizerScanlineAntialias& rasterizer, - RenderBase& renBase, - TransAffine& transform, - SpanBase& spanGen, - const Rect& rect, - bool isStroke); - -#if GRAPHIC_ENABLE_PATTERN_FILL_FLAG - /** - * Render pattern mode - */ - static void RenderPattern(const Paint& paint, - void* param, - RasterizerScanlineAntialias& rasterizer, - RenderBase& renBase, - FillBase& allocator, - const Rect& rect); -#endif - - static void ChangeColor(Rgba8T& color, ColorType colorType, uint8_t alpha) - { - color.red = colorType.red; - color.green = colorType.green; - color.blue = colorType.blue; - color.alpha = alpha; - } - void DrawRectSetCmd(const Point& startPoint, int16_t height, int16_t width, const Paint& paint, Paint::PaintStyle paintStyle); - void InitGfxMapBuffer(const BufferInfo& srcBuff, const Rect& rect); - BufferInfo* UpdateMapBufferInfo(const BufferInfo& srcBuff, const Rect& rect); - void DestroyMapBufferInfo(); - - void OnBlendDrawPattern(ListNode* curDraw, DrawCmd& drawCmd, - Rect& rect, const Rect& trunc, - RasterizerScanlineAntialias& blendRasterizer, - RasterizerScanlineAntialias& rasterizer, - RenderBase& renBase, - TransAffine& transform, - PathParam* pathParamBlend); - - void OnBlendDrawGradient(ListNode* curDraw, DrawCmd& drawCmd, - const Rect& trunc, - RasterizerScanlineAntialias& blendRasterizer, - RasterizerScanlineAntialias& rasterizer, - RenderBase& renBase, - TransAffine& transform, - PathParam* pathParamBlend); + static void InitGfxMapBuffer(const BufferInfo& srcBuff, const Rect& rect); + static BufferInfo* UpdateMapBufferInfo(const BufferInfo& srcBuff, const Rect& rect); + static void DestroyMapBufferInfo(); }; } // namespace OHOS #endif // GRAPHIC_LITE_UI_CANVAS_H diff --git a/test/unittest/components/ui_canvas_unit_test.cpp b/test/unittest/components/ui_canvas_unit_test.cpp index 8d1c50788b94bc0caee105a11135ac44090f9912..d13801071677b2b72b86e54da39faaae2d965298 100644 --- a/test/unittest/components/ui_canvas_unit_test.cpp +++ b/test/unittest/components/ui_canvas_unit_test.cpp @@ -2147,6 +2147,7 @@ HWTEST_F(UICanvasTest, UICanvasSetRotate_002, TestSize.Level1) * @tc.name: UICanvasInitDash_001 * @tc.desc: Verify InitDash function, equal. * @tc.type: FUNC + * @tc.require: AR000H8BAB */ HWTEST_F(UICanvasTest, UICanvasInitDash_001, TestSize.Level0) { @@ -2183,8 +2184,6 @@ HWTEST_F(UICanvasTest, UICanvasInitDash_001, TestSize.Level0) EXPECT_EQ(paint1->GetStyle(), Paint::PaintStyle::STROKE_FILL_STYLE); paint1->GetTransAffine(); - paint1->SetUICanvas(canvas_); - EXPECT_EQ(paint1->GetUICanvas(), canvas_); EXPECT_EQ(paint1->HaveComposite(), false); delete paint1; diff --git a/tools/qt/simulator/libui/libui.pro b/tools/qt/simulator/libui/libui.pro index d58dc3e1870c4aed9750a69172a9983834cf9284..c359c00eeac91ad9605c271e3268dfb0bb516502 100644 --- a/tools/qt/simulator/libui/libui.pro +++ b/tools/qt/simulator/libui/libui.pro @@ -126,6 +126,7 @@ SOURCES += \ ../../../../frameworks/engines/gfx/gfx_engine_manager.cpp \ ../../../../frameworks/draw/clip_utils.cpp \ ../../../../frameworks/draw/draw_arc.cpp \ + ../../../../frameworks/draw/draw_canvas.cpp \ ../../../../frameworks/draw/draw_curve.cpp \ ../../../../frameworks/draw/draw_image.cpp \ ../../../../frameworks/draw/draw_label.cpp \ @@ -181,6 +182,7 @@ HEADERS += \ ../../../../../utils/interfaces/kits/gfx_utils/diagram/common/common_basics.h \ ../../../../../utils/interfaces/kits/gfx_utils/diagram/common/common_clip_operate.h \ ../../../../../utils/interfaces/kits/gfx_utils/diagram/common/common_math.h \ + ../../../../../utils/interfaces/kits/gfx_utils/diagram/common/paint.h \ ../../../../../utils/interfaces/kits/gfx_utils/diagram/depiction/depict_adaptor_vertex_generate.h \ ../../../../../utils/interfaces/kits/gfx_utils/diagram/depiction/depict_curve.h \ ../../../../../utils/interfaces/kits/gfx_utils/diagram/depiction/depict_dash.h \ @@ -221,6 +223,7 @@ HEADERS += \ ../../../../frameworks/dock/virtual_input_device.h \ ../../../../frameworks/draw/clip_utils.h \ ../../../../frameworks/draw/draw_arc.h \ + ../../../../frameworks/draw/draw_canvas.h \ ../../../../frameworks/draw/draw_curve.h \ ../../../../frameworks/draw/draw_image.h \ ../../../../frameworks/draw/draw_label.h \ diff --git a/ui.gni b/ui.gni index 1f9c09be1815a5ac45debe003c4ddb72cd5cacb4..b336c78698864a8c076c2e18782bc24330bdee56 100755 --- a/ui.gni +++ b/ui.gni @@ -88,6 +88,7 @@ graphic_ui_sources = [ "$GRAPHIC_UI_PATH/frameworks/dock/virtual_input_device.cpp", "$GRAPHIC_UI_PATH/frameworks/draw/clip_utils.cpp", "$GRAPHIC_UI_PATH/frameworks/draw/draw_arc.cpp", + "$GRAPHIC_UI_PATH/frameworks/draw/draw_canvas.cpp", "$GRAPHIC_UI_PATH/frameworks/draw/draw_curve.cpp", "$GRAPHIC_UI_PATH/frameworks/draw/draw_image.cpp", "$GRAPHIC_UI_PATH/frameworks/draw/draw_label.cpp",