Add strokes, gradients, and blend modes to path_fuzz Diffs= 7d03c3faf Add strokes, gradients, and blend modes to path_fuzz (#7230) Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index 7af1aa1..9faac26 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -4342a3f049632b640b5522c1fabf7aef781423af +7d03c3fafeefd05513c4a418f320322ecd1c1e4e
diff --git a/renderer/pls_draw.cpp b/renderer/pls_draw.cpp index d311117..b37911c 100644 --- a/renderer/pls_draw.cpp +++ b/renderer/pls_draw.cpp
@@ -123,8 +123,8 @@ // If the cusps are extremely close together, don't allow the straddle points to cross. float minT = i == 0 ? 0.f : (cuspT[i - 1] + cuspT[i]) * .5f; float maxT = i + 1 == n ? 1.f : (cuspT[i + 1] + cuspT[i]) * .5f; - t[i * 2 + 0] = std::max(cuspT[i] - math::EPSILON, minT); - t[i * 2 + 1] = std::min(cuspT[i] + math::EPSILON, maxT); + t[i * 2 + 0] = fmaxf(cuspT[i] - math::EPSILON, minT); + t[i * 2 + 1] = fminf(cuspT[i] + math::EPSILON, maxT); } pathutils::ChopCubicAt(p, dst, t, n * 2); for (int i = 0; i < n; ++i) @@ -418,7 +418,7 @@ m_drawContents |= pls::DrawContents::stroke; m_strokeRadius = paint->getThickness() * .5f; // Ensure stroke radius is nonzero. (In PLS, zero radius means the path is filled.) - m_strokeRadius = std::max(m_strokeRadius, std::numeric_limits<float>::min()); + m_strokeRadius = fmaxf(m_strokeRadius, std::numeric_limits<float>::min()); assert(!std::isnan(m_strokeRadius)); // These should get culled in PLSRenderer::drawPath(). assert(m_strokeRadius > 0); } @@ -934,21 +934,23 @@ if (cap == StrokeCap::round) { // Round caps rotate 180 degrees. - contour->strokeCapSegmentCount = ceilf(polarSegmentsPerRad * math::PI); + float strokeCapSegmentCount = ceilf(polarSegmentsPerRad * math::PI); // +2 because round caps emulated as joins need to emit vertices at T=0 // and T=1, unlike normal round joins. - contour->strokeCapSegmentCount += 2; + strokeCapSegmentCount += 2; // Make sure not to exceed kMaxPolarSegments. - contour->strokeCapSegmentCount = - std::min(contour->strokeCapSegmentCount, kMaxPolarSegments); + strokeCapSegmentCount = fminf(strokeCapSegmentCount, kMaxPolarSegments); + contour->strokeCapSegmentCount = static_cast<uint32_t>(strokeCapSegmentCount); } else { contour->strokeCapSegmentCount = kNumSegmentsInMiterOrBevelJoin; } - // pushContourToRenderContext() uses "strokeCapSegmentCount != 0" to tell if it - // needs stroke caps. - assert(contour->strokeCapSegmentCount != 0); + // PLS expects all patches to have >0 tessellation vertices, so for the case of an + // empty patch with a stroke cap, contour->strokeCapSegmentCount can't be zero. + // Also, pushContourToRenderContext() uses "strokeCapSegmentCount != 0" to tell if + // it needs stroke caps. + assert(contour->strokeCapSegmentCount >= 2); // As long as a contour isn't empty, we can tack the end cap onto the join // section of the final curve in the stroke. Otherwise, we need to introduce // 0-tessellation-segment curves with non-empty joins to carry the caps. @@ -1322,6 +1324,7 @@ // Reverse the cubic and push it with zero parametric and polar segments, and a 180-degree join // tangent. This results in a solitary join, positioned immediately before the provided cubic, // that looks like the desired stroke cap. + assert(strokeCapSegmentCount >= 2); flush->pushCubic(std::array{cubic[3], cubic[2], cubic[1], cubic[0]}.data(), find_cubic_tan0(cubic), emulatedCapAsJoinFlags,
diff --git a/renderer/pls_paint.cpp b/renderer/pls_paint.cpp index 507f2bd..44090ad 100644 --- a/renderer/pls_paint.cpp +++ b/renderer/pls_paint.cpp
@@ -12,6 +12,35 @@ PLSPaint::~PLSPaint() {} +// Ensure the given gradient stops are in a format expected by PLS. +static bool validate_gradient_stops(const ColorInt colors[], // [count] + const float stops[], // [count] + size_t count) +{ + // Stops cannot be empty. + if (count == 0) + { + return false; + } + for (size_t i = 0; i < count; ++i) + { + // Stops must be finite, real numbers in the range [0, 1]. + if (!(0 <= stops[i] && stops[i] <= 1)) + { + return false; + } + } + for (size_t i = 1; i < count; ++i) + { + // Stops must be ordered. + if (!(stops[i - 1] <= stops[i])) + { + return false; + } + } + return true; +} + rcp<PLSGradient> PLSGradient::MakeLinear(float sx, float sy, float ex, @@ -20,6 +49,11 @@ const float stops[], // [count] size_t count) { + if (!validate_gradient_stops(colors, stops, count)) + { + return nullptr; + } + float2 start = {sx, sy}; float2 end = {ex, ey}; PLSGradDataArray<ColorInt> newColors(colors, count); @@ -69,6 +103,11 @@ const float stops[], // [count] size_t count) { + if (!validate_gradient_stops(colors, stops, count)) + { + return nullptr; + } + PLSGradDataArray<ColorInt> newColors(colors, count); PLSGradDataArray<float> newStops(stops, count);
diff --git a/renderer/pls_render_context.cpp b/renderer/pls_render_context.cpp index 785a9e0..b5a4a1f 100644 --- a/renderer/pls_render_context.cpp +++ b/renderer/pls_render_context.cpp
@@ -799,7 +799,7 @@ { float x = stops[i] * w + .5f; uint32_t xFixed = static_cast<uint32_t>(x * (65536.f / kGradTextureWidth)); - assert(lastXFixed <= xFixed && xFixed < 65536); + assert(lastXFixed <= xFixed && xFixed < 65536); // stops[] must be ordered. m_ctx->m_gradSpanData.set_back(lastXFixed, xFixed, y, lastColor, colors[i]); lastColor = colors[i]; lastXFixed = xFixed;
diff --git a/renderer/pls_renderer.cpp b/renderer/pls_renderer.cpp index cf87418..7d552b0 100644 --- a/renderer/pls_renderer.cpp +++ b/renderer/pls_renderer.cpp
@@ -173,8 +173,8 @@ return false; } currentToNew = currentToNew * currentMatrix; - float maxSkew = std::max(fabsf(currentToNew.xy()), fabsf(currentToNew.yx())); - float maxScale = std::max(fabsf(currentToNew.xx()), fabsf(currentToNew.yy())); + float maxSkew = fmaxf(fabsf(currentToNew.xy()), fabsf(currentToNew.yx())); + float maxScale = fmaxf(fabsf(currentToNew.xx()), fabsf(currentToNew.yy())); if (maxSkew > math::EPSILON && maxScale > math::EPSILON) { // Transforming this rect to the new view matrix would turn it into something that isn't a
diff --git a/submodules/rive-cpp b/submodules/rive-cpp index 448cda2..49a9ac5 160000 --- a/submodules/rive-cpp +++ b/submodules/rive-cpp
@@ -1 +1 @@ -Subproject commit 448cda297d1c21e3fc0a9ce63a8db1e4eca04af7 +Subproject commit 49a9ac541a9be88733f5d0ed492ed5cffe3f2384