Skip to content

Commit

Permalink
Added support to custom symbol lookup and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Oct 4, 2023
1 parent e74a40f commit 5929a19
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package overrungl;

import java.lang.foreign.SymbolLookup;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -68,11 +70,32 @@ public final class Configurations {
*/
public static final Entry<Boolean> CHECKS = new Entry<>(() -> true);
/**
* Forcing check all method handles for GL.
* Forcing to check all method handles for OpenGL.
* <p>
* The default value is {@code false}.
*/
public static final Entry<Boolean> GL_FORCE_CHECK_ALL = new Entry<>(() -> false);
/**
* The symbol lookup of GLFW.
* The returned value must not be null.
* <p>
* The default value is {@code null}.
*/
public static final Entry<Function<SymbolLookup, SymbolLookup>> GLFW_SYMBOL_LOOKUP = new Entry<>(() -> null);
/**
* The symbol lookup of NFD.
* The returned value must not be null.
* <p>
* The default value is {@code null}.
*/
public static final Entry<Function<SymbolLookup, SymbolLookup>> NFD_SYMBOL_LOOKUP = new Entry<>(() -> null);
/**
* The symbol lookup of STB.
* The returned value must not be null.
* <p>
* The default value is {@code null}.
*/
public static final Entry<Function<SymbolLookup, SymbolLookup>> STB_SYMBOL_LOOKUP = new Entry<>(() -> null);

private Configurations() {
//no instance
Expand Down
8 changes: 2 additions & 6 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFW.java
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,6 @@ public final class GLFW {
*/
public static final int DONT_CARE = -1;

static {
create();
}

private GLFW() {
throw new IllegalStateException("Do not construct instance");
}
Expand Down Expand Up @@ -3287,7 +3283,7 @@ public static MemorySegment getWindowMonitor(MemorySegment window) {
*/
public static void setWindowMonitor(MemorySegment window, MemorySegment monitor, int xpos, int ypos, int width, int height, int refreshRate) {
try {
glfwGetWindowMonitor.invokeExact(window, monitor, xpos, ypos, width, height, refreshRate);
glfwSetWindowMonitor.invokeExact(window, monitor, xpos, ypos, width, height, refreshRate);
} catch (Throwable e) {
throw new AssertionError("should not reach here", e);
}
Expand Down Expand Up @@ -3399,7 +3395,7 @@ public static void setWindowUserPointer(MemorySegment window, MemorySegment poin
*/
public static MemorySegment getWindowUserPointer(MemorySegment window) {
try {
return (MemorySegment) glfwSetWindowUserPointer.invokeExact(window);
return (MemorySegment) glfwGetWindowUserPointer.invokeExact(window);
} catch (Throwable e) {
throw new AssertionError("should not reach here", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
* @since 0.1.0
*/
public final class GLFWNative {
static {
create();
}

private GLFWNative() {
//no instance
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
* @since 0.1.0
*/
public final class GLFWVulkan {
static {
create();
}

/**
* constructor
*/
Expand Down
339 changes: 152 additions & 187 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/Handles.java

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package overrungl.nfd;

import overrungl.Configurations;
import overrungl.FunctionDescriptors;
import overrungl.NativeType;
import overrungl.OverrunGL;
import overrungl.internal.RuntimeHelper;
import overrungl.os.Platform;
import overrungl.util.MemoryStack;
Expand Down Expand Up @@ -119,7 +119,14 @@
* @since 0.1.0
*/
public final class NFD {
private static final SymbolLookup LOOKUP = RuntimeHelper.load("nfd", "nfd", "0.1.0.0");
private static final SymbolLookup LOOKUP;

static {
final SymbolLookup lib = RuntimeHelper.load("nfd", "nfd", "0.1.0.0");
final var function = Configurations.NFD_SYMBOL_LOOKUP.get();
LOOKUP = function != null ? function.apply(lib) : lib;
}

private static final Platform os = Platform.current();
private static final boolean isOsWin = os instanceof Platform.Windows;
private static final boolean isOsWinOrApple = isOsWin || os instanceof Platform.MacOSX;
Expand Down
16 changes: 8 additions & 8 deletions modules/overrungl.stb/src/main/java/overrungl/stb/Handles.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package overrungl.stb;

import overrungl.Configurations;
import overrungl.FunctionDescriptors;
import overrungl.internal.RuntimeHelper;

Expand All @@ -30,8 +31,13 @@
* @since 0.1.0
*/
final class Handles {
private static boolean loaded = false;
private static SymbolLookup lookup;
private static final SymbolLookup lookup;

static {
final SymbolLookup lib = RuntimeHelper.load("stb", "stb", "0.1.0.0");
final var function = Configurations.STB_SYMBOL_LOOKUP.get();
lookup = function != null ? function.apply(lib) : lib;
}

private Handles() {
//no instance
Expand All @@ -47,10 +53,4 @@ static MethodHandle downcallTrivial(String name,
FunctionDescriptors function) {
return downcall(name, function, Linker.Option.isTrivial());
}

public static void initialize() {
if (loaded) return;
loaded = true;
lookup = RuntimeHelper.load("stb", "stb", "0.1.0.0");
}
}
23 changes: 7 additions & 16 deletions modules/overrungl.stb/src/main/java/overrungl/stb/STBEasyFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,18 @@
* @since 0.1.0
*/
public final class STBEasyFont {
private static MethodHandle
stb_easy_font_draw_segs, stb_easy_font_get_spacing, stb_easy_font_spacing, stb_easy_font_print, stb_easy_font_width, stb_easy_font_height;

static {
initialize();
create();
}
private static final MethodHandle
stb_easy_font_draw_segs = downcall("stb_easy_font_draw_segs", FFPIIPPIII),
stb_easy_font_get_spacing = downcall("stb_easy_font_get_spacing", F),
stb_easy_font_spacing = downcall("stb_easy_font_spacing", FV),
stb_easy_font_print = downcall("stb_easy_font_print", FFPPPII),
stb_easy_font_width = downcall("stb_easy_font_width", fd_PI),
stb_easy_font_height = downcall("stb_easy_font_height", fd_PI);

private STBEasyFont() {
//no instance
}

private static void create() {
stb_easy_font_draw_segs = downcall("stb_easy_font_draw_segs", FFPIIPPIII);
stb_easy_font_get_spacing = downcall("stb_easy_font_get_spacing", F);
stb_easy_font_spacing = downcall("stb_easy_font_spacing", FV);
stb_easy_font_print = downcall("stb_easy_font_print", FFPPPII);
stb_easy_font_width = downcall("stb_easy_font_width", fd_PI);
stb_easy_font_height = downcall("stb_easy_font_height", fd_PI);
}

public static int ndrawSegs(float x, float y, MemorySegment segs, int numSegs, boolean vertical, MemorySegment c, MemorySegment vbuf, int vbufSize, int offset) {
try {
return (int) stb_easy_font_draw_segs.invokeExact(x, y, segs, numSegs, vertical ? 1 : 0, c, vbuf, vbufSize, offset);
Expand Down
106 changes: 44 additions & 62 deletions modules/overrungl.stb/src/main/java/overrungl/stb/STBImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,50 @@
* @since 0.1.0
*/
public final class STBImage {
private static MethodHandle
stbi_convert_iphone_png_to_rgb, stbi_convert_iphone_png_to_rgb_thread, stbi_failure_reason, stbi_hdr_to_ldr_gamma,
stbi_hdr_to_ldr_scale, stbi_image_free, stbi_info, stbi_info_from_callbacks, stbi_info_from_file, stbi_info_from_memory,
stbi_is_16_bit, stbi_is_16_bit_from_callbacks, stbi_is_16_bit_from_file, stbi_is_16_bit_from_memory, stbi_is_hdr,
stbi_is_hdr_from_callbacks, stbi_is_hdr_from_file, stbi_is_hdr_from_memory, stbi_ldr_to_hdr_gamma, stbi_ldr_to_hdr_scale,
stbi_load, stbi_load_16, stbi_load_16_from_callbacks, stbi_load_16_from_memory, stbi_load_from_callbacks, stbi_load_from_file,
stbi_load_from_file_16, stbi_load_from_memory, stbi_load_gif_from_memory, stbi_loadf, stbi_loadf_from_callbacks,
stbi_loadf_from_file, stbi_loadf_from_memory, stbi_set_flip_vertically_on_load, stbi_set_flip_vertically_on_load_thread,
stbi_set_unpremultiply_on_load, stbi_set_unpremultiply_on_load_thread, stbi_zlib_decode_buffer, stbi_zlib_decode_malloc,
stbi_zlib_decode_malloc_guesssize, stbi_zlib_decode_malloc_guesssize_headerflag, stbi_zlib_decode_noheader_buffer,
stbi_zlib_decode_noheader_malloc;
private static final MethodHandle
stbi_convert_iphone_png_to_rgb = downcall("stbi_convert_iphone_png_to_rgb", IV),
stbi_convert_iphone_png_to_rgb_thread = downcall("stbi_convert_iphone_png_to_rgb_thread", IV),
stbi_failure_reason = downcall("stbi_failure_reason", p),
stbi_hdr_to_ldr_gamma = downcall("stbi_hdr_to_ldr_gamma", FV),
stbi_hdr_to_ldr_scale = downcall("stbi_hdr_to_ldr_scale", FV),
stbi_image_free = downcall("stbi_image_free", PV),
stbi_info = downcall("stbi_info", PPPPI),
stbi_info_from_callbacks = downcall("stbi_info_from_callbacks", PPPPPI),
stbi_info_from_file = downcall("stbi_info_from_file", PPPPI),
stbi_info_from_memory = downcall("stbi_info_from_memory", PIPPPI),
stbi_is_16_bit = downcall("stbi_is_16_bit", fd_PI),
stbi_is_16_bit_from_callbacks = downcall("stbi_is_16_bit_from_callbacks", PPI),
stbi_is_16_bit_from_file = downcall("stbi_is_16_bit_from_file", fd_PI),
stbi_is_16_bit_from_memory = downcall("stbi_is_16_bit_from_memory", PII),
stbi_is_hdr = downcall("stbi_is_hdr", fd_PI),
stbi_is_hdr_from_callbacks = downcall("stbi_is_hdr_from_callbacks", PPI),
stbi_is_hdr_from_file = downcall("stbi_is_hdr_from_file", fd_PI),
stbi_is_hdr_from_memory = downcall("stbi_is_hdr_from_memory", PII),
stbi_ldr_to_hdr_gamma = downcall("stbi_ldr_to_hdr_gamma", FV),
stbi_ldr_to_hdr_scale = downcall("stbi_ldr_to_hdr_scale", FV),
stbi_load = downcall("stbi_load", PPPPIp),
stbi_load_16 = downcall("stbi_load_16", PPPPIp),
stbi_load_16_from_callbacks = downcall("stbi_load_16_from_callbacks", PPPPPIp),
stbi_load_16_from_memory = downcall("stbi_load_16_from_memory", PIPPPIp),
stbi_load_from_callbacks = downcall("stbi_load_from_callbacks", PPPPPIp),
stbi_load_from_file = downcall("stbi_load_from_file", PPPPIp),
stbi_load_from_file_16 = downcall("stbi_load_from_file_16", PPPPIp),
stbi_load_from_memory = downcall("stbi_load_from_memory", PIPPPIp),
stbi_load_gif_from_memory = downcall("stbi_load_gif_from_memory", PIPPPPPIp),
stbi_loadf = downcall("stbi_loadf", PPPPIp),
stbi_loadf_from_callbacks = downcall("stbi_loadf_from_callbacks", PPPPPIp),
stbi_loadf_from_file = downcall("stbi_loadf_from_file", PPPPIp),
stbi_loadf_from_memory = downcall("stbi_loadf_from_memory", PIPPPIp),
stbi_set_flip_vertically_on_load = downcall("stbi_set_flip_vertically_on_load", IV),
stbi_set_flip_vertically_on_load_thread = downcall("stbi_set_flip_vertically_on_load_thread", IV),
stbi_set_unpremultiply_on_load = downcall("stbi_set_unpremultiply_on_load", IV),
stbi_set_unpremultiply_on_load_thread = downcall("stbi_set_unpremultiply_on_load_thread", IV),
stbi_zlib_decode_buffer = downcall("stbi_zlib_decode_buffer", PIPII),
stbi_zlib_decode_malloc = downcall("stbi_zlib_decode_malloc", PIPp),
stbi_zlib_decode_malloc_guesssize = downcall("stbi_zlib_decode_malloc_guesssize", PIIPp),
stbi_zlib_decode_malloc_guesssize_headerflag = downcall("stbi_zlib_decode_malloc_guesssize_headerflag", PIIPIp),
stbi_zlib_decode_noheader_buffer = downcall("stbi_zlib_decode_noheader_buffer", PIPII),
stbi_zlib_decode_noheader_malloc = downcall("stbi_zlib_decode_noheader_malloc", PIPp);

// only used for desiredChannels
/**
Expand All @@ -59,57 +92,6 @@ public final class STBImage {
RGB = 3,
RGB_ALPHA = 4;

static {
initialize();
create();
}

private static void create() {
stbi_convert_iphone_png_to_rgb = downcall("stbi_convert_iphone_png_to_rgb", IV);
stbi_convert_iphone_png_to_rgb_thread = downcall("stbi_convert_iphone_png_to_rgb_thread", IV);
stbi_failure_reason = downcall("stbi_failure_reason", p);
stbi_hdr_to_ldr_gamma = downcall("stbi_hdr_to_ldr_gamma", FV);
stbi_hdr_to_ldr_scale = downcall("stbi_hdr_to_ldr_scale", FV);
stbi_image_free = downcall("stbi_image_free", PV);
stbi_info = downcall("stbi_info", PPPPI);
stbi_info_from_callbacks = downcall("stbi_info_from_callbacks", PPPPPI);
stbi_info_from_file = downcall("stbi_info_from_file", PPPPI);
stbi_info_from_memory = downcall("stbi_info_from_memory", PIPPPI);
stbi_is_16_bit = downcall("stbi_is_16_bit", fd_PI);
stbi_is_16_bit_from_callbacks = downcall("stbi_is_16_bit_from_callbacks", PPI);
stbi_is_16_bit_from_file = downcall("stbi_is_16_bit_from_file", fd_PI);
stbi_is_16_bit_from_memory = downcall("stbi_is_16_bit_from_memory", PII);
stbi_is_hdr = downcall("stbi_is_hdr", fd_PI);
stbi_is_hdr_from_callbacks = downcall("stbi_is_hdr_from_callbacks", PPI);
stbi_is_hdr_from_file = downcall("stbi_is_hdr_from_file", fd_PI);
stbi_is_hdr_from_memory = downcall("stbi_is_hdr_from_memory", PII);
stbi_ldr_to_hdr_gamma = downcall("stbi_ldr_to_hdr_gamma", FV);
stbi_ldr_to_hdr_scale = downcall("stbi_ldr_to_hdr_scale", FV);
stbi_load = downcall("stbi_load", PPPPIp);
stbi_load_16 = downcall("stbi_load_16", PPPPIp);
stbi_load_16_from_callbacks = downcall("stbi_load_16_from_callbacks", PPPPPIp);
stbi_load_16_from_memory = downcall("stbi_load_16_from_memory", PIPPPIp);
stbi_load_from_callbacks = downcall("stbi_load_from_callbacks", PPPPPIp);
stbi_load_from_file = downcall("stbi_load_from_file", PPPPIp);
stbi_load_from_file_16 = downcall("stbi_load_from_file_16", PPPPIp);
stbi_load_from_memory = downcall("stbi_load_from_memory", PIPPPIp);
stbi_load_gif_from_memory = downcall("stbi_load_gif_from_memory", PIPPPPPIp);
stbi_loadf = downcall("stbi_loadf", PPPPIp);
stbi_loadf_from_callbacks = downcall("stbi_loadf_from_callbacks", PPPPPIp);
stbi_loadf_from_file = downcall("stbi_loadf_from_file", PPPPIp);
stbi_loadf_from_memory = downcall("stbi_loadf_from_memory", PIPPPIp);
stbi_set_flip_vertically_on_load = downcall("stbi_set_flip_vertically_on_load", IV);
stbi_set_flip_vertically_on_load_thread = downcall("stbi_set_flip_vertically_on_load_thread", IV);
stbi_set_unpremultiply_on_load = downcall("stbi_set_unpremultiply_on_load", IV);
stbi_set_unpremultiply_on_load_thread = downcall("stbi_set_unpremultiply_on_load_thread", IV);
stbi_zlib_decode_buffer = downcall("stbi_zlib_decode_buffer", PIPII);
stbi_zlib_decode_malloc = downcall("stbi_zlib_decode_malloc", PIPp);
stbi_zlib_decode_malloc_guesssize = downcall("stbi_zlib_decode_malloc_guesssize", PIIPp);
stbi_zlib_decode_malloc_guesssize_headerflag = downcall("stbi_zlib_decode_malloc_guesssize_headerflag", PIIPIp);
stbi_zlib_decode_noheader_buffer = downcall("stbi_zlib_decode_noheader_buffer", PIPII);
stbi_zlib_decode_noheader_malloc = downcall("stbi_zlib_decode_noheader_malloc", PIPp);
}

private STBImage() {
throw new IllegalStateException("Do not construct instance");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static java.lang.foreign.ValueLayout.*;
import static overrungl.FunctionDescriptors.*;
import static overrungl.stb.Handles.downcall;
import static overrungl.stb.Handles.initialize;

/**
* The STB image resizer.
Expand All @@ -34,28 +33,17 @@
* @since 0.1.0
*/
public final class STBImageResize {
private static MethodHandle
stbir_resize, stbir_resize_float, stbir_resize_float_generic, stbir_resize_region, stbir_resize_subpixel,
stbir_resize_uint16_generic, stbir_resize_uint8, stbir_resize_uint8_generic, stbir_resize_uint8_srgb,
stbir_resize_uint8_srgb_edgemode;

static {
initialize();
create();
}

private static void create() {
stbir_resize = downcall("stbir_resize", PIIIPIIIIIIIIIIIIPI);
stbir_resize_float = downcall("stbir_resize_float", PIIIPIIIII);
stbir_resize_float_generic = downcall("stbir_resize_float_generic", PIIIPIIIIIIIIIPI);
stbir_resize_region = downcall("stbir_resize_region", PIIIPIIIIIIIIIIIIPFFFFI);
stbir_resize_subpixel = downcall("stbir_resize_subpixel", PIIIPIIIIIIIIIIIIPFFFFI);
stbir_resize_uint16_generic = downcall("stbir_resize_uint16_generic", PIIIPIIIIIIIIIPI);
stbir_resize_uint8 = downcall("stbir_resize_uint8", PIIIPIIIII);
stbir_resize_uint8_generic = downcall("stbir_resize_uint8_generic", PIIIPIIIIIIIIIPI);
stbir_resize_uint8_srgb = downcall("stbir_resize_uint8_srgb", PIIIPIIIIIII);
private static final MethodHandle
stbir_resize = downcall("stbir_resize", PIIIPIIIIIIIIIIIIPI),
stbir_resize_float = downcall("stbir_resize_float", PIIIPIIIII),
stbir_resize_float_generic = downcall("stbir_resize_float_generic", PIIIPIIIIIIIIIPI),
stbir_resize_region = downcall("stbir_resize_region", PIIIPIIIIIIIIIIIIPFFFFI),
stbir_resize_subpixel = downcall("stbir_resize_subpixel", PIIIPIIIIIIIIIIIIPFFFFI),
stbir_resize_uint16_generic = downcall("stbir_resize_uint16_generic", PIIIPIIIIIIIIIPI),
stbir_resize_uint8 = downcall("stbir_resize_uint8", PIIIPIIIII),
stbir_resize_uint8_generic = downcall("stbir_resize_uint8_generic", PIIIPIIIIIIIIIPI),
stbir_resize_uint8_srgb = downcall("stbir_resize_uint8_srgb", PIIIPIIIIIII),
stbir_resize_uint8_srgb_edgemode = downcall("stbir_resize_uint8_srgb_edgemode", PIIIPIIIIIIII);
}

private STBImageResize() {
throw new IllegalStateException("Do not construct instance");
Expand Down
Loading

0 comments on commit 5929a19

Please sign in to comment.