Passing pointer of address

Thiago Milczarek Sayão thiago.sayao at gmail.com
Mon Apr 29 12:28:07 UTC 2024


Hi Maurizio,

My mistake. It works.

I was playing around with it and my first version did not work. But the
problem was on another part of the code.

elgConfig is an array, so eglCreateWindowSurface only works by passing like
below (I might be mistaken again).

Thanks for replying.

if (EGL.eglChooseConfig(eglDisplay, attributes, eglConfig, 1,
numConfigs) == 0) {
    System.out.println("eglChooseConfig Failed");
    return null;
}

MemorySegment eglSurface = EGL.eglCreateWindowSurface(eglDisplay,
eglConfig.get(EGLConfig, 0), eglWindow.address(), MemorySegment.NULL);

if (eglSurface.equals(EGL_NO_SURFACE())) {
    System.out.printf("eglCreateWindowSurface Failed %d%n", eglGetError());
    return null;
}



Em seg., 29 de abr. de 2024 às 08:08, Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> escreveu:

> Hi Thiago,
>
> So, you have a memory segment with a given address, called "attributes".
>
> The code you have is:
>
> 1. obtaining the addres from "attributes"
> 2. repackaging the address in a _new_ memory segment
>
> Note that the segment you create in (2) is identical to the one you
> started from. They have the same address. The only difference is that the
> first segment has some meaningful spatial bounds (e.g. it has  a size), the
> second segment is a zero-length memory segment (so you can't dereference
> it).
>
> But from the perspective of passing this segment "by reference" to a
> native function, they are exactly the same - e.g. when passing a segment by
> reference, the Linker will call "MemorySegment::address()" and pass that to
> the underlying function. Since the address of segments in (1) and (2) is
> indeed the same, I don't think these segments should behave differently at
> all - meaning your code can be simplified, and you can just use
> "attributes".
>
> This misconception might have originated from the fact that FFM doesn't
> provide true "stack allocation". So when you have a segment you already
> have some address. Meaning that if the original C code said something like
> "&foo", and "foo" is a segment in the FFM code, you can just pass "foo" as
> a pointer instead.
>
> I hope this helps.
>
> Maurizio
> On 29/04/2024 11:45, Thiago Milczarek Sayão wrote:
>
> Hi,
>
> I'm doing some wayland binding experiments with jextract and FFM.
> So far, so good.
>
> But there's one thing that could be better, or I am doing it wrong
> (Please, excuse me if it's my mistake.).
>
> The eglChooseConfig call expects the pointer of an address on the second
> argument.
> https://registry.khronos.org/EGL/sdk/docs/man/html/eglChooseConfig.xhtml
>
> The below code works, but I feel it could be simpler. Maybe a call like
> attributes.addressPointer() ?
>
> MemorySegment eglDisplay = EGL.eglGetDisplay(display.getSegment());eglBindAPI(EGL_OPENGL_API());
> if (eglDisplay.equals(EGL_NO_DISPLAY())) {
>     System.out.println("NO DISPLAY");
>     return null;
> }
> if (eglInitialize(eglDisplay, MemorySegment.NULL, MemorySegment.NULL) == 0) {
>     System.out.println("eglInitialize Failed");
>     return null;
> };
> int[] attribs = {
>     EGL_RENDERABLE_TYPE(),
>     EGL_OPENGL_BIT(),
>     EGL_SURFACE_TYPE(),
>     EGL_WINDOW_BIT(),
>     EGL_RED_SIZE(),
>     8,
>     EGL_GREEN_SIZE(),
>     8,
>     EGL_BLUE_SIZE(),
>     8,
>     EGL_ALPHA_SIZE(),
>     8,
>     EGL_NONE()
> };
>
> MemorySegment eglConfig = Arena.global().allocate(C_POINTER);
> MemorySegment numConfigs = Arena.global().allocate(EGLint);
> MemorySegment attributes = Arena.global().allocateFrom(EGLint, attribs);
> if (EGL.eglChooseConfig(eglDisplay, MemorySegment.ofAddress(attributes.address()), eglConfig, 1, numConfigs) == 0) {
>     System.out.println("eglChooseConfig Failed");
>     return null;
> }
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240429/a788395b/attachment.htm>


More information about the panama-dev mailing list