视网膜显示器上的慢速滞后Javafx性能 [英] Slow laggy Javafx-Performance on Retina Display

查看:116
本文介绍了视网膜显示器上的慢速滞后Javafx性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法将像素写入画布'GrapicConte2D。一个int [] - pixelarray每帧随机化,并用PixelWriters setPixels() - 方法更新。

I have a simple method which writes pixels to a canvas' GrapicConte2D. A int[]-pixelarray is getting randomized every frame and updated with the PixelWriters setPixels()-Method.

我有一个双监视器设置,一个常规屏幕和macbook pro视网膜。将我的应用程序框架拖到我的常规屏幕时,一切都对我很好。但是把它放在我的MacBooks Retina显示器上它会变得非常迟钝。

I have a Dual-Monitor-setup, a "regular" screen and a macbook pro retina. When dragging my application frame into my "regular" screen everything works very fine for me. But placing it on my MacBooks Retina Displays it gets really laggy.

我真的不知道出了什么问题。我几次检查我的代码,但似乎我无法帮助自己。

I have really no idea what went wrong. I was checking my code a few times, but it seems that I cannot help myself.

我会非常感谢每一个可以帮助我的建议。

I would be very grateful for every advise that could help me.

谢谢

代码:

public class CanvasTest extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Random random = new Random();
        BorderPane root = new BorderPane();

        ScrollPane scrollPane = new ScrollPane();

        Canvas canvas = new Canvas(1280, 800);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        PixelWriter writer = gc.getPixelWriter();
        PixelFormat format = PixelFormat.getIntArgbInstance();

        int[] pixels = new int[(int) (canvas.getWidth() * canvas.getHeight())];



        scrollPane.setContent(canvas);

        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                for (int i = 0; i < pixels.length; i++) {
                    pixels[i] = (255 << 24) | random.nextInt(0xffffff);
                }


                writer.setPixels(0, 0, (int) canvas.getWidth(), (int) canvas.getHeight(),
                        format, pixels, 0, (int) canvas.getWidth());
            }
        };

        root.setCenter(scrollPane);
        primaryStage.setScene(new Scene(root,1280,800));
        primaryStage.show();

        timer.start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


推荐答案

我的最好的猜测是你的Retina显示器总共有更多的像素和更高的像素密度,因此每帧必须进行更多的计算。如果你没有设置使用 GraphicsContext2D ,我会研究OpenGL。

My best guess is that your Retina display has more pixels total and a higher pixel density and therefore more calculations have to be done per frame. If you are not set on using the GraphicsContext2D, I would look into OpenGL.

如果你仍想使用 GraphicsContext2D ,我会减少随机性样本,使其不那么迟钝。您可以为每个其他像素找到一个随机值,而不是为每个像素找到一个随机值,只需将附近的像素设置为该颜色即可。它会看起来稍差,但会减少滞后。同样,OpenGL使用GPU渲染并且速度会快得多。

If you still want to use the GraphicsContext2D, I would take less sample of randomness to make it less laggy. Instead of finding a random value for every pixel, you could find a random value for every other pixel, and just set the nearby pixels to that color as well. It would look slightly worse, but it would reduce lag. Again, OpenGL uses GPU rendering and will be much faster.

请尝试这样做:

for (int y = 0; y < height; y+=2) {
    for (int x = 0; x < width; x+=2) {
        int i = x + y * width;
        screen[i] = (255 << 24) | (random.nextInt(256) << 16) | (random.nextInt(256) << 8) | random.nextInt(256);
        screen[i+1] = screen[i]; // The pixel just to the right of the current one
        screen[i+width] = screen[i]; // The pixel just below the current one
        screen[i+width+1] = screen[i]; // The pixel one to the right and one below this one
    }
}

注意:这只有在宽度和高度可以被2整除时才有效。这就是为什么在这种情况下,使用很多像素,更容易使用GPU。

Note: This will only work when the width and height are divisible by 2. That is why in this case, with many pixels, it is much easier to use the GPU instead.

这篇关于视网膜显示器上的慢速滞后Javafx性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