Skip to content

Commit af82060

Browse files
authored
Automatically initialise libvips by default (#177)
1 parent b74fdf1 commit af82060

File tree

200 files changed

+360
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+360
-117
lines changed

.run/Run samples.run.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="Run samples" type="JetRunConfigurationType">
3+
<option name="ALTERNATIVE_JRE_PATH" value="24" />
34
<envs>
45
<env name="DYLD_LIBRARY_PATH" value="libvips/release/lib:/opt/homebrew/lib:$DYLD_LIBRARY_PATH" />
56
</envs>

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repositories {
2323
}
2424

2525
dependencies {
26-
implementation("app.photofox.vips-ffm:vips-ffm-core:1.9.0")
26+
implementation("app.photofox.vips-ffm:vips-ffm-core:1.9.1")
2727
}
2828
```
2929

@@ -56,11 +56,6 @@ import app.photofox.vipsffm.enums.VipsAccess
5656

5757
// ...
5858

59-
// Call once to initialise libvips when your program starts, from any thread
60-
// Note that by default this blocks untrusted operations (like loading PDFs)
61-
// See the "Allowing untrusted operations" section below to read about permitting untrusted operations
62-
Vips.init()
63-
6459
// Use `Vips.run` to wrap your usage of the API, and get an arena with an appropriate lifetime to use
6560
// Usage of the API, arena, and resulting V-Objects must be done from the thread that called `Vips.run`
6661
Vips.run { arena ->
@@ -162,7 +157,16 @@ which isn't present in `vips-ffm`, you can use `VipsOption.Enum(rawValue)` or `V
162157
> available in `VImage` and other `V`-prefixed classes, use those instead. If you notice something missing, please open
163158
> a GitHub Issue.
164159
165-
### Docker checks
160+
## Initialisation
161+
162+
Initialisation of libvips is performed automatically the first time the `Vips`, `VipsHelper`, or `VipsInvoker` classes
163+
are initialised (which will cover almost all normal usage of vips-ffm). Previous versions of vips-ffm required users to
164+
call `Vips.init` manually, but this is no longer required.
165+
166+
If you'd like to disable auto-initialisation of libvips, set the system property `vipsffm.autoinit` to the string value
167+
`false`.
168+
169+
## Docker checks
166170

167171
These samples are also run in Docker containers, to verify behaviour on specific Linux distributions. They're useful to
168172
look at if you're deploying `libvips` and `vips-ffm` workloads using containers.
@@ -207,7 +211,6 @@ If running an image proxy, or something that processes lots of different images,
207211
disable it:
208212

209213
```java
210-
Vips.init();
211214
Vips.disableOperationCache();
212215
```
213216

core/src/main/java/app/photofox/vipsffm/Vips.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,63 @@
11
package app.photofox.vipsffm;
22

33
import java.lang.foreign.Arena;
4+
import java.util.Locale;
5+
import java.util.Optional;
6+
import java.util.concurrent.atomic.AtomicBoolean;
47

58
/// Helper class for running Vips commands with an appropriate arena
69
///
710
/// Blocks untrusted operations by default
811
public class Vips {
912

13+
private static final AtomicBoolean HAS_INITIALISED = new AtomicBoolean(false);
14+
15+
static {
16+
autoInit();
17+
}
18+
19+
/// Note that you do not usually need to call [Vips#init] yourself, as initialisation is performed
20+
/// automatically when vips-ffm classes are used
1021
public static void init() {
11-
init(false, false);
22+
initOnce();
1223
}
1324

25+
/// @deprecated Please migrate to using [Vips#allowUntrustedOperations] and [Vips#enableLeakDetection], and note
26+
/// that calling [Vips#init] is no longer required
27+
@Deprecated(forRemoval = true)
1428
public static void init(boolean allowUntrusted, boolean detectLeaks) {
15-
var arena = Arena.global();
16-
VipsHelper.init(arena, allowUntrusted);
17-
VipsHelper.leak_set(detectLeaks);
29+
initOnce();
30+
if (allowUntrusted) {
31+
Vips.allowUntrustedOperations();
32+
}
33+
if (detectLeaks) {
34+
Vips.enableLeakDetection();
35+
}
36+
}
37+
38+
// Intentionally package-private
39+
static void autoInit() {
40+
if (HAS_INITIALISED.get()) {
41+
// Optimistically skip reading system property
42+
return;
43+
}
44+
var shouldAutoInitString = Optional.ofNullable(System.getProperty("vipsffm.autoinit"))
45+
.orElse("true")
46+
.toLowerCase(Locale.ENGLISH);
47+
var shouldAutoInit = Boolean.parseBoolean(shouldAutoInitString);
48+
if (!shouldAutoInit) {
49+
return;
50+
}
51+
52+
initOnce();
53+
}
54+
55+
private static void initOnce() {
56+
if (HAS_INITIALISED.get()) {
57+
return;
58+
}
59+
VipsHelper.init(Arena.global());
60+
HAS_INITIALISED.set(true);
1861
}
1962

2063
/// Provides a scoped arena to provide a memory boundary for running libvips operations
@@ -30,6 +73,14 @@ public static void shutdown() {
3073
VipsHelper.shutdown();
3174
}
3275

76+
/// Enables leak detection
77+
///
78+
/// Calling [Vips#shutdown] will make libvips print out any leaks, and what classes hold memory references
79+
/// causing these leaks
80+
public static void enableLeakDetection() {
81+
VipsHelper.leak_set(true);
82+
}
83+
3384
/// Permits untrusted operations, such as loading PDFs
3485
///
3586
/// vips-ffm blocks these by default - see the [libvips docs](https://www.libvips.org/API/8.17/func.block_untrusted_set.html)

core/src/main/java/app/photofox/vipsffm/VipsHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
///
1313
/// **Nothing in this class is guaranteed to stay the same across minor versions - use at your own risk!**
1414
public final class VipsHelper {
15-
public static void init(Arena arena, boolean allowUntrusted) {
15+
{
16+
Vips.autoInit();
17+
}
18+
19+
public static void init(Arena arena) {
1620
var nameCString = arena.allocateFrom("vips-ffm");
1721
var result = VipsRaw.vips_init(nameCString);
1822
if (!VipsValidation.isValidResult(result)) {
1923
VipsValidation.throwVipsError("vips_init");
2024
}
21-
VipsRaw.vips_block_untrusted_set(allowUntrusted ? 0 : 1);
2225
}
2326

2427
/// Binding for:

core/src/main/java/app/photofox/vipsffm/VipsInvoker.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
/// a new version of libvips. You can find examples of how to use it in [VImage]
2020
public class VipsInvoker {
2121

22+
static {
23+
Vips.autoInit();
24+
}
25+
2226
public static void invokeOperation(
2327
Arena arena,
2428
String nickname,

docs/allclasses-index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ul id="navbar-top-firstrow" class="nav-list" title="Navigation">
3030
<li><a href="index.html">Overview</a></li>
3131
<li><a href="overview-tree.html">Tree</a></li>
32+
<li><a href="deprecated-list.html">Deprecated</a></li>
3233
<li><a href="index-all.html">Index</a></li>
3334
<li><a href="search.html">Search</a></li>
3435
<li><a href="help-doc.html#all-classes">Help</a></li>

docs/allpackages-index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ul id="navbar-top-firstrow" class="nav-list" title="Navigation">
3030
<li><a href="index.html">Overview</a></li>
3131
<li><a href="overview-tree.html">Tree</a></li>
32+
<li><a href="deprecated-list.html">Deprecated</a></li>
3233
<li><a href="index-all.html">Index</a></li>
3334
<li><a href="search.html">Search</a></li>
3435
<li><a href="help-doc.html#all-packages">Help</a></li>

docs/app.photofox.vipsffm/app/photofox/vipsffm/VBlob.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<li><a href="../../../../index.html">Overview</a></li>
3131
<li class="nav-bar-cell1-rev">Class</li>
3232
<li><a href="package-tree.html">Tree</a></li>
33+
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
3334
<li><a href="../../../../index-all.html">Index</a></li>
3435
<li><a href="../../../../search.html">Search</a></li>
3536
<li><a href="../../../../help-doc.html#class">Help</a></li>

docs/app.photofox.vipsffm/app/photofox/vipsffm/VCustomSource.ReadCallback.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<li><a href="../../../../index.html">Overview</a></li>
3131
<li class="nav-bar-cell1-rev">Class</li>
3232
<li><a href="package-tree.html">Tree</a></li>
33+
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
3334
<li><a href="../../../../index-all.html">Index</a></li>
3435
<li><a href="../../../../search.html">Search</a></li>
3536
<li><a href="../../../../help-doc.html#class">Help</a></li>

docs/app.photofox.vipsffm/app/photofox/vipsffm/VCustomSource.SeekCallback.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<li><a href="../../../../index.html">Overview</a></li>
3131
<li class="nav-bar-cell1-rev">Class</li>
3232
<li><a href="package-tree.html">Tree</a></li>
33+
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
3334
<li><a href="../../../../index-all.html">Index</a></li>
3435
<li><a href="../../../../search.html">Search</a></li>
3536
<li><a href="../../../../help-doc.html#class">Help</a></li>

0 commit comments

Comments
 (0)