husker-dev / openglfx

OpenGL node for JavaFX
Apache License 2.0
88 stars 12 forks source link

need more open,and AsyncCanvas need stop on fxnode cant see。my app only need init one libgdx gl context。 #56

Closed qq1053831109 closed 12 months ago

qq1053831109 commented 1 year ago

package ose.core.sglfx;

import com.huskerdev.ojgl.GLContext;
import com.huskerdev.openglfx.GLExecutor;
import com.huskerdev.openglfx.canvas.GLCanvasAnimator;
import com.huskerdev.openglfx.canvas.GLProfile;
import com.huskerdev.openglfx.canvas.events.GLInitializeEvent;
import com.huskerdev.openglfx.canvas.events.GLRenderEvent;
import com.huskerdev.openglfx.canvas.implementations.async.AsyncSharedCanvasImpl;
import com.sun.prism.Graphics;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;

public class SolarAsyncSharedImpl extends AsyncSharedCanvasImpl {

    protected DefaultSolarFxInput input = null;
    private boolean _needInitField = true;
    private static GLContext _fxContext = null;
    private static GLContext _parallelContext = null;
    private static GLContext _resultContext = null;
    public static Field fxContextField;
    public static Field parallelContextField;
    public static Field resultContextField;

    static {
        try {
            fxContextField = AsyncSharedCanvasImpl.class.getDeclaredField("fxContext");
            fxContextField.setAccessible(true);
            parallelContextField = AsyncSharedCanvasImpl.class.getDeclaredField("parallelContext");
            parallelContextField.setAccessible(true);
            resultContextField = AsyncSharedCanvasImpl.class.getDeclaredField("resultContext");
            resultContextField.setAccessible(true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public SolarAsyncSharedImpl() {
        this(SolarLWJGLExecutor2.executor);
    }

    public SolarAsyncSharedImpl(GLExecutor executor) {
        super(executor, GLProfile.Core, false, 0);
    }

    public SolarAsyncSharedImpl(@NotNull GLExecutor executor, @NotNull GLProfile profile, boolean flipY, int msaa) {
        super(executor, profile, flipY, msaa);
    }

    @Override
    protected void onNGRender(@NotNull Graphics g) {
        if (_needInitField) {
            _needInitField = false;
            if (_fxContext == null) {
                super.onNGRender(g);
                try {
                    _fxContext = (GLContext) fxContextField.get(this);
                    _parallelContext = (GLContext) parallelContextField.get(this);
                    _resultContext = (GLContext) resultContextField.get(this);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return;
            } else {
                try {
                    fxContextField.set(this, _fxContext);
                    parallelContextField.set(this, _parallelContext);
                    resultContextField.set(this, _resultContext);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        super.onNGRender(g);
    }

    public void setInput(DefaultSolarFxInput input) {
        this.input = input;
    }

    public void setFpsLimit(int fps) {
        setAnimator(new GLCanvasAnimator(fps));
    }

    private int _fbo = 0;

    @NotNull
    @Override
    protected GLInitializeEvent createInitEvent() {
        System.out.println("ci="+Thread.currentThread().getId());
        SGLContext.create(0, true);
        return super.createInitEvent();
    }

    @NotNull
    @Override
    protected GLRenderEvent createRenderEvent(int currentFps, double delta, int width, int height, int fbo) {
        _fbo = fbo;
        if (input != null) {
            input.update();
        }
        System.out.println("r="+Thread.currentThread().getId());
        SolarGlFx.glFxApp.executedRunnables();
        return super.createRenderEvent(currentFps, delta, width, height, fbo);
    }

    public int getFbo() {
        return _fbo;
    }
}
`
qq1053831109 commented 1 year ago

The above code is my usage method

qq1053831109 commented 1 year ago

image it hard to hook。opengl Having complex usage requirements。Cannot be limited to simple APIs

husker-dev commented 1 year ago

For what reason would you need to override the AsyncSharedCanvasImpl logic?

If you need to hook to the init/render methods, you can add a listener. There can be many of them.

Please describe your case so that I can suggest the best solution

husker-dev commented 1 year ago

The best development option is not to rewrite special cases of the GLCanvas implementation. It may change in future

qq1053831109 commented 1 year ago

Mine is just one of them. Most interfaces should be open. For example, when creating a context, I need to create it myself. Because I am docking from libgdx. And asynchrony is only two threads, libgdx and javafx. Each fxnode cannot be a gl thread. You don't need to change it to me. Because everyone uses this in different scenarios. Need to be more open, we need class inheritance override

husker-dev commented 12 months ago

I can make a separate module with libgdx integration. As far as I understand, this shouldn't be too difficult.

But your approach with overriding seems very wrong to me.

qq1053831109 commented 12 months ago

yes. open the api. i have a jme3 game need use this。Need more customization