|
| 1 | +package fr.adamaq01.osmesa; |
| 2 | + |
| 3 | +import org.lwjgl.system.Library; |
| 4 | +import org.lwjgl.system.MemoryUtil; |
| 5 | +import org.lwjgl.system.SharedLibrary; |
| 6 | +import org.lwjgl.system.dyncall.DynCall; |
| 7 | + |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.IntBuffer; |
| 10 | +import java.util.HashMap; |
| 11 | + |
| 12 | +public class OSMesa { |
| 13 | + |
| 14 | + protected static final SharedLibrary OSMESA; |
| 15 | + protected static final HashMap<Long, OSMesaContext> CONTEXTS; |
| 16 | + |
| 17 | + static { |
| 18 | + OSMESA = Library.loadNative("OSMesa"); |
| 19 | + CONTEXTS = new HashMap<>(); |
| 20 | + } |
| 21 | + |
| 22 | + public static final int OSMESA_ROW_LENGTH = 0x10; |
| 23 | + public static final int OSMESA_Y_UP = 0x11; |
| 24 | + public static final int OSMESA_WIDTH = 0x20; |
| 25 | + public static final int OSMESA_HEIGHT = 0x21; |
| 26 | + public static final int OSMESA_FORMAT = 0x22; |
| 27 | + public static final int OSMESA_TYPE = 0x23; |
| 28 | + public static final int OSMESA_MAX_WIDTH = 0x24; |
| 29 | + public static final int OSMESA_MAX_HEIGHT = 0x25; |
| 30 | + |
| 31 | + public static OSMesaContext createContext(OSMesaFormat format) { |
| 32 | + long vm = DynCall.dcNewCallVM(128); |
| 33 | + DynCall.dcArgInt(vm, format.getGlId()); |
| 34 | + DynCall.dcArgPointer(vm, MemoryUtil.NULL); |
| 35 | + long pointer = DynCall.dcCallPointer(vm, OSMESA.getFunctionAddress("OSMesaCreateContext")); |
| 36 | + DynCall.dcFree(vm); |
| 37 | + if (pointer == 0) |
| 38 | + throw new RuntimeException("Could not create OSMesaContext !"); |
| 39 | + OSMesaContext context = new OSMesaContext(format, pointer); |
| 40 | + CONTEXTS.put(pointer, context); |
| 41 | + return context; |
| 42 | + } |
| 43 | + |
| 44 | + public static OSMesaContext createContextExt(OSMesaFormat format, int depthBits, int stencilBits, int accumBits) { |
| 45 | + long vm = DynCall.dcNewCallVM(128); |
| 46 | + DynCall.dcArgInt(vm, format.getGlId()); |
| 47 | + DynCall.dcArgInt(vm, depthBits); |
| 48 | + DynCall.dcArgInt(vm, stencilBits); |
| 49 | + DynCall.dcArgInt(vm, accumBits); |
| 50 | + DynCall.dcArgPointer(vm, MemoryUtil.NULL); |
| 51 | + long pointer = DynCall.dcCallPointer(vm, OSMESA.getFunctionAddress("OSMesaCreateContextExt")); |
| 52 | + DynCall.dcFree(vm); |
| 53 | + if (pointer == 0) |
| 54 | + throw new RuntimeException("Could not create OSMesaContext !"); |
| 55 | + OSMesaContext context = new OSMesaContext(format, pointer); |
| 56 | + CONTEXTS.put(pointer, context); |
| 57 | + return context; |
| 58 | + } |
| 59 | + |
| 60 | + public static void destroyContext(OSMesaContext context) { |
| 61 | + context.destroy(); |
| 62 | + } |
| 63 | + |
| 64 | + public static ByteBuffer makeContextCurrent(OSMesaContext context, int width, int height) { |
| 65 | + return context.makeCurrent(width, height); |
| 66 | + } |
| 67 | + |
| 68 | + public static OSMesaContext getCurrentContext() { |
| 69 | + long vm = DynCall.dcNewCallVM(64); |
| 70 | + long pointer = DynCall.dcCallPointer(vm, OSMESA.getFunctionAddress("OSMesaGetCurrentContext")); |
| 71 | + DynCall.dcFree(vm); |
| 72 | + if (pointer == 0) |
| 73 | + throw new RuntimeException("Could not get current OSMesaContext !"); |
| 74 | + OSMesaContext context = CONTEXTS.get(pointer); |
| 75 | + if (context == null) { |
| 76 | + context = new OSMesaContext(OSMesaFormat.fromGlId(getIntegerv(OSMESA_FORMAT)), pointer); |
| 77 | + CONTEXTS.put(pointer, context); |
| 78 | + } |
| 79 | + return context; |
| 80 | + } |
| 81 | + |
| 82 | + public static int getIntegerv(int property) { |
| 83 | + IntBuffer value = MemoryUtil.memAllocInt(1); |
| 84 | + long vm = DynCall.dcNewCallVM(128); |
| 85 | + DynCall.dcArgInt(vm, property); |
| 86 | + DynCall.dcArgPointer(vm, MemoryUtil.memAddress(value)); |
| 87 | + DynCall.dcCallVoid(vm, OSMesa.OSMESA.getFunctionAddress("OSMesaGetIntegerv")); |
| 88 | + DynCall.dcFree(vm); |
| 89 | + return value.get(0); |
| 90 | + } |
| 91 | +} |
0 commit comments