From 9bed64a4e74d85a54ecd601b5c6f901ec4d4ba58 Mon Sep 17 00:00:00 2001 From: RuiwenTang Date: Wed, 3 Jul 2024 20:53:31 +0800 Subject: [PATCH] gl_engine: support simple hairline stroke rendering Reduce alpha if stroke width is too thin to mock hairline rendering --- src/renderer/gl_engine/tvgGlGeometry.h | 1 + src/renderer/gl_engine/tvgGlRenderer.cpp | 21 +++++++++++++++++++-- src/renderer/gl_engine/tvgGlTessellator.h | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/renderer/gl_engine/tvgGlGeometry.h b/src/renderer/gl_engine/tvgGlGeometry.h index 9f4678a9..97fee915 100644 --- a/src/renderer/gl_engine/tvgGlGeometry.h +++ b/src/renderer/gl_engine/tvgGlGeometry.h @@ -27,6 +27,7 @@ #include "tvgGlCommon.h" #include "tvgMath.h" +#define MIN_GL_STROKE_WIDTH 0.5f #define MVP_MATRIX(w, h) \ float mvp[4*4] = { \ diff --git a/src/renderer/gl_engine/tvgGlRenderer.cpp b/src/renderer/gl_engine/tvgGlRenderer.cpp index a40c390b..640bfb79 100644 --- a/src/renderer/gl_engine/tvgGlRenderer.cpp +++ b/src/renderer/gl_engine/tvgGlRenderer.cpp @@ -643,6 +643,14 @@ void GlRenderer::drawPrimitive(GlShape& sdata, uint8_t r, uint8_t g, uint8_t b, a = MULTIPLY(a, sdata.opacity); + if (flag & RenderUpdateFlag::Stroke) { + float strokeWidth = sdata.rshape->strokeWidth(); + if (strokeWidth < MIN_GL_STROKE_WIDTH) { + float alpha = strokeWidth / MIN_GL_STROKE_WIDTH; + a = MULTIPLY(a, static_cast(alpha * 255)); + } + } + // matrix buffer { const auto& matrix = sdata.geometry->getTransformMatrix(); @@ -800,6 +808,15 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, RenderUpdateFla }); } + float alpha = 1.0f; + + if (flag & RenderUpdateFlag::GradientStroke) { + float strokeWidth = sdata.rshape->strokeWidth(); + if (strokeWidth < MIN_GL_STROKE_WIDTH) { + alpha = strokeWidth / MIN_GL_STROKE_WIDTH; + } + } + // gradient block { GlBindingResource gradientBinding{}; @@ -820,7 +837,7 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, RenderUpdateFla gradientBlock.stopColors[i * 4 + 0] = stops[i].r / 255.f; gradientBlock.stopColors[i * 4 + 1] = stops[i].g / 255.f; gradientBlock.stopColors[i * 4 + 2] = stops[i].b / 255.f; - gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f; + gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f * alpha; nStops++; } gradientBlock.nStops[0] = nStops * 1.f; @@ -856,7 +873,7 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, RenderUpdateFla gradientBlock.stopColors[i * 4 + 0] = stops[i].r / 255.f; gradientBlock.stopColors[i * 4 + 1] = stops[i].g / 255.f; gradientBlock.stopColors[i * 4 + 2] = stops[i].b / 255.f; - gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f; + gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f * alpha; nStops++; } gradientBlock.nStops[0] = nStops * 1.f; diff --git a/src/renderer/gl_engine/tvgGlTessellator.h b/src/renderer/gl_engine/tvgGlTessellator.h index d2d94771..20cfe759 100644 --- a/src/renderer/gl_engine/tvgGlTessellator.h +++ b/src/renderer/gl_engine/tvgGlTessellator.h @@ -148,7 +148,7 @@ private: Array* mResGlPoints; Array* mResIndices; Matrix mMatrix; - float mStrokeWidth = 1.f; + float mStrokeWidth = MIN_GL_STROKE_WIDTH; float mMiterLimit = 4.f; StrokeCap mStrokeCap = StrokeCap::Square; StrokeJoin mStrokeJoin = StrokeJoin::Bevel;