Skip to content

Commit

Permalink
Namespace corrections to rive instead of gpu
Browse files Browse the repository at this point in the history
Diffs=
bdd2e9bfd Namespace corrections to rive instead of gpu (#8006)

Co-authored-by: rivessamr <[email protected]>
  • Loading branch information
rivessamr and rivessamr committed Aug 30, 2024
1 parent fccefc8 commit 28b0354
Show file tree
Hide file tree
Showing 17 changed files with 397 additions and 355 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cc4f27b8fe70240d8cc452ecc01de069cea0a0bd
bdd2e9bfdccb09db3cfea5aca30d926053fc1491
8 changes: 6 additions & 2 deletions renderer/include/rive/renderer/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
#include "rive/shapes/paint/stroke_join.hpp"
#include "rive/refcnt.hpp"

namespace rive::gpu
namespace rive
{
class Draw;
class RiveRenderPath;
class RiveRenderPaint;
} // namespace rive

namespace rive::gpu
{
class Draw;
class RenderContext;
class Gradient;

Expand Down
6 changes: 5 additions & 1 deletion renderer/include/rive/renderer/gl/render_context_gl_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
#include "rive/renderer/render_context_helper_impl.hpp"
#include <map>

namespace rive::gpu
namespace rive
{
class RiveRenderPath;
class RiveRenderPaint;
} // namespace rive

namespace rive::gpu
{
class RenderTargetGL;

// OpenGL backend implementation of RenderContextImpl.
Expand Down
6 changes: 3 additions & 3 deletions renderer/include/rive/renderer/render_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class RenderContextTest;
namespace rive
{
class RawPath;
class RiveRenderPaint;
class RiveRenderPath;
} // namespace rive

namespace rive::gpu
Expand All @@ -32,10 +34,8 @@ class MidpointFanPathDraw;
class StencilClipReset;
class Draw;
class Gradient;
class RiveRenderPaint;
class RiveRenderPath;
class RiveRenderPathDraw;
class RenderContextImpl;
class RiveRenderPathDraw;

// Used as a key for complex gradients.
class GradientContentKey
Expand Down
4 changes: 2 additions & 2 deletions renderer/include/rive/renderer/rive_render_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "rive/factory.hpp"

namespace rive::gpu
namespace rive
{
// Partial rive::Factory implementation for the PLS objects that are backend-agnostic.
class RiveRenderFactory : public Factory
Expand All @@ -33,4 +33,4 @@ class RiveRenderFactory : public Factory

rcp<RenderPaint> makeRenderPaint() override;
};
} // namespace rive::gpu
} // namespace rive
22 changes: 11 additions & 11 deletions renderer/include/rive/renderer/rive_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
#include "rive/renderer/render_context.hpp"
#include <vector>

namespace rive
namespace rive::gpu
{
class GrInnerFanTriangulator;
};
class RenderContext;
} // namespace rive::gpu

namespace rive::gpu
namespace rive
{
class GrInnerFanTriangulator;
class RiveRenderPath;
class RiveRenderPaint;
class RenderContext;

// Renderer implementation for Rive's pixel local storage renderer.
class RiveRenderer : public Renderer
{
public:
RiveRenderer(RenderContext*);
RiveRenderer(gpu::RenderContext*);
~RiveRenderer() override;

void save() override;
Expand Down Expand Up @@ -59,13 +59,13 @@ class RiveRenderer : public Renderer

// Clips and pushes the given draw to m_context. If the clipped draw is too complex to be
// supported by the GPU buffers, even after a logical flush, then nothing is drawn.
void clipAndPushDraw(DrawUniquePtr);
void clipAndPushDraw(gpu::DrawUniquePtr);

// Pushes any necessary clip updates to m_internalDrawBatch and sets the Draw's clipID and
// clipRectInverseMatrix, if any.
// Returns false if the operation failed, at which point the caller should issue a logical flush
// and try again.
[[nodiscard]] bool applyClip(Draw*);
[[nodiscard]] bool applyClip(gpu::Draw*);

struct RenderState
{
Expand Down Expand Up @@ -97,14 +97,14 @@ class RiveRenderer : public Renderer
};
std::vector<ClipElement> m_clipStack;

RenderContext* const m_context;
gpu::RenderContext* const m_context;

std::vector<DrawUniquePtr> m_internalDrawBatch;
std::vector<gpu::DrawUniquePtr> m_internalDrawBatch;

// Path of the rectangle [0, 0, 1, 1]. Used to draw images.
rcp<RiveRenderPath> m_unitRectPath;

// Used to build coarse path interiors for the "interior triangulation" algorithm.
RawPath m_scratchPath;
};
} // namespace rive::gpu
} // namespace rive
1 change: 1 addition & 0 deletions renderer/src/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "rive_render_paint.hpp"
#include "rive/math/wangs_formula.hpp"
#include "rive/renderer/rive_render_image.hpp"
#include "gradient.hpp"
#include "shaders/constants.glsl"

