简体   繁体   English

隐藏画布时丢失 AWT DrawingSurface

[英]Losing AWT DrawingSurface when Canvas is hidden

I am writing 3d rendering module for an AWT/Swing application.我正在为 AWT/Swing 应用程序编写 3d 渲染模块。

To provide good FPS, I can't draw using Swing/AWT methods and graphics.为了提供良好的 FPS,我无法使用 Swing/AWT 方法和图形进行绘制。 Instead, I obtain the Drawing Surface from the Canvas element, and then render directly to it.相反,我从 Canvas 元素获取绘图表面,然后直接渲染到它。 Something like this:像这样的东西:

public class Window {

    private Component canvas;
    private JAWTDrawingSurface ds;

    public static final JAWT awt;
    static {
        awt = JAWT.calloc();
        awt.version(JAWT_VERSION_1_4);
        if (!JAWT_GetAWT(awt))
            throw new AssertionError("GetAWT failed");
    }

    public void lock() throws AWTException {
        int lock = JAWT_DrawingSurface_Lock(ds, ds.Lock());
        if ((lock & JAWT_LOCK_ERROR) != 0)
            throw new AWTException("JAWT_DrawingSurface_Lock() failed");
    }

    public void unlock() throws AWTException {
        JAWT_DrawingSurface_Unlock(ds, ds.Unlock());
    }

    public void Init2()
    {    
        this.ds = JAWT_GetDrawingSurface(canvas, awt.GetDrawingSurface());
        try
        {
            lock();
            // Create GL Capabilities
            unlock();
        }
     }

It works fine when I call it the first time.当我第一次调用它时它工作正常。 But when I hide the canvas for any reason (for example minimizing window or displaying another panel instead of Canvas), the ds variable remains the same, but it doesn't work after that.但是当我出于任何原因隐藏画布时(例如最小化窗口或显示另一个面板而不是画布), ds变量保持不变,但之后它不起作用。 Basically, even if I make sure I call the variable only when it is visible and on top - any call using ds will throw an exception.基本上,即使我确保仅在变量可见且位于顶部时才调用该变量 - 任何使用ds 的调用都会引发异常。 For example lock() function stops working.例如lock()函数停止工作。

I'm wondering why's that?我想知道这是为什么?

Also I tried to basically obtain a new DS if I minimize and then maximize the window again, but this also doesn't work - the new DS address is returned as it should, but I can't use that new object just as I couldn't use the original one.如果我最小化然后再次最大化窗口,我也试图基本上获得一个新的 DS,但这也不起作用 - 新的 DS 地址按原样返回,但我不能像我一样使用那个新对象不要用原来的。

There's probably something stupid I'm missing here, but I can't figure out what.我可能在这里遗漏了一些愚蠢的东西,但我不知道是什么。 Please help me sort this out.请帮我解决这个问题。 Thank you!谢谢!

The solution:解决方案:

When the Canvas is hidden, call eglMakeCurrent(eglDisplay,EGL_NO_SURFACE,EGL_NO_SURFACE,EGL_NO_CONTEXT) to unbind the current context.当 Canvas 隐藏时,调用 eglMakeCurrent(eglDisplay,EGL_NO_SURFACE,EGL_NO_SURFACE,EGL_NO_CONTEXT) 解除当前上下文的绑定。

When you need to start drawing again, you need to do something like this:当您需要再次开始绘图时,您需要执行以下操作:

public void Reinit()
{
    System.err.println("Context Reinit()");

    this.ds = JAWT_GetDrawingSurface(canvas, awt.GetDrawingSurface());
    try
    {
        lock();
        try
        {
            JAWTDrawingSurfaceInfo dsi = JAWT_DrawingSurface_GetDrawingSurfaceInfo(ds, ds.GetDrawingSurfaceInfo());

            JAWTX11DrawingSurfaceInfo dsiWin = JAWTX11DrawingSurfaceInfo.create(dsi.platformInfo());

            this.display = dsiWin.display();
            this.drawable = dsiWin.drawable();

            eglDisplay = eglGetDisplay(display);

            surface = eglCreateWindowSurface(eglDisplay,fbConfigs.get(0),drawable,(int[])null);

            eglMakeCurrent(eglDisplay,surface,surface,context);

            GLES.setCapabilities(glesCaps);

            JAWT_DrawingSurface_FreeDrawingSurfaceInfo(dsi, ds.FreeDrawingSurfaceInfo());
        }
        finally
        {
            unlock();
            System.err.printf("Unlock \n");
        }

    }
    catch (Exception e)
    {
        System.err.println("JAWT Failed" + e.getMessage());
    }
}

As You can see, I re-create the display and surface, but I use the previously created context for rendering, without needing to re-create it.如您所见,我重新创建了显示和表面,但我使用之前创建的上下文进行渲染,而无需重新创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM