Skip to content

Commit

Permalink
display: move thread operations to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Nov 6, 2024
1 parent b3c6d96 commit 29c48d2
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 67 deletions.
2 changes: 2 additions & 0 deletions src/display/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(display_SRC
nr-light.cpp
nr-style.cpp
nr-svgfonts.cpp
threading.cpp
translucency-group.cpp

control/canvas-temporary-item-list.cpp
Expand Down Expand Up @@ -111,6 +112,7 @@ set(display_SRC
nr-svgfonts.h
rendermode.h
tags.h
threading.h
translucency-group.h

control/canvas-temporary-item-list.h
Expand Down
9 changes: 5 additions & 4 deletions src/display/cairo-templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@

#include <glib.h>

#include "dispatch-pool.h"

// single-threaded operation if the number of pixels is below this threshold
static const int POOL_THRESHOLD = 2048;

#include <cmath>
#include <algorithm>
#include <cairo.h>
#include "display/nr-3dutils.h"
#include <cmath>

#include "display/cairo-utils.h"
#include "display/dispatch-pool.h"
#include "display/nr-3dutils.h"
#include "display/threading.h"

template <typename T>
struct surface_accessor {
Expand Down
14 changes: 0 additions & 14 deletions src/display/cairo-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <2geom/point.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/transforms.h>
#include <atomic>
#include <boost/algorithm/string.hpp>
#include <boost/operators.hpp>
#include <boost/optional/optional.hpp>
Expand All @@ -33,7 +32,6 @@
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gstdio.h>
#include <glibmm/fileutils.h>
#include <stdexcept>

#include "cairo-templates.h"
#include "colors/manager.h"
Expand Down Expand Up @@ -969,18 +967,6 @@ std::optional<Geom::PathVector> extract_pathvector_from_cairo(cairo_t *ct)
return res.peek();
}

static std::atomic<int> num_filter_threads = 4;

int get_num_filter_threads()
{
return num_filter_threads.load(std::memory_order_relaxed);
}

void set_num_filter_threads(int n)
{
num_filter_threads.store(n, std::memory_order_relaxed);
}

SPColorInterpolation
get_cairo_surface_ci(cairo_surface_t *surface) {
void* data = cairo_surface_get_user_data( surface, &ink_color_interpolation_key );
Expand Down
4 changes: 0 additions & 4 deletions src/display/cairo-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ class Pixbuf {

} // namespace Inkscape

// Atomic accessors to global variable governing number of filter threads.
int get_num_filter_threads();
void set_num_filter_threads(int);

SPColorInterpolation get_cairo_surface_ci(cairo_surface_t *surface);
void set_cairo_surface_ci(cairo_surface_t *surface, SPColorInterpolation cif);
void copy_cairo_surface_ci(cairo_surface_t *in, cairo_surface_t *out);
Expand Down
26 changes: 0 additions & 26 deletions src/display/dispatch-pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "dispatch-pool.h"

#include "cairo-utils.h"

namespace Inkscape {

dispatch_pool::dispatch_pool(int size)
Expand Down Expand Up @@ -114,30 +112,6 @@ void dispatch_pool::execute_batch(std::unique_lock<std::mutex> &lk, local_id id,
}
}

namespace {

std::mutex g_dispatch_lock;
std::shared_ptr<dispatch_pool> g_dispatch_pool;
int g_dispatch_threads;

} // namespace

std::shared_ptr<dispatch_pool> get_global_dispatch_pool()
{
int const num_threads = get_num_filter_threads();

std::scoped_lock lk(g_dispatch_lock);

if (g_dispatch_pool && num_threads == g_dispatch_threads) {
return g_dispatch_pool;
}

g_dispatch_pool = std::make_shared<dispatch_pool>(num_threads);
g_dispatch_threads = num_threads;

return g_dispatch_pool;
}

} // namespace Inkscape

/*
Expand Down
3 changes: 0 additions & 3 deletions src/display/dispatch-pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -110,8 +109,6 @@ class dispatch_pool
std::vector<std::thread> _threads;
};

std::shared_ptr<dispatch_pool> get_global_dispatch_pool();

} // namespace Inkscape

#endif // INKSCAPE_DISPLAY_DISPATCH_POOL_H
Expand Down
12 changes: 8 additions & 4 deletions src/display/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#include <thread>

#include "cairo-utils.h"
#include "drawing-context.h"
#include "control/canvas-item-drawing.h"
#include "drawing-context.h"
#include "nr-filter-gaussian.h"
#include "nr-filter-types.h"
#include "threading.h"

namespace Inkscape {

Expand Down Expand Up @@ -336,8 +337,9 @@ void Drawing::_loadPrefs()
_cache_budget = 0;
}

// Set the global variable governing the number of filter threads, and track it too. (This is ugly, but hopefully transitional.)
set_num_filter_threads(prefs->getIntLimited("/options/threading/numthreads", default_numthreads(), 1, 256));
// Set the global variable governing the number of threads, and track it too. (This is ugly, but hopefully
// transitional.)
set_num_dispatch_threads(prefs->getIntLimited("/options/threading/numthreads", default_numthreads(), 1, 256));

// Similarly, enable preference tracking only for the Canvas's drawing.
if (_canvas_item_drawing) {
Expand All @@ -354,7 +356,9 @@ void Drawing::_loadPrefs()
actions.emplace("/options/cursortolerance/value", [this] (auto &entry) { setCursorTolerance(entry.getDouble(1.0)); });
actions.emplace("/options/selection/zeroopacity", [this] (auto &entry) { setSelectZeroOpacity(entry.getBool(false)); });
actions.emplace("/options/renderingcache/size", [this] (auto &entry) { setCacheBudget((1 << 20) * entry.getIntLimited(64, 0, 4096)); });
actions.emplace("/options/threading/numthreads", [this] (auto &entry) { set_num_filter_threads(entry.getIntLimited(default_numthreads(), 1, 256)); });
actions.emplace("/options/threading/numthreads", [this](auto &entry) {
set_num_dispatch_threads(entry.getIntLimited(default_numthreads(), 1, 256));
});

_pref_tracker = Inkscape::Preferences::PreferencesObserver::create("/options", [actions = std::move(actions)] (auto &entry) {
auto it = actions.find(entry.getPath());
Expand Down
12 changes: 4 additions & 8 deletions src/display/nr-filter-gaussian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/

#include <2geom/affine.h>
#include <algorithm>
#include <cmath>
#include <complex>
Expand All @@ -21,18 +22,13 @@

#include "display/cairo-utils.h"
#include "display/dispatch-pool.h"
#include "display/nr-filter-primitive.h"
#include "display/nr-filter-gaussian.h"
#include "display/nr-filter-types.h"
#include "display/nr-filter-units.h"
#include "display/nr-filter-primitive.h"
#include "display/nr-filter-slot.h"
#include <2geom/affine.h>
#include "display/nr-filter-units.h"
#include "display/threading.h"
#include "util/fixed_point.h"

#ifndef INK_UNUSED
#define INK_UNUSED(x) ((void)(x))
#endif

// IIR filtering method based on:
// L.J. van Vliet, I.T. Young, and P.W. Verbeek, Recursive Gaussian Derivative Filters,
// in: A.K. Jain, S. Venkatesh, B.C. Lovell (eds.),
Expand Down
4 changes: 0 additions & 4 deletions src/display/nr-filter-morphology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/

#ifdef HAVE_CONFIG_H
# include "config.h" // only include where actually required!
#endif

#include <cmath>
#include <algorithm>
#include <deque>
Expand Down
56 changes: 56 additions & 0 deletions src/display/threading.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Author: Liam White
* Copyright (C) 2024 Authors
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/

#include "threading.h"

#include <atomic>
#include <mutex>

#include "dispatch-pool.h"

namespace Inkscape {

namespace {

std::mutex g_dispatch_lock;

std::shared_ptr<dispatch_pool> g_dispatch_pool;
std::atomic<int> g_num_dispatch_threads = 4;

} // namespace

void set_num_dispatch_threads(int num_dispatch_threads)
{
g_num_dispatch_threads.store(num_dispatch_threads, std::memory_order_relaxed);
}

std::shared_ptr<dispatch_pool> get_global_dispatch_pool()
{
int const num_threads = g_num_dispatch_threads.load(std::memory_order_relaxed);

std::scoped_lock lk(g_dispatch_lock);

if (g_dispatch_pool && num_threads == g_dispatch_pool->size()) {
return g_dispatch_pool;
}

g_dispatch_pool = std::make_shared<dispatch_pool>(num_threads);
return g_dispatch_pool;
}

} // namespace Inkscape

/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
35 changes: 35 additions & 0 deletions src/display/threading.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Author: Liam White
* Copyright (C) 2024 Authors
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/

#ifndef INKSCAPE_DISPLAY_THREADING_H
#define INKSCAPE_DISPLAY_THREADING_H

#include <memory>

namespace Inkscape {

class dispatch_pool;

// Atomic accessor to global variable governing number of dispatch_pool threads.
void set_num_dispatch_threads(int num_dispatch_threads);

std::shared_ptr<dispatch_pool> get_global_dispatch_pool();

} // namespace Inkscape

#endif // INKSCAPE_DISPLAY_THREADING_H

/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :

0 comments on commit 29c48d2

Please sign in to comment.