namespace rive::gpu
Expand Down
1 change: 1 addition & 0 deletions renderer/src/pls.cpp → renderer/src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "shaders/constants.glsl"
#include "rive/renderer/rive_render_image.hpp"
#include "rive_render_paint.hpp"
#include "gradient.hpp"

#include "generated/shaders/draw_path.exports.h"

Expand Down
184 changes: 184 additions & 0 deletions renderer/src/gradient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright 2022 Rive
*/

#include "gradient.hpp"

#include "rive/renderer/rive_render_image.hpp"

namespace rive::gpu
{
// 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<Gradient> Gradient::MakeLinear(float sx,
float sy,
float ex,
float ey,
const ColorInt colors[], // [count]
const float stops[], // [count]
size_t count)
{
if (!validate_gradient_stops(colors, stops, count))
{
return nullptr;
}

float2 start = {sx, sy};
float2 end = {ex, ey};
GradDataArray<ColorInt> newColors(colors, count);
GradDataArray<float> newStops(stops, count);

// If the stops don't begin and end on 0 and 1, transform the gradient so they do. This allows
// us to take full advantage of the gradient's range of pixels in the texture.
float firstStop = stops[0];
float lastStop = stops[count - 1];
if ((firstStop != 0 || lastStop != 1) && lastStop - firstStop > math::EPSILON)
{
// Tighten the endpoints to align with the mininum and maximum gradient stops.
float4 newEndpoints = simd::precise_mix(start.xyxy,
end.xyxy,
float4{firstStop, firstStop, lastStop, lastStop});
start = newEndpoints.xy;
end = newEndpoints.zw;
newStops[0] = 0;
newStops[count - 1] = 1;
if (count > 2)
{
// Transform the stops into the range defined by the new endpoints.
float m = 1.f / (lastStop - firstStop);
float a = -firstStop * m;
for (size_t i = 1; i < count - 1; ++i)
{
newStops[i] = stops[i] * m + a;
}

// Clamp the interior stops so they remain monotonically increasing. newStops[0] and
// newStops[count - 1] are already 0 and 1, so this also ensures they stay within 0..1.
for (size_t i = 1; i < count - 1; ++i)
{
newStops[i] = fmaxf(newStops[i - 1], newStops[i]);
}
for (size_t i = count - 2; i != 0; --i)
{
newStops[i] = fminf(newStops[i], newStops[i + 1]);
}
}
assert(validate_gradient_stops(newColors.get(), newStops.get(), count));
}

float2 v = end - start;
v *= 1.f / simd::dot(v, v); // dot(v, end - start) == 1
return rcp(new Gradient(gpu::PaintType::linearGradient,
std::move(newColors),
std::move(newStops),
count,
v.x,
v.y,
-simd::dot(v, start)));
}

rcp<Gradient> Gradient::MakeRadial(float cx,
float cy,
float radius,
const ColorInt colors[], // [count]
const float stops[], // [count]
size_t count)
{
if (!validate_gradient_stops(colors, stops, count))
{
return nullptr;
}

GradDataArray<ColorInt> newColors(colors, count);
GradDataArray<float> newStops(stops, count);

// If the stops don't end on 1, scale the gradient so they do. This allows us to take better
// advantage of the gradient's full range of pixels in the texture.
//
// TODO: If we want to take full advantage of the gradient texture pixels, we could add an inner
// radius that specifies where t=0 begins (instead of assuming it begins at the center).
float lastStop = stops[count - 1];
if (lastStop != 1 && lastStop > math::EPSILON)
{
// Update the gradient to finish on 1.
newStops[count - 1] = 1;

// Scale the radius to align with the final stop.
radius *= lastStop;

// Scale the stops into the range defined by the new radius.
float inverseLastStop = 1.f / lastStop;
for (size_t i = 0; i < count - 1; ++i)
{
newStops[i] = stops[i] * inverseLastStop;
}

if (count > 1)
{
// Clamp the stops so they remain monotonically increasing. newStops[count - 1] is
// already 1, so this also ensures they stay within 0..1.
newStops[0] = fmaxf(0, newStops[0]);
for (size_t i = 1; i < count - 1; ++i)
{
newStops[i] = fmaxf(newStops[i - 1], newStops[i]);
}
for (size_t i = count - 2; i != -1; --i)
{
newStops[i] = fminf(newStops[i], newStops[i + 1]);
}
}

assert(validate_gradient_stops(newColors.get(), newStops.get(), count));
}

return rcp(new Gradient(gpu::PaintType::radialGradient,
std::move(newColors),
std::move(newStops),
count,
cx,
cy,
radius));
}

bool Gradient::isOpaque() const
{
if (m_isOpaque == gpu::TriState::unknown)
{
ColorInt allColors = ~0;
for (int i = 0; i < m_count; ++i)
{
allColors &= m_colors[i];
}
m_isOpaque = colorAlpha(allColors) == 0xff ? gpu::TriState::yes : gpu::TriState::no;
}
return m_isOpaque == gpu::TriState::yes;
}

} // namespace rive::gpu
Loading

0 comments on commit 28b0354

Please sign in to comment.