Files
OrcaSlicer/src/slic3r/GUI/TextLines.cpp
SoftFever 3e4af2c723 WIP: Add native Wayland support for Linux (#13197)
* Add runtime display backend detection for Wayland support

Add LinuxDisplayBackend utility to detect X11 vs Wayland at runtime
using GDK_IS_X11_DISPLAY / GDK_IS_WAYLAND_DISPLAY macros. This is
the foundation for removing the forced GDK_BACKEND=x11 and enabling
native Wayland support.

- New files: LinuxDisplayBackend.hpp/.cpp with get_linux_display_backend(),
  is_running_on_wayland(), and is_running_on_x11()
- Propagate wxHAVE_GDK_X11 / wxHAVE_GDK_WAYLAND from FindGTK3.cmake
  as compile definitions to libslic3r_gui
- No-op on non-Linux platforms (returns Unknown / false)

* Fix Phase 1 code quality: pragma once, source ordering, static cache

* Make X11 initialization conditional for Wayland support

Remove the unconditional GDK_BACKEND=x11 force that blocked native
Wayland. Replace with conditional logic:

- EGL safety fallback: re-force X11 only when wxUSE_GLCANVAS_EGL is
  off and WAYLAND_DISPLAY is set, with a warning log
- XInitThreads() only called when DISPLAY is set (X11 in use)
- __GLX_VENDOR_LIBRARY_NAME only set when DISPLAY is present (GLX-specific)
- WEBKIT_DISABLE_COMPOSITING_MODE only set under XWayland (both
  DISPLAY and WAYLAND_DISPLAY present)
- Guard X11/Xlib.h include with __has_include for robustness
- Restore display validation to accept either DISPLAY or WAYLAND_DISPLAY

This is Phase 2 of the Wayland support plan.

* Fix Phase 2: safer EGL macro check, add clarifying comments

* Add GLAD2 library and replace GLEW linkage in build system

Set up GLAD2 as a static library to replace GLEW for OpenGL loading.
GLAD2 supports both GLX and EGL, which is required for Wayland support.

- Create src/glad/ with pre-generated GLAD2 sources (GL 4.6 compat)
- Add src/glad/CMakeLists.txt building glad as a static library
- Wire glad into src/CMakeLists.txt before libvgcode
- Modify libvgcode to use shared glad for GL path (keeps local copy
  only for GLES2/Emscripten) to avoid duplicate symbol conflicts
- Replace GLEW::GLEW with glad in libslic3r_gui link libraries

Note: GLEW is kept in deps for OpenCSG. Code migration from GL/glew.h
to glad/gl.h headers will follow in Phase 3B+3C.

* Fix Phase 3A+3D: libvgcode GLAD include, dead files, dlopen dep, OpenGL link var

* Migrate from GLEW to GLAD: replace headers and API calls across codebase

Replace all #include <GL/glew.h> with <glad/gl.h> across 49 source files.
Migrate GLEW API calls to GLAD equivalents:
- glewInit/glewExperimental -> gladLoaderLoadGL()
- GLEW_EXT_* / GLEW_ARB_* extension checks -> GLAD_GL_EXT_* / GLAD_GL_ARB_*
- Remove GLEW-specific EGL/GLX mismatch #error guards (not needed with GLAD)
- Replace unavailable EXT symbols with core GL equivalents in
  GLCanvas3D.cpp (GL_MAX_SAMPLES, glRenderbufferStorageMultisample,
  glBlitFramebuffer, GL_READ/DRAW_FRAMEBUFFER)
- Update log messages from glewInit to gladLoadGL

* Fix Phase 3B+3C: remove GLEW find, clean EXT symbols, update attribution

- Remove find_package(GLEW) block from root CMakeLists.txt since GLEW
  is no longer linked by any main application code
- Remove "glew" from SLIC3R_STATIC option description
- Replace all remaining EXT framebuffer symbols with core equivalents
  in render_thumbnail_framebuffer_ext and _rectangular_selection_picking_pass
- Update AboutDialog credits from GLEW to GLAD

* Enable EGL in wxWidgets and add runtime GLX/EGL selection for Wayland

- Set wxUSE_GLCANVAS_EGL=ON in wxWidgets build and Flatpak manifest
- Add PreferGLX() call on X11 sessions for driver compatibility
- Remove Phase 2 safety fallback (EGL is now always compiled in)
- Guard SwapBuffers against hidden canvases to prevent Wayland stalls

* Fix Phase 4: move PreferGLX to app startup, fix FPS counter guard

Move wxGLCanvas::PreferGLX() from OpenGLManager::create_wxglcanvas()
(static initializer) to GUI_App::on_init_inner() before any wxGLCanvas
is constructed. This prevents a race where SkipPartCanvas could trigger
wxGLBackend::Init() before the GLX preference is set. The new location
also adds explicit is_running_on_wayland() detection with a warning for
unknown backends.

Move increment_fps_counter() inside the IsShownOnScreen() guard so FPS
is only counted when a frame is actually swapped.

* Update GLFW from 3.3.7 to 3.4 for runtime Wayland/X11 backend selection

Replace the compile-time GLFW_USE_WAYLAND flag (which locked to a single
backend) with GLFW 3.4's GLFW_BUILD_WAYLAND + GLFW_BUILD_X11 flags that
build both backends and auto-select at runtime based on the available
display server. This enables the CLI thumbnail renderer to work on both
Wayland and X11 sessions without separate builds.

* wayland: Fix UI call sites that rely on global screen coordinates

On Wayland, wxGetMousePosition() returns (0,0) and SetPosition() is a
no-op for top-level windows. Fix the highest-impact call sites:

- GLCanvas3D: Use cached m_mouse.position from event handlers instead
  of wxGetMousePosition() + ScreenToClient() in get_local_mouse_position()
- Plater: Use event-relative coords via ClientToScreen(e.GetPosition())
  instead of wxGetMousePosition() in 3 leave-window handlers
- BBLTopbar: Use event.GetPosition() and FindToolByPosition() directly
  in mouse handlers instead of wxGetMousePosition()/FindToolByCurrentPosition()
- Search: Use focus-based dismiss logic on Wayland instead of
  wxGetMousePosition()-based rect checks in SearchDialog and
  SearchObjectDialog
- GUI_App: Skip SetPosition() in window_pos_restore() on Wayland where
  it is a no-op; still restore size and maximize state
- Button: Position tooltip relative to button widget via ClientToScreen
  instead of wxGetMousePosition()

* Fix SearchDialog Wayland dismiss: guard against search_line focus

* flatpak: Add Wayland socket permission for native Wayland support

* spec

* Fix crash on Wayland when wxWidgets lacks EGL support

Restore the safety fallback that forces GDK_BACKEND=x11 when wxWidgets
was not built with wxUSE_GLCANVAS_EGL=ON. Without this, the GLX backend
tries to access a non-existent X11 display on native Wayland, crashing
in wxGLCanvas::IsDisplaySupported() with SIGSEGV at offset 0xe4.

Also add a defense-in-depth guard in detect_multisample() that skips
the IsDisplaySupported call entirely on Wayland without EGL.

Root cause: deps/wxWidgets must be rebuilt after enabling EGL. The
compile-time check in OrcaSlicer.cpp detects the mismatch and falls
back safely.

* Fix EGL detection: use wxHAS_EGL instead of wxUSE_GLCANVAS_EGL

wxUSE_GLCANVAS_EGL is a CMake build option, NOT a C++ preprocessor
macro. The actual macro defined in wxWidgets setup.h is wxHAS_EGL.
All compile-time EGL checks were using the wrong macro, causing
the safety fallback to always trigger even with a properly built
EGL-enabled wxWidgets.

* Fix GL function pointers invalidated on Wayland/EGL

gladLoaderLoadGL() dlopen's libGL.so.1 to resolve GL function pointers
via dlsym, then immediately dlclose's the handle. On X11/GLX this is
fine because the GLX context keeps libGL.so mapped. On Wayland/EGL,
nothing else holds libGL.so open, so dlclose unmaps it and all function
pointers become dangling — causing SIGSEGV on the first GL call.

Fix: on Wayland, use gladLoadGL(eglGetProcAddress) which resolves
function pointers through the EGL loader without opening/closing
libGL.so.

* fix crash on start and various rendering issues

* fix crash on close

* small refactor

* move GPU selection to desktop file

* clean up a bit

* clean up more

* fix appimage error
2026-04-13 19:45:39 +08:00

355 lines
12 KiB
C++

#include "TextLines.hpp"
#include <glad/gl.h>
#include "libslic3r/Model.hpp"
#include "libslic3r/Emboss.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
#include "libslic3r/Tesselate.hpp"
#include "libslic3r/AABBTreeLines.hpp"
#include "libslic3r/ExPolygonsIndex.hpp"
#include "libslic3r/ClipperUtils.hpp"
#include "slic3r/GUI/Selection.hpp"
#include "slic3r/GUI/GLCanvas3D.hpp"
#include "slic3r/GUI/GLModel.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/Plater.hpp"
#include "slic3r/GUI/Camera.hpp"
#include "slic3r/GUI/3DScene.hpp"
using namespace Slic3r;
using namespace Slic3r::Emboss;
using namespace Slic3r::GUI;
namespace {
// Be careful it is not water tide and contain self intersections
// It is only for visualization purposes
indexed_triangle_set its_create_torus(const Slic3r::Polygon &polygon, float radius, size_t steps = 20)
{
assert(!polygon.empty());
if (polygon.empty())
return {};
size_t count = polygon.size();
if (count < 3)
return {};
// convert and scale to float
std::vector<Vec2f> points_d;
points_d.reserve(count);
for (const Point &point : polygon.points)
points_d.push_back(unscale(point).cast<float>());
// pre calculate normalized line directions
auto calc_line_norm = [](const Vec2f &f, const Vec2f &s) -> Vec2f { return (s - f).normalized(); };
std::vector<Vec2f> line_norm(points_d.size());
for (size_t i = 0; i < count - 1; ++i)
line_norm[i] = calc_line_norm(points_d[i], points_d[i + 1]);
line_norm.back() = calc_line_norm(points_d.back(), points_d.front());
// precalculate sinus and cosinus
double angle_step = 2 * M_PI / steps;
std::vector<std::pair<double, float>> sin_cos;
sin_cos.reserve(steps);
for (size_t s = 0; s < steps; ++s) {
double angle = s * angle_step;
sin_cos.emplace_back(
radius * std::sin(angle),
static_cast<float>(radius * std::cos(angle))
);
}
indexed_triangle_set sphere = its_make_sphere(radius, 2 * PI / steps);
// create torus model along polygon path
indexed_triangle_set model;
model.vertices.reserve(2 * steps * count + sphere.vertices.size()*count);
model.indices.reserve(2 * steps * count + sphere.indices.size()*count);
const Vec2f *prev_prev_point_d = &points_d[count-2]; // one before back
const Vec2f *prev_point_d = &points_d.back();
auto calc_angle = [](const Vec2f &d0, const Vec2f &d1) {
double dot = d0.dot(d1);
double det = d0.x() * d1.y() - d0.y() * d1.x(); // Determinant
return std::atan2(det, dot); // atan2(y, x) or atan2(sin, cos)
};
// opposit previos direction of line - for calculate angle
Vec2f opposit_prev_dir = (*prev_prev_point_d) - (*prev_point_d);
for (size_t i = 0; i < count; ++i) {
const Vec2f & point_d = points_d[i];
// line segment direction
Vec2f dir = point_d - (*prev_point_d);
double angle = calc_angle(opposit_prev_dir, dir);
double allowed_preccission = 1e-6;
if (angle >= (PI - allowed_preccission) ||
angle <= (-PI + allowed_preccission))
continue; // it is almost line
// perpendicular direction to line
Vec2d p_dir(dir.y(), -dir.x());
p_dir.normalize(); // Should done with double preccission
// p_dir is tube unit side vector
// tube unit top vector is z direction
// Tube
int prev_index = model.vertices.size() + 2 * sin_cos.size() - 2;
for (const auto &[s, c] : sin_cos) {
Vec2f side = (s * p_dir).cast<float>();
Vec2f xy0 = side + (*prev_point_d);
Vec2f xy1 = side + point_d;
model.vertices.emplace_back(xy0.x(), xy0.y(), c); // pointing of prev index
model.vertices.emplace_back(xy1.x(), xy1.y(), c);
// create triangle indices
int f0 = prev_index;
int s0 = f0 + 1;
int f1 = model.vertices.size() - 2;
int s1 = f1 + 1;
prev_index = f1;
model.indices.emplace_back(s0, f0, s1);
model.indices.emplace_back(f1, s1, f0);
}
prev_prev_point_d = prev_point_d;
prev_point_d = &point_d;
opposit_prev_dir = -dir;
}
// sphere on each point
for (Vec2f& p: points_d){
indexed_triangle_set sphere_copy = sphere;
its_translate(sphere_copy, Vec3f(p.x(), p.y(), 0.f));
its_merge(model, sphere_copy);
}
return model;
}
// select closest contour for each line
TextLines select_closest_contour(const std::vector<Polygons> &line_contours) {
TextLines result;
result.reserve(line_contours.size());
Vec2d zero(0., 0.);
for (const Polygons &polygons : line_contours){
if (polygons.empty()) {
result.emplace_back();
continue;
}
// Improve: use int values and polygons only
// Slic3r::Polygons polygons = union_(polygons);
// std::vector<Slic3r::Line> lines = to_lines(polygons);
// AABBTreeIndirect::Tree<2, Point> tree;
// size_t line_idx;
// Point hit_point;
// Point::Scalar distance = AABBTreeLines::squared_distance_to_indexed_lines(lines, tree, point, line_idx, hit_point);
ExPolygons expolygons = union_ex(polygons);
std::vector<Linef> linesf = to_linesf(expolygons);
AABBTreeIndirect::Tree2d tree = AABBTreeLines::build_aabb_tree_over_indexed_lines(linesf);
size_t line_idx = 0;
Vec2d hit_point;
// double distance =
AABBTreeLines::squared_distance_to_indexed_lines(linesf, tree, zero, line_idx, hit_point);
// conversion between index of point and expolygon
ExPolygonsIndices cvt(expolygons);
ExPolygonsIndex index = cvt.cvt(static_cast<uint32_t>(line_idx));
const Slic3r::Polygon& polygon = index.is_contour() ?
expolygons[index.expolygons_index].contour :
expolygons[index.expolygons_index].holes[index.hole_index()];
Point hit_point_int = hit_point.cast<Point::coord_type>();
TextLine tl{polygon, PolygonPoint{index.point_index, hit_point_int}};
result.emplace_back(tl);
}
return result;
}
inline Eigen::AngleAxis<double> get_rotation() { return Eigen::AngleAxis(-M_PI_2, Vec3d::UnitX()); }
indexed_triangle_set create_its(const TextLines &lines, float radius)
{
indexed_triangle_set its;
// create model from polygons
for (const TextLine &line : lines) {
const Slic3r::Polygon &polygon = line.polygon;
if (polygon.empty()) continue;
indexed_triangle_set line_its = its_create_torus(polygon, radius);
auto transl = Eigen::Translation3d(0., line.y, 0.);
Transform3d tr = transl * get_rotation();
its_transform(line_its, tr);
its_merge(its, line_its);
}
return its;
}
GLModel::Geometry create_geometry(const TextLines &lines, float radius, bool is_mirrored)
{
indexed_triangle_set its = create_its(lines, radius);
GLModel::Geometry geometry;
geometry.format = {GLModel::Geometry::EPrimitiveType::Triangles, GUI::GLModel::Geometry::EVertexLayout::P3};
ColorRGBA color(.7f, .7f, .7f, .7f); // Transparent Gray
geometry.color = color;
geometry.reserve_vertices(its.vertices.size());
for (Vec3f vertex : its.vertices)
geometry.add_vertex(vertex);
geometry.reserve_indices(its.indices.size() * 3);
if (is_mirrored) {
// change order of indices
for (Vec3i32 t : its.indices)
geometry.add_triangle(t[0], t[2], t[1]);
} else {
for (Vec3i32 t : its.indices)
geometry.add_triangle(t[0], t[1], t[2]);
}
return geometry;
}
} // namespace
void TextLinesModel::init(const Transform3d &text_tr,
const ModelVolumePtrs &volumes_to_slice,
/*const*/ Emboss::StyleManager &style_manager,
unsigned count_lines)
{
assert(style_manager.is_active_font());
if (!style_manager.is_active_font())
return;
const auto &ffc = style_manager.get_font_file_with_cache();
assert(ffc.has_value());
if (!ffc.has_value())
return;
const auto &ff_ptr = ffc.font_file;
assert(ff_ptr != nullptr);
if (ff_ptr == nullptr)
return;
const FontFile &ff = *ff_ptr;
const FontProp &fp = style_manager.get_font_prop();
FontProp::VerticalAlign align = fp.align.second;
double line_height_mm = calc_line_height_in_mm(ff, fp);
assert(line_height_mm > 0);
if (line_height_mm <= 0)
return;
m_model.reset();
m_lines.clear();
// size_in_mm .. contain volume scale and should be ascent value in mm
double line_offset = fp.size_in_mm * ascent_ratio_offset;
double first_line_center = line_offset + get_align_y_offset_in_mm(align, count_lines, ff, fp);
std::vector<float> line_centers(count_lines);
for (size_t i = 0; i < count_lines; ++i)
line_centers[i] = static_cast<float>(first_line_center - i * line_height_mm);
// contour transformation
Transform3d c_trafo = text_tr * get_rotation();
Transform3d c_trafo_inv = c_trafo.inverse();
std::vector<Polygons> line_contours(count_lines);
for (const ModelVolume *volume : volumes_to_slice) {
MeshSlicingParams slicing_params;
slicing_params.trafo = c_trafo_inv * volume->get_matrix();
for (size_t i = 0; i < count_lines; ++i) {
const Polygons polys = Slic3r::slice_mesh(volume->mesh().its, line_centers[i], slicing_params);
if (polys.empty())
continue;
Polygons &contours = line_contours[i];
contours.insert(contours.end(), polys.begin(), polys.end());
}
}
// fix for text line out of object
// When move text close to edge - line center could be out of object
for (Polygons &contours: line_contours) {
if (!contours.empty())
continue;
// use line center at zero, there should be some contour.
float line_center = 0.f;
for (const ModelVolume *volume : volumes_to_slice) {
MeshSlicingParams slicing_params;
slicing_params.trafo = c_trafo_inv * volume->get_matrix();
const Polygons polys = Slic3r::slice_mesh(volume->mesh().its, line_center, slicing_params);
if (polys.empty())
continue;
contours.insert(contours.end(), polys.begin(), polys.end());
}
}
m_lines = select_closest_contour(line_contours);
assert(m_lines.size() == count_lines);
assert(line_centers.size() == count_lines);
for (size_t i = 0; i < count_lines; ++i)
m_lines[i].y = line_centers[i];
bool is_mirrored = has_reflection(text_tr);
float radius = static_cast<float>(line_height_mm / 20.);
//*
GLModel::Geometry geometry = create_geometry(m_lines, radius, is_mirrored);
if (geometry.vertices_count() == 0 || geometry.indices_count() == 0)
return;
m_model.init_from(std::move(geometry));
/*/
// slower solution
ColorRGBA color(.7f, .7f, .7f, .7f); // Transparent Gray
m_model.set_color(color);
m_model.init_from(create_its(m_lines));
//*/
}
void TextLinesModel::render(const Transform3d &text_world)
{
if (!m_model.is_initialized())
return;
GUI_App &app = wxGetApp();
const GLShaderProgram *shader = app.get_shader("flat");
if (shader == nullptr)
return;
const Camera &camera = app.plater()->get_camera();
shader->start_using();
shader->set_uniform("view_model_matrix", camera.get_view_matrix() * text_world);
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
bool is_depth_test = glIsEnabled(GL_DEPTH_TEST);
if (!is_depth_test)
glsafe(::glEnable(GL_DEPTH_TEST));
bool is_blend = glIsEnabled(GL_BLEND);
if (!is_blend)
glsafe(::glEnable(GL_BLEND));
// glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
m_model.render();
if (!is_depth_test)
glsafe(::glDisable(GL_DEPTH_TEST));
if (!is_blend)
glsafe(::glDisable(GL_BLEND));
shader->stop_using();
}
double TextLinesModel::calc_line_height_in_mm(const Slic3r::Emboss::FontFile &ff, const FontProp &fp)
{
int line_height = Slic3r::Emboss::get_line_height(ff, fp); // In shape size
double scale = Slic3r::Emboss::get_text_shape_scale(fp, ff);
return line_height * scale;
}